| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/pattern` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the `core/pattern` block on the server. |
| 10 |
* |
| 11 |
* @since 5.9.0 |
| 12 |
*/ |
| 13 |
function gutenberg_register_block_core_pattern() { |
| 14 |
register_block_type_from_metadata( |
| 15 |
__DIR__ . '/pattern', |
| 16 |
array( |
| 17 |
'render_callback' => 'gutenberg_render_block_core_pattern', |
| 18 |
) |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Renders the `core/pattern` block on the server. |
| 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 |
* |
| 29 |
* @param array $attributes Block attributes. |
| 30 |
* |
| 31 |
* @return string Returns the output of the pattern. |
| 32 |
*/ |
| 33 |
function gutenberg_render_block_core_pattern( $attributes ) { |
| 34 |
static $seen_refs = array(); |
| 35 |
|
| 36 |
if ( empty( $attributes['slug'] ) ) { |
| 37 |
return ''; |
| 38 |
} |
| 39 |
|
| 40 |
$slug = $attributes['slug']; |
| 41 |
$registry = WP_Block_Patterns_Registry::get_instance(); |
| 42 |
|
| 43 |
if ( ! $registry->is_registered( $slug ) ) { |
| 44 |
return ''; |
| 45 |
} |
| 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 |
|
| 58 |
$pattern = $registry->get_registered( $slug ); |
| 59 |
$content = $pattern['content']; |
| 60 |
|
| 61 |
$seen_refs[ $attributes['slug'] ] = true; |
| 62 |
|
| 63 |
$content = do_blocks( $content ); |
| 64 |
|
| 65 |
global $wp_embed; |
| 66 |
$content = $wp_embed->autoembed( $content ); |
| 67 |
|
| 68 |
unset( $seen_refs[ $attributes['slug'] ] ); |
| 69 |
return $content; |
| 70 |
} |
| 71 |
|
| 72 |
add_action( 'init', 'gutenberg_register_block_core_pattern', 20 ); |
| 73 |
|