From 79f33fd2c0b48e79c89e18657a8e2f1047d91355 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Tue, 12 Sep 2017 20:25:54 -0700
Subject: [PATCH] Switch to one cron event for all

---
 includes/class-delete-all.php    | 19 ++++++++----------
 includes/class-main.php          | 33 ++++++++++++++++++++++++++++----
 includes/class-move-to-trash.php |  4 ++--
 3 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/includes/class-delete-all.php b/includes/class-delete-all.php
index f45a71b..f5d4412 100644
--- a/includes/class-delete-all.php
+++ b/includes/class-delete-all.php
@@ -14,8 +14,6 @@ class Delete_All {
 	/**
 	 * Class constants
 	 */
-	const CRON_EVENT = 'bulk_edit_cron_offload_delete_all';
-
 	const ADMIN_NOTICE_KEY = 'bulk_edit_cron_offload_deleted_all';
 
 	/**
@@ -23,7 +21,7 @@ class Delete_All {
 	 */
 	public static function register_hooks() {
 		add_action( Main::build_hook( 'delete_all' ), array( __CLASS__, 'process' ) );
-		add_action( self::CRON_EVENT, array( __CLASS__, 'process_via_cron' ) );
+		add_action( Main::build_cron_hook( 'delete_all' ), array( __CLASS__, 'process_via_cron' ) );
 
 		add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
 		add_filter( 'posts_where', array( __CLASS__, 'hide_posts_pending_delete' ), 999, 2 );
@@ -43,10 +41,10 @@ class Delete_All {
 		// Special keys are used to trigger this request, and we need to remove them on redirect.
 		$extra_keys = array( 'delete_all', 'delete_all2' );
 
-		$action_scheduled = self::action_next_scheduled( self::CRON_EVENT, $vars->post_type );
+		$action_scheduled = self::action_next_scheduled( $vars->post_type );
 
 		if ( empty( $action_scheduled ) ) {
-			Main::schedule_processing( self::CRON_EVENT, $vars );
+			Main::schedule_processing( $vars );
 			Main::do_admin_redirect( self::ADMIN_NOTICE_KEY, true, $extra_keys );
 		} else {
 			Main::do_admin_redirect( self::ADMIN_NOTICE_KEY, false, $extra_keys );
@@ -123,7 +121,7 @@ class Delete_All {
 				$message = __( 'A request to empty the trash is already pending for this post type.', 'bulk-edit-cron-offload' );
 			}
 		} elseif ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
-			if ( self::action_next_scheduled( self::CRON_EVENT, $screen->post_type ) ) {
+			if ( self::action_next_scheduled( $screen->post_type ) ) {
 				$class   = 'notice-warning';
 				$message = __( 'A pending request to empty the trash will be processed soon.', 'bulk-edit-cron-offload' );
 			}
@@ -161,7 +159,7 @@ class Delete_All {
 			return $where;
 		}
 
-		if ( self::action_next_scheduled( self::CRON_EVENT, $q->get( 'post_type' ) ) ) {
+		if ( self::action_next_scheduled( $q->get( 'post_type' ) ) ) {
 			$where .= ' AND 0=1';
 		}
 
@@ -195,7 +193,7 @@ class Delete_All {
 		}
 
 		// There isn't a pending purge, so one should be permitted.
-		if ( ! self::action_next_scheduled( self::CRON_EVENT, $screen->post_type ) ) {
+		if ( ! self::action_next_scheduled( $screen->post_type ) ) {
 			return $caps;
 		}
 
@@ -208,11 +206,10 @@ class Delete_All {
 	/**
 	 * Find the next scheduled instance of a given action, regardless of arguments
 	 *
-	 * @param  string $action_to_check Hook to search for.
 	 * @param  string $post_type       Post type hook is scheduled for.
 	 * @return array
 	 */
-	private static function action_next_scheduled( $action_to_check, $post_type ) {
+	private static function action_next_scheduled( $post_type ) {
 		$events = get_option( 'cron' );
 
 		if ( ! is_array( $events ) ) {
@@ -226,7 +223,7 @@ class Delete_All {
 			}
 
 			foreach ( $timestamp_events as $action => $action_instances ) {
-				if ( $action !== $action_to_check ) {
+				if ( Main::CRON_EVENT !== $action ) {
 					continue;
 				}
 
diff --git a/includes/class-main.php b/includes/class-main.php
index b2d8d78..8a27bbc 100644
--- a/includes/class-main.php
+++ b/includes/class-main.php
@@ -17,12 +17,28 @@ class Main {
 	const ACTION = 'a8c_bulk_edit_cron_';
 
 	/**
-	 * Register action
+	 * Common cron action
+	 */
+	const CRON_EVENT = 'bulk_edit_cron_offload';
+
+	/**
+	 * Register actions
 	 */
 	public static function load() {
+		add_action( self::CRON_EVENT, array( __CLASS__, 'do_cron' ) );
+
 		add_action( 'load-edit.php', array( __CLASS__, 'intercept' ) );
 	}
 
+	/**
+	 * Run appropriate cron callback
+	 *
+	 * @param object $vars Bulk-request variables.
+	 */
+	public static function do_cron( $vars ) {
+		do_action( self::build_cron_hook( $vars->action ), $vars );
+	}
+
 	/**
 	 * Call appropriate handler
 	 */
@@ -152,6 +168,16 @@ class Main {
 		return self::ACTION . $action;
 	}
 
+	/**
+	 * Build a cron hook specific to a bulk request
+	 *
+	 * @param  string $action Bulk action to register cron callback for.
+	 * @return string
+	 */
+	public static function build_cron_hook( $action ) {
+		return self::ACTION . $action . '_callback';
+	}
+
 	/**
 	 * Unset flags Core uses to trigger bulk processing
 	 */
@@ -188,12 +214,11 @@ class Main {
 	/**
 	 * Create cron event
 	 *
-	 * @param string $event Cron action.
 	 * @param object $vars Bulk-request variables.
 	 * @return bool
 	 */
-	public static function schedule_processing( $event, $vars ) {
-		return false !== wp_schedule_single_event( time(), $event, array( $vars ) );
+	public static function schedule_processing( $vars ) {
+		return false !== wp_schedule_single_event( time(), self::CRON_EVENT, array( $vars ) );
 	}
 }
 
diff --git a/includes/class-move-to-trash.php b/includes/class-move-to-trash.php
index c0c25e8..5a28774 100644
--- a/includes/class-move-to-trash.php
+++ b/includes/class-move-to-trash.php
@@ -21,7 +21,7 @@ class Move_To_Trash {
 	 */
 	public static function register_hooks() {
 		add_action( Main::build_hook( 'trash' ), array( __CLASS__, 'process' ) );
-		add_action( self::CRON_EVENT, array( __CLASS__, 'process_via_cron' ) );
+		add_action( Main::build_cron_hook( 'trash' ), array( __CLASS__, 'process_via_cron' ) );
 	}
 
 	/**
@@ -30,7 +30,7 @@ class Move_To_Trash {
 	 * @param object $vars Bulk-request variables.
 	 */
 	public static function process( $vars ) {
-		Main::schedule_processing( self::CRON_EVENT, $vars );
+		Main::schedule_processing( $vars );
 		Main::do_admin_redirect( self::ADMIN_NOTICE_KEY, true );
 	}
 
-- 
GitLab