From 7670b7dca84fccc40ec4c3271075046dfc2f6f65 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 23 May 2021 08:51:58 +0000 Subject: [PATCH] Widgets: Make sure `WP_Widget` constructor creates a correct `id_base` value for a namespaced widget class. The `id_base` value is used for the widget's `id` and `class` attributes and also for the option name which stores the widget settings (unless the widget specifies a custom `option_name` value). With this change, any backslashes in the `id_base` for a namespaced widget class are converted to hyphens, making it easier to style the output or target the widget with JavaScript. This also avoids a `preg_match(): Compilation failed` PHP warning from `next_widget_id_number()` on the Widgets screen, previously caused by unescaped backslashes. Props Mte90, hermpheus, rogerlos, welcher, SergeyBiryukov. Fixes #44098. Built from https://develop.svn.wordpress.org/trunk@50953 git-svn-id: http://core.svn.wordpress.org/trunk@50562 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-widget.php | 9 ++++++++- wp-includes/version.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/wp-includes/class-wp-widget.php b/wp-includes/class-wp-widget.php index 8d1de7c375..b177c82053 100644 --- a/wp-includes/class-wp-widget.php +++ b/wp-includes/class-wp-widget.php @@ -160,7 +160,14 @@ class WP_Widget { * information on accepted arguments. Default empty array. */ public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) { - $this->id_base = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base ); + if ( ! empty( $id_base ) ) { + $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; $this->name = $name; $this->option_name = 'widget_' . $this->id_base; $this->widget_options = wp_parse_args( diff --git a/wp-includes/version.php b/wp-includes/version.php index 935cfe79c7..66722db6da 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50952'; +$wp_version = '5.8-alpha-50953'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.