WordPress.org

Make WordPress Core

Ticket #53332: 53332.patch

File 53332.patch, 1.9 KB (added by imath, 6 months ago)
  • src/wp-includes/class-wp-widget-factory.php

    diff --git src/wp-includes/class-wp-widget-factory.php src/wp-includes/class-wp-widget-factory.php
    index 82eda94e3a..eaa02be3cf 100644
    class WP_Widget_Factory { 
    5555         * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    5656         */
    5757        public function register( $widget ) {
     58                $widget_id = '';
     59
    5860                if ( $widget instanceof WP_Widget ) {
    59                         $this->widgets[ spl_object_hash( $widget ) ] = $widget;
     61                        $widget_id                   = spl_object_hash( $widget );
     62                        $this->widgets[ $widget_id ] = $widget;
    6063                } else {
    61                         $this->widgets[ $widget ] = new $widget();
     64                        $widget_id                   = $widget;
     65                        $this->widgets[ $widget_id ] = new $widget();
     66                }
     67
     68                /**
     69                 * The WordPress Block Developer documentation site first informed allowing
     70                 * a Legacy Widget migration to a Block Widget needed the Legacy Widget to
     71                 * add a `$show_instance_in_rest` property to their class and set it to `true`.
     72                 *
     73                 * As WordPress 5.8.0 will finally use a widget option to allow this migration,
     74                 * it seems fair to check for this property to eventually add the corresponding
     75                 * option to the widget in case the early developer missed that change.
     76                 *
     77                 * see https://core.trac.wordpress.org/ticket/53332
     78                 */
     79                if ( isset( $this->widgets[ $widget_id ]->show_instance_in_rest ) && ! isset( $this->widgets[ $widget_id ]->widget_options['show_instance_in_rest'] ) ) {
     80                        $widget_options = array();
     81                        if ( is_array( $this->widgets[ $widget_id ]->widget_options ) ) {
     82                                $widget_options = $this->widgets[ $widget_id ]->widget_options;
     83                        }
     84
     85                        $this->widgets[ $widget_id ]->widget_options = array_merge(
     86                                $widget_options,
     87                                array(
     88                                        'show_instance_in_rest' => $this->widgets[ $widget_id ]->show_instance_in_rest,
     89                                )
     90                        );
    6291                }
    6392        }
    6493