Skip to content

Commit cd5ceab

Browse files
authored
Merge pull request #6534 from ampproject/fix/6521-block-editor-assets
Only load block editor related assets when it is used for a post
2 parents 24923a4 + 99915e9 commit cd5ceab

File tree

8 files changed

+128
-117
lines changed

8 files changed

+128
-117
lines changed

includes/admin/class-amp-post-meta-box.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ public function enqueue_admin_assets() {
146146
$validate = (
147147
isset( $screen->base ) &&
148148
'post' === $screen->base &&
149-
( ! isset( $screen->is_block_editor ) || ! $screen->is_block_editor ) &&
150-
in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true )
149+
empty( $screen->is_block_editor ) &&
150+
in_array( $post->post_type, AMP_Post_Type_Support::get_supported_post_types(), true )
151151
);
152152

153153
if ( ! $validate ) {
@@ -216,12 +216,16 @@ public function enqueue_admin_assets() {
216216
*/
217217
public function enqueue_block_assets() {
218218
$post = get_post();
219-
if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
220-
return;
219+
220+
// Block validation script uses features only available beginning with WP 5.6.
221+
$dependency_support = Services::get( 'dependency_support' );
222+
if ( ! $dependency_support->has_support() ) {
223+
return; // @codeCoverageIgnore
221224
}
222225

226+
// Only enqueue scripts on the block editor for AMP-enabled posts.
223227
$editor_support = Services::get( 'editor.editor_support' );
224-
if ( ! $editor_support->editor_supports_amp_block_editor_features() ) {
228+
if ( ! $editor_support->is_current_screen_block_editor_for_amp_enabled_post_type() ) {
225229
return;
226230
}
227231

@@ -340,7 +344,7 @@ public function render_status( $post ) {
340344
$verify = (
341345
isset( $post->ID )
342346
&&
343-
in_array( $post->post_type, AMP_Post_Type_Support::get_eligible_post_types(), true )
347+
in_array( $post->post_type, AMP_Post_Type_Support::get_supported_post_types(), true )
344348
&&
345349
current_user_can( 'edit_post', $post->ID )
346350
);

includes/templates/amp-enabled-classic-editor-toggle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<?php if ( ! Services::get( 'dependency_support' )->has_support_from_core() ) : ?>
3030
<div class="notice notice-info notice-alt inline">
3131
<p>
32-
<?php esc_html_e( 'Your version of WordPress is too old manage whether AMP is enabled. Please upgrade.', 'amp' ); ?>
32+
<?php esc_html_e( 'Your version of WordPress is too old to manage whether AMP is enabled. Please upgrade.', 'amp' ); ?>
3333
</p>
3434
</div>
3535
<?php else : ?>

includes/validation/class-amp-validation-manager.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,13 +2175,18 @@ public static function enqueue_block_validation() {
21752175
return;
21762176
}
21772177

2178-
$editor_support = Services::get( 'editor.editor_support' );
2179-
21802178
// Block validation script uses features only available beginning with WP 5.6.
2181-
if ( ! $editor_support->editor_supports_amp_block_editor_features() ) {
2179+
$dependency_support = Services::get( 'dependency_support' );
2180+
if ( ! $dependency_support->has_support() ) {
21822181
return; // @codeCoverageIgnore
21832182
}
21842183

2184+
// Only enqueue scripts on the block editor for AMP-enabled posts.
2185+
$editor_support = Services::get( 'editor.editor_support' );
2186+
if ( ! $editor_support->is_current_screen_block_editor_for_amp_enabled_post_type() ) {
2187+
return;
2188+
}
2189+
21852190
$slug = 'amp-block-validation';
21862191

21872192
$asset_file = AMP__DIR__ . '/assets/js/' . $slug . '.asset.php';

src/Editor/EditorSupport.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,11 @@ public function register() {
4444
* Shows a notice in the editor if the Gutenberg or WP version prevents plugin features from working.
4545
*/
4646
public function maybe_show_notice() {
47-
$screen = get_current_screen();
48-
49-
if ( ! $screen ) {
50-
return;
51-
}
52-
53-
$is_block_editor = (
54-
! empty( $screen->is_block_editor )
55-
||
56-
// Applicable to Gutenberg v5.5.0 and older.
57-
( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() )
58-
);
59-
if ( ! $is_block_editor ) {
47+
if ( $this->dependency_support->has_support() ) {
6048
return;
6149
}
6250

63-
if ( ! in_array( get_post_type(), AMP_Post_Type_Support::get_eligible_post_types(), true ) ) {
64-
return;
65-
}
66-
67-
if ( $this->editor_supports_amp_block_editor_features() ) {
51+
if ( ! $this->is_current_screen_block_editor_for_amp_enabled_post_type() ) {
6852
return;
6953
}
7054

@@ -86,11 +70,18 @@ function () {
8670
}
8771

8872
/**
89-
* Returns whether the editor in the current environment supports plugin features.
73+
* Returns whether the current screen is using the block editor and the post being edited supports AMP.
9074
*
9175
* @return bool
9276
*/
93-
public function editor_supports_amp_block_editor_features() {
94-
return $this->dependency_support->has_support();
77+
public function is_current_screen_block_editor_for_amp_enabled_post_type() {
78+
$screen = get_current_screen();
79+
return (
80+
$screen
81+
&&
82+
! empty( $screen->is_block_editor )
83+
&&
84+
in_array( get_post_type(), AMP_Post_Type_Support::get_supported_post_types(), true )
85+
);
9586
}
9687
}

tests/php/src/Editor/EditorSupportTest.php

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
use AmpProject\AmpWP\Editor\EditorSupport;
77
use AmpProject\AmpWP\Infrastructure\Registerable;
88
use AmpProject\AmpWP\Infrastructure\Service;
9+
use AmpProject\AmpWP\Tests\Helpers\WithBlockEditorSupport;
910
use AmpProject\AmpWP\Tests\TestCase;
1011

1112
/** @coversDefaultClass \AmpProject\AmpWP\Editor\EditorSupport */
1213
final class EditorSupportTest extends TestCase {
1314

15+
use WithBlockEditorSupport;
16+
1417
/** @var EditorSupport */
1518
private $instance;
1619

1720
public function setUp() {
1821
parent::setUp();
1922

2023
$this->instance = new EditorSupport( new DependencySupport() );
24+
25+
unset( $GLOBALS['current_screen'], $GLOBALS['wp_scripts'] );
2126
}
2227

2328
public function test_it_can_be_initialized() {
@@ -33,20 +38,34 @@ public function test_register() {
3338
$this->assertEquals( 99, has_action( 'admin_enqueue_scripts', [ $this->instance, 'maybe_show_notice' ] ) );
3439
}
3540

36-
public function test_editor_supports_amp_block_editor_features() {
37-
if (
38-
defined( 'GUTENBERG_VERSION' )
39-
&&
40-
version_compare( GUTENBERG_VERSION, DependencySupport::GB_MIN_VERSION, '>=' )
41-
) {
42-
$this->assertTrue( $this->instance->editor_supports_amp_block_editor_features() );
43-
} else {
44-
if ( version_compare( get_bloginfo( 'version' ), DependencySupport::WP_MIN_VERSION, '>=' ) ) {
45-
$this->assertTrue( $this->instance->editor_supports_amp_block_editor_features() );
46-
} else {
47-
$this->assertFalse( $this->instance->editor_supports_amp_block_editor_features() );
48-
}
49-
}
41+
/**
42+
* Test data for test_supports_current_screen().
43+
*
44+
* @return array
45+
*/
46+
public function get_data_for_test_supports_current_screen() {
47+
return [
48+
'supports post type and amp' => [ true, true, true ],
49+
'supports only post type' => [ true, false, false ],
50+
'supports only amp' => [ false, true, false ],
51+
'does not support post type nor amp' => [ false, false, false ],
52+
];
53+
}
54+
55+
/**
56+
* @covers ::is_current_screen_block_editor_for_amp_enabled_post_type()
57+
* @dataProvider get_data_for_test_supports_current_screen()
58+
*
59+
* @param bool $post_type_uses_block_editor Whether post type can be edited in the block editor.
60+
* @param bool $post_type_supports_amp Whether post type supports AMP.
61+
* @param bool $expected_result Expected result for test assertions.
62+
*/
63+
public function test_supports_current_screen( $post_type_uses_block_editor, $post_type_supports_amp, $expected_result ) {
64+
$this->setup_environment( $post_type_uses_block_editor, $post_type_supports_amp );
65+
66+
// Note: Without Gutenberg being installed on WP 4.9, the expected result would be `false`
67+
// when `$post_type_uses_block_editor` and `$post_type_supports_amp` are `true`.
68+
$this->assertSame( $expected_result, $this->instance->is_current_screen_block_editor_for_amp_enabled_post_type() );
5069
}
5170

5271
/** @covers ::maybe_show_notice() */
@@ -57,110 +76,56 @@ public function test_dont_show_notice_if_no_screen_defined() {
5776

5877
/** @covers ::maybe_show_notice() */
5978
public function test_dont_show_notice_for_unsupported_post_type() {
60-
global $post;
61-
62-
set_current_screen( 'post.php' );
63-
register_post_type( 'my-post-type' );
64-
$post = $this->factory()->post->create( [ 'post_type' => 'my-post-type' ] );
65-
setup_postdata( get_post( $post ) );
66-
67-
wp_set_current_user( $this->factory()->user->create( [ 'role' => 'administrator' ] ) );
79+
$this->setup_environment( true, false );
6880

6981
$this->instance->maybe_show_notice();
7082
$this->assertFalse( wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false ) );
71-
unset( $GLOBALS['current_screen'] );
72-
unset( $GLOBALS['wp_scripts'] );
7383
}
7484

7585
/** @covers ::maybe_show_notice() */
7686
public function test_show_notice_for_supported_post_type() {
77-
global $post;
78-
7987
if ( version_compare( get_bloginfo( 'version' ), DependencySupport::WP_MIN_VERSION, '<' ) ) {
8088
$this->markTestSkipped();
8189
}
8290

83-
set_current_screen( 'post.php' );
84-
$post = $this->factory()->post->create();
85-
setup_postdata( get_post( $post ) );
86-
87-
wp_set_current_user( $this->factory()->user->create( [ 'role' => 'administrator' ] ) );
91+
$this->setup_environment( true, true );
8892

8993
$this->instance->maybe_show_notice();
90-
if ( $this->instance->editor_supports_amp_block_editor_features() ) {
94+
if ( $this->instance->is_current_screen_block_editor_for_amp_enabled_post_type() ) {
9195
$this->assertFalse( wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false ) );
9296
} else {
9397
$this->assertStringContainsString(
9498
'AMP functionality is not available',
9599
wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false )
96100
);
97101
}
98-
unset( $GLOBALS['current_screen'] );
99-
unset( $GLOBALS['wp_scripts'] );
100102
}
101103

102104
/** @covers ::maybe_show_notice() */
103105
public function test_maybe_show_notice_for_unsupported_user() {
104-
global $post;
105-
106-
set_current_screen( 'post.php' );
107-
$post = $this->factory()->post->create();
108-
setup_postdata( get_post( $post ) );
106+
$this->setup_environment( true, true );
107+
wp_set_current_user( self::factory()->user->create() );
109108

110109
$this->instance->maybe_show_notice();
111110

112111
$this->assertFalse( wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false ) );
113-
unset( $GLOBALS['current_screen'] );
114-
unset( $GLOBALS['wp_scripts'] );
115-
}
116-
117-
/** @covers ::maybe_show_notice() */
118-
public function test_maybe_show_notice_with_cpt_supporting_gutenberg_but_not_amp() {
119-
global $post;
120-
121-
if ( ! $this->instance->editor_supports_amp_block_editor_features() ) {
122-
$this->markTestSkipped();
123-
}
124-
125-
register_post_type(
126-
'my-gb-post-type',
127-
[
128-
'public' => true,
129-
'show_in_rest' => true,
130-
'supports' => [ 'editor' ],
131-
]
132-
);
133-
134-
set_current_screen( 'post.php' );
135-
$post = $this->factory()->post->create( [ 'post_type' => 'my-gb-post-type' ] );
136-
setup_postdata( get_post( $post ) );
137-
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
138-
139-
$this->instance->maybe_show_notice();
140-
$this->assertFalse( wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false ) );
141-
unset( $GLOBALS['current_screen'] );
142-
unset( $GLOBALS['wp_scripts'] );
143112
}
144113

145114
/** @covers ::maybe_show_notice() */
146115
public function test_maybe_show_notice_for_gutenberg_4_9() {
147-
global $post;
148116
if ( ! defined( 'GUTENBERG_VERSION' ) || version_compare( GUTENBERG_VERSION, '4.9.0', '>' ) ) {
149117
$this->markTestSkipped( 'Test only applicable to Gutenberg v4.9.0 and older.' );
150118
}
151119

152-
$this->assertFalse( $this->instance->editor_supports_amp_block_editor_features() );
120+
$this->setup_environment( true, true );
121+
// WP < 5.0 doesn't include the block editor, but Gutenberg would be installed, so it should be supported.
122+
$this->assertTrue( $this->instance->is_current_screen_block_editor_for_amp_enabled_post_type() );
153123

154124
gutenberg_register_packages_scripts();
155-
set_current_screen( 'post.php' );
156-
$post = $this->factory()->post->create();
157-
setup_postdata( get_post( $post ) );
158125
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
159126

160127
$this->instance->maybe_show_notice();
161128
$inline_script = wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false );
162129
$this->assertStringContainsString( 'AMP functionality is not available', $inline_script );
163-
unset( $GLOBALS['current_screen'] );
164-
unset( $GLOBALS['wp_scripts'] );
165130
}
166131
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Trait WithoutBlockPreRendering.
4+
*
5+
* @package AmpProject\AmpWP
6+
*/
7+
8+
namespace AmpProject\AmpWP\Tests\Helpers;
9+
10+
use AMP_Options_Manager;
11+
use AmpProject\AmpWP\Option;
12+
use WP_User;
13+
14+
/**
15+
* Helper trait to help with setting up the test environment for block editor support.
16+
*/
17+
trait WithBlockEditorSupport {
18+
19+
/**
20+
* Setup test environment to ensure the correct result for ::supports_current_screen().
21+
*
22+
* @param bool $post_type_uses_block_editor Whether the post type uses the block editor.
23+
* @param bool $post_type_supports_amp Whether the post type supports AMP.
24+
* @param string $post_type Post type ID.
25+
*/
26+
public function setup_environment( $post_type_uses_block_editor, $post_type_supports_amp, $post_type = 'foo' ) {
27+
if ( $post_type_uses_block_editor ) {
28+
set_current_screen( 'post.php' );
29+
add_filter( 'replace_editor', '__return_false' );
30+
add_filter( 'use_block_editor_for_post', '__return_true' );
31+
}
32+
33+
if ( $post_type_supports_amp ) {
34+
register_post_type( $post_type, [ 'public' => true ] );
35+
$GLOBALS['post'] = self::factory()->post->create( [ 'post_type' => $post_type ] );
36+
37+
$previous_user = wp_get_current_user();
38+
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
39+
40+
$supported_post_types = array_merge(
41+
AMP_Options_Manager::get_option( Option::SUPPORTED_POST_TYPES ),
42+
[ $post_type ]
43+
);
44+
AMP_Options_Manager::update_option( Option::SUPPORTED_POST_TYPES, $supported_post_types );
45+
46+
wp_set_current_user( $previous_user instanceof WP_User ? $previous_user->ID : $previous_user );
47+
}
48+
}
49+
}

tests/php/test-class-amp-meta-box.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,11 @@ public function test_render_status() {
284284
$this->assertStringContainsString( $no_support_notice, $output );
285285
}
286286

287-
// Post type no longer supports AMP, so no status input.
287+
// Post type no longer supports AMP, so no status input (and no mention of AMP at all).
288288
$supported_post_types = array_diff( AMP_Options_Manager::get_option( Option::SUPPORTED_POST_TYPES ), [ 'post' ] );
289289
AMP_Options_Manager::update_option( Option::SUPPORTED_POST_TYPES, $supported_post_types );
290290
$output = get_echo( [ $this->instance, 'render_status' ], [ $post ] );
291-
if ( Services::get( 'dependency_support' )->has_support_from_core() ) {
292-
$this->assertStringContainsString( 'This post type is not', $output );
293-
$this->assertStringNotContainsString( $checkbox_enabled, $output );
294-
} else {
295-
$this->assertStringContainsString( $no_support_notice, $output );
296-
}
291+
$this->assertEmpty( $output );
297292
$supported_post_types[] = 'post';
298293
AMP_Options_Manager::update_option( Option::SUPPORTED_POST_TYPES, $supported_post_types );
299294

0 commit comments

Comments
 (0)