| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for features present in Gutenberg, pending |
| 4 |
* upstream commit to the WordPress core source repository. Functions here |
| 5 |
* exist only as long as necessary for corresponding WordPress support, and |
| 6 |
* each should be associated with a Trac ticket. |
| 7 |
* |
| 8 |
* @package gutenberg |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Filters allowed CSS attributes to include `flex-basis`, included in saved |
| 13 |
* markup of the Column block. |
| 14 |
* |
| 15 |
* This can be removed when plugin support requires WordPress 5.3.0+. |
| 16 |
* |
| 17 |
* @see https://core.trac.wordpress.org/ticket/47281 |
| 18 |
* @see https://core.trac.wordpress.org/changeset/45363 |
| 19 |
* |
| 20 |
* @since 5.7.0 |
| 21 |
* |
| 22 |
* @param string[] $attr Array of allowed CSS attributes. |
| 23 |
* |
| 24 |
* @return string[] Filtered array of allowed CSS attributes. |
| 25 |
*/ |
| 26 |
function gutenberg_safe_style_css_column_flex_basis( $attr ) { |
| 27 |
$attr[] = 'flex-basis'; |
| 28 |
|
| 29 |
return $attr; |
| 30 |
} |
| 31 |
add_filter( 'safe_style_css', 'gutenberg_safe_style_css_column_flex_basis' ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Shim that hooks into `pre_render_block` so as to override `render_block` |
| 35 |
* with a function that passes `render_callback` the block object as the |
| 36 |
* argument. |
| 37 |
* |
| 38 |
* @param string $pre_render The pre-rendered content. Default null. |
| 39 |
* @param array $block The block being rendered. |
| 40 |
* |
| 41 |
* @return string String of rendered HTML. |
| 42 |
*/ |
| 43 |
function gutenberg_provide_render_callback_with_block_object( $pre_render, $block ) { |
| 44 |
global $post; |
| 45 |
|
| 46 |
$source_block = $block; |
| 47 |
|
| 48 |
/** This filter is documented in src/wp-includes/blocks.php */ |
| 49 |
$block = apply_filters( 'render_block_data', $block, $source_block ); |
| 50 |
|
| 51 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 52 |
$is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); |
| 53 |
$block_content = ''; |
| 54 |
$index = 0; |
| 55 |
|
| 56 |
foreach ( $block['innerContent'] as $chunk ) { |
| 57 |
$block_content .= is_string( $chunk ) ? $chunk : render_block( $block['innerBlocks'][ $index++ ] ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! is_array( $block['attrs'] ) ) { |
| 61 |
$block['attrs'] = array(); |
| 62 |
} |
| 63 |
|
| 64 |
if ( $is_dynamic ) { |
| 65 |
$global_post = $post; |
| 66 |
|
| 67 |
$prepared_attributes = $block_type->prepare_attributes_for_render( $block['attrs'] ); |
| 68 |
$block_content = (string) call_user_func( $block_type->render_callback, $prepared_attributes, $block_content, $block ); |
| 69 |
|
| 70 |
$post = $global_post; |
| 71 |
} |
| 72 |
|
| 73 |
/** This filter is documented in src/wp-includes/blocks.php */ |
| 74 |
return apply_filters( 'render_block', $block_content, $block ); |
| 75 |
} |
| 76 |
add_filter( 'pre_render_block', 'gutenberg_provide_render_callback_with_block_object', 10, 2 ); |
| 77 |
|
| 78 |
/** |
| 79 |
* Sets the current post for usage in template blocks. |
| 80 |
* |
| 81 |
* @return WP_Post|null The post if any, or null otherwise. |
| 82 |
*/ |
| 83 |
function gutenberg_get_post_from_context() { |
| 84 |
// TODO: Without this temporary fix, an infinite loop can occur where |
| 85 |
// posts with post content blocks render themselves recursively. |
| 86 |
if ( is_admin() || defined( 'REST_REQUEST' ) ) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
if ( ! in_the_loop() ) { |
| 90 |
rewind_posts(); |
| 91 |
the_post(); |
| 92 |
} |
| 93 |
return get_post(); |
| 94 |
} |
| 95 |
|