]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/update.php
e071921458467d035028c947a7f8de6a010e581d
[autoinstalls/wordpress.git] / wp-includes / update.php
1 <?php
2 /**
3  * A simple set of functions to check our version 1.0 update service.
4  *
5  * @package WordPress
6  * @since 2.3.0
7  */
8
9 /**
10  * Check WordPress version against the newest version.
11  *
12  * The WordPress version, PHP version, and Locale is sent. Checks against the
13  * WordPress server at api.wordpress.org server. Will only check if WordPress
14  * isn't installing.
15  *
16  * @package WordPress
17  * @since 2.3.0
18  * @uses $wp_version Used to check against the newest WordPress version.
19  *
20  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
21  */
22 function wp_version_check() {
23         if ( defined('WP_INSTALLING') )
24                 return;
25
26         global $wp_version, $wpdb, $wp_local_package;
27         $php_version = phpversion();
28
29         $current = get_site_transient( 'update_core' );
30         if ( ! is_object($current) ) {
31                 $current = new stdClass;
32                 $current->updates = array();
33                 $current->version_checked = $wp_version;
34         }
35
36         $locale = apply_filters( 'core_version_check_locale', get_locale() );
37
38         // Update last_checked for current to prevent multiple blocking requests if request hangs
39         $current->last_checked = time();
40         set_site_transient( 'update_core', $current );
41
42         if ( method_exists( $wpdb, 'db_version' ) )
43                 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
44         else
45                 $mysql_version = 'N/A';
46
47         $num_blogs = 1;
48         $wp_install = home_url( '/' );
49         $multisite_enabled = 0;
50         $user_count = count_users( );
51         if ( is_multisite( ) ) {
52                 $num_blogs = get_blog_count( );
53                 $wp_install = network_site_url( );
54                 $multisite_enabled = 1;
55         }
56
57         $local_package = isset( $wp_local_package )? $wp_local_package : '';
58         $url = "http://api.wordpress.org/core/version-check/1.5/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package&blogs=$num_blogs&users={$user_count['total_users']}&multisite_enabled=$multisite_enabled";
59
60         $options = array(
61                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
62                 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
63                 'headers' => array(
64                         'wp_install' => $wp_install,
65                         'wp_blog' => home_url( '/' )
66                 )
67         );
68
69         $response = wp_remote_get($url, $options);
70
71         if ( is_wp_error( $response ) )
72                 return false;
73
74         if ( 200 != $response['response']['code'] )
75                 return false;
76
77         $body = trim( $response['body'] );
78         $body = str_replace(array("\r\n", "\r"), "\n", $body);
79         $new_options = array();
80         foreach ( explode( "\n\n", $body ) as $entry ) {
81                 $returns = explode("\n", $entry);
82                 $new_option = new stdClass();
83                 $new_option->response = esc_attr( $returns[0] );
84                 if ( isset( $returns[1] ) )
85                         $new_option->url = esc_url( $returns[1] );
86                 if ( isset( $returns[2] ) )
87                         $new_option->package = esc_url( $returns[2] );
88                 if ( isset( $returns[3] ) )
89                         $new_option->current = esc_attr( $returns[3] );
90                 if ( isset( $returns[4] ) )
91                         $new_option->locale = esc_attr( $returns[4] );
92                 if ( isset( $returns[5] ) )
93                         $new_option->php_version = esc_attr( $returns[5] );
94                 if ( isset( $returns[6] ) )
95                         $new_option->mysql_version = esc_attr( $returns[6] );
96                 $new_options[] = $new_option;
97         }
98
99         $updates = new stdClass();
100         $updates->updates = $new_options;
101         $updates->last_checked = time();
102         $updates->version_checked = $wp_version;
103         set_site_transient( 'update_core',  $updates);
104 }
105
106 /**
107  * Check plugin versions against the latest versions hosted on WordPress.org.
108  *
109  * The WordPress version, PHP version, and Locale is sent along with a list of
110  * all plugins installed. Checks against the WordPress server at
111  * api.wordpress.org. Will only check if WordPress isn't installing.
112  *
113  * @package WordPress
114  * @since 2.3.0
115  * @uses $wp_version Used to notidy the WordPress version.
116  *
117  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
118  */
119 function wp_update_plugins() {
120         global $wp_version;
121
122         if ( defined('WP_INSTALLING') )
123                 return false;
124
125         // If running blog-side, bail unless we've not checked in the last 12 hours
126         if ( !function_exists( 'get_plugins' ) )
127                 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
128
129         $plugins = get_plugins();
130         $active  = get_option( 'active_plugins', array() );
131         $current = get_site_transient( 'update_plugins' );
132         if ( ! is_object($current) )
133                 $current = new stdClass;
134
135         $new_option = new stdClass;
136         $new_option->last_checked = time();
137         $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
138         $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
139
140         $plugin_changed = false;
141         foreach ( $plugins as $file => $p ) {
142                 $new_option->checked[ $file ] = $p['Version'];
143
144                 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
145                         $plugin_changed = true;
146         }
147
148         if ( isset ( $current->response ) && is_array( $current->response ) ) {
149                 foreach ( $current->response as $plugin_file => $update_details ) {
150                         if ( ! isset($plugins[ $plugin_file ]) ) {
151                                 $plugin_changed = true;
152                                 break;
153                         }
154                 }
155         }
156
157         // Bail if we've checked in the last 12 hours and if nothing has changed
158         if ( $time_not_changed && !$plugin_changed )
159                 return false;
160
161         // Update last_checked for current to prevent multiple blocking requests if request hangs
162         $current->last_checked = time();
163         set_site_transient( 'update_plugins', $current );
164
165         $to_send = (object) compact('plugins', 'active');
166
167         $options = array(
168                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
169                 'body' => array( 'plugins' => serialize( $to_send ) ),
170                 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
171         );
172
173         $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
174
175         if ( is_wp_error( $raw_response ) )
176                 return false;
177
178         if ( 200 != $raw_response['response']['code'] )
179                 return false;
180
181         $response = unserialize( $raw_response['body'] );
182
183         if ( false !== $response )
184                 $new_option->response = $response;
185         else
186                 $new_option->response = array();
187
188         set_site_transient( 'update_plugins', $new_option );
189 }
190
191 /**
192  * Check theme versions against the latest versions hosted on WordPress.org.
193  *
194  * A list of all themes installed in sent to WP. Checks against the
195  * WordPress server at api.wordpress.org. Will only check if WordPress isn't
196  * installing.
197  *
198  * @package WordPress
199  * @since 2.7.0
200  * @uses $wp_version Used to notidy the WordPress version.
201  *
202  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
203  */
204 function wp_update_themes( ) {
205         global $wp_version;
206
207         if ( defined( 'WP_INSTALLING' ) )
208                 return false;
209
210         if ( !function_exists( 'get_themes' ) )
211                 require_once( ABSPATH . 'wp-includes/theme.php' );
212
213         $installed_themes = get_themes( );
214         $current_theme = get_site_transient( 'update_themes' );
215         if ( ! is_object($current_theme) )
216                 $current_theme = new stdClass;
217
218         $new_option = new stdClass;
219         $new_option->last_checked = time( );
220         $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
221         $time_not_changed = isset( $current_theme->last_checked ) && $timeout > ( time( ) - $current_theme->last_checked );
222
223         $themes = array();
224         $checked = array();
225         $themes['current_theme'] = (array) $current_theme;
226         foreach ( (array) $installed_themes as $theme_title => $theme ) {
227                 $themes[$theme['Stylesheet']] = array();
228                 $checked[$theme['Stylesheet']] = $theme['Version'];
229
230                 foreach ( (array) $theme as $key => $value )
231                         $themes[$theme['Stylesheet']][$key] = $value;
232         }
233
234         $theme_changed = false;
235         foreach ( $checked as $slug => $v ) {
236                 $new_option->checked[ $slug ] = $v;
237
238                 if ( !isset( $current_theme->checked[ $slug ] ) || strval($current_theme->checked[ $slug ]) !== strval($v) )
239                         $theme_changed = true;
240         }
241
242         if ( isset ( $current_theme->response ) && is_array( $current_theme->response ) ) {
243                 foreach ( $current_theme->response as $slug => $update_details ) {
244                         if ( ! isset($checked[ $slug ]) ) {
245                                 $theme_changed = true;
246                                 break;
247                         }
248                 }
249         }
250
251         if ( $time_not_changed && !$theme_changed )
252                 return false;
253
254         // Update last_checked for current to prevent multiple blocking requests if request hangs
255         $current_theme->last_checked = time();
256         set_site_transient( 'update_themes', $current_theme );
257
258         $current_theme->template = get_option( 'template' );
259
260         $options = array(
261                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
262                 'body'                  => array( 'themes' => serialize( $themes ) ),
263                 'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
264         );
265
266         $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
267
268         if ( is_wp_error( $raw_response ) )
269                 return false;
270
271         if ( 200 != $raw_response['response']['code'] )
272                 return false;
273
274         $response = unserialize( $raw_response['body'] );
275         if ( $response ) {
276                 $new_option->checked = $checked;
277                 $new_option->response = $response;
278         }
279
280         set_site_transient( 'update_themes', $new_option );
281 }
282
283 function _maybe_update_core() {
284         global $wp_version;
285
286         $current = get_site_transient( 'update_core' );
287
288         if ( isset( $current->last_checked ) &&
289                 43200 > ( time() - $current->last_checked ) &&
290                 isset( $current->version_checked ) &&
291                 $current->version_checked == $wp_version )
292                 return;
293
294         wp_version_check();
295 }
296 /**
297  * Check the last time plugins were run before checking plugin versions.
298  *
299  * This might have been backported to WordPress 2.6.1 for performance reasons.
300  * This is used for the wp-admin to check only so often instead of every page
301  * load.
302  *
303  * @since 2.7.0
304  * @access private
305  */
306 function _maybe_update_plugins() {
307         $current = get_site_transient( 'update_plugins' );
308         if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
309                 return;
310         wp_update_plugins();
311 }
312
313 /**
314  * Check themes versions only after a duration of time.
315  *
316  * This is for performance reasons to make sure that on the theme version
317  * checker is not run on every page load.
318  *
319  * @since 2.7.0
320  * @access private
321  */
322 function _maybe_update_themes( ) {
323         $current = get_site_transient( 'update_themes' );
324         if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
325                 return;
326
327         wp_update_themes();
328 }
329
330 add_action( 'admin_init', '_maybe_update_core' );
331 add_action( 'wp_version_check', 'wp_version_check' );
332
333 add_action( 'load-plugins.php', 'wp_update_plugins' );
334 add_action( 'load-update.php', 'wp_update_plugins' );
335 add_action( 'load-update-core.php', 'wp_update_plugins' );
336 add_action( 'admin_init', '_maybe_update_plugins' );
337 add_action( 'wp_update_plugins', 'wp_update_plugins' );
338
339 add_action( 'load-themes.php', 'wp_update_themes' );
340 add_action( 'load-update.php', 'wp_update_themes' );
341 add_action( 'load-update-core.php', 'wp_update_themes' );
342 add_action( 'admin_init', '_maybe_update_themes' );
343 add_action( 'wp_update_themes', 'wp_update_themes' );
344
345 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
346         wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
347
348 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
349         wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
350
351 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
352         wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
353
354 ?>