WordPress.org

Make WordPress Core

Opened 7 weeks ago

Closed 6 weeks ago

Last modified 5 weeks ago

#53427 closed defect (bug) (fixed)

Widget preview not working if widget registered via a instance

Reported by: spacedmonkey Owned by: noisysocks
Milestone: 5.8 Priority: normal
Severity: major Version: 5.8
Component: REST API Keywords: has-patch
Focuses: rest-api Cc:

Description

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

         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.

        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.

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

Change History (18)

This ticket was mentioned in PR #1377 on WordPress/wordpress-develop by spacedmonkey.


7 weeks ago

  • Keywords has-patch added; needs-patch removed

#2 @spacedmonkey
7 weeks ago

For more context, see #28216

This ticket was mentioned in Slack in #core-editor by desrosj. View the logs.


7 weeks ago

#5 @noisysocks
6 weeks ago

Looks good @spacedmonkey!

src/wp-includes/blocks/legacy-widget.php (and all block PHP) is copied from @wordpress/widgets during grunt build --dev so we'll have to make those changes upstream in the Gutenberg repo instead:

https://github.com/WordPress/gutenberg/blob/trunk/packages/widgets/src/blocks/legacy-widget/index.php

#6 @spacedmonkey
6 weeks ago

@noisysocks I have created a PR on the gutenberg repo - https://github.com/WordPress/gutenberg/pull/32781

This ticket was mentioned in Slack in #core by spacedmonkey. View the logs.


6 weeks ago

#8 @SergeyBiryukov
6 weeks ago

  • Summary changed from Widget preivew not working if widget registered via a instance to Widget preview not working if widget registered via a instance

#9 @noisysocks
6 weeks ago

Awesome. Let's wait until GB32781 is merged and back ported to Core and then we can commit everything except the src/wp-includes/blocks/legacy-widget.php changes that are in https://github.com/WordPress/wordpress-develop/pull/1378.

#10 @spacedmonkey
6 weeks ago

  • Owner set to noisysocks
  • Status changed from new to assigned

Now that GB32781 is merged, should should be good merge this. We will have to update the widget package at the same time.

Not sure how this affects #53441

#11 @zieladam
6 weeks ago

I wanted to test this but I don't have any function-based widgets installed. Would you share the one you use for testing?

#12 @spacedmonkey
6 weeks ago

@zieladam This patch doesn't fix function based widgets. It fixes an issue when if you pass an WP_Widget instance to register_widget it would not work.

I found this via a plugin I worked on called web-stories. See how we register our widget.

If you install our plugin, 1.8.0 or high you can help test.

This ticket was mentioned in Slack in #core-editor by spacedmonkey. View the logs.


6 weeks ago

This ticket was mentioned in Slack in #core by sergey. View the logs.


6 weeks ago

This ticket was mentioned in Slack in #core by noisysocks. View the logs.


6 weeks ago

#16 @noisysocks
6 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

r51216 fixes this.

#18 @desrosj
5 weeks ago

#53481 was marked as a duplicate.

Note: See TracTickets for help on using tickets.