]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/update.php
Wordpress 2.7.1
[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_option( 'update_core' );
30         if ( ! is_object($current) )
31                 $current = new stdClass;
32
33         $locale = get_locale();
34         if (
35                 isset( $current->last_checked ) &&
36                 43200 > ( time() - $current->last_checked ) &&
37                 $current->version_checked == $wp_version
38         )
39                 return false;
40
41         // Update last_checked for current to prevent multiple blocking requests if request hangs
42         $current->last_checked = time();
43         update_option( 'update_core', $current );
44
45         if ( method_exists( $wpdb, 'db_version' ) )
46                 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version($wpdb->users));
47         else
48                 $mysql_version = 'N/A';
49         $local_package = isset( $wp_local_package )? $wp_local_package : '';
50         $url = "http://api.wordpress.org/core/version-check/1.3/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package";
51
52         $options = array('timeout' => 3);
53         $options['headers'] = array(
54                 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
55                 'User-Agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
56         );
57
58         $response = wp_remote_request($url, $options);
59
60         if ( is_wp_error( $response ) )
61                 return false;
62
63         if ( 200 != $response['response']['code'] )
64                 return false;
65
66         $body = trim( $response['body'] );
67         $body = str_replace(array("\r\n", "\r"), "\n", $body);
68         $new_options = array();
69         foreach( explode( "\n\n", $body ) as $entry) {
70                 $returns = explode("\n", $entry);
71                 $new_option = new stdClass();
72                 $new_option->response = attribute_escape( $returns[0] );
73                 if ( isset( $returns[1] ) )
74                         $new_option->url = clean_url( $returns[1] );
75                 if ( isset( $returns[2] ) )
76                         $new_option->package = clean_url( $returns[2] );
77                 if ( isset( $returns[3] ) )
78                         $new_option->current = attribute_escape( $returns[3] );
79                 if ( isset( $returns[4] ) )
80                         $new_option->locale = attribute_escape( $returns[4] );
81                 $new_options[] = $new_option;
82         }
83
84         $updates = new stdClass();
85         $updates->updates = $new_options;
86         $updates->last_checked = time();
87         $updates->version_checked = $wp_version;
88         update_option( 'update_core',  $updates);
89 }
90 add_action( 'init', 'wp_version_check' );
91
92 /**
93  * Check plugin versions against the latest versions hosted on WordPress.org.
94  *
95  * The WordPress version, PHP version, and Locale is sent along with a list of
96  * all plugins installed. Checks against the WordPress server at
97  * api.wordpress.org. Will only check if WordPress isn't installing.
98  *
99  * @package WordPress
100  * @since 2.3.0
101  * @uses $wp_version Used to notidy the WordPress version.
102  *
103  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
104  */
105 function wp_update_plugins() {
106         global $wp_version;
107
108         if ( defined('WP_INSTALLING') )
109                 return false;
110
111         // If running blog-side, bail unless we've not checked in the last 12 hours
112         if ( !function_exists( 'get_plugins' ) )
113                 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
114
115         $plugins = get_plugins();
116         $active  = get_option( 'active_plugins' );
117         $current = get_option( 'update_plugins' );
118         if ( ! is_object($current) )
119                 $current = new stdClass;
120
121         $new_option = '';
122         $new_option->last_checked = time();
123         $time_not_changed = isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked );
124
125         $plugin_changed = false;
126         foreach ( $plugins as $file => $p ) {
127                 $new_option->checked[ $file ] = $p['Version'];
128
129                 if ( !isset( $current->checked[ $file ] ) ) {
130                         $plugin_changed = true;
131                         continue;
132                 }
133
134                 if ( strval($current->checked[ $file ]) !== strval($p['Version']) )
135                         $plugin_changed = true;
136         }
137
138         if ( isset ( $current->response ) && is_array( $current->response ) ) {
139                 foreach ( $current->response as $plugin_file => $update_details ) {
140                         if ( ! isset($plugins[ $plugin_file ]) ) {
141                                 $plugin_changed = true;
142                         }
143                 }
144         }
145
146         // Bail if we've checked in the last 12 hours and if nothing has changed
147         if ( $time_not_changed && !$plugin_changed )
148                 return false;
149
150         // Update last_checked for current to prevent multiple blocking requests if request hangs
151         $current->last_checked = time();
152         update_option( 'update_plugins', $current );
153
154         $to_send->plugins = $plugins;
155         $to_send->active = $active;
156         $send = serialize( $to_send );
157         $body = 'plugins=' . urlencode( $send );
158
159         $options = array('method' => 'POST', 'timeout' => 3, 'body' => $body);
160         $options['headers'] = array(
161                 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
162                 'Content-Length' => strlen($body),
163                 'User-Agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
164         );
165
166         $raw_response = wp_remote_request('http://api.wordpress.org/plugins/update-check/1.0/', $options);
167
168         if ( is_wp_error( $raw_response ) )
169                 return false;
170
171         if( 200 != $raw_response['response']['code'] ) {
172                 return false;
173         }
174
175         $response = unserialize( $raw_response['body'] );
176
177         if ( false !== $response )
178                 $new_option->response = $response;
179         else
180                 $new_option->response = array();
181
182         update_option( 'update_plugins', $new_option );
183 }
184
185 /**
186  * Check theme versions against the latest versions hosted on WordPress.org.
187  *
188  * A list of all themes installed in sent to WP. Checks against the
189  * WordPress server at api.wordpress.org. Will only check if WordPress isn't
190  * installing.
191  *
192  * @package WordPress
193  * @since 2.7.0
194  * @uses $wp_version Used to notidy the WordPress version.
195  *
196  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
197  */
198 function wp_update_themes( ) {
199         global $wp_version;
200
201         if( defined( 'WP_INSTALLING' ) )
202                 return false;
203
204         if( !function_exists( 'get_themes' ) )
205                 require_once( ABSPATH . 'wp-includes/theme.php' );
206
207         $installed_themes = get_themes( );
208         $current_theme = get_option( 'update_themes' );
209         if ( ! is_object($current_theme) )
210                 $current_theme = new stdClass;
211
212         $new_option = '';
213         $new_option->last_checked = time( );
214         $time_not_changed = isset( $current_theme->last_checked ) && 43200 > ( time( ) - $current_theme->last_checked );
215
216         if( $time_not_changed )
217                 return false;
218
219         // Update last_checked for current to prevent multiple blocking requests if request hangs
220         $current_theme->last_checked = time();
221         update_option( 'update_themes', $current_theme );
222
223         $themes = array( );
224         $themes['current_theme'] = $current_theme;
225         foreach( (array) $installed_themes as $theme_title => $theme ) {
226                 $themes[$theme['Template']] = array( );
227
228                 foreach( (array) $theme as $key => $value ) {
229                         $themes[$theme['Template']][$key] = $value;
230                 }
231         }
232
233         $options = array(
234                 'method'                => 'POST',
235                 'timeout'               => 3,
236                 'body'                  => 'themes=' . urlencode( serialize( $themes ) )
237         );
238         $options['headers'] = array(
239                 'Content-Type'          => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
240                 'Content-Length'        => strlen( $options['body'] ),
241                 'User-Agent'            => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
242         );
243
244         $raw_response = wp_remote_request( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
245
246         if( is_wp_error( $raw_response ) )
247                 return false;
248
249         if( 200 != $raw_response['response']['code'] )
250                 return false;
251
252         $response = unserialize( $raw_response['body'] );
253         if( $response )
254                 $new_option->response = $response;
255
256         update_option( 'update_themes', $new_option );
257 }
258
259 /**
260  * Check the last time plugins were run before checking plugin versions.
261  *
262  * This might have been backported to WordPress 2.6.1 for performance reasons.
263  * This is used for the wp-admin to check only so often instead of every page
264  * load.
265  *
266  * @since 2.7.0
267  * @access private
268  */
269 function _maybe_update_plugins() {
270         $current = get_option( 'update_plugins' );
271         if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
272                 return;
273         wp_update_plugins();
274 }
275
276 /**
277  * Check themes versions only after a duration of time.
278  *
279  * This is for performance reasons to make sure that on the theme version
280  * checker is not run on every page load.
281  *
282  * @since 2.7.0
283  * @access private
284  */
285 function _maybe_update_themes( ) {
286         $current = get_option( 'update_themes' );
287         if( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
288                 return;
289
290         wp_update_themes( );
291 }
292
293 add_action( 'load-plugins.php', 'wp_update_plugins' );
294 add_action( 'load-update.php', 'wp_update_plugins' );
295 add_action( 'admin_init', '_maybe_update_plugins' );
296 add_action( 'wp_update_plugins', 'wp_update_plugins' );
297
298 add_action( 'admin_init', '_maybe_update_themes' );
299 add_action( 'wp_update_themes', 'wp_update_themes' );
300
301 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
302         wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
303
304
305 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
306         wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
307
308 ?>