Skip to content

Commit 5c7119e

Browse files
Merge pull request #219 from Yoast/DUPP-176-prevent-fatal-error-post-widget
2 parents f565965 + bbb109e commit 5c7119e

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

js/src/duplicate-post-edit-script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ class DuplicatePost {
126126
* @returns {JSX.Element} The rendered links.
127127
*/
128128
render() {
129+
// Don't try to render anything if there is no store.
130+
if ( ! select( 'core/editor' ) || ! ( wp.editPost && wp.editPost.PluginPostStatusInfo ) ) {
131+
return null;
132+
}
133+
129134
const currentPostStatus = select( 'core/editor' ).getEditedPostAttribute( 'status' );
130135

131136
return (

src/ui/block-editor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function should_previously_used_keyword_assessment_run() {
119119
* @return void
120120
*/
121121
public function enqueue_block_editor_scripts() {
122+
if ( ! $this->permissions_helper->is_edit_post_screen() && ! $this->permissions_helper->is_new_post_screen() ) {
123+
return;
124+
}
125+
122126
$post = \get_post();
123127

124128
if ( ! $post instanceof WP_Post ) {

tests/ui/block-editor-test.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ class Block_Editor_Test extends TestCase {
1919
/**
2020
* Holds the object to create the action link to duplicate.
2121
*
22-
* @var Link_Builder
22+
* @var Link_Builder|Mockery\MockInterface
2323
*/
2424
protected $link_builder;
2525

2626
/**
2727
* Holds the permissions helper.
2828
*
29-
* @var Permissions_Helper
29+
* @var Permissions_Helper|Mockery\MockInterface
3030
*/
3131
protected $permissions_helper;
3232

3333
/**
3434
* Holds the asset manager.
3535
*
36-
* @var Asset_Manager
36+
* @var Asset_Manager|Mockery\MockInterface
3737
*/
3838
protected $asset_manager;
3939

4040
/**
4141
* The instance.
4242
*
43-
* @var Block_Editor
43+
* @var Block_Editor|Mockery\MockInterface
4444
*/
4545
protected $instance;
4646

@@ -275,6 +275,14 @@ public function test_enqueue_block_editor_scripts() {
275275
'bulkactions' => '1',
276276
];
277277

278+
$this->permissions_helper
279+
->expects( 'is_edit_post_screen' )
280+
->andReturnFalse();
281+
282+
$this->permissions_helper
283+
->expects( 'is_new_post_screen' )
284+
->andReturnTrue();
285+
278286
Monkey\Functions\expect( '\get_post' )
279287
->andReturn( $post );
280288

@@ -332,7 +340,7 @@ public function test_enqueue_block_editor_scripts() {
332340
* @runInSeparateProcess
333341
* @preserveGlobalState disabled
334342
*/
335-
public function test_get_new_draft_permalink_rewrite_and_republish() {
343+
public function test_get_enqueue_block_editor_scripts_rewrite_and_republish() {
336344
$utils = Mockery::mock( 'alias:\Yoast\WP\Duplicate_Post\Utils' );
337345
$post = Mockery::mock( WP_Post::class );
338346
$new_draft_link = 'http://fakeu.rl/new_draft';
@@ -354,6 +362,10 @@ public function test_get_new_draft_permalink_rewrite_and_republish() {
354362
'bulkactions' => '1',
355363
];
356364

365+
$this->permissions_helper
366+
->expects( 'is_edit_post_screen' )
367+
->andReturnTrue();
368+
357369
Monkey\Functions\expect( '\get_post' )
358370
->andReturn( $post );
359371

@@ -416,25 +428,38 @@ public function test_get_new_draft_permalink_rewrite_and_republish() {
416428
*
417429
* @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::enqueue_block_editor_scripts
418430
*/
419-
public function test_get_new_draft_permalink_no_post() {
431+
public function test_enqueue_block_editor_scripts_no_post() {
432+
$this->permissions_helper
433+
->expects( 'is_edit_post_screen' )
434+
->andReturnTrue();
435+
420436
Monkey\Functions\expect( '\get_post' )
421437
->andReturnNull();
422438

423-
$this->permissions_helper
424-
->expects( 'is_rewrite_and_republish_copy' )
439+
$this->asset_manager
440+
->expects( 'enqueue_edit_script' )
425441
->never();
426442

427-
$this->instance
428-
->expects( 'get_new_draft_permalink' )
443+
$this->asset_manager
444+
->expects( 'enqueue_strings_script' )
429445
->never();
430446

431-
$this->instance
432-
->expects( 'get_rewrite_republish_permalink' )
433-
->never();
447+
$this->instance->enqueue_block_editor_scripts();
448+
}
434449

435-
$this->instance
436-
->expects( 'get_original_post_edit_url' )
437-
->never();
450+
/**
451+
* Tests the enqueueing of the scripts when no post is displayed.
452+
*
453+
* @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::enqueue_block_editor_scripts
454+
*/
455+
public function test_enqueue_block_editor_scripts_not_editor() {
456+
$this->permissions_helper
457+
->expects( 'is_edit_post_screen' )
458+
->andReturnFalse();
459+
460+
$this->permissions_helper
461+
->expects( 'is_new_post_screen' )
462+
->andReturnFalse();
438463

439464
$this->asset_manager
440465
->expects( 'enqueue_edit_script' )
@@ -444,10 +469,6 @@ public function test_get_new_draft_permalink_no_post() {
444469
->expects( 'enqueue_strings_script' )
445470
->never();
446471

447-
$this->instance
448-
->expects( 'get_check_permalink' )
449-
->never();
450-
451472
$this->instance->enqueue_block_editor_scripts();
452473
}
453474

0 commit comments

Comments
 (0)