]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/update.php
WordPress 3.7.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / update.php
1 <?php
2 /**
3  * WordPress Administration Update API
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Selects the first update version from the update_core option
11  *
12  * @return object the response from the API
13  */
14 function get_preferred_from_update_core() {
15         $updates = get_core_updates();
16         if ( ! is_array( $updates ) )
17                 return false;
18         if ( empty( $updates ) )
19                 return (object) array( 'response' => 'latest' );
20         return $updates[0];
21 }
22
23 /**
24  * Get available core updates
25  *
26  * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
27  *      set $options['available'] to false to skip not-dismissed updates.
28  * @return array Array of the update objects
29  */
30 function get_core_updates( $options = array() ) {
31         $options = array_merge( array( 'available' => true, 'dismissed' => false ), $options );
32         $dismissed = get_site_option( 'dismissed_update_core' );
33
34         if ( ! is_array( $dismissed ) )
35                 $dismissed = array();
36
37         $from_api = get_site_transient( 'update_core' );
38
39         if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
40                 return false;
41
42         $updates = $from_api->updates;
43         $result = array();
44         foreach ( $updates as $update ) {
45                 if ( $update->response == 'autoupdate' )
46                         continue;
47
48                 if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
49                         if ( $options['dismissed'] ) {
50                                 $update->dismissed = true;
51                                 $result[] = $update;
52                         }
53                 } else {
54                         if ( $options['available'] ) {
55                                 $update->dismissed = false;
56                                 $result[] = $update;
57                         }
58                 }
59         }
60         return $result;
61 }
62
63 /**
64  * Gets the best available (and enabled) Auto-Update for WordPress Core.
65  *
66  * If there's 1.2.3 and 1.3 on offer, it'll choose 1.3 if the install allows it, else, 1.2.3
67  *
68  * @since 3.7.0
69  *
70  * @return bool|array False on failure, otherwise the core update offering.
71  */
72 function find_core_auto_update() {
73         $updates = get_site_transient( 'update_core' );
74         if ( ! $updates || empty( $updates->updates ) )
75                 return false;
76
77         include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
78
79         $auto_update = false;
80         $upgrader = new WP_Automatic_Updater;
81         foreach ( $updates->updates as $update ) {
82                 if ( 'autoupdate' != $update->response )
83                         continue;
84
85                 if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) )
86                         continue;
87
88                 if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )
89                         $auto_update = $update;
90         }
91         return $auto_update;
92 }
93
94 /**
95  * Gets and caches the checksums for the given version of WordPress.
96  *
97  * @since 3.7.0
98  *
99  * @param string $version Version string to query.
100  * @param string $locale  Locale to query.
101  * @return bool|array False on failure. An array of checksums on success.
102  */
103 function get_core_checksums( $version, $locale ) {
104         $return = array();
105
106         $url = $http_url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), null, '&' );
107
108         if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
109                 $url = set_url_scheme( $url, 'https' );
110
111         $options = array(
112                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
113         );
114
115         $response = wp_remote_get( $url, $options );
116         if ( $ssl && is_wp_error( $response ) ) {
117                 trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ) . ' ' . '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)', headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
118                 $response = wp_remote_get( $http_url, $options );
119         }
120
121         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
122                 return false;
123
124         $body = trim( wp_remote_retrieve_body( $response ) );
125         $body = json_decode( $body, true );
126
127         if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
128                 return false;
129
130         return $body['checksums'];
131 }
132
133 function dismiss_core_update( $update ) {
134         $dismissed = get_site_option( 'dismissed_update_core' );
135         $dismissed[ $update->current . '|' . $update->locale ] = true;
136         return update_site_option( 'dismissed_update_core', $dismissed );
137 }
138
139 function undismiss_core_update( $version, $locale ) {
140         $dismissed = get_site_option( 'dismissed_update_core' );
141         $key = $version . '|' . $locale;
142
143         if ( ! isset( $dismissed[$key] ) )
144                 return false;
145
146         unset( $dismissed[$key] );
147         return update_site_option( 'dismissed_update_core', $dismissed );
148 }
149
150 function find_core_update( $version, $locale ) {
151         $from_api = get_site_transient( 'update_core' );
152
153         if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
154                 return false;
155
156         $updates = $from_api->updates;
157         foreach ( $updates as $update ) {
158                 if ( $update->current == $version && $update->locale == $locale )
159                         return $update;
160         }
161         return false;
162 }
163
164 function core_update_footer( $msg = '' ) {
165         if ( !current_user_can('update_core') )
166                 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
167
168         $cur = get_preferred_from_update_core();
169         if ( ! is_object( $cur ) )
170                 $cur = new stdClass;
171
172         if ( ! isset( $cur->current ) )
173                 $cur->current = '';
174
175         if ( ! isset( $cur->url ) )
176                 $cur->url = '';
177
178         if ( ! isset( $cur->response ) )
179                 $cur->response = '';
180
181         switch ( $cur->response ) {
182         case 'development' :
183                 return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), get_bloginfo( 'version', 'display' ), network_admin_url( 'update-core.php' ) );
184         break;
185
186         case 'upgrade' :
187                 return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', network_admin_url( 'update-core.php' ), $cur->current);
188         break;
189
190         case 'latest' :
191         default :
192                 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
193         break;
194         }
195 }
196 add_filter( 'update_footer', 'core_update_footer' );
197
198 function update_nag() {
199         if ( is_multisite() && !current_user_can('update_core') )
200                 return false;
201
202         global $pagenow;
203
204         if ( 'update-core.php' == $pagenow )
205                 return;
206
207         $cur = get_preferred_from_update_core();
208
209         if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
210                 return false;
211
212         if ( current_user_can('update_core') ) {
213                 $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! <a href="%2$s">Please update now</a>.'), $cur->current, network_admin_url( 'update-core.php' ) );
214         } else {
215                 $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
216         }
217         echo "<div class='update-nag'>$msg</div>";
218 }
219 add_action( 'admin_notices', 'update_nag', 3 );
220 add_action( 'network_admin_notices', 'update_nag', 3 );
221
222 // Called directly from dashboard
223 function update_right_now_message() {
224         $msg = sprintf( __( 'You are using <span class="b">WordPress %s</span>.' ), get_bloginfo( 'version', 'display' ) );
225
226         if ( current_user_can('update_core') ) {
227                 $cur = get_preferred_from_update_core();
228
229                 if ( isset( $cur->response ) && $cur->response == 'upgrade' )
230                         $msg .= " <a href='" . network_admin_url( 'update-core.php' ) . "' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
231         }
232
233         echo "<span id='wp-version-message'>$msg</span>";
234 }
235
236 function get_plugin_updates() {
237         $all_plugins = get_plugins();
238         $upgrade_plugins = array();
239         $current = get_site_transient( 'update_plugins' );
240         foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
241                 if ( isset( $current->response[ $plugin_file ] ) ) {
242                         $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
243                         $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
244                 }
245         }
246
247         return $upgrade_plugins;
248 }
249
250 function wp_plugin_update_rows() {
251         if ( !current_user_can('update_plugins' ) )
252                 return;
253
254         $plugins = get_site_transient( 'update_plugins' );
255         if ( isset($plugins->response) && is_array($plugins->response) ) {
256                 $plugins = array_keys( $plugins->response );
257                 foreach( $plugins as $plugin_file ) {
258                         add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
259                 }
260         }
261 }
262 add_action( 'admin_init', 'wp_plugin_update_rows' );
263
264 function wp_plugin_update_row( $file, $plugin_data ) {
265         $current = get_site_transient( 'update_plugins' );
266         if ( !isset( $current->response[ $file ] ) )
267                 return false;
268
269         $r = $current->response[ $file ];
270
271         $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
272         $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
273
274         $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
275
276         $wp_list_table = _get_list_table('WP_Plugins_List_Table');
277
278         if ( is_network_admin() || !is_multisite() ) {
279                 echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
280
281                 if ( ! current_user_can('update_plugins') )
282                         printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
283                 else if ( empty($r->package) )
284                         printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
285                 else
286                         printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version, wp_nonce_url( self_admin_url('update.php?action=upgrade-plugin&plugin=') . $file, 'upgrade-plugin_' . $file) );
287
288                 do_action( "in_plugin_update_message-$file", $plugin_data, $r );
289
290                 echo '</div></td></tr>';
291         }
292 }
293
294 function get_theme_updates() {
295         $themes = wp_get_themes();
296         $current = get_site_transient('update_themes');
297
298         if ( ! isset( $current->response ) )
299                 return array();
300
301         $update_themes = array();
302         foreach ( $current->response as $stylesheet => $data ) {
303                 $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
304                 $update_themes[ $stylesheet ]->update = $data;
305         }
306
307         return $update_themes;
308 }
309
310 function wp_theme_update_rows() {
311         if ( !current_user_can('update_themes' ) )
312                 return;
313
314         $themes = get_site_transient( 'update_themes' );
315         if ( isset($themes->response) && is_array($themes->response) ) {
316                 $themes = array_keys( $themes->response );
317
318                 foreach( $themes as $theme ) {
319                         add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
320                 }
321         }
322 }
323 add_action( 'admin_init', 'wp_theme_update_rows' );
324
325 function wp_theme_update_row( $theme_key, $theme ) {
326         $current = get_site_transient( 'update_themes' );
327         if ( !isset( $current->response[ $theme_key ] ) )
328                 return false;
329         $r = $current->response[ $theme_key ];
330         $themes_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
331         $theme_name = wp_kses( $theme['Name'], $themes_allowedtags );
332
333         $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
334
335         $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
336
337         echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
338         if ( ! current_user_can('update_themes') )
339                 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r->new_version );
340         else if ( empty( $r['package'] ) )
341                 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r['new_version'] );
342         else
343                 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r['new_version'], wp_nonce_url( self_admin_url('update.php?action=upgrade-theme&theme=') . $theme_key, 'upgrade-theme_' . $theme_key) );
344
345         do_action( "in_theme_update_message-$theme_key", $theme, $r );
346
347         echo '</div></td></tr>';
348 }
349
350 function maintenance_nag() {
351         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
352         global $upgrading;
353         $nag = isset( $upgrading );
354         if ( ! $nag ) {
355                 $failed = get_site_option( 'auto_core_update_failed' );
356                 /*
357                  * If an update failed critically, we may have copied over version.php but not other files.
358                  * In that case, if the install claims we're running the version we attempted, nag.
359                  * This is serious enough to err on the side of nagging.
360                  *
361                  * If we simply failed to update before we tried to copy any files, then assume things are
362                  * OK if they are now running the latest.
363                  *
364                  * This flag is cleared whenever a successful update occurs using Core_Upgrader.
365                  */
366                 $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
367                 if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
368                         $nag = true;
369         }
370
371         if ( ! $nag )
372                 return false;
373
374         if ( current_user_can('update_core') )
375                 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
376         else
377                 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
378
379         echo "<div class='update-nag'>$msg</div>";
380 }
381 add_action( 'admin_notices', 'maintenance_nag' );
382 add_action( 'network_admin_notices', 'maintenance_nag' );