diff --git a/src/wp-includes/class-wp-widget-factory.php b/src/wp-includes/class-wp-widget-factory.php index 82eda94e3a..a7554846a2 100644 --- a/src/wp-includes/class-wp-widget-factory.php +++ b/src/wp-includes/class-wp-widget-factory.php @@ -112,12 +112,29 @@ class WP_Widget_Factory { * @return WP_Widget|null */ public function get_widget_object( $id_base ) { - foreach ( $this->widgets as $widget_object ) { + $key = $this->get_widget_key( $id_base ); + if ( '' === $key ) { + return null; + } + + return $this->widgets[ $key ]; + } + + /** + * Returns the registered key for the given widget type. + * + * @since 5.8.0 + * + * @param string $id_base Widget type ID. + * @return string + */ + public function get_widget_key( $id_base ) { + foreach ( $this->widgets as $key => $widget_object ) { if ( $widget_object->id_base === $id_base ) { - return $widget_object; + return $key; } } - return null; + return ''; } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php index 9c0837644b..b4ae1da184 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php @@ -471,6 +471,7 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller { } $serialized_instance = serialize( $instance ); + $widget_key = $wp_widget_factory->get_widget_key( $id ); $response = array( 'form' => trim( @@ -481,7 +482,7 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller { ), 'preview' => trim( $this->get_widget_preview( - $widget_object, + $widget_key, $instance ) ), @@ -503,13 +504,13 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller { * Returns the output of WP_Widget::widget() when called with the provided * instance. Used by encode_form_data() to preview a widget. - * @param WP_Widget $widget_object Widget object to call widget() on. + * @param string $widget The widget's PHP class name (see class-wp-widget.php). * @param array $instance Widget instance settings. * @return string */ - private function get_widget_preview( $widget_object, $instance ) { + private function get_widget_preview( $widget, $instance ) { ob_start(); - the_widget( get_class( $widget_object ), $instance ); + the_widget( $widget, $instance ); return ob_get_clean(); }