mirror of
git://develop.git.wordpress.org/
synced 2025-03-22 04:49:49 +01:00
Editor: Fix block template registration failing for custom post types 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
This commit is contained in:
parent
6db1a33402
commit
0dea78f349
@ -50,7 +50,7 @@ final class WP_Block_Templates_Registry {
|
||||
} 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 ) ) {
|
||||
|
@ -267,4 +267,51 @@ class Tests_Block_Templates_wpBlockTemplatesRegistry extends WP_UnitTestCase {
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user