Skip to content
Snippets Groups Projects
Commit 79f33fd2 authored by Erick Hitter's avatar Erick Hitter
Browse files

Switch to one cron event for all

parent f534e293
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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 ) );
}
}
......
......@@ -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 );
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment