block.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/block` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/block` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Rendered HTML of the referenced block.
  13. */
  14. function render_block_core_block( $attributes ) {
  15. static $seen_refs = array();
  16. if ( empty( $attributes['ref'] ) ) {
  17. return '';
  18. }
  19. $reusable_block = get_post( $attributes['ref'] );
  20. if ( ! $reusable_block || 'wp_block' !== $reusable_block->post_type ) {
  21. return '';
  22. }
  23. if ( isset( $seen_refs[ $attributes['ref'] ] ) ) {
  24. // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
  25. // is set in `wp_debug_mode()`.
  26. $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG &&
  27. defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
  28. return $is_debug ?
  29. // translators: Visible only in the front end, this warning takes the place of a faulty block.
  30. __( '[block rendering halted]' ) :
  31. '';
  32. }
  33. if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) {
  34. return '';
  35. }
  36. $seen_refs[ $attributes['ref'] ] = true;
  37. // Handle embeds for reusable blocks.
  38. global $wp_embed;
  39. $content = $wp_embed->run_shortcode( $reusable_block->post_content );
  40. $content = $wp_embed->autoembed( $content );
  41. $content = do_blocks( $content );
  42. unset( $seen_refs[ $attributes['ref'] ] );
  43. return $content;
  44. }
  45. /**
  46. * Registers the `core/block` block.
  47. */
  48. function register_block_core_block() {
  49. register_block_type_from_metadata(
  50. __DIR__ . '/block',
  51. array(
  52. 'render_callback' => 'render_block_core_block',
  53. )
  54. );
  55. }
  56. add_action( 'init', 'register_block_core_block' );