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 / lib / compat / wordpress-7.1 / block-bindings.php

block-bindings.php in Gutenberg 23.5.2, at lib/compat/wordpress-7.1/block-bindings.php

102 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block bindings additions for WordPress 7.1.
4 *
5 * @since 7.1.0
6 * @package gutenberg
7 * @subpackage Block Bindings
8 */
9
10 // The following filter can be removed once the minimum required WordPress version is 7.1 or newer.
11 add_filter(
12 'block_bindings_supported_attributes',
13 function ( $attributes, $block_type ) {
14 if ( 'core/list-item' === $block_type && ! in_array( 'content', $attributes, true ) ) {
15 $attributes[] = 'content';
16 }
17 return $attributes;
18 },
19 10,
20 2
21 );
22
23 /*
24 * On WordPress versions before 7.1, `WP_Block::replace_html()` lacks the
25 * inner-blocks fix (wordpress-develop#12113), so binding a List Item's content
26 * replaces the whole `<li>` and drops the nested List rendered inside it. Core
27 * still has the inner blocks on the instance, so re-append them after the
28 * binding has been applied.
29 *
30 * This can be removed once the minimum required WordPress version is 7.1.
31 */
32
33 /**
34 * Returns whether the list item metadata declares a content binding.
35 *
36 * @since 7.1.0
37 *
38 * @param mixed $metadata Block metadata attribute.
39 * @return bool Whether a content binding (direct or via pattern overrides) is present.
40 */
41 function gutenberg_list_item_metadata_has_content_binding( $metadata ) {
42 if ( empty( $metadata['bindings'] ) || ! is_array( $metadata['bindings'] ) ) {
43 return false;
44 }
45
46 $bindings = $metadata['bindings'];
47 return isset( $bindings['content'] ) ||
48 (
49 isset( $bindings['__default']['source'] ) &&
50 'core/pattern-overrides' === $bindings['__default']['source']
51 );
52 }
53
54 /**
55 * Restores the nested inner blocks dropped when binding a List Item's content.
56 *
57 * Core replaces the whole `<li>` when it applies the rich-text binding, removing
58 * the nested List. The inner blocks are still available on the instance, so they
59 * are rendered again and re-appended before the closing `</li>`.
60 *
61 * @since 7.1.0
62 *
63 * @param string $block_content The rendered block content.
64 * @param array $parsed_block The parsed block.
65 * @param WP_Block $instance The block instance.
66 * @return string The block content with the nested inner blocks restored.
67 */
68 function gutenberg_restore_list_item_inner_blocks_after_binding( $block_content, $parsed_block, $instance ) {
69 if ( 'core/list-item' !== ( $parsed_block['blockName'] ?? null ) ) {
70 return $block_content;
71 }
72
73 if ( ! $instance instanceof WP_Block || empty( $instance->inner_blocks ) ) {
74 return $block_content;
75 }
76
77 if ( ! gutenberg_list_item_metadata_has_content_binding( $instance->parsed_block['attrs']['metadata'] ?? null ) ) {
78 return $block_content;
79 }
80
81 $inner_blocks_html = '';
82 foreach ( $instance->inner_blocks as $inner_block ) {
83 $inner_blocks_html .= $inner_block->render();
84 }
85
86 /*
87 * Nothing to restore when the inner blocks did not render, or when they are
88 * still present because Core already preserves them (the fix is in place).
89 */
90 if ( '' === $inner_blocks_html || str_contains( $block_content, $inner_blocks_html ) ) {
91 return $block_content;
92 }
93
94 $closer_position = strripos( $block_content, '</li>' );
95 if ( false === $closer_position ) {
96 return $block_content;
97 }
98
99 return substr( $block_content, 0, $closer_position ) . $inner_blocks_html . substr( $block_content, $closer_position );
100 }
101 add_filter( 'render_block', 'gutenberg_restore_list_item_inner_blocks_after_binding', 10, 3 );
102