WordPress.org

Make WordPress Core

Changeset 51216


Ignore:
Timestamp:
06/23/2021 01:33:20 AM (3 months ago)
Author:
noisysocks
Message:

Widgets: Fix widget preview not working if widget registered via a instance

The register_widget function can be called with a class name or a class
instance. Once called with a class instance, the class instance is converted to
hash as used key in array.

Props spacedmonkey, zieladam.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

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

    r50995 r51216  
    113113     */
    114114    public function get_widget_object( $id_base ) {
    115         foreach ( $this->widgets as $widget_object ) {
     115        $key = $this->get_widget_key( $id_base );
     116        if ( '' === $key ) {
     117            return null;
     118        }
     119
     120        return $this->widgets[ $key ];
     121    }
     122
     123    /**
     124     * Returns the registered key for the given widget type.
     125     *
     126     * @since 5.8.0
     127     *
     128     * @param string $id_base Widget type ID.
     129     * @return string
     130     */
     131    public function get_widget_key( $id_base ) {
     132        foreach ( $this->widgets as $key => $widget_object ) {
    116133            if ( $widget_object->id_base === $id_base ) {
    117                 return $widget_object;
     134                return $key;
    118135            }
    119136        }
    120137
    121         return null;
     138        return '';
    122139    }
    123140}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php

    r51183 r51216  
    472472
    473473        $serialized_instance = serialize( $instance );
     474        $widget_key = $wp_widget_factory->get_widget_key( $id );
    474475
    475476        $response = array(
     
    482483            'preview'  => trim(
    483484                $this->get_widget_preview(
    484                     $widget_object,
     485                    $widget_key,
    485486                    $instance
    486487                )
     
    504505     * instance. Used by encode_form_data() to preview a widget.
    505506
    506      * @param WP_Widget $widget_object Widget object to call widget() on.
     507     * @param string    $widget   The widget's PHP class name (see class-wp-widget.php).
    507508     * @param array     $instance Widget instance settings.
    508509     * @return string
    509510     */
    510     private function get_widget_preview( $widget_object, $instance ) {
     511    private function get_widget_preview( $widget, $instance ) {
    511512        ob_start();
    512         the_widget( get_class( $widget_object ), $instance );
     513        the_widget( $widget, $instance );
    513514        return ob_get_clean();
    514515    }
Note: See TracChangeset for help on using the changeset viewer.