1
1
<?php
2
2
3
+ use WP_CLI \Utils ;
4
+
3
5
/**
4
6
* Schedules, runs, and deletes WP-Cron events.
5
7
*
@@ -31,6 +33,7 @@ class Cron_Event_Command extends WP_CLI_Command {
31
33
'next_run_relative ' ,
32
34
'recurrence ' ,
33
35
);
36
+
34
37
private static $ time_format = 'Y-m-d H:i:s ' ;
35
38
36
39
/**
@@ -112,7 +115,7 @@ public function list_( $args, $assoc_args ) {
112
115
}
113
116
}
114
117
115
- if ( 'ids ' == $ formatter ->format ) {
118
+ if ( 'ids ' === $ formatter ->format ) {
116
119
echo implode ( ' ' , wp_list_pluck ( $ events , 'hook ' ) );
117
120
} else {
118
121
$ formatter ->display_items ( $ events );
@@ -153,13 +156,13 @@ public function list_( $args, $assoc_args ) {
153
156
*/
154
157
public function schedule ( $ args , $ assoc_args ) {
155
158
156
- $ hook = $ args [0 ];
157
- $ next_run = \ WP_CLI \ Utils \get_flag_value ( $ args , 1 , 'now ' );
158
- $ recurrence = \ WP_CLI \ Utils \get_flag_value ( $ args , 2 , false );
159
+ $ hook = $ args [0 ];
160
+ $ next_run = Utils \get_flag_value ( $ args , 1 , 'now ' );
161
+ $ recurrence = Utils \get_flag_value ( $ args , 2 , false );
159
162
160
163
if ( empty ( $ next_run ) ) {
161
164
$ timestamp = time ();
162
- } else if ( is_numeric ( $ next_run ) ) {
165
+ } elseif ( is_numeric ( $ next_run ) ) {
163
166
$ timestamp = absint ( $ next_run );
164
167
} else {
165
168
$ timestamp = strtotime ( $ next_run );
@@ -173,7 +176,7 @@ public function schedule( $args, $assoc_args ) {
173
176
174
177
$ schedules = wp_get_schedules ();
175
178
176
- if ( ! isset ( $ schedules [$ recurrence ] ) ) {
179
+ if ( ! isset ( $ schedules [ $ recurrence ] ) ) {
177
180
WP_CLI ::error ( sprintf ( "'%s' is not a valid schedule name for recurrence. " , $ recurrence ) );
178
181
}
179
182
@@ -215,7 +218,7 @@ public function schedule( $args, $assoc_args ) {
215
218
*/
216
219
public function run ( $ args , $ assoc_args ) {
217
220
218
- if ( empty ( $ args ) && ! \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'due-now ' ) && ! \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'all ' ) ) {
221
+ if ( empty ( $ args ) && ! Utils \get_flag_value ( $ assoc_args , 'due-now ' ) && ! Utils \get_flag_value ( $ assoc_args , 'all ' ) ) {
219
222
WP_CLI ::error ( 'Please specify one or more cron events, or use --due-now/--all. ' );
220
223
}
221
224
@@ -226,15 +229,15 @@ public function run( $args, $assoc_args ) {
226
229
}
227
230
228
231
$ hooks = wp_list_pluck ( $ events , 'hook ' );
229
- foreach ( $ args as $ hook ) {
232
+ foreach ( $ args as $ hook ) {
230
233
if ( ! in_array ( $ hook , $ hooks , true ) ) {
231
234
WP_CLI ::error ( sprintf ( "Invalid cron event '%s' " , $ hook ) );
232
235
}
233
236
}
234
237
235
- if ( \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'due-now ' ) ) {
238
+ if ( Utils \get_flag_value ( $ assoc_args , 'due-now ' ) ) {
236
239
$ due_events = array ();
237
- foreach ( $ events as $ event ) {
240
+ foreach ( $ events as $ event ) {
238
241
if ( ! empty ( $ args ) && ! in_array ( $ event ->hook , $ args , true ) ) {
239
242
continue ;
240
243
}
@@ -243,10 +246,10 @@ public function run( $args, $assoc_args ) {
243
246
}
244
247
}
245
248
$ events = $ due_events ;
246
- } else if ( ! \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'all ' ) ) {
249
+ } elseif ( ! Utils \get_flag_value ( $ assoc_args , 'all ' ) ) {
247
250
$ due_events = array ();
248
- foreach ( $ events as $ event ) {
249
- if ( in_array ( $ event ->hook , $ args ) ) {
251
+ foreach ( $ events as $ event ) {
252
+ if ( in_array ( $ event ->hook , $ args, true ) ) {
250
253
$ due_events [] = $ event ;
251
254
}
252
255
}
@@ -255,9 +258,9 @@ public function run( $args, $assoc_args ) {
255
258
256
259
$ executed = 0 ;
257
260
foreach ( $ events as $ event ) {
258
- $ start = microtime ( true );
261
+ $ start = microtime ( true );
259
262
$ result = self ::run_event ( $ event );
260
- $ total = round ( microtime ( true ) - $ start , 3 );
263
+ $ total = round ( microtime ( true ) - $ start , 3 );
261
264
$ executed ++;
262
265
WP_CLI ::log ( sprintf ( "Executed the cron event '%s' in %ss. " , $ event ->hook , $ total ) );
263
266
}
@@ -275,16 +278,18 @@ public function run( $args, $assoc_args ) {
275
278
protected static function run_event ( stdClass $ event ) {
276
279
277
280
if ( ! defined ( 'DOING_CRON ' ) ) {
281
+ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Using native WordPress constant.
278
282
define ( 'DOING_CRON ' , true );
279
283
}
280
284
281
- if ( $ event ->schedule != false ) {
285
+ if ( false !== $ event ->schedule ) {
282
286
$ new_args = array ( $ event ->time , $ event ->schedule , $ event ->hook , $ event ->args );
283
287
call_user_func_array ( 'wp_reschedule_event ' , $ new_args );
284
288
}
285
289
286
290
wp_unschedule_event ( $ event ->time , $ event ->hook , $ event ->args );
287
291
292
+ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Can't prefix dynamic hooks here, calling registered hooks.
288
293
do_action_ref_array ( $ event ->hook , $ event ->args );
289
294
290
295
return true ;
@@ -316,7 +321,7 @@ public function delete( $args, $assoc_args ) {
316
321
317
322
$ deleted = 0 ;
318
323
foreach ( $ events as $ event ) {
319
- if ( $ event ->hook == $ hook ) {
324
+ if ( $ event ->hook === $ hook ) {
320
325
$ result = self ::delete_event ( $ event );
321
326
if ( $ result ) {
322
327
$ deleted ++;
@@ -327,7 +332,7 @@ public function delete( $args, $assoc_args ) {
327
332
}
328
333
329
334
if ( $ deleted ) {
330
- $ message = ( 1 == $ deleted ) ? "Deleted the cron event '%2 \$s'. " : "Deleted %1 \$d instances of the cron event '%2 \$s'. " ;
335
+ $ message = ( 1 === $ deleted ) ? "Deleted the cron event '%2 \$s'. " : "Deleted %1 \$d instances of the cron event '%2 \$s'. " ;
331
336
WP_CLI ::success ( sprintf ( $ message , $ deleted , $ hook ) );
332
337
} else {
333
338
WP_CLI ::error ( sprintf ( "Invalid cron event '%s'. " , $ hook ) );
@@ -344,7 +349,7 @@ public function delete( $args, $assoc_args ) {
344
349
protected static function delete_event ( stdClass $ event ) {
345
350
$ crons = _get_cron_array ();
346
351
347
- if ( ! isset ( $ crons [$ event ->time ][ $ event ->hook ][ $ event ->sig ] ) ) {
352
+ if ( ! isset ( $ crons [ $ event ->time ][ $ event ->hook ][ $ event ->sig ] ) ) {
348
353
return false ;
349
354
}
350
355
@@ -395,7 +400,7 @@ protected static function get_cron_events() {
395
400
'sig ' => $ sig ,
396
401
'args ' => $ data ['args ' ],
397
402
'schedule ' => $ data ['schedule ' ],
398
- 'interval ' => \ WP_CLI \ Utils \get_flag_value ( $ data , 'interval ' ),
403
+ 'interval ' => Utils \get_flag_value ( $ data , 'interval ' ),
399
404
);
400
405
401
406
}
@@ -425,13 +430,13 @@ private static function interval( $since ) {
425
430
426
431
// array of time period chunks
427
432
$ chunks = array (
428
- array ( 60 * 60 * 24 * 365 , \_n_noop ( ' %s year ', ' %s years ' ) ),
429
- array ( 60 * 60 * 24 * 30 , \_n_noop ( ' %s month ', ' %s months ' ) ),
430
- array ( 60 * 60 * 24 * 7 , \_n_noop ( ' %s week ', ' %s weeks ' ) ),
431
- array ( 60 * 60 * 24 , \_n_noop ( ' %s day ', ' %s days ' ) ),
432
- array ( 60 * 60 , \_n_noop ( ' %s hour ', ' %s hours ' ) ),
433
- array ( 60 , \_n_noop ( ' %s minute ', ' %s minutes ' ) ),
434
- array ( 1 , \_n_noop ( ' %s second ', ' %s seconds ' ) ),
433
+ array ( 60 * 60 * 24 * 365 , ' year ' ),
434
+ array ( 60 * 60 * 24 * 30 , ' month ' ),
435
+ array ( 60 * 60 * 24 * 7 , ' week ' ),
436
+ array ( 60 * 60 * 24 , ' day ' ),
437
+ array ( 60 * 60 , ' hour ' ),
438
+ array ( 60 , ' minute ' ),
439
+ array ( 1 , ' second ' ),
435
440
);
436
441
437
442
// we only want to output two chunks of time here, eg:
@@ -441,26 +446,28 @@ private static function interval( $since ) {
441
446
442
447
// step one: the first chunk
443
448
for ( $ i = 0 , $ j = count ( $ chunks ); $ i < $ j ; $ i ++ ) {
444
- $ seconds = $ chunks [$ i ][0 ];
445
- $ name = $ chunks [$ i ][1 ];
449
+ $ seconds = $ chunks [ $ i ][0 ];
450
+ $ name = $ chunks [ $ i ][1 ];
446
451
447
452
// finding the biggest chunk (if the chunk fits, break)
448
- if ( ( $ count = floor ( $ since / $ seconds ) ) != 0 ){
453
+ $ count = floor ( $ since / $ seconds );
454
+ if ( floatval ( 0 ) !== $ count ) {
449
455
break ;
450
456
}
451
457
}
452
458
453
459
// set output var
454
- $ output = sprintf ( \_n ( $ name [ 0 ], $ name[ 1 ], $ count ), $ count );
460
+ $ output = sprintf ( ' %d %s ' , $ count , Utils \pluralize ( $ name, absint ( $ count ) ) );
455
461
456
462
// step two: the second chunk
457
463
if ( $ i + 1 < $ j ) {
458
- $ seconds2 = $ chunks [$ i + 1 ][0 ];
459
- $ name2 = $ chunks [$ i + 1 ][1 ];
464
+ $ seconds2 = $ chunks [ $ i + 1 ][0 ];
465
+ $ name2 = $ chunks [ $ i + 1 ][1 ];
460
466
461
- if ( ( $ count2 = floor ( ( $ since - ( $ seconds * $ count ) ) / $ seconds2 ) ) != 0 ) {
467
+ $ count2 = floor ( ( $ since - ( $ seconds * $ count ) ) / $ seconds2 );
468
+ if ( floatval ( 0 ) !== $ count2 ) {
462
469
// add to output var
463
- $ output .= ' ' . sprintf ( \_n ( $ name2 [ 0 ], $ name2[ 1 ], $ count2 ), $ count2 );
470
+ $ output .= ' ' . sprintf ( ' %d %s ' , $ count2 , Utils \pluralize ( $ name2, absint ( $ count2 ) ) );
464
471
}
465
472
}
466
473
0 commit comments