Skip to content

Commit

Permalink
Editor: Fix block template registration failing for custom post types…
Browse files Browse the repository at this point in the history
… containing underscore characters.

Custom post types may contain underscores, however block template registration has been using a regular expression that disallows underscores. Since the block template name for certain templates is directly associated with which post type it applies to, this regular expression was causing unexpected failures. This changeset adjusts the regular expression to allow block template names with underscore characters, effectively allowing block templates to be registered for any custom post type.

Props alexandrebuffet, ankitkumarshah, gaambo, jorbin, karthickmurugan, oglekler, poena, sukhendu2002.
Fixes #62523.


git-svn-id: https://develop.svn.wordpress.org/trunk@59742 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Jan 30, 2025
1 parent 6db1a33 commit 0dea78f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-block-templates-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function register( $template_name, $args = array() ) {
} elseif ( preg_match( '/[A-Z]+/', $template_name ) ) {
$error_message = __( 'Template names must not contain uppercase characters.' );
$error_code = 'template_name_no_uppercase';
} elseif ( ! preg_match( '/^[a-z0-9-]+\/\/[a-z0-9-]+$/', $template_name ) ) {
} elseif ( ! preg_match( '/^[a-z0-9_\-]+\/\/[a-z0-9_\-]+$/', $template_name ) ) {
$error_message = __( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template' );
$error_code = 'template_no_prefix';
} elseif ( $this->is_registered( $template_name ) ) {
Expand Down
47 changes: 47 additions & 0 deletions tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,51 @@ public function test_unregister() {
$this->assertEquals( $template, $unregistered_template, 'Unregistered template should be the same as the registered one.' );
$this->assertFalse( self::$registry->is_registered( $template_name ), 'Template should not be registered after unregistering.' );
}

/**
* Data provider for test_template_name_validation.
*
* @return array[] Test data.
*/
public static function data_template_name_validation() {
return array(
'valid_simple_name' => array(
'my-plugin//my-template',
true,
'Valid template name with simple characters should be accepted',
),
'valid_with_underscores' => array(
'my-plugin//my_template',
true,
'Template name with underscores should be accepted',
),
'valid_cpt_archive' => array(
'my-plugin//archive-my_post_type',
true,
'Template name for CPT archive with underscore should be accepted',
),
);
}

/**
* Tests template name validation with various inputs.
*
* @ticket 62523
*
* @dataProvider data_template_name_validation
*
* @param string $template_name The template name to test.
* @param bool $expected Expected validation result.
* @param string $message Test assertion message.
*/
public function test_template_name_validation( $template_name, $expected, $message ) {
$result = self::$registry->register( $template_name, array() );

if ( $expected ) {
self::$registry->unregister( $template_name );
$this->assertNotWPError( $result, $message );
} else {
$this->assertWPError( $result, $message );
}
}
}

0 comments on commit 0dea78f

Please sign in to comment.