]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/cron.php
Wordpress 2.3.3-scripts
[autoinstalls/wordpress.git] / wp-includes / cron.php
1 <?php
2
3 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
4         $crons = _get_cron_array();
5         $key = md5(serialize($args));
6         $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
7         uksort( $crons, "strnatcasecmp" );
8         _set_cron_array( $crons );
9 }
10
11 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
12         $crons = _get_cron_array();
13         $schedules = wp_get_schedules();
14         $key = md5(serialize($args));
15         if ( !isset( $schedules[$recurrence] ) )
16                 return false;
17         $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
18         uksort( $crons, "strnatcasecmp" );
19         _set_cron_array( $crons );
20 }
21
22 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
23         $crons = _get_cron_array();
24         $schedules = wp_get_schedules();
25         $key = md5(serialize($args));
26         $interval = 0;
27
28         // First we try to get it from the schedule
29         if ( 0 == $interval )
30                 $interval = $schedules[$recurrence]['interval'];
31         // Now we try to get it from the saved interval in case the schedule disappears
32         if ( 0 == $interval )
33                 $interval = $crons[$timestamp][$hook][$key]['interval'];
34         // Now we assume something is wrong and fail to schedule
35         if ( 0 == $interval )
36                 return false;
37
38         while ( $timestamp < time() + 1 )
39                 $timestamp += $interval;
40
41         wp_schedule_event( $timestamp, $recurrence, $hook, $args );
42 }
43
44 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
45         $crons = _get_cron_array();
46         $key = md5(serialize($args));
47         unset( $crons[$timestamp][$hook][$key] );
48         if ( empty($crons[$timestamp][$hook]) )
49                 unset( $crons[$timestamp][$hook] );
50         if ( empty($crons[$timestamp]) )
51                 unset( $crons[$timestamp] );
52         _set_cron_array( $crons );
53 }
54
55 function wp_clear_scheduled_hook( $hook ) {
56         $args = array_slice( func_get_args(), 1 );
57
58         while ( $timestamp = wp_next_scheduled( $hook, $args ) )
59                 wp_unschedule_event( $timestamp, $hook, $args );
60 }
61
62 function wp_next_scheduled( $hook, $args = array() ) {
63         $crons = _get_cron_array();
64         $key = md5(serialize($args));
65         if ( empty($crons) )
66                 return false;
67         foreach ( $crons as $timestamp => $cron ) {
68                 if ( isset( $cron[$hook][$key] ) )
69                         return $timestamp;
70         }
71         return false;
72 }
73
74 function spawn_cron() {
75         $crons = _get_cron_array();
76
77         if ( !is_array($crons) )
78                 return;
79
80         $keys = array_keys( $crons );
81         if ( array_shift( $keys ) > time() )
82                 return;
83
84         $cron_url = get_option( 'siteurl' ) . '/wp-cron.php';
85         $parts = parse_url( $cron_url );
86
87         if ($parts['scheme'] == 'https') {
88                 // support for SSL was added in 4.3.0
89                 if (version_compare(phpversion(), '4.3.0', '>=') && function_exists('openssl_open')) {
90                         $port = isset($parts['port']) ? $parts['port'] : 443;
91                         $argyle = @fsockopen('ssl://' . $parts['host'], $port, $errno, $errstr, 0.01);
92                 } else {
93                         return false;
94                 }
95         } else {
96                 $port = isset($parts['port']) ? $parts['port'] : 80;
97                 $argyle = @ fsockopen( $parts['host'], $port, $errno, $errstr, 0.01 );
98         }
99
100         if ( $argyle )
101                 fputs( $argyle,
102                           "GET {$parts['path']}?check=" . wp_hash('187425') . " HTTP/1.0\r\n"
103                         . "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n"
104                 );
105 }
106
107 function wp_cron() {
108         // Prevent infinite loops caused by lack of wp-cron.php
109         if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
110                 return;
111
112         $crons = _get_cron_array();
113
114         if ( !is_array($crons) )
115                 return;
116
117         $keys = array_keys( $crons );
118         if ( isset($keys[0]) && $keys[0] > time() )
119                 return;
120
121         $schedules = wp_get_schedules();
122         foreach ( $crons as $timestamp => $cronhooks ) {
123                 if ( $timestamp > time() ) break;
124                 foreach ( $cronhooks as $hook => $args ) {
125                         if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
126                                 continue;
127                         spawn_cron();
128                         break 2;
129                 }
130         }
131 }
132
133 function wp_get_schedules() {
134         $schedules = array(
135                 'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
136                 'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
137         );
138         return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
139 }
140
141 function wp_get_schedule($hook, $args = array()) {
142         $crons = _get_cron_array();
143         $key = md5(serialize($args));
144         if ( empty($crons) )
145                 return false;
146         foreach ( $crons as $timestamp => $cron ) {
147                 if ( isset( $cron[$hook][$key] ) )
148                         return $cron[$hook][$key]['schedule'];
149         }
150         return false;
151 }
152
153 //
154 // Private functions
155 //
156
157 function _get_cron_array()  {
158         $cron = get_option('cron');
159         if ( ! is_array($cron) )
160                 return false;
161
162         if ( !isset($cron['version']) )
163                 $cron = _upgrade_cron_array($cron);
164
165         unset($cron['version']);
166
167         return $cron;
168 }
169
170 function _set_cron_array($cron) {
171         $cron['version'] = 2;
172         update_option( 'cron', $cron );
173 }
174
175 function _upgrade_cron_array($cron) {
176         if ( isset($cron['version']) && 2 == $cron['version'])
177                 return $cron;
178
179         $new_cron = array();
180
181         foreach ($cron as $timestamp => $hooks) {
182                 foreach ( $hooks as $hook => $args ) {
183                         $key = md5(serialize($args['args']));
184                         $new_cron[$timestamp][$hook][$key] = $args;
185                 }
186         }
187
188         $new_cron['version'] = 2;
189         update_option( 'cron', $new_cron );
190         return $new_cron;
191 }
192
193 ?>