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
This commit is contained in:
noisysocks 2021-06-23 01:34:58 +00:00
parent b588426f96
commit 0bf96b88d3
3 changed files with 26 additions and 8 deletions

View File

@ -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 '';
}
}

View File

@ -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();
}

View File

@ -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.