Skip to content

Commit 82d47f1

Browse files
Editor: Correct error message in WP_Block_Templates_Registry::unregister().
When attempting to unregister a non-existent block template, the error message will now include the template name. Follow-up to [59073]. Props mukesh27, shailu25. Fixes #64072. git-svn-id: https://develop.svn.wordpress.org/trunk@61090 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 80156af commit 82d47f1

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/wp-includes/class-wp-block-templates-registry.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ public function register( $template_name, $args = array() ) {
6060
}
6161

6262
if ( $error_message ) {
63-
_doing_it_wrong(
64-
__METHOD__,
65-
$error_message,
66-
'6.7.0'
67-
);
63+
_doing_it_wrong( __METHOD__, $error_message, '6.7.0' );
64+
6865
return new WP_Error( $error_code, $error_message );
6966
}
7067

@@ -221,14 +218,12 @@ public function is_registered( $template_name ) {
221218
*/
222219
public function unregister( $template_name ) {
223220
if ( ! $this->is_registered( $template_name ) ) {
224-
_doing_it_wrong(
225-
__METHOD__,
226-
/* translators: %s: Template name. */
227-
sprintf( __( 'Template "%s" is not registered.' ), $template_name ),
228-
'6.7.0'
229-
);
230221
/* translators: %s: Template name. */
231-
return new WP_Error( 'template_not_registered', __( 'Template "%s" is not registered.' ) );
222+
$error_message = sprintf( __( 'Template "%s" is not registered.' ), $template_name );
223+
224+
_doing_it_wrong( __METHOD__, $error_message, '6.7.0' );
225+
226+
return new WP_Error( 'template_not_registered', $error_message );
232227
}
233228

234229
$unregistered_template = $this->registered_templates[ $template_name ];

tests/phpunit/tests/block-template.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,34 @@ public function test_get_block_template_from_registry() {
478478
unregister_block_template( $template_name );
479479
}
480480

481+
/**
482+
* Tests that unregister_block_template() returns a WP_Error when trying to unregister
483+
* a non-registered template, and that the error message includes the template name.
484+
*
485+
* @ticket 64072
486+
*
487+
* @covers ::unregister_block_template
488+
*/
489+
public function test_unregister_block_template_error_message_includes_template_name() {
490+
$template_name = 'test-plugin//unregistered-template';
491+
492+
// Ensure template is not registered.
493+
unregister_block_template( $template_name );
494+
495+
// Expect _doing_it_wrong() notice.
496+
$this->setExpectedIncorrectUsage( 'WP_Block_Templates_Registry::unregister' );
497+
498+
// Try to unregister again, should return WP_Error.
499+
$error = unregister_block_template( $template_name );
500+
501+
$this->assertInstanceOf( 'WP_Error', $error );
502+
$this->assertStringContainsString(
503+
$template_name,
504+
$error->get_error_message(),
505+
'Error message should include the template name.'
506+
);
507+
}
508+
481509
/**
482510
* Registers a test block to log `in_the_loop()` results.
483511
*

0 commit comments

Comments
 (0)