Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}
}
Loading