Make WordPress Core


Ignore:
Timestamp:
06/23/2021 01:33:20 AM (4 years 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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.