From fc083b0f8a97642a39cbf959dd404bcffa9c727f Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 15:59:22 -0400 Subject: [PATCH 01/24] eliminate code smells --- classes/ActionScheduler_InvalidActionException.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/classes/ActionScheduler_InvalidActionException.php b/classes/ActionScheduler_InvalidActionException.php index 9bc418f21..c64e971ec 100644 --- a/classes/ActionScheduler_InvalidActionException.php +++ b/classes/ActionScheduler_InvalidActionException.php @@ -21,7 +21,7 @@ public static function from_schedule( $action_id, $schedule ) { /* translators: 1: action ID 2: schedule */ __( 'Action [%1$s] has an invalid schedule: %2$s', 'action-scheduler' ), $action_id, - var_export( $schedule, true ) + var_export( $schedule, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export ); return new static( $message ); @@ -30,8 +30,6 @@ public static function from_schedule( $action_id, $schedule ) { /** * Create a new exception when the action's args cannot be decoded to an array. * - * @author Jeremy Pry - * * @param string $action_id The action ID with bad args. * @param mixed $args Passed arguments. * @return static @@ -41,7 +39,7 @@ public static function from_decoding_args( $action_id, $args = array() ) { /* translators: 1: action ID 2: arguments */ __( 'Action [%1$s] has invalid arguments. It cannot be JSON decoded to an array. $args = %2$s', 'action-scheduler' ), $action_id, - var_export( $args, true ) + var_export( $args, true ) // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export ); return new static( $message ); From 5fc2cf1c2ab460e4df6312bff8d873404a113498 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 16:06:06 -0400 Subject: [PATCH 02/24] eliminate code smells --- classes/ActionScheduler_ListTable.php | 60 ++++++++++++++------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/classes/ActionScheduler_ListTable.php b/classes/ActionScheduler_ListTable.php index 83f54bc67..e8f1e4315 100644 --- a/classes/ActionScheduler_ListTable.php +++ b/classes/ActionScheduler_ListTable.php @@ -2,6 +2,7 @@ /** * Implements the admin view of the actions. + * * @codeCoverageIgnore */ class ActionScheduler_ListTable extends ActionScheduler_Abstract_ListTable { @@ -49,9 +50,8 @@ class ActionScheduler_ListTable extends ActionScheduler_Abstract_ListTable { protected $runner; /** - * Bulk actions. The key of the array is the method name of the implementation: - * - * bulk_(array $ids, string $sql_in). + * Bulk actions. The key of the array is the method name of the implementation. + * Example: bulk_(array $ids, string $sql_in). * * See the comments in the parent class for further details * @@ -118,16 +118,16 @@ public function __construct( ActionScheduler_Store $store, ActionScheduler_Logge if ( empty( $request_status ) ) { $this->sort_by[] = 'status'; - } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ) ) ) { + } elseif ( in_array( $request_status, array( 'in-progress', 'failed' ), true ) ) { $this->columns += array( 'claim_id' => __( 'Claim ID', 'action-scheduler' ) ); $this->sort_by[] = 'claim_id'; } $this->row_actions = array( 'hook' => array( - 'run' => array( - 'name' => __( 'Run', 'action-scheduler' ), - 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), + 'run' => array( + 'name' => __( 'Run', 'action-scheduler' ), + 'desc' => __( 'Process the action now as if it were run as part of a queue', 'action-scheduler' ), ), 'cancel' => array( 'name' => __( 'Cancel', 'action-scheduler' ), @@ -224,7 +224,7 @@ private static function human_interval( $interval, $periods_to_include = 2 ) { return __( 'Now!', 'action-scheduler' ); } - $output = ''; + $output = ''; $num_time_periods = count( self::$time_periods ); 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++ ) { @@ -235,7 +235,7 @@ private static function human_interval( $interval, $periods_to_include = 2 ) { if ( ! empty( $output ) ) { $output .= ' '; } - $output .= sprintf( translate_nooped_plural( self::$time_periods[ $time_period_index ]['names'], $periods_in_interval, 'action-scheduler' ), $periods_in_interval ); + $output .= sprintf( translate_nooped_plural( self::$time_periods[ $time_period_index ]['names'], $periods_in_interval, 'action-scheduler' ), $periods_in_interval ); $seconds_remaining -= $periods_in_interval * self::$time_periods[ $time_period_index ]['seconds']; $periods_included++; } @@ -281,7 +281,7 @@ public function column_args( array $row ) { $row_html = '
    '; foreach ( $row['args'] as $key => $value ) { - $row_html .= sprintf( '
  • %s => %s
  • ', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); + $row_html .= sprintf( '
  • %s => %s
  • ', esc_html( var_export( $key, true ) ), esc_html( var_export( $value, true ) ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export } $row_html .= '
'; @@ -331,7 +331,7 @@ protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, Date * @return string */ protected function maybe_render_actions( $row, $column_name ) { - if ( 'pending' === strtolower( $row[ 'status_name' ] ) ) { + if ( 'pending' === strtolower( $row['status_name'] ) ) { return parent::maybe_render_actions( $row, $column_name ); } @@ -359,10 +359,10 @@ public function display_admin_notices() { $found_tables = $wpdb->get_col( "SHOW TABLES LIKE '{$wpdb->prefix}actionscheduler%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared foreach ( $table_list as $table_name ) { - if ( ! in_array( $wpdb->prefix . $table_name, $found_tables ) ) { + if ( ! in_array( $wpdb->prefix . $table_name, $found_tables, true ) ) { $this->admin_notices[] = array( 'class' => 'error', - 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).' , 'action-scheduler' ), + 'message' => __( 'It appears one or more database tables were missing. Attempting to re-create the missing table(s).', 'action-scheduler' ), ); $this->recreate_tables(); parent::display_admin_notices(); @@ -393,7 +393,7 @@ public function display_admin_notices() { // No lock set or lock expired. if ( false === $async_request_lock_expiration || $async_request_lock_expiration < time() ) { - $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); + $in_progress_url = add_query_arg( 'status', 'in-progress', remove_query_arg( 'status' ) ); /* translators: %s: process URL */ $async_request_message = sprintf( __( 'A new queue has begun processing. View actions in-progress »', 'action-scheduler' ), esc_url( $in_progress_url ) ); } else { @@ -412,20 +412,21 @@ public function display_admin_notices() { if ( is_array( $notification ) ) { delete_transient( 'action_scheduler_admin_notice' ); - $action = $this->store->fetch_action( $notification['action_id'] ); + $action = $this->store->fetch_action( $notification['action_id'] ); $action_hook_html = '' . $action->get_hook() . ''; - if ( 1 == $notification['success'] ) { + + if ( 1 === absint( $notification['success'] ) ) { $class = 'updated'; switch ( $notification['row_action_type'] ) { - case 'run' : + case 'run': /* translators: %s: action HTML */ $action_message_html = sprintf( __( 'Successfully executed action: %s', 'action-scheduler' ), $action_hook_html ); break; - case 'cancel' : + case 'cancel': /* translators: %s: action HTML */ $action_message_html = sprintf( __( 'Successfully canceled action: %s', 'action-scheduler' ), $action_hook_html ); break; - default : + default: /* translators: %s: action HTML */ $action_message_html = sprintf( __( 'Successfully processed change for action: %s', 'action-scheduler' ), $action_hook_html ); break; @@ -508,6 +509,7 @@ protected function bulk_delete( array $ids, $ids_sql ) { } catch ( Exception $e ) { // A possible reason for an exception would include a scenario where the same action is deleted by a // concurrent request. + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( sprintf( /* translators: 1: action ID 2: exception message. */ @@ -567,17 +569,17 @@ protected function recreate_tables() { protected function process_row_action( $action_id, $row_action_type ) { try { switch ( $row_action_type ) { - case 'run' : + case 'run': $this->runner->process_action( $action_id, 'Admin List Table' ); break; - case 'cancel' : + case 'cancel': $this->store->cancel_action( $action_id ); break; } - $success = 1; + $success = 1; $error_message = ''; } catch ( Exception $e ) { - $success = 0; + $success = 0; $error_message = $e->getMessage(); } @@ -640,11 +642,13 @@ public function prepare_items() { ); } - $this->set_pagination_args( array( - 'total_items' => $total_items, - 'per_page' => $per_page, - 'total_pages' => ceil( $total_items / $per_page ), - ) ); + $this->set_pagination_args( + array( + 'total_items' => $total_items, + 'per_page' => $per_page, + 'total_pages' => ceil( $total_items / $per_page ), + ) + ); } /** From 0c5d7b368e8901e1f2cc95f094a3ae517e415758 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 16:08:11 -0400 Subject: [PATCH 03/24] eliminate code smells --- classes/ActionScheduler_LogEntry.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/classes/ActionScheduler_LogEntry.php b/classes/ActionScheduler_LogEntry.php index 10cedf546..726fea2ec 100644 --- a/classes/ActionScheduler_LogEntry.php +++ b/classes/ActionScheduler_LogEntry.php @@ -6,16 +6,22 @@ class ActionScheduler_LogEntry { /** + * Action's ID for log entry. + * * @var int $action_id */ - protected $action_id = ''; + protected $action_id = ''; /** + * Log entry's message. + * * @var string $message */ - protected $message = ''; + protected $message = ''; /** + * Log entry's date. + * * @var Datetime $date */ protected $date; @@ -43,7 +49,7 @@ public function __construct( $action_id, $message, $date = null ) { $this->action_id = $action_id; $this->message = $message; - $this->date = $date ? $date : new Datetime; + $this->date = $date ? $date : new Datetime(); } /** From 0fc545062c10e40f39e204b20cd17daadab8a20f Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 16:09:20 -0400 Subject: [PATCH 04/24] eliminate code smell --- classes/ActionScheduler_OptionLock.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/ActionScheduler_OptionLock.php b/classes/ActionScheduler_OptionLock.php index 911f9b77c..547857a0a 100644 --- a/classes/ActionScheduler_OptionLock.php +++ b/classes/ActionScheduler_OptionLock.php @@ -5,6 +5,7 @@ * for up-to a given duration. * * Class ActionScheduler_OptionLock + * * @since 3.0.0 */ class ActionScheduler_OptionLock extends ActionScheduler_Lock { From e58fdf246cc46f061b86d7a2b73d3e00f85036f6 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 16:13:47 -0400 Subject: [PATCH 05/24] eliminate code smells --- classes/ActionScheduler_QueueCleaner.php | 96 ++++++++++++++---------- 1 file changed, 58 insertions(+), 38 deletions(-) diff --git a/classes/ActionScheduler_QueueCleaner.php b/classes/ActionScheduler_QueueCleaner.php index a8f27ce57..ff5173ae0 100644 --- a/classes/ActionScheduler_QueueCleaner.php +++ b/classes/ActionScheduler_QueueCleaner.php @@ -5,10 +5,18 @@ */ class ActionScheduler_QueueCleaner { - /** @var int */ + /** + * The batch size. + * + * @var int + */ protected $batch_size; - /** @var ActionScheduler_Store */ + /** + * ActionScheduler_Store instance. + * + * @var ActionScheduler_Store + */ private $store = null; /** @@ -19,12 +27,14 @@ class ActionScheduler_QueueCleaner { private $month_in_seconds = 2678400; /** - * @var string[] Default list of statuses purged by the cleaner process. + * Default list of statuses purged by the cleaner process. + * + * @var string[] */ - private $default_statuses_to_purge = [ + private $default_statuses_to_purge = array( ActionScheduler_Store::STATUS_COMPLETE, ActionScheduler_Store::STATUS_CANCELED, - ]; + ); /** * ActionScheduler_QueueCleaner constructor. @@ -33,7 +43,7 @@ class ActionScheduler_QueueCleaner { * @param int $batch_size The batch size. */ public function __construct( ActionScheduler_Store $store = null, $batch_size = 20 ) { - $this->store = $store ? $store : ActionScheduler_Store::instance(); + $this->store = $store ? $store : ActionScheduler_Store::instance(); $this->batch_size = $batch_size; } @@ -86,22 +96,26 @@ public function delete_old_actions() { * @return array Actions deleted. */ public function clean_actions( array $statuses_to_purge, DateTime $cutoff_date, $batch_size = null, $context = 'old' ) { - $batch_size = $batch_size !== null ? $batch_size : $this->batch_size; - $cutoff = $cutoff_date !== null ? $cutoff_date : as_get_datetime_object( $this->month_in_seconds . ' seconds ago' ); + $batch_size = ! is_null( $batch_size ) ? $batch_size : $this->batch_size; + $cutoff = ! is_null( $cutoff_date ) ? $cutoff_date : as_get_datetime_object( $this->month_in_seconds . ' seconds ago' ); $lifespan = time() - $cutoff->getTimestamp(); + if ( empty( $statuses_to_purge ) ) { $statuses_to_purge = $this->default_statuses_to_purge; } - $deleted_actions = []; + $deleted_actions = array(); + foreach ( $statuses_to_purge as $status ) { - $actions_to_delete = $this->store->query_actions( array( - 'status' => $status, - 'modified' => $cutoff, - 'modified_compare' => '<=', - 'per_page' => $batch_size, - 'orderby' => 'none', - ) ); + $actions_to_delete = $this->store->query_actions( + array( + 'status' => $status, + 'modified' => $cutoff, + 'modified_compare' => '<=', + 'per_page' => $batch_size, + 'orderby' => 'none', + ) + ); $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) ); } @@ -118,8 +132,9 @@ public function clean_actions( array $statuses_to_purge, DateTime $cutoff_date, * @return array Deleted action IDs. */ private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) { - $deleted_actions = []; - if ( $lifespan === null ) { + $deleted_actions = array(); + + if ( is_null( $lifespan ) ) { $lifespan = $this->month_in_seconds; } @@ -139,7 +154,6 @@ private function delete_actions( array $actions_to_delete, $lifespan = null, $co * @param int $lifespan The retention period, in seconds, for old actions * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch * @since 2.0.0 - * */ do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) ); } @@ -157,18 +171,22 @@ private function delete_actions( array $actions_to_delete, $lifespan = null, $co */ public function reset_timeouts( $time_limit = 300 ) { $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit ); + if ( $timeout < 0 ) { return; } - $cutoff = as_get_datetime_object($timeout . ' seconds ago'); - $actions_to_reset = $this->store->query_actions( array( - 'status' => ActionScheduler_Store::STATUS_PENDING, - 'modified' => $cutoff, - 'modified_compare' => '<=', - 'claimed' => true, - 'per_page' => $this->get_batch_size(), - 'orderby' => 'none', - ) ); + + $cutoff = as_get_datetime_object( $timeout . ' seconds ago' ); + $actions_to_reset = $this->store->query_actions( + array( + 'status' => ActionScheduler_Store::STATUS_PENDING, + 'modified' => $cutoff, + 'modified_compare' => '<=', + 'claimed' => true, + 'per_page' => $this->get_batch_size(), + 'orderby' => 'none', + ) + ); foreach ( $actions_to_reset as $action_id ) { $this->store->unclaim_action( $action_id ); @@ -187,17 +205,21 @@ public function reset_timeouts( $time_limit = 300 ) { */ public function mark_failures( $time_limit = 300 ) { $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit ); + if ( $timeout < 0 ) { return; } - $cutoff = as_get_datetime_object($timeout . ' seconds ago'); - $actions_to_reset = $this->store->query_actions( array( - 'status' => ActionScheduler_Store::STATUS_RUNNING, - 'modified' => $cutoff, - 'modified_compare' => '<=', - 'per_page' => $this->get_batch_size(), - 'orderby' => 'none', - ) ); + + $cutoff = as_get_datetime_object( $timeout . ' seconds ago' ); + $actions_to_reset = $this->store->query_actions( + array( + 'status' => ActionScheduler_Store::STATUS_RUNNING, + 'modified' => $cutoff, + 'modified_compare' => '<=', + 'per_page' => $this->get_batch_size(), + 'orderby' => 'none', + ) + ); foreach ( $actions_to_reset as $action_id ) { $this->store->mark_failure( $action_id ); @@ -209,7 +231,6 @@ public function mark_failures( $time_limit = 300 ) { * Do all of the cleaning actions. * * @param int $time_limit The number of seconds to use as the timeout and failure period. Default 300 (5 minutes). - * @author Jeremy Pry */ public function clean( $time_limit = 300 ) { $this->delete_old_actions(); @@ -220,7 +241,6 @@ public function clean( $time_limit = 300 ) { /** * Get the batch size for cleaning the queue. * - * @author Jeremy Pry * @return int */ protected function get_batch_size() { From a41fa55b02062b6074fb4522d69438fa7e48825c Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 19:27:01 -0400 Subject: [PATCH 06/24] eliminate code smells --- classes/ActionScheduler_QueueRunner.php | 38 ++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/classes/ActionScheduler_QueueRunner.php b/classes/ActionScheduler_QueueRunner.php index 2e60f7fe1..2e2913898 100644 --- a/classes/ActionScheduler_QueueRunner.php +++ b/classes/ActionScheduler_QueueRunner.php @@ -8,24 +8,39 @@ class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner { const WP_CRON_SCHEDULE = 'every_minute'; - /** @var ActionScheduler_AsyncRequest_QueueRunner */ + /** + * ActionScheduler_AsyncRequest_QueueRunner instance. + * + * @var ActionScheduler_AsyncRequest_QueueRunner + */ protected $async_request; - /** @var ActionScheduler_QueueRunner */ + /** + * ActionScheduler_QueueRunner instance. + * + * @var ActionScheduler_QueueRunner + */ private static $runner = null; - /** @var int */ + /** + * Number of processed actions. + * + * @var int + */ private $processed_actions_count = 0; /** + * Get instance. + * * @return ActionScheduler_QueueRunner * @codeCoverageIgnore */ public static function instance() { - if ( empty(self::$runner) ) { - $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner'); + if ( empty( self::$runner ) ) { + $class = apply_filters( 'action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner' ); self::$runner = new $class(); } + return self::$runner; } @@ -48,11 +63,13 @@ public function __construct( ActionScheduler_Store $store = null, ActionSchedule } /** + * Initialize. + * * @codeCoverageIgnore */ public function init() { - add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); + add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param. $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); @@ -121,6 +138,7 @@ public function maybe_dispatch_async_request() { * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK, * should set a context as the first parameter. For an example of this, refer to the code seen in + * * @see ActionScheduler_AsyncRequest_QueueRunner::handle() * * @param string $context Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' @@ -158,13 +176,13 @@ public function run( $context = 'WP Cron' ) { * @return int The number of actions processed. */ protected function do_batch( $size = 100, $context = '' ) { - $claim = $this->store->stake_claim($size); - $this->monitor->attach($claim); + $claim = $this->store->stake_claim( $size ); + $this->monitor->attach( $claim ); $processed_actions = 0; foreach ( $claim->get_actions() as $action_id ) { // bail if we lost the claim. - if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) { + if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ), true ) ) { break; } $this->process_action( $action_id, $context ); @@ -174,7 +192,7 @@ protected function do_batch( $size = 100, $context = '' ) { break; } } - $this->store->release_claim($claim); + $this->store->release_claim( $claim ); $this->monitor->detach(); $this->clear_caches(); return $processed_actions; From 9efb67378ce2614c18970c7efd828cd8d1d9a9d8 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 19:32:13 -0400 Subject: [PATCH 07/24] eliminate code smells --- classes/ActionScheduler_Versions.php | 38 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/classes/ActionScheduler_Versions.php b/classes/ActionScheduler_Versions.php index 98c1b1acc..4bde88391 100644 --- a/classes/ActionScheduler_Versions.php +++ b/classes/ActionScheduler_Versions.php @@ -5,11 +5,17 @@ */ class ActionScheduler_Versions { /** + * ActionScheduler_Versions instance. + * * @var ActionScheduler_Versions */ - private static $instance = NULL; + private static $instance = null; - /** @var array */ + /** + * Versions. + * + * @var array + */ private $versions = array(); /** @@ -19,11 +25,11 @@ class ActionScheduler_Versions { * @param callable $initialization_callback Callback to initialize the version. */ public function register( $version_string, $initialization_callback ) { - if ( isset($this->versions[$version_string]) ) { - return FALSE; + if ( isset( $this->versions[ $version_string ] ) ) { + return false; } - $this->versions[$version_string] = $initialization_callback; - return TRUE; + $this->versions[ $version_string ] = $initialization_callback; + return true; } /** @@ -37,12 +43,12 @@ public function get_versions() { * Get latest version registered. */ public function latest_version() { - $keys = array_keys($this->versions); - if ( empty($keys) ) { + $keys = array_keys( $this->versions ); + if ( empty( $keys ) ) { return false; } uasort( $keys, 'version_compare' ); - return end($keys); + return end( $keys ); } /** @@ -50,28 +56,34 @@ public function latest_version() { */ public function latest_version_callback() { $latest = $this->latest_version(); - if ( empty($latest) || !isset($this->versions[$latest]) ) { + + if ( empty( $latest ) || ! isset( $this->versions[ $latest ] ) ) { return '__return_null'; } - return $this->versions[$latest]; + + return $this->versions[ $latest ]; } /** + * Get instance. + * * @return ActionScheduler_Versions * @codeCoverageIgnore */ public static function instance() { - if ( empty(self::$instance) ) { + if ( empty( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } /** + * Initialize. + * * @codeCoverageIgnore */ public static function initialize_latest_version() { $self = self::instance(); - call_user_func($self->latest_version_callback()); + call_user_func( $self->latest_version_callback() ); } } From 82313541a6cb7f20856577ae0d8a5981024930d3 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 19:45:15 -0400 Subject: [PATCH 08/24] eliminate code smells --- .../ActionScheduler_WPCLI_QueueRunner.php | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php b/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php index 4c5d9c2d6..0a975b7ba 100644 --- a/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php +++ b/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php @@ -9,13 +9,25 @@ */ class ActionScheduler_WPCLI_QueueRunner extends ActionScheduler_Abstract_QueueRunner { - /** @var array */ + /** + * Claimed actions. + * + * @var array + */ protected $actions; - /** @var ActionScheduler_ActionClaim */ + /** + * ActionScheduler_ActionClaim instance. + * + * @var ActionScheduler_ActionClaim + */ protected $claim; - /** @var \cli\progress\Bar */ + /** + * Progress bar instance. + * + * @var \cli\progress\Bar + */ protected $progress_bar; /** @@ -39,8 +51,6 @@ public function __construct( ActionScheduler_Store $store = null, ActionSchedule /** * Set up the Queue before processing. * - * @author Jeremy Pry - * * @param int $batch_size The batch size to process. * @param array $hooks The hooks being used to filter the actions claimed in this batch. * @param string $group The group of actions to claim with this batch. @@ -72,8 +82,6 @@ public function setup( $batch_size, $hooks = array(), $group = '', $force = fals /** * Add our hooks to the appropriate actions. - * - * @author Jeremy Pry */ protected function add_hooks() { add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) ); @@ -83,8 +91,6 @@ protected function add_hooks() { /** * Set up the WP CLI progress bar. - * - * @author Jeremy Pry */ protected function setup_progress_bar() { $count = count( $this->actions ); @@ -98,8 +104,6 @@ protected function setup_progress_bar() { /** * Process actions in the queue. * - * @author Jeremy Pry - * * @param string $context Optional runner context. Default 'WP CLI'. * * @return int The number of actions processed. @@ -109,7 +113,7 @@ public function run( $context = 'WP CLI' ) { $this->setup_progress_bar(); foreach ( $this->actions as $action_id ) { // Error if we lost the claim. - if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) { + if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ), true ) ) { WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'action-scheduler' ) ); break; } @@ -129,8 +133,6 @@ public function run( $context = 'WP CLI' ) { /** * Handle WP CLI message when the action is starting. * - * @author Jeremy Pry - * * @param int $action_id Action ID. */ public function before_execute( $action_id ) { @@ -141,8 +143,6 @@ public function before_execute( $action_id ) { /** * Handle WP CLI message when the action has completed. * - * @author Jeremy Pry - * * @param int $action_id ActionID. * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility. */ @@ -158,8 +158,6 @@ public function after_execute( $action_id, $action = null ) { /** * Handle WP CLI message when the action has failed. * - * @author Jeremy Pry - * * @param int $action_id Action ID. * @param Exception $exception Exception. * @throws \WP_CLI\ExitException With failure message. From 6a755a10e6d6938e8a12b37ae9a9b4a2cdfe9c26 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 19:55:10 -0400 Subject: [PATCH 09/24] eliminate code smells --- classes/WP_CLI/Migration_Command.php | 166 +++++++++++++++++---------- 1 file changed, 104 insertions(+), 62 deletions(-) diff --git a/classes/WP_CLI/Migration_Command.php b/classes/WP_CLI/Migration_Command.php index 289f2f42e..86f750fe8 100644 --- a/classes/WP_CLI/Migration_Command.php +++ b/classes/WP_CLI/Migration_Command.php @@ -21,7 +21,11 @@ */ class Migration_Command extends WP_CLI_Command { - /** @var int */ + /** + * Number of actions migrated. + * + * @var int + */ private $total_processed = 0; /** @@ -32,38 +36,42 @@ public function register() { return; } - WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [ - 'shortdesc' => 'Migrates actions to the DB tables store', - 'synopsis' => [ - [ - 'type' => 'assoc', - 'name' => 'batch-size', - 'optional' => true, - 'default' => 100, - 'description' => 'The number of actions to process in each batch', - ], - [ - 'type' => 'assoc', - 'name' => 'free-memory-on', - 'optional' => true, - 'default' => 50, - 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', - ], - [ - 'type' => 'assoc', - 'name' => 'pause', - 'optional' => true, - 'default' => 0, - 'description' => 'The number of seconds to pause when freeing memory', - ], - [ - 'type' => 'flag', - 'name' => 'dry-run', - 'optional' => true, - 'description' => 'Reports on the actions that would have been migrated, but does not change any data', - ], - ], - ] ); + WP_CLI::add_command( + 'action-scheduler migrate', + array( $this, 'migrate' ), + array( + 'shortdesc' => 'Migrates actions to the DB tables store', + 'synopsis' => array( + array( + 'type' => 'assoc', + 'name' => 'batch-size', + 'optional' => true, + 'default' => 100, + 'description' => 'The number of actions to process in each batch', + ), + array( + 'type' => 'assoc', + 'name' => 'free-memory-on', + 'optional' => true, + 'default' => 50, + 'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory', + ), + array( + 'type' => 'assoc', + 'name' => 'pause', + 'optional' => true, + 'default' => 0, + 'description' => 'The number of seconds to pause when freeing memory', + ), + array( + 'type' => 'flag', + 'name' => 'dry-run', + 'optional' => true, + 'description' => 'Reports on the actions that would have been migrated, but does not change any data', + ), + ), + ) + ); } /** @@ -81,14 +89,14 @@ public function migrate( $positional_args, $assoc_args ) { $runner = new Runner( $config ); $runner->init_destination(); - $batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100; - $free_on = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50; - $sleep = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0; + $batch_size = isset( $assoc_args['batch-size'] ) ? (int) $assoc_args['batch-size'] : 100; + $free_on = isset( $assoc_args['free-memory-on'] ) ? (int) $assoc_args['free-memory-on'] : 50; + $sleep = isset( $assoc_args['pause'] ) ? (int) $assoc_args['pause'] : 0; \ActionScheduler_DataController::set_free_ticks( $free_on ); \ActionScheduler_DataController::set_sleep_time( $sleep ); do { - $actions_processed = $runner->run( $batch_size ); + $actions_processed = $runner->run( $batch_size ); $this->total_processed += $actions_processed; } while ( $actions_processed > 0 ); @@ -109,12 +117,15 @@ public function migrate( $positional_args, $assoc_args ) { * @return ActionScheduler\Migration\Config */ private function get_migration_config( $args ) { - $args = wp_parse_args( $args, [ - 'dry-run' => false, - ] ); + $args = wp_parse_args( + $args, + array( + 'dry-run' => false, + ) + ); $config = Controller::instance()->get_migration_config_object(); - $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) ); + $config->set_dry_run( ! empty( $args['dry-run'] ) ); return $config; } @@ -123,26 +134,57 @@ private function get_migration_config( $args ) { * Hook command line logging into migration actions. */ private function init_logging() { - add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) { - WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); - }, 10, 1 ); - add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) { - WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); - }, 10, 1 ); - add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) { - WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); - }, 10, 1 ); - add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) { - WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); - }, 10, 2 ); - add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) { - WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); - }, 10, 2 ); - add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) { - WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); - }, 10, 1 ); - add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) { - WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); - }, 10, 1 ); + add_action( + 'action_scheduler/migrate_action_dry_run', + function ( $action_id ) { + WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) ); + } + ); + + add_action( + 'action_scheduler/no_action_to_migrate', + function ( $action_id ) { + WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) ); + } + ); + + add_action( + 'action_scheduler/migrate_action_failed', + function ( $action_id ) { + WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) ); + } + ); + + add_action( + 'action_scheduler/migrate_action_incomplete', + function ( $source_id, $destination_id ) { + WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) ); + }, + 10, + 2 + ); + + add_action( + 'action_scheduler/migrated_action', + function ( $source_id, $destination_id ) { + WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) ); + }, + 10, + 2 + ); + + add_action( + 'action_scheduler/migration_batch_starting', + function ( $batch ) { + WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r + } + ); + + add_action( + 'action_scheduler/migration_batch_complete', + function ( $batch ) { + WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) ); + } + ); } } From 717b2d9a9b796c890b7474e57979c95266a1dbc4 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 20:11:50 -0400 Subject: [PATCH 10/24] eliminate code smells --- classes/abstracts/ActionScheduler.php | 54 ++++++++++++++++++--------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/classes/abstracts/ActionScheduler.php b/classes/abstracts/ActionScheduler.php index 4b747b366..d3d281343 100644 --- a/classes/abstracts/ActionScheduler.php +++ b/classes/abstracts/ActionScheduler.php @@ -5,21 +5,37 @@ /** * Class ActionScheduler + * * @codeCoverageIgnore */ abstract class ActionScheduler { - /** @var string */ + + /** + * Plugin file path. + * + * @var string + */ private static $plugin_file = ''; - /** @var ActionScheduler_ActionFactory */ - private static $factory = NULL; - /** @var bool */ + + /** + * ActionScheduler_ActionFactory instance. + * + * @var ActionScheduler_ActionFactory + */ + private static $factory = null; + + /** + * Data store is initialized. + * + * @var bool + */ private static $data_store_initialized = false; /** * Factory. */ public static function factory() { - if ( !isset(self::$factory) ) { + if ( ! isset( self::$factory ) ) { self::$factory = new ActionScheduler_ActionFactory(); } return self::$factory; @@ -62,27 +78,29 @@ public static function admin_view() { /** * Get the absolute system path to the plugin directory, or a file therein + * * @static * @param string $path Path relative to plugin directory. * @return string */ public static function plugin_path( $path ) { - $base = dirname(self::$plugin_file); + $base = dirname( self::$plugin_file ); if ( $path ) { - return trailingslashit($base) . $path; + return trailingslashit( $base ) . $path; } else { - return untrailingslashit($base); + return untrailingslashit( $base ); } } /** * Get the absolute URL to the plugin directory, or a file therein + * * @static * @param string $path Path relative to plugin directory. * @return string */ public static function plugin_url( $path ) { - return plugins_url($path, self::$plugin_file); + return plugins_url( $path, self::$plugin_file ); } /** @@ -115,7 +133,7 @@ public static function autoload( $class ) { $dir = $classes_dir . 'schema' . $d; } elseif ( strpos( $class, 'ActionScheduler' ) === 0 ) { $segments = explode( '_', $class ); - $type = isset( $segments[ 1 ] ) ? $segments[ 1 ] : ''; + $type = isset( $segments[1] ) ? $segments[1] : ''; switch ( $type ) { case 'WPCLI': @@ -143,7 +161,7 @@ public static function autoload( $class ) { } if ( file_exists( $dir . "{$class}.php" ) ) { - include( $dir . "{$class}.php" ); + include $dir . "{$class}.php"; return; } } @@ -163,7 +181,7 @@ public static function init( $plugin_file ) { */ do_action( 'action_scheduler_pre_init' ); - require_once( self::plugin_path( 'functions.php' ) ); + require_once self::plugin_path( 'functions.php' ); ActionScheduler_DataController::init(); $store = self::store(); @@ -216,7 +234,7 @@ function () { } if ( apply_filters( 'action_scheduler_load_deprecated_functions', true ) ) { - require_once( self::plugin_path( 'deprecated/functions.php' ) ); + require_once self::plugin_path( 'deprecated/functions.php' ); } if ( defined( 'WP_CLI' ) && WP_CLI ) { @@ -251,7 +269,7 @@ public static function is_initialized( $function_name = null ) { __( '%s() was called before the Action Scheduler data store was initialized', 'action-scheduler' ), esc_attr( $function_name ) ); - _doing_it_wrong( $function_name, $message, '3.1.6' ); + _doing_it_wrong( esc_html( $function_name ), esc_html( $message ), '3.1.6' ); } return self::$data_store_initialized; @@ -306,7 +324,7 @@ protected static function is_class_migration( $class ) { ); $segments = explode( '_', $class ); - $segment = isset( $segments[ 1 ] ) ? $segments[ 1 ] : $class; + $segment = isset( $segments[1] ) ? $segments[1] : $class; return isset( $migration_segments[ $segment ] ) && $migration_segments[ $segment ]; } @@ -328,7 +346,7 @@ protected static function is_class_cli( $class ) { ); $segments = explode( '_', $class ); - $segment = isset( $segments[ 1 ] ) ? $segments[ 1 ] : $class; + $segment = isset( $segments[1] ) ? $segments[1] : $class; return isset( $cli_segments[ $segment ] ) && $cli_segments[ $segment ]; } @@ -337,14 +355,14 @@ protected static function is_class_cli( $class ) { * Clone. */ final public function __clone() { - trigger_error('Singleton. No cloning allowed!', E_USER_ERROR); + trigger_error( 'Singleton. No cloning allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error } /** * Wakeup. */ final public function __wakeup() { - trigger_error('Singleton. No serialization allowed!', E_USER_ERROR); + trigger_error( 'Singleton. No serialization allowed!', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error } /** From 39b4dab8c22822d991e0c28fdb5c2c4a9a829e92 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 20:14:38 -0400 Subject: [PATCH 11/24] eliminate code smells --- .../ActionScheduler_Abstract_QueueRunner.php | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/classes/abstracts/ActionScheduler_Abstract_QueueRunner.php b/classes/abstracts/ActionScheduler_Abstract_QueueRunner.php index f3526badd..574351a1e 100644 --- a/classes/abstracts/ActionScheduler_Abstract_QueueRunner.php +++ b/classes/abstracts/ActionScheduler_Abstract_QueueRunner.php @@ -5,13 +5,25 @@ */ abstract class ActionScheduler_Abstract_QueueRunner extends ActionScheduler_Abstract_QueueRunner_Deprecated { - /** @var ActionScheduler_QueueCleaner */ + /** + * ActionScheduler_QueueCleaner instance. + * + * @var ActionScheduler_QueueCleaner + */ protected $cleaner; - /** @var ActionScheduler_FatalErrorMonitor */ + /** + * ActionScheduler_FatalErrorMonitor instance. + * + * @var ActionScheduler_FatalErrorMonitor + */ protected $monitor; - /** @var ActionScheduler_Store */ + /** + * ActionScheduler_Store instance. + * + * @var ActionScheduler_Store + */ protected $store; /** @@ -50,6 +62,7 @@ public function __construct( ActionScheduler_Store $store = null, ActionSchedule */ public function process_action( $action_id, $context = '' ) { // Temporarily override the error handler while we process the current action. + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler set_error_handler( /** * Temporary error handler which can catch errors and convert them into exceptions. This facilitates more @@ -193,7 +206,7 @@ private function recurring_action_is_consistently_failing( ActionScheduler_Actio 'date' => date_create( 'now', timezone_open( 'UTC' ) )->format( 'Y-m-d H:i:s' ), 'date_compare' => '<', 'per_page' => 1, - 'offset' => $consistent_failure_threshold - 1 + 'offset' => $consistent_failure_threshold - 1, ); $first_failing_action_id = $this->store->query_actions( $query_args ); @@ -223,8 +236,6 @@ private function recurring_action_is_consistently_failing( ActionScheduler_Actio /** * Run the queue cleaner. - * - * @author Jeremy Pry */ protected function run_cleanup() { $this->cleaner->clean( 10 * $this->get_time_limit() ); @@ -364,7 +375,6 @@ protected function batch_limits_exceeded( $processed_actions ) { /** * Process actions in the queue. * - * @author Jeremy Pry * @param string $context Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' * Generally, this should be capitalised and not localised as it's a proper noun. * @return int The number of actions processed. From 75b4213ec0afa69c68dd376b55e18e4693f7f026 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 20:18:18 -0400 Subject: [PATCH 12/24] eliminate code smells --- ...onScheduler_Abstract_RecurringSchedule.php | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/classes/abstracts/ActionScheduler_Abstract_RecurringSchedule.php b/classes/abstracts/ActionScheduler_Abstract_RecurringSchedule.php index 8b32219d1..6cb9be313 100644 --- a/classes/abstracts/ActionScheduler_Abstract_RecurringSchedule.php +++ b/classes/abstracts/ActionScheduler_Abstract_RecurringSchedule.php @@ -15,14 +15,14 @@ abstract class ActionScheduler_Abstract_RecurringSchedule extends ActionSchedule * * @var DateTime */ - private $first_date = NULL; + private $first_date = null; /** * Timestamp equivalent of @see $this->first_date * * @var int */ - protected $first_timestamp = NULL; + protected $first_timestamp = null; /** * The recurrence between each time an action is run using this schedule. @@ -35,6 +35,8 @@ abstract class ActionScheduler_Abstract_RecurringSchedule extends ActionSchedule protected $recurrence; /** + * Construct. + * * @param DateTime $date The date & time to run the action. * @param mixed $recurrence The data used to determine the schedule's recurrence. * @param DateTime|null $first (Optional) The date & time the first instance of this interval schedule ran. Default null, meaning this is the first instance. @@ -46,6 +48,8 @@ public function __construct( DateTime $date, $recurrence, DateTime $first = null } /** + * Schedule is recurring. + * * @return bool */ public function is_recurring() { @@ -62,6 +66,8 @@ public function get_first_date() { } /** + * Get the schedule's recurrence. + * * @return string */ public function get_recurrence() { @@ -70,15 +76,19 @@ public function get_recurrence() { /** * For PHP 5.2 compat, since DateTime objects can't be serialized + * * @return array */ public function __sleep() { - $sleep_params = parent::__sleep(); + $sleep_params = parent::__sleep(); $this->first_timestamp = $this->first_date->getTimestamp(); - return array_merge( $sleep_params, array( - 'first_timestamp', - 'recurrence' - ) ); + return array_merge( + $sleep_params, + array( + 'first_timestamp', + 'recurrence', + ) + ); } /** From f43d43a92dc700cfe94c2f1d5cce2d86e88d4016 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 20:21:25 -0400 Subject: [PATCH 13/24] eliminate code smells --- classes/abstracts/ActionScheduler_Abstract_Schedule.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/classes/abstracts/ActionScheduler_Abstract_Schedule.php b/classes/abstracts/ActionScheduler_Abstract_Schedule.php index 44154a8a1..b7826b41a 100644 --- a/classes/abstracts/ActionScheduler_Abstract_Schedule.php +++ b/classes/abstracts/ActionScheduler_Abstract_Schedule.php @@ -10,16 +10,18 @@ abstract class ActionScheduler_Abstract_Schedule extends ActionScheduler_Schedul * * @var DateTime */ - private $scheduled_date = NULL; + private $scheduled_date = null; /** * Timestamp equivalent of @see $this->scheduled_date * * @var int */ - protected $scheduled_timestamp = NULL; + protected $scheduled_timestamp = null; /** + * Construct. + * * @param DateTime $date The date & time to run the action. */ public function __construct( DateTime $date ) { @@ -66,7 +68,8 @@ public function get_date() { } /** - * For PHP 5.2 compat, since DateTime objects can't be serialized + * For PHP 5.2 compat, because DateTime objects can't be serialized + * * @return array */ public function __sleep() { From 54409a001c4b72634a66dbd307042ddb4354acdc Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 20:28:54 -0400 Subject: [PATCH 14/24] eliminate code smells --- classes/abstracts/ActionScheduler_Logger.php | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/classes/abstracts/ActionScheduler_Logger.php b/classes/abstracts/ActionScheduler_Logger.php index 043194bb3..eedcf89a8 100644 --- a/classes/abstracts/ActionScheduler_Logger.php +++ b/classes/abstracts/ActionScheduler_Logger.php @@ -2,12 +2,17 @@ /** * Class ActionScheduler_Logger + * * @codeCoverageIgnore */ abstract class ActionScheduler_Logger { - /** @var null|self */ - private static $logger = NULL; + /** + * Instance. + * + * @var null|self + */ + private static $logger = null; /** * Get instance. @@ -15,8 +20,8 @@ abstract class ActionScheduler_Logger { * @return ActionScheduler_Logger */ public static function instance() { - if ( empty(self::$logger) ) { - $class = apply_filters('action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger'); + if ( empty( self::$logger ) ) { + $class = apply_filters( 'action_scheduler_logger_class', 'ActionScheduler_wpCommentLogger' ); self::$logger = new $class(); } return self::$logger; @@ -31,7 +36,7 @@ public static function instance() { * * @return string The log entry ID */ - abstract public function log( $action_id, $message, DateTime $date = NULL ); + abstract public function log( $action_id, $message, DateTime $date = null ); /** * Get action's log entry. @@ -125,9 +130,9 @@ public function log_started_action( $action_id, $context = '' ) { * * @param int $action_id Action ID. * @param null|ActionScheduler_Action $action Action. - * @param string $context Action exeuction context. + * @param string $context Action execution context. */ - public function log_completed_action( $action_id, $action = NULL, $context = '' ) { + public function log_completed_action( $action_id, $action = null, $context = '' ) { if ( ! empty( $context ) ) { /* translators: %s: context */ $message = sprintf( __( 'action complete via %s', 'action-scheduler' ), $context ); @@ -210,7 +215,7 @@ public function log_ignored_action( $action_id, $context = '' ) { * @param string $action_id Action ID. * @param null|Exception $exception The exception which occurred when fetching the action. NULL by default for backward compatibility. */ - public function log_failed_fetch_action( $action_id, Exception $exception = NULL ) { + public function log_failed_fetch_action( $action_id, Exception $exception = null ) { if ( ! is_null( $exception ) ) { /* translators: %s: exception message */ From c60bbeec122c4aae40ed07cacb74ddb54e9b72c4 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 20:37:31 -0400 Subject: [PATCH 15/24] eliminate code smells --- .../ActionScheduler_TimezoneHelper.php | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/classes/abstracts/ActionScheduler_TimezoneHelper.php b/classes/abstracts/ActionScheduler_TimezoneHelper.php index f064656ee..63813eba4 100644 --- a/classes/abstracts/ActionScheduler_TimezoneHelper.php +++ b/classes/abstracts/ActionScheduler_TimezoneHelper.php @@ -5,8 +5,12 @@ */ abstract class ActionScheduler_TimezoneHelper { - /** @var null|DateTimeZone */ - private static $local_timezone = NULL; + /** + * DateTimeZone object. + * + * @var null|DateTimeZone + */ + private static $local_timezone = null; /** * Set a DateTime's timezone to the WordPress site's timezone, or a UTC offset @@ -105,21 +109,21 @@ protected static function get_local_timezone_offset() { * @param bool $reset Toggle to discard stored value. * @deprecated 2.1.0 */ - public static function get_local_timezone( $reset = FALSE ) { + public static function get_local_timezone( $reset = false ) { _deprecated_function( __FUNCTION__, '2.1.0', 'ActionScheduler_TimezoneHelper::set_local_timezone()' ); if ( $reset ) { - self::$local_timezone = NULL; + self::$local_timezone = null; } - if ( !isset(self::$local_timezone) ) { - $tzstring = get_option('timezone_string'); + if ( ! isset( self::$local_timezone ) ) { + $tzstring = get_option( 'timezone_string' ); - if ( empty($tzstring) ) { - $gmt_offset = get_option('gmt_offset'); - if ( $gmt_offset == 0 ) { + if ( empty( $tzstring ) ) { + $gmt_offset = absint( get_option( 'gmt_offset' ) ); + if ( 0 === $gmt_offset ) { $tzstring = 'UTC'; } else { $gmt_offset *= HOUR_IN_SECONDS; - $tzstring = timezone_name_from_abbr( '', $gmt_offset, 1 ); + $tzstring = timezone_name_from_abbr( '', $gmt_offset, 1 ); // If there's no timezone string, try again with no DST. if ( false === $tzstring ) { @@ -131,9 +135,9 @@ public static function get_local_timezone( $reset = FALSE ) { $is_dst = date( 'I' ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date -- we are actually interested in the runtime timezone. foreach ( timezone_abbreviations_list() as $abbr ) { foreach ( $abbr as $city ) { - if ( $city['dst'] == $is_dst && $city['offset'] == $gmt_offset ) { + if ( $city['dst'] === $is_dst && $city['offset'] === $gmt_offset ) { // If there's no valid timezone ID, keep looking. - if ( null === $city['timezone_id'] ) { + if ( is_null( $city['timezone_id'] ) ) { continue; } @@ -151,7 +155,7 @@ public static function get_local_timezone( $reset = FALSE ) { } } - self::$local_timezone = new DateTimeZone($tzstring); + self::$local_timezone = new DateTimeZone( $tzstring ); } return self::$local_timezone; } From 95d49651d5cf9f626270fab07fe8bd347edc2581 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 22:12:25 -0400 Subject: [PATCH 16/24] eliminate code smells --- classes/schedules/ActionScheduler_CanceledSchedule.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/schedules/ActionScheduler_CanceledSchedule.php b/classes/schedules/ActionScheduler_CanceledSchedule.php index c70e812ee..b18960688 100644 --- a/classes/schedules/ActionScheduler_CanceledSchedule.php +++ b/classes/schedules/ActionScheduler_CanceledSchedule.php @@ -10,9 +10,11 @@ class ActionScheduler_CanceledSchedule extends ActionScheduler_SimpleSchedule { * * @var null */ - private $timestamp = NULL; + private $timestamp = null; /** + * Calculate when the next instance of this schedule would run based on a given date & time. + * * @param DateTime $after Timestamp. * * @return DateTime|null @@ -33,6 +35,8 @@ public function get_next( DateTime $after ) { } /** + * Action is not recurreing. + * * @return bool */ public function is_recurring() { From 54a512d8d7b4a3b158eca9e062b81c015ced7c81 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 22:16:51 -0400 Subject: [PATCH 17/24] eliminate code smells --- .../schedules/ActionScheduler_CronSchedule.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/classes/schedules/ActionScheduler_CronSchedule.php b/classes/schedules/ActionScheduler_CronSchedule.php index 4d32706c7..d721a2efd 100644 --- a/classes/schedules/ActionScheduler_CronSchedule.php +++ b/classes/schedules/ActionScheduler_CronSchedule.php @@ -10,14 +10,14 @@ class ActionScheduler_CronSchedule extends ActionScheduler_Abstract_RecurringSch * * @var null */ - private $start_timestamp = NULL; + private $start_timestamp = null; /** * Deprecated property @see $this->__wakeup() for details. * * @var null */ - private $cron = NULL; + private $cron = null; /** * Wrapper for parent constructor to accept a cron expression string and map it to a CronExpression for this @@ -53,6 +53,8 @@ protected function calculate_next( DateTime $after ) { } /** + * Get the schedule's recurrence. + * * @return string */ public function get_recurrence() { @@ -79,10 +81,13 @@ public function __sleep() { $this->start_timestamp = $this->scheduled_timestamp; $this->cron = $this->recurrence; - return array_merge( $sleep_params, array( - 'start_timestamp', - 'cron' - ) ); + return array_merge( + $sleep_params, + array( + 'start_timestamp', + 'cron', + ) + ); } /** From d8cdc1d50798b77d2d6e60dfcb52e3ef8ca6fd1d Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 22:19:44 -0400 Subject: [PATCH 18/24] eliminate code smells --- .../ActionScheduler_IntervalSchedule.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/classes/schedules/ActionScheduler_IntervalSchedule.php b/classes/schedules/ActionScheduler_IntervalSchedule.php index c88a480c1..228ef4c2c 100644 --- a/classes/schedules/ActionScheduler_IntervalSchedule.php +++ b/classes/schedules/ActionScheduler_IntervalSchedule.php @@ -10,14 +10,14 @@ class ActionScheduler_IntervalSchedule extends ActionScheduler_Abstract_Recurrin * * @var null */ - private $start_timestamp = NULL; + private $start_timestamp = null; /** * Deprecated property @see $this->__wakeup() for details. * * @var null */ - private $interval_in_seconds = NULL; + private $interval_in_seconds = null; /** * Calculate when this schedule should start after a given date & time using @@ -32,6 +32,8 @@ protected function calculate_next( DateTime $after ) { } /** + * Schedule interval in seconds. + * * @return int */ public function interval_in_seconds() { @@ -59,10 +61,13 @@ public function __sleep() { $this->start_timestamp = $this->scheduled_timestamp; $this->interval_in_seconds = $this->recurrence; - return array_merge( $sleep_params, array( - 'start_timestamp', - 'interval_in_seconds' - ) ); + return array_merge( + $sleep_params, + array( + 'start_timestamp', + 'interval_in_seconds', + ) + ); } /** From 22cbea404cd20dfe7602eea2c69cca64a309f389 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 22:21:14 -0400 Subject: [PATCH 19/24] eliminate code smells --- classes/schedules/ActionScheduler_NullSchedule.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/schedules/ActionScheduler_NullSchedule.php b/classes/schedules/ActionScheduler_NullSchedule.php index c9f74253a..cc1ddb581 100644 --- a/classes/schedules/ActionScheduler_NullSchedule.php +++ b/classes/schedules/ActionScheduler_NullSchedule.php @@ -5,7 +5,11 @@ */ class ActionScheduler_NullSchedule extends ActionScheduler_SimpleSchedule { - /** @var DateTime|null */ + /** + * DateTime instance. + * + * @var DateTime|null + */ protected $scheduled_date; /** @@ -18,7 +22,8 @@ public function __construct( DateTime $date = null ) { } /** - * This schedule has no scheduled DateTime, so we need to override the parent __sleep() + * This schedule has no scheduled DateTime, so we need to override the parent __sleep(). + * * @return array */ public function __sleep() { From a0039ece192c48b4b2c65804584e5b6b3760db30 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 23:26:48 -0400 Subject: [PATCH 20/24] eliminate code smells --- classes/schedules/ActionScheduler_Schedule.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/classes/schedules/ActionScheduler_Schedule.php b/classes/schedules/ActionScheduler_Schedule.php index cbe0af601..6fa175ed2 100644 --- a/classes/schedules/ActionScheduler_Schedule.php +++ b/classes/schedules/ActionScheduler_Schedule.php @@ -5,12 +5,17 @@ */ interface ActionScheduler_Schedule { /** + * Get the date & time this schedule was created to run, or calculate when it should be run + * after a given date & time. + * * @param null|DateTime $after Timestamp. * @return DateTime|null */ - public function next( DateTime $after = NULL ); + public function next( DateTime $after = null ); /** + * Identify the schedule as (not) recurring. + * * @return bool */ public function is_recurring(); From 130cebd5ae79993639bfe6f1b6d8ecd45b70081e Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 23:29:11 -0400 Subject: [PATCH 21/24] eliminate code smells --- .../schedules/ActionScheduler_SimpleSchedule.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/classes/schedules/ActionScheduler_SimpleSchedule.php b/classes/schedules/ActionScheduler_SimpleSchedule.php index 64f89c8e7..f1ce2e013 100644 --- a/classes/schedules/ActionScheduler_SimpleSchedule.php +++ b/classes/schedules/ActionScheduler_SimpleSchedule.php @@ -10,9 +10,12 @@ class ActionScheduler_SimpleSchedule extends ActionScheduler_Abstract_Schedule { * * @var null|DateTime */ - private $timestamp = NULL; + private $timestamp = null; /** + * Calculate when this schedule should start after a given date & time using + * the number of seconds between recurrences. + * * @param DateTime $after Timestamp. * * @return DateTime|null @@ -22,6 +25,8 @@ public function calculate_next( DateTime $after ) { } /** + * Schedule is not recurring. + * * @return bool */ public function is_recurring() { @@ -47,9 +52,12 @@ public function __sleep() { $this->timestamp = $this->scheduled_timestamp; - return array_merge( $sleep_params, array( - 'timestamp', - ) ); + return array_merge( + $sleep_params, + array( + 'timestamp', + ) + ); } /** From 748f18a9cdf169c771a730f217764c651d895a63 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 23:31:12 -0400 Subject: [PATCH 22/24] eliminate code smells --- classes/schema/ActionScheduler_LoggerSchema.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/classes/schema/ActionScheduler_LoggerSchema.php b/classes/schema/ActionScheduler_LoggerSchema.php index f31dc568b..27e1c7ff9 100644 --- a/classes/schema/ActionScheduler_LoggerSchema.php +++ b/classes/schema/ActionScheduler_LoggerSchema.php @@ -11,7 +11,11 @@ class ActionScheduler_LoggerSchema extends ActionScheduler_Abstract_Schema { const LOG_TABLE = 'actionscheduler_logs'; /** - * @var int Increment this value to trigger a schema update. + * Schema version. + * + * Increment this value to trigger a schema update. + * + * @var int */ protected $schema_version = 3; @@ -19,9 +23,9 @@ class ActionScheduler_LoggerSchema extends ActionScheduler_Abstract_Schema { * Construct. */ public function __construct() { - $this->tables = [ + $this->tables = array( self::LOG_TABLE, - ]; + ); } /** @@ -38,12 +42,11 @@ public function init() { */ protected function get_table_definition( $table ) { global $wpdb; - $table_name = $wpdb->$table; - $charset_collate = $wpdb->get_charset_collate(); + $table_name = $wpdb->$table; + $charset_collate = $wpdb->get_charset_collate(); switch ( $table ) { case self::LOG_TABLE: - $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; return "CREATE TABLE $table_name ( log_id bigint(20) unsigned NOT NULL auto_increment, From 090841cec422db30e45b2104561c0a47237aaa59 Mon Sep 17 00:00:00 2001 From: Caleb Stauffer Date: Thu, 3 Oct 2024 23:35:21 -0400 Subject: [PATCH 23/24] eliminate code smells --- .../schema/ActionScheduler_StoreSchema.php | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/classes/schema/ActionScheduler_StoreSchema.php b/classes/schema/ActionScheduler_StoreSchema.php index 68d93a268..37df226bc 100644 --- a/classes/schema/ActionScheduler_StoreSchema.php +++ b/classes/schema/ActionScheduler_StoreSchema.php @@ -14,7 +14,11 @@ class ActionScheduler_StoreSchema extends ActionScheduler_Abstract_Schema { const DEFAULT_DATE = '0000-00-00 00:00:00'; /** - * @var int Increment this value to trigger a schema update. + * Schema version. + * + * Increment this value to trigger a schema update. + * + * @var int */ protected $schema_version = 7; @@ -22,11 +26,11 @@ class ActionScheduler_StoreSchema extends ActionScheduler_Abstract_Schema { * Construct. */ public function __construct() { - $this->tables = [ + $this->tables = array( self::ACTIONS_TABLE, self::CLAIMS_TABLE, self::GROUPS_TABLE, - ]; + ); } /** @@ -43,16 +47,17 @@ public function init() { */ protected function get_table_definition( $table ) { global $wpdb; - $table_name = $wpdb->$table; - $charset_collate = $wpdb->get_charset_collate(); + $table_name = $wpdb->$table; + $charset_collate = $wpdb->get_charset_collate(); + $default_date = self::DEFAULT_DATE; // phpcs:ignore Squiz.PHP.CommentedOutCode $max_index_length = 191; // @see wp_get_db_schema() + $hook_status_scheduled_date_gmt_max_index_length = $max_index_length - 20 - 8; // - status, - scheduled_date_gmt - $default_date = self::DEFAULT_DATE; + switch ( $table ) { case self::ACTIONS_TABLE: - return "CREATE TABLE {$table_name} ( action_id bigint(20) unsigned NOT NULL auto_increment, hook varchar(191) NOT NULL, @@ -79,7 +84,6 @@ protected function get_table_definition( $table ) { ) $charset_collate"; case self::CLAIMS_TABLE: - return "CREATE TABLE {$table_name} ( claim_id bigint(20) unsigned NOT NULL auto_increment, date_created_gmt datetime NULL default '{$default_date}', @@ -88,7 +92,6 @@ protected function get_table_definition( $table ) { ) $charset_collate"; case self::GROUPS_TABLE: - return "CREATE TABLE {$table_name} ( group_id bigint(20) unsigned NOT NULL auto_increment, slug varchar(255) NOT NULL, From 6baa1d8348fe2e2d579e28a9535af02975788445 Mon Sep 17 00:00:00 2001 From: Barry Hughes <3594411+barryhughes@users.noreply.github.com> Date: Wed, 23 Oct 2024 12:11:41 -0700 Subject: [PATCH 24/24] Correct typo. --- classes/schedules/ActionScheduler_CanceledSchedule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/schedules/ActionScheduler_CanceledSchedule.php b/classes/schedules/ActionScheduler_CanceledSchedule.php index b18960688..4f90c8333 100644 --- a/classes/schedules/ActionScheduler_CanceledSchedule.php +++ b/classes/schedules/ActionScheduler_CanceledSchedule.php @@ -35,7 +35,7 @@ public function get_next( DateTime $after ) { } /** - * Action is not recurreing. + * Action is not recurring. * * @return bool */