forked from johnbillion/wp-crontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-crontrol.php
2231 lines (1955 loc) · 69.9 KB
/
wp-crontrol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: WP Crontrol
* Plugin URI: https://wordpress.org/plugins/wp-crontrol/
* Description: WP Crontrol enables you to view and control what's happening in the WP-Cron system.
* Author: John Blackbourn & crontributors
* Author URI: https://github.com/johnbillion/wp-crontrol/graphs/contributors
* Version: 1.13.2
* Text Domain: wp-crontrol
* Domain Path: /languages/
* Requires PHP: 5.6
* License: GPL v2 or later
*
* LICENSE
* This file is part of WP Crontrol.
*
* WP Crontrol is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @package wp-crontrol
* @author John Blackbourn <[email protected]> & Edward Dale <[email protected]>
* @copyright Copyright 2008 Edward Dale, 2012-2022 John Blackbourn
* @license http://www.gnu.org/licenses/gpl.txt GPL 2.0
* @link https://wordpress.org/plugins/wp-crontrol/
* @since 0.2
*/
namespace Crontrol;
use Crontrol\Event\Table;
use stdClass;
use WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! version_compare( PHP_VERSION, '5.6', '>=' ) ) {
return;
}
$autoload = __DIR__ . '/vendor/autoload.php';
if ( ! file_exists( $autoload ) ) {
return;
}
require_once $autoload;
require_once __DIR__ . '/src/event.php';
require_once __DIR__ . '/src/schedule.php';
const TRANSIENT = 'crontrol-message-%d';
const PAUSED_OPTION = 'wp_crontrol_paused';
/**
* Hook onto all of the actions and filters needed by the plugin.
*
* @return void
*/
function init_hooks() {
$plugin_file = plugin_basename( __FILE__ );
add_action( 'init', __NAMESPACE__ . '\action_init' );
add_action( 'init', __NAMESPACE__ . '\action_handle_posts' );
add_action( 'admin_menu', __NAMESPACE__ . '\action_admin_menu' );
add_action( 'wp_ajax_crontrol_checkhash', __NAMESPACE__ . '\ajax_check_events_hash' );
add_filter( "plugin_action_links_{$plugin_file}", __NAMESPACE__ . '\plugin_action_links', 10, 4 );
add_filter( 'removable_query_args', __NAMESPACE__ . '\filter_removable_query_args' );
add_filter( 'pre_unschedule_event', __NAMESPACE__ . '\maybe_clear_doing_cron' );
add_filter( 'plugin_row_meta', __NAMESPACE__ . '\filter_plugin_row_meta', 10, 4 );
add_action( 'load-tools_page_crontrol_admin_manage_page', __NAMESPACE__ . '\setup_manage_page' );
add_filter( 'cron_schedules', __NAMESPACE__ . '\filter_cron_schedules' );
add_action( 'crontrol_cron_job', __NAMESPACE__ . '\action_php_cron_event' );
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'crontrol/tab-header', __NAMESPACE__ . '\show_cron_status', 20 );
}
/**
* Sets an error message to show to the current user after a redirect.
*
* @param string $message The error message text.
* @return bool Whether the message was saved.
*/
function set_message( $message ) {
$key = sprintf(
TRANSIENT,
get_current_user_id()
);
return set_transient( $key, $message, 60 );
}
/**
* Gets the error message to show to the current user after a redirect.
*
* @return string The error message text.
*/
function get_message() {
$key = sprintf(
TRANSIENT,
get_current_user_id()
);
return get_transient( $key );
}
/**
* Filters the array of row meta for each plugin in the Plugins list table.
*
* @param array<int,string> $plugin_meta An array of the plugin row's meta data.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @return array<int,string> An array of the plugin row's meta data.
*/
function filter_plugin_row_meta( array $plugin_meta, $plugin_file ) {
if ( 'wp-crontrol/wp-crontrol.php' !== $plugin_file ) {
return $plugin_meta;
}
$plugin_meta[] = sprintf(
'<a href="%1$s"><span class="dashicons dashicons-star-filled" aria-hidden="true" style="font-size:14px;line-height:1.3"></span>%2$s</a>',
'https://github.com/sponsors/johnbillion',
esc_html_x( 'Sponsor', 'verb', 'wp-crontrol' )
);
return $plugin_meta;
}
/**
* Run using the 'init' action.
*
* @return void
*/
function action_init() {
load_plugin_textdomain( 'wp-crontrol', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
/** @var array<string, true>|false $paused */
$paused = get_option( PAUSED_OPTION, array() );
if ( is_array( $paused ) ) {
foreach ( $paused as $hook => $value ) {
add_action( $hook, __NAMESPACE__ . '\\pauser', -99999 );
}
}
}
/**
* @return void
*/
function pauser() {
remove_all_actions( current_filter() );
}
/**
* Handles any POSTs and GETs made by the plugin. Run using the 'init' action.
*
* @return void
*/
function action_handle_posts() {
$request = new Request();
if ( isset( $_POST['crontrol_action'] ) && ( 'new_cron' === $_POST['crontrol_action'] ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to add new cron events.', 'wp-crontrol' ), 401 );
}
check_admin_referer( 'crontrol-new-cron' );
$cr = $request->init( wp_unslash( $_POST ) );
if ( 'crontrol_cron_job' === $cr->hookname && ! current_user_can( 'edit_files' ) ) {
wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ), 401 );
}
$args = json_decode( $cr->args, true );
if ( empty( $args ) || ! is_array( $args ) ) {
$args = array();
}
$next_run_local = ( 'custom' === $cr->next_run_date_local ) ? $cr->next_run_date_local_custom_date . ' ' . $cr->next_run_date_local_custom_time : $cr->next_run_date_local;
add_filter( 'schedule_event', function( $event ) {
if ( ! $event ) {
return $event;
}
/**
* Fires after a new cron event is added.
*
* @param stdClass $event {
* An object containing the event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
*/
do_action( 'crontrol/added_new_event', $event );
return $event;
}, 99 );
$added = Event\add( $next_run_local, $cr->schedule, $cr->hookname, $args );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '5',
'crontrol_name' => rawurlencode( $cr->hookname ),
);
if ( is_wp_error( $added ) ) {
set_message( $added->get_error_message() );
$redirect['crontrol_message'] = 'error';
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_POST['crontrol_action'] ) && ( 'new_php_cron' === $_POST['crontrol_action'] ) ) {
if ( ! current_user_can( 'edit_files' ) ) {
wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ), 401 );
}
check_admin_referer( 'crontrol-new-cron' );
$cr = $request->init( wp_unslash( $_POST ) );
$next_run_local = ( 'custom' === $cr->next_run_date_local ) ? $cr->next_run_date_local_custom_date . ' ' . $cr->next_run_date_local_custom_time : $cr->next_run_date_local;
$args = array(
'code' => $cr->hookcode,
'name' => $cr->eventname,
);
add_filter( 'schedule_event', function( $event ) {
if ( ! $event ) {
return $event;
}
/**
* Fires after a new PHP cron event is added.
*
* @param stdClass $event {
* An object containing the event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
*/
do_action( 'crontrol/added_new_php_event', $event );
return $event;
}, 99 );
$added = Event\add( $next_run_local, $cr->schedule, 'crontrol_cron_job', $args );
$hookname = ( ! empty( $cr->eventname ) ) ? $cr->eventname : __( 'PHP Cron', 'wp-crontrol' );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '5',
'crontrol_name' => rawurlencode( $hookname ),
);
if ( is_wp_error( $added ) ) {
set_message( $added->get_error_message() );
$redirect['crontrol_message'] = 'error';
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_POST['crontrol_action'] ) && ( 'edit_cron' === $_POST['crontrol_action'] ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to edit cron events.', 'wp-crontrol' ), 401 );
}
$cr = $request->init( wp_unslash( $_POST ) );
check_admin_referer( "crontrol-edit-cron_{$cr->original_hookname}_{$cr->original_sig}_{$cr->original_next_run_utc}" );
if ( 'crontrol_cron_job' === $cr->hookname && ! current_user_can( 'edit_files' ) ) {
wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ), 401 );
}
$args = json_decode( $cr->args, true );
if ( empty( $args ) || ! is_array( $args ) ) {
$args = array();
}
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '4',
'crontrol_name' => rawurlencode( $cr->hookname ),
);
$original = Event\get_single( $cr->original_hookname, $cr->original_sig, $cr->original_next_run_utc );
if ( is_wp_error( $original ) ) {
set_message( $original->get_error_message() );
$redirect['crontrol_message'] = 'error';
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
}
$deleted = Event\delete( $cr->original_hookname, $cr->original_sig, $cr->original_next_run_utc );
if ( is_wp_error( $deleted ) ) {
set_message( $deleted->get_error_message() );
$redirect['crontrol_message'] = 'error';
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
}
$next_run_local = ( 'custom' === $cr->next_run_date_local ) ? $cr->next_run_date_local_custom_date . ' ' . $cr->next_run_date_local_custom_time : $cr->next_run_date_local;
/**
* Modifies an event before it is scheduled.
*
* @param stdClass|false $event An object containing the new event's data, or boolean false.
*/
add_filter( 'schedule_event', function( $event ) use ( $original ) {
if ( ! $event ) {
return $event;
}
/**
* Fires after a cron event is edited.
*
* @param stdClass $event {
* An object containing the new event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
* @param stdClass $original {
* An object containing the original event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
*/
do_action( 'crontrol/edited_event', $event, $original );
return $event;
}, 99 );
$added = Event\add( $next_run_local, $cr->schedule, $cr->hookname, $args );
if ( is_wp_error( $added ) ) {
set_message( $added->get_error_message() );
$redirect['crontrol_message'] = 'error';
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_POST['crontrol_action'] ) && ( 'edit_php_cron' === $_POST['crontrol_action'] ) ) {
if ( ! current_user_can( 'edit_files' ) ) {
wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ), 401 );
}
$cr = $request->init( wp_unslash( $_POST ) );
check_admin_referer( "crontrol-edit-cron_{$cr->original_hookname}_{$cr->original_sig}_{$cr->original_next_run_utc}" );
$args = array(
'code' => $cr->hookcode,
'name' => $cr->eventname,
);
$hookname = ( ! empty( $cr->eventname ) ) ? $cr->eventname : __( 'PHP Cron', 'wp-crontrol' );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '4',
'crontrol_name' => rawurlencode( $hookname ),
);
$original = Event\get_single( $cr->original_hookname, $cr->original_sig, $cr->original_next_run_utc );
if ( is_wp_error( $original ) ) {
set_message( $original->get_error_message() );
$redirect['crontrol_message'] = 'error';
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
}
$deleted = Event\delete( $cr->original_hookname, $cr->original_sig, $cr->original_next_run_utc );
if ( is_wp_error( $deleted ) ) {
set_message( $deleted->get_error_message() );
$redirect['crontrol_message'] = 'error';
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
}
$next_run_local = ( 'custom' === $cr->next_run_date_local ) ? $cr->next_run_date_local_custom_date . ' ' . $cr->next_run_date_local_custom_time : $cr->next_run_date_local;
/**
* Modifies an event before it is scheduled.
*
* @param stdClass|false $event An object containing the new event's data, or boolean false.
*/
add_filter( 'schedule_event', function( $event ) use ( $original ) {
if ( ! $event ) {
return $event;
}
/**
* Fires after a PHP cron event is edited.
*
* @param stdClass $event {
* An object containing the new event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
* @param stdClass $original {
* An object containing the original event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
*/
do_action( 'crontrol/edited_php_event', $event, $original );
return $event;
}, 99 );
$added = Event\add( $next_run_local, $cr->schedule, 'crontrol_cron_job', $args );
if ( is_wp_error( $added ) ) {
set_message( $added->get_error_message() );
$redirect['crontrol_message'] = 'error';
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_POST['crontrol_new_schedule'] ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to add new cron schedules.', 'wp-crontrol' ), 401 );
}
check_admin_referer( 'crontrol-new-schedule' );
$name = wp_unslash( $_POST['crontrol_schedule_internal_name'] );
$interval = absint( $_POST['crontrol_schedule_interval'] );
$display = wp_unslash( $_POST['crontrol_schedule_display_name'] );
Schedule\add( $name, $interval, $display );
$redirect = array(
'page' => 'crontrol_admin_options_page',
'crontrol_message' => '3',
'crontrol_name' => rawurlencode( $name ),
);
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
exit;
} elseif ( isset( $_GET['crontrol_action'] ) && 'delete-schedule' === $_GET['crontrol_action'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to delete cron schedules.', 'wp-crontrol' ), 401 );
}
$schedule = wp_unslash( $_GET['crontrol_id'] );
check_admin_referer( "crontrol-delete-schedule_{$schedule}" );
Schedule\delete( $schedule );
$redirect = array(
'page' => 'crontrol_admin_options_page',
'crontrol_message' => '2',
'crontrol_name' => rawurlencode( $schedule ),
);
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) );
exit;
} elseif ( ( isset( $_POST['action'] ) && 'crontrol_delete_crons' === $_POST['action'] ) || ( isset( $_POST['action2'] ) && 'crontrol_delete_crons' === $_POST['action2'] ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
}
check_admin_referer( 'bulk-crontrol-events' );
if ( empty( $_POST['crontrol_delete'] ) ) {
return;
}
/**
* @var array<string,array<string,string>>
*/
$delete = (array) wp_unslash( $_POST['crontrol_delete'] );
$deleted = 0;
foreach ( $delete as $next_run_utc => $events ) {
foreach ( (array) $events as $hook => $sig ) {
if ( 'crontrol_cron_job' === $hook && ! current_user_can( 'edit_files' ) ) {
continue;
}
$event = Event\get_single( urldecode( $hook ), $sig, $next_run_utc );
$deleted = Event\delete( urldecode( $hook ), $sig, $next_run_utc );
if ( ! is_wp_error( $deleted ) ) {
$deleted++;
/** This action is documented in wp-crontrol.php */
do_action( 'crontrol/deleted_event', $event );
}
}
}
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_name' => $deleted,
'crontrol_message' => '9',
);
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_GET['crontrol_action'] ) && 'delete-cron' === $_GET['crontrol_action'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
}
$hook = wp_unslash( $_GET['crontrol_id'] );
$sig = wp_unslash( $_GET['crontrol_sig'] );
$next_run_utc = wp_unslash( $_GET['crontrol_next_run_utc'] );
check_admin_referer( "crontrol-delete-cron_{$hook}_{$sig}_{$next_run_utc}" );
if ( 'crontrol_cron_job' === $hook && ! current_user_can( 'edit_files' ) ) {
wp_die( esc_html__( 'You are not allowed to delete PHP cron events.', 'wp-crontrol' ), 401 );
}
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '6',
'crontrol_name' => rawurlencode( $hook ),
);
$event = Event\get_single( $hook, $sig, $next_run_utc );
if ( is_wp_error( $event ) ) {
set_message( $event->get_error_message() );
$redirect['crontrol_message'] = 'error';
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
}
$deleted = Event\delete( $hook, $sig, $next_run_utc );
if ( is_wp_error( $deleted ) ) {
set_message( $deleted->get_error_message() );
$redirect['crontrol_message'] = 'error';
} else {
/**
* Fires after a cron event is deleted.
*
* @param stdClass $event {
* An object containing the event's data.
*
* @type string $hook Action hook to execute when the event is run.
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
* @type string|false $schedule How often the event should subsequently recur.
* @type mixed[] $args Array containing each separate argument to pass to the hook's callback function.
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
* }
*/
do_action( 'crontrol/deleted_event', $event );
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_GET['crontrol_action'] ) && 'delete-hook' === $_GET['crontrol_action'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ), 401 );
}
$hook = wp_unslash( $_GET['crontrol_id'] );
$deleted = false;
check_admin_referer( "crontrol-delete-hook_{$hook}" );
if ( 'crontrol_cron_job' === $hook ) {
wp_die( esc_html__( 'You are not allowed to delete PHP cron events.', 'wp-crontrol' ), 401 );
}
if ( function_exists( 'wp_unschedule_hook' ) ) {
/** @var int|false */
$deleted = wp_unschedule_hook( $hook );
}
if ( 0 === $deleted ) {
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '3',
'crontrol_name' => rawurlencode( $hook ),
);
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( $deleted ) {
/**
* Fires after all cron events with the given hook are deleted.
*
* @param string $hook The hook name.
* @param int $deleted The number of events that were deleted.
*/
do_action( 'crontrol/deleted_all_with_hook', $hook, $deleted );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '2',
'crontrol_name' => rawurlencode( $hook ),
);
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} else {
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '7',
'crontrol_name' => rawurlencode( $hook ),
);
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
}
} elseif ( isset( $_GET['crontrol_action'] ) && 'run-cron' === $_GET['crontrol_action'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to run cron events.', 'wp-crontrol' ), 401 );
}
$hook = wp_unslash( $_GET['crontrol_id'] );
$sig = wp_unslash( $_GET['crontrol_sig'] );
check_admin_referer( "crontrol-run-cron_{$hook}_{$sig}" );
$ran = Event\run( $hook, $sig );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '1',
'crontrol_name' => rawurlencode( $hook ),
);
if ( is_wp_error( $ran ) ) {
$set = set_message( $ran->get_error_message() );
// If we can't store the error message in a transient, just display it.
if ( ! $set ) {
wp_die(
esc_html( $ran->get_error_message() ),
'',
array(
'response' => 500,
'back_link' => true,
)
);
}
$redirect['crontrol_message'] = 'error';
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_GET['crontrol_action'] ) && 'pause-hook' === $_GET['crontrol_action'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to pause or resume cron events.', 'wp-crontrol' ), 401 );
}
$hook = wp_unslash( $_GET['crontrol_id'] );
check_admin_referer( "crontrol-pause-hook_{$hook}" );
$paused = Event\pause( $hook );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '11',
'crontrol_name' => rawurlencode( $hook ),
);
if ( is_wp_error( $paused ) ) {
$set = set_message( $paused->get_error_message() );
// If we can't store the error message in a transient, just display it.
if ( ! $set ) {
wp_die(
esc_html( $paused->get_error_message() ),
'',
array(
'response' => 500,
'back_link' => true,
)
);
}
$redirect['crontrol_message'] = 'error';
} else {
/**
* Fires after a cron event hook is paused.
*
* @param string $hook The event hook name.
*/
do_action( 'crontrol/paused_hook', $hook );
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_GET['crontrol_action'] ) && 'resume-hook' === $_GET['crontrol_action'] ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to pause or resume cron events.', 'wp-crontrol' ), 401 );
}
$hook = wp_unslash( $_GET['crontrol_id'] );
check_admin_referer( "crontrol-resume-hook_{$hook}" );
$resumed = Event\resume( $hook );
$redirect = array(
'page' => 'crontrol_admin_manage_page',
'crontrol_message' => '12',
'crontrol_name' => rawurlencode( $hook ),
);
if ( is_wp_error( $resumed ) ) {
$set = set_message( $resumed->get_error_message() );
// If we can't store the error message in a transient, just display it.
if ( ! $set ) {
wp_die(
esc_html( $resumed->get_error_message() ),
'',
array(
'response' => 500,
'back_link' => true,
)
);
}
$redirect['crontrol_message'] = 'error';
} else {
/**
* Fires after a paused cron event hook is resumed.
*
* @param string $hook The event hook name.
*/
do_action( 'crontrol/resumed_hook', $hook );
}
wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) );
exit;
} elseif ( isset( $_POST['crontrol_action'] ) && 'export-event-csv' === $_POST['crontrol_action'] ) {
check_admin_referer( 'crontrol-export-event-csv', 'crontrol_nonce' );
$type = isset( $_POST['crontrol_hooks_type'] ) ? $_POST['crontrol_hooks_type'] : 'all';
$headers = array(
'hook',
'arguments',
'next_run',
'next_run_gmt',
'action',
'recurrence',
'interval',
);
$filename = sprintf(
'cron-events-%s-%s.csv',
$type,
gmdate( 'Y-m-d-H.i.s' )
);
$csv = fopen( 'php://output', 'w' );
if ( false === $csv ) {
wp_die( esc_html__( 'Could not save CSV file.', 'wp-crontrol' ) );
}
$events = Table::get_filtered_events( Event\get() );
header( 'Content-Type: text/csv; charset=utf-8' );
header(
sprintf(
'Content-Disposition: attachment; filename="%s"',
esc_attr( $filename )
)
);
fputcsv( $csv, $headers );
if ( isset( $events[ $type ] ) ) {
foreach ( $events[ $type ] as $event ) {
$next_run_local = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $event->timestamp ), 'c' );
$next_run_utc = gmdate( 'c', $event->timestamp );
$hook_callbacks = \Crontrol\get_hook_callbacks( $event->hook );
if ( 'crontrol_cron_job' === $event->hook ) {
$args = __( 'PHP Code', 'wp-crontrol' );
} elseif ( empty( $event->args ) ) {
$args = '';
} else {
$args = \Crontrol\json_output( $event->args, false );
}
if ( 'crontrol_cron_job' === $event->hook ) {
$action = __( 'WP Crontrol', 'wp-crontrol' );
} else {
$callbacks = array();
foreach ( $hook_callbacks as $callback ) {
$callbacks[] = $callback['callback']['name'];
}
$action = implode( ',', $callbacks );
}
if ( $event->schedule ) {
$recurrence = Event\get_schedule_name( $event );
if ( is_wp_error( $recurrence ) ) {
$recurrence = $recurrence->get_error_message();
}
} else {
$recurrence = __( 'Non-repeating', 'wp-crontrol' );
}
$row = array(
$event->hook,
$args,
$next_run_local,
$next_run_utc,
$action,
$recurrence,
(int) $event->interval,
);
fputcsv( $csv, $row );
}
}
fclose( $csv );
exit;
}
}
/**
* Adds options & management pages to the admin menu.
*
* Run using the 'admin_menu' action.
*
* @return void
*/
function action_admin_menu() {
$schedules = add_options_page(
esc_html__( 'Cron Schedules', 'wp-crontrol' ),
esc_html__( 'Cron Schedules', 'wp-crontrol' ),
'manage_options',
'crontrol_admin_options_page',
__NAMESPACE__ . '\admin_options_page'
);
$events = add_management_page(
esc_html__( 'Cron Events', 'wp-crontrol' ),
esc_html__( 'Cron Events', 'wp-crontrol' ),
'manage_options',
'crontrol_admin_manage_page',
__NAMESPACE__ . '\admin_manage_page'
);
add_action( "load-{$schedules}", __NAMESPACE__ . '\admin_help_tab' );
add_action( "load-{$events}", __NAMESPACE__ . '\admin_help_tab' );
}
/**
* Adds a Help tab with links to help resources.
*
* @return void
*/
function admin_help_tab() {
$screen = get_current_screen();
if ( ! $screen ) {
return;
}
$content = '<p>' . __( 'There are several places to get help with issues relating to WP-Cron:', 'wp-crontrol' ) . '</p>';
$content .= '<ul>';
$content .= '<li>';
$content .= sprintf(
/* translators: %s: URL to the documentation */
__( '<a href="%s">Read the WP Crontrol wiki</a> which contains information about events that have missed their schedule, problems with spawning a call to the WP-Cron system, and much more.', 'wp-crontrol' ),
'https://github.com/johnbillion/wp-crontrol/wiki'
);
$content .= '</li>';
$content .= '<li>';
$content .= sprintf(
/* translators: %s: URL to the documentation */
__( '<a href="%s">Read the Frequently Asked Questions (FAQ)</a> which cover many common questions and answers.', 'wp-crontrol' ),
'https://wordpress.org/plugins/wp-crontrol/faq/'
);
$content .= '</li>';
$content .= '<li>';
$content .= sprintf(
/* translators: %s: URL to the documentation */
__( '<a href="%s">Read the WordPress.org documentation on WP-Cron</a> for more technical details about the WP-Cron system for developers.', 'wp-crontrol' ),
'https://developer.wordpress.org/plugins/cron/'
);
$content .= '</ul>';
$screen->add_help_tab(
array(
'id' => 'crontrol-help',
'title' => __( 'Help', 'wp-crontrol' ),
'content' => $content,
)
);
}
/**
* Adds items to the plugin's action links on the Plugins listing screen.
*
* @param array<string,string> $actions Array of action links.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @param mixed[] $plugin_data An array of plugin data.
* @param string $context The plugin context.
* @return array<string,string> Array of action links.
*/
function plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
$new = array(
'crontrol-events' => sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ),
esc_html__( 'Events', 'wp-crontrol' )
),
'crontrol-schedules' => sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'options-general.php?page=crontrol_admin_options_page' ) ),
esc_html__( 'Schedules', 'wp-crontrol' )
),
'crontrol-help' => sprintf(
'<a href="%s">%s</a>',
'https://github.com/johnbillion/wp-crontrol/wiki',
esc_html__( 'Help', 'wp-crontrol' )
),
);
return array_merge( $new, $actions );
}
/**
* Gives WordPress the plugin's set of cron schedules.
*
* Called by the `cron_schedules` filter.
*
* @param array<string,array<string,(int|string)>> $scheds Array of cron schedule arrays. Usually empty.
* @return array<string,array<string,(int|string)>> Array of modified cron schedule arrays.
*/
function filter_cron_schedules( array $scheds ) {
$new_scheds = get_option( 'crontrol_schedules', array() );
if ( ! is_array( $new_scheds ) ) {
return $scheds;
}
return array_merge( $new_scheds, $scheds );
}
/**
* Displays the options page for the plugin.
*
* @return void
*/
function admin_options_page() {
$messages = array(
'2' => array(
/* translators: 1: The name of the cron schedule. */
__( 'Deleted the cron schedule %s.', 'wp-crontrol' ),
'success',
),
'3' => array(
/* translators: 1: The name of the cron schedule. */
__( 'Added the cron schedule %s.', 'wp-crontrol' ),
'success',
),
);
if ( isset( $_GET['crontrol_message'] ) && isset( $_GET['crontrol_name'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) {
$hook = wp_unslash( $_GET['crontrol_name'] );
$message = wp_unslash( $_GET['crontrol_message'] );
printf(
'<div id="crontrol-message" class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>',
esc_attr( $messages[ $message ][1] ),
sprintf(
esc_html( $messages[ $message ][0] ),
'<strong>' . esc_html( $hook ) . '</strong>'
)
);
}
$table = new Schedule_List_Table();
$table->prepare_items();