Make WordPress Core

Changeset 50961


Ignore:
Timestamp:
05/24/2021 09:50:30 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Widgets: Make sure WP_Widget constructor creates a correct classname value for a namespaced widget class.

This reverts the changes to id_base from [50953] due to backward compatibility concerns, and instead focuses on the id and class attributes specifically.

With this change, any backslashes in the id or class attributes for a namespaced widget class are converted to underscores, making it easier to style the output or target the widget with JavaScript.

Follow-up to [50953].

Fixes #44098.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/widgets.php

    r49113 r50961  
    163163
    164164        foreach ( $wp_registered_widgets as $widget_id => $widget ) {
    165                 if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) ) {
     165                if ( preg_match( '/' . preg_quote( $id_base, '/' ) . '-([0-9]+)$/', $widget_id, $matches ) ) {
    166166                        $number = max( $number, $matches[1] );
    167167                }
  • trunk/src/wp-includes/class-wp-widget.php

    r50953 r50961  
    165165                } else {
    166166                        $id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) );
    167                         $id_base = str_replace( '\\', '-', $id_base );
    168167                }
    169168
    … …  
    174173                        $widget_options,
    175174                        array(
    176                                 'classname'                   => $this->option_name,
     175                                'classname'                   => str_replace( '\\', '_', $this->option_name ),
    177176                                'customize_selective_refresh' => false,
    178177                        )
  • trunk/src/wp-includes/widgets.php

    r50372 r50961  
    754754                        }
    755755                }
    756                 $classname_                 = ltrim( $classname_, '_' );
    757                 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ );
     756                $classname_ = ltrim( $classname_, '_' );
     757
     758                $params[0]['before_widget'] = sprintf(
     759                        $params[0]['before_widget'],
     760                        str_replace( '\\', '_', $id ),
     761                        $classname_
     762                );
    758763
    759764                /**
  • trunk/tests/phpunit/tests/widgets.php

    r50953 r50961  
    429429         * @ticket 44098
    430430         * @see WP_Widget::__construct()
    431          * @dataProvider data_wp_widget_id_base
    432          */
    433         function test_wp_widget_id_base( $expected, $widget_class ) {
     431         * @dataProvider data_wp_widget_classname
     432         */
     433        function test_wp_widget_classname( $expected, $widget_class ) {
    434434                require_once DIR_TESTDATA . '/widgets/custom-widget-classes.php';
    435435
    436436                $widget = new $widget_class( '', 'Foo' );
    437437
    438                 $this->assertSame( $expected, $widget->id_base );
     438                $this->assertSame( $expected, $widget->widget_options['classname'] );
    439439        }
    440440
    … …  
    442442         * Data provider.
    443443         *
    444          * Passes the expected `id_base` value and the class name.
     444         * Passes the expected `classname` value and the PHP class name.
    445445         *
    446446         * @since 5.8.0
    … …  
    448448         * @return array {
    449449         *     @type array {
    450          *         @type string $expected     The expected `id_base` value to be returned.
     450         *         @type string $expected     The expected `classname` value to be returned.
    451451         *         @type string $widget_class The widget class name for creating an instance.
    452452         *     }
    453453         * }
    454454         */
    455         function data_wp_widget_id_base() {
     455        function data_wp_widget_classname() {
    456456                return array(
    457457                        array(
    458                                 'search',
     458                                'widget_search',
    459459                                'WP_Widget_Search',
    460460                        ),
    461461                        array(
    462                                 'test-sub-sub-namespaced_widget',
     462                                'widget_test_sub_sub_namespaced_widget',
    463463                                'Test\Sub\Sub\Namespaced_Widget',
    464464                        ),
    465465                        array(
    466                                 'non_namespaced_widget',
     466                                'widget_non_namespaced_widget',
    467467                                'Non_Namespaced_Widget',
    468468                        ),
Note: See TracChangeset for help on using the changeset viewer.