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