WP_CLI
1 year ago
abstracts
1 year ago
actions
1 year ago
data-stores
1 year ago
migration
1 year ago
schedules
1 year ago
schema
1 year ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
1 year ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
4 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
4 years ago
index.php
3 years ago
ActionScheduler_ListTable.php
386 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class ActionScheduler_ListTable extends ActionScheduler_Abstract_ListTable { |
| 4 | protected $package = 'action-scheduler'; |
| 5 | protected $columns = array(); |
| 6 | protected $row_actions = array(); |
| 7 | protected $store; |
| 8 | protected $logger; |
| 9 | protected $runner; |
| 10 | protected $bulk_actions = array(); |
| 11 | protected static $did_notification = false; |
| 12 | private static $time_periods; |
| 13 | public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
| 14 | $this->store = $store; |
| 15 | $this->logger = $logger; |
| 16 | $this->runner = $runner; |
| 17 | $this->table_header = __( 'Scheduled Actions', 'action-scheduler' ); |
| 18 | $this->bulk_actions = array( |
| 19 | 'delete' => __( 'Delete', 'action-scheduler' ), |
| 20 | ); |
| 21 | $this->columns = array( |
| 22 | 'hook' => __( 'Hook', 'action-scheduler' ), |
| 23 | 'status' => __( 'Status', 'action-scheduler' ), |
| 24 | 'args' => __( 'Arguments', 'action-scheduler' ), |
| 25 | 'group' => __( 'Group', 'action-scheduler' ), |
| 26 | 'recurrence' => __( 'Recurrence', 'action-scheduler' ), |
| 27 | 'schedule' => __( 'Scheduled Date', 'action-scheduler' ), |
| 28 | 'log_entries' => __( 'Log', 'action-scheduler' ), |
| 29 | ); |
| 30 | $this->sort_by = array( |
| 31 | 'schedule', |
| 32 | 'hook', |
| 33 | 'group', |
| 34 | ); |
| 35 | $this->search_by = array( |
| 36 | 'hook', |
| 37 | 'args', |
| 38 | 'claim_id', |
| 39 | ); |
| 40 | $request_status = $this->get_request_status(); |
| 41 | if ( empty( $request_status ) ) { |
| 42 | $this->sort_by[] = 'status'; |
| 43 | } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ), true ) ) { |
| 44 | $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); |
| 45 | $this->sort_by[] = 'claim_id'; |
| 46 | } |
| 47 | $this->row_actions = array( |
| 48 | 'hook' => array( |
| 49 | 'run' => array( |
| 50 | 'name' => __( 'Run', 'action-scheduler' ), |
| 51 | 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), |
| 52 | ), |
| 53 | 'cancel' => array( |
| 54 | 'name' => __( 'Cancel', 'action-scheduler' ), |
| 55 | 'desc' => __( 'Cancel the action now to avoid it being run in future', 'action-scheduler' ), |
| 56 | 'class' => 'cancel trash', |
| 57 | ), |
| 58 | ), |
| 59 | ); |
| 60 | self::$time_periods = array( |
| 61 | array( |
| 62 | 'seconds' => YEAR_IN_SECONDS, |
| 63 | 'names' => _n_noop( '%s year', '%s years', 'action-scheduler' ), |
| 64 | ), |
| 65 | array( |
| 66 | 'seconds' => MONTH_IN_SECONDS, |
| 67 | 'names' => _n_noop( '%s month', '%s months', 'action-scheduler' ), |
| 68 | ), |
| 69 | array( |
| 70 | 'seconds' => WEEK_IN_SECONDS, |
| 71 | 'names' => _n_noop( '%s week', '%s weeks', 'action-scheduler' ), |
| 72 | ), |
| 73 | array( |
| 74 | 'seconds' => DAY_IN_SECONDS, |
| 75 | 'names' => _n_noop( '%s day', '%s days', 'action-scheduler' ), |
| 76 | ), |
| 77 | array( |
| 78 | 'seconds' => HOUR_IN_SECONDS, |
| 79 | 'names' => _n_noop( '%s hour', '%s hours', 'action-scheduler' ), |
| 80 | ), |
| 81 | array( |
| 82 | 'seconds' => MINUTE_IN_SECONDS, |
| 83 | 'names' => _n_noop( '%s minute', '%s minutes', 'action-scheduler' ), |
| 84 | ), |
| 85 | array( |
| 86 | 'seconds' => 1, |
| 87 | 'names' => _n_noop( '%s second', '%s seconds', 'action-scheduler' ), |
| 88 | ), |
| 89 | ); |
| 90 | parent::__construct( |
| 91 | array( |
| 92 | 'singular' => 'action-scheduler', |
| 93 | 'plural' => 'action-scheduler', |
| 94 | 'ajax' => false, |
| 95 | ) |
| 96 | ); |
| 97 | add_screen_option( |
| 98 | 'per_page', |
| 99 | array( |
| 100 | 'default' => $this->items_per_page, |
| 101 | ) |
| 102 | ); |
| 103 | add_filter( 'set_screen_option_' . $this->get_per_page_option_name(), array( $this, 'set_items_per_page_option' ), 10, 3 ); |
| 104 | set_screen_options(); |
| 105 | } |
| 106 | public function set_items_per_page_option( $status, $option, $value ) { |
| 107 | return $value; |
| 108 | } |
| 109 | private static function human_interval( $interval, $periods_to_include = 2 ) { |
| 110 | if ( $interval <= 0 ) { |
| 111 | return __( 'Now!', 'action-scheduler' ); |
| 112 | } |
| 113 | $output = ''; |
| 114 | $num_time_periods = count( self::$time_periods ); |
| 115 | for ( $time_period_index = 0, $periods_included = 0, $seconds_remaining = $interval; $time_period_index < $num_time_periods && $seconds_remaining > 0 && $periods_included < $periods_to_include; $time_period_index++ ) { |
| 116 | $periods_in_interval = floor( $seconds_remaining / self::$time_periods[ $time_period_index ]['seconds'] ); |
| 117 | if ( $periods_in_interval > 0 ) { |
| 118 | if ( ! empty( $output ) ) { |
| 119 | $output .= ' '; |
| 120 | } |
| 121 | $output .= sprintf( translate_nooped_plural( self::$time_periods[ $time_period_index ]['names'], $periods_in_interval, 'action-scheduler' ), $periods_in_interval ); |
| 122 | $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; |
| 123 | $periods_included++; |
| 124 | } |
| 125 | } |
| 126 | return $output; |
| 127 | } |
| 128 | protected function get_recurrence( $action ) { |
| 129 | $schedule = $action->get_schedule(); |
| 130 | if ( $schedule->is_recurring() && method_exists( $schedule, 'get_recurrence' ) ) { |
| 131 | $recurrence = $schedule->get_recurrence(); |
| 132 | if ( is_numeric( $recurrence ) ) { |
| 133 | return sprintf( __( 'Every %s', 'action-scheduler' ), self::human_interval( $recurrence ) ); |
| 134 | } else { |
| 135 | return $recurrence; |
| 136 | } |
| 137 | } |
| 138 | return __( 'Non-repeating', 'action-scheduler' ); |
| 139 | } |
| 140 | public function column_args( array $row ) { |
| 141 | if ( empty( $row['args'] ) ) { |
| 142 | return apply_filters( 'action_scheduler_list_table_column_args', '', $row ); |
| 143 | } |
| 144 | $row_html = '<ul>'; |
| 145 | foreach ( $row['args'] as $key => $value ) { |
| 146 | $row_html .= sprintf( '<li><code>%s => %s</code></li>', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 147 | } |
| 148 | $row_html .= '</ul>'; |
| 149 | return apply_filters( 'action_scheduler_list_table_column_args', $row_html, $row ); |
| 150 | } |
| 151 | public function column_log_entries( array $row ) { |
| 152 | $log_entries_html = '<ol>'; |
| 153 | $timezone = new DateTimezone( 'UTC' ); |
| 154 | foreach ( $row['log_entries'] as $log_entry ) { |
| 155 | $log_entries_html .= $this->get_log_entry_html( $log_entry, $timezone ); |
| 156 | } |
| 157 | $log_entries_html .= '</ol>'; |
| 158 | return $log_entries_html; |
| 159 | } |
| 160 | protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
| 161 | $date = $log_entry->get_date(); |
| 162 | $date->setTimezone( $timezone ); |
| 163 | return sprintf( '<li><strong>%s</strong><br/>%s</li>', esc_html( $date->format( 'Y-m-d H:i:s O' ) ), esc_html( $log_entry->get_message() ) ); |
| 164 | } |
| 165 | protected function maybe_render_actions( $row, $column_name ) { |
| 166 | if ( 'pending' === strtolower( $row['status_name'] ) ) { |
| 167 | return parent::maybe_render_actions( $row, $column_name ); |
| 168 | } |
| 169 | return ''; |
| 170 | } |
| 171 | public function display_admin_notices() { |
| 172 | global $wpdb; |
| 173 | if ( ( is_a( $this->store, 'ActionScheduler_HybridStore' ) || is_a( $this->store, 'ActionScheduler_DBStore' ) ) && apply_filters( 'action_scheduler_enable_recreate_data_store', true ) ) { |
| 174 | $table_list = array( |
| 175 | 'actionscheduler_actions', |
| 176 | 'actionscheduler_logs', |
| 177 | 'actionscheduler_groups', |
| 178 | 'actionscheduler_claims', |
| 179 | ); |
| 180 | $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 181 | foreach ( $table_list as $table_name ) { |
| 182 | if ( ! in_array( $wpdb->prefix . $table_name, $found_tables, true ) ) { |
| 183 | $this->admin_notices[] = array( |
| 184 | 'class' => 'error', |
| 185 | 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).', 'action-scheduler' ), |
| 186 | ); |
| 187 | $this->recreate_tables(); |
| 188 | parent::display_admin_notices(); |
| 189 | return; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | if ( $this->runner->has_maximum_concurrent_batches() ) { |
| 194 | $claim_count = $this->store->get_claim_count(); |
| 195 | $this->admin_notices[] = array( |
| 196 | 'class' => 'updated', |
| 197 | 'message' => sprintf( |
| 198 | _n( |
| 199 | 'Maximum simultaneous queues already in progress (%s queue). No additional queues will begin processing until the current queues are complete.', |
| 200 | 'Maximum simultaneous queues already in progress (%s queues). No additional queues will begin processing until the current queues are complete.', |
| 201 | $claim_count, |
| 202 | 'action-scheduler' |
| 203 | ), |
| 204 | $claim_count |
| 205 | ), |
| 206 | ); |
| 207 | } elseif ( $this->store->has_pending_actions_due() ) { |
| 208 | $async_request_lock_expiration = ActionScheduler::lock()->get_expiration( 'async-request-runner' ); |
| 209 | // No lock set or lock expired. |
| 210 | if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { |
| 211 | $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); |
| 212 | $async_request_message = sprintf( __( 'A new queue has begun processing. <a href="%s">View actions in-progress »</a>', 'action-scheduler' ), esc_url( $in_progress_url ) ); |
| 213 | } else { |
| 214 | $async_request_message = sprintf( __( 'The next queue will begin processing in approximately %d seconds.', 'action-scheduler' ), $async_request_lock_expiration - time() ); |
| 215 | } |
| 216 | $this->admin_notices[] = array( |
| 217 | 'class' => 'notice notice-info', |
| 218 | 'message' => $async_request_message, |
| 219 | ); |
| 220 | } |
| 221 | $notification = get_transient( 'action_scheduler_admin_notice' ); |
| 222 | if ( is_array( $notification ) ) { |
| 223 | delete_transient( 'action_scheduler_admin_notice' ); |
| 224 | $action = $this->store->fetch_action( $notification['action_id'] ); |
| 225 | $action_hook_html = '<strong><code>' . $action->get_hook() . '</code></strong>'; |
| 226 | if ( 1 === absint( $notification['success'] ) ) { |
| 227 | $class = 'updated'; |
| 228 | switch ( $notification['row_action_type'] ) { |
| 229 | case 'run': |
| 230 | $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); |
| 231 | break; |
| 232 | case 'cancel': |
| 233 | $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); |
| 234 | break; |
| 235 | default: |
| 236 | $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); |
| 237 | break; |
| 238 | } |
| 239 | } else { |
| 240 | $class = 'error'; |
| 241 | $action_message_html = sprintf( __( 'Could not process change for action: "%1$s" (ID: %2$d). Error: %3$s', 'action-scheduler' ), $action_hook_html, esc_html( $notification['action_id'] ), esc_html( $notification['error_message'] ) ); |
| 242 | } |
| 243 | $action_message_html = apply_filters( 'action_scheduler_admin_notice_html', $action_message_html, $action, $notification ); |
| 244 | $this->admin_notices[] = array( |
| 245 | 'class' => $class, |
| 246 | 'message' => $action_message_html, |
| 247 | ); |
| 248 | } |
| 249 | parent::display_admin_notices(); |
| 250 | } |
| 251 | public function column_schedule( $row ) { |
| 252 | return $this->get_schedule_display_string( $row['schedule'] ); |
| 253 | } |
| 254 | protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
| 255 | $schedule_display_string = ''; |
| 256 | if ( is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) { |
| 257 | return __( 'async', 'action-scheduler' ); |
| 258 | } |
| 259 | if ( ! method_exists( $schedule, 'get_date' ) || ! $schedule->get_date() ) { |
| 260 | return '0000-00-00 00:00:00'; |
| 261 | } |
| 262 | $next_timestamp = $schedule->get_date()->getTimestamp(); |
| 263 | $schedule_display_string .= $schedule->get_date()->format( 'Y-m-d H:i:s O' ); |
| 264 | $schedule_display_string .= '<br/>'; |
| 265 | if ( gmdate( 'U' ) > $next_timestamp ) { |
| 266 | $schedule_display_string .= sprintf( __( ' (%s ago)', 'action-scheduler' ), self::human_interval( gmdate( 'U' ) - $next_timestamp ) ); |
| 267 | } else { |
| 268 | $schedule_display_string .= sprintf( __( ' (%s)', 'action-scheduler' ), self::human_interval( $next_timestamp - gmdate( 'U' ) ) ); |
| 269 | } |
| 270 | return $schedule_display_string; |
| 271 | } |
| 272 | protected function bulk_delete( array $ids, $ids_sql ) { |
| 273 | foreach ( $ids as $id ) { |
| 274 | try { |
| 275 | $this->store->delete_action( $id ); |
| 276 | } catch ( Exception $e ) { |
| 277 | // A possible reason for an exception would include a scenario where the same action is deleted by a |
| 278 | // concurrent request. |
| 279 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 280 | error_log( |
| 281 | sprintf( |
| 282 | __( 'Action Scheduler was unable to delete action %1$d. Reason: %2$s', 'action-scheduler' ), |
| 283 | $id, |
| 284 | $e->getMessage() |
| 285 | ) |
| 286 | ); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | protected function row_action_cancel( $action_id ) { |
| 291 | $this->process_row_action( $action_id, 'cancel' ); |
| 292 | } |
| 293 | protected function row_action_run( $action_id ) { |
| 294 | $this->process_row_action( $action_id, 'run' ); |
| 295 | } |
| 296 | protected function recreate_tables() { |
| 297 | if ( is_a( $this->store, 'ActionScheduler_HybridStore' ) ) { |
| 298 | $store = $this->store; |
| 299 | } else { |
| 300 | $store = new ActionScheduler_HybridStore(); |
| 301 | } |
| 302 | add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
| 303 | $store_schema = new ActionScheduler_StoreSchema(); |
| 304 | $logger_schema = new ActionScheduler_LoggerSchema(); |
| 305 | $store_schema->register_tables( true ); |
| 306 | $logger_schema->register_tables( true ); |
| 307 | remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
| 308 | } |
| 309 | protected function process_row_action( $action_id, $row_action_type ) { |
| 310 | try { |
| 311 | switch ( $row_action_type ) { |
| 312 | case 'run': |
| 313 | $this->runner->process_action( $action_id, 'Admin List Table' ); |
| 314 | break; |
| 315 | case 'cancel': |
| 316 | $this->store->cancel_action( $action_id ); |
| 317 | break; |
| 318 | } |
| 319 | $success = 1; |
| 320 | $error_message = ''; |
| 321 | } catch ( Exception $e ) { |
| 322 | $success = 0; |
| 323 | $error_message = $e->getMessage(); |
| 324 | } |
| 325 | set_transient( 'action_scheduler_admin_notice', compact( 'action_id', 'success', 'error_message', 'row_action_type' ), 30 ); |
| 326 | } |
| 327 | public function prepare_items() { |
| 328 | $this->prepare_column_headers(); |
| 329 | $per_page = $this->get_items_per_page( $this->get_per_page_option_name(), $this->items_per_page ); |
| 330 | $query = array( |
| 331 | 'per_page' => $per_page, |
| 332 | 'offset' => $this->get_items_offset(), |
| 333 | 'status' => $this->get_request_status(), |
| 334 | 'orderby' => $this->get_request_orderby(), |
| 335 | 'order' => $this->get_request_order(), |
| 336 | 'search' => $this->get_request_search_query(), |
| 337 | ); |
| 338 | if ( 'past-due' === $this->get_request_status() ) { |
| 339 | $query['status'] = ActionScheduler_Store::STATUS_PENDING; |
| 340 | $query['date'] = as_get_datetime_object(); |
| 341 | } |
| 342 | $this->items = array(); |
| 343 | $total_items = $this->store->query_actions( $query, 'count' ); |
| 344 | $status_labels = $this->store->get_status_labels(); |
| 345 | foreach ( $this->store->query_actions( $query ) as $action_id ) { |
| 346 | try { |
| 347 | $action = $this->store->fetch_action( $action_id ); |
| 348 | } catch ( Exception $e ) { |
| 349 | continue; |
| 350 | } |
| 351 | if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
| 352 | continue; |
| 353 | } |
| 354 | $this->items[ $action_id ] = array( |
| 355 | 'ID' => $action_id, |
| 356 | 'hook' => $action->get_hook(), |
| 357 | 'status_name' => $this->store->get_status( $action_id ), |
| 358 | 'status' => $status_labels[ $this->store->get_status( $action_id ) ], |
| 359 | 'args' => $action->get_args(), |
| 360 | 'group' => $action->get_group(), |
| 361 | 'log_entries' => $this->logger->get_logs( $action_id ), |
| 362 | 'claim_id' => $this->store->get_claim_id( $action_id ), |
| 363 | 'recurrence' => $this->get_recurrence( $action ), |
| 364 | 'schedule' => $action->get_schedule(), |
| 365 | ); |
| 366 | } |
| 367 | $this->set_pagination_args( |
| 368 | array( |
| 369 | 'total_items' => $total_items, |
| 370 | 'per_page' => $per_page, |
| 371 | 'total_pages' => ceil( $total_items / $per_page ), |
| 372 | ) |
| 373 | ); |
| 374 | } |
| 375 | protected function display_filter_by_status() { |
| 376 | $this->status_counts = $this->store->action_counts() + $this->store->extra_action_counts(); |
| 377 | parent::display_filter_by_status(); |
| 378 | } |
| 379 | protected function get_search_box_button_text() { |
| 380 | return __( 'Search hook, args and claim ID', 'action-scheduler' ); |
| 381 | } |
| 382 | protected function get_per_page_option_name() { |
| 383 | return str_replace( '-', '_', $this->screen->id ) . '_per_page'; |
| 384 | } |
| 385 | } |
| 386 |