diff --git a/includes/class-main.php b/includes/class-main.php index da589245c3542312eec283ffbe03a7e98bc03404..74a78d0a18eb972e6a22cd594470a02d5b34ed3e 100644 --- a/includes/class-main.php +++ b/includes/class-main.php @@ -253,6 +253,78 @@ class Main { </div> <?php } + + /** + * Gather pending events for given conditions + * + * @param string $bulk_action Bulk action to filter by. + * @param string $post_type Post type needing exclusion. + * @param string $post_status Post status to filter by. + * @return array + */ + public static function get_all_pending_events_for_action( $bulk_action, $post_type, $post_status ) { + $events = get_option( 'cron' ); + + if ( ! is_array( $events ) ) { + return array(); + } + + $ids = 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 ) { + if ( $post_status === $vars->post_status || 'all' === $vars->post_status || 'all' === $post_status ) { + $ids[] = array( + 'timestamp' => $timestamp, + 'args' => $vars, + ); + } + } + } + } + } + + return $ids; + } + + /** + * Gather IDs of objects for given conditions + * + * @param string $bulk_action Bulk action to filter by. + * @param string $post_type Post type needing exclusion. + * @param string $post_status Post status to filter by. + * @return array + */ + public static function get_post_ids_for_pending_events( $bulk_action, $post_type, $post_status ) { + $events = wp_list_pluck( self::get_all_pending_events_for_action( $bulk_action, $post_type, $post_status ), 'args' ); + $events = wp_list_pluck( $events, 'posts' ); + + $ids = array(); + + foreach ( $events as $ids_to_merge ) { + $ids = array_merge( $ids, $ids_to_merge ); + } + + if ( ! empty( $ids ) ) { + $ids = array_map( 'absint', $ids ); + $ids = array_unique( $ids ); + } + + return $ids; + } } Main::load(); diff --git a/includes/class-move-to-trash.php b/includes/class-move-to-trash.php index 3350e81e2ac1ef0cb80125b03c6f1287068274c7..94fb84a3bbc9e751383db70c80b8b75a320921c2 100644 --- a/includes/class-move-to-trash.php +++ b/includes/class-move-to-trash.php @@ -118,9 +118,10 @@ class Move_To_Trash { return; } - $status = isset( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all'; + $status = isset( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all'; + $pending = Main::get_post_ids_for_pending_events( self::ACTION, $screen->post_type, $status ); - if ( self::get_all_pending_actions( $screen->post_type, $status ) ) { + if ( ! empty( $pending ) ) { $type = 'warning'; $message = __( 'Some items that would normally be shown here are waiting to be moved to the trash. These items are hidden until they are moved.', 'bulk-edit-cron-offload' ); } @@ -149,7 +150,7 @@ class Move_To_Trash { return $where; } - $post__not_in = self::get_post_ids_pending_move( $q->get( 'post_type' ), $q->get( 'post_status' ) ); + $post__not_in = Main::get_post_ids_for_pending_events( self::ACTION, $q->get( 'post_type' ), $q->get( 'post_status' ) ); if ( ! empty( $post__not_in ) ) { $post__not_in = implode( ',', $post__not_in ); @@ -158,76 +159,6 @@ class Move_To_Trash { return $where; } - - /** - * Gather all pending events for a given post type - * - * @param string $post_type Post type needing exclusion. - * @param string $post_status Post status to filter by. - * @return array - */ - private static function get_all_pending_actions( $post_type, $post_status ) { - $events = get_option( 'cron' ); - - if ( ! is_array( $events ) ) { - return array(); - } - - $ids = 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 ) { - if ( $post_status === $vars->post_status || 'all' === $vars->post_status || 'all' === $post_status ) { - $ids[] = array( - 'timestamp' => $timestamp, - 'args' => $vars, - ); - } - } - } - } - } - - return $ids; - } - - /** - * Gather IDs of objects pending move to trash, with given post type - * - * @param string $post_type Post type needing exclusion. - * @param string $post_status Post status to filter by. - * @return array - */ - private static function get_post_ids_pending_move( $post_type, $post_status ) { - $events = wp_list_pluck( self::get_all_pending_actions( $post_type, $post_status ), 'args' ); - $events = wp_list_pluck( $events, 'posts' ); - - $ids = array(); - - foreach ( $events as $ids_to_merge ) { - $ids = array_merge( $ids, $ids_to_merge ); - } - - if ( ! empty( $ids ) ) { - $ids = array_map( 'absint', $ids ); - $ids = array_unique( $ids ); - } - - return $ids; - } } Move_To_Trash::register_hooks(); diff --git a/includes/class-restore-from-trash.php b/includes/class-restore-from-trash.php index dcfa2be76e6c1a7233cd966c36ea56dba6562fe7..da005d3fc2f1d77133cb2bdc8b549dd7be51063b 100644 --- a/includes/class-restore-from-trash.php +++ b/includes/class-restore-from-trash.php @@ -120,7 +120,7 @@ class Restore_From_Trash { // // $status = isset( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all'; // -// if ( self::get_all_pending_actions( $screen->post_type, $status ) ) { +// if ( Main::get_post_ids_for_pending_events( self::ACTION, $screen->post_type, $status ) ) { // $type = 'warning'; // $message = __( 'Some items that would normally be shown here are waiting to be moved to the trash. These items are hidden until they are moved.', 'bulk-edit-cron-offload' ); // } @@ -149,7 +149,7 @@ class Restore_From_Trash { return $where; } - $post__not_in = self::get_post_ids_pending_move( $q->get( 'post_type' ), $q->get( 'post_status' ) ); + $post__not_in = Main::get_post_ids_for_pending_events( self::ACTION, $q->get( 'post_type' ), $q->get( 'post_status' ) ); if ( ! empty( $post__not_in ) ) { $post__not_in = implode( ',', $post__not_in ); @@ -158,76 +158,6 @@ class Restore_From_Trash { return $where; } - - /** - * Gather all pending events for a given post type - * - * @param string $post_type Post type needing exclusion. - * @param string $post_status Post status to filter by. - * @return array - */ - private static function get_all_pending_actions( $post_type, $post_status ) { - $events = get_option( 'cron' ); - - if ( ! is_array( $events ) ) { - return array(); - } - - $ids = 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 ) { - if ( $post_status === $vars->post_status || 'all' === $vars->post_status || 'all' === $post_status ) { - $ids[] = array( - 'timestamp' => $timestamp, - 'args' => $vars, - ); - } - } - } - } - } - - return $ids; - } - - /** - * Gather IDs of objects pending move to trash, with given post type - * - * @param string $post_type Post type needing exclusion. - * @param string $post_status Post status to filter by. - * @return array - */ - private static function get_post_ids_pending_move( $post_type, $post_status ) { - $events = wp_list_pluck( self::get_all_pending_actions( $post_type, $post_status ), 'args' ); - $events = wp_list_pluck( $events, 'posts' ); - - $ids = array(); - - foreach ( $events as $ids_to_merge ) { - $ids = array_merge( $ids, $ids_to_merge ); - } - - if ( ! empty( $ids ) ) { - $ids = array_map( 'absint', $ids ); - $ids = array_unique( $ids ); - } - - return $ids; - } } Restore_From_Trash::register_hooks();