-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Legacy widget's preview functionality is broken when the page is moved #34011
Conversation
…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.
👋 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. |
There was a problem hiding this 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.
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.
lib/editor-settings.php
Outdated
@@ -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() : ''; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
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.
There was a problem hiding this comment.
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
…sts is the recommended way to do it.
@@ -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() : ''; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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
?
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. |
This is the new PR with the proper fix that leverages the new REST API endpoint. |
Closed in favour of #34184 |
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?
Legacy Widget
block.Meta
widget from the dropdown.Meta
widget itself). You should see a preview of theMeta
widget now.<iframe />
element with thewp-block-legacy-widget__edit-preview-iframe
class. Check itssrc
attribute.Expected result:
The
src
attribute of the<iframe />
should contain an absolute URL.Please see the screenshots section for reference.
Screenshots
Types of changes
Bug fix.
Checklist:
*.native.js
files for terms that need renaming or removal).