author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* A pseudo-CRON daemon for scheduling WordPress tasks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* WP Cron is triggered when the site receives a visit. In the scenario |
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 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* CRON daemon for X number of times. |
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 |
|
14 |
* visit when the cron job is needed to run. |
|
15 |
* |
|
16 |
* @package WordPress |
|
17 |
*/ |
|
18 |
||
19 |
ignore_user_abort(true); |
|
20 |
||
21 |
if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') ) |
|
22 |
die(); |
|
23 |
||
24 |
/** |
|
25 |
* Tell WordPress we are doing the CRON task. |
|
26 |
* |
|
27 |
* @var bool |
|
28 |
*/ |
|
29 |
define('DOING_CRON', true); |
|
30 |
||
31 |
if ( !defined('ABSPATH') ) { |
|
32 |
/** Set up WordPress environment */ |
|
33 |
require_once( dirname( __FILE__ ) . '/wp-load.php' ); |
|
34 |
} |
|
35 |
||
5 | 36 |
/** |
37 |
* Retrieves the cron lock. |
|
38 |
* |
|
39 |
* Returns the uncached `doing_cron` transient. |
|
40 |
* |
|
41 |
* @ignore |
|
42 |
* @since 3.3.0 |
|
43 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* |
5 | 46 |
* @return string|false Value of the `doing_cron` transient, 0|false otherwise. |
47 |
*/ |
|
0 | 48 |
function _get_cron_lock() { |
49 |
global $wpdb; |
|
50 |
||
51 |
$value = 0; |
|
52 |
if ( wp_using_ext_object_cache() ) { |
|
5 | 53 |
/* |
54 |
* Skip local cache and force re-fetch of doing_cron transient |
|
55 |
* in case another process updated the cache. |
|
56 |
*/ |
|
0 | 57 |
$value = wp_cache_get( 'doing_cron', 'transient', true ); |
58 |
} else { |
|
59 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) ); |
|
60 |
if ( is_object( $row ) ) |
|
61 |
$value = $row->option_value; |
|
62 |
} |
|
63 |
||
64 |
return $value; |
|
65 |
} |
|
66 |
||
67 |
if ( false === $crons = _get_cron_array() ) |
|
68 |
die(); |
|
69 |
||
70 |
$keys = array_keys( $crons ); |
|
71 |
$gmt_time = microtime( true ); |
|
72 |
||
73 |
if ( isset($keys[0]) && $keys[0] > $gmt_time ) |
|
74 |
die(); |
|
75 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
// 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
|
78 |
$doing_cron_transient = get_transient( 'doing_cron' ); |
0 | 79 |
|
80 |
// Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock. |
|
81 |
if ( empty( $doing_wp_cron ) ) { |
|
82 |
if ( empty( $_GET[ 'doing_wp_cron' ] ) ) { |
|
83 |
// Called from external script/job. Try setting a lock. |
|
84 |
if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) |
|
85 |
return; |
|
86 |
$doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) ); |
|
87 |
set_transient( 'doing_cron', $doing_wp_cron ); |
|
88 |
} else { |
|
89 |
$doing_wp_cron = $_GET[ 'doing_wp_cron' ]; |
|
90 |
} |
|
91 |
} |
|
92 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* 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
|
95 |
* must match $doing_wp_cron (the "key"). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
*/ |
0 | 97 |
if ( $doing_cron_transient != $doing_wp_cron ) |
98 |
return; |
|
99 |
||
100 |
foreach ( $crons as $timestamp => $cronhooks ) { |
|
101 |
if ( $timestamp > $gmt_time ) |
|
102 |
break; |
|
103 |
||
104 |
foreach ( $cronhooks as $hook => $keys ) { |
|
105 |
||
106 |
foreach ( $keys as $k => $v ) { |
|
107 |
||
108 |
$schedule = $v['schedule']; |
|
109 |
||
110 |
if ( $schedule != false ) { |
|
111 |
$new_args = array($timestamp, $schedule, $hook, $v['args']); |
|
112 |
call_user_func_array('wp_reschedule_event', $new_args); |
|
113 |
} |
|
114 |
||
115 |
wp_unschedule_event( $timestamp, $hook, $v['args'] ); |
|
116 |
||
117 |
/** |
|
118 |
* Fires scheduled events. |
|
119 |
* |
|
5 | 120 |
* @ignore |
0 | 121 |
* @since 2.1.0 |
122 |
* |
|
123 |
* @param string $hook Name of the hook that was scheduled to be fired. |
|
5 | 124 |
* @param array $args The arguments to be passed to the hook. |
0 | 125 |
*/ |
126 |
do_action_ref_array( $hook, $v['args'] ); |
|
127 |
||
128 |
// If the hook ran too long and another cron process stole the lock, quit. |
|
129 |
if ( _get_cron_lock() != $doing_wp_cron ) |
|
130 |
return; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
if ( _get_cron_lock() == $doing_wp_cron ) |
|
136 |
delete_transient( 'doing_cron' ); |
|
137 |
||
138 |
die(); |