wp/wp-includes/blocks/pattern.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  *  Registers the `core/pattern` block on the server.
     9  *  Registers the `core/pattern` block on the server.
    10  *
    10  *
    11  * @return void
    11  * @since 5.9.0
    12  */
    12  */
    13 function register_block_core_pattern() {
    13 function register_block_core_pattern() {
    14 	register_block_type_from_metadata(
    14 	register_block_type_from_metadata(
    15 		__DIR__ . '/pattern',
    15 		__DIR__ . '/pattern',
    16 		array(
    16 		array(
    20 }
    20 }
    21 
    21 
    22 /**
    22 /**
    23  * Renders the `core/pattern` block on the server.
    23  * Renders the `core/pattern` block on the server.
    24  *
    24  *
       
    25  * @since 6.3.0 Backwards compatibility: blocks with no `syncStatus` attribute do not receive block wrapper.
       
    26  *
       
    27  * @global WP_Embed $wp_embed Used to process embedded content within patterns
       
    28  *
    25  * @param array $attributes Block attributes.
    29  * @param array $attributes Block attributes.
    26  *
    30  *
    27  * @return string Returns the output of the pattern.
    31  * @return string Returns the output of the pattern.
    28  */
    32  */
    29 function render_block_core_pattern( $attributes ) {
    33 function render_block_core_pattern( $attributes ) {
       
    34 	static $seen_refs = array();
       
    35 
    30 	if ( empty( $attributes['slug'] ) ) {
    36 	if ( empty( $attributes['slug'] ) ) {
    31 		return '';
    37 		return '';
    32 	}
    38 	}
    33 
    39 
    34 	$slug     = $attributes['slug'];
    40 	$slug     = $attributes['slug'];
    35 	$registry = WP_Block_Patterns_Registry::get_instance();
    41 	$registry = WP_Block_Patterns_Registry::get_instance();
       
    42 
    36 	if ( ! $registry->is_registered( $slug ) ) {
    43 	if ( ! $registry->is_registered( $slug ) ) {
    37 		return '';
    44 		return '';
    38 	}
    45 	}
    39 
    46 
       
    47 	if ( isset( $seen_refs[ $attributes['slug'] ] ) ) {
       
    48 		// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
       
    49 		// is set in `wp_debug_mode()`.
       
    50 		$is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
       
    51 
       
    52 		return $is_debug ?
       
    53 			// translators: Visible only in the front end, this warning takes the place of a faulty block. %s represents a pattern's slug.
       
    54 			sprintf( __( '[block rendering halted for pattern "%s"]' ), $slug ) :
       
    55 			'';
       
    56 	}
       
    57 
    40 	$pattern = $registry->get_registered( $slug );
    58 	$pattern = $registry->get_registered( $slug );
    41 	return do_blocks( $pattern['content'] );
    59 	$content = $pattern['content'];
       
    60 
       
    61 	// Backward compatibility for handling Block Hooks and injecting the theme attribute in the Gutenberg plugin.
       
    62 	// This can be removed when the minimum supported WordPress is >= 6.4.
       
    63 	if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && ! function_exists( 'traverse_and_serialize_blocks' ) ) {
       
    64 		$blocks  = parse_blocks( $content );
       
    65 		$content = gutenberg_serialize_blocks( $blocks );
       
    66 	}
       
    67 
       
    68 	$seen_refs[ $attributes['slug'] ] = true;
       
    69 
       
    70 	$content = do_blocks( $content );
       
    71 
       
    72 	global $wp_embed;
       
    73 	$content = $wp_embed->autoembed( $content );
       
    74 
       
    75 	unset( $seen_refs[ $attributes['slug'] ] );
       
    76 	return $content;
    42 }
    77 }
    43 
    78 
    44 add_action( 'init', 'register_block_core_pattern' );
    79 add_action( 'init', 'register_block_core_pattern' );