PluginProbe
Gutenberg / 23.5.2
Gutenberg v23.5.2
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 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / block.php

block.php in Gutenberg 23.5.2, at build/scripts/block-library/block.php

108 lines 3.1 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, $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 $block_instance->refresh_context_dependents();
87
88 $content = $block_instance->render( array( 'dynamic' => false ) );
89 unset( $seen_refs[ $attributes['ref'] ] );
90
91 return $content;
92 }
93
94 /**
95 * Registers the `core/block` block.
96 *
97 * @since 5.3.0
98 */
99 function gutenberg_register_block_core_block() {
100 register_block_type_from_metadata(
101 __DIR__ . '/block',
102 array(
103 'render_callback' => 'gutenberg_render_block_core_block',
104 )
105 );
106 }
107 add_action( 'init', 'gutenberg_register_block_core_block', 20 );
108