mirror of
git://develop.git.wordpress.org/
synced 2025-04-02 19:22:34 +02:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@50961 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
987acb5954
commit
2c7c677f25
@ -162,7 +162,7 @@ function next_widget_id_number( $id_base ) {
|
||||
$number = 1;
|
||||
|
||||
foreach ( $wp_registered_widgets as $widget_id => $widget ) {
|
||||
if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) ) {
|
||||
if ( preg_match( '/' . preg_quote( $id_base, '/' ) . '-([0-9]+)$/', $widget_id, $matches ) ) {
|
||||
$number = max( $number, $matches[1] );
|
||||
}
|
||||
}
|
||||
|
@ -164,7 +164,6 @@ class WP_Widget {
|
||||
$id_base = strtolower( $id_base );
|
||||
} else {
|
||||
$id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) );
|
||||
$id_base = str_replace( '\\', '-', $id_base );
|
||||
}
|
||||
|
||||
$this->id_base = $id_base;
|
||||
@ -173,7 +172,7 @@ class WP_Widget {
|
||||
$this->widget_options = wp_parse_args(
|
||||
$widget_options,
|
||||
array(
|
||||
'classname' => $this->option_name,
|
||||
'classname' => str_replace( '\\', '_', $this->option_name ),
|
||||
'customize_selective_refresh' => false,
|
||||
)
|
||||
);
|
||||
|
@ -753,8 +753,13 @@ function dynamic_sidebar( $index = 1 ) {
|
||||
$classname_ .= '_' . get_class( $cn );
|
||||
}
|
||||
}
|
||||
$classname_ = ltrim( $classname_, '_' );
|
||||
$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ );
|
||||
$classname_ = ltrim( $classname_, '_' );
|
||||
|
||||
$params[0]['before_widget'] = sprintf(
|
||||
$params[0]['before_widget'],
|
||||
str_replace( '\\', '_', $id ),
|
||||
$classname_
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the parameters passed to a widget's display callback.
|
||||
|
@ -428,42 +428,42 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
/**
|
||||
* @ticket 44098
|
||||
* @see WP_Widget::__construct()
|
||||
* @dataProvider data_wp_widget_id_base
|
||||
* @dataProvider data_wp_widget_classname
|
||||
*/
|
||||
function test_wp_widget_id_base( $expected, $widget_class ) {
|
||||
function test_wp_widget_classname( $expected, $widget_class ) {
|
||||
require_once DIR_TESTDATA . '/widgets/custom-widget-classes.php';
|
||||
|
||||
$widget = new $widget_class( '', 'Foo' );
|
||||
|
||||
$this->assertSame( $expected, $widget->id_base );
|
||||
$this->assertSame( $expected, $widget->widget_options['classname'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* Passes the expected `id_base` value and the class name.
|
||||
* Passes the expected `classname` value and the PHP class name.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @return array {
|
||||
* @type array {
|
||||
* @type string $expected The expected `id_base` value to be returned.
|
||||
* @type string $expected The expected `classname` value to be returned.
|
||||
* @type string $widget_class The widget class name for creating an instance.
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
function data_wp_widget_id_base() {
|
||||
function data_wp_widget_classname() {
|
||||
return array(
|
||||
array(
|
||||
'search',
|
||||
'widget_search',
|
||||
'WP_Widget_Search',
|
||||
),
|
||||
array(
|
||||
'test-sub-sub-namespaced_widget',
|
||||
'widget_test_sub_sub_namespaced_widget',
|
||||
'Test\Sub\Sub\Namespaced_Widget',
|
||||
),
|
||||
array(
|
||||
'non_namespaced_widget',
|
||||
'widget_non_namespaced_widget',
|
||||
'Non_Namespaced_Widget',
|
||||
),
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user