]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/update.php
WordPress 4.5.2
[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|array|false The response from the API on success, false on failure.
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|false Array of the update objects on success, false on failure.
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 array|false 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         $url = $http_url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), null, '&' );
105
106         if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
107                 $url = set_url_scheme( $url, 'https' );
108
109         $options = array(
110                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
111         );
112
113         $response = wp_remote_get( $url, $options );
114         if ( $ssl && is_wp_error( $response ) ) {
115                 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="https://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 );
116                 $response = wp_remote_get( $http_url, $options );
117         }
118
119         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
120                 return false;
121
122         $body = trim( wp_remote_retrieve_body( $response ) );
123         $body = json_decode( $body, true );
124
125         if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
126                 return false;
127
128         return $body['checksums'];
129 }
130
131 /**
132  *
133  * @param object $update
134  * @return bool
135  */
136 function dismiss_core_update( $update ) {
137         $dismissed = get_site_option( 'dismissed_update_core' );
138         $dismissed[ $update->current . '|' . $update->locale ] = true;
139         return update_site_option( 'dismissed_update_core', $dismissed );
140 }
141
142 /**
143  *
144  * @param string $version
145  * @param string $locale
146  * @return bool
147  */
148 function undismiss_core_update( $version, $locale ) {
149         $dismissed = get_site_option( 'dismissed_update_core' );
150         $key = $version . '|' . $locale;
151
152         if ( ! isset( $dismissed[$key] ) )
153                 return false;
154
155         unset( $dismissed[$key] );
156         return update_site_option( 'dismissed_update_core', $dismissed );
157 }
158
159 /**
160  *
161  * @param string $version
162  * @param string $locale
163  * @return object|false
164  */
165 function find_core_update( $version, $locale ) {
166         $from_api = get_site_transient( 'update_core' );
167
168         if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
169                 return false;
170
171         $updates = $from_api->updates;
172         foreach ( $updates as $update ) {
173                 if ( $update->current == $version && $update->locale == $locale )
174                         return $update;
175         }
176         return false;
177 }
178
179 /**
180  *
181  * @param string $msg
182  * @return string
183  */
184 function core_update_footer( $msg = '' ) {
185         if ( !current_user_can('update_core') )
186                 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
187
188         $cur = get_preferred_from_update_core();
189         if ( ! is_object( $cur ) )
190                 $cur = new stdClass;
191
192         if ( ! isset( $cur->current ) )
193                 $cur->current = '';
194
195         if ( ! isset( $cur->url ) )
196                 $cur->url = '';
197
198         if ( ! isset( $cur->response ) )
199                 $cur->response = '';
200
201         switch ( $cur->response ) {
202         case 'development' :
203                 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' ) );
204
205         case 'upgrade' :
206                 return '<strong><a href="' . network_admin_url( 'update-core.php' ) . '">' . sprintf( __( 'Get Version %s' ), $cur->current ) . '</a></strong>';
207
208         case 'latest' :
209         default :
210                 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
211         }
212 }
213
214 /**
215  *
216  * @global string $pagenow
217  * @return false|void
218  */
219 function update_nag() {
220         if ( is_multisite() && !current_user_can('update_core') )
221                 return false;
222
223         global $pagenow;
224
225         if ( 'update-core.php' == $pagenow )
226                 return;
227
228         $cur = get_preferred_from_update_core();
229
230         if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
231                 return false;
232
233         if ( current_user_can('update_core') ) {
234                 $msg = sprintf( __( '<a href="https://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! <a href="%2$s" aria-label="Please update WordPress now">Please update now</a>.' ), $cur->current, network_admin_url( 'update-core.php' ) );
235         } else {
236                 $msg = sprintf( __('<a href="https://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
237         }
238         echo "<div class='update-nag'>$msg</div>";
239 }
240
241 // Called directly from dashboard
242 function update_right_now_message() {
243         $theme_name = wp_get_theme();
244         if ( current_user_can( 'switch_themes' ) ) {
245                 $theme_name = sprintf( '<a href="themes.php">%1$s</a>', $theme_name );
246         }
247
248         $msg = '';
249
250         if ( current_user_can('update_core') ) {
251                 $cur = get_preferred_from_update_core();
252
253                 if ( isset( $cur->response ) && $cur->response == 'upgrade' )
254                         $msg .= '<a href="' . network_admin_url( 'update-core.php' ) . '" class="button" aria-describedby="wp-version">' . sprintf( __( 'Update to %s' ), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a> ';
255         }
256
257         /* translators: 1: version number, 2: theme name */
258         $content = __( 'WordPress %1$s running %2$s theme.' );
259
260         /**
261          * Filter the text displayed in the 'At a Glance' dashboard widget.
262          *
263          * Prior to 3.8.0, the widget was named 'Right Now'.
264          *
265          * @since 4.4.0
266          *
267          * @param string $content Default text.
268          */
269         $content = apply_filters( 'update_right_now_text', $content );
270
271         $msg .= sprintf( '<span id="wp-version">' . $content . '</span>', get_bloginfo( 'version', 'display' ), $theme_name );
272
273         echo "<p id='wp-version-message'>$msg</p>";
274 }
275
276 /**
277  * @since 2.9.0
278  *
279  * @return array
280  */
281 function get_plugin_updates() {
282         $all_plugins = get_plugins();
283         $upgrade_plugins = array();
284         $current = get_site_transient( 'update_plugins' );
285         foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
286                 if ( isset( $current->response[ $plugin_file ] ) ) {
287                         $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
288                         $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
289                 }
290         }
291
292         return $upgrade_plugins;
293 }
294
295 /**
296  * @since 2.9.0
297  */
298 function wp_plugin_update_rows() {
299         if ( !current_user_can('update_plugins' ) )
300                 return;
301
302         $plugins = get_site_transient( 'update_plugins' );
303         if ( isset($plugins->response) && is_array($plugins->response) ) {
304                 $plugins = array_keys( $plugins->response );
305                 foreach ( $plugins as $plugin_file ) {
306                         add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
307                 }
308         }
309 }
310
311 /**
312  *
313  * @param string $file
314  * @param array  $plugin_data
315  * @return false|void
316  */
317 function wp_plugin_update_row( $file, $plugin_data ) {
318         $current = get_site_transient( 'update_plugins' );
319         if ( !isset( $current->response[ $file ] ) )
320                 return false;
321
322         $r = $current->response[ $file ];
323
324         $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
325         $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
326
327         $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
328
329         $wp_list_table = _get_list_table('WP_Plugins_List_Table');
330
331         if ( is_network_admin() || !is_multisite() ) {
332                 if ( is_network_admin() ) {
333                         $active_class = is_plugin_active_for_network( $file ) ? ' active': '';
334                 } else {
335                         $active_class = is_plugin_active( $file ) ? ' active' : '';
336                 }
337
338                 echo '<tr class="plugin-update-tr' . $active_class . '" id="' . esc_attr( $r->slug . '-update' ) . '" data-slug="' . esc_attr( $r->slug ) . '" data-plugin="' . esc_attr( $file ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message">';
339
340                 if ( ! current_user_can( 'update_plugins' ) ) {
341                         /* translators: 1: plugin name, 2: details URL, 3: accessibility text, 4: version number */
342                         printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>.' ),
343                                 $plugin_name,
344                                 esc_url( $details_url ),
345                                 /* translators: 1: plugin name, 2: version number */
346                                 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $r->new_version ) ),
347                                 $r->new_version
348                         );
349                 } elseif ( empty( $r->package ) ) {
350                         /* translators: 1: plugin name, 2: details URL, 3: accessibility text, 4: version number */
351                         printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>' ),
352                                 $plugin_name,
353                                 esc_url( $details_url ),
354                                 /* translators: 1: plugin name, 2: version number */
355                                 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $r->new_version ) ),
356                                 $r->new_version
357                         );
358                 } else {
359                         /* translators: 1: plugin name, 2: details URL, 3: accessibility text, 4: version number, 5: update URL, 6: accessibility text */
360                         printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a> or <a href="%5$s" class="update-link" aria-label="%6$s">update now</a>.' ),
361                                 $plugin_name,
362                                 esc_url( $details_url ),
363                                 /* translators: 1: plugin name, 2: version number */
364                                 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $r->new_version ) ),
365                                 $r->new_version,
366                                 wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ),
367                                 /* translators: %s: plugin name */
368                                 esc_attr( sprintf( __( 'Update %s now' ), $plugin_name ) )
369                         );
370                 }
371                 /**
372                  * Fires at the end of the update message container in each
373                  * row of the plugins list table.
374                  *
375                  * The dynamic portion of the hook name, `$file`, refers to the path
376                  * of the plugin's primary file relative to the plugins directory.
377                  *
378                  * @since 2.8.0
379                  *
380                  * @param array $plugin_data {
381                  *     An array of plugin metadata.
382                  *
383                  *     @type string $name         The human-readable name of the plugin.
384                  *     @type string $plugin_uri   Plugin URI.
385                  *     @type string $version      Plugin version.
386                  *     @type string $description  Plugin description.
387                  *     @type string $author       Plugin author.
388                  *     @type string $author_uri   Plugin author URI.
389                  *     @type string $text_domain  Plugin text domain.
390                  *     @type string $domain_path  Relative path to the plugin's .mo file(s).
391                  *     @type bool   $network      Whether the plugin can only be activated network wide.
392                  *     @type string $title        The human-readable title of the plugin.
393                  *     @type string $author_name  Plugin author's name.
394                  *     @type bool   $update       Whether there's an available update. Default null.
395                  * }
396                  * @param array $r {
397                  *     An array of metadata about the available plugin update.
398                  *
399                  *     @type int    $id           Plugin ID.
400                  *     @type string $slug         Plugin slug.
401                  *     @type string $new_version  New plugin version.
402                  *     @type string $url          Plugin URL.
403                  *     @type string $package      Plugin update package URL.
404                  * }
405                  */
406                 do_action( "in_plugin_update_message-{$file}", $plugin_data, $r );
407
408                 echo '</div></td></tr>';
409         }
410 }
411
412 /**
413  *
414  * @return array
415  */
416 function get_theme_updates() {
417         $current = get_site_transient('update_themes');
418
419         if ( ! isset( $current->response ) )
420                 return array();
421
422         $update_themes = array();
423         foreach ( $current->response as $stylesheet => $data ) {
424                 $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
425                 $update_themes[ $stylesheet ]->update = $data;
426         }
427
428         return $update_themes;
429 }
430
431 /**
432  * @since 3.1.0
433  */
434 function wp_theme_update_rows() {
435         if ( !current_user_can('update_themes' ) )
436                 return;
437
438         $themes = get_site_transient( 'update_themes' );
439         if ( isset($themes->response) && is_array($themes->response) ) {
440                 $themes = array_keys( $themes->response );
441
442                 foreach ( $themes as $theme ) {
443                         add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
444                 }
445         }
446 }
447
448 /**
449  *
450  * @param string   $theme_key
451  * @param WP_Theme $theme
452  * @return false|void
453  */
454 function wp_theme_update_row( $theme_key, $theme ) {
455         $current = get_site_transient( 'update_themes' );
456         if ( !isset( $current->response[ $theme_key ] ) )
457                 return false;
458
459         $r = $current->response[ $theme_key ];
460
461         $theme_name = $theme['Name'];
462
463         $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
464
465         $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
466
467         $active = $theme->is_allowed( 'network' ) ? ' active': '';
468
469         echo '<tr class="plugin-update-tr' . $active . '" id="' . esc_attr( $theme->get_stylesheet() . '-update' ) . '" data-slug="' . esc_attr( $theme->get_stylesheet() ) . '"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
470         if ( ! current_user_can('update_themes') ) {
471                 /* translators: 1: theme name, 2: details URL, 3: accessibility text, 4: version number */
472                 printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>.'),
473                         $theme_name,
474                         esc_url( $details_url ),
475                         /* translators: 1: theme name, 2: version number */
476                         esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $r['new_version'] ) ),
477                         $r['new_version']
478                 );
479         } elseif ( empty( $r['package'] ) ) {
480                 /* translators: 1: theme name, 2: details URL, 3: accessibility text, 4: version number */
481                 printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ),
482                         $theme_name,
483                         esc_url( $details_url ),
484                         /* translators: 1: theme name, 2: version number */
485                         esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $r['new_version'] ) ),
486                         $r['new_version']
487                 );
488         } else {
489                 /* translators: 1: theme name, 2: details URL, 3: accessibility text, 4: version number, 5: update URL, 6: accessibility text */
490                 printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a> or <a href="%5$s" class="update-link" aria-label="%6$s">update now</a>.' ),
491                         $theme_name,
492                         esc_url( $details_url ),
493                         /* translators: 1: theme name, 2: version number */
494                         esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $r['new_version'] ) ),
495                         $r['new_version'],
496                         wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' ) . $theme_key, 'upgrade-theme_' . $theme_key ),
497                         /* translators: %s: theme name */
498                         esc_attr( sprintf( __( 'Update %s now' ), $theme_name ) )
499                 );
500         }
501         /**
502          * Fires at the end of the update message container in each
503          * row of the themes list table.
504          *
505          * The dynamic portion of the hook name, `$theme_key`, refers to
506          * the theme slug as found in the WordPress.org themes repository.
507          *
508          * @since 3.1.0
509          *
510          * @param WP_Theme $theme The WP_Theme object.
511          * @param array    $r {
512          *     An array of metadata about the available theme update.
513          *
514          *     @type string $new_version New theme version.
515          *     @type string $url         Theme URL.
516          *     @type string $package     Theme update package URL.
517          * }
518          */
519         do_action( "in_theme_update_message-{$theme_key}", $theme, $r );
520
521         echo '</div></td></tr>';
522 }
523
524 /**
525  *
526  * @global int $upgrading
527  * @return false|void
528  */
529 function maintenance_nag() {
530         include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
531         global $upgrading;
532         $nag = isset( $upgrading );
533         if ( ! $nag ) {
534                 $failed = get_site_option( 'auto_core_update_failed' );
535                 /*
536                  * If an update failed critically, we may have copied over version.php but not other files.
537                  * In that case, if the install claims we're running the version we attempted, nag.
538                  * This is serious enough to err on the side of nagging.
539                  *
540                  * If we simply failed to update before we tried to copy any files, then assume things are
541                  * OK if they are now running the latest.
542                  *
543                  * This flag is cleared whenever a successful update occurs using Core_Upgrader.
544                  */
545                 $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
546                 if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
547                         $nag = true;
548         }
549
550         if ( ! $nag )
551                 return false;
552
553         if ( current_user_can('update_core') )
554                 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
555         else
556                 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
557
558         echo "<div class='update-nag'>$msg</div>";
559 }