Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/src/duplicate-post-edit-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class DuplicatePost {
notice.text,
{
isDismissible: notice.isDismissible || true,
__unstableHTML: notice.isHTML || false,
}
);
}
Expand Down
31 changes: 28 additions & 3 deletions src/watchers/copied-post-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,33 @@ public function register_hooks() {
*/
public function get_notice_text( $post ) {
if ( $this->permissions_helper->has_trashed_rewrite_and_republish_copy( $post ) ) {
$trash_url = \add_query_arg(
[
'post_status' => 'trash',
'post_type' => $post->post_type,
],
\admin_url( 'edit.php' ),
);
$view_trash_html = \sprintf(
' <a href="%1$s">%2$s</a>',
\esc_url( $trash_url ),
\__( 'View trash.', 'duplicate-post' ),
);

return \__(
'You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate.',
'duplicate-post',
) . $view_trash_html;
}

$copy = $this->permissions_helper->get_rewrite_and_republish_copy( $post );
$edit_url = ( $copy instanceof WP_Post ) ? \get_edit_post_link( $copy->ID, 'raw' ) : '';
$link_html = '';
if ( $edit_url !== '' ) {
$link_html = \sprintf(
' <a href="%1$s">%2$s</a>',
\esc_url( $edit_url ),
\__( 'Edit the duplicate.', 'duplicate-post' ),
);
}

Expand All @@ -58,7 +82,7 @@ public function get_notice_text( $post ) {
return \__(
'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.',
'duplicate-post',
);
) . $link_html;
}

return \sprintf(
Expand All @@ -69,7 +93,7 @@ public function get_notice_text( $post ) {
),
\get_the_time( \get_option( 'date_format' ), $scheduled_copy ),
\get_the_time( \get_option( 'time_format' ), $scheduled_copy ),
);
) . $link_html;
}

