Changeset 56226
- Timestamp:
- 07/13/2023 11:32:19 AM (18 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/duotone.php
r56101 r56226 41 41 ); 42 42 43 // Set up metadata prior to rendering any blocks.44 add_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_styles_presets' ), 10 );45 add_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_style_block_names' ), 10 );46 47 43 // Add classnames to blocks using duotone support. 48 add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 2);44 add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 3 ); 49 45 50 46 // Enqueue styles. -
trunk/src/wp-includes/class-wp-duotone.php
r56225 r56226 55 55 * @var array 56 56 */ 57 private static $global_styles_block_names = array();57 private static $global_styles_block_names; 58 58 59 59 /** … … 79 79 * @var array 80 80 */ 81 private static $global_styles_presets = array();81 private static $global_styles_presets; 82 82 83 83 /** … … 576 576 $filter_id = self::get_filter_id( $slug ); 577 577 578 return array_key_exists( $filter_id, self:: $global_styles_presets);578 return array_key_exists( $filter_id, self::get_all_global_styles_presets() ); 579 579 } 580 580 … … 898 898 */ 899 899 private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) { 900 if ( ! array_key_exists( $filter_id, self::$global_styles_presets ) ) { 900 $global_styles_presets = self::get_all_global_styles_presets(); 901 if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) { 901 902 $error_message = sprintf( 902 903 /* translators: %s: duotone filter ID */ … … 907 908 return; 908 909 } 909 self::$used_global_styles_presets[ $filter_id ] = self::$global_styles_presets[ $filter_id ];910 self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, self::$global_styles_presets[ $filter_id ] );910 self::$used_global_styles_presets[ $filter_id ] = $global_styles_presets[ $filter_id ]; 911 self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $global_styles_presets[ $filter_id ] ); 911 912 } 912 913 … … 921 922 */ 922 923 public static function register_duotone_support( $block_type ) { 923 $has_duotone_support = false; 924 if ( property_exists( $block_type, 'supports' ) ) { 925 /* 926 * Previous `color.__experimentalDuotone` support flag is migrated 927 * to `filter.duotone` via `block_type_metadata_settings` filter. 928 */ 929 $has_duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), null ); 930 } 931 932 if ( $has_duotone_support ) { 924 /* 925 * Previous `color.__experimentalDuotone` support flag is migrated 926 * to `filter.duotone` via `block_type_metadata_settings` filter. 927 */ 928 if ( block_has_support( $block_type, array( 'filter', 'duotone' ), null ) ) { 933 929 if ( ! $block_type->attributes ) { 934 930 $block_type->attributes = array(); … … 949 945 * if `filter.duotone` support is not defined. 950 946 * 951 * @param string $block_name The block name. 952 * 953 * @internal 954 * 955 * @since 6.3.0 956 * 957 * @return string The CSS selector or null if there is no support. 958 */ 959 private static function get_selector( $block_name ) { 960 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); 961 962 if ( $block_type && property_exists( $block_type, 'supports' ) ) { 963 /* 964 * Backwards compatibility with `supports.color.__experimentalDuotone` 965 * is provided via the `block_type_metadata_settings` filter. If 966 * `supports.filter.duotone` has not been set and the experimental 967 * property has been, the experimental property value is copied into 968 * `supports.filter.duotone`. 969 */ 970 $duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false ); 971 if ( ! $duotone_support ) { 972 return null; 973 } 974 975 /* 976 * If the experimental duotone support was set, that value is to be 977 * treated as a selector and requires scoping. 978 */ 979 $experimental_duotone = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false ); 980 if ( $experimental_duotone ) { 981 $root_selector = wp_get_block_css_selector( $block_type ); 982 return is_string( $experimental_duotone ) 983 ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone ) 984 : $root_selector; 985 } 986 987 // Regular filter.duotone support uses filter.duotone selectors with fallbacks. 988 return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true ); 989 } 947 * @internal 948 * @since 6.3.0 949 * 950 * @param WP_Block_Type $block_type Block type to check for support. 951 * @return string|null The CSS selector or null if there is no support. 952 */ 953 private static function get_selector( $block_type ) { 954 if ( ! ( $block_type instanceof WP_Block_Type ) ) { 955 return null; 956 } 957 958 /* 959 * Backward compatibility with `supports.color.__experimentalDuotone` 960 * is provided via the `block_type_metadata_settings` filter. If 961 * `supports.filter.duotone` has not been set and the experimental 962 * property has been, the experimental property value is copied into 963 * `supports.filter.duotone`. 964 */ 965 $duotone_support = block_has_support( $block_type, array( 'filter', 'duotone' ) ); 966 if ( ! $duotone_support ) { 967 return null; 968 } 969 970 /* 971 * If the experimental duotone support was set, that value is to be 972 * treated as a selector and requires scoping. 973 */ 974 $experimental_duotone = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false ); 975 if ( $experimental_duotone ) { 976 $root_selector = wp_get_block_css_selector( $block_type ); 977 return is_string( $experimental_duotone ) 978 ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone ) 979 : $root_selector; 980 } 981 982 // Regular filter.duotone support uses filter.duotone selectors with fallbacks. 983 return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true ); 990 984 } 991 985 … … 998 992 * 999 993 * @since 6.3.0 1000 */ 1001 public static function set_global_styles_presets() { 994 * 995 * @return array An array of global styles presets, keyed on the filter ID. 996 */ 997 private static function get_all_global_styles_presets() { 998 if ( isset( self::$global_styles_presets ) ) { 999 return self::$global_styles_presets; 1000 } 1002 1001 // Get the per block settings from the theme.json. 1003 1002 $tree = wp_get_global_settings(); 1004 1003 $presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() ); 1005 1004 1005 self::$global_styles_presets = array(); 1006 1006 foreach ( $presets_by_origin as $presets ) { 1007 1007 foreach ( $presets as $preset ) { … … 1011 1011 } 1012 1012 } 1013 1014 return self::$global_styles_presets; 1013 1015 } 1014 1016 … … 1020 1022 * 1021 1023 * @since 6.3.0 1022 */ 1023 public static function set_global_style_block_names() { 1024 * 1025 * @return string[] An array of global style block slugs, keyed on the block name. 1026 */ 1027 private static function get_all_global_style_block_names() { 1028 if ( isset( self::$global_styles_block_names ) ) { 1029 return self::$global_styles_block_names; 1030 } 1024 1031 // Get the per block settings from the theme.json. 1025 1032 $tree = WP_Theme_JSON_Resolver::get_merged_data(); 1026 1033 $block_nodes = $tree->get_styles_block_nodes(); 1027 1034 $theme_json = $tree->get_raw_data(); 1035 1036 self::$global_styles_block_names = array(); 1028 1037 1029 1038 foreach ( $block_nodes as $block_node ) { … … 1047 1056 } 1048 1057 } 1058 return self::$global_styles_block_names; 1049 1059 } 1050 1060 … … 1057 1067 * @since 6.3.0 1058 1068 * 1059 * @param string $block_content Rendered block content. 1060 * @param array $block Block object. 1061 * @return string Filtered block content. 1062 */ 1063 public static function render_duotone_support( $block_content, $block ) { 1064 $duotone_selector = self::get_selector( $block['blockName'] ); 1069 * @param string $block_content Rendered block content. 1070 * @param array $block Block object. 1071 * @param WP_Block $wp_block The block instance. 1072 * @return string Filtered block content. 1073 */ 1074 public static function render_duotone_support( $block_content, $block, $wp_block ) { 1075 if ( empty( $block_content ) || ! $block['blockName'] ) { 1076 return $block_content; 1077 } 1078 $duotone_selector = self::get_selector( $wp_block->block_type ); 1079 1080 if ( ! $duotone_selector ) { 1081 return $block_content; 1082 } 1083 1084 $global_styles_block_names = self::get_all_global_style_block_names(); 1065 1085 1066 1086 // The block should have a duotone attribute or have duotone defined in its theme.json to be processed. 1067 1087 $has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] ); 1068 $has_global_styles_duotone = array_key_exists( $block['blockName'], self::$global_styles_block_names ); 1069 1070 if ( 1071 empty( $block_content ) || 1072 ! $duotone_selector || 1073 ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) 1074 ) { 1088 $has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names ); 1089 1090 if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) { 1075 1091 return $block_content; 1076 1092 } … … 1120 1136 } 1121 1137 } elseif ( $has_global_styles_duotone ) { 1122 $slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.1138 $slug = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'. 1123 1139 $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. 1124 1140 $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. … … 1207 1223 */ 1208 1224 public static function add_editor_settings( $settings ) { 1209 if ( ! empty( self::$global_styles_presets ) ) { 1225 $global_styles_presets = self::get_all_global_styles_presets(); 1226 if ( ! empty( $global_styles_presets ) ) { 1210 1227 if ( ! isset( $settings['styles'] ) ) { 1211 1228 $settings['styles'] = array(); … … 1214 1231 $settings['styles'][] = array( 1215 1232 // For the editor we can add all of the presets by default. 1216 'assets' => self::get_svg_definitions( self::$global_styles_presets ),1233 'assets' => self::get_svg_definitions( $global_styles_presets ), 1217 1234 // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work. 1218 1235 '__unstableType' => 'svgs', … … 1223 1240 $settings['styles'][] = array( 1224 1241 // For the editor we can add all of the presets by default. 1225 'css' => self::get_global_styles_presets( self::$global_styles_presets ),1242 'css' => self::get_global_styles_presets( $global_styles_presets ), 1226 1243 // This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component. 1227 1244 '__unstableType' => 'presets', … … 1243 1260 * @param array $settings Current block type settings. 1244 1261 * @param array $metadata Block metadata as read in via block.json. 1245 *1246 1262 * @return array Filtered block type settings. 1247 1263 */ -
trunk/src/wp-includes/deprecated.php
r56223 r56226 5216 5216 function wp_render_duotone_support( $block_content, $block ) { 5217 5217 _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone::render_duotone_support()' ); 5218 return WP_Duotone::render_duotone_support( $block_content, $block ); 5218 $wp_block = new WP_Block( $block ); 5219 return WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ); 5219 5220 } 5220 5221 -
trunk/tests/phpunit/tests/block-supports/duotone.php
r56101 r56226 28 28 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ), 29 29 ); 30 $wp_block = new WP_Block( $block ); 30 31 $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>'; 31 32 $expected = '<figure class="wp-block-image size-full wp-duotone-blue-orange"><img src="/my-image.jpg" /></figure>'; 32 $this->assertSame( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );33 $this->assertSame( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) ); 33 34 } 34 35 … … 45 46 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'unset' ) ) ), 46 47 ); 48 $wp_block = new WP_Block( $block ); 47 49 $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>'; 48 50 $expected = '/<figure class="wp-block-image size-full wp-duotone-unset-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/'; 49 $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );51 $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) ); 50 52 } 51 53 … … 60 62 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => array( '#FFFFFF', '#000000' ) ) ) ), 61 63 ); 64 $wp_block = new WP_Block( $block ); 62 65 $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>'; 63 66 $expected = '/<figure class="wp-block-image size-full wp-duotone-ffffff-000000-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/'; 64 $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );67 $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) ); 65 68 } 66 69 -
trunk/tests/phpunit/tests/blocks/supportedStyles.php
r55457 r56226 696 696 $this->register_block_type( 'core/example', $block_type_settings ); 697 697 698 $block = array(698 $block = array( 699 699 'blockName' => 'core/example', 700 700 'attrs' => array(), … … 703 703 'innerHTML' => array(), 704 704 ); 705 $wp_block = new WP_Block( $block ); 705 706 706 707 // Custom error handler's see Warnings even if they are suppressed by the @ symbol. … … 715 716 // HTML5 elements like <time> are not supported by the DOMDocument parser used by the block supports feature. 716 717 // This specific example is emitted by the "Display post date" setting in the latest-posts block. 717 apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block );718 apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block, $wp_block ); 718 719 719 720 restore_error_handler();
Note: See TracChangeset
for help on using the changeset viewer.