Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the watcher (and their tests) to make some methods private #379

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
87 changes: 51 additions & 36 deletions src/watchers/copied-post-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,6 @@ public function register_hooks() {
\add_action( 'enqueue_block_editor_assets', [ $this, 'add_block_editor_notice' ], 11 );
}

/**
* Generates the translated text for the notice.
*
* @param WP_Post $post The current post object.
*
* @return string The translated text for the notice.
*/
public function get_notice_text( $post ) {
if ( $this->permissions_helper->has_trashed_rewrite_and_republish_copy( $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'
);
}

$scheduled_copy = $this->permissions_helper->has_scheduled_rewrite_and_republish_copy( $post );
if ( ! $scheduled_copy ) {
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'
);
}

return \sprintf(
/* translators: %1$s: scheduled date of the copy, %2$s: scheduled time of the copy. */
\__(
'A duplicate of this post was made, which is scheduled to replace this post on %1$s at %2$s.',
'duplicate-post'
),
\get_the_time( \get_option( 'date_format' ), $scheduled_copy ),
\get_the_time( \get_option( 'time_format' ), $scheduled_copy )
);
}

/**
* Shows a notice on the Classic editor.
*
Expand All @@ -90,7 +56,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 ) )
. \esc_html( $this->get_notice_copy( $post ) )
. '</p></div>';
}
}
Expand All @@ -110,7 +76,7 @@ public function add_block_editor_notice() {
if ( $this->permissions_helper->has_rewrite_and_republish_copy( $post ) ) {

$notice = [
'text' => $this->get_notice_text( $post ),
'text' => $this->get_notice_copy( $post ),
'status' => 'warning',
'isDismissible' => true,
];
Expand All @@ -122,4 +88,53 @@ public function add_block_editor_notice() {
);
}
}

/**
* Generates the translated text for the notice.
*
* @param WP_Post $post The current post object.
*
* @return string The translated text for the notice.
*/
private function get_notice_copy( $post ) {
if ( $this->permissions_helper->has_trashed_rewrite_and_republish_copy( $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'
);
}

$scheduled_copy = $this->permissions_helper->has_scheduled_rewrite_and_republish_copy( $post );
if ( ! $scheduled_copy ) {
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'
);
}

return \sprintf(
/* translators: %1$s: scheduled date of the copy, %2$s: scheduled time of the copy. */
\__(
'A duplicate of this post was made, which is scheduled to replace this post on %1$s at %2$s.',
'duplicate-post'
),
\get_the_time( \get_option( 'date_format' ), $scheduled_copy ),
\get_the_time( \get_option( 'time_format' ), $scheduled_copy )
);
}

/**
* Generates the translated text for the republished notice.
*
* @deprecated 4.6
* @codeCoverageIgnore
*
* @param WP_Post $post The current post object.
*
* @return string The translated text for the republished notice.
*/
public function get_notice_text( $post ) {
\_deprecated_function( __METHOD__, '4.6', self::class . '::get_notice_copy' );
return $this->get_notice_copy( $post );
}
}
41 changes: 27 additions & 14 deletions src/watchers/original-post-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ public function register_hooks() {
\add_action( 'enqueue_block_editor_assets', [ $this, 'add_block_editor_notice' ], 11 );
}

/**
* Generates the translated text for the notice.
*
* @return string The translated text for the notice.
*/
public function get_notice_text() {
return \__(
'The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.',
'duplicate-post'
);
}

/**
* Shows a notice on the Classic editor.
*
Expand All @@ -72,7 +60,7 @@ public function add_admin_notice() {

if ( $this->permissions_helper->has_original_changed( $post ) ) {
print '<div id="message" class="notice notice-warning is-dismissible fade"><p>'
. \esc_html( $this->get_notice_text() )
. \esc_html( $this->get_notice_copy() )
. '</p></div>';
}
}
Expand All @@ -92,7 +80,7 @@ public function add_block_editor_notice() {
if ( $this->permissions_helper->has_original_changed( $post ) ) {

$notice = [
'text' => $this->get_notice_text(),
'text' => $this->get_notice_copy(),
'status' => 'warning',
'isDismissible' => true,
];
Expand All @@ -104,4 +92,29 @@ public function add_block_editor_notice() {
);
}
}

/**
* Generates the translated text for the notice.
*
* @return string The translated text for the notice.
*/
private function get_notice_copy() {
return \__(
'The original post has been edited in the meantime. If you click "Republish", this rewritten post will replace the original post.',
'duplicate-post'
);
}

/**
* Generates the translated text for the notice.
*
* @deprecated 4.6
* @codeCoverageIgnore
*
* @return string The translated text for the notice.
*/
public function get_notice_text() {
\_deprecated_function( __METHOD__, '4.6', self::class . '::get_notice_copy' );
return $this->get_notice_copy();
}
}
41 changes: 27 additions & 14 deletions src/watchers/republished-post-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ public function add_removable_query_args( $removable_query_args ) {
return $removable_query_args;
}

/**
* Generates the translated text for the republished notice.
*
* @return string The translated text for the republished notice.
*/
public function get_notice_text() {
return \__(
'Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.',
'duplicate-post'
);
}

/**
* Shows a notice on the Classic editor.
*
Expand All @@ -80,7 +68,7 @@ public function add_admin_notice() {

if ( ! empty( $_REQUEST['dprepublished'] ) ) {
echo '<div id="message" class="notice notice-success is-dismissible"><p>'
. \esc_html( $this->get_notice_text() )
. \esc_html( $this->get_notice_copy() )
. '</p></div>';
}
}
Expand All @@ -93,7 +81,7 @@ public function add_admin_notice() {
public function add_block_editor_notice() {
if ( ! empty( $_REQUEST['dprepublished'] ) ) {
$notice = [
'text' => $this->get_notice_text(),
'text' => $this->get_notice_copy(),
'status' => 'success',
'isDismissible' => true,
];
Expand All @@ -105,4 +93,29 @@ public function add_block_editor_notice() {
);
}
}

/**
* Generates the translated text for the notice.
*
* @return string The translated text for the notice.
*/
private function get_notice_copy() {
return \__(
'Your original post has been replaced with the rewritten post. You are now viewing the (rewritten) original post.',
'duplicate-post'
);
}

/**
* Generates the translated text for the republished notice.
*
* @deprecated 4.6
* @codeCoverageIgnore
*
* @return string The translated text for the republished notice.
*/
public function get_notice_text() {
\_deprecated_function( __METHOD__, '4.6', self::class . '::get_notice_copy' );
return $this->get_notice_copy();
}
}
Loading