3 * WordPress Administration Update API
6 * @subpackage Administration
10 * Selects the first update version from the update_core option.
12 * @return object|array|false The response from the API on success, false on failure.
14 function get_preferred_from_update_core() {
15 $updates = get_core_updates();
16 if ( ! is_array( $updates ) )
18 if ( empty( $updates ) )
19 return (object) array( 'response' => 'latest' );
24 * Get available core updates.
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.
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' );
34 if ( ! is_array( $dismissed ) )
37 $from_api = get_site_transient( 'update_core' );
39 if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
42 $updates = $from_api->updates;
44 foreach ( $updates as $update ) {
45 if ( $update->response == 'autoupdate' )
48 if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
49 if ( $options['dismissed'] ) {
50 $update->dismissed = true;
54 if ( $options['available'] ) {
55 $update->dismissed = false;
64 * Gets the best available (and enabled) Auto-Update for WordPress Core.
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
70 * @return array|false False on failure, otherwise the core update offering.
72 function find_core_auto_update() {
73 $updates = get_site_transient( 'update_core' );
74 if ( ! $updates || empty( $updates->updates ) )
77 include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
80 $upgrader = new WP_Automatic_Updater;
81 foreach ( $updates->updates as $update ) {
82 if ( 'autoupdate' != $update->response )
85 if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) )
88 if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )
89 $auto_update = $update;
95 * Gets and caches the checksums for the given version of WordPress.
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.
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, '&' );
106 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
107 $url = set_url_scheme( $url, 'https' );
110 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
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’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 );
119 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
122 $body = trim( wp_remote_retrieve_body( $response ) );
123 $body = json_decode( $body, true );
125 if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
128 return $body['checksums'];
133 * @param object $update
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 );
144 * @param string $version
145 * @param string $locale
148 function undismiss_core_update( $version, $locale ) {
149 $dismissed = get_site_option( 'dismissed_update_core' );
150 $key = $version . '|' . $locale;
152 if ( ! isset( $dismissed[$key] ) )
155 unset( $dismissed[$key] );
156 return update_site_option( 'dismissed_update_core', $dismissed );
161 * @param string $version
162 * @param string $locale
163 * @return object|false
165 function find_core_update( $version, $locale ) {
166 $from_api = get_site_transient( 'update_core' );
168 if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
171 $updates = $from_api->updates;
172 foreach ( $updates as $update ) {
173 if ( $update->current == $version && $update->locale == $locale )
184 function core_update_footer( $msg = '' ) {
185 if ( !current_user_can('update_core') )
186 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
188 $cur = get_preferred_from_update_core();
189 if ( ! is_object( $cur ) )
192 if ( ! isset( $cur->current ) )
195 if ( ! isset( $cur->url ) )
198 if ( ! isset( $cur->response ) )
201 switch ( $cur->response ) {
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' ) );
206 return '<strong><a href="' . network_admin_url( 'update-core.php' ) . '">' . sprintf( __( 'Get Version %s' ), $cur->current ) . '</a></strong>';
210 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
216 * @global string $pagenow
219 function update_nag() {
220 if ( is_multisite() && !current_user_can('update_core') )
225 if ( 'update-core.php' == $pagenow )
228 $cur = get_preferred_from_update_core();
230 if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
233 if ( current_user_can( 'update_core' ) ) {
235 /* translators: 1: Codex URL to release notes, 2: new WordPress version, 3: URL to network admin, 4: accessibility text */
236 __( '<a href="%1$s">WordPress %2$s</a> is available! <a href="%3$s" aria-label="%4$s">Please update now</a>.' ),
238 /* translators: %s: WordPress version */
239 esc_url( __( 'https://codex.wordpress.org/Version_%s' ) ),
243 network_admin_url( 'update-core.php' ),
244 esc_attr__( 'Please update WordPress now' )
248 /* translators: 1: Codex URL to release notes, 2: new WordPress version */
249 __( '<a href="%1$s">WordPress %2$s</a> is available! Please notify the site administrator.' ),
251 /* translators: %s: WordPress version */
252 esc_url( __( 'https://codex.wordpress.org/Version_%s' ) ),
258 echo "<div class='update-nag'>$msg</div>";
261 // Called directly from dashboard
262 function update_right_now_message() {
263 $theme_name = wp_get_theme();
264 if ( current_user_can( 'switch_themes' ) ) {
265 $theme_name = sprintf( '<a href="themes.php">%1$s</a>', $theme_name );
270 if ( current_user_can('update_core') ) {
271 $cur = get_preferred_from_update_core();
273 if ( isset( $cur->response ) && $cur->response == 'upgrade' )
274 $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> ';
277 /* translators: 1: version number, 2: theme name */
278 $content = __( 'WordPress %1$s running %2$s theme.' );
281 * Filters the text displayed in the 'At a Glance' dashboard widget.
283 * Prior to 3.8.0, the widget was named 'Right Now'.
287 * @param string $content Default text.
289 $content = apply_filters( 'update_right_now_text', $content );
291 $msg .= sprintf( '<span id="wp-version">' . $content . '</span>', get_bloginfo( 'version', 'display' ), $theme_name );
293 echo "<p id='wp-version-message'>$msg</p>";
301 function get_plugin_updates() {
302 $all_plugins = get_plugins();
303 $upgrade_plugins = array();
304 $current = get_site_transient( 'update_plugins' );
305 foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
306 if ( isset( $current->response[ $plugin_file ] ) ) {
307 $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
308 $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
312 return $upgrade_plugins;
318 function wp_plugin_update_rows() {
319 if ( !current_user_can('update_plugins' ) )
322 $plugins = get_site_transient( 'update_plugins' );
323 if ( isset($plugins->response) && is_array($plugins->response) ) {
324 $plugins = array_keys( $plugins->response );
325 foreach ( $plugins as $plugin_file ) {
326 add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
332 * Displays update information for a plugin.
334 * @param string $file Plugin basename.
335 * @param array $plugin_data Plugin information.
338 function wp_plugin_update_row( $file, $plugin_data ) {
339 $current = get_site_transient( 'update_plugins' );
340 if ( ! isset( $current->response[ $file ] ) ) {
344 $response = $current->response[ $file ];
346 $plugins_allowedtags = array(
347 'a' => array( 'href' => array(), 'title' => array() ),
348 'abbr' => array( 'title' => array() ),
349 'acronym' => array( 'title' => array() ),
355 $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
356 $details_url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $response->slug . '§ion=changelog&TB_iframe=true&width=600&height=800' );
358 /** @var WP_Plugins_List_Table $wp_list_table */
359 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
361 if ( is_network_admin() || ! is_multisite() ) {
362 if ( is_network_admin() ) {
363 $active_class = is_plugin_active_for_network( $file ) ? ' active' : '';
365 $active_class = is_plugin_active( $file ) ? ' active' : '';
368 echo '<tr class="plugin-update-tr' . $active_class . '" id="' . esc_attr( $response->slug . '-update' ) . '" data-slug="' . esc_attr( $response->slug ) . '" data-plugin="' . esc_attr( $file ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message notice inline notice-warning notice-alt"><p>';
370 if ( ! current_user_can( 'update_plugins' ) ) {
371 /* translators: 1: plugin name, 2: details URL, 3: additional link attributes, 4: version number */
372 printf( __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.' ),
374 esc_url( $details_url ),
375 sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
376 /* translators: 1: plugin name, 2: version number */
377 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
379 $response->new_version
381 } elseif ( empty( $response->package ) ) {
382 /* translators: 1: plugin name, 2: details URL, 3: additional link attributes, 4: version number */
383 printf( __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>' ),
385 esc_url( $details_url ),
386 sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
387 /* translators: 1: plugin name, 2: version number */
388 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
390 $response->new_version
393 /* translators: 1: plugin name, 2: details URL, 3: additional link attributes, 4: version number, 5: update URL, 6: additional link attributes */
394 printf( __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s" %6$s>update now</a>.' ),
396 esc_url( $details_url ),
397 sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
398 /* translators: 1: plugin name, 2: version number */
399 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
401 $response->new_version,
402 wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ),
403 sprintf( 'class="update-link" aria-label="%s"',
404 /* translators: %s: plugin name */
405 esc_attr( sprintf( __( 'Update %s now' ), $plugin_name ) )
411 * Fires at the end of the update message container in each
412 * row of the plugins list table.
414 * The dynamic portion of the hook name, `$file`, refers to the path
415 * of the plugin's primary file relative to the plugins directory.
419 * @param array $plugin_data {
420 * An array of plugin metadata.
422 * @type string $name The human-readable name of the plugin.
423 * @type string $plugin_uri Plugin URI.
424 * @type string $version Plugin version.
425 * @type string $description Plugin description.
426 * @type string $author Plugin author.
427 * @type string $author_uri Plugin author URI.
428 * @type string $text_domain Plugin text domain.
429 * @type string $domain_path Relative path to the plugin's .mo file(s).
430 * @type bool $network Whether the plugin can only be activated network wide.
431 * @type string $title The human-readable title of the plugin.
432 * @type string $author_name Plugin author's name.
433 * @type bool $update Whether there's an available update. Default null.
435 * @param array $response {
436 * An array of metadata about the available plugin update.
438 * @type int $id Plugin ID.
439 * @type string $slug Plugin slug.
440 * @type string $new_version New plugin version.
441 * @type string $url Plugin URL.
442 * @type string $package Plugin update package URL.
445 do_action( "in_plugin_update_message-{$file}", $plugin_data, $response );
447 echo '</p></div></td></tr>';
455 function get_theme_updates() {
456 $current = get_site_transient('update_themes');
458 if ( ! isset( $current->response ) )
461 $update_themes = array();
462 foreach ( $current->response as $stylesheet => $data ) {
463 $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
464 $update_themes[ $stylesheet ]->update = $data;
467 return $update_themes;
473 function wp_theme_update_rows() {
474 if ( !current_user_can('update_themes' ) )
477 $themes = get_site_transient( 'update_themes' );
478 if ( isset($themes->response) && is_array($themes->response) ) {
479 $themes = array_keys( $themes->response );
481 foreach ( $themes as $theme ) {
482 add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
488 * Displays update information for a theme.
490 * @param string $theme_key Theme stylesheet.
491 * @param WP_Theme $theme Theme object.
494 function wp_theme_update_row( $theme_key, $theme ) {
495 $current = get_site_transient( 'update_themes' );
497 if ( ! isset( $current->response[ $theme_key ] ) ) {
501 $response = $current->response[ $theme_key ];
503 $details_url = add_query_arg( array(
504 'TB_iframe' => 'true',
507 ), $current->response[ $theme_key ]['url'] );
509 /** @var WP_MS_Themes_List_Table $wp_list_table */
510 $wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
512 $active = $theme->is_allowed( 'network' ) ? ' active' : '';
514 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 notice inline notice-warning notice-alt"><p>';
515 if ( ! current_user_can( 'update_themes' ) ) {
516 /* translators: 1: theme name, 2: details URL, 3: additional link attributes, 4: version number */
517 printf( __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.'),
519 esc_url( $details_url ),
520 sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
521 /* translators: 1: theme name, 2: version number */
522 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme['Name'], $response['new_version'] ) )
524 $response['new_version']
526 } elseif ( empty( $response['package'] ) ) {
527 /* translators: 1: theme name, 2: details URL, 3: additional link attributes, 4: version number */
528 printf( __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ),
530 esc_url( $details_url ),
531 sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
532 /* translators: 1: theme name, 2: version number */
533 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme['Name'], $response['new_version'] ) )
535 $response['new_version']
538 /* translators: 1: theme name, 2: details URL, 3: additional link attributes, 4: version number, 5: update URL, 6: additional link attributes */
539 printf( __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s" %6$s>update now</a>.' ),
541 esc_url( $details_url ),
542 sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',
543 /* translators: 1: theme name, 2: version number */
544 esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme['Name'], $response['new_version'] ) )
546 $response['new_version'],
547 wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' ) . $theme_key, 'upgrade-theme_' . $theme_key ),
548 sprintf( 'class="update-link" aria-label="%s"',
549 /* translators: %s: theme name */
550 esc_attr( sprintf( __( 'Update %s now' ), $theme['Name'] ) )
556 * Fires at the end of the update message container in each
557 * row of the themes list table.
559 * The dynamic portion of the hook name, `$theme_key`, refers to
560 * the theme slug as found in the WordPress.org themes repository.
564 * @param WP_Theme $theme The WP_Theme object.
565 * @param array $response {
566 * An array of metadata about the available theme update.
568 * @type string $new_version New theme version.
569 * @type string $url Theme URL.
570 * @type string $package Theme update package URL.
573 do_action( "in_theme_update_message-{$theme_key}", $theme, $response );
575 echo '</p></div></td></tr>';
580 * @global int $upgrading
583 function maintenance_nag() {
584 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
586 $nag = isset( $upgrading );
588 $failed = get_site_option( 'auto_core_update_failed' );
590 * If an update failed critically, we may have copied over version.php but not other files.
591 * In that case, if the install claims we're running the version we attempted, nag.
592 * This is serious enough to err on the side of nagging.
594 * If we simply failed to update before we tried to copy any files, then assume things are
595 * OK if they are now running the latest.
597 * This flag is cleared whenever a successful update occurs using Core_Upgrader.
599 $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
600 if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
607 if ( current_user_can('update_core') )
608 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
610 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
612 echo "<div class='update-nag'>$msg</div>";
616 * Prints the JavaScript templates for update admin notices.
618 * Template takes one argument with four values:
620 * param {object} data {
621 * Arguments for admin notice.
623 * @type string id ID of the notice.
624 * @type string className Class names for the notice.
625 * @type string message The notice's message.
626 * @type string type The type of update the notice is for. Either 'plugin' or 'theme'.
631 function wp_print_admin_notice_templates() {
633 <script id="tmpl-wp-updates-admin-notice" type="text/html">
634 <div <# if ( data.id ) { #>id="{{ data.id }}"<# } #> class="notice {{ data.className }}"><p>{{{ data.message }}}</p></div>
636 <script id="tmpl-wp-bulk-updates-admin-notice" type="text/html">
637 <div id="{{ data.id }}" class="{{ data.className }} notice <# if ( data.errors ) { #>notice-error<# } else { #>notice-success<# } #>">
639 <# if ( data.successes ) { #>
640 <# if ( 1 === data.successes ) { #>
641 <# if ( 'plugin' === data.type ) { #>
643 /* translators: %s: Number of plugins */
644 printf( __( '%s plugin successfully updated.' ), '{{ data.successes }}' );
648 /* translators: %s: Number of themes */
649 printf( __( '%s theme successfully updated.' ), '{{ data.successes }}' );
653 <# if ( 'plugin' === data.type ) { #>
655 /* translators: %s: Number of plugins */
656 printf( __( '%s plugins successfully updated.' ), '{{ data.successes }}' );
660 /* translators: %s: Number of themes */
661 printf( __( '%s themes successfully updated.' ), '{{ data.successes }}' );
666 <# if ( data.errors ) { #>
667 <button class="button-link bulk-action-errors-collapsed" aria-expanded="false">
668 <# if ( 1 === data.errors ) { #>
670 /* translators: %s: Number of failed updates */
671 printf( __( '%s update failed.' ), '{{ data.errors }}' );
675 /* translators: %s: Number of failed updates */
676 printf( __( '%s updates failed.' ), '{{ data.errors }}' );
679 <span class="screen-reader-text"><?php _e( 'Show more details' ); ?></span>
680 <span class="toggle-indicator" aria-hidden="true"></span>
684 <# if ( data.errors ) { #>
685 <ul class="bulk-action-errors hidden">
686 <# _.each( data.errorMessages, function( errorMessage ) { #>
687 <li>{{ errorMessage }}</li>
697 * Prints the JavaScript templates for update and deletion rows in list tables.
699 * The update template takes one argument with four values:
701 * param {object} data {
702 * Arguments for the update row
704 * @type string slug Plugin slug.
705 * @type string plugin Plugin base name.
706 * @type string colspan The number of table columns this row spans.
707 * @type string content The row content.
710 * The delete template takes one argument with four values:
712 * param {object} data {
713 * Arguments for the update row
715 * @type string slug Plugin slug.
716 * @type string plugin Plugin base name.
717 * @type string name Plugin name.
718 * @type string colspan The number of table columns this row spans.
723 function wp_print_update_row_templates() {
725 <script id="tmpl-item-update-row" type="text/template">
726 <tr class="plugin-update-tr update" id="{{ data.slug }}-update" data-slug="{{ data.slug }}" <# if ( data.plugin ) { #>data-plugin="{{ data.plugin }}"<# } #>>
727 <td colspan="{{ data.colspan }}" class="plugin-update colspanchange">
732 <script id="tmpl-item-deleted-row" type="text/template">
733 <tr class="plugin-deleted-tr inactive deleted" id="{{ data.slug }}-deleted" data-slug="{{ data.slug }}" <# if ( data.plugin ) { #>data-plugin="{{ data.plugin }}"<# } #>>
734 <td colspan="{{ data.colspan }}" class="plugin-update colspanchange">
735 <# if ( data.plugin ) { #>
738 /* translators: %s: Plugin name */
739 _x( '%s was successfully deleted.', 'plugin' ),
740 '<strong>{{{ data.name }}}</strong>'
746 /* translators: %s: Theme name */
747 _x( '%s was successfully deleted.', 'theme' ),
748 '<strong>{{{ data.name }}}</strong>'