[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Block level presets support. 4 * 5 * @package WordPress 6 * @since 6.2.0 7 */ 8 9 /** 10 * Get the class name used on block level presets. 11 * 12 * @internal 13 * 14 * @since 6.2.0 15 * @access private 16 * 17 * @param array $block Block object. 18 * @return string The unique class name. 19 */ 20 function _wp_get_presets_class_name( $block ) { 21 return 'wp-settings-' . md5( serialize( $block ) ); 22 } 23 24 /** 25 * Update the block content with block level presets class name. 26 * 27 * @internal 28 * 29 * @since 6.2.0 30 * @access private 31 * 32 * @param string $block_content Rendered block content. 33 * @param array $block Block object. 34 * @return string Filtered block content. 35 */ 36 function _wp_add_block_level_presets_class( $block_content, $block ) { 37 if ( ! $block_content ) { 38 return $block_content; 39 } 40 41 // return early if the block doesn't have support for settings. 42 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 43 if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) { 44 return $block_content; 45 } 46 47 // return early if no settings are found on the block attributes. 48 $block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null ); 49 if ( empty( $block_settings ) ) { 50 return $block_content; 51 } 52 53 // Like the layout hook this assumes the hook only applies to blocks with a single wrapper. 54 // Add the class name to the first element, presuming it's the wrapper, if it exists. 55 $tags = new WP_HTML_Tag_Processor( $block_content ); 56 if ( $tags->next_tag() ) { 57 $tags->add_class( _wp_get_presets_class_name( $block ) ); 58 } 59 60 return $tags->get_updated_html(); 61 } 62 63 /** 64 * Render the block level presets stylesheet. 65 * 66 * @internal 67 * 68 * @since 6.2.0 69 * @access private 70 * 71 * @param string|null $pre_render The pre-rendered content. Default null. 72 * @param array $block The block being rendered. 73 * 74 * @return null 75 */ 76 function _wp_add_block_level_preset_styles( $pre_render, $block ) { 77 // Return early if the block has not support for descendent block styles. 78 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 79 if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) { 80 return null; 81 } 82 83 // return early if no settings are found on the block attributes. 84 $block_settings = _wp_array_get( $block, array( 'attrs', 'settings' ), null ); 85 if ( empty( $block_settings ) ) { 86 return null; 87 } 88 89 $class_name = '.' . _wp_get_presets_class_name( $block ); 90 91 // the root selector for preset variables needs to target every possible block selector 92 // in order for the general setting to override any bock specific setting of a parent block or 93 // the site root. 94 $variables_root_selector = '*,[class*="wp-block"]'; 95 $registry = WP_Block_Type_Registry::get_instance(); 96 $blocks = $registry->get_all_registered(); 97 foreach ( $blocks as $block_type ) { 98 if ( 99 isset( $block_type->supports['__experimentalSelector'] ) && 100 is_string( $block_type->supports['__experimentalSelector'] ) 101 ) { 102 $variables_root_selector .= ',' . $block_type->supports['__experimentalSelector']; 103 } 104 } 105 $variables_root_selector = WP_Theme_JSON::scope_selector( $class_name, $variables_root_selector ); 106 107 // Remove any potentially unsafe styles. 108 $theme_json_shape = WP_Theme_JSON::remove_insecure_properties( 109 array( 110 'version' => WP_Theme_JSON::LATEST_SCHEMA, 111 'settings' => $block_settings, 112 ) 113 ); 114 $theme_json_object = new WP_Theme_JSON( $theme_json_shape ); 115 116 $styles = ''; 117 118 // include preset css variables declaration on the stylesheet. 119 $styles .= $theme_json_object->get_stylesheet( 120 array( 'variables' ), 121 null, 122 array( 123 'root_selector' => $variables_root_selector, 124 'scope' => $class_name, 125 ) 126 ); 127 128 // include preset css classes on the the stylesheet. 129 $styles .= $theme_json_object->get_stylesheet( 130 array( 'presets' ), 131 null, 132 array( 133 'root_selector' => $class_name . ',' . $class_name . ' *', 134 'scope' => $class_name, 135 ) 136 ); 137 138 if ( ! empty( $styles ) ) { 139 wp_enqueue_block_support_styles( $styles ); 140 } 141 142 return null; 143 } 144 145 add_filter( 'render_block', '_wp_add_block_level_presets_class', 10, 2 ); 146 add_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10, 2 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sun Jun 4 08:20:02 2023 | Cross-referenced by PHPXref |