| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('Unauthorized Access'); |
| 3 |
|
| 4 |
if (!Phpinfo_WP_License::is_valid()) { |
| 5 |
phpinfowp_render_feature_lock([ |
| 6 |
'feature' => 'WP Cron Monitor', |
| 7 |
'icon' => 'dashicons-clock', |
| 8 |
'tagline' => 'Find overdue cron events, orphan hooks, and recently-run tasks that hint at performance issues.', |
| 9 |
'previews' => [ |
| 10 |
'Overdue events: <strong>—</strong>', |
| 11 |
'Next scheduled run: <strong>—</strong>', |
| 12 |
'Orphan hooks: <strong>—</strong>', |
| 13 |
], |
| 14 |
]); |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
if (isset($_POST['phpinfowp_cron_run']) && check_admin_referer('phpinfowp_cron_nonce')) { |
| 19 |
$hook = sanitize_text_field($_POST['hook'] ?? ''); |
| 20 |
if (Phpinfo_WP_Cron_Monitor::run_now($hook)) { |
| 21 |
$msg = "Hook '{$hook}' triggered to run immediately."; |
| 22 |
} |
| 23 |
} |
| 24 |
if (isset($_POST['phpinfowp_cron_delete']) && check_admin_referer('phpinfowp_cron_nonce')) { |
| 25 |
$hook = sanitize_text_field($_POST['hook'] ?? ''); |
| 26 |
$ts = (int) ($_POST['ts'] ?? 0); |
| 27 |
if (Phpinfo_WP_Cron_Monitor::delete($hook, $ts)) { |
| 28 |
$msg = "Event removed from schedule."; |
| 29 |
} |
| 30 |
} |
| 31 |
if (isset($_POST['phpinfowp_cron_purge']) && check_admin_referer('phpinfowp_cron_nonce')) { |
| 32 |
$hook = sanitize_text_field($_POST['hook'] ?? ''); |
| 33 |
$removed = Phpinfo_WP_Cron_Monitor::purge_hook($hook); |
| 34 |
$msg = $removed > 0 |
| 35 |
? sprintf('Purged %d scheduled instance%s of "%s".', $removed, $removed === 1 ? '' : 's', $hook) |
| 36 |
: "Removed all scheduled instances of \"{$hook}\"."; |
| 37 |
} |
| 38 |
|
| 39 |
$events = Phpinfo_WP_Cron_Monitor::events(); |
| 40 |
$summary = Phpinfo_WP_Cron_Monitor::summary(); |
| 41 |
?> |
| 42 |
|
| 43 |
<div class="phpinfowp-pro-page"> |
| 44 |
|
| 45 |
<div class="phpinfowp-page-header"> |
| 46 |
<div> |
| 47 |
<h1>WP Cron Monitor <span class="phpinfowp-pro-badge"><?php _e('PRO', 'phpinfo-wp'); ?></span></h1> |
| 48 |
<p class="phpinfowp-page-subtitle"><?php _e('Inspect scheduled events, spot missed crons, and find orphans', 'phpinfo-wp'); ?></p> |
| 49 |
</div> |
| 50 |
</div> |
| 51 |
|
| 52 |
<?php if (isset($msg)): ?> |
| 53 |
<div class="notice notice-success inline" style="margin:0 0 20px"><p><?php echo esc_html($msg); ?></p></div> |
| 54 |
<?php endif; ?> |
| 55 |
|
| 56 |
<?php if ($summary['disabled']): ?> |
| 57 |
<div class="notice notice-warning inline" style="margin:0 0 20px"> |
| 58 |
<p><strong>WP_CRON is disabled</strong> via the <code>DISABLE_WP_CRON</code> constant. Make sure a real system cron is calling <code>wp-cron.php</code>, or scheduled tasks will never run.</p> |
| 59 |
</div> |
| 60 |
<?php endif; ?> |
| 61 |
|
| 62 |
<!-- Summary card --> |
| 63 |
<div class="phpinfowp-cron-summary"> |
| 64 |
<div class="phpinfowp-cron-stat"> |
| 65 |
<div class="phpinfowp-cron-stat-num"><?php echo (int)$summary['total']; ?></div> |
| 66 |
<div class="phpinfowp-cron-stat-label"><?php _e('Scheduled events', 'phpinfo-wp'); ?></div> |
| 67 |
</div> |
| 68 |
<div class="phpinfowp-cron-stat" style="border-top-color:<?php echo $summary['overdue'] ? '#d63638' : '#00a32a'; ?>"> |
| 69 |
<div class="phpinfowp-cron-stat-num" style="color:<?php echo $summary['overdue'] ? '#d63638' : '#00a32a'; ?>"><?php echo (int)$summary['overdue']; ?></div> |
| 70 |
<div class="phpinfowp-cron-stat-label"><?php _e('Overdue', 'phpinfo-wp'); ?></div> |
| 71 |
</div> |
| 72 |
<div class="phpinfowp-cron-stat" style="border-top-color:<?php echo $summary['orphan'] ? '#dba617' : '#00a32a'; ?>"> |
| 73 |
<div class="phpinfowp-cron-stat-num" style="color:<?php echo $summary['orphan'] ? '#dba617' : '#00a32a'; ?>"><?php echo (int)$summary['orphan']; ?></div> |
| 74 |
<div class="phpinfowp-cron-stat-label"><?php _e('Orphans (no callback)', 'phpinfo-wp'); ?></div> |
| 75 |
</div> |
| 76 |
<div class="phpinfowp-cron-stat"> |
| 77 |
<div class="phpinfowp-cron-stat-num"><?php echo (int)$summary['imminent']; ?></div> |
| 78 |
<div class="phpinfowp-cron-stat-label"><?php _e('Due within 60s', 'phpinfo-wp'); ?></div> |
| 79 |
</div> |
| 80 |
</div> |
| 81 |
|
| 82 |
<?php if ($summary['orphan']): ?> |
| 83 |
<div class="notice notice-info inline" style="margin:20px 0 0"> |
| 84 |
<p style="margin:8px 0"> |
| 85 |
<strong><?php _e('Why orphan events come back after you delete them:', 'phpinfo-wp'); ?></strong> |
| 86 |
"Delete" removes a single instance at one timestamp, but recurring events reschedule the next instance when WP processes cron — even with no callback. Use <strong><?php _e('Purge hook', 'phpinfo-wp'); ?></strong> on orphan rows to remove every instance of that hook in one shot (<code>wp_unschedule_hook()</code>). If it still reappears, an active plugin is re-registering it on every page load — check that plugin or fully uninstall it. |
| 87 |
</p> |
| 88 |
</div> |
| 89 |
<?php endif; ?> |
| 90 |
|
| 91 |
<?php if (!$events): ?> |
| 92 |
<p style="margin-top:24px"><?php _e('No scheduled events found.', 'phpinfo-wp'); ?></p> |
| 93 |
<?php else: ?> |
| 94 |
|
| 95 |
<table class="wp-list-table widefat fixed striped" style="margin-top:24px"> |
| 96 |
<thead> |
| 97 |
<tr> |
| 98 |
<th><?php _e('Hook', 'phpinfo-wp'); ?></th> |
| 99 |
<th style="width:140px"><?php _e('Next Run', 'phpinfo-wp'); ?></th> |
| 100 |
<th style="width:120px"><?php _e('Schedule', 'phpinfo-wp'); ?></th> |
| 101 |
<th style="width:80px"><?php _e('Status', 'phpinfo-wp'); ?></th> |
| 102 |
<th style="width:140px"><?php _e('Actions', 'phpinfo-wp'); ?></th> |
| 103 |
</tr> |
| 104 |
</thead> |
| 105 |
<tbody> |
| 106 |
<?php foreach ($events as $e): |
| 107 |
$row_class = ''; |
| 108 |
if ($e['overdue']) $row_class = 'phpinfowp-cron-overdue'; |
| 109 |
elseif ($e['imminent']) $row_class = 'phpinfowp-cron-imminent'; |
| 110 |
?> |
| 111 |
<tr class="<?php echo $row_class; ?>"> |
| 112 |
<td> |
| 113 |
<code><?php echo esc_html($e['hook']); ?></code> |
| 114 |
<?php if (!$e['has_callback']): ?> |
| 115 |
<span class="phpinfowp-cron-orphan-badge" title="No PHP callback registered for this hook — it will run with no effect"><?php _e('orphan', 'phpinfo-wp'); ?></span> |
| 116 |
<?php endif; ?> |
| 117 |
</td> |
| 118 |
<td> |
| 119 |
<?php echo esc_html(wp_date('Y-m-d H:i:s', $e['timestamp'])); ?> |
| 120 |
<div style="font-size:11px;color:#888"> |
| 121 |
<?php if ($e['diff'] < 0): ?> |
| 122 |
<span style="color:#d63638"><?php echo human_time_diff(time(), $e['timestamp']); ?> ago</span> |
| 123 |
<?php else: ?> |
| 124 |
in <?php echo human_time_diff(time(), $e['timestamp']); ?> |
| 125 |
<?php endif; ?> |
| 126 |
</div> |
| 127 |
</td> |
| 128 |
<td><?php echo esc_html($e['schedule_label']); ?></td> |
| 129 |
<td> |
| 130 |
<?php if ($e['overdue']): ?> |
| 131 |
<span style="color:#d63638;font-weight:600"><?php _e('Overdue', 'phpinfo-wp'); ?></span> |
| 132 |
<?php elseif ($e['imminent']): ?> |
| 133 |
<span style="color:#dba617;font-weight:600"><?php _e('Soon', 'phpinfo-wp'); ?></span> |
| 134 |
<?php else: ?> |
| 135 |
<span style="color:#00a32a"><?php _e('OK', 'phpinfo-wp'); ?></span> |
| 136 |
<?php endif; ?> |
| 137 |
</td> |
| 138 |
<td> |
| 139 |
<form method="post" style="display:inline"> |
| 140 |
<?php wp_nonce_field('phpinfowp_cron_nonce'); ?> |
| 141 |
<input type="hidden" name="hook" value="<?php echo esc_attr($e['hook']); ?>"> |
| 142 |
<?php if ($e['has_callback']): ?> |
| 143 |
<button type="submit" name="phpinfowp_cron_run" value="1" class="button button-small"><?php _e('Run now', 'phpinfo-wp'); ?></button> |
| 144 |
<?php endif; ?> |
| 145 |
</form> |
| 146 |
<form method="post" style="display:inline"> |
| 147 |
<?php wp_nonce_field('phpinfowp_cron_nonce'); ?> |
| 148 |
<input type="hidden" name="hook" value="<?php echo esc_attr($e['hook']); ?>"> |
| 149 |
<input type="hidden" name="ts" value="<?php echo esc_attr($e['timestamp']); ?>"> |
| 150 |
<button type="submit" name="phpinfowp_cron_delete" value="1" class="button button-small" |
| 151 |
onclick="return confirm('Remove this scheduled event?')"><?php _e('Delete', 'phpinfo-wp'); ?></button> |
| 152 |
</form> |
| 153 |
<?php if (!$e['has_callback']): ?> |
| 154 |
<form method="post" style="display:inline"> |
| 155 |
<?php wp_nonce_field('phpinfowp_cron_nonce'); ?> |
| 156 |
<input type="hidden" name="hook" value="<?php echo esc_attr($e['hook']); ?>"> |
| 157 |
<button type="submit" name="phpinfowp_cron_purge" value="1" class="button button-small" |
| 158 |
onclick="return confirm('Purge every scheduled instance of \'<?php echo esc_js($e['hook']); ?>\'? This wipes all timestamps and arg variants of this hook.')" |
| 159 |
title="Removes every scheduled instance of this hook (wp_unschedule_hook). Use this if Delete keeps coming back.">Purge hook</button> |
| 160 |
</form> |
| 161 |
<?php endif; ?> |
| 162 |
</td> |
| 163 |
</tr> |
| 164 |
<?php endforeach; ?> |
| 165 |
</tbody> |
| 166 |
</table> |
| 167 |
|
| 168 |
<?php endif; ?> |
| 169 |
</div> |
| 170 |
|