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

DUPP-176 Prevent rendering of the block editor script when not on an edit screen #219

Merged
merged 2 commits into from
Dec 17, 2021

Conversation

enricobattocchi
Copy link
Member

@enricobattocchi enricobattocchi commented Dec 17, 2021

Context

  • We want to prevent a fatal error in the plugin's script for the Block Editor when it's executed where it's not supposed to.

A plugin or a theme may (wrongfully) set the global var $post in the Widgets screen. This, paired with the fact that the Widgets screen executes the enqueue_block_editor_assets even if there is no post to be edited, trigger the execution of out duplicate-post-edit.js script, which fails because there is no core/editor store or PluginPostStatusInfo component available.
With the FSE transition undergoing, this might happen even more frequently.

Summary

This PR can be summarized in the following changelog entry:

  • Fixes a bug where a fatal error would be triggered in the Widgets page in combination with some themes or plugins.

Relevant technical choices:

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

Reproduce the error

  • build from master (or use any 4.* version)
  • add this custom code to the end of your theme's functions.php:
function my_set_post() {
    global $post;
    $posts = get_posts();
    $post = $posts[0];
}
add_action( 'init', 'my_set_post' );
  • visit Appearance > Widgets
  • see that the page fails with a fatal error that can't be recovered

Test the fix

  • Switch to this branch and build
  • visit Appearance > Widgets
  • see that the page doesn't fail anymore
  • activate Yoast SEO (it's been found that it can enable certain conditions that we check)
  • visit Appearance > Widgets
  • see that the page doesn't fail anymore
  • edit a published post with the Block editor and check that Copy to a new draft and Rewrite & Republish are visible in the Status and visibility section of the sidebar.
  • create a new post with the Block editor and check that Copy to a new draft is visible in the Status and visibility section of the sidebar.

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Documentation

  • I have written documentation for this change.

Quality assurance

  • I have tested this code to the best of my abilities
  • I have added unittests to verify the code works as intended

Fixes DUPP-176

@maartenleenders
Copy link
Contributor

Acceptance 🚧 it still loads and errors:

we now tried ! ( window.wp.editPost && window.wp.editPost.PluginPostStatusInfo ) and that works better. Also you have suggested checking whether we are outside of the post editor.

@maartenleenders
Copy link
Contributor

Yes that does the trick! 🎉

@maartenleenders maartenleenders merged commit 5c7119e into trunk Dec 17, 2021
@maartenleenders maartenleenders deleted the DUPP-176-prevent-fatal-error-post-widget branch December 17, 2021 14:49
@enricobattocchi enricobattocchi changed the title DUPP-176 Prevent rendering of the script when the store is not available DUPP-176 Prevent rendering of the block editor script when not on an edit screen Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants