@@ -12,97 +12,97 @@ |
||
12 | 12 | * @codeCoverageIgnore |
13 | 13 | */ |
14 | 14 | class ActionMigrator { |
15 | - /** var ActionScheduler_Store */ |
|
16 | - private $source; |
|
17 | - |
|
18 | - /** var ActionScheduler_Store */ |
|
19 | - private $destination; |
|
20 | - |
|
21 | - /** var LogMigrator */ |
|
22 | - private $log_migrator; |
|
23 | - |
|
24 | - /** |
|
25 | - * ActionMigrator constructor. |
|
26 | - * |
|
27 | - * @param ActionScheduler_Store $source_store Source store object. |
|
28 | - * @param ActionScheduler_Store $destination_store Destination store object. |
|
29 | - * @param LogMigrator $log_migrator Log migrator object. |
|
30 | - */ |
|
31 | - public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
|
32 | - $this->source = $source_store; |
|
33 | - $this->destination = $destination_store; |
|
34 | - $this->log_migrator = $log_migrator; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Migrate an action. |
|
39 | - * |
|
40 | - * @param int $source_action_id Action ID. |
|
41 | - * |
|
42 | - * @return int 0|new action ID |
|
43 | - */ |
|
44 | - public function migrate( $source_action_id ) { |
|
45 | - try { |
|
46 | - $action = $this->source->fetch_action( $source_action_id ); |
|
47 | - $status = $this->source->get_status( $source_action_id ); |
|
48 | - } catch ( \Exception $e ) { |
|
49 | - $action = null; |
|
50 | - $status = ''; |
|
51 | - } |
|
52 | - |
|
53 | - if ( $action === null || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
|
54 | - // null action or empty status means the fetch operation failed or the action didn't exist |
|
55 | - // null schedule means it's missing vital data |
|
56 | - // delete it and move on |
|
57 | - try { |
|
58 | - $this->source->delete_action( $source_action_id ); |
|
59 | - } catch ( \Exception $e ) { |
|
60 | - // nothing to do, it didn't exist in the first place |
|
61 | - } |
|
62 | - do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
|
63 | - |
|
64 | - return 0; |
|
65 | - } |
|
66 | - |
|
67 | - try { |
|
68 | - |
|
69 | - // Make sure the last attempt date is set correctly for completed and failed actions |
|
70 | - $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
|
71 | - |
|
72 | - $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
|
73 | - } catch ( \Exception $e ) { |
|
74 | - do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
|
75 | - |
|
76 | - return 0; // could not save the action in the new store |
|
77 | - } |
|
78 | - |
|
79 | - try { |
|
80 | - switch ( $status ) { |
|
81 | - case \ActionScheduler_Store::STATUS_FAILED: |
|
82 | - $this->destination->mark_failure( $destination_action_id ); |
|
83 | - break; |
|
84 | - case \ActionScheduler_Store::STATUS_CANCELED: |
|
85 | - $this->destination->cancel_action( $destination_action_id ); |
|
86 | - break; |
|
87 | - } |
|
88 | - |
|
89 | - $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
|
90 | - $this->source->delete_action( $source_action_id ); |
|
91 | - |
|
92 | - $test_action = $this->source->fetch_action( $source_action_id ); |
|
93 | - if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
|
94 | - throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
|
95 | - } |
|
96 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
97 | - |
|
98 | - return $destination_action_id; |
|
99 | - } catch ( \Exception $e ) { |
|
100 | - // could not delete from the old store |
|
101 | - $this->source->mark_migrated( $source_action_id ); |
|
102 | - do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
103 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
104 | - |
|
105 | - return $destination_action_id; |
|
106 | - } |
|
107 | - } |
|
15 | + /** var ActionScheduler_Store */ |
|
16 | + private $source; |
|
17 | + |
|
18 | + /** var ActionScheduler_Store */ |
|
19 | + private $destination; |
|
20 | + |
|
21 | + /** var LogMigrator */ |
|
22 | + private $log_migrator; |
|
23 | + |
|
24 | + /** |
|
25 | + * ActionMigrator constructor. |
|
26 | + * |
|
27 | + * @param ActionScheduler_Store $source_store Source store object. |
|
28 | + * @param ActionScheduler_Store $destination_store Destination store object. |
|
29 | + * @param LogMigrator $log_migrator Log migrator object. |
|
30 | + */ |
|
31 | + public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
|
32 | + $this->source = $source_store; |
|
33 | + $this->destination = $destination_store; |
|
34 | + $this->log_migrator = $log_migrator; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Migrate an action. |
|
39 | + * |
|
40 | + * @param int $source_action_id Action ID. |
|
41 | + * |
|
42 | + * @return int 0|new action ID |
|
43 | + */ |
|
44 | + public function migrate( $source_action_id ) { |
|
45 | + try { |
|
46 | + $action = $this->source->fetch_action( $source_action_id ); |
|
47 | + $status = $this->source->get_status( $source_action_id ); |
|
48 | + } catch ( \Exception $e ) { |
|
49 | + $action = null; |
|
50 | + $status = ''; |
|
51 | + } |
|
52 | + |
|
53 | + if ( $action === null || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
|
54 | + // null action or empty status means the fetch operation failed or the action didn't exist |
|
55 | + // null schedule means it's missing vital data |
|
56 | + // delete it and move on |
|
57 | + try { |
|
58 | + $this->source->delete_action( $source_action_id ); |
|
59 | + } catch ( \Exception $e ) { |
|
60 | + // nothing to do, it didn't exist in the first place |
|
61 | + } |
|
62 | + do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
|
63 | + |
|
64 | + return 0; |
|
65 | + } |
|
66 | + |
|
67 | + try { |
|
68 | + |
|
69 | + // Make sure the last attempt date is set correctly for completed and failed actions |
|
70 | + $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
|
71 | + |
|
72 | + $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
|
73 | + } catch ( \Exception $e ) { |
|
74 | + do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
|
75 | + |
|
76 | + return 0; // could not save the action in the new store |
|
77 | + } |
|
78 | + |
|
79 | + try { |
|
80 | + switch ( $status ) { |
|
81 | + case \ActionScheduler_Store::STATUS_FAILED: |
|
82 | + $this->destination->mark_failure( $destination_action_id ); |
|
83 | + break; |
|
84 | + case \ActionScheduler_Store::STATUS_CANCELED: |
|
85 | + $this->destination->cancel_action( $destination_action_id ); |
|
86 | + break; |
|
87 | + } |
|
88 | + |
|
89 | + $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
|
90 | + $this->source->delete_action( $source_action_id ); |
|
91 | + |
|
92 | + $test_action = $this->source->fetch_action( $source_action_id ); |
|
93 | + if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
|
94 | + throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
|
95 | + } |
|
96 | + do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
97 | + |
|
98 | + return $destination_action_id; |
|
99 | + } catch ( \Exception $e ) { |
|
100 | + // could not delete from the old store |
|
101 | + $this->source->mark_migrated( $source_action_id ); |
|
102 | + do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
103 | + do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
104 | + |
|
105 | + return $destination_action_id; |
|
106 | + } |
|
107 | + } |
|
108 | 108 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | * @param ActionScheduler_Store $destination_store Destination store object. |
29 | 29 | * @param LogMigrator $log_migrator Log migrator object. |
30 | 30 | */ |
31 | - public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) { |
|
31 | + public function __construct(\ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator) { |
|
32 | 32 | $this->source = $source_store; |
33 | 33 | $this->destination = $destination_store; |
34 | 34 | $this->log_migrator = $log_migrator; |
@@ -41,25 +41,25 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return int 0|new action ID |
43 | 43 | */ |
44 | - public function migrate( $source_action_id ) { |
|
44 | + public function migrate($source_action_id) { |
|
45 | 45 | try { |
46 | - $action = $this->source->fetch_action( $source_action_id ); |
|
47 | - $status = $this->source->get_status( $source_action_id ); |
|
48 | - } catch ( \Exception $e ) { |
|
46 | + $action = $this->source->fetch_action($source_action_id); |
|
47 | + $status = $this->source->get_status($source_action_id); |
|
48 | + } catch (\Exception $e) { |
|
49 | 49 | $action = null; |
50 | 50 | $status = ''; |
51 | 51 | } |
52 | 52 | |
53 | - if ( $action === null || empty( $status ) || ! $action->get_schedule()->get_date() ) { |
|
53 | + if ($action === null || empty($status) || ! $action->get_schedule()->get_date()) { |
|
54 | 54 | // null action or empty status means the fetch operation failed or the action didn't exist |
55 | 55 | // null schedule means it's missing vital data |
56 | 56 | // delete it and move on |
57 | 57 | try { |
58 | - $this->source->delete_action( $source_action_id ); |
|
59 | - } catch ( \Exception $e ) { |
|
58 | + $this->source->delete_action($source_action_id); |
|
59 | + } catch (\Exception $e) { |
|
60 | 60 | // nothing to do, it didn't exist in the first place |
61 | 61 | } |
62 | - do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination ); |
|
62 | + do_action('action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination); |
|
63 | 63 | |
64 | 64 | return 0; |
65 | 65 | } |
@@ -67,40 +67,40 @@ discard block |
||
67 | 67 | try { |
68 | 68 | |
69 | 69 | // Make sure the last attempt date is set correctly for completed and failed actions |
70 | - $last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null; |
|
70 | + $last_attempt_date = ($status !== \ActionScheduler_Store::STATUS_PENDING) ? $this->source->get_date($source_action_id) : null; |
|
71 | 71 | |
72 | - $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date ); |
|
73 | - } catch ( \Exception $e ) { |
|
74 | - do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination ); |
|
72 | + $destination_action_id = $this->destination->save_action($action, null, $last_attempt_date); |
|
73 | + } catch (\Exception $e) { |
|
74 | + do_action('action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination); |
|
75 | 75 | |
76 | 76 | return 0; // could not save the action in the new store |
77 | 77 | } |
78 | 78 | |
79 | 79 | try { |
80 | - switch ( $status ) { |
|
80 | + switch ($status) { |
|
81 | 81 | case \ActionScheduler_Store::STATUS_FAILED: |
82 | - $this->destination->mark_failure( $destination_action_id ); |
|
82 | + $this->destination->mark_failure($destination_action_id); |
|
83 | 83 | break; |
84 | 84 | case \ActionScheduler_Store::STATUS_CANCELED: |
85 | - $this->destination->cancel_action( $destination_action_id ); |
|
85 | + $this->destination->cancel_action($destination_action_id); |
|
86 | 86 | break; |
87 | 87 | } |
88 | 88 | |
89 | - $this->log_migrator->migrate( $source_action_id, $destination_action_id ); |
|
90 | - $this->source->delete_action( $source_action_id ); |
|
89 | + $this->log_migrator->migrate($source_action_id, $destination_action_id); |
|
90 | + $this->source->delete_action($source_action_id); |
|
91 | 91 | |
92 | - $test_action = $this->source->fetch_action( $source_action_id ); |
|
93 | - if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) { |
|
94 | - throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) ); |
|
92 | + $test_action = $this->source->fetch_action($source_action_id); |
|
93 | + if ( ! is_a($test_action, 'ActionScheduler_NullAction')) { |
|
94 | + throw new \RuntimeException(sprintf(__('Unable to remove source migrated action %s', 'action-scheduler'), $source_action_id)); |
|
95 | 95 | } |
96 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
96 | + do_action('action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination); |
|
97 | 97 | |
98 | 98 | return $destination_action_id; |
99 | - } catch ( \Exception $e ) { |
|
99 | + } catch (\Exception $e) { |
|
100 | 100 | // could not delete from the old store |
101 | - $this->source->mark_migrated( $source_action_id ); |
|
102 | - do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
103 | - do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination ); |
|
101 | + $this->source->mark_migrated($source_action_id); |
|
102 | + do_action('action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination); |
|
103 | + do_action('action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination); |
|
104 | 104 | |
105 | 105 | return $destination_action_id; |
106 | 106 | } |
@@ -12,127 +12,127 @@ |
||
12 | 12 | * @codeCoverageIgnore |
13 | 13 | */ |
14 | 14 | class Runner { |
15 | - /** @var ActionScheduler_Store */ |
|
16 | - private $source_store; |
|
17 | - |
|
18 | - /** @var ActionScheduler_Store */ |
|
19 | - private $destination_store; |
|
20 | - |
|
21 | - /** @var ActionScheduler_Logger */ |
|
22 | - private $source_logger; |
|
23 | - |
|
24 | - /** @var ActionScheduler_Logger */ |
|
25 | - private $destination_logger; |
|
26 | - |
|
27 | - /** @var BatchFetcher */ |
|
28 | - private $batch_fetcher; |
|
29 | - |
|
30 | - /** @var ActionMigrator */ |
|
31 | - private $action_migrator; |
|
32 | - |
|
33 | - /** @var LogMigrator */ |
|
34 | - private $log_migrator; |
|
35 | - |
|
36 | - /** @var ProgressBar */ |
|
37 | - private $progress_bar; |
|
38 | - |
|
39 | - /** |
|
40 | - * Runner constructor. |
|
41 | - * |
|
42 | - * @param Config $config Migration configuration object. |
|
43 | - */ |
|
44 | - public function __construct( Config $config ) { |
|
45 | - $this->source_store = $config->get_source_store(); |
|
46 | - $this->destination_store = $config->get_destination_store(); |
|
47 | - $this->source_logger = $config->get_source_logger(); |
|
48 | - $this->destination_logger = $config->get_destination_logger(); |
|
49 | - |
|
50 | - $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
|
51 | - if ( $config->get_dry_run() ) { |
|
52 | - $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
|
53 | - $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
54 | - } else { |
|
55 | - $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
|
56 | - $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
57 | - } |
|
58 | - |
|
59 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
60 | - $this->progress_bar = $config->get_progress_bar(); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Run migration batch. |
|
66 | - * |
|
67 | - * @param int $batch_size Optional batch size. Default 10. |
|
68 | - * |
|
69 | - * @return int Size of batch processed. |
|
70 | - */ |
|
71 | - public function run( $batch_size = 10 ) { |
|
72 | - $batch = $this->batch_fetcher->fetch( $batch_size ); |
|
73 | - $batch_size = count( $batch ); |
|
74 | - |
|
75 | - if ( ! $batch_size ) { |
|
76 | - return 0; |
|
77 | - } |
|
78 | - |
|
79 | - if ( $this->progress_bar ) { |
|
80 | - /* translators: %d: amount of actions */ |
|
81 | - $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), number_format_i18n( $batch_size ) ) ); |
|
82 | - $this->progress_bar->set_count( $batch_size ); |
|
83 | - } |
|
84 | - |
|
85 | - $this->migrate_actions( $batch ); |
|
86 | - |
|
87 | - return $batch_size; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Migration a batch of actions. |
|
92 | - * |
|
93 | - * @param array $action_ids List of action IDs to migrate. |
|
94 | - */ |
|
95 | - public function migrate_actions( array $action_ids ) { |
|
96 | - do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
|
97 | - |
|
98 | - \ActionScheduler::logger()->unhook_stored_action(); |
|
99 | - $this->destination_logger->unhook_stored_action(); |
|
100 | - |
|
101 | - foreach ( $action_ids as $source_action_id ) { |
|
102 | - $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
|
103 | - if ( $destination_action_id ) { |
|
104 | - $this->destination_logger->log( |
|
105 | - $destination_action_id, |
|
106 | - sprintf( |
|
107 | - /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
|
108 | - __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
|
109 | - $source_action_id, |
|
110 | - get_class( $this->source_store ), |
|
111 | - $destination_action_id, |
|
112 | - get_class( $this->destination_store ) |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - if ( $this->progress_bar ) { |
|
118 | - $this->progress_bar->tick(); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - if ( $this->progress_bar ) { |
|
123 | - $this->progress_bar->finish(); |
|
124 | - } |
|
125 | - |
|
126 | - \ActionScheduler::logger()->hook_stored_action(); |
|
127 | - |
|
128 | - do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Initialize destination store and logger. |
|
133 | - */ |
|
134 | - public function init_destination() { |
|
135 | - $this->destination_store->init(); |
|
136 | - $this->destination_logger->init(); |
|
137 | - } |
|
15 | + /** @var ActionScheduler_Store */ |
|
16 | + private $source_store; |
|
17 | + |
|
18 | + /** @var ActionScheduler_Store */ |
|
19 | + private $destination_store; |
|
20 | + |
|
21 | + /** @var ActionScheduler_Logger */ |
|
22 | + private $source_logger; |
|
23 | + |
|
24 | + /** @var ActionScheduler_Logger */ |
|
25 | + private $destination_logger; |
|
26 | + |
|
27 | + /** @var BatchFetcher */ |
|
28 | + private $batch_fetcher; |
|
29 | + |
|
30 | + /** @var ActionMigrator */ |
|
31 | + private $action_migrator; |
|
32 | + |
|
33 | + /** @var LogMigrator */ |
|
34 | + private $log_migrator; |
|
35 | + |
|
36 | + /** @var ProgressBar */ |
|
37 | + private $progress_bar; |
|
38 | + |
|
39 | + /** |
|
40 | + * Runner constructor. |
|
41 | + * |
|
42 | + * @param Config $config Migration configuration object. |
|
43 | + */ |
|
44 | + public function __construct( Config $config ) { |
|
45 | + $this->source_store = $config->get_source_store(); |
|
46 | + $this->destination_store = $config->get_destination_store(); |
|
47 | + $this->source_logger = $config->get_source_logger(); |
|
48 | + $this->destination_logger = $config->get_destination_logger(); |
|
49 | + |
|
50 | + $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
|
51 | + if ( $config->get_dry_run() ) { |
|
52 | + $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
|
53 | + $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
54 | + } else { |
|
55 | + $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
|
56 | + $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
57 | + } |
|
58 | + |
|
59 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
60 | + $this->progress_bar = $config->get_progress_bar(); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Run migration batch. |
|
66 | + * |
|
67 | + * @param int $batch_size Optional batch size. Default 10. |
|
68 | + * |
|
69 | + * @return int Size of batch processed. |
|
70 | + */ |
|
71 | + public function run( $batch_size = 10 ) { |
|
72 | + $batch = $this->batch_fetcher->fetch( $batch_size ); |
|
73 | + $batch_size = count( $batch ); |
|
74 | + |
|
75 | + if ( ! $batch_size ) { |
|
76 | + return 0; |
|
77 | + } |
|
78 | + |
|
79 | + if ( $this->progress_bar ) { |
|
80 | + /* translators: %d: amount of actions */ |
|
81 | + $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), number_format_i18n( $batch_size ) ) ); |
|
82 | + $this->progress_bar->set_count( $batch_size ); |
|
83 | + } |
|
84 | + |
|
85 | + $this->migrate_actions( $batch ); |
|
86 | + |
|
87 | + return $batch_size; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Migration a batch of actions. |
|
92 | + * |
|
93 | + * @param array $action_ids List of action IDs to migrate. |
|
94 | + */ |
|
95 | + public function migrate_actions( array $action_ids ) { |
|
96 | + do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
|
97 | + |
|
98 | + \ActionScheduler::logger()->unhook_stored_action(); |
|
99 | + $this->destination_logger->unhook_stored_action(); |
|
100 | + |
|
101 | + foreach ( $action_ids as $source_action_id ) { |
|
102 | + $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
|
103 | + if ( $destination_action_id ) { |
|
104 | + $this->destination_logger->log( |
|
105 | + $destination_action_id, |
|
106 | + sprintf( |
|
107 | + /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
|
108 | + __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
|
109 | + $source_action_id, |
|
110 | + get_class( $this->source_store ), |
|
111 | + $destination_action_id, |
|
112 | + get_class( $this->destination_store ) |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + if ( $this->progress_bar ) { |
|
118 | + $this->progress_bar->tick(); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + if ( $this->progress_bar ) { |
|
123 | + $this->progress_bar->finish(); |
|
124 | + } |
|
125 | + |
|
126 | + \ActionScheduler::logger()->hook_stored_action(); |
|
127 | + |
|
128 | + do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Initialize destination store and logger. |
|
133 | + */ |
|
134 | + public function init_destination() { |
|
135 | + $this->destination_store->init(); |
|
136 | + $this->destination_logger->init(); |
|
137 | + } |
|
138 | 138 | } |
@@ -41,22 +41,22 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @param Config $config Migration configuration object. |
43 | 43 | */ |
44 | - public function __construct( Config $config ) { |
|
44 | + public function __construct(Config $config) { |
|
45 | 45 | $this->source_store = $config->get_source_store(); |
46 | 46 | $this->destination_store = $config->get_destination_store(); |
47 | 47 | $this->source_logger = $config->get_source_logger(); |
48 | 48 | $this->destination_logger = $config->get_destination_logger(); |
49 | 49 | |
50 | - $this->batch_fetcher = new BatchFetcher( $this->source_store ); |
|
51 | - if ( $config->get_dry_run() ) { |
|
52 | - $this->log_migrator = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger ); |
|
53 | - $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
50 | + $this->batch_fetcher = new BatchFetcher($this->source_store); |
|
51 | + if ($config->get_dry_run()) { |
|
52 | + $this->log_migrator = new DryRun_LogMigrator($this->source_logger, $this->destination_logger); |
|
53 | + $this->action_migrator = new DryRun_ActionMigrator($this->source_store, $this->destination_store, $this->log_migrator); |
|
54 | 54 | } else { |
55 | - $this->log_migrator = new LogMigrator( $this->source_logger, $this->destination_logger ); |
|
56 | - $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator ); |
|
55 | + $this->log_migrator = new LogMigrator($this->source_logger, $this->destination_logger); |
|
56 | + $this->action_migrator = new ActionMigrator($this->source_store, $this->destination_store, $this->log_migrator); |
|
57 | 57 | } |
58 | 58 | |
59 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
59 | + if (defined('WP_CLI') && WP_CLI) { |
|
60 | 60 | $this->progress_bar = $config->get_progress_bar(); |
61 | 61 | } |
62 | 62 | } |
@@ -68,21 +68,21 @@ discard block |
||
68 | 68 | * |
69 | 69 | * @return int Size of batch processed. |
70 | 70 | */ |
71 | - public function run( $batch_size = 10 ) { |
|
72 | - $batch = $this->batch_fetcher->fetch( $batch_size ); |
|
73 | - $batch_size = count( $batch ); |
|
71 | + public function run($batch_size = 10) { |
|
72 | + $batch = $this->batch_fetcher->fetch($batch_size); |
|
73 | + $batch_size = count($batch); |
|
74 | 74 | |
75 | - if ( ! $batch_size ) { |
|
75 | + if ( ! $batch_size) { |
|
76 | 76 | return 0; |
77 | 77 | } |
78 | 78 | |
79 | - if ( $this->progress_bar ) { |
|
79 | + if ($this->progress_bar) { |
|
80 | 80 | /* translators: %d: amount of actions */ |
81 | - $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler' ), number_format_i18n( $batch_size ) ) ); |
|
82 | - $this->progress_bar->set_count( $batch_size ); |
|
81 | + $this->progress_bar->set_message(sprintf(_n('Migrating %d action', 'Migrating %d actions', $batch_size, 'action-scheduler'), number_format_i18n($batch_size))); |
|
82 | + $this->progress_bar->set_count($batch_size); |
|
83 | 83 | } |
84 | 84 | |
85 | - $this->migrate_actions( $batch ); |
|
85 | + $this->migrate_actions($batch); |
|
86 | 86 | |
87 | 87 | return $batch_size; |
88 | 88 | } |
@@ -92,40 +92,40 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @param array $action_ids List of action IDs to migrate. |
94 | 94 | */ |
95 | - public function migrate_actions( array $action_ids ) { |
|
96 | - do_action( 'action_scheduler/migration_batch_starting', $action_ids ); |
|
95 | + public function migrate_actions(array $action_ids) { |
|
96 | + do_action('action_scheduler/migration_batch_starting', $action_ids); |
|
97 | 97 | |
98 | 98 | \ActionScheduler::logger()->unhook_stored_action(); |
99 | 99 | $this->destination_logger->unhook_stored_action(); |
100 | 100 | |
101 | - foreach ( $action_ids as $source_action_id ) { |
|
102 | - $destination_action_id = $this->action_migrator->migrate( $source_action_id ); |
|
103 | - if ( $destination_action_id ) { |
|
101 | + foreach ($action_ids as $source_action_id) { |
|
102 | + $destination_action_id = $this->action_migrator->migrate($source_action_id); |
|
103 | + if ($destination_action_id) { |
|
104 | 104 | $this->destination_logger->log( |
105 | 105 | $destination_action_id, |
106 | 106 | sprintf( |
107 | 107 | /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */ |
108 | - __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler' ), |
|
108 | + __('Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'action-scheduler'), |
|
109 | 109 | $source_action_id, |
110 | - get_class( $this->source_store ), |
|
110 | + get_class($this->source_store), |
|
111 | 111 | $destination_action_id, |
112 | - get_class( $this->destination_store ) |
|
112 | + get_class($this->destination_store) |
|
113 | 113 | ) |
114 | 114 | ); |
115 | 115 | } |
116 | 116 | |
117 | - if ( $this->progress_bar ) { |
|
117 | + if ($this->progress_bar) { |
|
118 | 118 | $this->progress_bar->tick(); |
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
122 | - if ( $this->progress_bar ) { |
|
122 | + if ($this->progress_bar) { |
|
123 | 123 | $this->progress_bar->finish(); |
124 | 124 | } |
125 | 125 | |
126 | 126 | \ActionScheduler::logger()->hook_stored_action(); |
127 | 127 | |
128 | - do_action( 'action_scheduler/migration_batch_complete', $action_ids ); |
|
128 | + do_action('action_scheduler/migration_batch_complete', $action_ids); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
@@ -9,39 +9,39 @@ |
||
9 | 9 | */ |
10 | 10 | class ActionScheduler_DBStoreMigrator extends ActionScheduler_DBStore { |
11 | 11 | |
12 | - /** |
|
13 | - * Save an action with optional last attempt date. |
|
14 | - * |
|
15 | - * Normally, saving an action sets its attempted date to 0000-00-00 00:00:00 because when an action is first saved, |
|
16 | - * it can't have been attempted yet, but migrated completed actions will have an attempted date, so we need to save |
|
17 | - * that when first saving the action. |
|
18 | - * |
|
19 | - * @param ActionScheduler_Action $action |
|
20 | - * @param \DateTime $scheduled_date Optional date of the first instance to store. |
|
21 | - * @param \DateTime $last_attempt_date Optional date the action was last attempted. |
|
22 | - * |
|
23 | - * @return string The action ID |
|
24 | - * @throws \RuntimeException When the action is not saved. |
|
25 | - */ |
|
26 | - public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null, \DateTime $last_attempt_date = null ) { |
|
27 | - try { |
|
28 | - /** @var \wpdb $wpdb */ |
|
29 | - global $wpdb; |
|
12 | + /** |
|
13 | + * Save an action with optional last attempt date. |
|
14 | + * |
|
15 | + * Normally, saving an action sets its attempted date to 0000-00-00 00:00:00 because when an action is first saved, |
|
16 | + * it can't have been attempted yet, but migrated completed actions will have an attempted date, so we need to save |
|
17 | + * that when first saving the action. |
|
18 | + * |
|
19 | + * @param ActionScheduler_Action $action |
|
20 | + * @param \DateTime $scheduled_date Optional date of the first instance to store. |
|
21 | + * @param \DateTime $last_attempt_date Optional date the action was last attempted. |
|
22 | + * |
|
23 | + * @return string The action ID |
|
24 | + * @throws \RuntimeException When the action is not saved. |
|
25 | + */ |
|
26 | + public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null, \DateTime $last_attempt_date = null ) { |
|
27 | + try { |
|
28 | + /** @var \wpdb $wpdb */ |
|
29 | + global $wpdb; |
|
30 | 30 | |
31 | - $action_id = parent::save_action( $action, $scheduled_date ); |
|
31 | + $action_id = parent::save_action( $action, $scheduled_date ); |
|
32 | 32 | |
33 | - if ( null !== $last_attempt_date ) { |
|
34 | - $data = array( |
|
35 | - 'last_attempt_gmt' => $this->get_scheduled_date_string( $action, $last_attempt_date ), |
|
36 | - 'last_attempt_local' => $this->get_scheduled_date_string_local( $action, $last_attempt_date ), |
|
37 | - ); |
|
33 | + if ( null !== $last_attempt_date ) { |
|
34 | + $data = array( |
|
35 | + 'last_attempt_gmt' => $this->get_scheduled_date_string( $action, $last_attempt_date ), |
|
36 | + 'last_attempt_local' => $this->get_scheduled_date_string_local( $action, $last_attempt_date ), |
|
37 | + ); |
|
38 | 38 | |
39 | - $wpdb->update( $wpdb->actionscheduler_actions, $data, array( 'action_id' => $action_id ), array( '%s', '%s' ), array( '%d' ) ); |
|
40 | - } |
|
39 | + $wpdb->update( $wpdb->actionscheduler_actions, $data, array( 'action_id' => $action_id ), array( '%s', '%s' ), array( '%d' ) ); |
|
40 | + } |
|
41 | 41 | |
42 | - return $action_id; |
|
43 | - } catch ( \Exception $e ) { |
|
44 | - throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
|
45 | - } |
|
46 | - } |
|
42 | + return $action_id; |
|
43 | + } catch ( \Exception $e ) { |
|
44 | + throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | } |
@@ -23,25 +23,25 @@ |
||
23 | 23 | * @return string The action ID |
24 | 24 | * @throws \RuntimeException When the action is not saved. |
25 | 25 | */ |
26 | - public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null, \DateTime $last_attempt_date = null ) { |
|
26 | + public function save_action(ActionScheduler_Action $action, \DateTime $scheduled_date = null, \DateTime $last_attempt_date = null) { |
|
27 | 27 | try { |
28 | 28 | /** @var \wpdb $wpdb */ |
29 | 29 | global $wpdb; |
30 | 30 | |
31 | - $action_id = parent::save_action( $action, $scheduled_date ); |
|
31 | + $action_id = parent::save_action($action, $scheduled_date); |
|
32 | 32 | |
33 | - if ( null !== $last_attempt_date ) { |
|
33 | + if (null !== $last_attempt_date) { |
|
34 | 34 | $data = array( |
35 | - 'last_attempt_gmt' => $this->get_scheduled_date_string( $action, $last_attempt_date ), |
|
36 | - 'last_attempt_local' => $this->get_scheduled_date_string_local( $action, $last_attempt_date ), |
|
35 | + 'last_attempt_gmt' => $this->get_scheduled_date_string($action, $last_attempt_date), |
|
36 | + 'last_attempt_local' => $this->get_scheduled_date_string_local($action, $last_attempt_date), |
|
37 | 37 | ); |
38 | 38 | |
39 | - $wpdb->update( $wpdb->actionscheduler_actions, $data, array( 'action_id' => $action_id ), array( '%s', '%s' ), array( '%d' ) ); |
|
39 | + $wpdb->update($wpdb->actionscheduler_actions, $data, array('action_id' => $action_id), array('%s', '%s'), array('%d')); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | return $action_id; |
43 | - } catch ( \Exception $e ) { |
|
44 | - throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
|
43 | + } catch (\Exception $e) { |
|
44 | + throw new \RuntimeException(sprintf(__('Error saving action: %s', 'action-scheduler'), $e->getMessage()), 0); |
|
45 | 45 | } |
46 | 46 | } |
47 | 47 | } |
@@ -8,35 +8,35 @@ discard block |
||
8 | 8 | * Creates a custom table for storing action logs |
9 | 9 | */ |
10 | 10 | class ActionScheduler_LoggerSchema extends ActionScheduler_Abstract_Schema { |
11 | - const LOG_TABLE = 'actionscheduler_logs'; |
|
11 | + const LOG_TABLE = 'actionscheduler_logs'; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var int Increment this value to trigger a schema update. |
|
15 | - */ |
|
16 | - protected $schema_version = 3; |
|
13 | + /** |
|
14 | + * @var int Increment this value to trigger a schema update. |
|
15 | + */ |
|
16 | + protected $schema_version = 3; |
|
17 | 17 | |
18 | - public function __construct() { |
|
19 | - $this->tables = array( |
|
20 | - self::LOG_TABLE, |
|
21 | - ); |
|
22 | - } |
|
18 | + public function __construct() { |
|
19 | + $this->tables = array( |
|
20 | + self::LOG_TABLE, |
|
21 | + ); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * Performs additional setup work required to support this schema. |
|
26 | - */ |
|
27 | - public function init() { |
|
28 | - add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_3_0' ), 10, 2 ); |
|
29 | - } |
|
24 | + /** |
|
25 | + * Performs additional setup work required to support this schema. |
|
26 | + */ |
|
27 | + public function init() { |
|
28 | + add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_3_0' ), 10, 2 ); |
|
29 | + } |
|
30 | 30 | |
31 | - protected function get_table_definition( $table ) { |
|
32 | - global $wpdb; |
|
33 | - $table_name = $wpdb->$table; |
|
34 | - $charset_collate = $wpdb->get_charset_collate(); |
|
35 | - switch ( $table ) { |
|
31 | + protected function get_table_definition( $table ) { |
|
32 | + global $wpdb; |
|
33 | + $table_name = $wpdb->$table; |
|
34 | + $charset_collate = $wpdb->get_charset_collate(); |
|
35 | + switch ( $table ) { |
|
36 | 36 | |
37 | - case self::LOG_TABLE: |
|
38 | - $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
|
39 | - return "CREATE TABLE $table_name ( |
|
37 | + case self::LOG_TABLE: |
|
38 | + $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
|
39 | + return "CREATE TABLE $table_name ( |
|
40 | 40 | log_id bigint(20) unsigned NOT NULL auto_increment, |
41 | 41 | action_id bigint(20) unsigned NOT NULL, |
42 | 42 | message text NOT NULL, |
@@ -47,43 +47,43 @@ discard block |
||
47 | 47 | KEY log_date_gmt (log_date_gmt) |
48 | 48 | ) $charset_collate"; |
49 | 49 | |
50 | - default: |
|
51 | - return ''; |
|
52 | - } |
|
53 | - } |
|
50 | + default: |
|
51 | + return ''; |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Update the logs table schema, allowing datetime fields to be NULL. |
|
57 | - * |
|
58 | - * This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL |
|
59 | - * configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created. |
|
60 | - * |
|
61 | - * Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however |
|
62 | - * that method relies on dbDelta() and this change is not possible when using that function. |
|
63 | - * |
|
64 | - * @param string $table Name of table being updated. |
|
65 | - * @param string $db_version The existing schema version of the table. |
|
66 | - */ |
|
67 | - public function update_schema_3_0( $table, $db_version ) { |
|
68 | - global $wpdb; |
|
55 | + /** |
|
56 | + * Update the logs table schema, allowing datetime fields to be NULL. |
|
57 | + * |
|
58 | + * This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL |
|
59 | + * configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created. |
|
60 | + * |
|
61 | + * Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however |
|
62 | + * that method relies on dbDelta() and this change is not possible when using that function. |
|
63 | + * |
|
64 | + * @param string $table Name of table being updated. |
|
65 | + * @param string $db_version The existing schema version of the table. |
|
66 | + */ |
|
67 | + public function update_schema_3_0( $table, $db_version ) { |
|
68 | + global $wpdb; |
|
69 | 69 | |
70 | - if ( 'actionscheduler_logs' !== $table || version_compare( $db_version, '3', '>=' ) ) { |
|
71 | - return; |
|
72 | - } |
|
70 | + if ( 'actionscheduler_logs' !== $table || version_compare( $db_version, '3', '>=' ) ) { |
|
71 | + return; |
|
72 | + } |
|
73 | 73 | |
74 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
75 | - $table_name = $wpdb->prefix . 'actionscheduler_logs'; |
|
76 | - $table_list = $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" ); |
|
77 | - $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
|
74 | + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
75 | + $table_name = $wpdb->prefix . 'actionscheduler_logs'; |
|
76 | + $table_list = $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" ); |
|
77 | + $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
|
78 | 78 | |
79 | - if ( ! empty( $table_list ) ) { |
|
80 | - $query = " |
|
79 | + if ( ! empty( $table_list ) ) { |
|
80 | + $query = " |
|
81 | 81 | ALTER TABLE {$table_name} |
82 | 82 | MODIFY COLUMN log_date_gmt datetime NULL default '{$default_date}', |
83 | 83 | MODIFY COLUMN log_date_local datetime NULL default '{$default_date}' |
84 | 84 | "; |
85 | - $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
86 | - } |
|
87 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
88 | - } |
|
85 | + $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
86 | + } |
|
87 | + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
88 | + } |
|
89 | 89 | } |
@@ -25,14 +25,14 @@ discard block |
||
25 | 25 | * Performs additional setup work required to support this schema. |
26 | 26 | */ |
27 | 27 | public function init() { |
28 | - add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_3_0' ), 10, 2 ); |
|
28 | + add_action('action_scheduler_before_schema_update', array($this, 'update_schema_3_0'), 10, 2); |
|
29 | 29 | } |
30 | 30 | |
31 | - protected function get_table_definition( $table ) { |
|
31 | + protected function get_table_definition($table) { |
|
32 | 32 | global $wpdb; |
33 | 33 | $table_name = $wpdb->$table; |
34 | 34 | $charset_collate = $wpdb->get_charset_collate(); |
35 | - switch ( $table ) { |
|
35 | + switch ($table) { |
|
36 | 36 | |
37 | 37 | case self::LOG_TABLE: |
38 | 38 | $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
@@ -64,25 +64,25 @@ discard block |
||
64 | 64 | * @param string $table Name of table being updated. |
65 | 65 | * @param string $db_version The existing schema version of the table. |
66 | 66 | */ |
67 | - public function update_schema_3_0( $table, $db_version ) { |
|
67 | + public function update_schema_3_0($table, $db_version) { |
|
68 | 68 | global $wpdb; |
69 | 69 | |
70 | - if ( 'actionscheduler_logs' !== $table || version_compare( $db_version, '3', '>=' ) ) { |
|
70 | + if ('actionscheduler_logs' !== $table || version_compare($db_version, '3', '>=')) { |
|
71 | 71 | return; |
72 | 72 | } |
73 | 73 | |
74 | 74 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
75 | - $table_name = $wpdb->prefix . 'actionscheduler_logs'; |
|
76 | - $table_list = $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" ); |
|
75 | + $table_name = $wpdb->prefix.'actionscheduler_logs'; |
|
76 | + $table_list = $wpdb->get_col("SHOW TABLES LIKE '{$table_name}'"); |
|
77 | 77 | $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
78 | 78 | |
79 | - if ( ! empty( $table_list ) ) { |
|
79 | + if ( ! empty($table_list)) { |
|
80 | 80 | $query = " |
81 | 81 | ALTER TABLE {$table_name} |
82 | 82 | MODIFY COLUMN log_date_gmt datetime NULL default '{$default_date}', |
83 | 83 | MODIFY COLUMN log_date_local datetime NULL default '{$default_date}' |
84 | 84 | "; |
85 | - $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
85 | + $wpdb->query($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
86 | 86 | } |
87 | 87 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
88 | 88 | } |
@@ -8,41 +8,41 @@ discard block |
||
8 | 8 | * Creates custom tables for storing scheduled actions |
9 | 9 | */ |
10 | 10 | class ActionScheduler_StoreSchema extends ActionScheduler_Abstract_Schema { |
11 | - const ACTIONS_TABLE = 'actionscheduler_actions'; |
|
12 | - const CLAIMS_TABLE = 'actionscheduler_claims'; |
|
13 | - const GROUPS_TABLE = 'actionscheduler_groups'; |
|
14 | - const DEFAULT_DATE = '0000-00-00 00:00:00'; |
|
11 | + const ACTIONS_TABLE = 'actionscheduler_actions'; |
|
12 | + const CLAIMS_TABLE = 'actionscheduler_claims'; |
|
13 | + const GROUPS_TABLE = 'actionscheduler_groups'; |
|
14 | + const DEFAULT_DATE = '0000-00-00 00:00:00'; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var int Increment this value to trigger a schema update. |
|
18 | - */ |
|
19 | - protected $schema_version = 6; |
|
16 | + /** |
|
17 | + * @var int Increment this value to trigger a schema update. |
|
18 | + */ |
|
19 | + protected $schema_version = 6; |
|
20 | 20 | |
21 | - public function __construct() { |
|
22 | - $this->tables = array( |
|
23 | - self::ACTIONS_TABLE, |
|
24 | - self::CLAIMS_TABLE, |
|
25 | - self::GROUPS_TABLE, |
|
26 | - ); |
|
27 | - } |
|
21 | + public function __construct() { |
|
22 | + $this->tables = array( |
|
23 | + self::ACTIONS_TABLE, |
|
24 | + self::CLAIMS_TABLE, |
|
25 | + self::GROUPS_TABLE, |
|
26 | + ); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Performs additional setup work required to support this schema. |
|
31 | - */ |
|
32 | - public function init() { |
|
33 | - add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_5_0' ), 10, 2 ); |
|
34 | - } |
|
29 | + /** |
|
30 | + * Performs additional setup work required to support this schema. |
|
31 | + */ |
|
32 | + public function init() { |
|
33 | + add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_5_0' ), 10, 2 ); |
|
34 | + } |
|
35 | 35 | |
36 | - protected function get_table_definition( $table ) { |
|
37 | - global $wpdb; |
|
38 | - $table_name = $wpdb->$table; |
|
39 | - $charset_collate = $wpdb->get_charset_collate(); |
|
40 | - $max_index_length = 191; // @see wp_get_db_schema() |
|
41 | - $default_date = self::DEFAULT_DATE; |
|
42 | - switch ( $table ) { |
|
36 | + protected function get_table_definition( $table ) { |
|
37 | + global $wpdb; |
|
38 | + $table_name = $wpdb->$table; |
|
39 | + $charset_collate = $wpdb->get_charset_collate(); |
|
40 | + $max_index_length = 191; // @see wp_get_db_schema() |
|
41 | + $default_date = self::DEFAULT_DATE; |
|
42 | + switch ( $table ) { |
|
43 | 43 | |
44 | - case self::ACTIONS_TABLE: |
|
45 | - return "CREATE TABLE {$table_name} ( |
|
44 | + case self::ACTIONS_TABLE: |
|
45 | + return "CREATE TABLE {$table_name} ( |
|
46 | 46 | action_id bigint(20) unsigned NOT NULL auto_increment, |
47 | 47 | hook varchar(191) NOT NULL, |
48 | 48 | status varchar(20) NOT NULL, |
@@ -66,61 +66,61 @@ discard block |
||
66 | 66 | KEY `claim_id_status_scheduled_date_gmt` (`claim_id`, `status`, `scheduled_date_gmt`) |
67 | 67 | ) $charset_collate"; |
68 | 68 | |
69 | - case self::CLAIMS_TABLE: |
|
70 | - return "CREATE TABLE {$table_name} ( |
|
69 | + case self::CLAIMS_TABLE: |
|
70 | + return "CREATE TABLE {$table_name} ( |
|
71 | 71 | claim_id bigint(20) unsigned NOT NULL auto_increment, |
72 | 72 | date_created_gmt datetime NULL default '${default_date}', |
73 | 73 | PRIMARY KEY (claim_id), |
74 | 74 | KEY date_created_gmt (date_created_gmt) |
75 | 75 | ) $charset_collate"; |
76 | 76 | |
77 | - case self::GROUPS_TABLE: |
|
78 | - return "CREATE TABLE {$table_name} ( |
|
77 | + case self::GROUPS_TABLE: |
|
78 | + return "CREATE TABLE {$table_name} ( |
|
79 | 79 | group_id bigint(20) unsigned NOT NULL auto_increment, |
80 | 80 | slug varchar(255) NOT NULL, |
81 | 81 | PRIMARY KEY (group_id), |
82 | 82 | KEY slug (slug($max_index_length)) |
83 | 83 | ) $charset_collate"; |
84 | 84 | |
85 | - default: |
|
86 | - return ''; |
|
87 | - } |
|
88 | - } |
|
85 | + default: |
|
86 | + return ''; |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Update the actions table schema, allowing datetime fields to be NULL. |
|
92 | - * |
|
93 | - * This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL |
|
94 | - * configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created. |
|
95 | - * |
|
96 | - * Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however |
|
97 | - * that method relies on dbDelta() and this change is not possible when using that function. |
|
98 | - * |
|
99 | - * @param string $table Name of table being updated. |
|
100 | - * @param string $db_version The existing schema version of the table. |
|
101 | - */ |
|
102 | - public function update_schema_5_0( $table, $db_version ) { |
|
103 | - global $wpdb; |
|
90 | + /** |
|
91 | + * Update the actions table schema, allowing datetime fields to be NULL. |
|
92 | + * |
|
93 | + * This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL |
|
94 | + * configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created. |
|
95 | + * |
|
96 | + * Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however |
|
97 | + * that method relies on dbDelta() and this change is not possible when using that function. |
|
98 | + * |
|
99 | + * @param string $table Name of table being updated. |
|
100 | + * @param string $db_version The existing schema version of the table. |
|
101 | + */ |
|
102 | + public function update_schema_5_0( $table, $db_version ) { |
|
103 | + global $wpdb; |
|
104 | 104 | |
105 | - if ( 'actionscheduler_actions' !== $table || version_compare( $db_version, '5', '>=' ) ) { |
|
106 | - return; |
|
107 | - } |
|
105 | + if ( 'actionscheduler_actions' !== $table || version_compare( $db_version, '5', '>=' ) ) { |
|
106 | + return; |
|
107 | + } |
|
108 | 108 | |
109 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
110 | - $table_name = $wpdb->prefix . 'actionscheduler_actions'; |
|
111 | - $table_list = $wpdb->get_col( "SHOW TABLES LIKE '${table_name}'" ); |
|
112 | - $default_date = self::DEFAULT_DATE; |
|
109 | + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
110 | + $table_name = $wpdb->prefix . 'actionscheduler_actions'; |
|
111 | + $table_list = $wpdb->get_col( "SHOW TABLES LIKE '${table_name}'" ); |
|
112 | + $default_date = self::DEFAULT_DATE; |
|
113 | 113 | |
114 | - if ( ! empty( $table_list ) ) { |
|
115 | - $query = " |
|
114 | + if ( ! empty( $table_list ) ) { |
|
115 | + $query = " |
|
116 | 116 | ALTER TABLE ${table_name} |
117 | 117 | MODIFY COLUMN scheduled_date_gmt datetime NULL default '${default_date}', |
118 | 118 | MODIFY COLUMN scheduled_date_local datetime NULL default '${default_date}', |
119 | 119 | MODIFY COLUMN last_attempt_gmt datetime NULL default '${default_date}', |
120 | 120 | MODIFY COLUMN last_attempt_local datetime NULL default '${default_date}' |
121 | 121 | "; |
122 | - $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
123 | - } |
|
124 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
125 | - } |
|
122 | + $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
123 | + } |
|
124 | + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
125 | + } |
|
126 | 126 | } |
@@ -30,16 +30,16 @@ discard block |
||
30 | 30 | * Performs additional setup work required to support this schema. |
31 | 31 | */ |
32 | 32 | public function init() { |
33 | - add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_5_0' ), 10, 2 ); |
|
33 | + add_action('action_scheduler_before_schema_update', array($this, 'update_schema_5_0'), 10, 2); |
|
34 | 34 | } |
35 | 35 | |
36 | - protected function get_table_definition( $table ) { |
|
36 | + protected function get_table_definition($table) { |
|
37 | 37 | global $wpdb; |
38 | 38 | $table_name = $wpdb->$table; |
39 | 39 | $charset_collate = $wpdb->get_charset_collate(); |
40 | 40 | $max_index_length = 191; // @see wp_get_db_schema() |
41 | 41 | $default_date = self::DEFAULT_DATE; |
42 | - switch ( $table ) { |
|
42 | + switch ($table) { |
|
43 | 43 | |
44 | 44 | case self::ACTIONS_TABLE: |
45 | 45 | return "CREATE TABLE {$table_name} ( |
@@ -99,19 +99,19 @@ discard block |
||
99 | 99 | * @param string $table Name of table being updated. |
100 | 100 | * @param string $db_version The existing schema version of the table. |
101 | 101 | */ |
102 | - public function update_schema_5_0( $table, $db_version ) { |
|
102 | + public function update_schema_5_0($table, $db_version) { |
|
103 | 103 | global $wpdb; |
104 | 104 | |
105 | - if ( 'actionscheduler_actions' !== $table || version_compare( $db_version, '5', '>=' ) ) { |
|
105 | + if ('actionscheduler_actions' !== $table || version_compare($db_version, '5', '>=')) { |
|
106 | 106 | return; |
107 | 107 | } |
108 | 108 | |
109 | 109 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
110 | - $table_name = $wpdb->prefix . 'actionscheduler_actions'; |
|
111 | - $table_list = $wpdb->get_col( "SHOW TABLES LIKE '${table_name}'" ); |
|
110 | + $table_name = $wpdb->prefix.'actionscheduler_actions'; |
|
111 | + $table_list = $wpdb->get_col("SHOW TABLES LIKE '${table_name}'"); |
|
112 | 112 | $default_date = self::DEFAULT_DATE; |
113 | 113 | |
114 | - if ( ! empty( $table_list ) ) { |
|
114 | + if ( ! empty($table_list)) { |
|
115 | 115 | $query = " |
116 | 116 | ALTER TABLE ${table_name} |
117 | 117 | MODIFY COLUMN scheduled_date_gmt datetime NULL default '${default_date}', |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | MODIFY COLUMN last_attempt_gmt datetime NULL default '${default_date}', |
120 | 120 | MODIFY COLUMN last_attempt_local datetime NULL default '${default_date}' |
121 | 121 | "; |
122 | - $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
122 | + $wpdb->query($query); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
123 | 123 | } |
124 | 124 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
125 | 125 | } |
@@ -5,105 +5,105 @@ |
||
5 | 5 | */ |
6 | 6 | class ActionScheduler_Compatibility { |
7 | 7 | |
8 | - /** |
|
9 | - * Converts a shorthand byte value to an integer byte value. |
|
10 | - * |
|
11 | - * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php |
|
12 | - * |
|
13 | - * @link https://secure.php.net/manual/en/function.ini-get.php |
|
14 | - * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes |
|
15 | - * |
|
16 | - * @param string $value A (PHP ini) byte value, either shorthand or ordinary. |
|
17 | - * @return int An integer byte value. |
|
18 | - */ |
|
19 | - public static function convert_hr_to_bytes( $value ) { |
|
20 | - if ( function_exists( 'wp_convert_hr_to_bytes' ) ) { |
|
21 | - return wp_convert_hr_to_bytes( $value ); |
|
22 | - } |
|
8 | + /** |
|
9 | + * Converts a shorthand byte value to an integer byte value. |
|
10 | + * |
|
11 | + * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php |
|
12 | + * |
|
13 | + * @link https://secure.php.net/manual/en/function.ini-get.php |
|
14 | + * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes |
|
15 | + * |
|
16 | + * @param string $value A (PHP ini) byte value, either shorthand or ordinary. |
|
17 | + * @return int An integer byte value. |
|
18 | + */ |
|
19 | + public static function convert_hr_to_bytes( $value ) { |
|
20 | + if ( function_exists( 'wp_convert_hr_to_bytes' ) ) { |
|
21 | + return wp_convert_hr_to_bytes( $value ); |
|
22 | + } |
|
23 | 23 | |
24 | - $value = strtolower( trim( $value ) ); |
|
25 | - $bytes = (int) $value; |
|
24 | + $value = strtolower( trim( $value ) ); |
|
25 | + $bytes = (int) $value; |
|
26 | 26 | |
27 | - if ( false !== strpos( $value, 'g' ) ) { |
|
28 | - $bytes *= GB_IN_BYTES; |
|
29 | - } elseif ( false !== strpos( $value, 'm' ) ) { |
|
30 | - $bytes *= MB_IN_BYTES; |
|
31 | - } elseif ( false !== strpos( $value, 'k' ) ) { |
|
32 | - $bytes *= KB_IN_BYTES; |
|
33 | - } |
|
27 | + if ( false !== strpos( $value, 'g' ) ) { |
|
28 | + $bytes *= GB_IN_BYTES; |
|
29 | + } elseif ( false !== strpos( $value, 'm' ) ) { |
|
30 | + $bytes *= MB_IN_BYTES; |
|
31 | + } elseif ( false !== strpos( $value, 'k' ) ) { |
|
32 | + $bytes *= KB_IN_BYTES; |
|
33 | + } |
|
34 | 34 | |
35 | - // Deal with large (float) values which run into the maximum integer size. |
|
36 | - return min( $bytes, PHP_INT_MAX ); |
|
37 | - } |
|
35 | + // Deal with large (float) values which run into the maximum integer size. |
|
36 | + return min( $bytes, PHP_INT_MAX ); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Attempts to raise the PHP memory limit for memory intensive processes. |
|
41 | - * |
|
42 | - * Only allows raising the existing limit and prevents lowering it. |
|
43 | - * |
|
44 | - * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0 |
|
45 | - * |
|
46 | - * @return bool|int|string The limit that was set or false on failure. |
|
47 | - */ |
|
48 | - public static function raise_memory_limit() { |
|
49 | - if ( function_exists( 'wp_raise_memory_limit' ) ) { |
|
50 | - return wp_raise_memory_limit( 'admin' ); |
|
51 | - } |
|
39 | + /** |
|
40 | + * Attempts to raise the PHP memory limit for memory intensive processes. |
|
41 | + * |
|
42 | + * Only allows raising the existing limit and prevents lowering it. |
|
43 | + * |
|
44 | + * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0 |
|
45 | + * |
|
46 | + * @return bool|int|string The limit that was set or false on failure. |
|
47 | + */ |
|
48 | + public static function raise_memory_limit() { |
|
49 | + if ( function_exists( 'wp_raise_memory_limit' ) ) { |
|
50 | + return wp_raise_memory_limit( 'admin' ); |
|
51 | + } |
|
52 | 52 | |
53 | - $current_limit = @ini_get( 'memory_limit' ); |
|
54 | - $current_limit_int = self::convert_hr_to_bytes( $current_limit ); |
|
53 | + $current_limit = @ini_get( 'memory_limit' ); |
|
54 | + $current_limit_int = self::convert_hr_to_bytes( $current_limit ); |
|
55 | 55 | |
56 | - if ( -1 === $current_limit_int ) { |
|
57 | - return false; |
|
58 | - } |
|
56 | + if ( -1 === $current_limit_int ) { |
|
57 | + return false; |
|
58 | + } |
|
59 | 59 | |
60 | - $wp_max_limit = WP_MAX_MEMORY_LIMIT; |
|
61 | - $wp_max_limit_int = self::convert_hr_to_bytes( $wp_max_limit ); |
|
62 | - $filtered_limit = apply_filters( 'admin_memory_limit', $wp_max_limit ); |
|
63 | - $filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit ); |
|
60 | + $wp_max_limit = WP_MAX_MEMORY_LIMIT; |
|
61 | + $wp_max_limit_int = self::convert_hr_to_bytes( $wp_max_limit ); |
|
62 | + $filtered_limit = apply_filters( 'admin_memory_limit', $wp_max_limit ); |
|
63 | + $filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit ); |
|
64 | 64 | |
65 | - if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { |
|
66 | - if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { |
|
67 | - return $filtered_limit; |
|
68 | - } else { |
|
69 | - return false; |
|
70 | - } |
|
71 | - } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { |
|
72 | - if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { |
|
73 | - return $wp_max_limit; |
|
74 | - } else { |
|
75 | - return false; |
|
76 | - } |
|
77 | - } |
|
78 | - return false; |
|
79 | - } |
|
65 | + if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { |
|
66 | + if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { |
|
67 | + return $filtered_limit; |
|
68 | + } else { |
|
69 | + return false; |
|
70 | + } |
|
71 | + } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { |
|
72 | + if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { |
|
73 | + return $wp_max_limit; |
|
74 | + } else { |
|
75 | + return false; |
|
76 | + } |
|
77 | + } |
|
78 | + return false; |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Attempts to raise the PHP timeout for time intensive processes. |
|
83 | - * |
|
84 | - * Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available. |
|
85 | - * |
|
86 | - * @param int $limit The time limit in seconds. |
|
87 | - */ |
|
88 | - public static function raise_time_limit( $limit = 0 ) { |
|
89 | - $limit = (int) $limit; |
|
90 | - $max_execution_time = (int) ini_get( 'max_execution_time' ); |
|
81 | + /** |
|
82 | + * Attempts to raise the PHP timeout for time intensive processes. |
|
83 | + * |
|
84 | + * Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available. |
|
85 | + * |
|
86 | + * @param int $limit The time limit in seconds. |
|
87 | + */ |
|
88 | + public static function raise_time_limit( $limit = 0 ) { |
|
89 | + $limit = (int) $limit; |
|
90 | + $max_execution_time = (int) ini_get( 'max_execution_time' ); |
|
91 | 91 | |
92 | - /* |
|
92 | + /* |
|
93 | 93 | * If the max execution time is already unlimited (zero), or if it exceeds or is equal to the proposed |
94 | 94 | * limit, there is no reason for us to make further changes (we never want to lower it). |
95 | 95 | */ |
96 | - if ( |
|
97 | - 0 === $max_execution_time |
|
98 | - || ( $max_execution_time >= $limit && $limit !== 0 ) |
|
99 | - ) { |
|
100 | - return; |
|
101 | - } |
|
96 | + if ( |
|
97 | + 0 === $max_execution_time |
|
98 | + || ( $max_execution_time >= $limit && $limit !== 0 ) |
|
99 | + ) { |
|
100 | + return; |
|
101 | + } |
|
102 | 102 | |
103 | - if ( function_exists( 'wc_set_time_limit' ) ) { |
|
104 | - wc_set_time_limit( $limit ); |
|
105 | - } elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
|
106 | - @set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
107 | - } |
|
108 | - } |
|
103 | + if ( function_exists( 'wc_set_time_limit' ) ) { |
|
104 | + wc_set_time_limit( $limit ); |
|
105 | + } elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
|
106 | + @set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
107 | + } |
|
108 | + } |
|
109 | 109 | } |
@@ -16,24 +16,24 @@ discard block |
||
16 | 16 | * @param string $value A (PHP ini) byte value, either shorthand or ordinary. |
17 | 17 | * @return int An integer byte value. |
18 | 18 | */ |
19 | - public static function convert_hr_to_bytes( $value ) { |
|
20 | - if ( function_exists( 'wp_convert_hr_to_bytes' ) ) { |
|
21 | - return wp_convert_hr_to_bytes( $value ); |
|
19 | + public static function convert_hr_to_bytes($value) { |
|
20 | + if (function_exists('wp_convert_hr_to_bytes')) { |
|
21 | + return wp_convert_hr_to_bytes($value); |
|
22 | 22 | } |
23 | 23 | |
24 | - $value = strtolower( trim( $value ) ); |
|
24 | + $value = strtolower(trim($value)); |
|
25 | 25 | $bytes = (int) $value; |
26 | 26 | |
27 | - if ( false !== strpos( $value, 'g' ) ) { |
|
27 | + if (false !== strpos($value, 'g')) { |
|
28 | 28 | $bytes *= GB_IN_BYTES; |
29 | - } elseif ( false !== strpos( $value, 'm' ) ) { |
|
29 | + } elseif (false !== strpos($value, 'm')) { |
|
30 | 30 | $bytes *= MB_IN_BYTES; |
31 | - } elseif ( false !== strpos( $value, 'k' ) ) { |
|
31 | + } elseif (false !== strpos($value, 'k')) { |
|
32 | 32 | $bytes *= KB_IN_BYTES; |
33 | 33 | } |
34 | 34 | |
35 | 35 | // Deal with large (float) values which run into the maximum integer size. |
36 | - return min( $bytes, PHP_INT_MAX ); |
|
36 | + return min($bytes, PHP_INT_MAX); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
@@ -46,30 +46,30 @@ discard block |
||
46 | 46 | * @return bool|int|string The limit that was set or false on failure. |
47 | 47 | */ |
48 | 48 | public static function raise_memory_limit() { |
49 | - if ( function_exists( 'wp_raise_memory_limit' ) ) { |
|
50 | - return wp_raise_memory_limit( 'admin' ); |
|
49 | + if (function_exists('wp_raise_memory_limit')) { |
|
50 | + return wp_raise_memory_limit('admin'); |
|
51 | 51 | } |
52 | 52 | |
53 | - $current_limit = @ini_get( 'memory_limit' ); |
|
54 | - $current_limit_int = self::convert_hr_to_bytes( $current_limit ); |
|
53 | + $current_limit = @ini_get('memory_limit'); |
|
54 | + $current_limit_int = self::convert_hr_to_bytes($current_limit); |
|
55 | 55 | |
56 | 56 | if ( -1 === $current_limit_int ) { |
57 | 57 | return false; |
58 | 58 | } |
59 | 59 | |
60 | 60 | $wp_max_limit = WP_MAX_MEMORY_LIMIT; |
61 | - $wp_max_limit_int = self::convert_hr_to_bytes( $wp_max_limit ); |
|
62 | - $filtered_limit = apply_filters( 'admin_memory_limit', $wp_max_limit ); |
|
63 | - $filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit ); |
|
61 | + $wp_max_limit_int = self::convert_hr_to_bytes($wp_max_limit); |
|
62 | + $filtered_limit = apply_filters('admin_memory_limit', $wp_max_limit); |
|
63 | + $filtered_limit_int = self::convert_hr_to_bytes($filtered_limit); |
|
64 | 64 | |
65 | - if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { |
|
66 | - if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { |
|
65 | + if ( -1 === $filtered_limit_int || ($filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int) ) { |
|
66 | + if (false !== @ini_set('memory_limit', $filtered_limit)) { |
|
67 | 67 | return $filtered_limit; |
68 | 68 | } else { |
69 | 69 | return false; |
70 | 70 | } |
71 | 71 | } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { |
72 | - if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { |
|
72 | + if (false !== @ini_set('memory_limit', $wp_max_limit)) { |
|
73 | 73 | return $wp_max_limit; |
74 | 74 | } else { |
75 | 75 | return false; |
@@ -85,9 +85,9 @@ discard block |
||
85 | 85 | * |
86 | 86 | * @param int $limit The time limit in seconds. |
87 | 87 | */ |
88 | - public static function raise_time_limit( $limit = 0 ) { |
|
88 | + public static function raise_time_limit($limit = 0) { |
|
89 | 89 | $limit = (int) $limit; |
90 | - $max_execution_time = (int) ini_get( 'max_execution_time' ); |
|
90 | + $max_execution_time = (int) ini_get('max_execution_time'); |
|
91 | 91 | |
92 | 92 | /* |
93 | 93 | * If the max execution time is already unlimited (zero), or if it exceeds or is equal to the proposed |
@@ -95,15 +95,15 @@ discard block |
||
95 | 95 | */ |
96 | 96 | if ( |
97 | 97 | 0 === $max_execution_time |
98 | - || ( $max_execution_time >= $limit && $limit !== 0 ) |
|
98 | + || ($max_execution_time >= $limit && $limit !== 0) |
|
99 | 99 | ) { |
100 | 100 | return; |
101 | 101 | } |
102 | 102 | |
103 | - if ( function_exists( 'wc_set_time_limit' ) ) { |
|
104 | - wc_set_time_limit( $limit ); |
|
105 | - } elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
|
106 | - @set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
103 | + if (function_exists('wc_set_time_limit')) { |
|
104 | + wc_set_time_limit($limit); |
|
105 | + } elseif (function_exists('set_time_limit') && false === strpos(ini_get('disable_functions'), 'set_time_limit') && ! ini_get('safe_mode')) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
|
106 | + @set_time_limit($limit); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
107 | 107 | } |
108 | 108 | } |
109 | 109 | } |
@@ -4,214 +4,214 @@ |
||
4 | 4 | * Class ActionScheduler_QueueRunner |
5 | 5 | */ |
6 | 6 | class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner { |
7 | - const WP_CRON_HOOK = 'action_scheduler_run_queue'; |
|
8 | - |
|
9 | - const WP_CRON_SCHEDULE = 'every_minute'; |
|
10 | - |
|
11 | - /** @var ActionScheduler_AsyncRequest_QueueRunner */ |
|
12 | - protected $async_request; |
|
13 | - |
|
14 | - /** @var ActionScheduler_QueueRunner */ |
|
15 | - private static $runner = null; |
|
16 | - |
|
17 | - /** |
|
18 | - * @return ActionScheduler_QueueRunner |
|
19 | - * @codeCoverageIgnore |
|
20 | - */ |
|
21 | - public static function instance() { |
|
22 | - if ( empty( self::$runner ) ) { |
|
23 | - $class = apply_filters( 'action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner' ); |
|
24 | - self::$runner = new $class(); |
|
25 | - } |
|
26 | - return self::$runner; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * ActionScheduler_QueueRunner constructor. |
|
31 | - * |
|
32 | - * @param ActionScheduler_Store $store |
|
33 | - * @param ActionScheduler_FatalErrorMonitor $monitor |
|
34 | - * @param ActionScheduler_QueueCleaner $cleaner |
|
35 | - */ |
|
36 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { |
|
37 | - parent::__construct( $store, $monitor, $cleaner ); |
|
38 | - |
|
39 | - if ( $async_request === null ) { |
|
40 | - $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store ); |
|
41 | - } |
|
42 | - |
|
43 | - $this->async_request = $async_request; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @codeCoverageIgnore |
|
48 | - */ |
|
49 | - public function init() { |
|
50 | - |
|
51 | - add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); |
|
52 | - |
|
53 | - // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param |
|
54 | - $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); |
|
55 | - if ( $next_timestamp ) { |
|
56 | - wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK ); |
|
57 | - } |
|
58 | - |
|
59 | - $cron_context = array( 'WP Cron' ); |
|
60 | - |
|
61 | - if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) { |
|
62 | - $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE ); |
|
63 | - wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context ); |
|
64 | - } |
|
65 | - |
|
66 | - add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) ); |
|
67 | - $this->hook_dispatch_async_request(); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Hook check for dispatching an async request. |
|
72 | - */ |
|
73 | - public function hook_dispatch_async_request() { |
|
74 | - add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Unhook check for dispatching an async request. |
|
79 | - */ |
|
80 | - public function unhook_dispatch_async_request() { |
|
81 | - remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Check if we should dispatch an async request to process actions. |
|
86 | - * |
|
87 | - * This method is attached to 'shutdown', so is called frequently. To avoid slowing down |
|
88 | - * the site, it mitigates the work performed in each request by: |
|
89 | - * 1. checking if it's in the admin context and then |
|
90 | - * 2. haven't run on the 'shutdown' hook within the lock time (60 seconds by default) |
|
91 | - * 3. haven't exceeded the number of allowed batches. |
|
92 | - * |
|
93 | - * The order of these checks is important, because they run from a check on a value: |
|
94 | - * 1. in memory - is_admin() maps to $GLOBALS or the WP_ADMIN constant |
|
95 | - * 2. in memory - transients use autoloaded options by default |
|
96 | - * 3. from a database query - has_maximum_concurrent_batches() run the query |
|
97 | - * $this->store->get_claim_count() to find the current number of claims in the DB. |
|
98 | - * |
|
99 | - * If all of these conditions are met, then we request an async runner check whether it |
|
100 | - * should dispatch a request to process pending actions. |
|
101 | - */ |
|
102 | - public function maybe_dispatch_async_request() { |
|
103 | - if ( is_admin() && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) ) { |
|
104 | - // Only start an async queue at most once every 60 seconds |
|
105 | - ActionScheduler::lock()->set( 'async-request-runner' ); |
|
106 | - $this->async_request->maybe_dispatch(); |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Process actions in the queue. Attached to self::WP_CRON_HOOK i.e. 'action_scheduler_run_queue' |
|
112 | - * |
|
113 | - * The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0 |
|
114 | - * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context |
|
115 | - * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK, |
|
116 | - * should set a context as the first parameter. For an example of this, refer to the code seen in |
|
117 | - * |
|
118 | - * @see ActionScheduler_AsyncRequest_QueueRunner::handle() |
|
119 | - * |
|
120 | - * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
121 | - * Generally, this should be capitalised and not localised as it's a proper noun. |
|
122 | - * @return int The number of actions processed. |
|
123 | - */ |
|
124 | - public function run( $context = 'WP Cron' ) { |
|
125 | - ActionScheduler_Compatibility::raise_memory_limit(); |
|
126 | - ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() ); |
|
127 | - do_action( 'action_scheduler_before_process_queue' ); |
|
128 | - $this->run_cleanup(); |
|
129 | - $processed_actions = 0; |
|
130 | - if ( false === $this->has_maximum_concurrent_batches() ) { |
|
131 | - $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); |
|
132 | - do { |
|
133 | - $processed_actions_in_batch = $this->do_batch( $batch_size, $context ); |
|
134 | - $processed_actions += $processed_actions_in_batch; |
|
135 | - } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory |
|
136 | - } |
|
137 | - |
|
138 | - do_action( 'action_scheduler_after_process_queue' ); |
|
139 | - return $processed_actions; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Process a batch of actions pending in the queue. |
|
144 | - * |
|
145 | - * Actions are processed by claiming a set of pending actions then processing each one until either the batch |
|
146 | - * size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded(). |
|
147 | - * |
|
148 | - * @param int $size The maximum number of actions to process in the batch. |
|
149 | - * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
150 | - * Generally, this should be capitalised and not localised as it's a proper noun. |
|
151 | - * @return int The number of actions processed. |
|
152 | - */ |
|
153 | - protected function do_batch( $size = 100, $context = '' ) { |
|
154 | - $claim = $this->store->stake_claim( $size ); |
|
155 | - $this->monitor->attach( $claim ); |
|
156 | - $processed_actions = 0; |
|
157 | - |
|
158 | - foreach ( $claim->get_actions() as $action_id ) { |
|
159 | - // bail if we lost the claim |
|
160 | - if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) { |
|
161 | - break; |
|
162 | - } |
|
163 | - $this->process_action( $action_id, $context ); |
|
164 | - $processed_actions++; |
|
165 | - |
|
166 | - if ( $this->batch_limits_exceeded( $processed_actions ) ) { |
|
167 | - break; |
|
168 | - } |
|
169 | - } |
|
170 | - $this->store->release_claim( $claim ); |
|
171 | - $this->monitor->detach(); |
|
172 | - $this->clear_caches(); |
|
173 | - return $processed_actions; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Flush the cache if possible (intended for use after a batch of actions has been processed). |
|
178 | - * |
|
179 | - * This is useful because running large batches can eat up memory and because invalid data can accrue in the |
|
180 | - * runtime cache, which may lead to unexpected results. |
|
181 | - */ |
|
182 | - protected function clear_caches() { |
|
183 | - /* |
|
7 | + const WP_CRON_HOOK = 'action_scheduler_run_queue'; |
|
8 | + |
|
9 | + const WP_CRON_SCHEDULE = 'every_minute'; |
|
10 | + |
|
11 | + /** @var ActionScheduler_AsyncRequest_QueueRunner */ |
|
12 | + protected $async_request; |
|
13 | + |
|
14 | + /** @var ActionScheduler_QueueRunner */ |
|
15 | + private static $runner = null; |
|
16 | + |
|
17 | + /** |
|
18 | + * @return ActionScheduler_QueueRunner |
|
19 | + * @codeCoverageIgnore |
|
20 | + */ |
|
21 | + public static function instance() { |
|
22 | + if ( empty( self::$runner ) ) { |
|
23 | + $class = apply_filters( 'action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner' ); |
|
24 | + self::$runner = new $class(); |
|
25 | + } |
|
26 | + return self::$runner; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * ActionScheduler_QueueRunner constructor. |
|
31 | + * |
|
32 | + * @param ActionScheduler_Store $store |
|
33 | + * @param ActionScheduler_FatalErrorMonitor $monitor |
|
34 | + * @param ActionScheduler_QueueCleaner $cleaner |
|
35 | + */ |
|
36 | + public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { |
|
37 | + parent::__construct( $store, $monitor, $cleaner ); |
|
38 | + |
|
39 | + if ( $async_request === null ) { |
|
40 | + $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store ); |
|
41 | + } |
|
42 | + |
|
43 | + $this->async_request = $async_request; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @codeCoverageIgnore |
|
48 | + */ |
|
49 | + public function init() { |
|
50 | + |
|
51 | + add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); |
|
52 | + |
|
53 | + // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param |
|
54 | + $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); |
|
55 | + if ( $next_timestamp ) { |
|
56 | + wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK ); |
|
57 | + } |
|
58 | + |
|
59 | + $cron_context = array( 'WP Cron' ); |
|
60 | + |
|
61 | + if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) { |
|
62 | + $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE ); |
|
63 | + wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context ); |
|
64 | + } |
|
65 | + |
|
66 | + add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) ); |
|
67 | + $this->hook_dispatch_async_request(); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Hook check for dispatching an async request. |
|
72 | + */ |
|
73 | + public function hook_dispatch_async_request() { |
|
74 | + add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Unhook check for dispatching an async request. |
|
79 | + */ |
|
80 | + public function unhook_dispatch_async_request() { |
|
81 | + remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Check if we should dispatch an async request to process actions. |
|
86 | + * |
|
87 | + * This method is attached to 'shutdown', so is called frequently. To avoid slowing down |
|
88 | + * the site, it mitigates the work performed in each request by: |
|
89 | + * 1. checking if it's in the admin context and then |
|
90 | + * 2. haven't run on the 'shutdown' hook within the lock time (60 seconds by default) |
|
91 | + * 3. haven't exceeded the number of allowed batches. |
|
92 | + * |
|
93 | + * The order of these checks is important, because they run from a check on a value: |
|
94 | + * 1. in memory - is_admin() maps to $GLOBALS or the WP_ADMIN constant |
|
95 | + * 2. in memory - transients use autoloaded options by default |
|
96 | + * 3. from a database query - has_maximum_concurrent_batches() run the query |
|
97 | + * $this->store->get_claim_count() to find the current number of claims in the DB. |
|
98 | + * |
|
99 | + * If all of these conditions are met, then we request an async runner check whether it |
|
100 | + * should dispatch a request to process pending actions. |
|
101 | + */ |
|
102 | + public function maybe_dispatch_async_request() { |
|
103 | + if ( is_admin() && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) ) { |
|
104 | + // Only start an async queue at most once every 60 seconds |
|
105 | + ActionScheduler::lock()->set( 'async-request-runner' ); |
|
106 | + $this->async_request->maybe_dispatch(); |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Process actions in the queue. Attached to self::WP_CRON_HOOK i.e. 'action_scheduler_run_queue' |
|
112 | + * |
|
113 | + * The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0 |
|
114 | + * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context |
|
115 | + * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK, |
|
116 | + * should set a context as the first parameter. For an example of this, refer to the code seen in |
|
117 | + * |
|
118 | + * @see ActionScheduler_AsyncRequest_QueueRunner::handle() |
|
119 | + * |
|
120 | + * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
121 | + * Generally, this should be capitalised and not localised as it's a proper noun. |
|
122 | + * @return int The number of actions processed. |
|
123 | + */ |
|
124 | + public function run( $context = 'WP Cron' ) { |
|
125 | + ActionScheduler_Compatibility::raise_memory_limit(); |
|
126 | + ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() ); |
|
127 | + do_action( 'action_scheduler_before_process_queue' ); |
|
128 | + $this->run_cleanup(); |
|
129 | + $processed_actions = 0; |
|
130 | + if ( false === $this->has_maximum_concurrent_batches() ) { |
|
131 | + $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); |
|
132 | + do { |
|
133 | + $processed_actions_in_batch = $this->do_batch( $batch_size, $context ); |
|
134 | + $processed_actions += $processed_actions_in_batch; |
|
135 | + } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory |
|
136 | + } |
|
137 | + |
|
138 | + do_action( 'action_scheduler_after_process_queue' ); |
|
139 | + return $processed_actions; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Process a batch of actions pending in the queue. |
|
144 | + * |
|
145 | + * Actions are processed by claiming a set of pending actions then processing each one until either the batch |
|
146 | + * size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded(). |
|
147 | + * |
|
148 | + * @param int $size The maximum number of actions to process in the batch. |
|
149 | + * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
|
150 | + * Generally, this should be capitalised and not localised as it's a proper noun. |
|
151 | + * @return int The number of actions processed. |
|
152 | + */ |
|
153 | + protected function do_batch( $size = 100, $context = '' ) { |
|
154 | + $claim = $this->store->stake_claim( $size ); |
|
155 | + $this->monitor->attach( $claim ); |
|
156 | + $processed_actions = 0; |
|
157 | + |
|
158 | + foreach ( $claim->get_actions() as $action_id ) { |
|
159 | + // bail if we lost the claim |
|
160 | + if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) { |
|
161 | + break; |
|
162 | + } |
|
163 | + $this->process_action( $action_id, $context ); |
|
164 | + $processed_actions++; |
|
165 | + |
|
166 | + if ( $this->batch_limits_exceeded( $processed_actions ) ) { |
|
167 | + break; |
|
168 | + } |
|
169 | + } |
|
170 | + $this->store->release_claim( $claim ); |
|
171 | + $this->monitor->detach(); |
|
172 | + $this->clear_caches(); |
|
173 | + return $processed_actions; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Flush the cache if possible (intended for use after a batch of actions has been processed). |
|
178 | + * |
|
179 | + * This is useful because running large batches can eat up memory and because invalid data can accrue in the |
|
180 | + * runtime cache, which may lead to unexpected results. |
|
181 | + */ |
|
182 | + protected function clear_caches() { |
|
183 | + /* |
|
184 | 184 | * Calling wp_cache_flush_runtime() lets us clear the runtime cache without invalidating the external object |
185 | 185 | * cache, so we will always prefer this when it is available (but it was only introduced in WordPress 6.0). |
186 | 186 | */ |
187 | - if ( function_exists( 'wp_cache_flush_runtime' ) ) { |
|
188 | - wp_cache_flush_runtime(); |
|
189 | - } elseif ( |
|
190 | - ! wp_using_ext_object_cache() |
|
191 | - /** |
|
192 | - * When an external object cache is in use, and when wp_cache_flush_runtime() is not available, then |
|
193 | - * normally the cache will not be flushed after processing a batch of actions (to avoid a performance |
|
194 | - * penalty for other processes). |
|
195 | - * |
|
196 | - * This filter makes it possible to override this behavior and always flush the cache, even if an external |
|
197 | - * object cache is in use. |
|
198 | - * |
|
199 | - * @since 1.0 |
|
200 | - * |
|
201 | - * @param bool $flush_cache If the cache should be flushed. |
|
202 | - */ |
|
203 | - || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) |
|
204 | - ) { |
|
205 | - wp_cache_flush(); |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - public function add_wp_cron_schedule( $schedules ) { |
|
210 | - $schedules['every_minute'] = array( |
|
211 | - 'interval' => 60, // in seconds |
|
212 | - 'display' => __( 'Every minute', 'action-scheduler' ), |
|
213 | - ); |
|
214 | - |
|
215 | - return $schedules; |
|
216 | - } |
|
187 | + if ( function_exists( 'wp_cache_flush_runtime' ) ) { |
|
188 | + wp_cache_flush_runtime(); |
|
189 | + } elseif ( |
|
190 | + ! wp_using_ext_object_cache() |
|
191 | + /** |
|
192 | + * When an external object cache is in use, and when wp_cache_flush_runtime() is not available, then |
|
193 | + * normally the cache will not be flushed after processing a batch of actions (to avoid a performance |
|
194 | + * penalty for other processes). |
|
195 | + * |
|
196 | + * This filter makes it possible to override this behavior and always flush the cache, even if an external |
|
197 | + * object cache is in use. |
|
198 | + * |
|
199 | + * @since 1.0 |
|
200 | + * |
|
201 | + * @param bool $flush_cache If the cache should be flushed. |
|
202 | + */ |
|
203 | + || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) |
|
204 | + ) { |
|
205 | + wp_cache_flush(); |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + public function add_wp_cron_schedule( $schedules ) { |
|
210 | + $schedules['every_minute'] = array( |
|
211 | + 'interval' => 60, // in seconds |
|
212 | + 'display' => __( 'Every minute', 'action-scheduler' ), |
|
213 | + ); |
|
214 | + |
|
215 | + return $schedules; |
|
216 | + } |
|
217 | 217 | } |
@@ -19,8 +19,8 @@ discard block |
||
19 | 19 | * @codeCoverageIgnore |
20 | 20 | */ |
21 | 21 | public static function instance() { |
22 | - if ( empty( self::$runner ) ) { |
|
23 | - $class = apply_filters( 'action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner' ); |
|
22 | + if (empty(self::$runner)) { |
|
23 | + $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner'); |
|
24 | 24 | self::$runner = new $class(); |
25 | 25 | } |
26 | 26 | return self::$runner; |
@@ -33,11 +33,11 @@ discard block |
||
33 | 33 | * @param ActionScheduler_FatalErrorMonitor $monitor |
34 | 34 | * @param ActionScheduler_QueueCleaner $cleaner |
35 | 35 | */ |
36 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { |
|
37 | - parent::__construct( $store, $monitor, $cleaner ); |
|
36 | + public function __construct(ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null) { |
|
37 | + parent::__construct($store, $monitor, $cleaner); |
|
38 | 38 | |
39 | - if ( $async_request === null ) { |
|
40 | - $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store ); |
|
39 | + if ($async_request === null) { |
|
40 | + $async_request = new ActionScheduler_AsyncRequest_QueueRunner($this->store); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | $this->async_request = $async_request; |
@@ -48,22 +48,22 @@ discard block |
||
48 | 48 | */ |
49 | 49 | public function init() { |
50 | 50 | |
51 | - add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); |
|
51 | + add_filter('cron_schedules', array(self::instance(), 'add_wp_cron_schedule')); |
|
52 | 52 | |
53 | 53 | // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param |
54 | - $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); |
|
55 | - if ( $next_timestamp ) { |
|
56 | - wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK ); |
|
54 | + $next_timestamp = wp_next_scheduled(self::WP_CRON_HOOK); |
|
55 | + if ($next_timestamp) { |
|
56 | + wp_unschedule_event($next_timestamp, self::WP_CRON_HOOK); |
|
57 | 57 | } |
58 | 58 | |
59 | - $cron_context = array( 'WP Cron' ); |
|
59 | + $cron_context = array('WP Cron'); |
|
60 | 60 | |
61 | - if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) { |
|
62 | - $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE ); |
|
63 | - wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context ); |
|
61 | + if ( ! wp_next_scheduled(self::WP_CRON_HOOK, $cron_context)) { |
|
62 | + $schedule = apply_filters('action_scheduler_run_schedule', self::WP_CRON_SCHEDULE); |
|
63 | + wp_schedule_event(time(), $schedule, self::WP_CRON_HOOK, $cron_context); |
|
64 | 64 | } |
65 | 65 | |
66 | - add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) ); |
|
66 | + add_action(self::WP_CRON_HOOK, array(self::instance(), 'run')); |
|
67 | 67 | $this->hook_dispatch_async_request(); |
68 | 68 | } |
69 | 69 | |
@@ -71,14 +71,14 @@ discard block |
||
71 | 71 | * Hook check for dispatching an async request. |
72 | 72 | */ |
73 | 73 | public function hook_dispatch_async_request() { |
74 | - add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
|
74 | + add_action('shutdown', array($this, 'maybe_dispatch_async_request')); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | 78 | * Unhook check for dispatching an async request. |
79 | 79 | */ |
80 | 80 | public function unhook_dispatch_async_request() { |
81 | - remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
|
81 | + remove_action('shutdown', array($this, 'maybe_dispatch_async_request')); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -100,9 +100,9 @@ discard block |
||
100 | 100 | * should dispatch a request to process pending actions. |
101 | 101 | */ |
102 | 102 | public function maybe_dispatch_async_request() { |
103 | - if ( is_admin() && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) ) { |
|
103 | + if (is_admin() && ! ActionScheduler::lock()->is_locked('async-request-runner')) { |
|
104 | 104 | // Only start an async queue at most once every 60 seconds |
105 | - ActionScheduler::lock()->set( 'async-request-runner' ); |
|
105 | + ActionScheduler::lock()->set('async-request-runner'); |
|
106 | 106 | $this->async_request->maybe_dispatch(); |
107 | 107 | } |
108 | 108 | } |
@@ -121,21 +121,21 @@ discard block |
||
121 | 121 | * Generally, this should be capitalised and not localised as it's a proper noun. |
122 | 122 | * @return int The number of actions processed. |
123 | 123 | */ |
124 | - public function run( $context = 'WP Cron' ) { |
|
124 | + public function run($context = 'WP Cron') { |
|
125 | 125 | ActionScheduler_Compatibility::raise_memory_limit(); |
126 | - ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() ); |
|
127 | - do_action( 'action_scheduler_before_process_queue' ); |
|
126 | + ActionScheduler_Compatibility::raise_time_limit($this->get_time_limit()); |
|
127 | + do_action('action_scheduler_before_process_queue'); |
|
128 | 128 | $this->run_cleanup(); |
129 | 129 | $processed_actions = 0; |
130 | - if ( false === $this->has_maximum_concurrent_batches() ) { |
|
131 | - $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); |
|
130 | + if (false === $this->has_maximum_concurrent_batches()) { |
|
131 | + $batch_size = apply_filters('action_scheduler_queue_runner_batch_size', 25); |
|
132 | 132 | do { |
133 | - $processed_actions_in_batch = $this->do_batch( $batch_size, $context ); |
|
133 | + $processed_actions_in_batch = $this->do_batch($batch_size, $context); |
|
134 | 134 | $processed_actions += $processed_actions_in_batch; |
135 | - } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory |
|
135 | + } while ($processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded($processed_actions)); // keep going until we run out of actions, time, or memory |
|
136 | 136 | } |
137 | 137 | |
138 | - do_action( 'action_scheduler_after_process_queue' ); |
|
138 | + do_action('action_scheduler_after_process_queue'); |
|
139 | 139 | return $processed_actions; |
140 | 140 | } |
141 | 141 | |
@@ -150,24 +150,24 @@ discard block |
||
150 | 150 | * Generally, this should be capitalised and not localised as it's a proper noun. |
151 | 151 | * @return int The number of actions processed. |
152 | 152 | */ |
153 | - protected function do_batch( $size = 100, $context = '' ) { |
|
154 | - $claim = $this->store->stake_claim( $size ); |
|
155 | - $this->monitor->attach( $claim ); |
|
153 | + protected function do_batch($size = 100, $context = '') { |
|
154 | + $claim = $this->store->stake_claim($size); |
|
155 | + $this->monitor->attach($claim); |
|
156 | 156 | $processed_actions = 0; |
157 | 157 | |
158 | - foreach ( $claim->get_actions() as $action_id ) { |
|
158 | + foreach ($claim->get_actions() as $action_id) { |
|
159 | 159 | // bail if we lost the claim |
160 | - if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) { |
|
160 | + if ( ! in_array($action_id, $this->store->find_actions_by_claim_id($claim->get_id()))) { |
|
161 | 161 | break; |
162 | 162 | } |
163 | - $this->process_action( $action_id, $context ); |
|
163 | + $this->process_action($action_id, $context); |
|
164 | 164 | $processed_actions++; |
165 | 165 | |
166 | - if ( $this->batch_limits_exceeded( $processed_actions ) ) { |
|
166 | + if ($this->batch_limits_exceeded($processed_actions)) { |
|
167 | 167 | break; |
168 | 168 | } |
169 | 169 | } |
170 | - $this->store->release_claim( $claim ); |
|
170 | + $this->store->release_claim($claim); |
|
171 | 171 | $this->monitor->detach(); |
172 | 172 | $this->clear_caches(); |
173 | 173 | return $processed_actions; |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | * Calling wp_cache_flush_runtime() lets us clear the runtime cache without invalidating the external object |
185 | 185 | * cache, so we will always prefer this when it is available (but it was only introduced in WordPress 6.0). |
186 | 186 | */ |
187 | - if ( function_exists( 'wp_cache_flush_runtime' ) ) { |
|
187 | + if (function_exists('wp_cache_flush_runtime')) { |
|
188 | 188 | wp_cache_flush_runtime(); |
189 | 189 | } elseif ( |
190 | 190 | ! wp_using_ext_object_cache() |
@@ -200,16 +200,16 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @param bool $flush_cache If the cache should be flushed. |
202 | 202 | */ |
203 | - || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) |
|
203 | + || apply_filters('action_scheduler_queue_runner_flush_cache', false) |
|
204 | 204 | ) { |
205 | 205 | wp_cache_flush(); |
206 | 206 | } |
207 | 207 | } |
208 | 208 | |
209 | - public function add_wp_cron_schedule( $schedules ) { |
|
209 | + public function add_wp_cron_schedule($schedules) { |
|
210 | 210 | $schedules['every_minute'] = array( |
211 | 211 | 'interval' => 60, // in seconds |
212 | - 'display' => __( 'Every minute', 'action-scheduler' ), |
|
212 | + 'display' => __('Every minute', 'action-scheduler'), |
|
213 | 213 | ); |
214 | 214 | |
215 | 215 | return $schedules; |
@@ -9,1055 +9,1055 @@ |
||
9 | 9 | */ |
10 | 10 | class ActionScheduler_DBStore extends ActionScheduler_Store { |
11 | 11 | |
12 | - /** |
|
13 | - * Used to share information about the before_date property of claims internally. |
|
14 | - * |
|
15 | - * This is used in preference to passing the same information as a method param |
|
16 | - * for backwards-compatibility reasons. |
|
17 | - * |
|
18 | - * @var DateTime|null |
|
19 | - */ |
|
20 | - private $claim_before_date = null; |
|
21 | - |
|
22 | - /** @var int */ |
|
23 | - protected static $max_args_length = 8000; |
|
24 | - |
|
25 | - /** @var int */ |
|
26 | - protected static $max_index_length = 191; |
|
27 | - |
|
28 | - /** |
|
29 | - * Initialize the data store |
|
30 | - * |
|
31 | - * @codeCoverageIgnore |
|
32 | - */ |
|
33 | - public function init() { |
|
34 | - $table_maker = new ActionScheduler_StoreSchema(); |
|
35 | - $table_maker->init(); |
|
36 | - $table_maker->register_tables(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Save an action, checks if this is a unique action before actually saving. |
|
41 | - * |
|
42 | - * @param ActionScheduler_Action $action Action object. |
|
43 | - * @param \DateTime $scheduled_date Optional schedule date. Default null. |
|
44 | - * |
|
45 | - * @return int Action ID. |
|
46 | - * @throws RuntimeException Throws exception when saving the action fails. |
|
47 | - */ |
|
48 | - public function save_unique_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { |
|
49 | - return $this->save_action_to_db( $action, $scheduled_date, true ); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Save an action. Can save duplicate action as well, prefer using `save_unique_action` instead. |
|
54 | - * |
|
55 | - * @param ActionScheduler_Action $action Action object. |
|
56 | - * @param \DateTime $scheduled_date Optional schedule date. Default null. |
|
57 | - * |
|
58 | - * @return int Action ID. |
|
59 | - * @throws RuntimeException Throws exception when saving the action fails. |
|
60 | - */ |
|
61 | - public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { |
|
62 | - return $this->save_action_to_db( $action, $scheduled_date, false ); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Save an action. |
|
67 | - * |
|
68 | - * @param ActionScheduler_Action $action Action object. |
|
69 | - * @param ?DateTime $date Optional schedule date. Default null. |
|
70 | - * @param bool $unique Whether the action should be unique. |
|
71 | - * |
|
72 | - * @return int Action ID. |
|
73 | - * @throws RuntimeException Throws exception when saving the action fails. |
|
74 | - */ |
|
75 | - private function save_action_to_db( ActionScheduler_Action $action, DateTime $date = null, $unique = false ) { |
|
76 | - global $wpdb; |
|
77 | - |
|
78 | - try { |
|
79 | - $this->validate_action( $action ); |
|
80 | - |
|
81 | - $data = array( |
|
82 | - 'hook' => $action->get_hook(), |
|
83 | - 'status' => ( $action->is_finished() ? self::STATUS_COMPLETE : self::STATUS_PENDING ), |
|
84 | - 'scheduled_date_gmt' => $this->get_scheduled_date_string( $action, $date ), |
|
85 | - 'scheduled_date_local' => $this->get_scheduled_date_string_local( $action, $date ), |
|
86 | - 'schedule' => serialize( $action->get_schedule() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
87 | - 'group_id' => $this->get_group_id( $action->get_group() ), |
|
88 | - ); |
|
89 | - |
|
90 | - $args = wp_json_encode( $action->get_args() ); |
|
91 | - if ( strlen( $args ) <= static::$max_index_length ) { |
|
92 | - $data['args'] = $args; |
|
93 | - } else { |
|
94 | - $data['args'] = $this->hash_args( $args ); |
|
95 | - $data['extended_args'] = $args; |
|
96 | - } |
|
97 | - |
|
98 | - $insert_sql = $this->build_insert_sql( $data, $unique ); |
|
99 | - |
|
100 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $insert_sql should be already prepared. |
|
101 | - $wpdb->query( $insert_sql ); |
|
102 | - $action_id = $wpdb->insert_id; |
|
103 | - |
|
104 | - if ( is_wp_error( $action_id ) ) { |
|
105 | - throw new \RuntimeException( $action_id->get_error_message() ); |
|
106 | - } elseif ( empty( $action_id ) ) { |
|
107 | - if ( $unique ) { |
|
108 | - return 0; |
|
109 | - } |
|
110 | - throw new \RuntimeException( $wpdb->last_error ? $wpdb->last_error : __( 'Database error.', 'action-scheduler' ) ); |
|
111 | - } |
|
112 | - |
|
113 | - do_action( 'action_scheduler_stored_action', $action_id ); |
|
114 | - |
|
115 | - return $action_id; |
|
116 | - } catch ( \Exception $e ) { |
|
117 | - /* translators: %s: error message */ |
|
118 | - throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Helper function to build insert query. |
|
124 | - * |
|
125 | - * @param array $data Row data for action. |
|
126 | - * @param bool $unique Whether the action should be unique. |
|
127 | - * |
|
128 | - * @return string Insert query. |
|
129 | - */ |
|
130 | - private function build_insert_sql( array $data, $unique ) { |
|
131 | - global $wpdb; |
|
132 | - $columns = array_keys( $data ); |
|
133 | - $values = array_values( $data ); |
|
134 | - $placeholders = array_map( array( $this, 'get_placeholder_for_column' ), $columns ); |
|
135 | - |
|
136 | - $table_name = ! empty( $wpdb->actionscheduler_actions ) ? $wpdb->actionscheduler_actions : $wpdb->prefix . 'actionscheduler_actions'; |
|
137 | - |
|
138 | - $column_sql = '`' . implode( '`, `', $columns ) . '`'; |
|
139 | - $placeholder_sql = implode( ', ', $placeholders ); |
|
140 | - $where_clause = $this->build_where_clause_for_insert( $data, $table_name, $unique ); |
|
141 | - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $column_sql and $where_clause are already prepared. $placeholder_sql is hardcoded. |
|
142 | - $insert_query = $wpdb->prepare( |
|
143 | - " |
|
12 | + /** |
|
13 | + * Used to share information about the before_date property of claims internally. |
|
14 | + * |
|
15 | + * This is used in preference to passing the same information as a method param |
|
16 | + * for backwards-compatibility reasons. |
|
17 | + * |
|
18 | + * @var DateTime|null |
|
19 | + */ |
|
20 | + private $claim_before_date = null; |
|
21 | + |
|
22 | + /** @var int */ |
|
23 | + protected static $max_args_length = 8000; |
|
24 | + |
|
25 | + /** @var int */ |
|
26 | + protected static $max_index_length = 191; |
|
27 | + |
|
28 | + /** |
|
29 | + * Initialize the data store |
|
30 | + * |
|
31 | + * @codeCoverageIgnore |
|
32 | + */ |
|
33 | + public function init() { |
|
34 | + $table_maker = new ActionScheduler_StoreSchema(); |
|
35 | + $table_maker->init(); |
|
36 | + $table_maker->register_tables(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Save an action, checks if this is a unique action before actually saving. |
|
41 | + * |
|
42 | + * @param ActionScheduler_Action $action Action object. |
|
43 | + * @param \DateTime $scheduled_date Optional schedule date. Default null. |
|
44 | + * |
|
45 | + * @return int Action ID. |
|
46 | + * @throws RuntimeException Throws exception when saving the action fails. |
|
47 | + */ |
|
48 | + public function save_unique_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { |
|
49 | + return $this->save_action_to_db( $action, $scheduled_date, true ); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Save an action. Can save duplicate action as well, prefer using `save_unique_action` instead. |
|
54 | + * |
|
55 | + * @param ActionScheduler_Action $action Action object. |
|
56 | + * @param \DateTime $scheduled_date Optional schedule date. Default null. |
|
57 | + * |
|
58 | + * @return int Action ID. |
|
59 | + * @throws RuntimeException Throws exception when saving the action fails. |
|
60 | + */ |
|
61 | + public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { |
|
62 | + return $this->save_action_to_db( $action, $scheduled_date, false ); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Save an action. |
|
67 | + * |
|
68 | + * @param ActionScheduler_Action $action Action object. |
|
69 | + * @param ?DateTime $date Optional schedule date. Default null. |
|
70 | + * @param bool $unique Whether the action should be unique. |
|
71 | + * |
|
72 | + * @return int Action ID. |
|
73 | + * @throws RuntimeException Throws exception when saving the action fails. |
|
74 | + */ |
|
75 | + private function save_action_to_db( ActionScheduler_Action $action, DateTime $date = null, $unique = false ) { |
|
76 | + global $wpdb; |
|
77 | + |
|
78 | + try { |
|
79 | + $this->validate_action( $action ); |
|
80 | + |
|
81 | + $data = array( |
|
82 | + 'hook' => $action->get_hook(), |
|
83 | + 'status' => ( $action->is_finished() ? self::STATUS_COMPLETE : self::STATUS_PENDING ), |
|
84 | + 'scheduled_date_gmt' => $this->get_scheduled_date_string( $action, $date ), |
|
85 | + 'scheduled_date_local' => $this->get_scheduled_date_string_local( $action, $date ), |
|
86 | + 'schedule' => serialize( $action->get_schedule() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
87 | + 'group_id' => $this->get_group_id( $action->get_group() ), |
|
88 | + ); |
|
89 | + |
|
90 | + $args = wp_json_encode( $action->get_args() ); |
|
91 | + if ( strlen( $args ) <= static::$max_index_length ) { |
|
92 | + $data['args'] = $args; |
|
93 | + } else { |
|
94 | + $data['args'] = $this->hash_args( $args ); |
|
95 | + $data['extended_args'] = $args; |
|
96 | + } |
|
97 | + |
|
98 | + $insert_sql = $this->build_insert_sql( $data, $unique ); |
|
99 | + |
|
100 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $insert_sql should be already prepared. |
|
101 | + $wpdb->query( $insert_sql ); |
|
102 | + $action_id = $wpdb->insert_id; |
|
103 | + |
|
104 | + if ( is_wp_error( $action_id ) ) { |
|
105 | + throw new \RuntimeException( $action_id->get_error_message() ); |
|
106 | + } elseif ( empty( $action_id ) ) { |
|
107 | + if ( $unique ) { |
|
108 | + return 0; |
|
109 | + } |
|
110 | + throw new \RuntimeException( $wpdb->last_error ? $wpdb->last_error : __( 'Database error.', 'action-scheduler' ) ); |
|
111 | + } |
|
112 | + |
|
113 | + do_action( 'action_scheduler_stored_action', $action_id ); |
|
114 | + |
|
115 | + return $action_id; |
|
116 | + } catch ( \Exception $e ) { |
|
117 | + /* translators: %s: error message */ |
|
118 | + throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Helper function to build insert query. |
|
124 | + * |
|
125 | + * @param array $data Row data for action. |
|
126 | + * @param bool $unique Whether the action should be unique. |
|
127 | + * |
|
128 | + * @return string Insert query. |
|
129 | + */ |
|
130 | + private function build_insert_sql( array $data, $unique ) { |
|
131 | + global $wpdb; |
|
132 | + $columns = array_keys( $data ); |
|
133 | + $values = array_values( $data ); |
|
134 | + $placeholders = array_map( array( $this, 'get_placeholder_for_column' ), $columns ); |
|
135 | + |
|
136 | + $table_name = ! empty( $wpdb->actionscheduler_actions ) ? $wpdb->actionscheduler_actions : $wpdb->prefix . 'actionscheduler_actions'; |
|
137 | + |
|
138 | + $column_sql = '`' . implode( '`, `', $columns ) . '`'; |
|
139 | + $placeholder_sql = implode( ', ', $placeholders ); |
|
140 | + $where_clause = $this->build_where_clause_for_insert( $data, $table_name, $unique ); |
|
141 | + // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $column_sql and $where_clause are already prepared. $placeholder_sql is hardcoded. |
|
142 | + $insert_query = $wpdb->prepare( |
|
143 | + " |
|
144 | 144 | INSERT INTO $table_name ( $column_sql ) |
145 | 145 | SELECT $placeholder_sql FROM DUAL |
146 | 146 | WHERE ( $where_clause ) IS NULL", |
147 | - $values |
|
148 | - ); |
|
149 | - // phpcs:enable |
|
150 | - |
|
151 | - return $insert_query; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Helper method to build where clause for action insert statement. |
|
156 | - * |
|
157 | - * @param array $data Row data for action. |
|
158 | - * @param string $table_name Action table name. |
|
159 | - * @param bool $unique Where action should be unique. |
|
160 | - * |
|
161 | - * @return string Where clause to be used with insert. |
|
162 | - */ |
|
163 | - private function build_where_clause_for_insert( $data, $table_name, $unique ) { |
|
164 | - global $wpdb; |
|
165 | - |
|
166 | - if ( ! $unique ) { |
|
167 | - return 'SELECT NULL FROM DUAL'; |
|
168 | - } |
|
169 | - |
|
170 | - $pending_statuses = array( |
|
171 | - ActionScheduler_Store::STATUS_PENDING, |
|
172 | - ActionScheduler_Store::STATUS_RUNNING, |
|
173 | - ); |
|
174 | - $pending_status_placeholders = implode( ', ', array_fill( 0, count( $pending_statuses ), '%s' ) ); |
|
175 | - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $pending_status_placeholders is hardcoded. |
|
176 | - $where_clause = $wpdb->prepare( |
|
177 | - " |
|
147 | + $values |
|
148 | + ); |
|
149 | + // phpcs:enable |
|
150 | + |
|
151 | + return $insert_query; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Helper method to build where clause for action insert statement. |
|
156 | + * |
|
157 | + * @param array $data Row data for action. |
|
158 | + * @param string $table_name Action table name. |
|
159 | + * @param bool $unique Where action should be unique. |
|
160 | + * |
|
161 | + * @return string Where clause to be used with insert. |
|
162 | + */ |
|
163 | + private function build_where_clause_for_insert( $data, $table_name, $unique ) { |
|
164 | + global $wpdb; |
|
165 | + |
|
166 | + if ( ! $unique ) { |
|
167 | + return 'SELECT NULL FROM DUAL'; |
|
168 | + } |
|
169 | + |
|
170 | + $pending_statuses = array( |
|
171 | + ActionScheduler_Store::STATUS_PENDING, |
|
172 | + ActionScheduler_Store::STATUS_RUNNING, |
|
173 | + ); |
|
174 | + $pending_status_placeholders = implode( ', ', array_fill( 0, count( $pending_statuses ), '%s' ) ); |
|
175 | + // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $pending_status_placeholders is hardcoded. |
|
176 | + $where_clause = $wpdb->prepare( |
|
177 | + " |
|
178 | 178 | SELECT action_id FROM $table_name |
179 | 179 | WHERE status IN ( $pending_status_placeholders ) |
180 | 180 | AND hook = %s |
181 | 181 | AND `group_id` = %d |
182 | 182 | ", |
183 | - array_merge( |
|
184 | - $pending_statuses, |
|
185 | - array( |
|
186 | - $data['hook'], |
|
187 | - $data['group_id'], |
|
188 | - ) |
|
189 | - ) |
|
190 | - ); |
|
191 | - // phpcs:enable |
|
192 | - |
|
193 | - return "$where_clause" . ' LIMIT 1'; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Helper method to get $wpdb->prepare placeholder for a given column name. |
|
198 | - * |
|
199 | - * @param string $column_name Name of column in actions table. |
|
200 | - * |
|
201 | - * @return string Placeholder to use for given column. |
|
202 | - */ |
|
203 | - private function get_placeholder_for_column( $column_name ) { |
|
204 | - $string_columns = array( |
|
205 | - 'hook', |
|
206 | - 'status', |
|
207 | - 'scheduled_date_gmt', |
|
208 | - 'scheduled_date_local', |
|
209 | - 'args', |
|
210 | - 'schedule', |
|
211 | - 'last_attempt_gmt', |
|
212 | - 'last_attempt_local', |
|
213 | - 'extended_args', |
|
214 | - ); |
|
215 | - |
|
216 | - return in_array( $column_name, $string_columns ) ? '%s' : '%d'; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Generate a hash from json_encoded $args using MD5 as this isn't for security. |
|
221 | - * |
|
222 | - * @param string $args JSON encoded action args. |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - protected function hash_args( $args ) { |
|
226 | - return md5( $args ); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Get action args query param value from action args. |
|
231 | - * |
|
232 | - * @param array $args Action args. |
|
233 | - * @return string |
|
234 | - */ |
|
235 | - protected function get_args_for_query( $args ) { |
|
236 | - $encoded = wp_json_encode( $args ); |
|
237 | - if ( strlen( $encoded ) <= static::$max_index_length ) { |
|
238 | - return $encoded; |
|
239 | - } |
|
240 | - return $this->hash_args( $encoded ); |
|
241 | - } |
|
242 | - /** |
|
243 | - * Get a group's ID based on its name/slug. |
|
244 | - * |
|
245 | - * @param string $slug The string name of a group. |
|
246 | - * @param bool $create_if_not_exists Whether to create the group if it does not already exist. Default, true - create the group. |
|
247 | - * |
|
248 | - * @return int The group's ID, if it exists or is created, or 0 if it does not exist and is not created. |
|
249 | - */ |
|
250 | - protected function get_group_id( $slug, $create_if_not_exists = true ) { |
|
251 | - if ( empty( $slug ) ) { |
|
252 | - return 0; |
|
253 | - } |
|
254 | - /** @var \wpdb $wpdb */ |
|
255 | - global $wpdb; |
|
256 | - $group_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$wpdb->actionscheduler_groups} WHERE slug=%s", $slug ) ); |
|
257 | - if ( empty( $group_id ) && $create_if_not_exists ) { |
|
258 | - $group_id = $this->create_group( $slug ); |
|
259 | - } |
|
260 | - |
|
261 | - return $group_id; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Create an action group. |
|
266 | - * |
|
267 | - * @param string $slug Group slug. |
|
268 | - * |
|
269 | - * @return int Group ID. |
|
270 | - */ |
|
271 | - protected function create_group( $slug ) { |
|
272 | - /** @var \wpdb $wpdb */ |
|
273 | - global $wpdb; |
|
274 | - $wpdb->insert( $wpdb->actionscheduler_groups, array( 'slug' => $slug ) ); |
|
275 | - |
|
276 | - return (int) $wpdb->insert_id; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Retrieve an action. |
|
281 | - * |
|
282 | - * @param int $action_id Action ID. |
|
283 | - * |
|
284 | - * @return ActionScheduler_Action |
|
285 | - */ |
|
286 | - public function fetch_action( $action_id ) { |
|
287 | - /** @var \wpdb $wpdb */ |
|
288 | - global $wpdb; |
|
289 | - $data = $wpdb->get_row( |
|
290 | - $wpdb->prepare( |
|
291 | - "SELECT a.*, g.slug AS `group` FROM {$wpdb->actionscheduler_actions} a LEFT JOIN {$wpdb->actionscheduler_groups} g ON a.group_id=g.group_id WHERE a.action_id=%d", |
|
292 | - $action_id |
|
293 | - ) |
|
294 | - ); |
|
295 | - |
|
296 | - if ( empty( $data ) ) { |
|
297 | - return $this->get_null_action(); |
|
298 | - } |
|
299 | - |
|
300 | - if ( ! empty( $data->extended_args ) ) { |
|
301 | - $data->args = $data->extended_args; |
|
302 | - unset( $data->extended_args ); |
|
303 | - } |
|
304 | - |
|
305 | - // Convert NULL dates to zero dates. |
|
306 | - $date_fields = array( |
|
307 | - 'scheduled_date_gmt', |
|
308 | - 'scheduled_date_local', |
|
309 | - 'last_attempt_gmt', |
|
310 | - 'last_attempt_gmt', |
|
311 | - ); |
|
312 | - foreach ( $date_fields as $date_field ) { |
|
313 | - if ( $data->$date_field === null ) { |
|
314 | - $data->$date_field = ActionScheduler_StoreSchema::DEFAULT_DATE; |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - try { |
|
319 | - $action = $this->make_action_from_db_record( $data ); |
|
320 | - } catch ( ActionScheduler_InvalidActionException $exception ) { |
|
321 | - do_action( 'action_scheduler_failed_fetch_action', $action_id, $exception ); |
|
322 | - return $this->get_null_action(); |
|
323 | - } |
|
324 | - |
|
325 | - return $action; |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Create a null action. |
|
330 | - * |
|
331 | - * @return ActionScheduler_NullAction |
|
332 | - */ |
|
333 | - protected function get_null_action() { |
|
334 | - return new ActionScheduler_NullAction(); |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Create an action from a database record. |
|
339 | - * |
|
340 | - * @param object $data Action database record. |
|
341 | - * |
|
342 | - * @return ActionScheduler_Action|ActionScheduler_CanceledAction|ActionScheduler_FinishedAction |
|
343 | - */ |
|
344 | - protected function make_action_from_db_record( $data ) { |
|
345 | - |
|
346 | - $hook = $data->hook; |
|
347 | - $args = json_decode( $data->args, true ); |
|
348 | - $schedule = unserialize( $data->schedule ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
349 | - |
|
350 | - $this->validate_args( $args, $data->action_id ); |
|
351 | - $this->validate_schedule( $schedule, $data->action_id ); |
|
352 | - |
|
353 | - if ( empty( $schedule ) ) { |
|
354 | - $schedule = new ActionScheduler_NullSchedule(); |
|
355 | - } |
|
356 | - $group = $data->group ? $data->group : ''; |
|
357 | - |
|
358 | - return ActionScheduler::factory()->get_stored_action( $data->status, $data->hook, $args, $schedule, $group ); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Returns the SQL statement to query (or count) actions. |
|
363 | - * |
|
364 | - * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status. |
|
365 | - * |
|
366 | - * @param array $query Filtering options. |
|
367 | - * @param string $select_or_count Whether the SQL should select and return the IDs or just the row count. |
|
368 | - * |
|
369 | - * @return string SQL statement already properly escaped. |
|
370 | - * @throws InvalidArgumentException If the query is invalid. |
|
371 | - */ |
|
372 | - protected function get_query_actions_sql( array $query, $select_or_count = 'select' ) { |
|
373 | - |
|
374 | - if ( ! in_array( $select_or_count, array( 'select', 'count' ), true ) ) { |
|
375 | - throw new InvalidArgumentException( __( 'Invalid value for select or count parameter. Cannot query actions.', 'action-scheduler' ) ); |
|
376 | - } |
|
377 | - |
|
378 | - $query = wp_parse_args( |
|
379 | - $query, |
|
380 | - array( |
|
381 | - 'hook' => '', |
|
382 | - 'args' => null, |
|
383 | - 'partial_args_matching' => 'off', // can be 'like' or 'json' |
|
384 | - 'date' => null, |
|
385 | - 'date_compare' => '<=', |
|
386 | - 'modified' => null, |
|
387 | - 'modified_compare' => '<=', |
|
388 | - 'group' => '', |
|
389 | - 'status' => '', |
|
390 | - 'claimed' => null, |
|
391 | - 'per_page' => 5, |
|
392 | - 'offset' => 0, |
|
393 | - 'orderby' => 'date', |
|
394 | - 'order' => 'ASC', |
|
395 | - ) |
|
396 | - ); |
|
397 | - |
|
398 | - /** @var \wpdb $wpdb */ |
|
399 | - global $wpdb; |
|
400 | - |
|
401 | - $db_server_info = is_callable( array( $wpdb, 'db_server_info' ) ) ? $wpdb->db_server_info() : $wpdb->db_version(); |
|
402 | - if ( false !== strpos( $db_server_info, 'MariaDB' ) ) { |
|
403 | - $supports_json = version_compare( |
|
404 | - PHP_VERSION_ID >= 80016 ? $wpdb->db_version() : preg_replace( '/[^0-9.].*/', '', str_replace( '5.5.5-', '', $db_server_info ) ), |
|
405 | - '10.2', |
|
406 | - '>=' |
|
407 | - ); |
|
408 | - } else { |
|
409 | - $supports_json = version_compare( $wpdb->db_version(), '5.7', '>=' ); |
|
410 | - } |
|
411 | - |
|
412 | - $sql = ( 'count' === $select_or_count ) ? 'SELECT count(a.action_id)' : 'SELECT a.action_id'; |
|
413 | - $sql .= " FROM {$wpdb->actionscheduler_actions} a"; |
|
414 | - $sql_params = array(); |
|
415 | - |
|
416 | - if ( ! empty( $query['group'] ) || 'group' === $query['orderby'] ) { |
|
417 | - $sql .= " LEFT JOIN {$wpdb->actionscheduler_groups} g ON g.group_id=a.group_id"; |
|
418 | - } |
|
419 | - |
|
420 | - $sql .= ' WHERE 1=1'; |
|
421 | - |
|
422 | - if ( ! empty( $query['group'] ) ) { |
|
423 | - $sql .= ' AND g.slug=%s'; |
|
424 | - $sql_params[] = $query['group']; |
|
425 | - } |
|
426 | - |
|
427 | - if ( ! empty( $query['hook'] ) ) { |
|
428 | - $sql .= ' AND a.hook=%s'; |
|
429 | - $sql_params[] = $query['hook']; |
|
430 | - } |
|
431 | - |
|
432 | - if ( $query['args'] !== null ) { |
|
433 | - switch ( $query['partial_args_matching'] ) { |
|
434 | - case 'json': |
|
435 | - if ( ! $supports_json ) { |
|
436 | - throw new \RuntimeException( __( 'JSON partial matching not supported in your environment. Please check your MySQL/MariaDB version.', 'action-scheduler' ) ); |
|
437 | - } |
|
438 | - $supported_types = array( |
|
439 | - 'integer' => '%d', |
|
440 | - 'boolean' => '%s', |
|
441 | - 'double' => '%f', |
|
442 | - 'string' => '%s', |
|
443 | - ); |
|
444 | - foreach ( $query['args'] as $key => $value ) { |
|
445 | - $value_type = gettype( $value ); |
|
446 | - if ( 'boolean' === $value_type ) { |
|
447 | - $value = $value ? 'true' : 'false'; |
|
448 | - } |
|
449 | - $placeholder = isset( $supported_types[ $value_type ] ) ? $supported_types[ $value_type ] : false; |
|
450 | - if ( ! $placeholder ) { |
|
451 | - throw new \RuntimeException( |
|
452 | - sprintf( |
|
453 | - /* translators: %s: provided value type */ |
|
454 | - __( 'The value type for the JSON partial matching is not supported. Must be either integer, boolean, double or string. %s type provided.', 'action-scheduler' ), |
|
455 | - $value_type |
|
456 | - ) |
|
457 | - ); |
|
458 | - } |
|
459 | - $sql .= ' AND JSON_EXTRACT(a.args, %s)=' . $placeholder; |
|
460 | - $sql_params[] = '$.' . $key; |
|
461 | - $sql_params[] = $value; |
|
462 | - } |
|
463 | - break; |
|
464 | - case 'like': |
|
465 | - foreach ( $query['args'] as $key => $value ) { |
|
466 | - $sql .= ' AND a.args LIKE %s'; |
|
467 | - $json_partial = $wpdb->esc_like( trim( json_encode( array( $key => $value ) ), '{}' ) ); |
|
468 | - $sql_params[] = "%{$json_partial}%"; |
|
469 | - } |
|
470 | - break; |
|
471 | - case 'off': |
|
472 | - $sql .= ' AND a.args=%s'; |
|
473 | - $sql_params[] = $this->get_args_for_query( $query['args'] ); |
|
474 | - break; |
|
475 | - default: |
|
476 | - throw new \RuntimeException( __( 'Unknown partial args matching value.', 'action-scheduler' ) ); |
|
477 | - } |
|
478 | - } |
|
479 | - |
|
480 | - if ( $query['status'] ) { |
|
481 | - $statuses = (array) $query['status']; |
|
482 | - $placeholders = array_fill( 0, count( $statuses ), '%s' ); |
|
483 | - $sql .= ' AND a.status IN (' . join( ', ', $placeholders ) . ')'; |
|
484 | - $sql_params = array_merge( $sql_params, array_values( $statuses ) ); |
|
485 | - } |
|
486 | - |
|
487 | - if ( $query['date'] instanceof \DateTime ) { |
|
488 | - $date = clone $query['date']; |
|
489 | - $date->setTimezone( new \DateTimeZone( 'UTC' ) ); |
|
490 | - $date_string = $date->format( 'Y-m-d H:i:s' ); |
|
491 | - $comparator = $this->validate_sql_comparator( $query['date_compare'] ); |
|
492 | - $sql .= " AND a.scheduled_date_gmt $comparator %s"; |
|
493 | - $sql_params[] = $date_string; |
|
494 | - } |
|
495 | - |
|
496 | - if ( $query['modified'] instanceof \DateTime ) { |
|
497 | - $modified = clone $query['modified']; |
|
498 | - $modified->setTimezone( new \DateTimeZone( 'UTC' ) ); |
|
499 | - $date_string = $modified->format( 'Y-m-d H:i:s' ); |
|
500 | - $comparator = $this->validate_sql_comparator( $query['modified_compare'] ); |
|
501 | - $sql .= " AND a.last_attempt_gmt $comparator %s"; |
|
502 | - $sql_params[] = $date_string; |
|
503 | - } |
|
504 | - |
|
505 | - if ( true === $query['claimed'] ) { |
|
506 | - $sql .= ' AND a.claim_id != 0'; |
|
507 | - } elseif ( false === $query['claimed'] ) { |
|
508 | - $sql .= ' AND a.claim_id = 0'; |
|
509 | - } elseif ( $query['claimed'] !== null ) { |
|
510 | - $sql .= ' AND a.claim_id = %d'; |
|
511 | - $sql_params[] = $query['claimed']; |
|
512 | - } |
|
513 | - |
|
514 | - if ( ! empty( $query['search'] ) ) { |
|
515 | - $sql .= ' AND (a.hook LIKE %s OR (a.extended_args IS NULL AND a.args LIKE %s) OR a.extended_args LIKE %s'; |
|
516 | - for ( $i = 0; $i < 3; $i++ ) { |
|
517 | - $sql_params[] = sprintf( '%%%s%%', $query['search'] ); |
|
518 | - } |
|
519 | - |
|
520 | - $search_claim_id = (int) $query['search']; |
|
521 | - if ( $search_claim_id ) { |
|
522 | - $sql .= ' OR a.claim_id = %d'; |
|
523 | - $sql_params[] = $search_claim_id; |
|
524 | - } |
|
525 | - |
|
526 | - $sql .= ')'; |
|
527 | - } |
|
528 | - |
|
529 | - if ( 'select' === $select_or_count ) { |
|
530 | - if ( 'ASC' === strtoupper( $query['order'] ) ) { |
|
531 | - $order = 'ASC'; |
|
532 | - } else { |
|
533 | - $order = 'DESC'; |
|
534 | - } |
|
535 | - switch ( $query['orderby'] ) { |
|
536 | - case 'hook': |
|
537 | - $sql .= " ORDER BY a.hook $order"; |
|
538 | - break; |
|
539 | - case 'group': |
|
540 | - $sql .= " ORDER BY g.slug $order"; |
|
541 | - break; |
|
542 | - case 'modified': |
|
543 | - $sql .= " ORDER BY a.last_attempt_gmt $order"; |
|
544 | - break; |
|
545 | - case 'none': |
|
546 | - break; |
|
547 | - case 'action_id': |
|
548 | - $sql .= " ORDER BY a.action_id $order"; |
|
549 | - break; |
|
550 | - case 'date': |
|
551 | - default: |
|
552 | - $sql .= " ORDER BY a.scheduled_date_gmt $order"; |
|
553 | - break; |
|
554 | - } |
|
555 | - |
|
556 | - if ( $query['per_page'] > 0 ) { |
|
557 | - $sql .= ' LIMIT %d, %d'; |
|
558 | - $sql_params[] = $query['offset']; |
|
559 | - $sql_params[] = $query['per_page']; |
|
560 | - } |
|
561 | - } |
|
562 | - |
|
563 | - if ( ! empty( $sql_params ) ) { |
|
564 | - $sql = $wpdb->prepare( $sql, $sql_params ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
565 | - } |
|
566 | - |
|
567 | - return $sql; |
|
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Query for action count or list of action IDs. |
|
572 | - * |
|
573 | - * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status. |
|
574 | - * |
|
575 | - * @see ActionScheduler_Store::query_actions for $query arg usage. |
|
576 | - * |
|
577 | - * @param array $query Query filtering options. |
|
578 | - * @param string $query_type Whether to select or count the results. Defaults to select. |
|
579 | - * |
|
580 | - * @return string|array|null The IDs of actions matching the query. Null on failure. |
|
581 | - */ |
|
582 | - public function query_actions( $query = array(), $query_type = 'select' ) { |
|
583 | - /** @var wpdb $wpdb */ |
|
584 | - global $wpdb; |
|
585 | - |
|
586 | - $sql = $this->get_query_actions_sql( $query, $query_type ); |
|
587 | - |
|
588 | - return ( 'count' === $query_type ) ? $wpdb->get_var( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoSql, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Get a count of all actions in the store, grouped by status. |
|
593 | - * |
|
594 | - * @return array Set of 'status' => int $count pairs for statuses with 1 or more actions of that status. |
|
595 | - */ |
|
596 | - public function action_counts() { |
|
597 | - global $wpdb; |
|
598 | - |
|
599 | - $sql = "SELECT a.status, count(a.status) as 'count'"; |
|
600 | - $sql .= " FROM {$wpdb->actionscheduler_actions} a"; |
|
601 | - $sql .= ' GROUP BY a.status'; |
|
602 | - |
|
603 | - $actions_count_by_status = array(); |
|
604 | - $action_stati_and_labels = $this->get_status_labels(); |
|
605 | - |
|
606 | - foreach ( $wpdb->get_results( $sql ) as $action_data ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
607 | - // Ignore any actions with invalid status. |
|
608 | - if ( array_key_exists( $action_data->status, $action_stati_and_labels ) ) { |
|
609 | - $actions_count_by_status[ $action_data->status ] = $action_data->count; |
|
610 | - } |
|
611 | - } |
|
612 | - |
|
613 | - return $actions_count_by_status; |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Cancel an action. |
|
618 | - * |
|
619 | - * @param int $action_id Action ID. |
|
620 | - * |
|
621 | - * @return void |
|
622 | - * @throws \InvalidArgumentException If the action update failed. |
|
623 | - */ |
|
624 | - public function cancel_action( $action_id ) { |
|
625 | - /** @var \wpdb $wpdb */ |
|
626 | - global $wpdb; |
|
627 | - |
|
628 | - $updated = $wpdb->update( |
|
629 | - $wpdb->actionscheduler_actions, |
|
630 | - array( 'status' => self::STATUS_CANCELED ), |
|
631 | - array( 'action_id' => $action_id ), |
|
632 | - array( '%s' ), |
|
633 | - array( '%d' ) |
|
634 | - ); |
|
635 | - if ( false === $updated ) { |
|
636 | - /* translators: %s: action ID */ |
|
637 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); |
|
638 | - } |
|
639 | - do_action( 'action_scheduler_canceled_action', $action_id ); |
|
640 | - } |
|
641 | - |
|
642 | - /** |
|
643 | - * Cancel pending actions by hook. |
|
644 | - * |
|
645 | - * @since 3.0.0 |
|
646 | - * |
|
647 | - * @param string $hook Hook name. |
|
648 | - * |
|
649 | - * @return void |
|
650 | - */ |
|
651 | - public function cancel_actions_by_hook( $hook ) { |
|
652 | - $this->bulk_cancel_actions( array( 'hook' => $hook ) ); |
|
653 | - } |
|
654 | - |
|
655 | - /** |
|
656 | - * Cancel pending actions by group. |
|
657 | - * |
|
658 | - * @param string $group Group slug. |
|
659 | - * |
|
660 | - * @return void |
|
661 | - */ |
|
662 | - public function cancel_actions_by_group( $group ) { |
|
663 | - $this->bulk_cancel_actions( array( 'group' => $group ) ); |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * Bulk cancel actions. |
|
668 | - * |
|
669 | - * @since 3.0.0 |
|
670 | - * |
|
671 | - * @param array $query_args Query parameters. |
|
672 | - */ |
|
673 | - protected function bulk_cancel_actions( $query_args ) { |
|
674 | - /** @var \wpdb $wpdb */ |
|
675 | - global $wpdb; |
|
676 | - |
|
677 | - if ( ! is_array( $query_args ) ) { |
|
678 | - return; |
|
679 | - } |
|
680 | - |
|
681 | - // Don't cancel actions that are already canceled. |
|
682 | - if ( isset( $query_args['status'] ) && self::STATUS_CANCELED === $query_args['status'] ) { |
|
683 | - return; |
|
684 | - } |
|
685 | - |
|
686 | - $action_ids = true; |
|
687 | - $query_args = wp_parse_args( |
|
688 | - $query_args, |
|
689 | - array( |
|
690 | - 'per_page' => 1000, |
|
691 | - 'status' => self::STATUS_PENDING, |
|
692 | - 'orderby' => 'action_id', |
|
693 | - ) |
|
694 | - ); |
|
695 | - |
|
696 | - while ( $action_ids ) { |
|
697 | - $action_ids = $this->query_actions( $query_args ); |
|
698 | - if ( empty( $action_ids ) ) { |
|
699 | - break; |
|
700 | - } |
|
701 | - |
|
702 | - $format = array_fill( 0, count( $action_ids ), '%d' ); |
|
703 | - $query_in = '(' . implode( ',', $format ) . ')'; |
|
704 | - $parameters = $action_ids; |
|
705 | - array_unshift( $parameters, self::STATUS_CANCELED ); |
|
706 | - |
|
707 | - $wpdb->query( |
|
708 | - $wpdb->prepare( |
|
709 | - "UPDATE {$wpdb->actionscheduler_actions} SET status = %s WHERE action_id IN {$query_in}", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
710 | - $parameters |
|
711 | - ) |
|
712 | - ); |
|
713 | - |
|
714 | - do_action( 'action_scheduler_bulk_cancel_actions', $action_ids ); |
|
715 | - } |
|
716 | - } |
|
717 | - |
|
718 | - /** |
|
719 | - * Delete an action. |
|
720 | - * |
|
721 | - * @param int $action_id Action ID. |
|
722 | - * @throws \InvalidArgumentException If the action deletion failed. |
|
723 | - */ |
|
724 | - public function delete_action( $action_id ) { |
|
725 | - /** @var \wpdb $wpdb */ |
|
726 | - global $wpdb; |
|
727 | - $deleted = $wpdb->delete( $wpdb->actionscheduler_actions, array( 'action_id' => $action_id ), array( '%d' ) ); |
|
728 | - if ( empty( $deleted ) ) { |
|
729 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
730 | - } |
|
731 | - do_action( 'action_scheduler_deleted_action', $action_id ); |
|
732 | - } |
|
733 | - |
|
734 | - /** |
|
735 | - * Get the schedule date for an action. |
|
736 | - * |
|
737 | - * @param string $action_id Action ID. |
|
738 | - * |
|
739 | - * @return \DateTime The local date the action is scheduled to run, or the date that it ran. |
|
740 | - */ |
|
741 | - public function get_date( $action_id ) { |
|
742 | - $date = $this->get_date_gmt( $action_id ); |
|
743 | - ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
744 | - return $date; |
|
745 | - } |
|
746 | - |
|
747 | - /** |
|
748 | - * Get the GMT schedule date for an action. |
|
749 | - * |
|
750 | - * @param int $action_id Action ID. |
|
751 | - * |
|
752 | - * @throws \InvalidArgumentException If action cannot be identified. |
|
753 | - * @return \DateTime The GMT date the action is scheduled to run, or the date that it ran. |
|
754 | - */ |
|
755 | - protected function get_date_gmt( $action_id ) { |
|
756 | - /** @var \wpdb $wpdb */ |
|
757 | - global $wpdb; |
|
758 | - $record = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d", $action_id ) ); |
|
759 | - if ( empty( $record ) ) { |
|
760 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
761 | - } |
|
762 | - if ( self::STATUS_PENDING === $record->status ) { |
|
763 | - return as_get_datetime_object( $record->scheduled_date_gmt ); |
|
764 | - } else { |
|
765 | - return as_get_datetime_object( $record->last_attempt_gmt ); |
|
766 | - } |
|
767 | - } |
|
768 | - |
|
769 | - /** |
|
770 | - * Stake a claim on actions. |
|
771 | - * |
|
772 | - * @param int $max_actions Maximum number of action to include in claim. |
|
773 | - * @param \DateTime $before_date Jobs must be schedule before this date. Defaults to now. |
|
774 | - * @param array $hooks Hooks to filter for. |
|
775 | - * @param string $group Group to filter for. |
|
776 | - * |
|
777 | - * @return ActionScheduler_ActionClaim |
|
778 | - */ |
|
779 | - public function stake_claim( $max_actions = 10, \DateTime $before_date = null, $hooks = array(), $group = '' ) { |
|
780 | - $claim_id = $this->generate_claim_id(); |
|
781 | - |
|
782 | - $this->claim_before_date = $before_date; |
|
783 | - $this->claim_actions( $claim_id, $max_actions, $before_date, $hooks, $group ); |
|
784 | - $action_ids = $this->find_actions_by_claim_id( $claim_id ); |
|
785 | - $this->claim_before_date = null; |
|
786 | - |
|
787 | - return new ActionScheduler_ActionClaim( $claim_id, $action_ids ); |
|
788 | - } |
|
789 | - |
|
790 | - /** |
|
791 | - * Generate a new action claim. |
|
792 | - * |
|
793 | - * @return int Claim ID. |
|
794 | - */ |
|
795 | - protected function generate_claim_id() { |
|
796 | - /** @var \wpdb $wpdb */ |
|
797 | - global $wpdb; |
|
798 | - $now = as_get_datetime_object(); |
|
799 | - $wpdb->insert( $wpdb->actionscheduler_claims, array( 'date_created_gmt' => $now->format( 'Y-m-d H:i:s' ) ) ); |
|
800 | - |
|
801 | - return $wpdb->insert_id; |
|
802 | - } |
|
803 | - |
|
804 | - /** |
|
805 | - * Mark actions claimed. |
|
806 | - * |
|
807 | - * @param string $claim_id Claim Id. |
|
808 | - * @param int $limit Number of action to include in claim. |
|
809 | - * @param \DateTime $before_date Should use UTC timezone. |
|
810 | - * @param array $hooks Hooks to filter for. |
|
811 | - * @param string $group Group to filter for. |
|
812 | - * |
|
813 | - * @return int The number of actions that were claimed. |
|
814 | - * @throws \InvalidArgumentException Throws InvalidArgumentException if group doesn't exist. |
|
815 | - * @throws \RuntimeException Throws RuntimeException if unable to claim action. |
|
816 | - */ |
|
817 | - protected function claim_actions( $claim_id, $limit, \DateTime $before_date = null, $hooks = array(), $group = '' ) { |
|
818 | - /** @var \wpdb $wpdb */ |
|
819 | - global $wpdb; |
|
820 | - |
|
821 | - $now = as_get_datetime_object(); |
|
822 | - $date = $before_date === null ? $now : clone $before_date; |
|
823 | - |
|
824 | - // can't use $wpdb->update() because of the <= condition. |
|
825 | - $update = "UPDATE {$wpdb->actionscheduler_actions} SET claim_id=%d, last_attempt_gmt=%s, last_attempt_local=%s"; |
|
826 | - $params = array( |
|
827 | - $claim_id, |
|
828 | - $now->format( 'Y-m-d H:i:s' ), |
|
829 | - current_time( 'mysql' ), |
|
830 | - ); |
|
831 | - |
|
832 | - $where = 'WHERE claim_id = 0 AND scheduled_date_gmt <= %s AND status=%s'; |
|
833 | - $params[] = $date->format( 'Y-m-d H:i:s' ); |
|
834 | - $params[] = self::STATUS_PENDING; |
|
835 | - |
|
836 | - if ( ! empty( $hooks ) ) { |
|
837 | - $placeholders = array_fill( 0, count( $hooks ), '%s' ); |
|
838 | - $where .= ' AND hook IN (' . join( ', ', $placeholders ) . ')'; |
|
839 | - $params = array_merge( $params, array_values( $hooks ) ); |
|
840 | - } |
|
841 | - |
|
842 | - if ( ! empty( $group ) ) { |
|
843 | - |
|
844 | - $group_id = $this->get_group_id( $group, false ); |
|
845 | - |
|
846 | - // throw exception if no matching group found, this matches ActionScheduler_wpPostStore's behaviour. |
|
847 | - if ( empty( $group_id ) ) { |
|
848 | - /* translators: %s: group name */ |
|
849 | - throw new InvalidArgumentException( sprintf( __( 'The group "%s" does not exist.', 'action-scheduler' ), $group ) ); |
|
850 | - } |
|
851 | - |
|
852 | - $where .= ' AND group_id = %d'; |
|
853 | - $params[] = $group_id; |
|
854 | - } |
|
855 | - |
|
856 | - /** |
|
857 | - * Sets the order-by clause used in the action claim query. |
|
858 | - * |
|
859 | - * @since 3.4.0 |
|
860 | - * |
|
861 | - * @param string $order_by_sql |
|
862 | - */ |
|
863 | - $order = apply_filters( 'action_scheduler_claim_actions_order_by', 'ORDER BY attempts ASC, scheduled_date_gmt ASC, action_id ASC' ); |
|
864 | - $params[] = $limit; |
|
865 | - |
|
866 | - $sql = $wpdb->prepare( "{$update} {$where} {$order} LIMIT %d", $params ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders |
|
867 | - $rows_affected = $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
868 | - if ( false === $rows_affected ) { |
|
869 | - throw new \RuntimeException( __( 'Unable to claim actions. Database error.', 'action-scheduler' ) ); |
|
870 | - } |
|
871 | - |
|
872 | - return (int) $rows_affected; |
|
873 | - } |
|
874 | - |
|
875 | - /** |
|
876 | - * Get the number of active claims. |
|
877 | - * |
|
878 | - * @return int |
|
879 | - */ |
|
880 | - public function get_claim_count() { |
|
881 | - global $wpdb; |
|
882 | - |
|
883 | - $sql = "SELECT COUNT(DISTINCT claim_id) FROM {$wpdb->actionscheduler_actions} WHERE claim_id != 0 AND status IN ( %s, %s)"; |
|
884 | - $sql = $wpdb->prepare( $sql, array( self::STATUS_PENDING, self::STATUS_RUNNING ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
885 | - |
|
886 | - return (int) $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
887 | - } |
|
888 | - |
|
889 | - /** |
|
890 | - * Return an action's claim ID, as stored in the claim_id column. |
|
891 | - * |
|
892 | - * @param string $action_id Action ID. |
|
893 | - * @return mixed |
|
894 | - */ |
|
895 | - public function get_claim_id( $action_id ) { |
|
896 | - /** @var \wpdb $wpdb */ |
|
897 | - global $wpdb; |
|
898 | - |
|
899 | - $sql = "SELECT claim_id FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d"; |
|
900 | - $sql = $wpdb->prepare( $sql, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
901 | - |
|
902 | - return (int) $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
903 | - } |
|
904 | - |
|
905 | - /** |
|
906 | - * Retrieve the action IDs of action in a claim. |
|
907 | - * |
|
908 | - * @param int $claim_id Claim ID. |
|
909 | - * @return int[] |
|
910 | - */ |
|
911 | - public function find_actions_by_claim_id( $claim_id ) { |
|
912 | - /** @var \wpdb $wpdb */ |
|
913 | - global $wpdb; |
|
914 | - |
|
915 | - $action_ids = array(); |
|
916 | - $before_date = isset( $this->claim_before_date ) ? $this->claim_before_date : as_get_datetime_object(); |
|
917 | - $cut_off = $before_date->format( 'Y-m-d H:i:s' ); |
|
918 | - |
|
919 | - $sql = $wpdb->prepare( |
|
920 | - "SELECT action_id, scheduled_date_gmt FROM {$wpdb->actionscheduler_actions} WHERE claim_id = %d", |
|
921 | - $claim_id |
|
922 | - ); |
|
923 | - |
|
924 | - // Verify that the scheduled date for each action is within the expected bounds (in some unusual |
|
925 | - // cases, we cannot depend on MySQL to honor all of the WHERE conditions we specify). |
|
926 | - foreach ( $wpdb->get_results( $sql ) as $claimed_action ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
927 | - if ( $claimed_action->scheduled_date_gmt <= $cut_off ) { |
|
928 | - $action_ids[] = absint( $claimed_action->action_id ); |
|
929 | - } |
|
930 | - } |
|
931 | - |
|
932 | - return $action_ids; |
|
933 | - } |
|
934 | - |
|
935 | - /** |
|
936 | - * Release actions from a claim and delete the claim. |
|
937 | - * |
|
938 | - * @param ActionScheduler_ActionClaim $claim Claim object. |
|
939 | - */ |
|
940 | - public function release_claim( ActionScheduler_ActionClaim $claim ) { |
|
941 | - /** @var \wpdb $wpdb */ |
|
942 | - global $wpdb; |
|
943 | - $wpdb->update( $wpdb->actionscheduler_actions, array( 'claim_id' => 0 ), array( 'claim_id' => $claim->get_id() ), array( '%d' ), array( '%d' ) ); |
|
944 | - $wpdb->delete( $wpdb->actionscheduler_claims, array( 'claim_id' => $claim->get_id() ), array( '%d' ) ); |
|
945 | - } |
|
946 | - |
|
947 | - /** |
|
948 | - * Remove the claim from an action. |
|
949 | - * |
|
950 | - * @param int $action_id Action ID. |
|
951 | - * |
|
952 | - * @return void |
|
953 | - */ |
|
954 | - public function unclaim_action( $action_id ) { |
|
955 | - /** @var \wpdb $wpdb */ |
|
956 | - global $wpdb; |
|
957 | - $wpdb->update( |
|
958 | - $wpdb->actionscheduler_actions, |
|
959 | - array( 'claim_id' => 0 ), |
|
960 | - array( 'action_id' => $action_id ), |
|
961 | - array( '%s' ), |
|
962 | - array( '%d' ) |
|
963 | - ); |
|
964 | - } |
|
965 | - |
|
966 | - /** |
|
967 | - * Mark an action as failed. |
|
968 | - * |
|
969 | - * @param int $action_id Action ID. |
|
970 | - * @throws \InvalidArgumentException Throw an exception if action was not updated. |
|
971 | - */ |
|
972 | - public function mark_failure( $action_id ) { |
|
973 | - /** @var \wpdb $wpdb */ |
|
974 | - global $wpdb; |
|
975 | - $updated = $wpdb->update( |
|
976 | - $wpdb->actionscheduler_actions, |
|
977 | - array( 'status' => self::STATUS_FAILED ), |
|
978 | - array( 'action_id' => $action_id ), |
|
979 | - array( '%s' ), |
|
980 | - array( '%d' ) |
|
981 | - ); |
|
982 | - if ( empty( $updated ) ) { |
|
983 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
984 | - } |
|
985 | - } |
|
986 | - |
|
987 | - /** |
|
988 | - * Add execution message to action log. |
|
989 | - * |
|
990 | - * @param int $action_id Action ID. |
|
991 | - * |
|
992 | - * @return void |
|
993 | - */ |
|
994 | - public function log_execution( $action_id ) { |
|
995 | - /** @var \wpdb $wpdb */ |
|
996 | - global $wpdb; |
|
997 | - |
|
998 | - $sql = "UPDATE {$wpdb->actionscheduler_actions} SET attempts = attempts+1, status=%s, last_attempt_gmt = %s, last_attempt_local = %s WHERE action_id = %d"; |
|
999 | - $sql = $wpdb->prepare( $sql, self::STATUS_RUNNING, current_time( 'mysql', true ), current_time( 'mysql' ), $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1000 | - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1001 | - } |
|
1002 | - |
|
1003 | - /** |
|
1004 | - * Mark an action as complete. |
|
1005 | - * |
|
1006 | - * @param int $action_id Action ID. |
|
1007 | - * |
|
1008 | - * @return void |
|
1009 | - * @throws \InvalidArgumentException Throw an exception if action was not updated. |
|
1010 | - */ |
|
1011 | - public function mark_complete( $action_id ) { |
|
1012 | - /** @var \wpdb $wpdb */ |
|
1013 | - global $wpdb; |
|
1014 | - $updated = $wpdb->update( |
|
1015 | - $wpdb->actionscheduler_actions, |
|
1016 | - array( |
|
1017 | - 'status' => self::STATUS_COMPLETE, |
|
1018 | - 'last_attempt_gmt' => current_time( 'mysql', true ), |
|
1019 | - 'last_attempt_local' => current_time( 'mysql' ), |
|
1020 | - ), |
|
1021 | - array( 'action_id' => $action_id ), |
|
1022 | - array( '%s' ), |
|
1023 | - array( '%d' ) |
|
1024 | - ); |
|
1025 | - if ( empty( $updated ) ) { |
|
1026 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
1027 | - } |
|
1028 | - |
|
1029 | - /** |
|
1030 | - * Fires after a scheduled action has been completed. |
|
1031 | - * |
|
1032 | - * @since 3.4.2 |
|
1033 | - * |
|
1034 | - * @param int $action_id Action ID. |
|
1035 | - */ |
|
1036 | - do_action( 'action_scheduler_completed_action', $action_id ); |
|
1037 | - } |
|
1038 | - |
|
1039 | - /** |
|
1040 | - * Get an action's status. |
|
1041 | - * |
|
1042 | - * @param int $action_id Action ID. |
|
1043 | - * |
|
1044 | - * @return string |
|
1045 | - * @throws \InvalidArgumentException Throw an exception if not status was found for action_id. |
|
1046 | - * @throws \RuntimeException Throw an exception if action status could not be retrieved. |
|
1047 | - */ |
|
1048 | - public function get_status( $action_id ) { |
|
1049 | - /** @var \wpdb $wpdb */ |
|
1050 | - global $wpdb; |
|
1051 | - $sql = "SELECT status FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d"; |
|
1052 | - $sql = $wpdb->prepare( $sql, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1053 | - $status = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1054 | - |
|
1055 | - if ( null === $status ) { |
|
1056 | - throw new \InvalidArgumentException( __( 'Invalid action ID. No status found.', 'action-scheduler' ) ); |
|
1057 | - } elseif ( empty( $status ) ) { |
|
1058 | - throw new \RuntimeException( __( 'Unknown status found for action.', 'action-scheduler' ) ); |
|
1059 | - } else { |
|
1060 | - return $status; |
|
1061 | - } |
|
1062 | - } |
|
183 | + array_merge( |
|
184 | + $pending_statuses, |
|
185 | + array( |
|
186 | + $data['hook'], |
|
187 | + $data['group_id'], |
|
188 | + ) |
|
189 | + ) |
|
190 | + ); |
|
191 | + // phpcs:enable |
|
192 | + |
|
193 | + return "$where_clause" . ' LIMIT 1'; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Helper method to get $wpdb->prepare placeholder for a given column name. |
|
198 | + * |
|
199 | + * @param string $column_name Name of column in actions table. |
|
200 | + * |
|
201 | + * @return string Placeholder to use for given column. |
|
202 | + */ |
|
203 | + private function get_placeholder_for_column( $column_name ) { |
|
204 | + $string_columns = array( |
|
205 | + 'hook', |
|
206 | + 'status', |
|
207 | + 'scheduled_date_gmt', |
|
208 | + 'scheduled_date_local', |
|
209 | + 'args', |
|
210 | + 'schedule', |
|
211 | + 'last_attempt_gmt', |
|
212 | + 'last_attempt_local', |
|
213 | + 'extended_args', |
|
214 | + ); |
|
215 | + |
|
216 | + return in_array( $column_name, $string_columns ) ? '%s' : '%d'; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Generate a hash from json_encoded $args using MD5 as this isn't for security. |
|
221 | + * |
|
222 | + * @param string $args JSON encoded action args. |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + protected function hash_args( $args ) { |
|
226 | + return md5( $args ); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Get action args query param value from action args. |
|
231 | + * |
|
232 | + * @param array $args Action args. |
|
233 | + * @return string |
|
234 | + */ |
|
235 | + protected function get_args_for_query( $args ) { |
|
236 | + $encoded = wp_json_encode( $args ); |
|
237 | + if ( strlen( $encoded ) <= static::$max_index_length ) { |
|
238 | + return $encoded; |
|
239 | + } |
|
240 | + return $this->hash_args( $encoded ); |
|
241 | + } |
|
242 | + /** |
|
243 | + * Get a group's ID based on its name/slug. |
|
244 | + * |
|
245 | + * @param string $slug The string name of a group. |
|
246 | + * @param bool $create_if_not_exists Whether to create the group if it does not already exist. Default, true - create the group. |
|
247 | + * |
|
248 | + * @return int The group's ID, if it exists or is created, or 0 if it does not exist and is not created. |
|
249 | + */ |
|
250 | + protected function get_group_id( $slug, $create_if_not_exists = true ) { |
|
251 | + if ( empty( $slug ) ) { |
|
252 | + return 0; |
|
253 | + } |
|
254 | + /** @var \wpdb $wpdb */ |
|
255 | + global $wpdb; |
|
256 | + $group_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$wpdb->actionscheduler_groups} WHERE slug=%s", $slug ) ); |
|
257 | + if ( empty( $group_id ) && $create_if_not_exists ) { |
|
258 | + $group_id = $this->create_group( $slug ); |
|
259 | + } |
|
260 | + |
|
261 | + return $group_id; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Create an action group. |
|
266 | + * |
|
267 | + * @param string $slug Group slug. |
|
268 | + * |
|
269 | + * @return int Group ID. |
|
270 | + */ |
|
271 | + protected function create_group( $slug ) { |
|
272 | + /** @var \wpdb $wpdb */ |
|
273 | + global $wpdb; |
|
274 | + $wpdb->insert( $wpdb->actionscheduler_groups, array( 'slug' => $slug ) ); |
|
275 | + |
|
276 | + return (int) $wpdb->insert_id; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Retrieve an action. |
|
281 | + * |
|
282 | + * @param int $action_id Action ID. |
|
283 | + * |
|
284 | + * @return ActionScheduler_Action |
|
285 | + */ |
|
286 | + public function fetch_action( $action_id ) { |
|
287 | + /** @var \wpdb $wpdb */ |
|
288 | + global $wpdb; |
|
289 | + $data = $wpdb->get_row( |
|
290 | + $wpdb->prepare( |
|
291 | + "SELECT a.*, g.slug AS `group` FROM {$wpdb->actionscheduler_actions} a LEFT JOIN {$wpdb->actionscheduler_groups} g ON a.group_id=g.group_id WHERE a.action_id=%d", |
|
292 | + $action_id |
|
293 | + ) |
|
294 | + ); |
|
295 | + |
|
296 | + if ( empty( $data ) ) { |
|
297 | + return $this->get_null_action(); |
|
298 | + } |
|
299 | + |
|
300 | + if ( ! empty( $data->extended_args ) ) { |
|
301 | + $data->args = $data->extended_args; |
|
302 | + unset( $data->extended_args ); |
|
303 | + } |
|
304 | + |
|
305 | + // Convert NULL dates to zero dates. |
|
306 | + $date_fields = array( |
|
307 | + 'scheduled_date_gmt', |
|
308 | + 'scheduled_date_local', |
|
309 | + 'last_attempt_gmt', |
|
310 | + 'last_attempt_gmt', |
|
311 | + ); |
|
312 | + foreach ( $date_fields as $date_field ) { |
|
313 | + if ( $data->$date_field === null ) { |
|
314 | + $data->$date_field = ActionScheduler_StoreSchema::DEFAULT_DATE; |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + try { |
|
319 | + $action = $this->make_action_from_db_record( $data ); |
|
320 | + } catch ( ActionScheduler_InvalidActionException $exception ) { |
|
321 | + do_action( 'action_scheduler_failed_fetch_action', $action_id, $exception ); |
|
322 | + return $this->get_null_action(); |
|
323 | + } |
|
324 | + |
|
325 | + return $action; |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Create a null action. |
|
330 | + * |
|
331 | + * @return ActionScheduler_NullAction |
|
332 | + */ |
|
333 | + protected function get_null_action() { |
|
334 | + return new ActionScheduler_NullAction(); |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Create an action from a database record. |
|
339 | + * |
|
340 | + * @param object $data Action database record. |
|
341 | + * |
|
342 | + * @return ActionScheduler_Action|ActionScheduler_CanceledAction|ActionScheduler_FinishedAction |
|
343 | + */ |
|
344 | + protected function make_action_from_db_record( $data ) { |
|
345 | + |
|
346 | + $hook = $data->hook; |
|
347 | + $args = json_decode( $data->args, true ); |
|
348 | + $schedule = unserialize( $data->schedule ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
349 | + |
|
350 | + $this->validate_args( $args, $data->action_id ); |
|
351 | + $this->validate_schedule( $schedule, $data->action_id ); |
|
352 | + |
|
353 | + if ( empty( $schedule ) ) { |
|
354 | + $schedule = new ActionScheduler_NullSchedule(); |
|
355 | + } |
|
356 | + $group = $data->group ? $data->group : ''; |
|
357 | + |
|
358 | + return ActionScheduler::factory()->get_stored_action( $data->status, $data->hook, $args, $schedule, $group ); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Returns the SQL statement to query (or count) actions. |
|
363 | + * |
|
364 | + * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status. |
|
365 | + * |
|
366 | + * @param array $query Filtering options. |
|
367 | + * @param string $select_or_count Whether the SQL should select and return the IDs or just the row count. |
|
368 | + * |
|
369 | + * @return string SQL statement already properly escaped. |
|
370 | + * @throws InvalidArgumentException If the query is invalid. |
|
371 | + */ |
|
372 | + protected function get_query_actions_sql( array $query, $select_or_count = 'select' ) { |
|
373 | + |
|
374 | + if ( ! in_array( $select_or_count, array( 'select', 'count' ), true ) ) { |
|
375 | + throw new InvalidArgumentException( __( 'Invalid value for select or count parameter. Cannot query actions.', 'action-scheduler' ) ); |
|
376 | + } |
|
377 | + |
|
378 | + $query = wp_parse_args( |
|
379 | + $query, |
|
380 | + array( |
|
381 | + 'hook' => '', |
|
382 | + 'args' => null, |
|
383 | + 'partial_args_matching' => 'off', // can be 'like' or 'json' |
|
384 | + 'date' => null, |
|
385 | + 'date_compare' => '<=', |
|
386 | + 'modified' => null, |
|
387 | + 'modified_compare' => '<=', |
|
388 | + 'group' => '', |
|
389 | + 'status' => '', |
|
390 | + 'claimed' => null, |
|
391 | + 'per_page' => 5, |
|
392 | + 'offset' => 0, |
|
393 | + 'orderby' => 'date', |
|
394 | + 'order' => 'ASC', |
|
395 | + ) |
|
396 | + ); |
|
397 | + |
|
398 | + /** @var \wpdb $wpdb */ |
|
399 | + global $wpdb; |
|
400 | + |
|
401 | + $db_server_info = is_callable( array( $wpdb, 'db_server_info' ) ) ? $wpdb->db_server_info() : $wpdb->db_version(); |
|
402 | + if ( false !== strpos( $db_server_info, 'MariaDB' ) ) { |
|
403 | + $supports_json = version_compare( |
|
404 | + PHP_VERSION_ID >= 80016 ? $wpdb->db_version() : preg_replace( '/[^0-9.].*/', '', str_replace( '5.5.5-', '', $db_server_info ) ), |
|
405 | + '10.2', |
|
406 | + '>=' |
|
407 | + ); |
|
408 | + } else { |
|
409 | + $supports_json = version_compare( $wpdb->db_version(), '5.7', '>=' ); |
|
410 | + } |
|
411 | + |
|
412 | + $sql = ( 'count' === $select_or_count ) ? 'SELECT count(a.action_id)' : 'SELECT a.action_id'; |
|
413 | + $sql .= " FROM {$wpdb->actionscheduler_actions} a"; |
|
414 | + $sql_params = array(); |
|
415 | + |
|
416 | + if ( ! empty( $query['group'] ) || 'group' === $query['orderby'] ) { |
|
417 | + $sql .= " LEFT JOIN {$wpdb->actionscheduler_groups} g ON g.group_id=a.group_id"; |
|
418 | + } |
|
419 | + |
|
420 | + $sql .= ' WHERE 1=1'; |
|
421 | + |
|
422 | + if ( ! empty( $query['group'] ) ) { |
|
423 | + $sql .= ' AND g.slug=%s'; |
|
424 | + $sql_params[] = $query['group']; |
|
425 | + } |
|
426 | + |
|
427 | + if ( ! empty( $query['hook'] ) ) { |
|
428 | + $sql .= ' AND a.hook=%s'; |
|
429 | + $sql_params[] = $query['hook']; |
|
430 | + } |
|
431 | + |
|
432 | + if ( $query['args'] !== null ) { |
|
433 | + switch ( $query['partial_args_matching'] ) { |
|
434 | + case 'json': |
|
435 | + if ( ! $supports_json ) { |
|
436 | + throw new \RuntimeException( __( 'JSON partial matching not supported in your environment. Please check your MySQL/MariaDB version.', 'action-scheduler' ) ); |
|
437 | + } |
|
438 | + $supported_types = array( |
|
439 | + 'integer' => '%d', |
|
440 | + 'boolean' => '%s', |
|
441 | + 'double' => '%f', |
|
442 | + 'string' => '%s', |
|
443 | + ); |
|
444 | + foreach ( $query['args'] as $key => $value ) { |
|
445 | + $value_type = gettype( $value ); |
|
446 | + if ( 'boolean' === $value_type ) { |
|
447 | + $value = $value ? 'true' : 'false'; |
|
448 | + } |
|
449 | + $placeholder = isset( $supported_types[ $value_type ] ) ? $supported_types[ $value_type ] : false; |
|
450 | + if ( ! $placeholder ) { |
|
451 | + throw new \RuntimeException( |
|
452 | + sprintf( |
|
453 | + /* translators: %s: provided value type */ |
|
454 | + __( 'The value type for the JSON partial matching is not supported. Must be either integer, boolean, double or string. %s type provided.', 'action-scheduler' ), |
|
455 | + $value_type |
|
456 | + ) |
|
457 | + ); |
|
458 | + } |
|
459 | + $sql .= ' AND JSON_EXTRACT(a.args, %s)=' . $placeholder; |
|
460 | + $sql_params[] = '$.' . $key; |
|
461 | + $sql_params[] = $value; |
|
462 | + } |
|
463 | + break; |
|
464 | + case 'like': |
|
465 | + foreach ( $query['args'] as $key => $value ) { |
|
466 | + $sql .= ' AND a.args LIKE %s'; |
|
467 | + $json_partial = $wpdb->esc_like( trim( json_encode( array( $key => $value ) ), '{}' ) ); |
|
468 | + $sql_params[] = "%{$json_partial}%"; |
|
469 | + } |
|
470 | + break; |
|
471 | + case 'off': |
|
472 | + $sql .= ' AND a.args=%s'; |
|
473 | + $sql_params[] = $this->get_args_for_query( $query['args'] ); |
|
474 | + break; |
|
475 | + default: |
|
476 | + throw new \RuntimeException( __( 'Unknown partial args matching value.', 'action-scheduler' ) ); |
|
477 | + } |
|
478 | + } |
|
479 | + |
|
480 | + if ( $query['status'] ) { |
|
481 | + $statuses = (array) $query['status']; |
|
482 | + $placeholders = array_fill( 0, count( $statuses ), '%s' ); |
|
483 | + $sql .= ' AND a.status IN (' . join( ', ', $placeholders ) . ')'; |
|
484 | + $sql_params = array_merge( $sql_params, array_values( $statuses ) ); |
|
485 | + } |
|
486 | + |
|
487 | + if ( $query['date'] instanceof \DateTime ) { |
|
488 | + $date = clone $query['date']; |
|
489 | + $date->setTimezone( new \DateTimeZone( 'UTC' ) ); |
|
490 | + $date_string = $date->format( 'Y-m-d H:i:s' ); |
|
491 | + $comparator = $this->validate_sql_comparator( $query['date_compare'] ); |
|
492 | + $sql .= " AND a.scheduled_date_gmt $comparator %s"; |
|
493 | + $sql_params[] = $date_string; |
|
494 | + } |
|
495 | + |
|
496 | + if ( $query['modified'] instanceof \DateTime ) { |
|
497 | + $modified = clone $query['modified']; |
|
498 | + $modified->setTimezone( new \DateTimeZone( 'UTC' ) ); |
|
499 | + $date_string = $modified->format( 'Y-m-d H:i:s' ); |
|
500 | + $comparator = $this->validate_sql_comparator( $query['modified_compare'] ); |
|
501 | + $sql .= " AND a.last_attempt_gmt $comparator %s"; |
|
502 | + $sql_params[] = $date_string; |
|
503 | + } |
|
504 | + |
|
505 | + if ( true === $query['claimed'] ) { |
|
506 | + $sql .= ' AND a.claim_id != 0'; |
|
507 | + } elseif ( false === $query['claimed'] ) { |
|
508 | + $sql .= ' AND a.claim_id = 0'; |
|
509 | + } elseif ( $query['claimed'] !== null ) { |
|
510 | + $sql .= ' AND a.claim_id = %d'; |
|
511 | + $sql_params[] = $query['claimed']; |
|
512 | + } |
|
513 | + |
|
514 | + if ( ! empty( $query['search'] ) ) { |
|
515 | + $sql .= ' AND (a.hook LIKE %s OR (a.extended_args IS NULL AND a.args LIKE %s) OR a.extended_args LIKE %s'; |
|
516 | + for ( $i = 0; $i < 3; $i++ ) { |
|
517 | + $sql_params[] = sprintf( '%%%s%%', $query['search'] ); |
|
518 | + } |
|
519 | + |
|
520 | + $search_claim_id = (int) $query['search']; |
|
521 | + if ( $search_claim_id ) { |
|
522 | + $sql .= ' OR a.claim_id = %d'; |
|
523 | + $sql_params[] = $search_claim_id; |
|
524 | + } |
|
525 | + |
|
526 | + $sql .= ')'; |
|
527 | + } |
|
528 | + |
|
529 | + if ( 'select' === $select_or_count ) { |
|
530 | + if ( 'ASC' === strtoupper( $query['order'] ) ) { |
|
531 | + $order = 'ASC'; |
|
532 | + } else { |
|
533 | + $order = 'DESC'; |
|
534 | + } |
|
535 | + switch ( $query['orderby'] ) { |
|
536 | + case 'hook': |
|
537 | + $sql .= " ORDER BY a.hook $order"; |
|
538 | + break; |
|
539 | + case 'group': |
|
540 | + $sql .= " ORDER BY g.slug $order"; |
|
541 | + break; |
|
542 | + case 'modified': |
|
543 | + $sql .= " ORDER BY a.last_attempt_gmt $order"; |
|
544 | + break; |
|
545 | + case 'none': |
|
546 | + break; |
|
547 | + case 'action_id': |
|
548 | + $sql .= " ORDER BY a.action_id $order"; |
|
549 | + break; |
|
550 | + case 'date': |
|
551 | + default: |
|
552 | + $sql .= " ORDER BY a.scheduled_date_gmt $order"; |
|
553 | + break; |
|
554 | + } |
|
555 | + |
|
556 | + if ( $query['per_page'] > 0 ) { |
|
557 | + $sql .= ' LIMIT %d, %d'; |
|
558 | + $sql_params[] = $query['offset']; |
|
559 | + $sql_params[] = $query['per_page']; |
|
560 | + } |
|
561 | + } |
|
562 | + |
|
563 | + if ( ! empty( $sql_params ) ) { |
|
564 | + $sql = $wpdb->prepare( $sql, $sql_params ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
565 | + } |
|
566 | + |
|
567 | + return $sql; |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Query for action count or list of action IDs. |
|
572 | + * |
|
573 | + * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status. |
|
574 | + * |
|
575 | + * @see ActionScheduler_Store::query_actions for $query arg usage. |
|
576 | + * |
|
577 | + * @param array $query Query filtering options. |
|
578 | + * @param string $query_type Whether to select or count the results. Defaults to select. |
|
579 | + * |
|
580 | + * @return string|array|null The IDs of actions matching the query. Null on failure. |
|
581 | + */ |
|
582 | + public function query_actions( $query = array(), $query_type = 'select' ) { |
|
583 | + /** @var wpdb $wpdb */ |
|
584 | + global $wpdb; |
|
585 | + |
|
586 | + $sql = $this->get_query_actions_sql( $query, $query_type ); |
|
587 | + |
|
588 | + return ( 'count' === $query_type ) ? $wpdb->get_var( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoSql, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Get a count of all actions in the store, grouped by status. |
|
593 | + * |
|
594 | + * @return array Set of 'status' => int $count pairs for statuses with 1 or more actions of that status. |
|
595 | + */ |
|
596 | + public function action_counts() { |
|
597 | + global $wpdb; |
|
598 | + |
|
599 | + $sql = "SELECT a.status, count(a.status) as 'count'"; |
|
600 | + $sql .= " FROM {$wpdb->actionscheduler_actions} a"; |
|
601 | + $sql .= ' GROUP BY a.status'; |
|
602 | + |
|
603 | + $actions_count_by_status = array(); |
|
604 | + $action_stati_and_labels = $this->get_status_labels(); |
|
605 | + |
|
606 | + foreach ( $wpdb->get_results( $sql ) as $action_data ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
607 | + // Ignore any actions with invalid status. |
|
608 | + if ( array_key_exists( $action_data->status, $action_stati_and_labels ) ) { |
|
609 | + $actions_count_by_status[ $action_data->status ] = $action_data->count; |
|
610 | + } |
|
611 | + } |
|
612 | + |
|
613 | + return $actions_count_by_status; |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Cancel an action. |
|
618 | + * |
|
619 | + * @param int $action_id Action ID. |
|
620 | + * |
|
621 | + * @return void |
|
622 | + * @throws \InvalidArgumentException If the action update failed. |
|
623 | + */ |
|
624 | + public function cancel_action( $action_id ) { |
|
625 | + /** @var \wpdb $wpdb */ |
|
626 | + global $wpdb; |
|
627 | + |
|
628 | + $updated = $wpdb->update( |
|
629 | + $wpdb->actionscheduler_actions, |
|
630 | + array( 'status' => self::STATUS_CANCELED ), |
|
631 | + array( 'action_id' => $action_id ), |
|
632 | + array( '%s' ), |
|
633 | + array( '%d' ) |
|
634 | + ); |
|
635 | + if ( false === $updated ) { |
|
636 | + /* translators: %s: action ID */ |
|
637 | + throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); |
|
638 | + } |
|
639 | + do_action( 'action_scheduler_canceled_action', $action_id ); |
|
640 | + } |
|
641 | + |
|
642 | + /** |
|
643 | + * Cancel pending actions by hook. |
|
644 | + * |
|
645 | + * @since 3.0.0 |
|
646 | + * |
|
647 | + * @param string $hook Hook name. |
|
648 | + * |
|
649 | + * @return void |
|
650 | + */ |
|
651 | + public function cancel_actions_by_hook( $hook ) { |
|
652 | + $this->bulk_cancel_actions( array( 'hook' => $hook ) ); |
|
653 | + } |
|
654 | + |
|
655 | + /** |
|
656 | + * Cancel pending actions by group. |
|
657 | + * |
|
658 | + * @param string $group Group slug. |
|
659 | + * |
|
660 | + * @return void |
|
661 | + */ |
|
662 | + public function cancel_actions_by_group( $group ) { |
|
663 | + $this->bulk_cancel_actions( array( 'group' => $group ) ); |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * Bulk cancel actions. |
|
668 | + * |
|
669 | + * @since 3.0.0 |
|
670 | + * |
|
671 | + * @param array $query_args Query parameters. |
|
672 | + */ |
|
673 | + protected function bulk_cancel_actions( $query_args ) { |
|
674 | + /** @var \wpdb $wpdb */ |
|
675 | + global $wpdb; |
|
676 | + |
|
677 | + if ( ! is_array( $query_args ) ) { |
|
678 | + return; |
|
679 | + } |
|
680 | + |
|
681 | + // Don't cancel actions that are already canceled. |
|
682 | + if ( isset( $query_args['status'] ) && self::STATUS_CANCELED === $query_args['status'] ) { |
|
683 | + return; |
|
684 | + } |
|
685 | + |
|
686 | + $action_ids = true; |
|
687 | + $query_args = wp_parse_args( |
|
688 | + $query_args, |
|
689 | + array( |
|
690 | + 'per_page' => 1000, |
|
691 | + 'status' => self::STATUS_PENDING, |
|
692 | + 'orderby' => 'action_id', |
|
693 | + ) |
|
694 | + ); |
|
695 | + |
|
696 | + while ( $action_ids ) { |
|
697 | + $action_ids = $this->query_actions( $query_args ); |
|
698 | + if ( empty( $action_ids ) ) { |
|
699 | + break; |
|
700 | + } |
|
701 | + |
|
702 | + $format = array_fill( 0, count( $action_ids ), '%d' ); |
|
703 | + $query_in = '(' . implode( ',', $format ) . ')'; |
|
704 | + $parameters = $action_ids; |
|
705 | + array_unshift( $parameters, self::STATUS_CANCELED ); |
|
706 | + |
|
707 | + $wpdb->query( |
|
708 | + $wpdb->prepare( |
|
709 | + "UPDATE {$wpdb->actionscheduler_actions} SET status = %s WHERE action_id IN {$query_in}", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
710 | + $parameters |
|
711 | + ) |
|
712 | + ); |
|
713 | + |
|
714 | + do_action( 'action_scheduler_bulk_cancel_actions', $action_ids ); |
|
715 | + } |
|
716 | + } |
|
717 | + |
|
718 | + /** |
|
719 | + * Delete an action. |
|
720 | + * |
|
721 | + * @param int $action_id Action ID. |
|
722 | + * @throws \InvalidArgumentException If the action deletion failed. |
|
723 | + */ |
|
724 | + public function delete_action( $action_id ) { |
|
725 | + /** @var \wpdb $wpdb */ |
|
726 | + global $wpdb; |
|
727 | + $deleted = $wpdb->delete( $wpdb->actionscheduler_actions, array( 'action_id' => $action_id ), array( '%d' ) ); |
|
728 | + if ( empty( $deleted ) ) { |
|
729 | + throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
730 | + } |
|
731 | + do_action( 'action_scheduler_deleted_action', $action_id ); |
|
732 | + } |
|
733 | + |
|
734 | + /** |
|
735 | + * Get the schedule date for an action. |
|
736 | + * |
|
737 | + * @param string $action_id Action ID. |
|
738 | + * |
|
739 | + * @return \DateTime The local date the action is scheduled to run, or the date that it ran. |
|
740 | + */ |
|
741 | + public function get_date( $action_id ) { |
|
742 | + $date = $this->get_date_gmt( $action_id ); |
|
743 | + ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
744 | + return $date; |
|
745 | + } |
|
746 | + |
|
747 | + /** |
|
748 | + * Get the GMT schedule date for an action. |
|
749 | + * |
|
750 | + * @param int $action_id Action ID. |
|
751 | + * |
|
752 | + * @throws \InvalidArgumentException If action cannot be identified. |
|
753 | + * @return \DateTime The GMT date the action is scheduled to run, or the date that it ran. |
|
754 | + */ |
|
755 | + protected function get_date_gmt( $action_id ) { |
|
756 | + /** @var \wpdb $wpdb */ |
|
757 | + global $wpdb; |
|
758 | + $record = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d", $action_id ) ); |
|
759 | + if ( empty( $record ) ) { |
|
760 | + throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
761 | + } |
|
762 | + if ( self::STATUS_PENDING === $record->status ) { |
|
763 | + return as_get_datetime_object( $record->scheduled_date_gmt ); |
|
764 | + } else { |
|
765 | + return as_get_datetime_object( $record->last_attempt_gmt ); |
|
766 | + } |
|
767 | + } |
|
768 | + |
|
769 | + /** |
|
770 | + * Stake a claim on actions. |
|
771 | + * |
|
772 | + * @param int $max_actions Maximum number of action to include in claim. |
|
773 | + * @param \DateTime $before_date Jobs must be schedule before this date. Defaults to now. |
|
774 | + * @param array $hooks Hooks to filter for. |
|
775 | + * @param string $group Group to filter for. |
|
776 | + * |
|
777 | + * @return ActionScheduler_ActionClaim |
|
778 | + */ |
|
779 | + public function stake_claim( $max_actions = 10, \DateTime $before_date = null, $hooks = array(), $group = '' ) { |
|
780 | + $claim_id = $this->generate_claim_id(); |
|
781 | + |
|
782 | + $this->claim_before_date = $before_date; |
|
783 | + $this->claim_actions( $claim_id, $max_actions, $before_date, $hooks, $group ); |
|
784 | + $action_ids = $this->find_actions_by_claim_id( $claim_id ); |
|
785 | + $this->claim_before_date = null; |
|
786 | + |
|
787 | + return new ActionScheduler_ActionClaim( $claim_id, $action_ids ); |
|
788 | + } |
|
789 | + |
|
790 | + /** |
|
791 | + * Generate a new action claim. |
|
792 | + * |
|
793 | + * @return int Claim ID. |
|
794 | + */ |
|
795 | + protected function generate_claim_id() { |
|
796 | + /** @var \wpdb $wpdb */ |
|
797 | + global $wpdb; |
|
798 | + $now = as_get_datetime_object(); |
|
799 | + $wpdb->insert( $wpdb->actionscheduler_claims, array( 'date_created_gmt' => $now->format( 'Y-m-d H:i:s' ) ) ); |
|
800 | + |
|
801 | + return $wpdb->insert_id; |
|
802 | + } |
|
803 | + |
|
804 | + /** |
|
805 | + * Mark actions claimed. |
|
806 | + * |
|
807 | + * @param string $claim_id Claim Id. |
|
808 | + * @param int $limit Number of action to include in claim. |
|
809 | + * @param \DateTime $before_date Should use UTC timezone. |
|
810 | + * @param array $hooks Hooks to filter for. |
|
811 | + * @param string $group Group to filter for. |
|
812 | + * |
|
813 | + * @return int The number of actions that were claimed. |
|
814 | + * @throws \InvalidArgumentException Throws InvalidArgumentException if group doesn't exist. |
|
815 | + * @throws \RuntimeException Throws RuntimeException if unable to claim action. |
|
816 | + */ |
|
817 | + protected function claim_actions( $claim_id, $limit, \DateTime $before_date = null, $hooks = array(), $group = '' ) { |
|
818 | + /** @var \wpdb $wpdb */ |
|
819 | + global $wpdb; |
|
820 | + |
|
821 | + $now = as_get_datetime_object(); |
|
822 | + $date = $before_date === null ? $now : clone $before_date; |
|
823 | + |
|
824 | + // can't use $wpdb->update() because of the <= condition. |
|
825 | + $update = "UPDATE {$wpdb->actionscheduler_actions} SET claim_id=%d, last_attempt_gmt=%s, last_attempt_local=%s"; |
|
826 | + $params = array( |
|
827 | + $claim_id, |
|
828 | + $now->format( 'Y-m-d H:i:s' ), |
|
829 | + current_time( 'mysql' ), |
|
830 | + ); |
|
831 | + |
|
832 | + $where = 'WHERE claim_id = 0 AND scheduled_date_gmt <= %s AND status=%s'; |
|
833 | + $params[] = $date->format( 'Y-m-d H:i:s' ); |
|
834 | + $params[] = self::STATUS_PENDING; |
|
835 | + |
|
836 | + if ( ! empty( $hooks ) ) { |
|
837 | + $placeholders = array_fill( 0, count( $hooks ), '%s' ); |
|
838 | + $where .= ' AND hook IN (' . join( ', ', $placeholders ) . ')'; |
|
839 | + $params = array_merge( $params, array_values( $hooks ) ); |
|
840 | + } |
|
841 | + |
|
842 | + if ( ! empty( $group ) ) { |
|
843 | + |
|
844 | + $group_id = $this->get_group_id( $group, false ); |
|
845 | + |
|
846 | + // throw exception if no matching group found, this matches ActionScheduler_wpPostStore's behaviour. |
|
847 | + if ( empty( $group_id ) ) { |
|
848 | + /* translators: %s: group name */ |
|
849 | + throw new InvalidArgumentException( sprintf( __( 'The group "%s" does not exist.', 'action-scheduler' ), $group ) ); |
|
850 | + } |
|
851 | + |
|
852 | + $where .= ' AND group_id = %d'; |
|
853 | + $params[] = $group_id; |
|
854 | + } |
|
855 | + |
|
856 | + /** |
|
857 | + * Sets the order-by clause used in the action claim query. |
|
858 | + * |
|
859 | + * @since 3.4.0 |
|
860 | + * |
|
861 | + * @param string $order_by_sql |
|
862 | + */ |
|
863 | + $order = apply_filters( 'action_scheduler_claim_actions_order_by', 'ORDER BY attempts ASC, scheduled_date_gmt ASC, action_id ASC' ); |
|
864 | + $params[] = $limit; |
|
865 | + |
|
866 | + $sql = $wpdb->prepare( "{$update} {$where} {$order} LIMIT %d", $params ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders |
|
867 | + $rows_affected = $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
868 | + if ( false === $rows_affected ) { |
|
869 | + throw new \RuntimeException( __( 'Unable to claim actions. Database error.', 'action-scheduler' ) ); |
|
870 | + } |
|
871 | + |
|
872 | + return (int) $rows_affected; |
|
873 | + } |
|
874 | + |
|
875 | + /** |
|
876 | + * Get the number of active claims. |
|
877 | + * |
|
878 | + * @return int |
|
879 | + */ |
|
880 | + public function get_claim_count() { |
|
881 | + global $wpdb; |
|
882 | + |
|
883 | + $sql = "SELECT COUNT(DISTINCT claim_id) FROM {$wpdb->actionscheduler_actions} WHERE claim_id != 0 AND status IN ( %s, %s)"; |
|
884 | + $sql = $wpdb->prepare( $sql, array( self::STATUS_PENDING, self::STATUS_RUNNING ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
885 | + |
|
886 | + return (int) $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
887 | + } |
|
888 | + |
|
889 | + /** |
|
890 | + * Return an action's claim ID, as stored in the claim_id column. |
|
891 | + * |
|
892 | + * @param string $action_id Action ID. |
|
893 | + * @return mixed |
|
894 | + */ |
|
895 | + public function get_claim_id( $action_id ) { |
|
896 | + /** @var \wpdb $wpdb */ |
|
897 | + global $wpdb; |
|
898 | + |
|
899 | + $sql = "SELECT claim_id FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d"; |
|
900 | + $sql = $wpdb->prepare( $sql, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
901 | + |
|
902 | + return (int) $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
903 | + } |
|
904 | + |
|
905 | + /** |
|
906 | + * Retrieve the action IDs of action in a claim. |
|
907 | + * |
|
908 | + * @param int $claim_id Claim ID. |
|
909 | + * @return int[] |
|
910 | + */ |
|
911 | + public function find_actions_by_claim_id( $claim_id ) { |
|
912 | + /** @var \wpdb $wpdb */ |
|
913 | + global $wpdb; |
|
914 | + |
|
915 | + $action_ids = array(); |
|
916 | + $before_date = isset( $this->claim_before_date ) ? $this->claim_before_date : as_get_datetime_object(); |
|
917 | + $cut_off = $before_date->format( 'Y-m-d H:i:s' ); |
|
918 | + |
|
919 | + $sql = $wpdb->prepare( |
|
920 | + "SELECT action_id, scheduled_date_gmt FROM {$wpdb->actionscheduler_actions} WHERE claim_id = %d", |
|
921 | + $claim_id |
|
922 | + ); |
|
923 | + |
|
924 | + // Verify that the scheduled date for each action is within the expected bounds (in some unusual |
|
925 | + // cases, we cannot depend on MySQL to honor all of the WHERE conditions we specify). |
|
926 | + foreach ( $wpdb->get_results( $sql ) as $claimed_action ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
927 | + if ( $claimed_action->scheduled_date_gmt <= $cut_off ) { |
|
928 | + $action_ids[] = absint( $claimed_action->action_id ); |
|
929 | + } |
|
930 | + } |
|
931 | + |
|
932 | + return $action_ids; |
|
933 | + } |
|
934 | + |
|
935 | + /** |
|
936 | + * Release actions from a claim and delete the claim. |
|
937 | + * |
|
938 | + * @param ActionScheduler_ActionClaim $claim Claim object. |
|
939 | + */ |
|
940 | + public function release_claim( ActionScheduler_ActionClaim $claim ) { |
|
941 | + /** @var \wpdb $wpdb */ |
|
942 | + global $wpdb; |
|
943 | + $wpdb->update( $wpdb->actionscheduler_actions, array( 'claim_id' => 0 ), array( 'claim_id' => $claim->get_id() ), array( '%d' ), array( '%d' ) ); |
|
944 | + $wpdb->delete( $wpdb->actionscheduler_claims, array( 'claim_id' => $claim->get_id() ), array( '%d' ) ); |
|
945 | + } |
|
946 | + |
|
947 | + /** |
|
948 | + * Remove the claim from an action. |
|
949 | + * |
|
950 | + * @param int $action_id Action ID. |
|
951 | + * |
|
952 | + * @return void |
|
953 | + */ |
|
954 | + public function unclaim_action( $action_id ) { |
|
955 | + /** @var \wpdb $wpdb */ |
|
956 | + global $wpdb; |
|
957 | + $wpdb->update( |
|
958 | + $wpdb->actionscheduler_actions, |
|
959 | + array( 'claim_id' => 0 ), |
|
960 | + array( 'action_id' => $action_id ), |
|
961 | + array( '%s' ), |
|
962 | + array( '%d' ) |
|
963 | + ); |
|
964 | + } |
|
965 | + |
|
966 | + /** |
|
967 | + * Mark an action as failed. |
|
968 | + * |
|
969 | + * @param int $action_id Action ID. |
|
970 | + * @throws \InvalidArgumentException Throw an exception if action was not updated. |
|
971 | + */ |
|
972 | + public function mark_failure( $action_id ) { |
|
973 | + /** @var \wpdb $wpdb */ |
|
974 | + global $wpdb; |
|
975 | + $updated = $wpdb->update( |
|
976 | + $wpdb->actionscheduler_actions, |
|
977 | + array( 'status' => self::STATUS_FAILED ), |
|
978 | + array( 'action_id' => $action_id ), |
|
979 | + array( '%s' ), |
|
980 | + array( '%d' ) |
|
981 | + ); |
|
982 | + if ( empty( $updated ) ) { |
|
983 | + throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
984 | + } |
|
985 | + } |
|
986 | + |
|
987 | + /** |
|
988 | + * Add execution message to action log. |
|
989 | + * |
|
990 | + * @param int $action_id Action ID. |
|
991 | + * |
|
992 | + * @return void |
|
993 | + */ |
|
994 | + public function log_execution( $action_id ) { |
|
995 | + /** @var \wpdb $wpdb */ |
|
996 | + global $wpdb; |
|
997 | + |
|
998 | + $sql = "UPDATE {$wpdb->actionscheduler_actions} SET attempts = attempts+1, status=%s, last_attempt_gmt = %s, last_attempt_local = %s WHERE action_id = %d"; |
|
999 | + $sql = $wpdb->prepare( $sql, self::STATUS_RUNNING, current_time( 'mysql', true ), current_time( 'mysql' ), $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1000 | + $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1001 | + } |
|
1002 | + |
|
1003 | + /** |
|
1004 | + * Mark an action as complete. |
|
1005 | + * |
|
1006 | + * @param int $action_id Action ID. |
|
1007 | + * |
|
1008 | + * @return void |
|
1009 | + * @throws \InvalidArgumentException Throw an exception if action was not updated. |
|
1010 | + */ |
|
1011 | + public function mark_complete( $action_id ) { |
|
1012 | + /** @var \wpdb $wpdb */ |
|
1013 | + global $wpdb; |
|
1014 | + $updated = $wpdb->update( |
|
1015 | + $wpdb->actionscheduler_actions, |
|
1016 | + array( |
|
1017 | + 'status' => self::STATUS_COMPLETE, |
|
1018 | + 'last_attempt_gmt' => current_time( 'mysql', true ), |
|
1019 | + 'last_attempt_local' => current_time( 'mysql' ), |
|
1020 | + ), |
|
1021 | + array( 'action_id' => $action_id ), |
|
1022 | + array( '%s' ), |
|
1023 | + array( '%d' ) |
|
1024 | + ); |
|
1025 | + if ( empty( $updated ) ) { |
|
1026 | + throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
1027 | + } |
|
1028 | + |
|
1029 | + /** |
|
1030 | + * Fires after a scheduled action has been completed. |
|
1031 | + * |
|
1032 | + * @since 3.4.2 |
|
1033 | + * |
|
1034 | + * @param int $action_id Action ID. |
|
1035 | + */ |
|
1036 | + do_action( 'action_scheduler_completed_action', $action_id ); |
|
1037 | + } |
|
1038 | + |
|
1039 | + /** |
|
1040 | + * Get an action's status. |
|
1041 | + * |
|
1042 | + * @param int $action_id Action ID. |
|
1043 | + * |
|
1044 | + * @return string |
|
1045 | + * @throws \InvalidArgumentException Throw an exception if not status was found for action_id. |
|
1046 | + * @throws \RuntimeException Throw an exception if action status could not be retrieved. |
|
1047 | + */ |
|
1048 | + public function get_status( $action_id ) { |
|
1049 | + /** @var \wpdb $wpdb */ |
|
1050 | + global $wpdb; |
|
1051 | + $sql = "SELECT status FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d"; |
|
1052 | + $sql = $wpdb->prepare( $sql, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1053 | + $status = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1054 | + |
|
1055 | + if ( null === $status ) { |
|
1056 | + throw new \InvalidArgumentException( __( 'Invalid action ID. No status found.', 'action-scheduler' ) ); |
|
1057 | + } elseif ( empty( $status ) ) { |
|
1058 | + throw new \RuntimeException( __( 'Unknown status found for action.', 'action-scheduler' ) ); |
|
1059 | + } else { |
|
1060 | + return $status; |
|
1061 | + } |
|
1062 | + } |
|
1063 | 1063 | } |
@@ -45,8 +45,8 @@ discard block |
||
45 | 45 | * @return int Action ID. |
46 | 46 | * @throws RuntimeException Throws exception when saving the action fails. |
47 | 47 | */ |
48 | - public function save_unique_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { |
|
49 | - return $this->save_action_to_db( $action, $scheduled_date, true ); |
|
48 | + public function save_unique_action(ActionScheduler_Action $action, \DateTime $scheduled_date = null) { |
|
49 | + return $this->save_action_to_db($action, $scheduled_date, true); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | /** |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | * @return int Action ID. |
59 | 59 | * @throws RuntimeException Throws exception when saving the action fails. |
60 | 60 | */ |
61 | - public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null ) { |
|
62 | - return $this->save_action_to_db( $action, $scheduled_date, false ); |
|
61 | + public function save_action(ActionScheduler_Action $action, \DateTime $scheduled_date = null) { |
|
62 | + return $this->save_action_to_db($action, $scheduled_date, false); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -72,50 +72,50 @@ discard block |
||
72 | 72 | * @return int Action ID. |
73 | 73 | * @throws RuntimeException Throws exception when saving the action fails. |
74 | 74 | */ |
75 | - private function save_action_to_db( ActionScheduler_Action $action, DateTime $date = null, $unique = false ) { |
|
75 | + private function save_action_to_db(ActionScheduler_Action $action, DateTime $date = null, $unique = false) { |
|
76 | 76 | global $wpdb; |
77 | 77 | |
78 | 78 | try { |
79 | - $this->validate_action( $action ); |
|
79 | + $this->validate_action($action); |
|
80 | 80 | |
81 | 81 | $data = array( |
82 | 82 | 'hook' => $action->get_hook(), |
83 | - 'status' => ( $action->is_finished() ? self::STATUS_COMPLETE : self::STATUS_PENDING ), |
|
84 | - 'scheduled_date_gmt' => $this->get_scheduled_date_string( $action, $date ), |
|
85 | - 'scheduled_date_local' => $this->get_scheduled_date_string_local( $action, $date ), |
|
86 | - 'schedule' => serialize( $action->get_schedule() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
87 | - 'group_id' => $this->get_group_id( $action->get_group() ), |
|
83 | + 'status' => ($action->is_finished() ? self::STATUS_COMPLETE : self::STATUS_PENDING), |
|
84 | + 'scheduled_date_gmt' => $this->get_scheduled_date_string($action, $date), |
|
85 | + 'scheduled_date_local' => $this->get_scheduled_date_string_local($action, $date), |
|
86 | + 'schedule' => serialize($action->get_schedule()), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
87 | + 'group_id' => $this->get_group_id($action->get_group()), |
|
88 | 88 | ); |
89 | 89 | |
90 | - $args = wp_json_encode( $action->get_args() ); |
|
91 | - if ( strlen( $args ) <= static::$max_index_length ) { |
|
90 | + $args = wp_json_encode($action->get_args()); |
|
91 | + if (strlen($args) <= static::$max_index_length) { |
|
92 | 92 | $data['args'] = $args; |
93 | 93 | } else { |
94 | - $data['args'] = $this->hash_args( $args ); |
|
94 | + $data['args'] = $this->hash_args($args); |
|
95 | 95 | $data['extended_args'] = $args; |
96 | 96 | } |
97 | 97 | |
98 | - $insert_sql = $this->build_insert_sql( $data, $unique ); |
|
98 | + $insert_sql = $this->build_insert_sql($data, $unique); |
|
99 | 99 | |
100 | 100 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $insert_sql should be already prepared. |
101 | - $wpdb->query( $insert_sql ); |
|
101 | + $wpdb->query($insert_sql); |
|
102 | 102 | $action_id = $wpdb->insert_id; |
103 | 103 | |
104 | - if ( is_wp_error( $action_id ) ) { |
|
105 | - throw new \RuntimeException( $action_id->get_error_message() ); |
|
106 | - } elseif ( empty( $action_id ) ) { |
|
107 | - if ( $unique ) { |
|
104 | + if (is_wp_error($action_id)) { |
|
105 | + throw new \RuntimeException($action_id->get_error_message()); |
|
106 | + } elseif (empty($action_id)) { |
|
107 | + if ($unique) { |
|
108 | 108 | return 0; |
109 | 109 | } |
110 | - throw new \RuntimeException( $wpdb->last_error ? $wpdb->last_error : __( 'Database error.', 'action-scheduler' ) ); |
|
110 | + throw new \RuntimeException($wpdb->last_error ? $wpdb->last_error : __('Database error.', 'action-scheduler')); |
|
111 | 111 | } |
112 | 112 | |
113 | - do_action( 'action_scheduler_stored_action', $action_id ); |
|
113 | + do_action('action_scheduler_stored_action', $action_id); |
|
114 | 114 | |
115 | 115 | return $action_id; |
116 | - } catch ( \Exception $e ) { |
|
116 | + } catch (\Exception $e) { |
|
117 | 117 | /* translators: %s: error message */ |
118 | - throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
|
118 | + throw new \RuntimeException(sprintf(__('Error saving action: %s', 'action-scheduler'), $e->getMessage()), 0); |
|
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
@@ -127,17 +127,17 @@ discard block |
||
127 | 127 | * |
128 | 128 | * @return string Insert query. |
129 | 129 | */ |
130 | - private function build_insert_sql( array $data, $unique ) { |
|
130 | + private function build_insert_sql(array $data, $unique) { |
|
131 | 131 | global $wpdb; |
132 | - $columns = array_keys( $data ); |
|
133 | - $values = array_values( $data ); |
|
134 | - $placeholders = array_map( array( $this, 'get_placeholder_for_column' ), $columns ); |
|
132 | + $columns = array_keys($data); |
|
133 | + $values = array_values($data); |
|
134 | + $placeholders = array_map(array($this, 'get_placeholder_for_column'), $columns); |
|
135 | 135 | |
136 | - $table_name = ! empty( $wpdb->actionscheduler_actions ) ? $wpdb->actionscheduler_actions : $wpdb->prefix . 'actionscheduler_actions'; |
|
136 | + $table_name = ! empty($wpdb->actionscheduler_actions) ? $wpdb->actionscheduler_actions : $wpdb->prefix.'actionscheduler_actions'; |
|
137 | 137 | |
138 | - $column_sql = '`' . implode( '`, `', $columns ) . '`'; |
|
139 | - $placeholder_sql = implode( ', ', $placeholders ); |
|
140 | - $where_clause = $this->build_where_clause_for_insert( $data, $table_name, $unique ); |
|
138 | + $column_sql = '`'.implode('`, `', $columns).'`'; |
|
139 | + $placeholder_sql = implode(', ', $placeholders); |
|
140 | + $where_clause = $this->build_where_clause_for_insert($data, $table_name, $unique); |
|
141 | 141 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $column_sql and $where_clause are already prepared. $placeholder_sql is hardcoded. |
142 | 142 | $insert_query = $wpdb->prepare( |
143 | 143 | " |
@@ -160,18 +160,18 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @return string Where clause to be used with insert. |
162 | 162 | */ |
163 | - private function build_where_clause_for_insert( $data, $table_name, $unique ) { |
|
163 | + private function build_where_clause_for_insert($data, $table_name, $unique) { |
|
164 | 164 | global $wpdb; |
165 | 165 | |
166 | - if ( ! $unique ) { |
|
166 | + if ( ! $unique) { |
|
167 | 167 | return 'SELECT NULL FROM DUAL'; |
168 | 168 | } |
169 | 169 | |
170 | - $pending_statuses = array( |
|
170 | + $pending_statuses = array( |
|
171 | 171 | ActionScheduler_Store::STATUS_PENDING, |
172 | 172 | ActionScheduler_Store::STATUS_RUNNING, |
173 | 173 | ); |
174 | - $pending_status_placeholders = implode( ', ', array_fill( 0, count( $pending_statuses ), '%s' ) ); |
|
174 | + $pending_status_placeholders = implode(', ', array_fill(0, count($pending_statuses), '%s')); |
|
175 | 175 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $pending_status_placeholders is hardcoded. |
176 | 176 | $where_clause = $wpdb->prepare( |
177 | 177 | " |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | ); |
191 | 191 | // phpcs:enable |
192 | 192 | |
193 | - return "$where_clause" . ' LIMIT 1'; |
|
193 | + return "$where_clause".' LIMIT 1'; |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @return string Placeholder to use for given column. |
202 | 202 | */ |
203 | - private function get_placeholder_for_column( $column_name ) { |
|
203 | + private function get_placeholder_for_column($column_name) { |
|
204 | 204 | $string_columns = array( |
205 | 205 | 'hook', |
206 | 206 | 'status', |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | 'extended_args', |
214 | 214 | ); |
215 | 215 | |
216 | - return in_array( $column_name, $string_columns ) ? '%s' : '%d'; |
|
216 | + return in_array($column_name, $string_columns) ? '%s' : '%d'; |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
@@ -222,8 +222,8 @@ discard block |
||
222 | 222 | * @param string $args JSON encoded action args. |
223 | 223 | * @return string |
224 | 224 | */ |
225 | - protected function hash_args( $args ) { |
|
226 | - return md5( $args ); |
|
225 | + protected function hash_args($args) { |
|
226 | + return md5($args); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | /** |
@@ -232,12 +232,12 @@ discard block |
||
232 | 232 | * @param array $args Action args. |
233 | 233 | * @return string |
234 | 234 | */ |
235 | - protected function get_args_for_query( $args ) { |
|
236 | - $encoded = wp_json_encode( $args ); |
|
237 | - if ( strlen( $encoded ) <= static::$max_index_length ) { |
|
235 | + protected function get_args_for_query($args) { |
|
236 | + $encoded = wp_json_encode($args); |
|
237 | + if (strlen($encoded) <= static::$max_index_length) { |
|
238 | 238 | return $encoded; |
239 | 239 | } |
240 | - return $this->hash_args( $encoded ); |
|
240 | + return $this->hash_args($encoded); |
|
241 | 241 | } |
242 | 242 | /** |
243 | 243 | * Get a group's ID based on its name/slug. |
@@ -247,15 +247,15 @@ discard block |
||
247 | 247 | * |
248 | 248 | * @return int The group's ID, if it exists or is created, or 0 if it does not exist and is not created. |
249 | 249 | */ |
250 | - protected function get_group_id( $slug, $create_if_not_exists = true ) { |
|
251 | - if ( empty( $slug ) ) { |
|
250 | + protected function get_group_id($slug, $create_if_not_exists = true) { |
|
251 | + if (empty($slug)) { |
|
252 | 252 | return 0; |
253 | 253 | } |
254 | 254 | /** @var \wpdb $wpdb */ |
255 | 255 | global $wpdb; |
256 | - $group_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$wpdb->actionscheduler_groups} WHERE slug=%s", $slug ) ); |
|
257 | - if ( empty( $group_id ) && $create_if_not_exists ) { |
|
258 | - $group_id = $this->create_group( $slug ); |
|
256 | + $group_id = (int) $wpdb->get_var($wpdb->prepare("SELECT group_id FROM {$wpdb->actionscheduler_groups} WHERE slug=%s", $slug)); |
|
257 | + if (empty($group_id) && $create_if_not_exists) { |
|
258 | + $group_id = $this->create_group($slug); |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | return $group_id; |
@@ -268,10 +268,10 @@ discard block |
||
268 | 268 | * |
269 | 269 | * @return int Group ID. |
270 | 270 | */ |
271 | - protected function create_group( $slug ) { |
|
271 | + protected function create_group($slug) { |
|
272 | 272 | /** @var \wpdb $wpdb */ |
273 | 273 | global $wpdb; |
274 | - $wpdb->insert( $wpdb->actionscheduler_groups, array( 'slug' => $slug ) ); |
|
274 | + $wpdb->insert($wpdb->actionscheduler_groups, array('slug' => $slug)); |
|
275 | 275 | |
276 | 276 | return (int) $wpdb->insert_id; |
277 | 277 | } |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | * |
284 | 284 | * @return ActionScheduler_Action |
285 | 285 | */ |
286 | - public function fetch_action( $action_id ) { |
|
286 | + public function fetch_action($action_id) { |
|
287 | 287 | /** @var \wpdb $wpdb */ |
288 | 288 | global $wpdb; |
289 | 289 | $data = $wpdb->get_row( |
@@ -293,13 +293,13 @@ discard block |
||
293 | 293 | ) |
294 | 294 | ); |
295 | 295 | |
296 | - if ( empty( $data ) ) { |
|
296 | + if (empty($data)) { |
|
297 | 297 | return $this->get_null_action(); |
298 | 298 | } |
299 | 299 | |
300 | - if ( ! empty( $data->extended_args ) ) { |
|
300 | + if ( ! empty($data->extended_args)) { |
|
301 | 301 | $data->args = $data->extended_args; |
302 | - unset( $data->extended_args ); |
|
302 | + unset($data->extended_args); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | // Convert NULL dates to zero dates. |
@@ -309,16 +309,16 @@ discard block |
||
309 | 309 | 'last_attempt_gmt', |
310 | 310 | 'last_attempt_gmt', |
311 | 311 | ); |
312 | - foreach ( $date_fields as $date_field ) { |
|
313 | - if ( $data->$date_field === null ) { |
|
312 | + foreach ($date_fields as $date_field) { |
|
313 | + if ($data->$date_field === null) { |
|
314 | 314 | $data->$date_field = ActionScheduler_StoreSchema::DEFAULT_DATE; |
315 | 315 | } |
316 | 316 | } |
317 | 317 | |
318 | 318 | try { |
319 | - $action = $this->make_action_from_db_record( $data ); |
|
320 | - } catch ( ActionScheduler_InvalidActionException $exception ) { |
|
321 | - do_action( 'action_scheduler_failed_fetch_action', $action_id, $exception ); |
|
319 | + $action = $this->make_action_from_db_record($data); |
|
320 | + } catch (ActionScheduler_InvalidActionException $exception) { |
|
321 | + do_action('action_scheduler_failed_fetch_action', $action_id, $exception); |
|
322 | 322 | return $this->get_null_action(); |
323 | 323 | } |
324 | 324 | |
@@ -341,21 +341,21 @@ discard block |
||
341 | 341 | * |
342 | 342 | * @return ActionScheduler_Action|ActionScheduler_CanceledAction|ActionScheduler_FinishedAction |
343 | 343 | */ |
344 | - protected function make_action_from_db_record( $data ) { |
|
344 | + protected function make_action_from_db_record($data) { |
|
345 | 345 | |
346 | 346 | $hook = $data->hook; |
347 | - $args = json_decode( $data->args, true ); |
|
348 | - $schedule = unserialize( $data->schedule ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
347 | + $args = json_decode($data->args, true); |
|
348 | + $schedule = unserialize($data->schedule); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
349 | 349 | |
350 | - $this->validate_args( $args, $data->action_id ); |
|
351 | - $this->validate_schedule( $schedule, $data->action_id ); |
|
350 | + $this->validate_args($args, $data->action_id); |
|
351 | + $this->validate_schedule($schedule, $data->action_id); |
|
352 | 352 | |
353 | - if ( empty( $schedule ) ) { |
|
353 | + if (empty($schedule)) { |
|
354 | 354 | $schedule = new ActionScheduler_NullSchedule(); |
355 | 355 | } |
356 | 356 | $group = $data->group ? $data->group : ''; |
357 | 357 | |
358 | - return ActionScheduler::factory()->get_stored_action( $data->status, $data->hook, $args, $schedule, $group ); |
|
358 | + return ActionScheduler::factory()->get_stored_action($data->status, $data->hook, $args, $schedule, $group); |
|
359 | 359 | } |
360 | 360 | |
361 | 361 | /** |
@@ -369,10 +369,10 @@ discard block |
||
369 | 369 | * @return string SQL statement already properly escaped. |
370 | 370 | * @throws InvalidArgumentException If the query is invalid. |
371 | 371 | */ |
372 | - protected function get_query_actions_sql( array $query, $select_or_count = 'select' ) { |
|
372 | + protected function get_query_actions_sql(array $query, $select_or_count = 'select') { |
|
373 | 373 | |
374 | - if ( ! in_array( $select_or_count, array( 'select', 'count' ), true ) ) { |
|
375 | - throw new InvalidArgumentException( __( 'Invalid value for select or count parameter. Cannot query actions.', 'action-scheduler' ) ); |
|
374 | + if ( ! in_array($select_or_count, array('select', 'count'), true)) { |
|
375 | + throw new InvalidArgumentException(__('Invalid value for select or count parameter. Cannot query actions.', 'action-scheduler')); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | $query = wp_parse_args( |
@@ -398,42 +398,42 @@ discard block |
||
398 | 398 | /** @var \wpdb $wpdb */ |
399 | 399 | global $wpdb; |
400 | 400 | |
401 | - $db_server_info = is_callable( array( $wpdb, 'db_server_info' ) ) ? $wpdb->db_server_info() : $wpdb->db_version(); |
|
402 | - if ( false !== strpos( $db_server_info, 'MariaDB' ) ) { |
|
401 | + $db_server_info = is_callable(array($wpdb, 'db_server_info')) ? $wpdb->db_server_info() : $wpdb->db_version(); |
|
402 | + if (false !== strpos($db_server_info, 'MariaDB')) { |
|
403 | 403 | $supports_json = version_compare( |
404 | - PHP_VERSION_ID >= 80016 ? $wpdb->db_version() : preg_replace( '/[^0-9.].*/', '', str_replace( '5.5.5-', '', $db_server_info ) ), |
|
404 | + PHP_VERSION_ID >= 80016 ? $wpdb->db_version() : preg_replace('/[^0-9.].*/', '', str_replace('5.5.5-', '', $db_server_info)), |
|
405 | 405 | '10.2', |
406 | 406 | '>=' |
407 | 407 | ); |
408 | 408 | } else { |
409 | - $supports_json = version_compare( $wpdb->db_version(), '5.7', '>=' ); |
|
409 | + $supports_json = version_compare($wpdb->db_version(), '5.7', '>='); |
|
410 | 410 | } |
411 | 411 | |
412 | - $sql = ( 'count' === $select_or_count ) ? 'SELECT count(a.action_id)' : 'SELECT a.action_id'; |
|
412 | + $sql = ('count' === $select_or_count) ? 'SELECT count(a.action_id)' : 'SELECT a.action_id'; |
|
413 | 413 | $sql .= " FROM {$wpdb->actionscheduler_actions} a"; |
414 | 414 | $sql_params = array(); |
415 | 415 | |
416 | - if ( ! empty( $query['group'] ) || 'group' === $query['orderby'] ) { |
|
416 | + if ( ! empty($query['group']) || 'group' === $query['orderby']) { |
|
417 | 417 | $sql .= " LEFT JOIN {$wpdb->actionscheduler_groups} g ON g.group_id=a.group_id"; |
418 | 418 | } |
419 | 419 | |
420 | 420 | $sql .= ' WHERE 1=1'; |
421 | 421 | |
422 | - if ( ! empty( $query['group'] ) ) { |
|
422 | + if ( ! empty($query['group'])) { |
|
423 | 423 | $sql .= ' AND g.slug=%s'; |
424 | 424 | $sql_params[] = $query['group']; |
425 | 425 | } |
426 | 426 | |
427 | - if ( ! empty( $query['hook'] ) ) { |
|
427 | + if ( ! empty($query['hook'])) { |
|
428 | 428 | $sql .= ' AND a.hook=%s'; |
429 | 429 | $sql_params[] = $query['hook']; |
430 | 430 | } |
431 | 431 | |
432 | - if ( $query['args'] !== null ) { |
|
433 | - switch ( $query['partial_args_matching'] ) { |
|
432 | + if ($query['args'] !== null) { |
|
433 | + switch ($query['partial_args_matching']) { |
|
434 | 434 | case 'json': |
435 | - if ( ! $supports_json ) { |
|
436 | - throw new \RuntimeException( __( 'JSON partial matching not supported in your environment. Please check your MySQL/MariaDB version.', 'action-scheduler' ) ); |
|
435 | + if ( ! $supports_json) { |
|
436 | + throw new \RuntimeException(__('JSON partial matching not supported in your environment. Please check your MySQL/MariaDB version.', 'action-scheduler')); |
|
437 | 437 | } |
438 | 438 | $supported_types = array( |
439 | 439 | 'integer' => '%d', |
@@ -441,84 +441,84 @@ discard block |
||
441 | 441 | 'double' => '%f', |
442 | 442 | 'string' => '%s', |
443 | 443 | ); |
444 | - foreach ( $query['args'] as $key => $value ) { |
|
445 | - $value_type = gettype( $value ); |
|
446 | - if ( 'boolean' === $value_type ) { |
|
444 | + foreach ($query['args'] as $key => $value) { |
|
445 | + $value_type = gettype($value); |
|
446 | + if ('boolean' === $value_type) { |
|
447 | 447 | $value = $value ? 'true' : 'false'; |
448 | 448 | } |
449 | - $placeholder = isset( $supported_types[ $value_type ] ) ? $supported_types[ $value_type ] : false; |
|
450 | - if ( ! $placeholder ) { |
|
449 | + $placeholder = isset($supported_types[$value_type]) ? $supported_types[$value_type] : false; |
|
450 | + if ( ! $placeholder) { |
|
451 | 451 | throw new \RuntimeException( |
452 | 452 | sprintf( |
453 | 453 | /* translators: %s: provided value type */ |
454 | - __( 'The value type for the JSON partial matching is not supported. Must be either integer, boolean, double or string. %s type provided.', 'action-scheduler' ), |
|
454 | + __('The value type for the JSON partial matching is not supported. Must be either integer, boolean, double or string. %s type provided.', 'action-scheduler'), |
|
455 | 455 | $value_type |
456 | 456 | ) |
457 | 457 | ); |
458 | 458 | } |
459 | - $sql .= ' AND JSON_EXTRACT(a.args, %s)=' . $placeholder; |
|
460 | - $sql_params[] = '$.' . $key; |
|
459 | + $sql .= ' AND JSON_EXTRACT(a.args, %s)='.$placeholder; |
|
460 | + $sql_params[] = '$.'.$key; |
|
461 | 461 | $sql_params[] = $value; |
462 | 462 | } |
463 | 463 | break; |
464 | 464 | case 'like': |
465 | - foreach ( $query['args'] as $key => $value ) { |
|
465 | + foreach ($query['args'] as $key => $value) { |
|
466 | 466 | $sql .= ' AND a.args LIKE %s'; |
467 | - $json_partial = $wpdb->esc_like( trim( json_encode( array( $key => $value ) ), '{}' ) ); |
|
467 | + $json_partial = $wpdb->esc_like(trim(json_encode(array($key => $value)), '{}')); |
|
468 | 468 | $sql_params[] = "%{$json_partial}%"; |
469 | 469 | } |
470 | 470 | break; |
471 | 471 | case 'off': |
472 | 472 | $sql .= ' AND a.args=%s'; |
473 | - $sql_params[] = $this->get_args_for_query( $query['args'] ); |
|
473 | + $sql_params[] = $this->get_args_for_query($query['args']); |
|
474 | 474 | break; |
475 | 475 | default: |
476 | - throw new \RuntimeException( __( 'Unknown partial args matching value.', 'action-scheduler' ) ); |
|
476 | + throw new \RuntimeException(__('Unknown partial args matching value.', 'action-scheduler')); |
|
477 | 477 | } |
478 | 478 | } |
479 | 479 | |
480 | - if ( $query['status'] ) { |
|
480 | + if ($query['status']) { |
|
481 | 481 | $statuses = (array) $query['status']; |
482 | - $placeholders = array_fill( 0, count( $statuses ), '%s' ); |
|
483 | - $sql .= ' AND a.status IN (' . join( ', ', $placeholders ) . ')'; |
|
484 | - $sql_params = array_merge( $sql_params, array_values( $statuses ) ); |
|
482 | + $placeholders = array_fill(0, count($statuses), '%s'); |
|
483 | + $sql .= ' AND a.status IN ('.join(', ', $placeholders).')'; |
|
484 | + $sql_params = array_merge($sql_params, array_values($statuses)); |
|
485 | 485 | } |
486 | 486 | |
487 | - if ( $query['date'] instanceof \DateTime ) { |
|
487 | + if ($query['date'] instanceof \DateTime) { |
|
488 | 488 | $date = clone $query['date']; |
489 | - $date->setTimezone( new \DateTimeZone( 'UTC' ) ); |
|
490 | - $date_string = $date->format( 'Y-m-d H:i:s' ); |
|
491 | - $comparator = $this->validate_sql_comparator( $query['date_compare'] ); |
|
489 | + $date->setTimezone(new \DateTimeZone('UTC')); |
|
490 | + $date_string = $date->format('Y-m-d H:i:s'); |
|
491 | + $comparator = $this->validate_sql_comparator($query['date_compare']); |
|
492 | 492 | $sql .= " AND a.scheduled_date_gmt $comparator %s"; |
493 | 493 | $sql_params[] = $date_string; |
494 | 494 | } |
495 | 495 | |
496 | - if ( $query['modified'] instanceof \DateTime ) { |
|
496 | + if ($query['modified'] instanceof \DateTime) { |
|
497 | 497 | $modified = clone $query['modified']; |
498 | - $modified->setTimezone( new \DateTimeZone( 'UTC' ) ); |
|
499 | - $date_string = $modified->format( 'Y-m-d H:i:s' ); |
|
500 | - $comparator = $this->validate_sql_comparator( $query['modified_compare'] ); |
|
498 | + $modified->setTimezone(new \DateTimeZone('UTC')); |
|
499 | + $date_string = $modified->format('Y-m-d H:i:s'); |
|
500 | + $comparator = $this->validate_sql_comparator($query['modified_compare']); |
|
501 | 501 | $sql .= " AND a.last_attempt_gmt $comparator %s"; |
502 | 502 | $sql_params[] = $date_string; |
503 | 503 | } |
504 | 504 | |
505 | - if ( true === $query['claimed'] ) { |
|
505 | + if (true === $query['claimed']) { |
|
506 | 506 | $sql .= ' AND a.claim_id != 0'; |
507 | - } elseif ( false === $query['claimed'] ) { |
|
507 | + } elseif (false === $query['claimed']) { |
|
508 | 508 | $sql .= ' AND a.claim_id = 0'; |
509 | - } elseif ( $query['claimed'] !== null ) { |
|
509 | + } elseif ($query['claimed'] !== null) { |
|
510 | 510 | $sql .= ' AND a.claim_id = %d'; |
511 | 511 | $sql_params[] = $query['claimed']; |
512 | 512 | } |
513 | 513 | |
514 | - if ( ! empty( $query['search'] ) ) { |
|
514 | + if ( ! empty($query['search'])) { |
|
515 | 515 | $sql .= ' AND (a.hook LIKE %s OR (a.extended_args IS NULL AND a.args LIKE %s) OR a.extended_args LIKE %s'; |
516 | - for ( $i = 0; $i < 3; $i++ ) { |
|
517 | - $sql_params[] = sprintf( '%%%s%%', $query['search'] ); |
|
516 | + for ($i = 0; $i < 3; $i++) { |
|
517 | + $sql_params[] = sprintf('%%%s%%', $query['search']); |
|
518 | 518 | } |
519 | 519 | |
520 | 520 | $search_claim_id = (int) $query['search']; |
521 | - if ( $search_claim_id ) { |
|
521 | + if ($search_claim_id) { |
|
522 | 522 | $sql .= ' OR a.claim_id = %d'; |
523 | 523 | $sql_params[] = $search_claim_id; |
524 | 524 | } |
@@ -526,13 +526,13 @@ discard block |
||
526 | 526 | $sql .= ')'; |
527 | 527 | } |
528 | 528 | |
529 | - if ( 'select' === $select_or_count ) { |
|
530 | - if ( 'ASC' === strtoupper( $query['order'] ) ) { |
|
529 | + if ('select' === $select_or_count) { |
|
530 | + if ('ASC' === strtoupper($query['order'])) { |
|
531 | 531 | $order = 'ASC'; |
532 | 532 | } else { |
533 | 533 | $order = 'DESC'; |
534 | 534 | } |
535 | - switch ( $query['orderby'] ) { |
|
535 | + switch ($query['orderby']) { |
|
536 | 536 | case 'hook': |
537 | 537 | $sql .= " ORDER BY a.hook $order"; |
538 | 538 | break; |
@@ -553,15 +553,15 @@ discard block |
||
553 | 553 | break; |
554 | 554 | } |
555 | 555 | |
556 | - if ( $query['per_page'] > 0 ) { |
|
556 | + if ($query['per_page'] > 0) { |
|
557 | 557 | $sql .= ' LIMIT %d, %d'; |
558 | 558 | $sql_params[] = $query['offset']; |
559 | 559 | $sql_params[] = $query['per_page']; |
560 | 560 | } |
561 | 561 | } |
562 | 562 | |
563 | - if ( ! empty( $sql_params ) ) { |
|
564 | - $sql = $wpdb->prepare( $sql, $sql_params ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
563 | + if ( ! empty($sql_params)) { |
|
564 | + $sql = $wpdb->prepare($sql, $sql_params); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
565 | 565 | } |
566 | 566 | |
567 | 567 | return $sql; |
@@ -579,13 +579,13 @@ discard block |
||
579 | 579 | * |
580 | 580 | * @return string|array|null The IDs of actions matching the query. Null on failure. |
581 | 581 | */ |
582 | - public function query_actions( $query = array(), $query_type = 'select' ) { |
|
582 | + public function query_actions($query = array(), $query_type = 'select') { |
|
583 | 583 | /** @var wpdb $wpdb */ |
584 | 584 | global $wpdb; |
585 | 585 | |
586 | - $sql = $this->get_query_actions_sql( $query, $query_type ); |
|
586 | + $sql = $this->get_query_actions_sql($query, $query_type); |
|
587 | 587 | |
588 | - return ( 'count' === $query_type ) ? $wpdb->get_var( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoSql, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
588 | + return ('count' === $query_type) ? $wpdb->get_var($sql) : $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoSql, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
589 | 589 | } |
590 | 590 | |
591 | 591 | /** |
@@ -603,10 +603,10 @@ discard block |
||
603 | 603 | $actions_count_by_status = array(); |
604 | 604 | $action_stati_and_labels = $this->get_status_labels(); |
605 | 605 | |
606 | - foreach ( $wpdb->get_results( $sql ) as $action_data ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
606 | + foreach ($wpdb->get_results($sql) as $action_data) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
607 | 607 | // Ignore any actions with invalid status. |
608 | - if ( array_key_exists( $action_data->status, $action_stati_and_labels ) ) { |
|
609 | - $actions_count_by_status[ $action_data->status ] = $action_data->count; |
|
608 | + if (array_key_exists($action_data->status, $action_stati_and_labels)) { |
|
609 | + $actions_count_by_status[$action_data->status] = $action_data->count; |
|
610 | 610 | } |
611 | 611 | } |
612 | 612 | |
@@ -621,22 +621,22 @@ discard block |
||
621 | 621 | * @return void |
622 | 622 | * @throws \InvalidArgumentException If the action update failed. |
623 | 623 | */ |
624 | - public function cancel_action( $action_id ) { |
|
624 | + public function cancel_action($action_id) { |
|
625 | 625 | /** @var \wpdb $wpdb */ |
626 | 626 | global $wpdb; |
627 | 627 | |
628 | 628 | $updated = $wpdb->update( |
629 | 629 | $wpdb->actionscheduler_actions, |
630 | - array( 'status' => self::STATUS_CANCELED ), |
|
631 | - array( 'action_id' => $action_id ), |
|
632 | - array( '%s' ), |
|
633 | - array( '%d' ) |
|
630 | + array('status' => self::STATUS_CANCELED), |
|
631 | + array('action_id' => $action_id), |
|
632 | + array('%s'), |
|
633 | + array('%d') |
|
634 | 634 | ); |
635 | - if ( false === $updated ) { |
|
635 | + if (false === $updated) { |
|
636 | 636 | /* translators: %s: action ID */ |
637 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); |
|
637 | + throw new \InvalidArgumentException(sprintf(__('Unidentified action %s', 'action-scheduler'), $action_id)); |
|
638 | 638 | } |
639 | - do_action( 'action_scheduler_canceled_action', $action_id ); |
|
639 | + do_action('action_scheduler_canceled_action', $action_id); |
|
640 | 640 | } |
641 | 641 | |
642 | 642 | /** |
@@ -648,8 +648,8 @@ discard block |
||
648 | 648 | * |
649 | 649 | * @return void |
650 | 650 | */ |
651 | - public function cancel_actions_by_hook( $hook ) { |
|
652 | - $this->bulk_cancel_actions( array( 'hook' => $hook ) ); |
|
651 | + public function cancel_actions_by_hook($hook) { |
|
652 | + $this->bulk_cancel_actions(array('hook' => $hook)); |
|
653 | 653 | } |
654 | 654 | |
655 | 655 | /** |
@@ -659,8 +659,8 @@ discard block |
||
659 | 659 | * |
660 | 660 | * @return void |
661 | 661 | */ |
662 | - public function cancel_actions_by_group( $group ) { |
|
663 | - $this->bulk_cancel_actions( array( 'group' => $group ) ); |
|
662 | + public function cancel_actions_by_group($group) { |
|
663 | + $this->bulk_cancel_actions(array('group' => $group)); |
|
664 | 664 | } |
665 | 665 | |
666 | 666 | /** |
@@ -670,16 +670,16 @@ discard block |
||
670 | 670 | * |
671 | 671 | * @param array $query_args Query parameters. |
672 | 672 | */ |
673 | - protected function bulk_cancel_actions( $query_args ) { |
|
673 | + protected function bulk_cancel_actions($query_args) { |
|
674 | 674 | /** @var \wpdb $wpdb */ |
675 | 675 | global $wpdb; |
676 | 676 | |
677 | - if ( ! is_array( $query_args ) ) { |
|
677 | + if ( ! is_array($query_args)) { |
|
678 | 678 | return; |
679 | 679 | } |
680 | 680 | |
681 | 681 | // Don't cancel actions that are already canceled. |
682 | - if ( isset( $query_args['status'] ) && self::STATUS_CANCELED === $query_args['status'] ) { |
|
682 | + if (isset($query_args['status']) && self::STATUS_CANCELED === $query_args['status']) { |
|
683 | 683 | return; |
684 | 684 | } |
685 | 685 | |
@@ -693,16 +693,16 @@ discard block |
||
693 | 693 | ) |
694 | 694 | ); |
695 | 695 | |
696 | - while ( $action_ids ) { |
|
697 | - $action_ids = $this->query_actions( $query_args ); |
|
698 | - if ( empty( $action_ids ) ) { |
|
696 | + while ($action_ids) { |
|
697 | + $action_ids = $this->query_actions($query_args); |
|
698 | + if (empty($action_ids)) { |
|
699 | 699 | break; |
700 | 700 | } |
701 | 701 | |
702 | - $format = array_fill( 0, count( $action_ids ), '%d' ); |
|
703 | - $query_in = '(' . implode( ',', $format ) . ')'; |
|
702 | + $format = array_fill(0, count($action_ids), '%d'); |
|
703 | + $query_in = '('.implode(',', $format).')'; |
|
704 | 704 | $parameters = $action_ids; |
705 | - array_unshift( $parameters, self::STATUS_CANCELED ); |
|
705 | + array_unshift($parameters, self::STATUS_CANCELED); |
|
706 | 706 | |
707 | 707 | $wpdb->query( |
708 | 708 | $wpdb->prepare( |
@@ -711,7 +711,7 @@ discard block |
||
711 | 711 | ) |
712 | 712 | ); |
713 | 713 | |
714 | - do_action( 'action_scheduler_bulk_cancel_actions', $action_ids ); |
|
714 | + do_action('action_scheduler_bulk_cancel_actions', $action_ids); |
|
715 | 715 | } |
716 | 716 | } |
717 | 717 | |
@@ -721,14 +721,14 @@ discard block |
||
721 | 721 | * @param int $action_id Action ID. |
722 | 722 | * @throws \InvalidArgumentException If the action deletion failed. |
723 | 723 | */ |
724 | - public function delete_action( $action_id ) { |
|
724 | + public function delete_action($action_id) { |
|
725 | 725 | /** @var \wpdb $wpdb */ |
726 | 726 | global $wpdb; |
727 | - $deleted = $wpdb->delete( $wpdb->actionscheduler_actions, array( 'action_id' => $action_id ), array( '%d' ) ); |
|
728 | - if ( empty( $deleted ) ) { |
|
729 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
727 | + $deleted = $wpdb->delete($wpdb->actionscheduler_actions, array('action_id' => $action_id), array('%d')); |
|
728 | + if (empty($deleted)) { |
|
729 | + throw new \InvalidArgumentException(sprintf(__('Unidentified action %s', 'action-scheduler'), $action_id)); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
730 | 730 | } |
731 | - do_action( 'action_scheduler_deleted_action', $action_id ); |
|
731 | + do_action('action_scheduler_deleted_action', $action_id); |
|
732 | 732 | } |
733 | 733 | |
734 | 734 | /** |
@@ -738,9 +738,9 @@ discard block |
||
738 | 738 | * |
739 | 739 | * @return \DateTime The local date the action is scheduled to run, or the date that it ran. |
740 | 740 | */ |
741 | - public function get_date( $action_id ) { |
|
742 | - $date = $this->get_date_gmt( $action_id ); |
|
743 | - ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
741 | + public function get_date($action_id) { |
|
742 | + $date = $this->get_date_gmt($action_id); |
|
743 | + ActionScheduler_TimezoneHelper::set_local_timezone($date); |
|
744 | 744 | return $date; |
745 | 745 | } |
746 | 746 | |
@@ -752,17 +752,17 @@ discard block |
||
752 | 752 | * @throws \InvalidArgumentException If action cannot be identified. |
753 | 753 | * @return \DateTime The GMT date the action is scheduled to run, or the date that it ran. |
754 | 754 | */ |
755 | - protected function get_date_gmt( $action_id ) { |
|
755 | + protected function get_date_gmt($action_id) { |
|
756 | 756 | /** @var \wpdb $wpdb */ |
757 | 757 | global $wpdb; |
758 | - $record = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d", $action_id ) ); |
|
759 | - if ( empty( $record ) ) { |
|
760 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
758 | + $record = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d", $action_id)); |
|
759 | + if (empty($record)) { |
|
760 | + throw new \InvalidArgumentException(sprintf(__('Unidentified action %s', 'action-scheduler'), $action_id)); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
761 | 761 | } |
762 | - if ( self::STATUS_PENDING === $record->status ) { |
|
763 | - return as_get_datetime_object( $record->scheduled_date_gmt ); |
|
762 | + if (self::STATUS_PENDING === $record->status) { |
|
763 | + return as_get_datetime_object($record->scheduled_date_gmt); |
|
764 | 764 | } else { |
765 | - return as_get_datetime_object( $record->last_attempt_gmt ); |
|
765 | + return as_get_datetime_object($record->last_attempt_gmt); |
|
766 | 766 | } |
767 | 767 | } |
768 | 768 | |
@@ -776,15 +776,15 @@ discard block |
||
776 | 776 | * |
777 | 777 | * @return ActionScheduler_ActionClaim |
778 | 778 | */ |
779 | - public function stake_claim( $max_actions = 10, \DateTime $before_date = null, $hooks = array(), $group = '' ) { |
|
779 | + public function stake_claim($max_actions = 10, \DateTime $before_date = null, $hooks = array(), $group = '') { |
|
780 | 780 | $claim_id = $this->generate_claim_id(); |
781 | 781 | |
782 | 782 | $this->claim_before_date = $before_date; |
783 | - $this->claim_actions( $claim_id, $max_actions, $before_date, $hooks, $group ); |
|
784 | - $action_ids = $this->find_actions_by_claim_id( $claim_id ); |
|
783 | + $this->claim_actions($claim_id, $max_actions, $before_date, $hooks, $group); |
|
784 | + $action_ids = $this->find_actions_by_claim_id($claim_id); |
|
785 | 785 | $this->claim_before_date = null; |
786 | 786 | |
787 | - return new ActionScheduler_ActionClaim( $claim_id, $action_ids ); |
|
787 | + return new ActionScheduler_ActionClaim($claim_id, $action_ids); |
|
788 | 788 | } |
789 | 789 | |
790 | 790 | /** |
@@ -796,7 +796,7 @@ discard block |
||
796 | 796 | /** @var \wpdb $wpdb */ |
797 | 797 | global $wpdb; |
798 | 798 | $now = as_get_datetime_object(); |
799 | - $wpdb->insert( $wpdb->actionscheduler_claims, array( 'date_created_gmt' => $now->format( 'Y-m-d H:i:s' ) ) ); |
|
799 | + $wpdb->insert($wpdb->actionscheduler_claims, array('date_created_gmt' => $now->format('Y-m-d H:i:s'))); |
|
800 | 800 | |
801 | 801 | return $wpdb->insert_id; |
802 | 802 | } |
@@ -814,7 +814,7 @@ discard block |
||
814 | 814 | * @throws \InvalidArgumentException Throws InvalidArgumentException if group doesn't exist. |
815 | 815 | * @throws \RuntimeException Throws RuntimeException if unable to claim action. |
816 | 816 | */ |
817 | - protected function claim_actions( $claim_id, $limit, \DateTime $before_date = null, $hooks = array(), $group = '' ) { |
|
817 | + protected function claim_actions($claim_id, $limit, \DateTime $before_date = null, $hooks = array(), $group = '') { |
|
818 | 818 | /** @var \wpdb $wpdb */ |
819 | 819 | global $wpdb; |
820 | 820 | |
@@ -825,28 +825,28 @@ discard block |
||
825 | 825 | $update = "UPDATE {$wpdb->actionscheduler_actions} SET claim_id=%d, last_attempt_gmt=%s, last_attempt_local=%s"; |
826 | 826 | $params = array( |
827 | 827 | $claim_id, |
828 | - $now->format( 'Y-m-d H:i:s' ), |
|
829 | - current_time( 'mysql' ), |
|
828 | + $now->format('Y-m-d H:i:s'), |
|
829 | + current_time('mysql'), |
|
830 | 830 | ); |
831 | 831 | |
832 | 832 | $where = 'WHERE claim_id = 0 AND scheduled_date_gmt <= %s AND status=%s'; |
833 | - $params[] = $date->format( 'Y-m-d H:i:s' ); |
|
833 | + $params[] = $date->format('Y-m-d H:i:s'); |
|
834 | 834 | $params[] = self::STATUS_PENDING; |
835 | 835 | |
836 | - if ( ! empty( $hooks ) ) { |
|
837 | - $placeholders = array_fill( 0, count( $hooks ), '%s' ); |
|
838 | - $where .= ' AND hook IN (' . join( ', ', $placeholders ) . ')'; |
|
839 | - $params = array_merge( $params, array_values( $hooks ) ); |
|
836 | + if ( ! empty($hooks)) { |
|
837 | + $placeholders = array_fill(0, count($hooks), '%s'); |
|
838 | + $where .= ' AND hook IN ('.join(', ', $placeholders).')'; |
|
839 | + $params = array_merge($params, array_values($hooks)); |
|
840 | 840 | } |
841 | 841 | |
842 | - if ( ! empty( $group ) ) { |
|
842 | + if ( ! empty($group)) { |
|
843 | 843 | |
844 | - $group_id = $this->get_group_id( $group, false ); |
|
844 | + $group_id = $this->get_group_id($group, false); |
|
845 | 845 | |
846 | 846 | // throw exception if no matching group found, this matches ActionScheduler_wpPostStore's behaviour. |
847 | - if ( empty( $group_id ) ) { |
|
847 | + if (empty($group_id)) { |
|
848 | 848 | /* translators: %s: group name */ |
849 | - throw new InvalidArgumentException( sprintf( __( 'The group "%s" does not exist.', 'action-scheduler' ), $group ) ); |
|
849 | + throw new InvalidArgumentException(sprintf(__('The group "%s" does not exist.', 'action-scheduler'), $group)); |
|
850 | 850 | } |
851 | 851 | |
852 | 852 | $where .= ' AND group_id = %d'; |
@@ -860,13 +860,13 @@ discard block |
||
860 | 860 | * |
861 | 861 | * @param string $order_by_sql |
862 | 862 | */ |
863 | - $order = apply_filters( 'action_scheduler_claim_actions_order_by', 'ORDER BY attempts ASC, scheduled_date_gmt ASC, action_id ASC' ); |
|
863 | + $order = apply_filters('action_scheduler_claim_actions_order_by', 'ORDER BY attempts ASC, scheduled_date_gmt ASC, action_id ASC'); |
|
864 | 864 | $params[] = $limit; |
865 | 865 | |
866 | - $sql = $wpdb->prepare( "{$update} {$where} {$order} LIMIT %d", $params ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders |
|
867 | - $rows_affected = $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
868 | - if ( false === $rows_affected ) { |
|
869 | - throw new \RuntimeException( __( 'Unable to claim actions. Database error.', 'action-scheduler' ) ); |
|
866 | + $sql = $wpdb->prepare("{$update} {$where} {$order} LIMIT %d", $params); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders |
|
867 | + $rows_affected = $wpdb->query($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
|
868 | + if (false === $rows_affected) { |
|
869 | + throw new \RuntimeException(__('Unable to claim actions. Database error.', 'action-scheduler')); |
|
870 | 870 | } |
871 | 871 | |
872 | 872 | return (int) $rows_affected; |
@@ -881,9 +881,9 @@ discard block |
||
881 | 881 | global $wpdb; |
882 | 882 | |
883 | 883 | $sql = "SELECT COUNT(DISTINCT claim_id) FROM {$wpdb->actionscheduler_actions} WHERE claim_id != 0 AND status IN ( %s, %s)"; |
884 | - $sql = $wpdb->prepare( $sql, array( self::STATUS_PENDING, self::STATUS_RUNNING ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
884 | + $sql = $wpdb->prepare($sql, array(self::STATUS_PENDING, self::STATUS_RUNNING)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
885 | 885 | |
886 | - return (int) $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
886 | + return (int) $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
887 | 887 | } |
888 | 888 | |
889 | 889 | /** |
@@ -892,14 +892,14 @@ discard block |
||
892 | 892 | * @param string $action_id Action ID. |
893 | 893 | * @return mixed |
894 | 894 | */ |
895 | - public function get_claim_id( $action_id ) { |
|
895 | + public function get_claim_id($action_id) { |
|
896 | 896 | /** @var \wpdb $wpdb */ |
897 | 897 | global $wpdb; |
898 | 898 | |
899 | 899 | $sql = "SELECT claim_id FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d"; |
900 | - $sql = $wpdb->prepare( $sql, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
900 | + $sql = $wpdb->prepare($sql, $action_id); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
901 | 901 | |
902 | - return (int) $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
902 | + return (int) $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
903 | 903 | } |
904 | 904 | |
905 | 905 | /** |
@@ -908,13 +908,13 @@ discard block |
||
908 | 908 | * @param int $claim_id Claim ID. |
909 | 909 | * @return int[] |
910 | 910 | */ |
911 | - public function find_actions_by_claim_id( $claim_id ) { |
|
911 | + public function find_actions_by_claim_id($claim_id) { |
|
912 | 912 | /** @var \wpdb $wpdb */ |
913 | 913 | global $wpdb; |
914 | 914 | |
915 | 915 | $action_ids = array(); |
916 | - $before_date = isset( $this->claim_before_date ) ? $this->claim_before_date : as_get_datetime_object(); |
|
917 | - $cut_off = $before_date->format( 'Y-m-d H:i:s' ); |
|
916 | + $before_date = isset($this->claim_before_date) ? $this->claim_before_date : as_get_datetime_object(); |
|
917 | + $cut_off = $before_date->format('Y-m-d H:i:s'); |
|
918 | 918 | |
919 | 919 | $sql = $wpdb->prepare( |
920 | 920 | "SELECT action_id, scheduled_date_gmt FROM {$wpdb->actionscheduler_actions} WHERE claim_id = %d", |
@@ -923,9 +923,9 @@ discard block |
||
923 | 923 | |
924 | 924 | // Verify that the scheduled date for each action is within the expected bounds (in some unusual |
925 | 925 | // cases, we cannot depend on MySQL to honor all of the WHERE conditions we specify). |
926 | - foreach ( $wpdb->get_results( $sql ) as $claimed_action ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
927 | - if ( $claimed_action->scheduled_date_gmt <= $cut_off ) { |
|
928 | - $action_ids[] = absint( $claimed_action->action_id ); |
|
926 | + foreach ($wpdb->get_results($sql) as $claimed_action) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
927 | + if ($claimed_action->scheduled_date_gmt <= $cut_off) { |
|
928 | + $action_ids[] = absint($claimed_action->action_id); |
|
929 | 929 | } |
930 | 930 | } |
931 | 931 | |
@@ -937,11 +937,11 @@ discard block |
||
937 | 937 | * |
938 | 938 | * @param ActionScheduler_ActionClaim $claim Claim object. |
939 | 939 | */ |
940 | - public function release_claim( ActionScheduler_ActionClaim $claim ) { |
|
940 | + public function release_claim(ActionScheduler_ActionClaim $claim) { |
|
941 | 941 | /** @var \wpdb $wpdb */ |
942 | 942 | global $wpdb; |
943 | - $wpdb->update( $wpdb->actionscheduler_actions, array( 'claim_id' => 0 ), array( 'claim_id' => $claim->get_id() ), array( '%d' ), array( '%d' ) ); |
|
944 | - $wpdb->delete( $wpdb->actionscheduler_claims, array( 'claim_id' => $claim->get_id() ), array( '%d' ) ); |
|
943 | + $wpdb->update($wpdb->actionscheduler_actions, array('claim_id' => 0), array('claim_id' => $claim->get_id()), array('%d'), array('%d')); |
|
944 | + $wpdb->delete($wpdb->actionscheduler_claims, array('claim_id' => $claim->get_id()), array('%d')); |
|
945 | 945 | } |
946 | 946 | |
947 | 947 | /** |
@@ -951,15 +951,15 @@ discard block |
||
951 | 951 | * |
952 | 952 | * @return void |
953 | 953 | */ |
954 | - public function unclaim_action( $action_id ) { |
|
954 | + public function unclaim_action($action_id) { |
|
955 | 955 | /** @var \wpdb $wpdb */ |
956 | 956 | global $wpdb; |
957 | 957 | $wpdb->update( |
958 | 958 | $wpdb->actionscheduler_actions, |
959 | - array( 'claim_id' => 0 ), |
|
960 | - array( 'action_id' => $action_id ), |
|
961 | - array( '%s' ), |
|
962 | - array( '%d' ) |
|
959 | + array('claim_id' => 0), |
|
960 | + array('action_id' => $action_id), |
|
961 | + array('%s'), |
|
962 | + array('%d') |
|
963 | 963 | ); |
964 | 964 | } |
965 | 965 | |
@@ -969,18 +969,18 @@ discard block |
||
969 | 969 | * @param int $action_id Action ID. |
970 | 970 | * @throws \InvalidArgumentException Throw an exception if action was not updated. |
971 | 971 | */ |
972 | - public function mark_failure( $action_id ) { |
|
972 | + public function mark_failure($action_id) { |
|
973 | 973 | /** @var \wpdb $wpdb */ |
974 | 974 | global $wpdb; |
975 | 975 | $updated = $wpdb->update( |
976 | 976 | $wpdb->actionscheduler_actions, |
977 | - array( 'status' => self::STATUS_FAILED ), |
|
978 | - array( 'action_id' => $action_id ), |
|
979 | - array( '%s' ), |
|
980 | - array( '%d' ) |
|
977 | + array('status' => self::STATUS_FAILED), |
|
978 | + array('action_id' => $action_id), |
|
979 | + array('%s'), |
|
980 | + array('%d') |
|
981 | 981 | ); |
982 | - if ( empty( $updated ) ) { |
|
983 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
982 | + if (empty($updated)) { |
|
983 | + throw new \InvalidArgumentException(sprintf(__('Unidentified action %s', 'action-scheduler'), $action_id)); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
984 | 984 | } |
985 | 985 | } |
986 | 986 | |
@@ -991,13 +991,13 @@ discard block |
||
991 | 991 | * |
992 | 992 | * @return void |
993 | 993 | */ |
994 | - public function log_execution( $action_id ) { |
|
994 | + public function log_execution($action_id) { |
|
995 | 995 | /** @var \wpdb $wpdb */ |
996 | 996 | global $wpdb; |
997 | 997 | |
998 | 998 | $sql = "UPDATE {$wpdb->actionscheduler_actions} SET attempts = attempts+1, status=%s, last_attempt_gmt = %s, last_attempt_local = %s WHERE action_id = %d"; |
999 | - $sql = $wpdb->prepare( $sql, self::STATUS_RUNNING, current_time( 'mysql', true ), current_time( 'mysql' ), $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1000 | - $wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
999 | + $sql = $wpdb->prepare($sql, self::STATUS_RUNNING, current_time('mysql', true), current_time('mysql'), $action_id); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1000 | + $wpdb->query($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1001 | 1001 | } |
1002 | 1002 | |
1003 | 1003 | /** |
@@ -1008,22 +1008,22 @@ discard block |
||
1008 | 1008 | * @return void |
1009 | 1009 | * @throws \InvalidArgumentException Throw an exception if action was not updated. |
1010 | 1010 | */ |
1011 | - public function mark_complete( $action_id ) { |
|
1011 | + public function mark_complete($action_id) { |
|
1012 | 1012 | /** @var \wpdb $wpdb */ |
1013 | 1013 | global $wpdb; |
1014 | 1014 | $updated = $wpdb->update( |
1015 | 1015 | $wpdb->actionscheduler_actions, |
1016 | 1016 | array( |
1017 | 1017 | 'status' => self::STATUS_COMPLETE, |
1018 | - 'last_attempt_gmt' => current_time( 'mysql', true ), |
|
1019 | - 'last_attempt_local' => current_time( 'mysql' ), |
|
1018 | + 'last_attempt_gmt' => current_time('mysql', true), |
|
1019 | + 'last_attempt_local' => current_time('mysql'), |
|
1020 | 1020 | ), |
1021 | - array( 'action_id' => $action_id ), |
|
1022 | - array( '%s' ), |
|
1023 | - array( '%d' ) |
|
1021 | + array('action_id' => $action_id), |
|
1022 | + array('%s'), |
|
1023 | + array('%d') |
|
1024 | 1024 | ); |
1025 | - if ( empty( $updated ) ) { |
|
1026 | - throw new \InvalidArgumentException( sprintf( __( 'Unidentified action %s', 'action-scheduler' ), $action_id ) ); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
1025 | + if (empty($updated)) { |
|
1026 | + throw new \InvalidArgumentException(sprintf(__('Unidentified action %s', 'action-scheduler'), $action_id)); //phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
|
1027 | 1027 | } |
1028 | 1028 | |
1029 | 1029 | /** |
@@ -1033,7 +1033,7 @@ discard block |
||
1033 | 1033 | * |
1034 | 1034 | * @param int $action_id Action ID. |
1035 | 1035 | */ |
1036 | - do_action( 'action_scheduler_completed_action', $action_id ); |
|
1036 | + do_action('action_scheduler_completed_action', $action_id); |
|
1037 | 1037 | } |
1038 | 1038 | |
1039 | 1039 | /** |
@@ -1045,17 +1045,17 @@ discard block |
||
1045 | 1045 | * @throws \InvalidArgumentException Throw an exception if not status was found for action_id. |
1046 | 1046 | * @throws \RuntimeException Throw an exception if action status could not be retrieved. |
1047 | 1047 | */ |
1048 | - public function get_status( $action_id ) { |
|
1048 | + public function get_status($action_id) { |
|
1049 | 1049 | /** @var \wpdb $wpdb */ |
1050 | 1050 | global $wpdb; |
1051 | 1051 | $sql = "SELECT status FROM {$wpdb->actionscheduler_actions} WHERE action_id=%d"; |
1052 | - $sql = $wpdb->prepare( $sql, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1053 | - $status = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1052 | + $sql = $wpdb->prepare($sql, $action_id); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1053 | + $status = $wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
1054 | 1054 | |
1055 | - if ( null === $status ) { |
|
1056 | - throw new \InvalidArgumentException( __( 'Invalid action ID. No status found.', 'action-scheduler' ) ); |
|
1057 | - } elseif ( empty( $status ) ) { |
|
1058 | - throw new \RuntimeException( __( 'Unknown status found for action.', 'action-scheduler' ) ); |
|
1055 | + if (null === $status) { |
|
1056 | + throw new \InvalidArgumentException(__('Invalid action ID. No status found.', 'action-scheduler')); |
|
1057 | + } elseif (empty($status)) { |
|
1058 | + throw new \RuntimeException(__('Unknown status found for action.', 'action-scheduler')); |
|
1059 | 1059 | } else { |
1060 | 1060 | return $status; |
1061 | 1061 | } |
@@ -4,243 +4,243 @@ |
||
4 | 4 | * Class ActionScheduler_wpCommentLogger |
5 | 5 | */ |
6 | 6 | class ActionScheduler_wpCommentLogger extends ActionScheduler_Logger { |
7 | - const AGENT = 'ActionScheduler'; |
|
8 | - const TYPE = 'action_log'; |
|
9 | - |
|
10 | - /** |
|
11 | - * @param string $action_id |
|
12 | - * @param string $message |
|
13 | - * @param DateTime $date |
|
14 | - * |
|
15 | - * @return string The log entry ID |
|
16 | - */ |
|
17 | - public function log( $action_id, $message, DateTime $date = null ) { |
|
18 | - if ( empty( $date ) ) { |
|
19 | - $date = as_get_datetime_object(); |
|
20 | - } else { |
|
21 | - $date = as_get_datetime_object( clone $date ); |
|
22 | - } |
|
23 | - $comment_id = $this->create_wp_comment( $action_id, $message, $date ); |
|
24 | - return $comment_id; |
|
25 | - } |
|
26 | - |
|
27 | - protected function create_wp_comment( $action_id, $message, DateTime $date ) { |
|
28 | - |
|
29 | - $comment_date_gmt = $date->format( 'Y-m-d H:i:s' ); |
|
30 | - ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
31 | - $comment_data = array( |
|
32 | - 'comment_post_ID' => $action_id, |
|
33 | - 'comment_date' => $date->format( 'Y-m-d H:i:s' ), |
|
34 | - 'comment_date_gmt' => $comment_date_gmt, |
|
35 | - 'comment_author' => self::AGENT, |
|
36 | - 'comment_content' => $message, |
|
37 | - 'comment_agent' => self::AGENT, |
|
38 | - 'comment_type' => self::TYPE, |
|
39 | - ); |
|
40 | - return wp_insert_comment( $comment_data ); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @param string $entry_id |
|
45 | - * |
|
46 | - * @return ActionScheduler_LogEntry |
|
47 | - */ |
|
48 | - public function get_entry( $entry_id ) { |
|
49 | - $comment = $this->get_comment( $entry_id ); |
|
50 | - if ( empty( $comment ) || $comment->comment_type != self::TYPE ) { |
|
51 | - return new ActionScheduler_NullLogEntry(); |
|
52 | - } |
|
53 | - |
|
54 | - $date = as_get_datetime_object( $comment->comment_date_gmt ); |
|
55 | - ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
56 | - return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date ); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $action_id |
|
61 | - * |
|
62 | - * @return ActionScheduler_LogEntry[] |
|
63 | - */ |
|
64 | - public function get_logs( $action_id ) { |
|
65 | - $status = 'all'; |
|
66 | - if ( get_post_status( $action_id ) == 'trash' ) { |
|
67 | - $status = 'post-trashed'; |
|
68 | - } |
|
69 | - $comments = get_comments( |
|
70 | - array( |
|
71 | - 'post_id' => $action_id, |
|
72 | - 'orderby' => 'comment_date_gmt', |
|
73 | - 'order' => 'ASC', |
|
74 | - 'type' => self::TYPE, |
|
75 | - 'status' => $status, |
|
76 | - ) |
|
77 | - ); |
|
78 | - $logs = array(); |
|
79 | - foreach ( $comments as $c ) { |
|
80 | - $entry = $this->get_entry( $c ); |
|
81 | - if ( ! empty( $entry ) ) { |
|
82 | - $logs[] = $entry; |
|
83 | - } |
|
84 | - } |
|
85 | - return $logs; |
|
86 | - } |
|
87 | - |
|
88 | - protected function get_comment( $comment_id ) { |
|
89 | - return get_comment( $comment_id ); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param WP_Comment_Query $query |
|
94 | - */ |
|
95 | - public function filter_comment_queries( $query ) { |
|
96 | - foreach ( array( 'ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID' ) as $key ) { |
|
97 | - if ( ! empty( $query->query_vars[ $key ] ) ) { |
|
98 | - return; // don't slow down queries that wouldn't include action_log comments anyway |
|
99 | - } |
|
100 | - } |
|
101 | - $query->query_vars['action_log_filter'] = true; |
|
102 | - add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 ); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param array $clauses |
|
107 | - * @param WP_Comment_Query $query |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - public function filter_comment_query_clauses( $clauses, $query ) { |
|
112 | - if ( ! empty( $query->query_vars['action_log_filter'] ) ) { |
|
113 | - $clauses['where'] .= $this->get_where_clause(); |
|
114 | - } |
|
115 | - return $clauses; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Make sure Action Scheduler logs are excluded from comment feeds, which use WP_Query, not |
|
120 | - * the WP_Comment_Query class handled by @see self::filter_comment_queries(). |
|
121 | - * |
|
122 | - * @param string $where |
|
123 | - * @param WP_Query $query |
|
124 | - * |
|
125 | - * @return string |
|
126 | - */ |
|
127 | - public function filter_comment_feed( $where, $query ) { |
|
128 | - if ( is_comment_feed() ) { |
|
129 | - $where .= $this->get_where_clause(); |
|
130 | - } |
|
131 | - return $where; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Return a SQL clause to exclude Action Scheduler comments. |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - protected function get_where_clause() { |
|
140 | - global $wpdb; |
|
141 | - return sprintf( " AND {$wpdb->comments}.comment_type != '%s'", self::TYPE ); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Remove action log entries from wp_count_comments() |
|
146 | - * |
|
147 | - * @param array $stats |
|
148 | - * @param int $post_id |
|
149 | - * |
|
150 | - * @return object |
|
151 | - */ |
|
152 | - public function filter_comment_count( $stats, $post_id ) { |
|
153 | - global $wpdb; |
|
154 | - |
|
155 | - if ( 0 === $post_id ) { |
|
156 | - $stats = $this->get_comment_count(); |
|
157 | - } |
|
158 | - |
|
159 | - return $stats; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Retrieve the comment counts from our cache, or the database if the cached version isn't set. |
|
164 | - * |
|
165 | - * @return object |
|
166 | - */ |
|
167 | - protected function get_comment_count() { |
|
168 | - global $wpdb; |
|
169 | - |
|
170 | - $stats = get_transient( 'as_comment_count' ); |
|
171 | - |
|
172 | - if ( ! $stats ) { |
|
173 | - $stats = array(); |
|
174 | - |
|
175 | - $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A ); |
|
176 | - |
|
177 | - $total = 0; |
|
178 | - $stats = array(); |
|
179 | - $approved = array( |
|
180 | - '0' => 'moderated', |
|
181 | - '1' => 'approved', |
|
182 | - 'spam' => 'spam', |
|
183 | - 'trash' => 'trash', |
|
184 | - 'post-trashed' => 'post-trashed', |
|
185 | - ); |
|
186 | - |
|
187 | - foreach ( (array) $count as $row ) { |
|
188 | - // Don't count post-trashed toward totals |
|
189 | - if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) { |
|
190 | - $total += $row['num_comments']; |
|
191 | - } |
|
192 | - if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
193 | - $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - $stats['total_comments'] = $total; |
|
198 | - $stats['all'] = $total; |
|
199 | - |
|
200 | - foreach ( $approved as $key ) { |
|
201 | - if ( empty( $stats[ $key ] ) ) { |
|
202 | - $stats[ $key ] = 0; |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - $stats = (object) $stats; |
|
207 | - set_transient( 'as_comment_count', $stats ); |
|
208 | - } |
|
209 | - |
|
210 | - return $stats; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Delete comment count cache whenever there is new comment or the status of a comment changes. Cache |
|
215 | - * will be regenerated next time ActionScheduler_wpCommentLogger::filter_comment_count() is called. |
|
216 | - */ |
|
217 | - public function delete_comment_count_cache() { |
|
218 | - delete_transient( 'as_comment_count' ); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * @codeCoverageIgnore |
|
223 | - */ |
|
224 | - public function init() { |
|
225 | - add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 ); |
|
226 | - add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 ); |
|
227 | - |
|
228 | - parent::init(); |
|
229 | - |
|
230 | - add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 ); |
|
231 | - add_action( 'wp_count_comments', array( $this, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
232 | - add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 ); |
|
233 | - |
|
234 | - // Delete comments count cache whenever there is a new comment or a comment status changes |
|
235 | - add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) ); |
|
236 | - add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) ); |
|
237 | - } |
|
238 | - |
|
239 | - public function disable_comment_counting() { |
|
240 | - wp_defer_comment_counting( true ); |
|
241 | - } |
|
242 | - public function enable_comment_counting() { |
|
243 | - wp_defer_comment_counting( false ); |
|
244 | - } |
|
7 | + const AGENT = 'ActionScheduler'; |
|
8 | + const TYPE = 'action_log'; |
|
9 | + |
|
10 | + /** |
|
11 | + * @param string $action_id |
|
12 | + * @param string $message |
|
13 | + * @param DateTime $date |
|
14 | + * |
|
15 | + * @return string The log entry ID |
|
16 | + */ |
|
17 | + public function log( $action_id, $message, DateTime $date = null ) { |
|
18 | + if ( empty( $date ) ) { |
|
19 | + $date = as_get_datetime_object(); |
|
20 | + } else { |
|
21 | + $date = as_get_datetime_object( clone $date ); |
|
22 | + } |
|
23 | + $comment_id = $this->create_wp_comment( $action_id, $message, $date ); |
|
24 | + return $comment_id; |
|
25 | + } |
|
26 | + |
|
27 | + protected function create_wp_comment( $action_id, $message, DateTime $date ) { |
|
28 | + |
|
29 | + $comment_date_gmt = $date->format( 'Y-m-d H:i:s' ); |
|
30 | + ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
31 | + $comment_data = array( |
|
32 | + 'comment_post_ID' => $action_id, |
|
33 | + 'comment_date' => $date->format( 'Y-m-d H:i:s' ), |
|
34 | + 'comment_date_gmt' => $comment_date_gmt, |
|
35 | + 'comment_author' => self::AGENT, |
|
36 | + 'comment_content' => $message, |
|
37 | + 'comment_agent' => self::AGENT, |
|
38 | + 'comment_type' => self::TYPE, |
|
39 | + ); |
|
40 | + return wp_insert_comment( $comment_data ); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @param string $entry_id |
|
45 | + * |
|
46 | + * @return ActionScheduler_LogEntry |
|
47 | + */ |
|
48 | + public function get_entry( $entry_id ) { |
|
49 | + $comment = $this->get_comment( $entry_id ); |
|
50 | + if ( empty( $comment ) || $comment->comment_type != self::TYPE ) { |
|
51 | + return new ActionScheduler_NullLogEntry(); |
|
52 | + } |
|
53 | + |
|
54 | + $date = as_get_datetime_object( $comment->comment_date_gmt ); |
|
55 | + ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
56 | + return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date ); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $action_id |
|
61 | + * |
|
62 | + * @return ActionScheduler_LogEntry[] |
|
63 | + */ |
|
64 | + public function get_logs( $action_id ) { |
|
65 | + $status = 'all'; |
|
66 | + if ( get_post_status( $action_id ) == 'trash' ) { |
|
67 | + $status = 'post-trashed'; |
|
68 | + } |
|
69 | + $comments = get_comments( |
|
70 | + array( |
|
71 | + 'post_id' => $action_id, |
|
72 | + 'orderby' => 'comment_date_gmt', |
|
73 | + 'order' => 'ASC', |
|
74 | + 'type' => self::TYPE, |
|
75 | + 'status' => $status, |
|
76 | + ) |
|
77 | + ); |
|
78 | + $logs = array(); |
|
79 | + foreach ( $comments as $c ) { |
|
80 | + $entry = $this->get_entry( $c ); |
|
81 | + if ( ! empty( $entry ) ) { |
|
82 | + $logs[] = $entry; |
|
83 | + } |
|
84 | + } |
|
85 | + return $logs; |
|
86 | + } |
|
87 | + |
|
88 | + protected function get_comment( $comment_id ) { |
|
89 | + return get_comment( $comment_id ); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param WP_Comment_Query $query |
|
94 | + */ |
|
95 | + public function filter_comment_queries( $query ) { |
|
96 | + foreach ( array( 'ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID' ) as $key ) { |
|
97 | + if ( ! empty( $query->query_vars[ $key ] ) ) { |
|
98 | + return; // don't slow down queries that wouldn't include action_log comments anyway |
|
99 | + } |
|
100 | + } |
|
101 | + $query->query_vars['action_log_filter'] = true; |
|
102 | + add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 ); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param array $clauses |
|
107 | + * @param WP_Comment_Query $query |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + public function filter_comment_query_clauses( $clauses, $query ) { |
|
112 | + if ( ! empty( $query->query_vars['action_log_filter'] ) ) { |
|
113 | + $clauses['where'] .= $this->get_where_clause(); |
|
114 | + } |
|
115 | + return $clauses; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Make sure Action Scheduler logs are excluded from comment feeds, which use WP_Query, not |
|
120 | + * the WP_Comment_Query class handled by @see self::filter_comment_queries(). |
|
121 | + * |
|
122 | + * @param string $where |
|
123 | + * @param WP_Query $query |
|
124 | + * |
|
125 | + * @return string |
|
126 | + */ |
|
127 | + public function filter_comment_feed( $where, $query ) { |
|
128 | + if ( is_comment_feed() ) { |
|
129 | + $where .= $this->get_where_clause(); |
|
130 | + } |
|
131 | + return $where; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Return a SQL clause to exclude Action Scheduler comments. |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + protected function get_where_clause() { |
|
140 | + global $wpdb; |
|
141 | + return sprintf( " AND {$wpdb->comments}.comment_type != '%s'", self::TYPE ); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Remove action log entries from wp_count_comments() |
|
146 | + * |
|
147 | + * @param array $stats |
|
148 | + * @param int $post_id |
|
149 | + * |
|
150 | + * @return object |
|
151 | + */ |
|
152 | + public function filter_comment_count( $stats, $post_id ) { |
|
153 | + global $wpdb; |
|
154 | + |
|
155 | + if ( 0 === $post_id ) { |
|
156 | + $stats = $this->get_comment_count(); |
|
157 | + } |
|
158 | + |
|
159 | + return $stats; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Retrieve the comment counts from our cache, or the database if the cached version isn't set. |
|
164 | + * |
|
165 | + * @return object |
|
166 | + */ |
|
167 | + protected function get_comment_count() { |
|
168 | + global $wpdb; |
|
169 | + |
|
170 | + $stats = get_transient( 'as_comment_count' ); |
|
171 | + |
|
172 | + if ( ! $stats ) { |
|
173 | + $stats = array(); |
|
174 | + |
|
175 | + $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A ); |
|
176 | + |
|
177 | + $total = 0; |
|
178 | + $stats = array(); |
|
179 | + $approved = array( |
|
180 | + '0' => 'moderated', |
|
181 | + '1' => 'approved', |
|
182 | + 'spam' => 'spam', |
|
183 | + 'trash' => 'trash', |
|
184 | + 'post-trashed' => 'post-trashed', |
|
185 | + ); |
|
186 | + |
|
187 | + foreach ( (array) $count as $row ) { |
|
188 | + // Don't count post-trashed toward totals |
|
189 | + if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) { |
|
190 | + $total += $row['num_comments']; |
|
191 | + } |
|
192 | + if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
193 | + $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + $stats['total_comments'] = $total; |
|
198 | + $stats['all'] = $total; |
|
199 | + |
|
200 | + foreach ( $approved as $key ) { |
|
201 | + if ( empty( $stats[ $key ] ) ) { |
|
202 | + $stats[ $key ] = 0; |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + $stats = (object) $stats; |
|
207 | + set_transient( 'as_comment_count', $stats ); |
|
208 | + } |
|
209 | + |
|
210 | + return $stats; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Delete comment count cache whenever there is new comment or the status of a comment changes. Cache |
|
215 | + * will be regenerated next time ActionScheduler_wpCommentLogger::filter_comment_count() is called. |
|
216 | + */ |
|
217 | + public function delete_comment_count_cache() { |
|
218 | + delete_transient( 'as_comment_count' ); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * @codeCoverageIgnore |
|
223 | + */ |
|
224 | + public function init() { |
|
225 | + add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 ); |
|
226 | + add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 ); |
|
227 | + |
|
228 | + parent::init(); |
|
229 | + |
|
230 | + add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 ); |
|
231 | + add_action( 'wp_count_comments', array( $this, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
232 | + add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 ); |
|
233 | + |
|
234 | + // Delete comments count cache whenever there is a new comment or a comment status changes |
|
235 | + add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) ); |
|
236 | + add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) ); |
|
237 | + } |
|
238 | + |
|
239 | + public function disable_comment_counting() { |
|
240 | + wp_defer_comment_counting( true ); |
|
241 | + } |
|
242 | + public function enable_comment_counting() { |
|
243 | + wp_defer_comment_counting( false ); |
|
244 | + } |
|
245 | 245 | |
246 | 246 | } |
@@ -14,30 +14,30 @@ discard block |
||
14 | 14 | * |
15 | 15 | * @return string The log entry ID |
16 | 16 | */ |
17 | - public function log( $action_id, $message, DateTime $date = null ) { |
|
18 | - if ( empty( $date ) ) { |
|
17 | + public function log($action_id, $message, DateTime $date = null) { |
|
18 | + if (empty($date)) { |
|
19 | 19 | $date = as_get_datetime_object(); |
20 | 20 | } else { |
21 | - $date = as_get_datetime_object( clone $date ); |
|
21 | + $date = as_get_datetime_object(clone $date); |
|
22 | 22 | } |
23 | - $comment_id = $this->create_wp_comment( $action_id, $message, $date ); |
|
23 | + $comment_id = $this->create_wp_comment($action_id, $message, $date); |
|
24 | 24 | return $comment_id; |
25 | 25 | } |
26 | 26 | |
27 | - protected function create_wp_comment( $action_id, $message, DateTime $date ) { |
|
27 | + protected function create_wp_comment($action_id, $message, DateTime $date) { |
|
28 | 28 | |
29 | - $comment_date_gmt = $date->format( 'Y-m-d H:i:s' ); |
|
30 | - ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
29 | + $comment_date_gmt = $date->format('Y-m-d H:i:s'); |
|
30 | + ActionScheduler_TimezoneHelper::set_local_timezone($date); |
|
31 | 31 | $comment_data = array( |
32 | 32 | 'comment_post_ID' => $action_id, |
33 | - 'comment_date' => $date->format( 'Y-m-d H:i:s' ), |
|
33 | + 'comment_date' => $date->format('Y-m-d H:i:s'), |
|
34 | 34 | 'comment_date_gmt' => $comment_date_gmt, |
35 | 35 | 'comment_author' => self::AGENT, |
36 | 36 | 'comment_content' => $message, |
37 | 37 | 'comment_agent' => self::AGENT, |
38 | 38 | 'comment_type' => self::TYPE, |
39 | 39 | ); |
40 | - return wp_insert_comment( $comment_data ); |
|
40 | + return wp_insert_comment($comment_data); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -45,15 +45,15 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @return ActionScheduler_LogEntry |
47 | 47 | */ |
48 | - public function get_entry( $entry_id ) { |
|
49 | - $comment = $this->get_comment( $entry_id ); |
|
50 | - if ( empty( $comment ) || $comment->comment_type != self::TYPE ) { |
|
48 | + public function get_entry($entry_id) { |
|
49 | + $comment = $this->get_comment($entry_id); |
|
50 | + if (empty($comment) || $comment->comment_type != self::TYPE) { |
|
51 | 51 | return new ActionScheduler_NullLogEntry(); |
52 | 52 | } |
53 | 53 | |
54 | - $date = as_get_datetime_object( $comment->comment_date_gmt ); |
|
55 | - ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
|
56 | - return new ActionScheduler_LogEntry( $comment->comment_post_ID, $comment->comment_content, $date ); |
|
54 | + $date = as_get_datetime_object($comment->comment_date_gmt); |
|
55 | + ActionScheduler_TimezoneHelper::set_local_timezone($date); |
|
56 | + return new ActionScheduler_LogEntry($comment->comment_post_ID, $comment->comment_content, $date); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @return ActionScheduler_LogEntry[] |
63 | 63 | */ |
64 | - public function get_logs( $action_id ) { |
|
64 | + public function get_logs($action_id) { |
|
65 | 65 | $status = 'all'; |
66 | - if ( get_post_status( $action_id ) == 'trash' ) { |
|
66 | + if (get_post_status($action_id) == 'trash') { |
|
67 | 67 | $status = 'post-trashed'; |
68 | 68 | } |
69 | 69 | $comments = get_comments( |
@@ -75,31 +75,31 @@ discard block |
||
75 | 75 | 'status' => $status, |
76 | 76 | ) |
77 | 77 | ); |
78 | - $logs = array(); |
|
79 | - foreach ( $comments as $c ) { |
|
80 | - $entry = $this->get_entry( $c ); |
|
81 | - if ( ! empty( $entry ) ) { |
|
78 | + $logs = array(); |
|
79 | + foreach ($comments as $c) { |
|
80 | + $entry = $this->get_entry($c); |
|
81 | + if ( ! empty($entry)) { |
|
82 | 82 | $logs[] = $entry; |
83 | 83 | } |
84 | 84 | } |
85 | 85 | return $logs; |
86 | 86 | } |
87 | 87 | |
88 | - protected function get_comment( $comment_id ) { |
|
89 | - return get_comment( $comment_id ); |
|
88 | + protected function get_comment($comment_id) { |
|
89 | + return get_comment($comment_id); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
93 | 93 | * @param WP_Comment_Query $query |
94 | 94 | */ |
95 | - public function filter_comment_queries( $query ) { |
|
96 | - foreach ( array( 'ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID' ) as $key ) { |
|
97 | - if ( ! empty( $query->query_vars[ $key ] ) ) { |
|
95 | + public function filter_comment_queries($query) { |
|
96 | + foreach (array('ID', 'parent', 'post_author', 'post_name', 'post_parent', 'type', 'post_type', 'post_id', 'post_ID') as $key) { |
|
97 | + if ( ! empty($query->query_vars[$key])) { |
|
98 | 98 | return; // don't slow down queries that wouldn't include action_log comments anyway |
99 | 99 | } |
100 | 100 | } |
101 | 101 | $query->query_vars['action_log_filter'] = true; |
102 | - add_filter( 'comments_clauses', array( $this, 'filter_comment_query_clauses' ), 10, 2 ); |
|
102 | + add_filter('comments_clauses', array($this, 'filter_comment_query_clauses'), 10, 2); |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | /** |
@@ -108,8 +108,8 @@ discard block |
||
108 | 108 | * |
109 | 109 | * @return array |
110 | 110 | */ |
111 | - public function filter_comment_query_clauses( $clauses, $query ) { |
|
112 | - if ( ! empty( $query->query_vars['action_log_filter'] ) ) { |
|
111 | + public function filter_comment_query_clauses($clauses, $query) { |
|
112 | + if ( ! empty($query->query_vars['action_log_filter'])) { |
|
113 | 113 | $clauses['where'] .= $this->get_where_clause(); |
114 | 114 | } |
115 | 115 | return $clauses; |
@@ -124,8 +124,8 @@ discard block |
||
124 | 124 | * |
125 | 125 | * @return string |
126 | 126 | */ |
127 | - public function filter_comment_feed( $where, $query ) { |
|
128 | - if ( is_comment_feed() ) { |
|
127 | + public function filter_comment_feed($where, $query) { |
|
128 | + if (is_comment_feed()) { |
|
129 | 129 | $where .= $this->get_where_clause(); |
130 | 130 | } |
131 | 131 | return $where; |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | */ |
139 | 139 | protected function get_where_clause() { |
140 | 140 | global $wpdb; |
141 | - return sprintf( " AND {$wpdb->comments}.comment_type != '%s'", self::TYPE ); |
|
141 | + return sprintf(" AND {$wpdb->comments}.comment_type != '%s'", self::TYPE); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | /** |
@@ -149,10 +149,10 @@ discard block |
||
149 | 149 | * |
150 | 150 | * @return object |
151 | 151 | */ |
152 | - public function filter_comment_count( $stats, $post_id ) { |
|
152 | + public function filter_comment_count($stats, $post_id) { |
|
153 | 153 | global $wpdb; |
154 | 154 | |
155 | - if ( 0 === $post_id ) { |
|
155 | + if (0 === $post_id) { |
|
156 | 156 | $stats = $this->get_comment_count(); |
157 | 157 | } |
158 | 158 | |
@@ -167,12 +167,12 @@ discard block |
||
167 | 167 | protected function get_comment_count() { |
168 | 168 | global $wpdb; |
169 | 169 | |
170 | - $stats = get_transient( 'as_comment_count' ); |
|
170 | + $stats = get_transient('as_comment_count'); |
|
171 | 171 | |
172 | - if ( ! $stats ) { |
|
172 | + if ( ! $stats) { |
|
173 | 173 | $stats = array(); |
174 | 174 | |
175 | - $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A ); |
|
175 | + $count = $wpdb->get_results("SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN('order_note','action_log') GROUP BY comment_approved", ARRAY_A); |
|
176 | 176 | |
177 | 177 | $total = 0; |
178 | 178 | $stats = array(); |
@@ -184,27 +184,27 @@ discard block |
||
184 | 184 | 'post-trashed' => 'post-trashed', |
185 | 185 | ); |
186 | 186 | |
187 | - foreach ( (array) $count as $row ) { |
|
187 | + foreach ((array) $count as $row) { |
|
188 | 188 | // Don't count post-trashed toward totals |
189 | - if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) { |
|
189 | + if ('post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved']) { |
|
190 | 190 | $total += $row['num_comments']; |
191 | 191 | } |
192 | - if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
193 | - $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
192 | + if (isset($approved[$row['comment_approved']])) { |
|
193 | + $stats[$approved[$row['comment_approved']]] = $row['num_comments']; |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
197 | 197 | $stats['total_comments'] = $total; |
198 | 198 | $stats['all'] = $total; |
199 | 199 | |
200 | - foreach ( $approved as $key ) { |
|
201 | - if ( empty( $stats[ $key ] ) ) { |
|
202 | - $stats[ $key ] = 0; |
|
200 | + foreach ($approved as $key) { |
|
201 | + if (empty($stats[$key])) { |
|
202 | + $stats[$key] = 0; |
|
203 | 203 | } |
204 | 204 | } |
205 | 205 | |
206 | 206 | $stats = (object) $stats; |
207 | - set_transient( 'as_comment_count', $stats ); |
|
207 | + set_transient('as_comment_count', $stats); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | return $stats; |
@@ -215,32 +215,32 @@ discard block |
||
215 | 215 | * will be regenerated next time ActionScheduler_wpCommentLogger::filter_comment_count() is called. |
216 | 216 | */ |
217 | 217 | public function delete_comment_count_cache() { |
218 | - delete_transient( 'as_comment_count' ); |
|
218 | + delete_transient('as_comment_count'); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
222 | 222 | * @codeCoverageIgnore |
223 | 223 | */ |
224 | 224 | public function init() { |
225 | - add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 ); |
|
226 | - add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 ); |
|
225 | + add_action('action_scheduler_before_process_queue', array($this, 'disable_comment_counting'), 10, 0); |
|
226 | + add_action('action_scheduler_after_process_queue', array($this, 'enable_comment_counting'), 10, 0); |
|
227 | 227 | |
228 | 228 | parent::init(); |
229 | 229 | |
230 | - add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 ); |
|
231 | - add_action( 'wp_count_comments', array( $this, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
232 | - add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 ); |
|
230 | + add_action('pre_get_comments', array($this, 'filter_comment_queries'), 10, 1); |
|
231 | + add_action('wp_count_comments', array($this, 'filter_comment_count'), 20, 2); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs |
|
232 | + add_action('comment_feed_where', array($this, 'filter_comment_feed'), 10, 2); |
|
233 | 233 | |
234 | 234 | // Delete comments count cache whenever there is a new comment or a comment status changes |
235 | - add_action( 'wp_insert_comment', array( $this, 'delete_comment_count_cache' ) ); |
|
236 | - add_action( 'wp_set_comment_status', array( $this, 'delete_comment_count_cache' ) ); |
|
235 | + add_action('wp_insert_comment', array($this, 'delete_comment_count_cache')); |
|
236 | + add_action('wp_set_comment_status', array($this, 'delete_comment_count_cache')); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | public function disable_comment_counting() { |
240 | - wp_defer_comment_counting( true ); |
|
240 | + wp_defer_comment_counting(true); |
|
241 | 241 | } |
242 | 242 | public function enable_comment_counting() { |
243 | - wp_defer_comment_counting( false ); |
|
243 | + wp_defer_comment_counting(false); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | } |