Skip to content

Only load block editor related assets when it is used for a post #6534

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

Merged
merged 15 commits into from
Aug 19, 2021

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Aug 12, 2021

Summary

Fixes #6521

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@pierlon pierlon added this to the v2.1.4 milestone Aug 12, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Aug 12, 2021

Plugin builds for 99915e9 are ready 🛎️!

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

I think changes like this may be a better alternative:

diff --git a/includes/validation/class-amp-validation-manager.php b/includes/validation/class-amp-validation-manager.php
index 402ec4070..a5ff256e0 100644
--- a/includes/validation/class-amp-validation-manager.php
+++ b/includes/validation/class-amp-validation-manager.php
@@ -2175,6 +2175,11 @@ public static function enqueue_block_validation() {
 			return;
 		}
 
+		$screen = get_current_screen();
+		if ( ! $screen instanceof WP_Screen || 'post' !== $screen->base ) {
+			return;
+		}
+
 		$editor_support = Services::get( 'editor.editor_support' );
 
 		// Block validation script uses features only available beginning with WP 5.6.
diff --git a/src/Editor/EditorSupport.php b/src/Editor/EditorSupport.php
index 4e64a4ed6..0fb0f32aa 100644
--- a/src/Editor/EditorSupport.php
+++ b/src/Editor/EditorSupport.php
@@ -50,6 +50,10 @@ public function maybe_show_notice() {
 			return;
 		}
 
