diff --git a/js/src/duplicate-post-edit-script.js b/js/src/duplicate-post-edit-script.js index e5697aafe..ccbb7645e 100644 --- a/js/src/duplicate-post-edit-script.js +++ b/js/src/duplicate-post-edit-script.js @@ -126,6 +126,11 @@ class DuplicatePost { * @returns {JSX.Element} The rendered links. */ render() { + // Don't try to render anything if there is no store. + if ( ! select( 'core/editor' ) || ! ( wp.editPost && wp.editPost.PluginPostStatusInfo ) ) { + return null; + } + const currentPostStatus = select( 'core/editor' ).getEditedPostAttribute( 'status' ); return ( diff --git a/src/ui/block-editor.php b/src/ui/block-editor.php index b10ba39da..db6b4243f 100644 --- a/src/ui/block-editor.php +++ b/src/ui/block-editor.php @@ -119,6 +119,10 @@ public function should_previously_used_keyword_assessment_run() { * @return void */ public function enqueue_block_editor_scripts() { + if ( ! $this->permissions_helper->is_edit_post_screen() && ! $this->permissions_helper->is_new_post_screen() ) { + return; + } + $post = \get_post(); if ( ! $post instanceof WP_Post ) { diff --git a/tests/ui/block-editor-test.php b/tests/ui/block-editor-test.php index aa3caa4d5..70312aef4 100644 --- a/tests/ui/block-editor-test.php +++ b/tests/ui/block-editor-test.php @@ -19,28 +19,28 @@ class Block_Editor_Test extends TestCase { /** * Holds the object to create the action link to duplicate. * - * @var Link_Builder + * @var Link_Builder|Mockery\MockInterface */ protected $link_builder; /** * Holds the permissions helper. * - * @var Permissions_Helper + * @var Permissions_Helper|Mockery\MockInterface */ protected $permissions_helper; /** * Holds the asset manager. * - * @var Asset_Manager + * @var Asset_Manager|Mockery\MockInterface */ protected $asset_manager; /** * The instance. * - * @var Block_Editor + * @var Block_Editor|Mockery\MockInterface */ protected $instance; @@ -275,6 +275,14 @@ public function test_enqueue_block_editor_scripts() { 'bulkactions' => '1', ]; + $this->permissions_helper + ->expects( 'is_edit_post_screen' ) + ->andReturnFalse(); + + $this->permissions_helper + ->expects( 'is_new_post_screen' ) + ->andReturnTrue(); + Monkey\Functions\expect( '\get_post' ) ->andReturn( $post ); @@ -332,7 +340,7 @@ public function test_enqueue_block_editor_scripts() { * @runInSeparateProcess * @preserveGlobalState disabled */ - public function test_get_new_draft_permalink_rewrite_and_republish() { + public function test_get_enqueue_block_editor_scripts_rewrite_and_republish() { $utils = Mockery::mock( 'alias:\Yoast\WP\Duplicate_Post\Utils' ); $post = Mockery::mock( WP_Post::class ); $new_draft_link = 'http://fakeu.rl/new_draft'; @@ -354,6 +362,10 @@ public function test_get_new_draft_permalink_rewrite_and_republish() { 'bulkactions' => '1', ]; + $this->permissions_helper + ->expects( 'is_edit_post_screen' ) + ->andReturnTrue(); + Monkey\Functions\expect( '\get_post' ) ->andReturn( $post ); @@ -416,25 +428,38 @@ public function test_get_new_draft_permalink_rewrite_and_republish() { * * @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::enqueue_block_editor_scripts */ - public function test_get_new_draft_permalink_no_post() { + public function test_enqueue_block_editor_scripts_no_post() { + $this->permissions_helper + ->expects( 'is_edit_post_screen' ) + ->andReturnTrue(); + Monkey\Functions\expect( '\get_post' ) ->andReturnNull(); - $this->permissions_helper - ->expects( 'is_rewrite_and_republish_copy' ) + $this->asset_manager + ->expects( 'enqueue_edit_script' ) ->never(); - $this->instance - ->expects( 'get_new_draft_permalink' ) + $this->asset_manager + ->expects( 'enqueue_strings_script' ) ->never(); - $this->instance - ->expects( 'get_rewrite_republish_permalink' ) - ->never(); + $this->instance->enqueue_block_editor_scripts(); + } - $this->instance - ->expects( 'get_original_post_edit_url' ) - ->never(); + /** + * Tests the enqueueing of the scripts when no post is displayed. + * + * @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::enqueue_block_editor_scripts + */ + public function test_enqueue_block_editor_scripts_not_editor() { + $this->permissions_helper + ->expects( 'is_edit_post_screen' ) + ->andReturnFalse(); + + $this->permissions_helper + ->expects( 'is_new_post_screen' ) + ->andReturnFalse(); $this->asset_manager ->expects( 'enqueue_edit_script' ) @@ -444,10 +469,6 @@ public function test_get_new_draft_permalink_no_post() { ->expects( 'enqueue_strings_script' ) ->never(); - $this->instance - ->expects( 'get_check_permalink' ) - ->never(); - $this->instance->enqueue_block_editor_scripts(); }