root/tags/2.1/wp-includes/cron.php

Revision 4362, 4.5 kB (checked in by ryan, 2 years ago)

Change cron arg passing. Props masquerade. fixes #3169.

  • Property svn:eol-style set to native
Line 
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     $argyle = @ fsockopen( $parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01 );
88     if ( $argyle )
89         fputs( $argyle,
90               "GET {$parts['path']}?check=" . md5(DB_PASS . '187425') . " HTTP/1.0\r\n"
91             . "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n"
92         );
93 }
94
95 function wp_cron() {
96     $crons = _get_cron_array();
97     
98     if ( !is_array($crons) )
99         return;
100
101     $keys = array_keys( $crons );
102     if ( isset($keys[0]) && $keys[0] > time() )
103         return;
104
105     $schedules = wp_get_schedules();
106     foreach ( $crons as $timestamp => $cronhooks ) {
107         if ( $timestamp > time() ) break;
108         foreach ( $cronhooks as $hook => $args ) {
109             if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
110                 continue;
111             spawn_cron();
112             break 2;
113         }
114     }
115 }
116
117 function wp_get_schedules() {
118     $schedules = array(
119         'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
120         'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
121     );
122     return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
123 }
124
125 function wp_get_schedule($hook, $args = array()) {
126     $crons = _get_cron_array();
127     $key = md5(serialize($args));
128     if ( empty($crons) )
129         return false;
130     foreach ( $crons as $timestamp => $cron ) {
131         if ( isset( $cron[$hook][$key] ) )
132             return $cron[$hook][$key]['schedule'];
133     }
134     return false;
135 }
136
137 //
138 // Private functions
139 //
140
141 function _get_cron_array()  {
142     $cron = get_option('cron');
143     if ( ! is_array($cron) )
144         return false;
145
146     if ( !isset($cron['version']) )
147         $cron = _upgrade_cron_array($cron);
148
149     unset($cron['version']);
150
151     return $cron;
152 }
153
154 function _set_cron_array($cron) {
155     $cron['version'] = 2;
156     update_option( 'cron', $cron );
157 }
158
159 function _upgrade_cron_array($cron) {
160     if ( isset($cron['version']) && 2 == $cron['version'])
161         return $cron;
162
163     $new_cron = array();
164
165     foreach ($cron as $timestamp => $hooks) {
166         foreach ( $hooks as $hook => $args ) {
167             $key = md5(serialize($args['args']));
168             $new_cron[$timestamp][$hook][$key] = $args;
169         }
170     }
171
172     $new_cron['version'] = 2;
173     update_option( 'cron', $new_cron );
174     return $new_cron;
175 }
176
177 ?>
Note: See TracBrowser for help on using the browser.