+		if ( 'post' !== $screen->base ) {
+			return;
+		}
+
 		$is_block_editor = (
 			! empty( $screen->is_block_editor )
 			||

@@ -91,6 +91,6 @@ function () {
* @return bool
*/
public function editor_supports_amp_block_editor_features() {
return $this->dependency_support->has_support();
return $this->dependency_support->has_support() && use_block_editor_for_post( get_post() );
Copy link
Member

Choose a reason for hiding this comment

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

This might be too low level as it would prevent using this method in other contexts, such as if we want to show a notice on the settings screen or site health to encourage the user to update to use the block editor.

Copy link
Contributor Author

@pierlon pierlon Aug 13, 2021

Choose a reason for hiding this comment

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

That would be what DependencySupport::has_support() is for, no? EditorSupport::editor_supports_amp_block_editor_features() is specifically for checking block editor support and is currently being used as a check before enqueuing block editor related assets (for posts).

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so because isn't the problem that we're enqueueing on the wrong screen?

It's being used in three places:

  1. \AMP_Post_Meta_Box::enqueue_block_assets()
  2. \AMP_Validation_Manager::enqueue_block_validation()
  3. \AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice()

In the first, it's checking if the post global is set and so it isn't having this problem. However, the second two are not checking for the $post global and so they could be causing problems.

The three cases here should all be checking if get_current_screen()->is_block_editor (instead of checking whether the base is 'post') rather than exclusively checking if get_post() is set since it could be that a plugin creates that global on another screen. This is actually being done today in the last one so my suggested change isn't relevant there.

So maybe this is a better suggested alternative:

diff --git a/includes/admin/class-amp-post-meta-box.php b/includes/admin/class-amp-post-meta-box.php
index 86998c170..83e9955db 100644
--- a/includes/admin/class-amp-post-meta-box.php
+++ b/includes/admin/class-amp-post-meta-box.php
@@ -215,6 +215,11 @@ public function enqueue_admin_assets() {
 	 * @since 1.0
 	 */
 	public function enqueue_block_assets() {
+		$screen = get_current_screen();
+		if ( ! $screen || empty( $screen->is_block_editor ) ) {
+			return;
+		}
+
 		$post = get_post();
 		if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
 			return;
diff --git a/includes/validation/class-amp-validation-manager.php b/includes/validation/class-amp-validation-manager.php
index 402ec4070..779e958df 100644
--- a/includes/validation/class-amp-validation-manager.php
+++ b/includes/validation/class-amp-validation-manager.php
@@ -2175,6 +2175,11 @@ public static function enqueue_block_validation() {
 			return;
 		}
 
+		$screen = get_current_screen();
+		if ( ! $screen || empty( $screen->is_block_editor ) ) {
+			return;
+		}
+
 		$editor_support = Services::get( 'editor.editor_support' );
 
 		// Block validation script uses features only available beginning with WP 5.6.

In \AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice() it's checking is_gutenberg_page() which may not be relevant anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The three cases here should all be checking if get_current_screen()->is_block_editor (instead of checking whether the base is 'post') rather than exclusively checking if get_post() is set since it could be that a plugin creates that global on another screen.

Since all those cases are using \AmpProject\AmpWP\Editor\EditorSupport::editor_supports_amp_block_editor_features() would you agree that that would be the most correct place to modify? That is:

diff --git a/src/Editor/EditorSupport.php b/src/Editor/EditorSupport.php
--- a/src/Editor/EditorSupport.php	(revision 38de6f6f86ca13e7c834e5688c4fa7144fc377b6)
+++ b/src/Editor/EditorSupport.php	(date 1628896711900)
@@ -91,6 +91,7 @@
 	 * @return bool
 	 */
 	public function editor_supports_amp_block_editor_features() {
-		return $this->dependency_support->has_support() && use_block_editor_for_post( get_post() );
+		$screen = get_current_screen();
+		return $this->dependency_support->has_support() && $screen && $screen->is_block_editor;
 	}
 }

In \AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice() it's checking is_gutenberg_page() which may not be relevant anymore.

That's only relevant to users using Gutenberg v5.5 and older, where it's likely that their instance of WordPress (WP < v5) does not have get_current_screen()->is_block_editor.

Copy link
Member

Choose a reason for hiding this comment

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

Since all those cases are using \AmpProject\AmpWP\Editor\EditorSupport::editor_supports_amp_block_editor_features() would you agree that that would be the most correct place to modify?

I think so, but probably good to use ! empty( $screen->is_block_editor ) to prevent possible notices on older versions of WP.

Nevertheless, if this is done the phpdoc for the method should be updated to indicate that it is not only whether the editor in the “current environment” supports plugin features, but also that the current screen is the block editor.

Maybe the method name should be changed to reflect this.

That's only relevant to users using Gutenberg v5.5 and older, where it's likely that their instance of WordPress (WP < v5) does not have get_current_screen()->is_block_editor.

Correct, as we don't fully support versions older than WP 5.6.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any concerns on also checking if the post type is eligible for AMP in the same method (EditorSupport::editor_supports_amp_block_editor_features())?

Copy link
Member

Choose a reason for hiding this comment

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

This logic, yeah?

if ( ! in_array( get_post_type(), AMP_Post_Type_Support::get_supported_post_types(), true ) ) {
	return;
}

Yeah, that seems like a good move because that same logic is duplicated currently in \AMP_Post_Meta_Box::enqueue_block_assets() and \AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice():

public function enqueue_block_assets() {
$post = get_post();
if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
return;
}

if ( ! in_array( get_post_type(), AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
return;
}

The logic is absent however in \AMP_Validation_Manager::enqueue_block_validation(). Indeed, I just disabled AMP for the Page post type, and erroneously the AMP toggle and AMP sidebar show up on that screen:

amp-ui-on-disabled-post-type.mov

This is fixed by adding the above condition.

This also extends to AMP_Post_Meta_Box::enqueue_block_assets() as I can see it is currently using this condition:

$post = get_post();
if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
return;
}

Note how it's using AMP_Post_Type_Support::get_eligible_post_types() when it should be using AMP_Post_Type_Support::get_supported_post_types(), as a post can be eligible without being actually supported (e.g. by being disabled). So in the above example with the page post type disabled, I'm erroneously getting amp-block-editor enqueued onto the page even though it is entirely disabled.

This is also seems to be a bug in \AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice():

if ( ! in_array( get_post_type(), AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
return;
}

It seems this should also rather be using AMP_Post_Type_Support::get_supported_post_types().

@pierlon pierlon force-pushed the fix/6521-block-editor-assets branch from f207589 to 1a48487 Compare August 14, 2021 03:40
@pierlon pierlon requested a review from westonruter August 14, 2021 04:34
*
* @return bool
*/
public function editor_supports_amp_block_editor_features() {
return $this->dependency_support->has_support();
public function supports_current_screen() {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like is_current_screen_supported_block_editor_for_amp_enabled_post_type would be more clear if also verbose. To me supports_current_screen is somewhat vague.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 8235b9f.

Comment on lines 218 to 223
$post = get_post();
if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
return;
}

$editor_support = Services::get( 'editor.editor_support' );
if ( ! $editor_support->editor_supports_amp_block_editor_features() ) {
if ( ! $editor_support->is_current_screen_supported_block_editor_for_amp_enabled_post_type() ) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

Per below, to use the proposed is_block_editor_screen_for_amp_enabled_post_type:

$dependency_support = Services::get( 'dependency_support' );
if ( ! $dependency_support->has_support() ) {
	return; // @codeCoverageIgnore
}

$editor_support = Services::get( 'editor.editor_support' );
if ( ! $editor_support->is_block_editor_screen_for_amp_enabled_post_type() ) {
	return;
}

Copy link
Member

Choose a reason for hiding this comment

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

Notice how here we need ! $dependency_support->has_support() but in maybe_show_notice we need $dependency_support->has_support().

@@ -29,7 +29,7 @@
<?php if ( ! Services::get( 'dependency_support' )->has_support_from_core() ) : ?>
<div class="notice notice-info notice-alt inline">
<p>
<?php esc_html_e( 'Your version of WordPress is too old manage whether AMP is enabled. Please upgrade.', 'amp' ); ?>
<?php esc_html_e( 'Your version of WordPress is too old to manage whether AMP is enabled. Please upgrade.', 'amp' ); ?>
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch. But if someone is on an old version of WP, they they deserve to have a typo. 😉

Comment on lines 2180 to 2183
// Block validation script uses features only available beginning with WP 5.6.
if ( ! $editor_support->editor_supports_amp_block_editor_features() ) {
if ( ! $editor_support->is_current_screen_supported_block_editor_for_amp_enabled_post_type() ) {
return; // @codeCoverageIgnore
}
Copy link
Member

Choose a reason for hiding this comment

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

Per above, this could then be:

// Block validation script uses features only available beginning with WP 5.6.
$dependency_support = Services::get( 'dependency_support' );
if ( ! $dependency_support->has_support() ) {
	return; // @codeCoverageIgnore
}

// Only enqueue scripts on the block editor for AMP-enabled posts.
$editor_support = Services::get( 'editor.editor_support' );
if ( ! $editor_support->is_block_editor_screen_for_amp_enabled_post_type() ) {
	return;
}

Comment on lines 93 to 83
public function editor_supports_amp_block_editor_features() {
return $this->dependency_support->has_support();
public function is_current_screen_supported_block_editor_for_amp_enabled_post_type() {
$screen = get_current_screen();
return $this->dependency_support->has_support()
&&
$screen
&&
! empty( $screen->is_block_editor )
&&
in_array( get_post_type(), AMP_Post_Type_Support::get_supported_post_types(), true );
}
Copy link
Member

Choose a reason for hiding this comment

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

Per above, this could instead be:

	public function is_block_editor_screen_for_amp_enabled_post_type() {
		$screen = get_current_screen();
		return (
			$screen
			&&
			! empty( $screen->is_block_editor )
			&&
			in_array( get_post_type(), AMP_Post_Type_Support::get_supported_post_types(), true )
		);
	}

Comment on lines 47 to 49
$screen = get_current_screen();

if ( ! $screen ) {
return;
}

$is_block_editor = (
! empty( $screen->is_block_editor )
||
// Applicable to Gutenberg v5.5.0 and older.
( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() )
);
if ( ! $is_block_editor ) {
return;
}

if ( ! in_array( get_post_type(), AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
return;
}

if ( $this->editor_supports_amp_block_editor_features() ) {
if ( $this->is_current_screen_supported_block_editor_for_amp_enabled_post_type() ) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

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

I just realized that this is currently resulting in the wp_add_inline_script() logic being run for all screens other than the edit post screen. Like if I go to the Dashboard, is_current_screen_supported_block_editor_for_amp_enabled_post_type() is returning false because it's not the block editor.

Maybe putting all that logic in the same method goes too far.

Maybe instead of is_current_screen_supported_block_editor_for_amp_enabled_post_type we need a is_block_editor_screen_for_amp_enabled_post_type which looks like:

public function is_block_editor_screen_for_amp_enabled_post_type() {
	$screen = get_current_screen();
	return (
		$screen
		&&
		! empty( $screen->is_block_editor )
		&&
		in_array( get_post_type(), AMP_Post_Type_Support::get_supported_post_types(), true )
	);
}

Then this method can instead be starting with:

if ( $this->dependency_support->has_support() ) {
	return;
}

if ( ! $this->is_block_editor_screen_for_amp_enabled_post_type() ) {
	return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh right, nice catch! I've made the amendment in 5c93b00.

@pierlon pierlon requested a review from westonruter August 17, 2021 00:37
@westonruter
Copy link
Member

With 99915e9, the \AMP_Post_Meta_Box::enqueue_admin_assets() method is updated for the Classic Editor.

In the following tests, the page post type disabled for AMP and the post post type enabled.

Site with has_support_from_core

Post Type Before After
page Enqueued amp-post-meta-box.css and amp-post-meta-box.js 👎 Nothing enqueued. 👍
post Enqueued amp-post-meta-box.css and amp-post-meta-box.js Both still enqueued. 👍

To align with the block editor's behavior, the AMP status is now hidden entirely in the classic editor when the post type is disabled for AMP.

Before After
image image

Site without has_support_from_core

Post Type Before After
page Enqueued amp-post-meta-box.css 👎 Nothing enqueued. 👍
post Enqueued amp-post-meta-box.css Style still enqueued. 👍
Post Type Before After
post image image
page image image

@westonruter
Copy link
Member

For now the Block Editor, again with post enabled for AMP and page disabled for AMP.

Site with has_support

\AMP_Post_Meta_Box::enqueue_block_assets()

Post Type Before After
post Both amp-block-editor.js and amp-block-editor.css are enqueued. 👍 Both amp-block-editor.js and amp-block-editor.css are enqueued. 👍
page Both amp-block-editor.js and amp-block-editor.css are enqueued (and AMP Settings appear in blocks erroneously). 👎 Neither script nor style is enqueued (and no AMP Settings in blocks). 👍

\AMP_Validation_Manager::enqueue_block_validation()

Post Type Before After
post amp-block-validation script & style enqueued amp-block-validation script & style still enqueued 👍
page amp-block-validation script & style enqueued (when they should not be) 👎 image Neither amp-block-validation script nor style enqueued 👍 image

\AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice()

No notice should be shown in any scenario since has_support.

Post Type Before After
post No notice shown. No notice shown. 👍
page No notice shown. No notice shown. 👍

Site without has_support

\AMP_Post_Meta_Box::enqueue_block_assets()

Post Type Before After
post amp-block-editor script/style not enqueued amp-block-editor script/style not enqueued 👍
page amp-block-editor script/style not enqueued amp-block-editor script/style not enqueued 👍

\AMP_Validation_Manager::enqueue_block_validation()

Post Type Before After
post amp-block-validation script/style not enqueued amp-block-validation script/style not enqueued 👍
page amp-block-validation script/style not enqueued amp-block-validation script/style not enqueued 👍

\AmpProject\AmpWP\Editor\EditorSupport::maybe_show_notice()

Post Type Before After
post Notice displayed. Notice displayed. 👍
page Notice displayed 👎 Notice not shown (since post type not enabled). 👍

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Good work. This was a tedious one.

@westonruter westonruter merged commit cd5ceab into develop Aug 19, 2021
@westonruter westonruter deleted the fix/6521-block-editor-assets branch August 19, 2021 00:26
westonruter added a commit that referenced this pull request Aug 19, 2021
@westonruter
Copy link
Member

Oh, most important of all, this the PHP notice no longer shows up on the widgets screen.

@pierlon
Copy link
Contributor Author

pierlon commented Aug 19, 2021

Thanks for carrying this one across the finish line 🙇.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AMP Block validation fails on widget screen
2 participants