From aabce962aede289ec9132fd88cae6985f9c2e5f0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 24 May 2021 09:51:56 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@50961 git-svn-id: http://core.svn.wordpress.org/trunk@50570 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/widgets.php | 2 +- wp-includes/class-wp-widget.php | 3 +-- wp-includes/version.php | 2 +- wp-includes/widgets.php | 9 +++++++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/wp-admin/includes/widgets.php b/wp-admin/includes/widgets.php index ac98cdb527..af33787fa6 100644 --- a/wp-admin/includes/widgets.php +++ b/wp-admin/includes/widgets.php @@ -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] ); } } diff --git a/wp-includes/class-wp-widget.php b/wp-includes/class-wp-widget.php index b177c82053..71381d35e6 100644 --- a/wp-includes/class-wp-widget.php +++ b/wp-includes/class-wp-widget.php @@ -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, ) ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 12070c10e3..d9c1bc9558 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50960'; +$wp_version = '5.8-alpha-50961'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php index 1987023463..6030ecc9fc 100644 --- a/wp-includes/widgets.php +++ b/wp-includes/widgets.php @@ -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.