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