author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
18 | 3 |
* A pseudo-cron daemon for scheduling WordPress tasks. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4 |
* |
18 | 5 |
* WP-Cron is triggered when the site receives a visit. In the scenario |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* where a site may not receive enough visits to execute scheduled tasks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* in a timely manner, this file can be called directly or via a server |
18 | 8 |
* cron daemon for X number of times. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
* Defining DISABLE_WP_CRON as true and calling this file directly are |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* mutually exclusive and the latter does not rely on the former to work. |
0 | 12 |
* |
13 |
* The HTTP request to this file will not slow down the visitor who happens to |
|
18 | 14 |
* visit when a scheduled cron event runs. |
0 | 15 |
* |
16 |
* @package WordPress |
|
17 |
*/ |
|
18 |
||
9 | 19 |
ignore_user_abort( true ); |
0 | 20 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
if ( ! headers_sent() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
23 |
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
24 |
} |
16 | 25 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
26 |
// Don't run cron until the request finishes, if possible. |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
27 |
if ( function_exists( 'fastcgi_finish_request' ) ) { |
9 | 28 |
fastcgi_finish_request(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
} elseif ( function_exists( 'litespeed_finish_request' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
litespeed_finish_request(); |
9 | 31 |
} |
32 |
||
33 |
if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) { |
|
0 | 34 |
die(); |
9 | 35 |
} |
0 | 36 |
|
37 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
38 |
* Tell WordPress the cron task is running. |
0 | 39 |
* |
40 |
* @var bool |
|
41 |
*/ |
|
9 | 42 |
define( 'DOING_CRON', true ); |
0 | 43 |
|
9 | 44 |
if ( ! defined( 'ABSPATH' ) ) { |
0 | 45 |
/** Set up WordPress environment */ |
16 | 46 |
require_once __DIR__ . '/wp-load.php'; |
0 | 47 |
} |
48 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
// Attempt to raise the PHP memory limit for cron event processing. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
wp_raise_memory_limit( 'cron' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
|
5 | 52 |
/** |
53 |
* Retrieves the cron lock. |
|
54 |
* |
|
55 |
* Returns the uncached `doing_cron` transient. |
|
56 |
* |
|
57 |
* @ignore |
|
58 |
* @since 3.3.0 |
|
59 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* |
19 | 62 |
* @return string|int|false Value of the `doing_cron` transient, 0|false otherwise. |
5 | 63 |
*/ |
0 | 64 |
function _get_cron_lock() { |
65 |
global $wpdb; |
|
66 |
||
67 |
$value = 0; |
|
68 |
if ( wp_using_ext_object_cache() ) { |
|
5 | 69 |
/* |
70 |
* Skip local cache and force re-fetch of doing_cron transient |
|
71 |
* in case another process updated the cache. |
|
72 |
*/ |
|
0 | 73 |
$value = wp_cache_get( 'doing_cron', 'transient', true ); |
74 |
} else { |
|
75 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) ); |
|
9 | 76 |
if ( is_object( $row ) ) { |
0 | 77 |
$value = $row->option_value; |
9 | 78 |
} |
0 | 79 |
} |
80 |
||
81 |
return $value; |
|
82 |
} |
|
83 |
||
9 | 84 |
$crons = wp_get_ready_cron_jobs(); |
85 |
if ( empty( $crons ) ) { |
|
0 | 86 |
die(); |
9 | 87 |
} |
0 | 88 |
|
89 |
$gmt_time = microtime( true ); |
|
90 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
// The cron lock: a unix timestamp from when the cron was spawned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
$doing_cron_transient = get_transient( 'doing_cron' ); |
0 | 93 |
|
16 | 94 |
// Use global $doing_wp_cron lock, otherwise use the GET lock. If no lock, try to grab a new lock. |
0 | 95 |
if ( empty( $doing_wp_cron ) ) { |
9 | 96 |
if ( empty( $_GET['doing_wp_cron'] ) ) { |
0 | 97 |
// Called from external script/job. Try setting a lock. |
9 | 98 |
if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) { |
0 | 99 |
return; |
9 | 100 |
} |
16 | 101 |
$doing_wp_cron = sprintf( '%.22F', microtime( true ) ); |
102 |
$doing_cron_transient = $doing_wp_cron; |
|
0 | 103 |
set_transient( 'doing_cron', $doing_wp_cron ); |
104 |
} else { |
|
9 | 105 |
$doing_wp_cron = $_GET['doing_wp_cron']; |
0 | 106 |
} |
107 |
} |
|
108 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* The cron lock (a unix timestamp set when the cron was spawned), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
* must match $doing_wp_cron (the "key"). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
*/ |
16 | 113 |
if ( $doing_cron_transient !== $doing_wp_cron ) { |
0 | 114 |
return; |
9 | 115 |
} |
0 | 116 |
|
117 |
foreach ( $crons as $timestamp => $cronhooks ) { |
|
9 | 118 |
if ( $timestamp > $gmt_time ) { |
0 | 119 |
break; |
9 | 120 |
} |
0 | 121 |
|
122 |
foreach ( $cronhooks as $hook => $keys ) { |
|
123 |
||
124 |
foreach ( $keys as $k => $v ) { |
|
125 |
||
126 |
$schedule = $v['schedule']; |
|
127 |
||
16 | 128 |
if ( $schedule ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
$result = wp_reschedule_event( $timestamp, $schedule, $hook, $v['args'], true ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
if ( is_wp_error( $result ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
error_log( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
/* translators: 1: Hook name, 2: Error code, 3: Error message, 4: Event data. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
__( 'Cron reschedule event error for hook: %1$s, Error code: %2$s, Error message: %3$s, Data: %4$s' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
$hook, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
$result->get_error_code(), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
$result->get_error_message(), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
wp_json_encode( $v ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
/** |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
144 |
* Fires if an error happens when rescheduling a cron event. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
145 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
146 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
147 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
* @param WP_Error $result The WP_Error object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
* @param string $hook Action hook to execute when the event is run. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
* @param array $v Event data. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
do_action( 'cron_reschedule_event_error', $result, $hook, $v ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
} |
0 | 154 |
} |
155 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
$result = wp_unschedule_event( $timestamp, $hook, $v['args'], true ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
if ( is_wp_error( $result ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
error_log( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
/* translators: 1: Hook name, 2: Error code, 3: Error message, 4: Event data. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
__( 'Cron unschedule event error for hook: %1$s, Error code: %2$s, Error message: %3$s, Data: %4$s' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
$hook, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
$result->get_error_code(), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
$result->get_error_message(), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
wp_json_encode( $v ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
170 |
/** |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
171 |
* Fires if an error happens when unscheduling a cron event. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
172 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
173 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
174 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
175 |
* @param WP_Error $result The WP_Error object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
176 |
* @param string $hook Action hook to execute when the event is run. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
177 |
* @param array $v Event data. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
178 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
179 |
do_action( 'cron_unschedule_event_error', $result, $hook, $v ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
180 |
} |
0 | 181 |
|
182 |
/** |
|
183 |
* Fires scheduled events. |
|
184 |
* |
|
5 | 185 |
* @ignore |
0 | 186 |
* @since 2.1.0 |
187 |
* |
|
188 |
* @param string $hook Name of the hook that was scheduled to be fired. |
|
5 | 189 |
* @param array $args The arguments to be passed to the hook. |
0 | 190 |
*/ |
9 | 191 |
do_action_ref_array( $hook, $v['args'] ); |
0 | 192 |
|
193 |
// If the hook ran too long and another cron process stole the lock, quit. |
|
16 | 194 |
if ( _get_cron_lock() !== $doing_wp_cron ) { |
0 | 195 |
return; |
9 | 196 |
} |
0 | 197 |
} |
198 |
} |
|
199 |
} |
|
200 |
||
16 | 201 |
if ( _get_cron_lock() === $doing_wp_cron ) { |
0 | 202 |
delete_transient( 'doing_cron' ); |
9 | 203 |
} |
0 | 204 |
|
205 |
die(); |