Skip to content

Commit

Permalink
Refactor the watchers (and their tests) to make some methods private
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobattocchi committed Sep 19, 2024
1 parent 3080bae commit a4f23a5
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 178 deletions.
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

0 comments on commit a4f23a5

Please sign in to comment.