/**
Expand All @@ -90,7 +114,7 @@ public function add_admin_notice() {

if ( $this->permissions_helper->has_rewrite_and_republish_copy( $post ) ) {
print '<div id="message" class="notice notice-warning is-dismissible fade"><p>'
. \esc_html( $this->get_notice_text( $post ) )
. \wp_kses( $this->get_notice_text( $post ), [ 'a' => [ 'href' => [] ] ] )
. '</p></div>';
}
}
Expand All @@ -113,6 +137,7 @@ public function add_block_editor_notice() {
'text' => $this->get_notice_text( $post ),
'status' => 'warning',
'isDismissible' => true,
'isHTML' => true,
];

\wp_add_inline_script(
Expand Down
47 changes: 42 additions & 5 deletions tests/Unit/Watchers/Copied_Post_Watcher_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ public function test_register_hooks() {
*/
public function test_get_notice_text_not_scheduled() {
$this->stubTranslationFunctions();
$this->stubEscapeFunctions();

$post = Mockery::mock( WP_Post::class );
$copy = Mockery::mock( WP_Post::class );

$copy->ID = 456;

$this->permissions_helper
->expects( 'has_scheduled_rewrite_and_republish_copy' )
Expand All @@ -94,8 +98,16 @@ public function test_get_notice_text_not_scheduled() {
->with( $post )
->andReturnFalse();

$this->permissions_helper
->expects( 'get_rewrite_and_republish_copy' )
->with( $post )
->andReturn( $copy );

Monkey\Functions\when( '\get_edit_post_link' )
->justReturn( 'http://example.com/edit?post=456' );

$this->assertSame(
'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished.',
'A duplicate of this post was made. Please note that any changes you make to this post will be replaced when the duplicated version is republished. <a href="http://example.com/edit?post=456">Edit the duplicate.</a>',
$this->instance->get_notice_text( $post ),
);
}
Expand All @@ -109,10 +121,13 @@ public function test_get_notice_text_not_scheduled() {
*/
public function test_get_notice_text_scheduled() {
$this->stubTranslationFunctions();
$this->stubEscapeFunctions();

$post = Mockery::mock( WP_Post::class );
$copy = Mockery::mock( WP_Post::class );

$copy->ID = 789;

$this->permissions_helper
->expects( 'has_scheduled_rewrite_and_republish_copy' )
->with( $post )
Expand All @@ -123,6 +138,14 @@ public function test_get_notice_text_scheduled() {
->with( $post )
->andReturnFalse();

$this->permissions_helper
->expects( 'get_rewrite_and_republish_copy' )
->with( $post )
->andReturn( $copy );

Monkey\Functions\when( '\get_edit_post_link' )
->justReturn( 'http://example.com/edit?post=789' );

Monkey\Functions\expect( '\get_option' )
->twice()
->andReturnValues( [ 'Y/m/d', 'g:i a' ] );
Expand All @@ -132,7 +155,7 @@ public function test_get_notice_text_scheduled() {
->andReturnValues( [ '2020/12/02', '10:30 am' ] );

$this->assertSame(
'A duplicate of this post was made, which is scheduled to replace this post on 2020/12/02 at 10:30 am.',
'A duplicate of this post was made, which is scheduled to replace this post on 2020/12/02 at 10:30 am. <a href="http://example.com/edit?post=789">Edit the duplicate.</a>',
$this->instance->get_notice_text( $post ),
);
}
Expand All @@ -146,9 +169,12 @@ public function test_get_notice_text_scheduled() {
*/
public function test_get_notice_text_copy_in_the_trash() {
$this->stubTranslationFunctions();
$this->stubEscapeFunctions();

$post = Mockery::mock( WP_Post::class );

$post->post_type = 'post';

$this->permissions_helper
->expects( 'has_scheduled_rewrite_and_republish_copy' )
->never();
Expand All @@ -158,8 +184,14 @@ public function test_get_notice_text_copy_in_the_trash() {
->with( $post )
->andReturnTrue();

Monkey\Functions\when( '\admin_url' )
->justReturn( 'http://example.com/wp-admin/edit.php' );

Monkey\Functions\when( '\add_query_arg' )
->justReturn( 'http://example.com/wp-admin/edit.php?post_status=trash&post_type=post' );

$this->assertSame(
'You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate.',
'You can only make one Rewrite & Republish duplicate at a time, and a duplicate of this post already exists in the trash. Permanently delete it if you want to make a new duplicate. <a href="http://example.com/wp-admin/edit.php?post_status=trash&post_type=post">View trash.</a>',
$this->instance->get_notice_text( $post ),
);
}
Expand Down Expand Up @@ -192,6 +224,10 @@ public function test_add_admin_notice_classic() {
->expects( 'get_notice_text' )
->andReturn( 'notice' );

Monkey\Functions\expect( '\wp_kses' )
->with( 'notice', [ 'a' => [ 'href' => [] ] ] )
->andReturn( 'notice' );

$this->instance->add_admin_notice();

$this->expectOutputString( '<div id="message" class="notice notice-warning is-dismissible fade"><p>notice</p></div>' );
Expand Down Expand Up @@ -268,16 +304,17 @@ public function test_add_block_editor_notice() {
'text' => 'notice',
'status' => 'warning',
'isDismissible' => true,
'isHTML' => true,
];

Monkey\Functions\expect( '\wp_json_encode' )
->with( $notice )
->andReturn( '{"text":"notice","status":"warning","isDismissible":true}' );
->andReturn( '{"text":"notice","status":"warning","isDismissible":true,"isHTML":true}' );

Monkey\Functions\expect( '\wp_add_inline_script' )
->with(
'duplicate_post_edit_script',
'duplicatePostNotices.has_rewrite_and_republish_notice = {"text":"notice","status":"warning","isDismissible":true};',
'duplicatePostNotices.has_rewrite_and_republish_notice = {"text":"notice","status":"warning","isDismissible":true,"isHTML":true};',
'before',
);

Expand Down
Loading