Make WordPress Core

Changeset 61090


Ignore:
Timestamp:
10/30/2025 04:01:56 PM (11 months ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-templates-registry.php

    r60904 r61090  
    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                }
     
    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
  • trunk/tests/phpunit/tests/block-template.php

    r60729 r61090  
    480480
    481481        /**
     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
     509        /**
    482510         * Registers a test block to log `in_the_loop()` results.
    483511         *
Note: See TracChangeset for help on using the changeset viewer.