| @@ -5,8 +5,20 @@ | ||
| 5 | 5 | * @package gutenberg |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * Get the class name used on block level presets. | |
| 10 | + * | |
| 11 | + * @access private | |
| 12 | + * | |
| 13 | + * @param array $block Block object. | |
| 14 | + * @return string The unique class name. | |
| 15 | + */ | |
| 16 | +function _gutenberg_get_presets_class_name( $block ) { | |
| 17 | + return 'wp-settings-' . md5( serialize( $block ) ); | |
| 18 | +} | |
| 19 | + | |
| 20 | +/** | |
| 9 | 21 | * Update the block content with block level presets class name. |
| 10 | 22 | * |
| 11 | 23 | * @access private |
| 12 | 24 | * |
| @@ -25,21 +37,36 @@ | ||
| 25 | 37 | return $block_content; |
| 26 | 38 | } |
| 27 | 39 | |
| 28 | 40 | // return early if no settings are found on the block attributes. |
| 29 | - $block_settings = $block['attrs']['settings'] ?? null; | |
| 41 | + $block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null ); | |
| 30 | 42 | if ( empty( $block_settings ) ) { |
| 31 | 43 | return $block_content; |
| 32 | 44 | } |
| 33 | 45 | |
| 46 | + $class_name = _gutenberg_get_presets_class_name( $block ); | |
| 47 | + | |
| 34 | 48 | // Like the layout hook this assumes the hook only applies to blocks with a single wrapper. |
| 35 | - // Add the class name to the first element, presuming it's the wrapper, if it exists. | |
| 36 | - $tags = new WP_HTML_Tag_Processor( $block_content ); | |
| 37 | - if ( $tags->next_tag() ) { | |
| 38 | - $tags->add_class( _wp_get_presets_class_name( $block ) ); | |
| 49 | + // Retrieve the opening tag of the first HTML element. | |
| 50 | + $html_element_matches = array(); | |
| 51 | + preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE ); | |
| 52 | + $first_element = $html_element_matches[0][0]; | |
| 53 | + // If the first HTML element has a class attribute just add the new class | |
| 54 | + // as we do on layout and duotone. | |
| 55 | + if ( strpos( $first_element, 'class="' ) !== false ) { | |
| 56 | + $content = preg_replace( | |
| 57 | + '/' . preg_quote( 'class="', '/' ) . '/', | |
| 58 | + 'class="' . $class_name . ' ', | |
| 59 | + $block_content, | |
| 60 | + 1 | |
| 61 | + ); | |
| 62 | + } else { | |
| 63 | + // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end. | |
| 64 | + $first_element_offset = $html_element_matches[0][1]; | |
| 65 | + $content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 ); | |
| 39 | 66 | } |
| 40 | 67 | |
| 41 | - return $tags->get_updated_html(); | |
| 68 | + return $content; | |
| 42 | 69 | } |
| 43 | 70 | |
| 44 | 71 | /** |
| 45 | 72 | * Render the block level presets stylesheet. |
| @@ -58,14 +85,14 @@ | ||
| 58 | 85 | return null; |
| 59 | 86 | } |
| 60 | 87 | |
| 61 | 88 | // return early if no settings are found on the block attributes. |
| 62 | - $block_settings = $block['attrs']['settings'] ?? null; | |
| 89 | + $block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null ); | |
| 63 | 90 | if ( empty( $block_settings ) ) { |
| 64 | 91 | return null; |
| 65 | 92 | } |
| 66 | 93 | |
| 67 | - $class_name = '.' . _wp_get_presets_class_name( $block ); | |
| 94 | + $class_name = '.' . _gutenberg_get_presets_class_name( $block ); | |
| 68 | 95 | |
| 69 | 96 | // the root selector for preset variables needs to target every possible block selector |
| 70 | 97 | // in order for the general setting to override any bock specific setting of a parent block or |
| 71 | 98 | // the site root. |
| @@ -72,19 +99,16 @@ | ||
| 72 | 99 | $variables_root_selector = '*,[class*="wp-block"]'; |
| 73 | 100 | $registry = WP_Block_Type_Registry::get_instance(); |
| 74 | 101 | $blocks = $registry->get_all_registered(); |
| 75 | 102 | foreach ( $blocks as $block_type ) { |
| 76 | - // We only want to append selectors for block's using custom selectors | |
| 77 | - // i.e. not `wp-block-<name>`. | |
| 78 | - $has_custom_selector = | |
| 79 | - ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) || | |
| 80 | - ( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) ); | |
| 81 | - | |
| 82 | - if ( $has_custom_selector ) { | |
| 83 | - $variables_root_selector .= ',' . wp_get_block_css_selector( $block_type ); | |
| 103 | + if ( | |
| 104 | + isset( $block_type->supports['__experimentalSelector'] ) && | |
| 105 | + is_string( $block_type->supports['__experimentalSelector'] ) | |
| 106 | + ) { | |
| 107 | + $variables_root_selector .= ',' . $block_type->supports['__experimentalSelector']; | |
| 84 | 108 | } |
| 85 | 109 | } |
| 86 | - $variables_root_selector = WP_Theme_JSON_Gutenberg::scope_selector( $class_name, $variables_root_selector ); | |
| 110 | + $variables_root_selector = WP_Theme_JSON_6_1::scope_selector( $class_name, $variables_root_selector ); | |
| 87 | 111 | |
| 88 | 112 | // Remove any potentially unsafe styles. |
| 89 | 113 | $theme_json_shape = WP_Theme_JSON_Gutenberg::remove_insecure_properties( |
| 90 | 114 | array( |
| @@ -105,9 +129,9 @@ | ||
| 105 | 129 | 'scope' => $class_name, |
| 106 | 130 | ) |
| 107 | 131 | ); |
| 108 | 132 | |
| 109 | - // include preset css classes on the stylesheet. | |
| 133 | + // include preset css classes on the the stylesheet. | |
| 110 | 134 | $styles .= $theme_json_object->get_stylesheet( |
| 111 | 135 | array( 'presets' ), |
| 112 | 136 | null, |
| 113 | 137 | array( |
| @@ -116,19 +140,12 @@ | ||
| 116 | 140 | ) |
| 117 | 141 | ); |
| 118 | 142 | |
| 119 | 143 | if ( ! empty( $styles ) ) { |
| 120 | - /* | |
| 121 | - * This method is deprecated since WordPress 6.2. | |
| 122 | - * We could enqueue these styles separately, | |
| 123 | - * or print them out with other settings presets. | |
| 124 | - */ | |
| 125 | - wp_enqueue_block_support_styles( $styles ); | |
| 144 | + gutenberg_enqueue_block_support_styles( $styles ); | |
| 126 | 145 | } |
| 127 | 146 | |
| 128 | 147 | return null; |
| 129 | 148 | } |
| 130 | -// Remove WordPress core filter to avoid rendering duplicate settings style blocks. | |
| 131 | -remove_filter( 'render_block', '_wp_add_block_level_presets_class', 10 ); | |
| 132 | -remove_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10 ); | |
| 149 | + | |
| 133 | 150 | add_filter( 'render_block', '_gutenberg_add_block_level_presets_class', 10, 2 ); |
| 134 | 151 | add_filter( 'pre_render_block', '_gutenberg_add_block_level_preset_styles', 10, 2 ); |