1+ <?php
2+
3+ if ( ! class_exists ( 'WP_CLI ' ) ) {
4+ return ;
5+ }
6+
7+ /**
8+ * Logs cron events before they run via WP-CLI.
9+ */
10+ class Cron_Logger_Command {
11+
12+ /**
13+ * Manually list upcoming cron jobs.
14+ *
15+ * ## EXAMPLES
16+ *
17+ * wp cron-logger run
18+ *
19+ * @when after_wp_load
20+ */
21+ public function print ( $ args , $ assoc_args ) {
22+ $ cron_jobs = _get_cron_array ();
23+ if ( empty ( $ cron_jobs ) ) {
24+ WP_CLI ::log ( "No cron jobs scheduled. " );
25+ return ;
26+ }
27+
28+ $ current_time = time ();
29+ $ due_events = [];
30+
31+ foreach ( $ cron_jobs as $ timestamp => $ events ) {
32+ if ( $ timestamp <= $ current_time ) {
33+ foreach ( $ events as $ hook => $ details ) {
34+ foreach ( $ details as $ event ) {
35+ $ due_events [] = sprintf (
36+ 'Scheduled event: "%s" at %s with args: %s ' ,
37+ $ hook ,
38+ date ( 'Y-m-d H:i:s ' , $ timestamp ),
39+ json_encode ( $ event ['args ' ] )
40+ );
41+ }
42+ }
43+ }
44+ }
45+
46+ if ( ! empty ( $ due_events ) ) {
47+ WP_CLI ::log ( "Upcoming cron events: " );
48+ foreach ( $ due_events as $ event_log ) {
49+ WP_CLI ::log ( $ event_log );
50+ }
51+ } else {
52+ WP_CLI ::log ( "No due cron events found. " );
53+ }
54+ }
55+
56+ /**
57+ * Run all events due now.
58+ *
59+ * ## EXAMPLES
60+ *
61+ * wp cron-logger run
62+ *
63+ * @when after_wp_load
64+ */
65+
66+ public function run ( $ args , $ assoc_args ) {
67+ $ cron_jobs = _get_cron_array ();
68+ if ( empty ( $ cron_jobs ) ) {
69+ WP_CLI ::log ( "No cron jobs scheduled. " );
70+ return ;
71+ }
72+
73+ $ current_time = time ();
74+ $ due_events = [];
75+
76+ foreach ( $ cron_jobs as $ timestamp => $ events ) {
77+ if ( $ timestamp <= $ current_time ) {
78+ foreach ( $ events as $ hook => $ details ) {
79+ foreach ( $ details as $ event ) {
80+ $ due_events [] = $ hook ;
81+ }
82+ }
83+ }
84+ }
85+
86+ if ( ! empty ( $ due_events ) ) {
87+ foreach ( $ due_events as $ event_name ) {
88+ WP_CLI ::log ( sprintf ( '** Running cron event: "%s" ' , $ event_name ) );
89+ WP_CLI ::runcommand ( "cron event run $ event_name " );
90+ }
91+ } else {
92+ WP_CLI ::log ( "No due cron events found. " );
93+ }
94+ }
95+ }
96+
97+ /**
98+ * Hook into WP-CLI cron event execution to log each event before it runs.
99+ */
100+ WP_CLI ::add_hook ( 'before_invoke:cron_event_run ' , function ( $ args ) {
101+ if ( !empty ( $ args ) && is_array ( $ args ) ) {
102+ $ event_name = trim ( $ args [0 ] );
103+ $ start_time = microtime (true );
104+ // Store start time in a global variable for later use
105+ $ GLOBALS ['cron_event_start_time ' ] = $ start_time ;
106+ WP_CLI ::log ( sprintf ( '=== [%s] Starting cron event: "%s" === ' ,
107+ date ('Y-m-d H:i:s ' ),
108+ $ event_name
109+ ));
110+ } else {
111+ WP_CLI ::log ( sprintf ( '=== [%s] Running all due cron events... === ' ,
112+ date ('Y-m-d H:i:s ' )
113+ ));
114+ }
115+ });
116+
117+ /**
118+ * Hook to log when a cron event finishes running
119+ */
120+ WP_CLI ::add_hook ( 'after_invoke:cron_event_run ' , function ( $ args ) {
121+ $ end_time = microtime (true );
122+ $ start_time = isset ($ GLOBALS ['cron_event_start_time ' ]) ? $ GLOBALS ['cron_event_start_time ' ] : $ end_time ;
123+ $ duration = round ($ end_time - $ start_time , 2 );
124+
125+ if ( !empty ( $ args ) && is_array ( $ args ) ) {
126+ $ event_name = trim ( $ args [0 ] );
127+ WP_CLI ::log ( sprintf ( '=== [%s] Completed cron event: "%s" (took %s seconds) === ' ,
128+ date ('Y-m-d H:i:s ' ),
129+ $ event_name ,
130+ $ duration
131+ ));
132+ } else {
133+ WP_CLI ::log ( sprintf ( '=== [%s] Completed all due cron events (took %s seconds) === ' ,
134+ date ('Y-m-d H:i:s ' ),
135+ $ duration
136+ ));
137+ }
138+ });
139+
140+ WP_CLI ::add_command ( 'cron-logger ' , 'Cron_Logger_Command ' );
0 commit comments