]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/update.php
WordPress 3.7-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://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), null, '&' );
107
108         if ( 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
117         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
118                 return false;
119
120         $body = trim( wp_remote_retrieve_body( $response ) );
121         $body = json_decode( $body, true );
122
123         if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
124                 return false;
125
126         return $body['checksums'];
127 }
128
129 function dismiss_core_update( $update ) {
130         $dismissed = get_site_option( 'dismissed_update_core' );
131         $dismissed[ $update->current . '|' . $update->locale ] = true;
132         return update_site_option( 'dismissed_update_core', $dismissed );
133 }
134
135 function undismiss_core_update( $version, $locale ) {
136         $dismissed = get_site_option( 'dismissed_update_core' );
137         $key = $version . '|' . $locale;
138
139         if ( ! isset( $dismissed[$key] ) )
140                 return false;
141
142         unset( $dismissed[$key] );
143         return update_site_option( 'dismissed_update_core', $dismissed );
144 }
145
146 function find_core_update( $version, $locale ) {
147         $from_api = get_site_transient( 'update_core' );
148
149         if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
150                 return false;
151
152         $updates = $from_api->updates;
153         foreach ( $updates as $update ) {
154                 if ( $update->current == $version && $update->locale == $locale )
155                         return $update;
156         }
157         return false;
158 }
159
160 function core_update_footer( $msg = '' ) {
161         if ( !current_user_can('update_core') )
162                 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
163
164         $cur = get_preferred_from_update_core();
165         if ( ! is_object( $cur ) )
166                 $cur = new stdClass;
167
168         if ( ! isset( $cur->current ) )
169                 $cur->current = '';
170
171         if ( ! isset( $cur->url ) )
172                 $cur->url = '';
173
174         if ( ! isset( $cur->response ) )
175                 $cur->response = '';
176
177         switch ( $cur->response ) {
178         case 'development' :
179                 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' ) );
180         break;
181
182         case 'upgrade' :
183                 return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', network_admin_url( 'update-core.php' ), $cur->current);
184         break;
185
186         case 'latest' :
187         default :
188                 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
189         break;
190         }
191 }
192 add_filter( 'update_footer', 'core_update_footer' );
193
194 function update_nag() {
195         if ( is_multisite() && !current_user_can('update_core') )
196                 return false;
197
198         global $pagenow;
199
200         if ( 'update-core.php' == $pagenow )
201                 return;
202
203         $cur = get_preferred_from_update_core();
204
205         if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
206                 return false;
207
208         if ( current_user_can('update_core') ) {
209                 $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' ) );
210         } else {
211                 $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
212         }
213         echo "<div class='update-nag'>$msg</div>";
214 }
215 add_action( 'admin_notices', 'update_nag', 3 );
216 add_action( 'network_admin_notices', 'update_nag', 3 );
217
218 // Called directly from dashboard
219 function update_right_now_message() {
220         $msg = sprintf( __( 'You are using <span class="b">WordPress %s</span>.' ), get_bloginfo( 'version', 'display' ) );
221
222         if ( current_user_can('update_core') ) {
223                 $cur = get_preferred_from_update_core();
224
225                 if ( isset( $cur->response ) && $cur->response == 'upgrade' )
226                         $msg .= " <a href='" . network_admin_url( 'update-core.php' ) . "' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
227         }
228
229         echo "<span id='wp-version-message'>$msg</span>";
230 }
231
232 function get_plugin_updates() {
233         $all_plugins = get_plugins();
234         $upgrade_plugins = array();
235         $current = get_site_transient( 'update_plugins' );
236         foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
237                 if ( isset( $current->response[ $plugin_file ] ) ) {
238                         $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
239                         $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
240                 }
241         }
242
243         return $upgrade_plugins;
244 }
245
246 function wp_plugin_update_rows() {
247         if ( !current_user_can('update_plugins' ) )
248                 return;
249
250         $plugins = get_site_transient( 'update_plugins' );
251         if ( isset($plugins->response) && is_array($plugins->response) ) {
252                 $plugins = array_keys( $plugins->response );
253                 foreach( $plugins as $plugin_file ) {
254                         add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
255                 }
256         }
257 }
258 add_action( 'admin_init', 'wp_plugin_update_rows' );
259
260 function wp_plugin_update_row( $file, $plugin_data ) {
261         $current = get_site_transient( 'update_plugins' );
262         if ( !isset( $current->response[ $file ] ) )
263                 return false;
264
265         $r = $current->response[ $file ];
266
267         $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
268         $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
269
270         $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
271
272         $wp_list_table = _get_list_table('WP_Plugins_List_Table');
273
274         if ( is_network_admin() || !is_multisite() ) {
275                 echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
276
277                 if ( ! current_user_can('update_plugins') )
278                         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 );
279                 else if ( empty($r->package) )
280                         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 );
281                 else
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> 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) );
283
284                 do_action( "in_plugin_update_message-$file", $plugin_data, $r );
285
286                 echo '</div></td></tr>';
287         }
288 }
289
290 function get_theme_updates() {
291         $themes = wp_get_themes();
292         $current = get_site_transient('update_themes');
293
294         if ( ! isset( $current->response ) )
295                 return array();
296
297         $update_themes = array();
298         foreach ( $current->response as $stylesheet => $data ) {
299                 $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
300                 $update_themes[ $stylesheet ]->update = $data;
301         }
302
303         return $update_themes;
304 }
305
306 function wp_theme_update_rows() {
307         if ( !current_user_can('update_themes' ) )
308                 return;
309
310         $themes = get_site_transient( 'update_themes' );
311         if ( isset($themes->response) && is_array($themes->response) ) {
312                 $themes = array_keys( $themes->response );
313
314                 foreach( $themes as $theme ) {
315                         add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
316                 }
317         }
318 }
319 add_action( 'admin_init', 'wp_theme_update_rows' );
320
321 function wp_theme_update_row( $theme_key, $theme ) {
322         $current = get_site_transient( 'update_themes' );
323         if ( !isset( $current->response[ $theme_key ] ) )
324                 return false;
325         $r = $current->response[ $theme_key ];
326         $themes_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
327         $theme_name = wp_kses( $theme['Name'], $themes_allowedtags );
328
329         $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
330
331         $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
332
333         echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
334         if ( ! current_user_can('update_themes') )
335                 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 );
336         else if ( empty( $r['package'] ) )
337                 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'] );
338         else
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> 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) );
340
341         do_action( "in_theme_update_message-$theme_key", $theme, $r );
342
343         echo '</div></td></tr>';
344 }
345
346 function maintenance_nag() {
347         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
348         global $upgrading;
349         $nag = isset( $upgrading );
350         if ( ! $nag ) {
351                 $failed = get_site_option( 'auto_core_update_failed' );
352                 /*
353                  * If an update failed critically, we may have copied over version.php but not other files.
354                  * In that case, if the install claims we're running the version we attempted, nag.
355                  * This is serious enough to err on the side of nagging.
356                  *
357                  * If we simply failed to update before we tried to copy any files, then assume things are
358                  * OK if they are now running the latest.
359                  *
360                  * This flag is cleared whenever a successful update occurs using Core_Upgrader.
361                  */
362                 $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
363                 if ( version_compare( $failed['attempted'], $wp_version, '>=' ) )
364                         $nag = true;
365         }
366
367         if ( ! $nag )
368                 return false;
369
370         if ( current_user_can('update_core') )
371                 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
372         else
373                 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
374
375         echo "<div class='update-nag'>$msg</div>";
376 }
377 add_action( 'admin_notices', 'maintenance_nag' );
378 add_action( 'network_admin_notices', 'maintenance_nag' );