Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy widget's preview functionality is broken when the page is moved #34011

Closed
wants to merge 18 commits into from
Closed

Legacy widget's preview functionality is broken when the page is moved #34011

wants to merge 18 commits into from

Conversation

anton-vlasenko
Copy link
Contributor

@anton-vlasenko anton-vlasenko commented Aug 11, 2021

Description

Legacy Widget block has a preview function, however, it uses a relative URL.
iframe cannot load the widgets.php file when the code is no longer running from the /wp-admin/ URL.
Fixes #30049

How has this been tested?

  1. Go to Appearance -> Widgets page.
  2. Click on the "plus sign" button at the very bottom of the page.
  3. Add a Legacy Widget block.
  4. Select Meta widget from the dropdown.
  5. Click somewhere on the background of the page (not on the Meta widget itself). You should see a preview of the Meta widget now.
  6. Open your browser's developer tools and find an <iframe /> element with the wp-block-legacy-widget__edit-preview-iframe class. Check its src attribute.

Expected result:

The src attribute of the <iframe /> should contain an absolute URL.
Please see the screenshots section for reference.

Screenshots

Screenshot 2021-08-12 at 20 00 39
Screenshot 2021-08-12 at 19 50 44

Types of changes

Bug fix.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

…t and get WP Admin URL.

This is needed because we have to have the ability to get the actual WordPress admin URL for some blocks (e.g. legacy widgets block).
…e url for widgets.php iframe.

We have to use absolute URLs because in some cases (frontenberg) relative URL is not enough and we have to explitly specifiy path to load widgets.php from.
We need this to know absolute URL of the WP's admin panel. Some components need that value.
It fixes the issue described in #30049.
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Aug 11, 2021
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @anton-vlasenko! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@anton-vlasenko anton-vlasenko changed the title Legacy Widget block is not able to render preview in some cases Legacy widget's preview functionality is broken when the page is moved Aug 11, 2021
@anton-vlasenko anton-vlasenko added the [Type] Bug An existing feature does not function as intended label Aug 11, 2021
@anton-vlasenko anton-vlasenko added this to the WordPress 5.8.1 milestone Aug 11, 2021
Copy link
Member

@kevin940726 kevin940726 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks pretty good! I'm not sure whether it's the best place to add the editor settings though, could get some 👀 from PHP folks too.

lib/editor-settings.php Outdated Show resolved Hide resolved
packages/widgets/src/blocks/legacy-widget/edit/preview.js Outdated Show resolved Hide resolved
packages/widgets/src/blocks/legacy-widget/edit/preview.js Outdated Show resolved Hide resolved
2. Rename wpAbsoluteAdminUrl to adminUrl. IMO This name is shorter and better.
2. Don't assume adminUrl is defined. If it's not defined, return empty string instead.
2. Don't assume adminUrl is defined. If it's not defined, return empty string instead.
@@ -28,6 +28,9 @@ function gutenberg_extend_post_editor_settings( $settings ) {
$settings['defaultTemplatePartAreas'] = gutenberg_get_allowed_template_part_areas();
}

// Some blocks have to use absolute url.
$settings['adminUrl'] = is_callable( 'admin_url' ) ? admin_url() : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function_exists is the default pattern for determining if a function is in memory for use

I'm wondering: Should a REST endpoint be used instead of the backend admin URL?

Gutenberg is using the REST API and is not loading the full backend functionality. I'm wondering if using admin_url() here, assuming it's loaded into memory at this point, might expose unintentional leaks.

Hmm not sure. Raising the point for consideration. cc @azaozz @TimothyBJacobs

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind using function_exists.
Fixed in 198191e

I'm wondering: Should a REST endpoint be used instead of the backend admin URL?

+1 That's a very good question.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a ticket about creating a REST API endpoint for this whole thing, I'm not sure what the expected timing on that is, WordPress/wordpress-develop#1508. CC: @adamziel.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we wait for this simple fix to land after we have that endpoint?

This fix will be ported into core, 100% that function will be there. It is not a feature of the plugin, it is a feature of how core initializes the editors. We've used this in e.g. modify_welcome_panel to modify links in WP Admin with no issues.

So I think we can safely use it as it does not load more resources, it does not tie Gutenberg to core more, and it's a quick fix for other problems of the future: having the admin url available as a setting,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was away for last two weeks but I'm back now. I will fix the test failures on WordPress/wordpress-develop#1508 and then we'll only be an approval away from merging.

Copy link
Contributor Author

@anton-vlasenko anton-vlasenko Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@draganescu

Should we wait for this simple fix to land after we have that endpoint?

Maybe I'm not getting it, but I'd not merge this ticket now because, as @hellofromtonya mentioned, it will expose the backend admin URL. Instead, I'd wait until @adamziel's PR is merged. Then we can implement the fix using a REST endpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the tests as promised, all the reviews are welcome :D

@@ -28,6 +28,9 @@ function gutenberg_extend_post_editor_settings( $settings ) {
$settings['defaultTemplatePartAreas'] = gutenberg_get_allowed_template_part_areas();
}

// Some blocks have to use absolute url.
$settings['adminUrl'] = function_exists( 'admin_url' ) ? admin_url() : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we better create our own filter? This function is a shim that exists for WordPress 5.7 and may get removed so we should not tie into it. I think it's better to define something like gutenberg_add_admin_url_to_editor_settings and run that through add_filter just like gutenberg_extend_post_editor_settings.

Copy link
Contributor

@azaozz azaozz Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think there's a chance admin_url() will be missing. It is in wp-includes/link-template.php which is included before the REST API and plugins.

return select( blockEditorStore ).getSettings();
}, [] );

const widgetPreviewUrl = ( adminUrl ?? '' ) + 'widgets.php';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we ever not have adminUrl?

@draganescu
Copy link
Contributor

cc @noisysocks do you think this fix can land like this in 5.8.1 and maybe later we'll just have a nice REST endpoint for rendering legacy widgets?

@noisysocks
Copy link
Member

cc @noisysocks do you think this fix can land like this in 5.8.1 and maybe later we'll just have a nice REST endpoint for rendering legacy widgets?

I don't think the issue is that urgent. Happy to wait for a proper fix.

@anton-vlasenko
Copy link
Contributor Author

anton-vlasenko commented Aug 19, 2021

This is the new PR with the proper fix that leverages the new REST API endpoint.
Let's close this PR when that new PR is merged (just saying).

@anton-vlasenko
Copy link
Contributor Author

Closed in favour of #34184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Widgets preview uses a relative URL that breaks when the page is moved
9 participants