From 0bf96b88d32e4da1ef32eab796de0ead2dbcee80 Mon Sep 17 00:00:00 2001 From: noisysocks Date: Wed, 23 Jun 2021 01:34:58 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@51216 git-svn-id: http://core.svn.wordpress.org/trunk@50825 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-widget-factory.php | 23 ++++++++++++++++--- .../class-wp-rest-widget-types-controller.php | 9 ++++---- wp-includes/version.php | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/wp-includes/class-wp-widget-factory.php b/wp-includes/class-wp-widget-factory.php index 82eda94e3a..a7554846a2 100644 --- a/wp-includes/class-wp-widget-factory.php +++ b/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/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php index 9c0837644b..b4ae1da184 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php +++ b/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(); } diff --git a/wp-includes/version.php b/wp-includes/version.php index ab93ad2702..2122b6081c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-beta2-51215'; +$wp_version = '5.8-beta2-51216'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.