[wp-trac] [WordPress Trac] #53427: Widget preivew not working if widget registered via a instance

WordPress Trac noreply at wordpress.org
Wed Jun 16 11:42:45 UTC 2021


#53427: Widget preivew not working if widget registered via a instance
--------------------------+-------------------------
 Reporter:  spacedmonkey  |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  5.8
Component:  REST API      |    Version:  trunk
 Severity:  major         |   Keywords:  needs-patch
  Focuses:  rest-api      |
--------------------------+-------------------------
 While testing the current WordPress 5.8 beta 2, I found an issue with
 widget preview.

 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. See widget factory

 {{{#!php
          public function register( $widget ) {
                 if ( $widget instanceof WP_Widget ) {
                         $this->widgets[ spl_object_hash( $widget ) ] =
 $widget;
                 } else {
                         $this->widgets[ $widget ] = new $widget();
                 }
         }

 }}}

 In the new widget type controller, this method only passed by class name.

 {{{#!php
         private function get_widget_preview( $widget_object, $instance ) {
                 ob_start();
                 the_widget( get_class( $widget_object ), $instance );
                 return ob_get_clean();
         }

 }}}

 In `the_widget` function, there is no look for `spl_object_hash`.

 {{{#!php
 if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
                 _doing_it_wrong(
                         __FUNCTION__,
                         sprintf(
                                 /* translators: %s: register_widget() */
                                 __( 'Widgets need to be registered using
 %s, before they can be displayed.' ),
                                 '<code>register_widget()</code>'
                         ),
                         '4.9.0'
                 );
                 return;
         }

         $widget_obj = $wp_widget_factory->widgets[ $widget ];
         if ( ! ( $widget_obj instanceof WP_Widget ) ) {
                 return;
         }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53427>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list