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

Abstract next_scheduled function

parent a5dac90b
Branches
Tags
No related merge requests found
...@@ -43,7 +43,7 @@ class Delete_All { ...@@ -43,7 +43,7 @@ class Delete_All {
// Special keys are used to trigger this request, and we need to remove them on redirect. // Special keys are used to trigger this request, and we need to remove them on redirect.
$extra_keys = array( self::ACTION, self::ACTION . '2' ); $extra_keys = array( self::ACTION, self::ACTION . '2' );
$action_scheduled = self::action_next_scheduled( $vars->post_type ); $action_scheduled = Main::get_action_next_scheduled( self::ACTION, $vars->post_type );
if ( empty( $action_scheduled ) ) { if ( empty( $action_scheduled ) ) {
Main::schedule_processing( $vars ); Main::schedule_processing( $vars );
...@@ -126,7 +126,7 @@ class Delete_All { ...@@ -126,7 +126,7 @@ class Delete_All {
$message = __( 'A request to empty the trash is already pending for this post type.', 'bulk-edit-cron-offload' ); $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'] ) { } elseif ( 'edit' === $screen->base && isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
if ( self::action_next_scheduled( $screen->post_type ) ) { if ( Main::get_action_next_scheduled( self::ACTION, $screen->post_type ) ) {
$type = 'warning'; $type = 'warning';
$message = __( 'A pending request to empty the trash will be processed soon.', 'bulk-edit-cron-offload' ); $message = __( 'A pending request to empty the trash will be processed soon.', 'bulk-edit-cron-offload' );
} }
...@@ -155,7 +155,7 @@ class Delete_All { ...@@ -155,7 +155,7 @@ class Delete_All {
return $where; return $where;
} }
if ( self::action_next_scheduled( $q->get( 'post_type' ) ) ) { if ( Main::get_action_next_scheduled( self::ACTION, $q->get( 'post_type' ) ) ) {
$where .= ' AND 0=1'; $where .= ' AND 0=1';
} }
...@@ -189,7 +189,7 @@ class Delete_All { ...@@ -189,7 +189,7 @@ class Delete_All {
} }
// There isn't a pending purge, so one should be permitted. // There isn't a pending purge, so one should be permitted.
if ( ! self::action_next_scheduled( $screen->post_type ) ) { if ( ! Main::get_action_next_scheduled( self::ACTION, $screen->post_type ) ) {
return $caps; return $caps;
} }
...@@ -198,47 +198,6 @@ class Delete_All { ...@@ -198,47 +198,6 @@ class Delete_All {
return $caps; return $caps;
} }
/**
* Find the next scheduled instance of a given action, regardless of arguments
*
* @param string $post_type Post type hook is scheduled for.
* @return array
*/
private static function action_next_scheduled( $post_type ) {
$events = get_option( 'cron' );
if ( ! is_array( $events ) ) {
return array();
}
foreach ( $events as $timestamp => $timestamp_events ) {
// Skip non-event data that Core includes in the option.
if ( ! is_numeric( $timestamp ) ) {
continue;
}
foreach ( $timestamp_events as $action => $action_instances ) {
if ( Main::CRON_EVENT !== $action ) {
continue;
}
foreach ( $action_instances as $instance => $instance_args ) {
$vars = array_shift( $instance_args['args'] );
if ( self::ACTION === $vars->action && $post_type === $vars->post_type ) {
return array(
'timestamp' => $timestamp,
'args' => $vars,
);
}
}
}
}
// No matching event found.
return array();
}
} }
Delete_All::register_hooks(); Delete_All::register_hooks();
...@@ -254,6 +254,48 @@ class Main { ...@@ -254,6 +254,48 @@ class Main {
<?php <?php
} }
/**
* Find the next scheduled instance of a given action, regardless of arguments
*
* @param string $bulk_action Bulk action to filter by.
* @param string $post_type Post type hook is scheduled for.
* @return array
*/
public static function get_action_next_scheduled( $bulk_action, $post_type ) {
$events = get_option( 'cron' );
if ( ! is_array( $events ) ) {
return array();
}
foreach ( $events as $timestamp => $timestamp_events ) {
// Skip non-event data that Core includes in the option.
if ( ! is_numeric( $timestamp ) ) {
continue;
}
foreach ( $timestamp_events as $action => $action_instances ) {
if ( self::CRON_EVENT !== $action ) {
continue;
}
foreach ( $action_instances as $instance => $instance_args ) {
$vars = array_shift( $instance_args['args'] );
if ( $bulk_action === $vars->action && $post_type === $vars->post_type ) {
return array(
'timestamp' => $timestamp,
'args' => $vars,
);
}
}
}
}
// No matching event found.
return array();
}
/** /**
* Gather pending events for given conditions * Gather pending events for given conditions
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment