1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName // Needed for WP_Block_Cloner helper class.
2/**
3 * Server-side rendering of the `core/block` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/block` block on server.
10 *
11 * @since 5.0.0
12 *
13 * @global WP_Embed $wp_embed
14 *
15 * @param array $attributes The block attributes.
16 *
17 * @return string Rendered HTML of the referenced block.
18 */
19function render_block_core_block( $attributes, $content, $block_instance ) {
20 static $seen_refs = array();
21
22 if ( empty( $attributes['ref'] ) ) {
23 return '';
24 }
25
26 $reusable_block = get_post( $attributes['ref'] );
27 if ( ! $reusable_block || 'wp_block' !== $reusable_block->post_type ) {
28 return '';
29 }
30
31 if ( isset( $seen_refs[ $attributes['ref'] ] ) ) {
32 // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
33 // is set in `wp_debug_mode()`.
34 $is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
35
36 return $is_debug ?
37 // translators: Visible only in the front end, this warning takes the place of a faulty block.
38 __( '[block rendering halted]' ) :
39 '';
40 }
41
42 if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) {
43 return '';
44 }
45
46 $seen_refs[ $attributes['ref'] ] = true;
47
48 // Handle embeds for reusable blocks.
49 global $wp_embed;
50 $content = $wp_embed->run_shortcode( $reusable_block->post_content );
51 $content = $wp_embed->autoembed( $content );
52
53 // Back compat.
54 // For blocks that have not been migrated in the editor, add some back compat
55 // so that front-end rendering continues to work.
56
57 // This matches the `v2` deprecation. Removes the inner `values` property
58 // from every item.
59 if ( isset( $attributes['content'] ) ) {
60 foreach ( $attributes['content'] as &$content_data ) {
61 if ( isset( $content_data['values'] ) ) {
62 $is_assoc_array = is_array( $content_data['values'] ) && ! wp_is_numeric_array( $content_data['values'] );
63
64 if ( $is_assoc_array ) {
65 $content_data = $content_data['values'];
66 }
67 }
68 }
69 }
70
71 // This matches the `v1` deprecation. Rename `overrides` to `content`.
72 if ( isset( $attributes['overrides'] ) && ! isset( $attributes['content'] ) ) {
73 $attributes['content'] = $attributes['overrides'];
74 }
75
76 // Apply Block Hooks.
77 $content = apply_block_hooks_to_content_from_post_object( $content, $reusable_block );
78
79 /**
80 * We attach the blocks from $content as inner blocks to the Synced Pattern block instance.
81 * This ensures that block context available to the Synced Pattern block instance is provided to
82 * those blocks.
83 */
84 $block_instance->parsed_block['innerBlocks'] = parse_blocks( $content );
85 $block_instance->parsed_block['innerContent'] = array_fill( 0, count( $block_instance->parsed_block['innerBlocks'] ), null );
86 if ( method_exists( $block_instance, 'refresh_context_dependents' ) ) {
87 // WP_Block::refresh_context_dependents() was introduced in WordPress 6.8.
88 $block_instance->refresh_context_dependents();
89 } else {
90 // This branch can be removed once Gutenberg requires WordPress 6.8 or later.
91 if ( ! class_exists( 'WP_Block_Cloner' ) ) {
92 // phpcs:ignore Gutenberg.Commenting.SinceTag.MissingClassSinceTag
93 class WP_Block_Cloner extends WP_Block {
94 /**
95 * Static methods of subclasses have access to protected properties
96 * of instances of the parent class.
97 * In this case, this gives us access to `available_context` and `registry`.
98 */
99 // phpcs:ignore Gutenberg.Commenting.SinceTag.MissingMethodSinceTag
100 public static function clone_instance( $instance ) {
101 return new WP_Block(
102 $instance->parsed_block,
103 $instance->available_context,
104 $instance->registry
105 );
106 }
107 }
108 }
109 $block_instance = WP_Block_Cloner::clone_instance( $block_instance );
110 }
111
112 $content = $block_instance->render( array( 'dynamic' => false ) );
113 unset( $seen_refs[ $attributes['ref'] ] );
114
115 return $content;
116}
117
118/**
119 * Registers the `core/block` block.
120 *
121 * @since 5.3.0
122 */
123function register_block_core_block() {
124 register_block_type_from_metadata(
125 __DIR__ . '/block',
126 array(
127 'render_callback' => 'render_block_core_block',
128 )
129 );
130}
131add_action( 'init', 'register_block_core_block' );
132