Skip to content

Commit

Permalink
Create an alias wp_theme_element_class_name to use it in blocks (#4…
Browse files Browse the repository at this point in the history
…4099)

Create an alias for the internal API class `WP_Theme_JSON_Gutenberg`.
  • Loading branch information
cbravobernal authored Sep 13, 2022
1 parent 780cfdf commit c2d06e3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
16 changes: 16 additions & 0 deletions lib/compat/wordpress-6.1/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ function gutenberg_create_initial_theme_features() {
);
}
add_action( 'setup_theme', 'gutenberg_create_initial_theme_features', 0 );

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 );
}
}
2 changes: 1 addition & 1 deletion packages/block-library/src/comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function register_block_core_comments() {
*/
function comments_block_form_defaults( $fields ) {
if ( wp_is_block_theme() ) {
$fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="%3$s wp-block-button__link ' . WP_Theme_JSON_Gutenberg::get_element_class_name( 'button' ) . '" value="%4$s" />';
$fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="%3$s wp-block-button__link ' . wp_theme_element_class_name( 'button' ) . '" value="%4$s" />';
$fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
}

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<input name="%1$s" type="submit" id="%2$s" class="wp-block-button__link ' . WP_Theme_JSON_Gutenberg::get_element_class_name( 'button' ) . '" value="%4$s" />';
$fields['submit_button'] = '<input name="%1$s" type="submit" id="%2$s" class="wp-block-button__link ' . wp_theme_element_class_name( 'button' ) . '" value="%4$s" />';
$fields['submit_field'] = '<p class="form-submit wp-block-button">%1$s %2$s</p>';
}

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<button type="submit" class="%s" %s %s>%s</button>',
esc_attr( implode( ' ', $button_classes ) ),
Expand Down
4 changes: 2 additions & 2 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = wp_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 = wp_theme_element_class_name( 'unknown-element' );

$this->assertEquals( $expected, $actual );
}
Expand Down

0 comments on commit c2d06e3

Please sign in to comment.