Make WordPress Core

Changeset 59743 for branches/6.7


Ignore:
Timestamp:
01/30/2025 10:30:13 PM (3 weeks ago)
Author:
flixos90
Message:

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.

Reviewed by jorbin.
Merges [59742] to the 6.7 branch.

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

Location:
branches/6.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.7

  • branches/6.7/src/wp-includes/class-wp-block-templates-registry.php

    r59073 r59743  
    5151            $error_message = __( 'Template names must not contain uppercase characters.' );
    5252            $error_code    = 'template_name_no_uppercase';
    53         } elseif ( ! preg_match( '/^[a-z0-9-]+\/\/[a-z0-9-]+$/', $template_name ) ) {
     53        } elseif ( ! preg_match( '/^[a-z0-9_\-]+\/\/[a-z0-9_\-]+$/', $template_name ) ) {
    5454            $error_message = __( 'Template names must contain a namespace prefix. Example: my-plugin//my-custom-template' );
    5555            $error_code    = 'template_no_prefix';
  • branches/6.7/tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php

    r59073 r59743  
    268268        $this->assertFalse( self::$registry->is_registered( $template_name ), 'Template should not be registered after unregistering.' );
    269269    }
     270
     271    /**
     272     * Data provider for test_template_name_validation.
     273     *
     274     * @return array[] Test data.
     275     */
     276    public static function data_template_name_validation() {
     277        return array(
     278            'valid_simple_name'      => array(
     279                'my-plugin//my-template',
     280                true,
     281                'Valid template name with simple characters should be accepted',
     282            ),
     283            'valid_with_underscores' => array(
     284                'my-plugin//my_template',
     285                true,
     286                'Template name with underscores should be accepted',
     287            ),
     288            'valid_cpt_archive'      => array(
     289                'my-plugin//archive-my_post_type',
     290                true,
     291                'Template name for CPT archive with underscore should be accepted',
     292            ),
     293        );
     294    }
     295
     296    /**
     297     * Tests template name validation with various inputs.
     298     *
     299     * @ticket 62523
     300     *
     301     * @dataProvider data_template_name_validation
     302     *
     303     * @param string $template_name The template name to test.
     304     * @param bool   $expected      Expected validation result.
     305     * @param string $message       Test assertion message.
     306     */
     307    public function test_template_name_validation( $template_name, $expected, $message ) {
     308        $result = self::$registry->register( $template_name, array() );
     309
     310        if ( $expected ) {
     311            self::$registry->unregister( $template_name );
     312            $this->assertNotWPError( $result, $message );
     313        } else {
     314            $this->assertWPError( $result, $message );
     315        }
     316    }
    270317}
Note: See TracChangeset for help on using the changeset viewer.