This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrony.php
690 lines (614 loc) · 23.3 KB
/
crony.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
<?php
/*
Plugin Name: Crony Cronjob Manager
Plugin URI: https://www.scottkclark.com/
Description: Create and Manage Cronjobs in WP by loading Scripts via URLs, including Scripts, running Functions, and/or running PHP code. This plugin utilizes the wp_cron API.
Version: 0.5.0
Author: Scott Kingsley Clark
Author URI: https://www.scottkclark.com/
GitHub Plugin URI: https://github.com/sc0ttkclark/crony
*/
global $wpdb;
define( 'CRONY_TBL', $wpdb->prefix . 'crony_' );
define( 'CRONY_VERSION', '050' );
define( 'CRONY_URL', WP_PLUGIN_URL . '/crony' );
define( 'CRONY_DIR', WP_PLUGIN_DIR . '/crony' );
add_action( 'admin_init', 'crony_init' );
add_action( 'admin_menu', 'crony_menu' );
add_action( 'crony', 'crony', 10, 1 );
add_filter( 'cron_schedules', 'crony_schedules', 10, 2 );
if ( is_admin() && isset( $_GET['page'] ) && strpos( $_GET['page'], 'crony' ) !== false ) {
add_action( 'wp_admin_ui_post_save', 'crony_add_job', 10, 2 );
add_action( 'wp_admin_ui_post_delete', 'crony_remove_job', 10, 2 );
}
function crony_install_update() {
global $wpdb;
// check version
$version = intval( get_option( 'crony_version' ) );
if ( empty( $version ) || $version == 10 ) {
// thx pods ;)
$sql = file_get_contents( CRONY_DIR . '/assets/dump.sql' );
$sql_explode = preg_split( "/;\n/", str_replace( 'wp_', $wpdb->prefix, $sql ) );
if ( count( $sql_explode ) == 1 ) {
$sql_explode = preg_split( "/;\r/", str_replace( 'wp_', $wpdb->prefix, $sql ) );
}
for ( $i = 0, $z = count( $sql_explode ); $i < $z; $i ++ ) {
$wpdb->query( $sql_explode[ $i ] );
}
$version = CRONY_VERSION;
delete_option( 'crony_version' );
add_option( 'crony_version', CRONY_VERSION );
}
if ( $version == 11 || $version == 12 ) {
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `email` varchar(255) AFTER `schedule`" );
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `last_run` datetime AFTER `email`" );
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `next_run` datetime AFTER `last_run`" );
// in case these fail (already exist) then on refresh it won't cause issues
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `script` varchar(255) AFTER `next_run`" );
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `function` varchar(255) AFTER `script`" );
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `phpcode` longtext AFTER `function`" );
}
if ( $version < 30 ) {
$wpdb->query( $wpdb->prepare( "UPDATE `" . CRONY_TBL . "jobs` SET `phpcode` = CONCAT(%s,`phpcode`) WHERE `phpcode` != ''", "<?php\n" ) );
$wpdb->query( "DROP TABLE IF EXISTS `" . CRONY_TBL . "logs`" );
$wpdb->query( "CREATE TABLE `" . CRONY_TBL . "logs` (`id` int(10) NOT NULL AUTO_INCREMENT,`crony_id` int(10) NOT NULL,`output` longtext NOT NULL,`real_time` datetime NOT NULL,`start` datetime NOT NULL,`end` datetime NOT NULL,PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8" );
}
if ( $version < 31 ) {
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "logs` CHANGE `real` `real_time` datetime" );
}
if ( $version < 40 ) {
$wpdb->query( "ALTER TABLE `" . CRONY_TBL . "jobs` ADD COLUMN `url` varchar(255) AFTER `next_run`" );
}
if ( $version != CRONY_VERSION ) {
delete_option( 'crony_version' );
add_option( 'crony_version', CRONY_VERSION );
}
}
function crony_init() {
crony_install_update();
global $current_user;
// thx gravity forms, great way of integration with members!
$capabilities = crony_capabilities();
if ( function_exists( 'members_get_capabilities' ) ) {
add_filter( 'members_get_capabilities', 'crony_get_capabilities' );
if ( current_user_can( "crony_full_access" ) ) {
$current_user->remove_cap( "crony_full_access" );
}
$is_admin_with_no_permissions = current_user_can( "administrator" ) && ! crony_current_user_can_any( crony_capabilities() );
if ( $is_admin_with_no_permissions ) {
$role = get_role( "administrator" );
foreach ( $capabilities as $cap ) {
$role->add_cap( $cap );
}
}
} else {
$crony_full_access = current_user_can( "administrator" ) ? "crony_full_access" : "";
$crony_full_access = apply_filters( "crony_full_access", $crony_full_access );
if ( ! empty( $crony_full_access ) ) {
$current_user->add_cap( $crony_full_access );
}
}
}
function crony_menu() {
global $wpdb;
$has_full_access = current_user_can( 'crony_full_access' );
if ( ! $has_full_access && ( current_user_can( 'administrator' ) || is_super_admin() ) ) {
$has_full_access = true;
}
$min_cap = crony_current_user_can_which( crony_capabilities() );
if ( empty( $min_cap ) ) {
$min_cap = 'crony_full_access';
}
add_menu_page( 'Cronjobs', 'Cronjobs', $has_full_access ? 'read' : $min_cap, 'crony', null, CRONY_URL . '/assets/icons/16.png' );
add_submenu_page( 'crony', 'Manage Cronjobs', 'Manage Cronjobs', $has_full_access ? 'read' : 'crony_manage', 'crony', 'crony_manage' );
add_submenu_page( 'crony', 'View Schedule', 'View Schedule', $has_full_access ? 'read' : 'crony_view', 'crony-view', 'crony_view' );
add_submenu_page( 'crony', 'View Logs', 'View Logs', $has_full_access ? 'read' : 'crony_view_logs', 'crony-logs', 'crony_view_logs' );
add_submenu_page( 'crony', 'Settings', 'Settings', $has_full_access ? 'read' : 'crony_settings', 'crony-settings', 'crony_settings' );
}
function crony_get_capabilities( $caps ) {
return array_merge( $caps, crony_capabilities() );
}
function crony_capabilities() {
return [ 'crony_full_access', 'crony_settings', 'crony_manage' ];
}
function crony_current_user_can_any( $caps ) {
if ( ! is_array( $caps ) ) {
return current_user_can( $caps ) || current_user_can( "crony_full_access" );
}
foreach ( $caps as $cap ) {
if ( current_user_can( $cap ) ) {
return true;
}
}
return current_user_can( "crony_full_access" );
}
function crony_current_user_can_which( $caps ) {
foreach ( $caps as $cap ) {
if ( current_user_can( $cap ) ) {
return $cap;
}
}
return "";
}
function crony_settings() {
global $wpdb;
$tables = $wpdb->get_results( 'SHOW TABLES LIKE "' . CRONY_TBL . '%"', ARRAY_N );
// Force reset if tables don't exist
if ( count( $tables ) < 2 ) {
$_POST['reset'] = 1;
}
if ( isset( $_POST['clear-logs'] ) && ! empty( $_POST['clear-logs'] ) ) {
crony_empty_log();
?>
<div id="message" class="updated fade"><p>Crony Logs have been removed</p></div>
<?php
} elseif ( isset( $_POST['reset'] ) && ! empty( $_POST['reset'] ) ) {
crony_reset();
?>
<div id="message" class="updated fade"><p>All Crony Jobs, Logs, and Settings have been removed</p></div>
<?php
}
?>
<div class="wrap">
<div id="icon-edit-pages" class="icon32" style="background-position:0 0;background-image:url(<?php echo CRONY_URL; ?>/assets/icons/32.png);">
<br /></div>
<h2>Crony Cronjob Manager - Settings</h2>
<div style="height:20px;"></div>
<link type="text/css" rel="stylesheet" href="<?php echo CRONY_URL; ?>/assets/admin.css" />
<form method="post" action="">
<table class="form-table">
<tr valign="top">
<th scope="row"><label>Reset Crony Logs</label></th>
<td>
<input name="clear-logs" type="submit" id="clear" value=" Clear Now " onclick="return confirm('Are you sure?');" />
<span class="description">This will remove all Crony Logs</span>
</td>
</tr>
<tr valign="top">
<th scope="row"><label>Reset Crony</label></th>
<td>
<input name="reset" type="submit" id="reset" value=" Reset Now " onclick="return confirm('Are you sure?');" />
<span class="description">This will remove all Crony Jobs, Logs, and Settings</span>
</td>
</tr><!--
<tr valign="top">
<th scope="row"><label for=""></label></th>
<td>
<input name="" type="text" id="" value="0" class="small-text" />
<span class="description"></span>
</td>
</tr>-->
</table><!--
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value=" Save Changes " />
</p>-->
</form>
</div>
<?php
}
function crony_manage() {
require_once CRONY_DIR . '/wp-admin-ui/Admin.class.php';
$columns = [
'name',
'disabled' => [ 'Disabled?', 'type' => 'bool' ],
'start' => [ 'type' => 'datetime' ],
'next_run' => [
'label' => 'Next Run On',
'type' => 'datetime',
'comments' => 'This is the date to run the next Cronjob on',
'comments_top' => true,
],
'last_run' => [ 'label' => 'Last Run On', 'type' => 'datetime' ],
'schedule' => [
'custom_display' => 'crony_schedule_display',
'custom_input' => 'crony_schedule_input',
],
'created' => [ 'label' => 'Date Created', 'type' => 'date' ],
'updated' => [ 'label' => 'Last Modified', 'type' => 'date' ],
];
$form_columns = $columns;
unset( $columns['start'] );
unset( $form_columns['last_run'] );
$form_columns['start'] = [
'label' => 'Start On',
'type' => 'datetime',
'comments' => 'This is the date to start allowing Cronjobs to run, set to the future to delay and override Next Run date',
'comments_top' => true,
];
$form_columns['email'] = [
'label' => 'E-mail Notifications',
'comments' => 'Enter the E-mail Address you would like notifications / output to be sent to',
'comments_top' => true,
];
$form_columns['url'] = [
'label' => 'URL to Load',
'comments' => 'URL of a Page or Script to be loaded',
'comments_top' => true,
];
$form_columns['script'] = [
'label' => 'Script to Include',
'comments' => 'Path of Script to be included',
'comments_top' => true,
];
$form_columns['function'] = [ 'label' => 'Function to Run' ];
$form_columns['phpcode'] = [
'label' => 'Custom PHP to Run',
'type' => 'desc',
'comments' => '<strong>NOTE:</strong> You must put your <?php tag to initiate PHP where you want it used',
'comments_top' => true,
];
$form_columns['created']['date_touch_on_create'] = true;
$form_columns['created']['display'] = false;
$form_columns['updated']['date_touch'] = true;
$form_columns['updated']['display'] = false;
$admin = new WP_Admin_UI( [
'css' => CRONY_URL . '/assets/admin.css',
'item' => 'Cronjob',
'items' => 'Cronjobs',
'table' => CRONY_TBL . 'jobs',
'columns' => $columns,
'form_columns' => $form_columns,
'icon' => CRONY_URL . '/assets/icons/32.png',
] );
$admin->go();
}
function crony_schedule_display( $column, $data, $obj ) {
$schedules = wp_get_schedules();
return $schedules[ $column ]['display'];
}
function crony_schedule_input( $column, $attributes, $obj ) {
$schedules = wp_get_schedules();
$interval = [];
foreach ( $schedules as $key => $value ) {
$interval[ $key ] = $value['interval'];
}
array_multisort( $interval, SORT_NUMERIC, $schedules );
?>
<select name="<?php echo $column; ?>">
<?php
foreach ( $schedules as $id => $schedule ) {
?>
<option value="<?php echo $id; ?>"<?php echo( $obj->row[ $column ] == $id ? ' SELECTED' : '' ); ?>><?php echo $schedule['display']; ?></option>
<?php
}
?>
</select>
<?php
}
function crony_view() {
require_once CRONY_DIR . '/wp-admin-ui/Admin.class.php';
$admin = new WP_Admin_UI();
$cronjobs = _get_cron_array();
$schedules = wp_get_schedules();
if ( isset( $_GET['crony_cronjob_remove'] ) && ! empty( $_GET['crony_cronjob_remove'] ) ) {
$remove = explode( ':', $_GET['crony_cronjob_remove'] );
$removal_timestamp = $remove[0];
$removal_function = $remove[1];
$removal_key = $remove[2];
$error = false;
if ( isset( $cronjobs[ $removal_timestamp ] ) ) {
if ( isset( $cronjobs[ $removal_timestamp ][ $removal_function ] ) && isset( $cronjobs[ $removal_timestamp ][ $removal_function ][ $removal_key ] ) ) {
wp_unschedule_event( $removal_timestamp, $removal_function, $cronjobs[ $removal_timestamp ][ $removal_function ][ $removal_key ]['args'] );
unset( $cronjobs[ $removal_timestamp ][ $removal_function ][ $removal_key ] );
if ( empty( $cronjobs[ $removal_timestamp ][ $removal_function ] ) ) {
unset( $cronjobs[ $removal_timestamp ][ $removal_function ] );
}
if ( empty( $cronjobs[ $removal_timestamp ] ) ) {
unset( $cronjobs[ $removal_timestamp ] );
}
} else {
$error = '<strong>Error:</strong> Cronjob not found.';
}
} else {
$error = '<strong>Error:</strong> Cronjob not found.';
}
if ( false === $error ) {
$admin->message( 'Cronjob removed.' );
} else {
$admin->error( $error );
}
}
$interval = [];
foreach ( $schedules as $schedule => $info ) {
$interval[ $schedule ] = $info['interval'];
}
array_multisort( $interval, SORT_NUMERIC, $schedules );
?>
<div class="wrap wp-admin-ui-wrap">
<div id="icon-edit-pages" class="icon32" style="background-position:0 0;background-image:url(<?php echo CRONY_URL; ?>/assets/icons/32.png);">
<br /></div>
<h2>View Scheduled Cronjobs</h2>
<div style="height:20px;"></div>
<link type="text/css" rel="stylesheet" href="<?php echo CRONY_URL; ?>/assets/admin.css" />
<h3>Schedule Cronjobs</h3>
<table class="widefat fixed">
<thead>
<tr>
<th scope="col">Function</th>
<th scope="col">Schedule</th>
<th scope="col">Next Run</th>
<th scope="col">Arguments</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach ( $cronjobs as $timestamp => $cron ) {
$cron = (array) $cron;
foreach ( $cron as $function => $events ) {
$events = (array) $events;
foreach ( $events as $key => $event ) {
?>
<tr>
<th scope="row"><?php echo esc_html( $function ); ?></th>
<td><?php echo( $event['schedule'] ? esc_html( $schedules[ $event['schedule'] ]['display'] ) : '<em>One-off event</em>' ); ?></td>
<td><?php echo get_date_from_gmt( date_i18n( 'Y-m-d H:i:s', $timestamp, true ), 'M j, Y @ h:ia' ); ?></td>
<td>
<?php
if ( 'crony' == $function && ! empty( $event['args'] ) ) {
$crony_id = (int) $event['args'][0];
?>
<a href="admin.php?page=crony&action=edit&id=<?php echo (int) $crony_id; ?>"><?php echo esc_html( crony_get_job_name( $crony_id ) ); ?></a>
<?php
} elseif ( ! empty( $event['args'] ) ) {
?>
<ul>
<?php
foreach ( $event['args'] as $arg => $val ) {
?>
<li>
<strong>[Arg <?php echo esc_html( $arg ); ?>]:</strong> <?php echo esc_html( $val ); ?>
</li>
<?php
}
?>
</ul>
<?php
} else {
echo '<em>N/A</em>';
}
?>
</td>
<td>
<input type="button" class="button" value=" Remove " onclick="crony_remove_job(this);" />
<input type="hidden" class="crony-identifier" value="<?php echo esc_attr( $timestamp . ':' . $function . ':' . $key ); ?>" />
</td>
</tr>
<?php
}
}
}
?>
</tbody>
</table>
<script type="text/javascript">
function crony_remove_job( that ) {
if ( confirm( 'Are you sure you want to remove this Cronjob?\n\nPlease Note: Whatever scheduled this job may add it back automatically.' ) ) {
var crony_identifier = jQuery( that ).parent().find( '.crony-identifier' ).val();
jQuery( that ).parent().parent().slideUp( 'fast' );
document.location = '<?php echo $admin->var_update( [ 'page' => $_GET['page'] ], false, false, true ); ?>&crony_cronjob_remove=' + encodeURI( crony_identifier );
}
}
</script>
<div style="height:20px;"></div>
<h3>Available Schedules</h3>
<table class="widefat fixed">
<thead>
<tr>
<th scope="col">Schedule</th>
<th scope="col">Interval</th>
</tr>
</thead>
<tbody>
<?php
foreach ( $schedules as $id => $schedule ) {
?>
<tr>
<th scope="row"><?php echo esc_html( $schedule['display'] ); ?></th>
<td><?php echo human_time_diff( 0, (int) $schedule['interval'] ); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
function crony_view_logs() {
require_once CRONY_DIR . '/wp-admin-ui/Admin.class.php';
$columns = [
'crony_id' => [
'label' => 'Cronjob',
'custom_display' => 'crony_cronjob_name',
'custom_view' => 'crony_cronjob_name',
],
'real_time' => [ 'label' => 'Scheduled Time', 'type' => 'datetime' ],
'start' => [ 'label' => 'Start Time', 'type' => 'datetime' ],
'end' => [ 'label' => 'End Time', 'type' => 'datetime' ],
];
$view_columns = $columns;
$view_columns['output'] = [ 'label' => 'Cronjob Output', 'custom_view' => 'crony_cronjob_output' ];
$admin = new WP_Admin_UI( [
'css' => CRONY_URL . '/assets/admin.css',
'item' => 'Cronjob Log',
'items' => 'Cronjob Logs',
'table' => CRONY_TBL . 'logs',
'columns' => $columns,
'view_columns' => $view_columns,
'icon' => CRONY_URL . '/assets/icons/32.png',
'readonly' => true,
'view' => true,
] );
$admin->go();
}
function crony_cronjob_name( $id ) {
global $wpdb;
return @current( $wpdb->get_col( "SELECT `name` FROM `" . CRONY_TBL . "jobs` WHERE `id`=" . $wpdb->_real_escape( $id ) ) );
}
function crony_cronjob_output( $output ) {
if ( 0 < strlen( $output ) ) {
return '<textarea rows="10" cols="50">' . esc_textarea( $output ) . '</textarea>';
}
return 'N/A';
}
function crony_get_job_name( $id ) {
global $wpdb;
$row = @current( $wpdb->get_results( 'SELECT `name` FROM `' . CRONY_TBL . 'jobs` WHERE `id`=' . $wpdb->_real_escape( $id ), ARRAY_A ) );
if ( false === $row ) {
return '<em>Job not found</em>';
}
return $row['name'];
}
function crony( $id ) {
global $wpdb;
$row = @current( $wpdb->get_results( 'SELECT * FROM `' . CRONY_TBL . 'jobs` WHERE `disabled`=0 AND `id`=' . $wpdb->_real_escape( $id ), ARRAY_A ) );
if ( false === $row ) {
return false;
}
$start = current_time( 'mysql' );
$real = $row['next_run'];
ob_start();
if ( 0 < strlen( $row['url'] ) && false !== @parse_url( $row['url'] ) ) {
echo wp_remote_retrieve_body( wp_remote_post( $row['url'], [ 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ] ) );
}
if ( 0 < strlen( $row['script'] ) ) {
include_once $row['script'];
}
if ( 0 < strlen( $row['function'] ) && function_exists( "{$row['function']}" ) ) {
echo $row['function']();
}
if ( 0 < strlen( $row['phpcode'] ) ) {
eval( '?>' . $row['phpcode'] );
}
$output = ob_get_clean();
$schedules = wp_get_schedules();
$last_run = date_i18n( 'Y-m-d H:i:s' );
$next_run = date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) + $schedules[ $row['schedule'] ]['interval'] );
$wpdb->query( "UPDATE `" . CRONY_TBL . "jobs` SET `last_run` = '$last_run', `next_run` = '$next_run' WHERE `id`=" . $wpdb->_real_escape( $id ) );
if ( ! empty( $row['email'] ) ) {
wp_mail( $row['email'], '[' . get_bloginfo( 'sitename' ) . '] Cronjob Run: ' . $row['name'], 'The following was output (if any) from the cronjob <strong>' . $row['name'] . '</strong> that was run on ' . date_i18n( 'm/d/Y h:i:sa' ) . '<br /><br />' . "\r\n\r\n" . $output, "Content-Type: text/html" );
}
$end = current_time( 'mysql' );
crony_add_log( $id, $output, $real, $start, $end );
if ( 0 < strlen( $output ) ) {
return $output;
}
return true;
}
function crony_schedules( $schedules ) {
if ( ! isset( $schedules['twicehourly'] ) ) {
$schedules['twicehourly'] = [ 'interval' => 1800, 'display' => __( 'Twice Hourly' ) ];
}
if ( ! isset( $schedules['weekly'] ) ) {
$schedules['weekly'] = [ 'interval' => 604800, 'display' => __( 'Once Weekly' ) ];
}
if ( ! isset( $schedules['twiceweekly'] ) ) {
$schedules['twiceweekly'] = [ 'interval' => 302400, 'display' => __( 'Twice Weekly' ) ];
}
if ( ! isset( $schedules['monthly'] ) ) {
$schedules['monthly'] = [ 'interval' => 2628002, 'display' => __( 'Once Monthly' ) ];
}
if ( ! isset( $schedules['twicemonthly'] ) ) {
$schedules['twicemonthly'] = [ 'interval' => 1314001, 'display' => __( 'Twice Monthly' ) ];
}
if ( ! isset( $schedules['yearly'] ) ) {
$schedules['yearly'] = [ 'interval' => 31536000, 'display' => __( 'Once Yearly' ) ];
}
if ( ! isset( $schedules['twiceyearly'] ) ) {
$schedules['twiceyearly'] = [ 'interval' => 15768012, 'display' => __( 'Twice Yearly' ) ];
}
if ( ! isset( $schedules['fouryearly'] ) ) {
$schedules['fouryearly'] = [ 'interval' => 7884006, 'display' => __( 'Four Times Yearly' ) ];
}
if ( ! isset( $schedules['sixyearly'] ) ) {
$schedules['sixyearly'] = [ 'interval' => 5256004, 'display' => __( 'Six Times Yearly' ) ];
}
return apply_filters( 'crony_schedules', $schedules );
}
function crony_add_job( $args, $obj ) {
if ( $obj[0]->table != CRONY_TBL . 'jobs' ) {
return false;
}
if ( ! isset( $args[3] ) || false === $args[3] || ! isset( $args[2] ) || empty( $args[2] ) ) {
return false;
}
crony_remove_job( $args, $obj );
if ( $args[2]['disabled'] == 1 ) {
return true;
}
$timestamp = mysql2date( 'U', get_gmt_from_date( $args[2]['next_run'] ) );
$recurrence = $args[2]['schedule'];
return wp_schedule_event( $timestamp, $recurrence, 'crony', [ $args[1] ] );
}
function crony_remove_job( $args, $obj ) {
if ( $obj[0]->table != CRONY_TBL . 'jobs' ) {
return false;
}
$timestamp = false;
$key = md5( serialize( [ $args[1] ] ) );
$schedules = _get_cron_array();
foreach ( $schedules as $ts => $schedule ) {
if ( isset( $schedule['crony'] ) && isset( $schedule['crony'][ $key ] ) ) {
$timestamp = $ts;
wp_unschedule_event( $timestamp, 'crony', [ $args[1] ] );
}
}
if ( false === $timestamp ) {
return false;
}
return true;
}
function crony_add_log( $id, $output, $real, $start, $end ) {
global $wpdb;
// Drop any older logs
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . CRONY_TBL . "logs` WHERE `end` < %s", [ date_i18n( 'Y-m-d H:i:s', strtotime( '-2 weeks' ) ) ] ) );
return $wpdb->query( $wpdb->prepare( "INSERT INTO `" . CRONY_TBL . "logs` (`crony_id`, `output`, `real_time`, `start`, `end`) VALUES (%d, %s, %s, %s, %s)", [
$id,
$output,
$real,
$start,
$end,
] ) );
}
function crony_empty_log() {
global $wpdb;
return $wpdb->query( "TRUNCATE `" . CRONY_TBL . "logs`" );
}
function crony_reset() {
global $wpdb;
$tables = $wpdb->get_results( 'SHOW TABLES LIKE "' . CRONY_TBL . '%"', ARRAY_N );
$remove_jobs = false;
// Force reset if tables don't exist
if ( 2 == count( $tables ) ) {
$remove_jobs = true;
} elseif ( ! empty( $tables[0][0] ) && CRONY_TBL . 'jobs' == $tables[0][0] ) {
$remove_jobs = true;
}
// Get current crons
$schedules = _get_cron_array();
// Remove Existing Jobs
if ( $remove_jobs ) {
$jobs = $wpdb->get_col( 'SELECT `id` FROM `' . CRONY_TBL . 'jobs`' );
foreach ( $jobs as $job ) {
$key = md5( serialize( [ $job ] ) );
foreach ( $schedules as $ts => $schedule ) {
if ( isset( $schedule['crony'] ) && isset( $schedule['crony'][ $key ] ) ) {
wp_unschedule_event( $ts, 'crony', [ $job ] );
}
}
}
$wpdb->query( "DROP TABLE IF EXISTS `" . CRONY_TBL . "jobs`" );
$wpdb->query( "DROP TABLE IF EXISTS `" . CRONY_TBL . "logs`" );
} else {
// Remove general crony events
foreach ( $schedules as $ts => $schedule ) {
if ( isset( $schedule['crony'] ) ) {
wp_unschedule_event( $ts, 'crony' );
}
}
}
// Delete option
delete_option( 'crony_version' );
crony_install_update();
}