From 9ded201fb84758973adedaf93869fff509dc5a51 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Thu, 3 Nov 2022 15:33:24 +1100 Subject: [PATCH 1/5] Add new public method for block to selector lookup --- .../wordpress-6.2/get-global-styles-and-settings.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 4dbd9d0ba8bde1..c1c6a15a8ef0bd 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -45,3 +45,14 @@ function wp_theme_clean_theme_json_cached_data() { WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); } } + +function wp_theme_get_selector_for_block( $block_name ) { + $block_to_selector_map = WP_Theme_JSON_Gutenberg::get_blocks_metadata(); + $block_selector = null; + + if ( isset( $block_to_selector_map[ $block_name ]['selector'] ) ) { + $block_selector = $block_to_selector_map[ $block_name ]['selector']; + } + + return $block_selector; +} From 643bd56da82a6422a40dcd504f71c2ceaefed1a3 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Thu, 3 Nov 2022 16:05:34 +1100 Subject: [PATCH 2/5] Re-write the public method so it doesn't depend on an internal method --- .../get-global-styles-and-settings.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index c1c6a15a8ef0bd..3cbc071e15ba66 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -46,13 +46,23 @@ function wp_theme_clean_theme_json_cached_data() { } } -function wp_theme_get_selector_for_block( $block_name ) { - $block_to_selector_map = WP_Theme_JSON_Gutenberg::get_blocks_metadata(); - $block_selector = null; +function wp_theme_get_selector_for_block( $block_name_to_lookup ) { + $registry = WP_Block_Type_Registry::get_instance(); + $blocks = $registry->get_all_registered(); - if ( isset( $block_to_selector_map[ $block_name ]['selector'] ) ) { - $block_selector = $block_to_selector_map[ $block_name ]['selector']; + // Lookup the selector quickly + if ( isset( $blocks[ $block_name_to_lookup ] ) ) { + $block = $blocks[ $block_name_to_lookup ]; + if ( + isset( $block->supports['__experimentalSelector'] ) && + is_string( $block->supports['__experimentalSelector'] ) + ) { + return $block->supports['__experimentalSelector']; + } else { + return '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name_to_lookup ) ); + } } - return $block_selector; + // Selector for the block was not found + return null; } From 07781516ddf27e47e794b670af1e4408f0ba8fbc Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Thu, 3 Nov 2022 16:08:47 +1100 Subject: [PATCH 3/5] Add some public documentation and clean it up a bit --- .../wordpress-6.2/get-global-styles-and-settings.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 3cbc071e15ba66..a765fca92c81a5 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -46,11 +46,18 @@ function wp_theme_clean_theme_json_cached_data() { } } -function wp_theme_get_selector_for_block( $block_name_to_lookup ) { +/** + * Lookup a CSS selector for the block provided, and return it if it exists + * + * @param string $block_name_to_lookup The name of the block to lookup the CSS selector for + * + * @return string the CSS selector for the block + */ +function get_css_selector_for_block( $block_name_to_lookup ) { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - // Lookup the selector quickly + // Lookup the block quickly if ( isset( $blocks[ $block_name_to_lookup ] ) ) { $block = $blocks[ $block_name_to_lookup ]; if ( From 5650f5483548bd6c8b26148861aab9a2e6cf0a37 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Thu, 3 Nov 2022 16:21:15 +1100 Subject: [PATCH 4/5] Cleanup based on internal comments the implementation so it's cleaner --- .../get-global-styles-and-settings.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index a765fca92c81a5..fb0fe94d9a87f2 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -49,24 +49,23 @@ function wp_theme_clean_theme_json_cached_data() { /** * Lookup a CSS selector for the block provided, and return it if it exists * - * @param string $block_name_to_lookup The name of the block to lookup the CSS selector for + * @param string $block_name The name of the block to lookup the CSS selector for * - * @return string the CSS selector for the block + * @return string|null the CSS selector for the block */ -function get_css_selector_for_block( $block_name_to_lookup ) { +function wp_theme_get_css_selector_for_block( $block_name ) { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - // Lookup the block quickly - if ( isset( $blocks[ $block_name_to_lookup ] ) ) { - $block = $blocks[ $block_name_to_lookup ]; + if ( isset( $blocks[ $block_name ] ) ) { + $block = $blocks[ $block_name ]; if ( isset( $block->supports['__experimentalSelector'] ) && is_string( $block->supports['__experimentalSelector'] ) ) { return $block->supports['__experimentalSelector']; } else { - return '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name_to_lookup ) ); + return '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) ); } } From fa9727624299cd4b984046dd5649250434a47d45 Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Thu, 3 Nov 2022 17:44:47 +1100 Subject: [PATCH 5/5] Fix the linting errors that were raised --- .../wordpress-6.2/get-global-styles-and-settings.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index fb0fe94d9a87f2..dc0f78adeb9e9c 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -47,11 +47,11 @@ function wp_theme_clean_theme_json_cached_data() { } /** - * Lookup a CSS selector for the block provided, and return it if it exists - * - * @param string $block_name The name of the block to lookup the CSS selector for - * - * @return string|null the CSS selector for the block + * Lookup a CSS selector for the block provided, and return it if it exists. + * + * @param string $block_name The name of the block to lookup the CSS selector for. + * + * @return string|null the CSS selector for the block. */ function wp_theme_get_css_selector_for_block( $block_name ) { $registry = WP_Block_Type_Registry::get_instance(); @@ -69,6 +69,6 @@ function wp_theme_get_css_selector_for_block( $block_name ) { } } - // Selector for the block was not found + // Selector for the block was not found. return null; }