Skip to content

Commit 837ac3d

Browse files
committed
feat: add copyable cron support report
1 parent f075840 commit 837ac3d

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This plugin is read-only in the initial version. It lists scheduled WP-Cron even
1010
- Scheduled event list
1111
- Duplicate hook detection
1212
- Unusually frequent event detection
13-
- Copy/export-friendly report
13+
- Copy support report button for support tickets/debugging notes
1414
- Capability checks and escaped output
1515
- No destructive cleanup actions
1616

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Initial scope:
2020
* Duplicate hook detection.
2121
* Unusually frequent recurring event detection.
2222
* Overdue event count.
23+
* Copy support report button.
2324
* Read-only default behavior.
2425

2526
No destructive cleanup actions are included in v0.1.0.

src/AdminPage.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function render(): void
2828
$cron = function_exists('_get_cron_array') ? _get_cron_array() : [];
2929
$inspector = new CronEventInspector(is_array($cron) ? $cron : []);
3030
$report = $inspector->report();
31+
$report_text = $inspector->reportText();
3132
?>
3233
<div class="wrap">
3334
<h1><?php echo esc_html__('WP Cron Inspector Lite', 'wp-cron-inspector-lite'); ?></h1>
@@ -51,6 +52,47 @@ public function render(): void
5152
<p><?php echo esc_html(implode(', ', $report['frequent_hooks'])); ?></p>
5253
<?php endif; ?>
5354

55+
<h2><?php echo esc_html__('Copy support report', 'wp-cron-inspector-lite'); ?></h2>
56+
<p><?php echo esc_html__('Use this read-only report in support tickets or debugging notes. Review it before sharing externally.', 'wp-cron-inspector-lite'); ?></p>
57+
<p>
58+
<button type="button" class="button button-secondary" id="wp-cron-inspector-lite-copy-report">
59+
<?php echo esc_html__('Copy report', 'wp-cron-inspector-lite'); ?>
60+
</button>
61+
<span id="wp-cron-inspector-lite-copy-status" aria-live="polite" style="margin-left: 8px;"></span>
62+
</p>
63+
<textarea id="wp-cron-inspector-lite-report" class="large-text code" rows="12" readonly><?php echo esc_textarea($report_text); ?></textarea>
64+
<script>
65+
document.addEventListener('DOMContentLoaded', function () {
66+
var button = document.getElementById('wp-cron-inspector-lite-copy-report');
67+
var report = document.getElementById('wp-cron-inspector-lite-report');
68+
var status = document.getElementById('wp-cron-inspector-lite-copy-status');
69+
70+
if (!button || !report || !status) {
71+
return;
72+
}
73+
74+
button.addEventListener('click', function () {
75+
report.focus();
76+
report.select();
77+
78+
var markCopied = function () {
79+
status.textContent = <?php echo wp_json_encode(__('Copied.', 'wp-cron-inspector-lite')); ?>;
80+
};
81+
82+
if (navigator.clipboard && navigator.clipboard.writeText) {
83+
navigator.clipboard.writeText(report.value).then(markCopied).catch(function () {
84+
document.execCommand('copy');
85+
markCopied();
86+
});
87+
return;
88+
}
89+
90+
document.execCommand('copy');
91+
markCopied();
92+
});
93+
});
94+
</script>
95+
5496
<h2><?php echo esc_html__('Events', 'wp-cron-inspector-lite'); ?></h2>
5597
<table class="widefat striped">
5698
<thead>

src/CronEventInspector.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,48 @@ static function (array $a, array $b): int {
7676
return $events;
7777
}
7878

79+
public function reportText(): string
80+
{
81+
$report = $this->report();
82+
$summary = $report['summary'];
83+
$lines = [
84+
'WP Cron Inspector Lite Report',
85+
'Generated (GMT): ' . gmdate('Y-m-d H:i:s', $this->now),
86+
'',
87+
'Summary',
88+
'Total events: ' . $summary['total_events'],
89+
'Duplicate event instances: ' . $summary['duplicate_hook_count'],
90+
'Frequent hooks: ' . $summary['frequent_hook_count'],
91+
'Overdue events: ' . $summary['overdue_event_count'],
92+
'',
93+
'Duplicate hooks: ' . (empty($report['duplicates']) ? 'none' : implode(', ', $report['duplicates'])),
94+
'Frequent hooks: ' . (empty($report['frequent_hooks']) ? 'none' : implode(', ', $report['frequent_hooks'])),
95+
'',
96+
'Events',
97+
'Hook | Next run (GMT) | Schedule | Interval seconds | Flags',
98+
];
99+
100+
foreach ($report['events'] as $event) {
101+
$flags = array_filter([
102+
! empty($event['is_overdue']) ? 'overdue' : '',
103+
! empty($event['is_frequent']) ? 'frequent' : '',
104+
]);
105+
106+
$lines[] = implode(
107+
' | ',
108+
[
109+
(string) $event['hook'],
110+
(string) $event['next_run_gmt'],
111+
(string) $event['schedule'],
112+
null === $event['interval'] ? '-' : (string) $event['interval'],
113+
empty($flags) ? '-' : implode(', ', $flags),
114+
]
115+
);
116+
}
117+
118+
return implode("\n", $lines);
119+
}
120+
79121
/**
80122
* @return array{summary:array<string,int>,duplicates:array<int,string>,frequent_hooks:array<int,string>,events:array<int,array<string,mixed>>}
81123
*/

tests/run.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ function assert_same($expected, $actual, string $message): void {
7272
assert_true(in_array('backup_every_minute', $report['frequent_hooks'], true), 'it reports unusually frequent recurring hooks');
7373
assert_same(1, $report['summary']['frequent_hook_count'], 'it counts frequent hooks');
7474

75+
$report_text = $inspector->reportText();
76+
assert_true(strpos($report_text, 'WP Cron Inspector Lite Report') !== false, 'it creates a copy-friendly text report title');
77+
assert_true(strpos($report_text, 'Total events: 4') !== false, 'it includes summary totals in the text report');
78+
assert_true(strpos($report_text, 'Duplicate hooks: peepso_daily_email') !== false, 'it includes duplicate hooks in the text report');
79+
assert_true(strpos($report_text, 'Frequent hooks: backup_every_minute') !== false, 'it includes frequent hooks in the text report');
80+
assert_true(strpos($report_text, 'peepso_daily_email | 2023-11-14 22:13:20 | daily | 86400 | overdue') !== false, 'it includes event rows in the text report');
81+
7582
if ($failures > 0) {
7683
fwrite(STDERR, "{$failures} test failure(s).\n");
7784
exit(1);

0 commit comments

Comments
 (0)