From 539a304a28fbeb5358ef43539997667e29cf1059 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 12 Sep 2022 21:42:01 +0200 Subject: [PATCH 1/3] Alias get_element_class_name to use it in blocks --- lib/compat/wordpress-6.1/theme.php | 19 +++++++++++++++++++ packages/block-library/src/comments/index.php | 2 +- .../src/post-comments-form/index.php | 2 +- packages/block-library/src/search/index.php | 2 +- phpunit/class-wp-theme-json-test.php | 4 ++-- tools/webpack/blocks.js | 1 + 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.1/theme.php b/lib/compat/wordpress-6.1/theme.php index 441fc630714369..0381ca5ad9da5a 100644 --- a/lib/compat/wordpress-6.1/theme.php +++ b/lib/compat/wordpress-6.1/theme.php @@ -29,3 +29,22 @@ function gutenberg_create_initial_theme_features() { ); } add_action( 'setup_theme', 'gutenberg_create_initial_theme_features', 0 ); + + + +/** + * Given an element name, returns a class name. + * Alias from WP_Theme_JSON_Gutenberg::get_element_class_name. + * + * @param string $element The name of the element. + * + * @return string The name of the class. + * + * @since 6.1.0 + */ +function gutenberg_theme_element_class_name( $element ) { + if ( ! class_exists( 'WP_Theme_JSON_Gutenberg' ) || ! method_exists( 'WP_Theme_JSON_Gutenberg', 'get_element_class_name' ) ) { + return ''; + } + return WP_Theme_JSON_Gutenberg::get_element_class_name( $element ); +} diff --git a/packages/block-library/src/comments/index.php b/packages/block-library/src/comments/index.php index 14fe6355a11a70..edc1bedab083f1 100644 --- a/packages/block-library/src/comments/index.php +++ b/packages/block-library/src/comments/index.php @@ -109,7 +109,7 @@ function register_block_core_comments() { */ function comments_block_form_defaults( $fields ) { if ( wp_is_block_theme() ) { - $fields['submit_button'] = ''; + $fields['submit_button'] = ''; $fields['submit_field'] = '

%1$s %2$s

'; } diff --git a/packages/block-library/src/post-comments-form/index.php b/packages/block-library/src/post-comments-form/index.php index 40dad1a5304955..4bbd24b2454112 100644 --- a/packages/block-library/src/post-comments-form/index.php +++ b/packages/block-library/src/post-comments-form/index.php @@ -72,7 +72,7 @@ function register_block_core_post_comments_form() { */ function post_comments_form_block_form_defaults( $fields ) { if ( wp_is_block_theme() ) { - $fields['submit_button'] = ''; + $fields['submit_button'] = ''; $fields['submit_field'] = '

%1$s %2$s

'; } diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 8e2a158969acb4..40cf5fcf5478ff 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -121,7 +121,7 @@ function render_block_core_search( $attributes ) { } // Include the button element class. - $button_classes[] = WP_Theme_JSON_Gutenberg::get_element_class_name( 'button' ); + $button_classes[] = wp_theme_element_class_name( 'button' ); $button_markup = sprintf( '', esc_attr( implode( ' ', $button_classes ) ), diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 42146c66e66352..8ca26b888b7985 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -797,14 +797,14 @@ function test_remove_invalid_element_pseudo_selectors() { function test_get_element_class_name_button() { $expected = 'wp-element-button'; - $actual = WP_Theme_JSON_Gutenberg::get_element_class_name( 'button' ); + $actual = gutenberg_theme_element_class_name( 'button' ); $this->assertEquals( $expected, $actual ); } function test_get_element_class_name_invalid() { $expected = ''; - $actual = WP_Theme_JSON_Gutenberg::get_element_class_name( 'unknown-element' ); + $actual = gutenberg_theme_element_class_name( 'unknown-element' ); $this->assertEquals( $expected, $actual ); } diff --git a/tools/webpack/blocks.js b/tools/webpack/blocks.js index 1113bb38589b61..3b6671743c374a 100644 --- a/tools/webpack/blocks.js +++ b/tools/webpack/blocks.js @@ -30,6 +30,7 @@ const blockViewRegex = new RegExp( const prefixFunctions = [ 'build_query_vars_from_query_block', 'wp_enqueue_block_support_styles', + 'wp_theme_element_class_name', ]; /** From 6adccca44e5fef311732e2b88b62aebc4f8a2d0b Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Tue, 13 Sep 2022 14:06:42 +0200 Subject: [PATCH 2/3] Use wp as prefix --- lib/compat/wordpress-6.1/theme.php | 29 +++++++++++++---------------- tools/webpack/blocks.js | 1 - 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/compat/wordpress-6.1/theme.php b/lib/compat/wordpress-6.1/theme.php index 0381ca5ad9da5a..4787f1057b474d 100644 --- a/lib/compat/wordpress-6.1/theme.php +++ b/lib/compat/wordpress-6.1/theme.php @@ -30,21 +30,18 @@ function gutenberg_create_initial_theme_features() { } add_action( 'setup_theme', 'gutenberg_create_initial_theme_features', 0 ); - - -/** - * Given an element name, returns a class name. - * Alias from WP_Theme_JSON_Gutenberg::get_element_class_name. - * - * @param string $element The name of the element. - * - * @return string The name of the class. - * - * @since 6.1.0 - */ -function gutenberg_theme_element_class_name( $element ) { - if ( ! class_exists( 'WP_Theme_JSON_Gutenberg' ) || ! method_exists( 'WP_Theme_JSON_Gutenberg', 'get_element_class_name' ) ) { - return ''; +if ( ! function_exists( 'wp_theme_element_class_name' ) ) { + /** + * Given an element name, returns a class name. + * Alias from WP_Theme_JSON_Gutenberg::get_element_class_name. + * + * @param string $element The name of the element. + * + * @return string The name of the class. + * + * @since 6.1.0 + */ + function wp_theme_element_class_name( $element ) { + return WP_Theme_JSON_Gutenberg::get_element_class_name( $element ); } - return WP_Theme_JSON_Gutenberg::get_element_class_name( $element ); } diff --git a/tools/webpack/blocks.js b/tools/webpack/blocks.js index 3b6671743c374a..1113bb38589b61 100644 --- a/tools/webpack/blocks.js +++ b/tools/webpack/blocks.js @@ -30,7 +30,6 @@ const blockViewRegex = new RegExp( const prefixFunctions = [ 'build_query_vars_from_query_block', 'wp_enqueue_block_support_styles', - 'wp_theme_element_class_name', ]; /** From e50cc4266f2bff5babd14c50b27d1d91cfdae4ed Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Tue, 13 Sep 2022 14:08:56 +0200 Subject: [PATCH 3/3] Update unit test --- phpunit/class-wp-theme-json-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 8ca26b888b7985..35c4bf153ddb27 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -797,14 +797,14 @@ function test_remove_invalid_element_pseudo_selectors() { function test_get_element_class_name_button() { $expected = 'wp-element-button'; - $actual = gutenberg_theme_element_class_name( 'button' ); + $actual = wp_theme_element_class_name( 'button' ); $this->assertEquals( $expected, $actual ); } function test_get_element_class_name_invalid() { $expected = ''; - $actual = gutenberg_theme_element_class_name( 'unknown-element' ); + $actual = wp_theme_element_class_name( 'unknown-element' ); $this->assertEquals( $expected, $actual ); }