From cbbf536b9209f862a79fc7107ac77e285b45ec15 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Thu, 14 Sep 2017 19:51:51 -0700
Subject: [PATCH] Split post-redirect notices from the bits that are specific
 to the action

---
 includes/class-delete-all.php           | 30 +++++++++----
 includes/class-delete-permanently.php   | 30 +++++++++----
 includes/class-edit.php                 | 30 +++++++++----
 includes/class-move-to-trash.php        | 30 +++++++++----
 includes/class-restore-from-trash.php   | 30 +++++++++----
 includes/trait-bulk-actions.php         | 40 +++++++++++++++++-
 languages/bulk-actions-cron-offload.pot | 56 ++++++++++++-------------
 7 files changed, 172 insertions(+), 74 deletions(-)

diff --git a/includes/class-delete-all.php b/includes/class-delete-all.php
index 149e17f..a7d15c7 100644
--- a/includes/class-delete-all.php
+++ b/includes/class-delete-all.php
@@ -108,6 +108,8 @@ class Delete_All {
 
 	/**
 	 * Let the user know what's going on
+	 *
+	 * Not used for post-request redirect
 	 */
 	public static function admin_notices() {
 		$screen = get_current_screen();
@@ -115,15 +117,7 @@ class Delete_All {
 		$type    = '';
 		$message = '';
 
-		if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) {
-			if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) {
-				$type    = 'success';
-				$message = __( 'Success! The trash will be emptied shortly.', 'bulk-actions-cron-offload' );
-			} else {
-				$type    = 'error';
-				$message = __( 'A request to empty the trash is already pending for this post type.', 'bulk-actions-cron-offload' );
-			}
-		} elseif ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
+		if ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
 			if ( Main::get_action_next_scheduled( self::ACTION, $screen->post_type ) ) {
 				$type    = 'warning';
 				$message = __( 'A pending request to empty the trash will be processed soon.', 'bulk-actions-cron-offload' );
@@ -133,6 +127,24 @@ class Delete_All {
 		Main::render_admin_notice( $type, $message );
 	}
 
+	/**
+	 * Provide post-redirect success message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_success_message() {
+		return __( 'Success! The trash will be emptied shortly.', 'bulk-actions-cron-offload' );
+	}
+
+	/**
+	 * Provide post-redirect error message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_error_message() {
+		return __( 'A request to empty the trash is already pending for this post type.', 'bulk-actions-cron-offload' );
+	}
+
 	/**
 	 * When a delete is pending for a given post type, hide those posts in the admin
 	 *
diff --git a/includes/class-delete-permanently.php b/includes/class-delete-permanently.php
index 4b80dff..7400593 100644
--- a/includes/class-delete-permanently.php
+++ b/includes/class-delete-permanently.php
@@ -76,6 +76,8 @@ class Delete_Permanently {
 
 	/**
 	 * Let the user know what's going on
+	 *
+	 * Not used for post-request redirect
 	 */
 	public static function admin_notices() {
 		$screen = get_current_screen();
@@ -83,15 +85,7 @@ class Delete_Permanently {
 		$type    = '';
 		$message = '';
 
-		if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) {
-			if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) {
-				$type    = 'success';
-				$message = __( 'Success! The selected posts will be deleted shortly.', 'bulk-actions-cron-offload' );
-			} else {
-				$type    = 'error';
-				$message = __( 'The selected posts are already scheduled to be deleted.', 'bulk-actions-cron-offload' );
-			}
-		} elseif ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
+		if ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
 			if ( Main::get_post_ids_for_pending_events( self::ACTION, $screen->post_type, 'trash' ) ) {
 				$type    = 'warning';
 				$message = __( 'Some items that would normally be shown here are waiting to be deleted permanently. These items are hidden until then.', 'bulk-actions-cron-offload' );
@@ -101,6 +95,24 @@ class Delete_Permanently {
 		Main::render_admin_notice( $type, $message );
 	}
 
+	/**
+	 * Provide post-redirect success message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_success_message() {
+		return __( 'Success! The selected posts will be deleted shortly.', 'bulk-actions-cron-offload' );
+	}
+
+	/**
+	 * Provide post-redirect error message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_error_message() {
+		return __( 'The selected posts are already scheduled to be deleted.', 'bulk-actions-cron-offload' );
+	}
+
 	/**
 	 * When a delete is pending for a given post type, hide those posts in the admin
 	 *
diff --git a/includes/class-edit.php b/includes/class-edit.php
index 42185cb..5361b30 100644
--- a/includes/class-edit.php
+++ b/includes/class-edit.php
@@ -77,6 +77,8 @@ class Edit {
 
 	/**
 	 * Let the user know what's going on
+	 *
+	 * Not used for post-request redirect
 	 */
 	public static function admin_notices() {
 		$screen = get_current_screen();
@@ -84,15 +86,7 @@ class Edit {
 		$type    = '';
 		$message = '';
 
-		if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) {
-			if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) {
-				$type    = 'success';
-				$message = __( 'Success! The selected posts will be edited shortly.', 'bulk-actions-cron-offload' );
-			} else {
-				$type    = 'error';
-				$message = __( 'The requested edits are already pending for the chosen posts.', 'bulk-actions-cron-offload' );
-			}
-		} elseif ( 'edit' === $screen->base ) {
+		if ( 'edit' === $screen->base ) {
 			if ( isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
 				return;
 			}
@@ -109,6 +103,24 @@ class Edit {
 		Main::render_admin_notice( $type, $message );
 	}
 
+	/**
+	 * Provide post-redirect success message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_success_message() {
+		return __( 'Success! The selected posts will be edited shortly.', 'bulk-actions-cron-offload' );
+	}
+
+	/**
+	 * Provide post-redirect error message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_error_message() {
+		return __( 'The requested edits are already pending for the chosen posts.', 'bulk-actions-cron-offload' );
+	}
+
 	/**
 	 * When an edit is pending for a given post type, hide those posts in the admin
 	 *
diff --git a/includes/class-move-to-trash.php b/includes/class-move-to-trash.php
index d087726..74588f7 100644
--- a/includes/class-move-to-trash.php
+++ b/includes/class-move-to-trash.php
@@ -76,6 +76,8 @@ class Move_To_Trash {
 
 	/**
 	 * Let the user know what's going on
+	 *
+	 * Not used for post-request redirect
 	 */
 	public static function admin_notices() {
 		$screen = get_current_screen();
@@ -83,15 +85,7 @@ class Move_To_Trash {
 		$type    = '';
 		$message = '';
 
-		if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) {
-			if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) {
-				$type    = 'success';
-				$message = __( 'Success! The selected posts will be moved to the trash shortly.', 'bulk-actions-cron-offload' );
-			} else {
-				$type    = 'error';
-				$message = __( 'The selected posts are already scheduled to be moved to the trash.', 'bulk-actions-cron-offload' );
-			}
-		} elseif ( 'edit' === $screen->base ) {
+		if ( 'edit' === $screen->base ) {
 			if ( isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
 				return;
 			}
@@ -108,6 +102,24 @@ class Move_To_Trash {
 		Main::render_admin_notice( $type, $message );
 	}
 
+	/**
+	 * Provide post-redirect success message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_success_message() {
+		return __( 'Success! The selected posts will be moved to the trash shortly.', 'bulk-actions-cron-offload' );
+	}
+
+	/**
+	 * Provide post-redirect error message
+	 *
+	 * @return string
+	 */
+	public static function admin_notice_error_message() {
+		return __( 'The selected posts are already scheduled to be moved to the trash.', 'bulk-actions-cron-offload' );
+	}
+
 	/**
 	 * When a move is pending for a given post type, hide those posts in the admin
 	 *
diff --git a/includes/class-restore-from-trash.php b/includes/class-restore-from-trash.php
index 65092a7..c3bc234 100644
--- a/includes/class-restore-from-trash.php
+++ b/includes/class-restore-from-trash.php
@@ -76,6 +76,8 @@ class Restore_From_Trash {
 
 	/**
 	 * Let the user know what's going on
+	 *
+	 * Not used for post-request redirect
 	 */
 	public static function admin_notices() {
 		$screen = get_current_screen();
@@ -83,15 +85,7 @@ class Restore_From_Trash {
 		$type    = '';
 		$message = '';
 
-		if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) {
-			if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) {
-				$type    = 'success';
-				$message = __( 'Success! The selected posts will be restored shortly.', 'bulk-actions-cron-offload' );
-			} else {
-				$type    = 'error';
-				$message = __( 'The selected posts are already scheduled to be restored.', 'bulk-actions-cron-offload' );
-			}
-		} elseif ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
+		if ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
 			if ( Main::get_post_ids_for_pending_events( self::ACTION, $screen->post_type, 'trash' ) ) {
 				$type    = 'warning';
 				$message = __( 'Some items that would normally be shown here are waiting to be restored from the trash. These items are hidden until they are restored.', 'bulk-actions-cron-offload' );
@@ -101,6 +95,24 @@ class Restore_From_Trash {
 		Main::render_admin_notice( $type, $message );
 	}
 
+	/**
+	 * Provide post-redirect success message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_success_message() {
+		return __( 'Success! The selected posts will be restored shortly.', 'bulk-actions-cron-offload' );
+	}
+
+	/**
+	 * Provide post-redirect error message
+	 *
+	 * @retun string
+	 */
+	public static function admin_notice_error_message() {
+		return __( 'The selected posts are already scheduled to be restored.', 'bulk-actions-cron-offload' );
+	}
+
 	/**
 	 * When a restore is pending for a given post type, hide those posts in the admin
 	 *
diff --git a/includes/trait-bulk-actions.php b/includes/trait-bulk-actions.php
index 163e574..59dcf4f 100644
--- a/includes/trait-bulk-actions.php
+++ b/includes/trait-bulk-actions.php
@@ -15,7 +15,7 @@ trait Bulk_Actions {
 		add_action( Main::build_hook( self::ACTION ), array( __CLASS__, 'process' ) );
 		add_action( Main::build_cron_hook( self::ACTION ), array( __CLASS__, 'process_via_cron' ) );
 
-		add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
+		add_action( 'admin_notices', array( __CLASS__, 'render_admin_notices' ) );
 		add_filter( 'posts_where', array( __CLASS__, 'hide_posts' ), 999, 2 );
 
 		add_filter( 'removable_query_args', array( __CLASS__, 'remove_notice_arg' ) );
@@ -44,6 +44,44 @@ trait Bulk_Actions {
 		}
 	}
 
+	/**
+	 * Render the post-redirect notice, or hand off to class for other notices
+	 */
+	public static function render_admin_notices() {
+		if ( isset( $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) ) {
+			if ( 1 === (int) $_REQUEST[ self::ADMIN_NOTICE_KEY ] ) {
+				$type    = 'success';
+				$message = self::admin_notice_success_message();
+			} else {
+				$type    = 'error';
+				$message = self::admin_notice_error_message();
+			}
+
+			Main::render_admin_notice( $type, $message );
+			return;
+		}
+
+		self::admin_notices();
+	}
+
+	/**
+	 * Provide translated success message for bulk action
+	 *
+	 * @return string
+	 */
+	public static function admin_notice_success_message() {
+		return '';
+	}
+
+	/**
+	 * Provide translated error message for bulk action
+	 *
+	 * @return string
+	 */
+	public static function admin_notice_error_message() {
+		return '';
+	}
+
 	/**
 	 * When an edit is pending for a given post type, hide those posts in the admin
 	 *
diff --git a/languages/bulk-actions-cron-offload.pot b/languages/bulk-actions-cron-offload.pot
index b5519b1..b7fc9a7 100644
--- a/languages/bulk-actions-cron-offload.pot
+++ b/languages/bulk-actions-cron-offload.pot
@@ -5,7 +5,7 @@ msgstr ""
 "Project-Id-Version: Bulk Actions Cron Offload 1.0\n"
 "Report-Msgid-Bugs-To: "
 "https://wordpress.org/support/plugin/bulk-actions-cron-offload\n"
-"POT-Creation-Date: 2017-09-15 00:51:21+00:00\n"
+"POT-Creation-Date: 2017-09-15 02:51:23+00:00\n"
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=utf-8\n"
 "Content-Transfer-Encoding: 8bit\n"
@@ -26,77 +26,77 @@ msgstr ""
 "X-Textdomain-Support: yes\n"
 
 #: includes/class-delete-all.php:123
+msgid "A pending request to empty the trash will be processed soon."
+msgstr ""
+
+#: includes/class-delete-all.php:136
 msgid "Success! The trash will be emptied shortly."
 msgstr ""
 
-#: includes/class-delete-all.php:126
+#: includes/class-delete-all.php:145
 msgid "A request to empty the trash is already pending for this post type."
 msgstr ""
 
-#: includes/class-delete-all.php:131
-msgid "A pending request to empty the trash will be processed soon."
+#: includes/class-delete-permanently.php:91
+msgid ""
+"Some items that would normally be shown here are waiting to be deleted "
+"permanently. These items are hidden until then."
 msgstr ""
 
-#: includes/class-delete-permanently.php:111
+#: includes/class-delete-permanently.php:104
 msgid "Success! The selected posts will be deleted shortly."
 msgstr ""
 
-#: includes/class-delete-permanently.php:114
+#: includes/class-delete-permanently.php:113
 msgid "The selected posts are already scheduled to be deleted."
 msgstr ""
 
-#: includes/class-delete-permanently.php:119
+#: includes/class-edit.php:99
 msgid ""
-"Some items that would normally be shown here are waiting to be deleted "
-"permanently. These items are hidden until then."
+"Some items that would normally be shown here are waiting to be edited. "
+"These items are hidden until they are processed."
 msgstr ""
 
 #: includes/class-edit.php:112
 msgid "Success! The selected posts will be edited shortly."
 msgstr ""
 
-#: includes/class-edit.php:115
+#: includes/class-edit.php:121
 msgid "The requested edits are already pending for the chosen posts."
 msgstr ""
 
-#: includes/class-edit.php:127
-msgid ""
-"Some items that would normally be shown here are waiting to be edited. "
-"These items are hidden until they are processed."
-msgstr ""
-
 #: includes/class-main.php:227
 msgid "The requested bulk action was not processed because no posts were selected."
 msgstr ""
 
+#: includes/class-move-to-trash.php:98
+msgid ""
+"Some items that would normally be shown here are waiting to be moved to the "
+"trash. These items are hidden until they are moved."
+msgstr ""
+
 #: includes/class-move-to-trash.php:111
 msgid "Success! The selected posts will be moved to the trash shortly."
 msgstr ""
 
-#: includes/class-move-to-trash.php:114
+#: includes/class-move-to-trash.php:120
 msgid "The selected posts are already scheduled to be moved to the trash."
 msgstr ""
 
-#: includes/class-move-to-trash.php:126
+#: includes/class-restore-from-trash.php:91
 msgid ""
-"Some items that would normally be shown here are waiting to be moved to the "
-"trash. These items are hidden until they are moved."
+"Some items that would normally be shown here are waiting to be restored "
+"from the trash. These items are hidden until they are restored."
 msgstr ""
 
-#: includes/class-restore-from-trash.php:111
+#: includes/class-restore-from-trash.php:104
 msgid "Success! The selected posts will be restored shortly."
 msgstr ""
 
-#: includes/class-restore-from-trash.php:114
+#: includes/class-restore-from-trash.php:113
 msgid "The selected posts are already scheduled to be restored."
 msgstr ""
 
-#: includes/class-restore-from-trash.php:119
-msgid ""
-"Some items that would normally be shown here are waiting to be restored "
-"from the trash. These items are hidden until they are restored."
-msgstr ""
-
 #. Plugin Name of the plugin/theme
 msgid "Bulk Actions Cron Offload"
 msgstr ""
-- 
GitLab