Skip to content

Commit

Permalink
Merge pull request #219 from Yoast/DUPP-176-prevent-fatal-error-post-…
Browse files Browse the repository at this point in the history
…widget
  • Loading branch information
maartenleenders authored Dec 17, 2021
2 parents f565965 + bbb109e commit 5c7119e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
5 changes: 5 additions & 0 deletions js/src/duplicate-post-edit-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions src/ui/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
61 changes: 41 additions & 20 deletions tests/ui/block-editor-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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';
Expand All @@ -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 );

Expand Down Expand Up @@ -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' )
Expand All @@ -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();
}

Expand Down

0 comments on commit 5c7119e

Please sign in to comment.