WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/23/2021 08:50:44 AM (5 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-widget.php

    r49946 r50953  
    161161     */
    162162    public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    163         $this->id_base         = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base );
     163        if ( ! empty( $id_base ) ) {
     164            $id_base = strtolower( $id_base );
     165        } else {
     166            $id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) );
     167            $id_base = str_replace( '\\', '-', $id_base );
     168        }
     169
     170        $this->id_base         = $id_base;
    164171        $this->name            = $name;
    165172        $this->option_name     = 'widget_' . $this->id_base;
Note: See TracChangeset for help on using the changeset viewer.