PluginProbe
Gutenberg / 20.0.1
Gutenberg v20.0.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / block.php

block.php in Gutenberg 20.0.1, at build/block-library/blocks/block.php

134 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @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 */
19 function gutenberg_render_block_core_block( $attributes ) {
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 /**
77 * We set the `pattern/overrides` context through the `render_block_context`
78 * filter so that it is available when a pattern's inner blocks are
79 * rendering via do_blocks given it only receives the inner content.
80 */
81 $has_pattern_overrides = isset( $attributes['content'] ) && null !== get_block_bindings_source( 'core/pattern-overrides' );
82 if ( $has_pattern_overrides ) {
83 $filter_block_context = static function ( $context ) use ( $attributes ) {
84 $context['pattern/overrides'] = $attributes['content'];
85 return $context;
86 };
87 add_filter( 'render_block_context', $filter_block_context, 1 );
88 }
89
90 $ignored_hooked_blocks = get_post_meta( $attributes['ref'], '_wp_ignored_hooked_blocks', true );
91 if ( ! empty( $ignored_hooked_blocks ) ) {
92 $ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
93 $attributes['metadata'] = array(
94 'ignoredHookedBlocks' => $ignored_hooked_blocks,
95 );
96 }
97
98 // Wrap in "Block" block so the Block Hooks algorithm can insert blocks
99 // that are hooked as first or last child of `core/block`.
100 $content = get_comment_delimited_block_content(
101 'core/block',
102 $attributes,
103 $content
104 );
105 // Apply Block Hooks.
106 $content = apply_block_hooks_to_content( $content, $reusable_block );
107 // Remove block wrapper.
108 $content = remove_serialized_parent_block( $content );
109
110 $content = do_blocks( $content );
111 unset( $seen_refs[ $attributes['ref'] ] );
112
113 if ( $has_pattern_overrides ) {
114 remove_filter( 'render_block_context', $filter_block_context, 1 );
115 }
116
117 return $content;
118 }
119
120 /**
121 * Registers the `core/block` block.
122 *
123 * @since 5.3.0
124 */
125 function gutenberg_register_block_core_block() {
126 register_block_type_from_metadata(
127 __DIR__ . '/block',
128 array(
129 'render_callback' => 'gutenberg_render_block_core_block',
130 )
131 );
132 }
133 add_action( 'init', 'gutenberg_register_block_core_block', 20 );
134