[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> update.php (source)

   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.
  13   *
  14   * Checks against the WordPress server at api.wordpress.org. Will only check
  15   * if WordPress isn't installing.
  16   *
  17   * @since 2.3.0
  18   *
  19   * @global string $wp_version       Used to check against the newest WordPress version.
  20   * @global wpdb   $wpdb             WordPress database abstraction object.
  21   * @global string $wp_local_package Locale code of the package.
  22   *
  23   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
  24   * @param bool  $force_check Whether to bypass the transient cache and force a fresh update check. Defaults to false, true if $extra_stats is set.
  25   */
  26  function wp_version_check( $extra_stats = array(), $force_check = false ) {
  27      global $wpdb, $wp_local_package;
  28  
  29      if ( wp_installing() ) {
  30          return;
  31      }
  32  
  33      // Include an unmodified $wp_version.
  34      require  ABSPATH . WPINC . '/version.php';
  35      $php_version = phpversion();
  36  
  37      $current      = get_site_transient( 'update_core' );
  38      $translations = wp_get_installed_translations( 'core' );
  39  
  40      // Invalidate the transient when $wp_version changes.
  41      if ( is_object( $current ) && $wp_version !== $current->version_checked ) {
  42          $current = false;
  43      }
  44  
  45      if ( ! is_object( $current ) ) {
  46          $current                  = new stdClass;
  47          $current->updates         = array();
  48          $current->version_checked = $wp_version;
  49      }
  50  
  51      if ( ! empty( $extra_stats ) ) {
  52          $force_check = true;
  53      }
  54  
  55      // Wait 1 minute between multiple version check requests.
  56      $timeout          = MINUTE_IN_SECONDS;
  57      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  58  
  59      if ( ! $force_check && $time_not_changed ) {
  60          return;
  61      }
  62  
  63      /**
  64       * Filters the locale requested for WordPress core translations.
  65       *
  66       * @since 2.8.0
  67       *
  68       * @param string $locale Current locale.
  69       */
  70      $locale = apply_filters( 'core_version_check_locale', get_locale() );
  71  
  72      // Update last_checked for current to prevent multiple blocking requests if request hangs.
  73      $current->last_checked = time();
  74      set_site_transient( 'update_core', $current );
  75  
  76      if ( method_exists( $wpdb, 'db_version' ) ) {
  77          $mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
  78      } else {
  79          $mysql_version = 'N/A';
  80      }
  81  
  82      if ( is_multisite() ) {
  83          $num_blogs         = get_blog_count();
  84          $wp_install        = network_site_url();
  85          $multisite_enabled = 1;
  86      } else {
  87          $multisite_enabled = 0;
  88          $num_blogs         = 1;
  89          $wp_install        = home_url( '/' );
  90      }
  91  
  92      $query = array(
  93          'version'            => $wp_version,
  94          'php'                => $php_version,
  95          'locale'             => $locale,
  96          'mysql'              => $mysql_version,
  97          'local_package'      => isset( $wp_local_package ) ? $wp_local_package : '',
  98          'blogs'              => $num_blogs,
  99          'users'              => get_user_count(),
 100          'multisite_enabled'  => $multisite_enabled,
 101          'initial_db_version' => get_site_option( 'initial_db_version' ),
 102      );
 103  
 104      /**
 105       * Filters the query arguments sent as part of the core version check.
 106       *
 107       * WARNING: Changing this data may result in your site not receiving security updates.
 108       * Please exercise extreme caution.
 109       *
 110       * @since 4.9.0
 111       *
 112       * @param array $query {
 113       *     Version check query arguments.
 114       *
 115       *     @type string $version            WordPress version number.
 116       *     @type string $php                PHP version number.
 117       *     @type string $locale             The locale to retrieve updates for.
 118       *     @type string $mysql              MySQL version number.
 119       *     @type string $local_package      The value of the $wp_local_package global, when set.
 120       *     @type int    $blogs              Number of sites on this WordPress installation.
 121       *     @type int    $users              Number of users on this WordPress installation.
 122       *     @type int    $multisite_enabled  Whether this WordPress installation uses Multisite.
 123       *     @type int    $initial_db_version Database version of WordPress at time of installation.
 124       * }
 125       */
 126      $query = apply_filters( 'core_version_check_query_args', $query );
 127  
 128      $post_body = array(
 129          'translations' => wp_json_encode( $translations ),
 130      );
 131  
 132      if ( is_array( $extra_stats ) ) {
 133          $post_body = array_merge( $post_body, $extra_stats );
 134      }
 135  
 136      // Allow for WP_AUTO_UPDATE_CORE to specify beta/RC/development releases.
 137      if ( defined( 'WP_AUTO_UPDATE_CORE' )
 138          && in_array( WP_AUTO_UPDATE_CORE, array( 'beta', 'rc', 'development', 'branch-development' ), true )
 139      ) {
 140          $query['channel'] = WP_AUTO_UPDATE_CORE;
 141      }
 142  
 143      $url      = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, '', '&' );
 144      $http_url = $url;
 145      $ssl      = wp_http_supports( array( 'ssl' ) );
 146  
 147      if ( $ssl ) {
 148          $url = set_url_scheme( $url, 'https' );
 149      }
 150  
 151      $doing_cron = wp_doing_cron();
 152  
 153      $options = array(
 154          'timeout'    => $doing_cron ? 30 : 3,
 155          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 156          'headers'    => array(
 157              'wp_install' => $wp_install,
 158              'wp_blog'    => home_url( '/' ),
 159          ),
 160          'body'       => $post_body,
 161      );
 162  
 163      $response = wp_remote_post( $url, $options );
 164  
 165      if ( $ssl && is_wp_error( $response ) ) {
 166          trigger_error(
 167              sprintf(
 168                  /* translators: %s: Support forums URL. */
 169                  __( '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="%s">support forums</a>.' ),
 170                  __( 'https://wordpress.org/support/forums/' )
 171              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 172              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 173          );
 174          $response = wp_remote_post( $http_url, $options );
 175      }
 176  
 177      if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
 178          return;
 179      }
 180  
 181      $body = trim( wp_remote_retrieve_body( $response ) );
 182      $body = json_decode( $body, true );
 183  
 184      if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
 185          return;
 186      }
 187  
 188      $offers = $body['offers'];
 189  
 190      foreach ( $offers as &$offer ) {
 191          foreach ( $offer as $offer_key => $value ) {
 192              if ( 'packages' === $offer_key ) {
 193                  $offer['packages'] = (object) array_intersect_key(
 194                      array_map( 'esc_url', $offer['packages'] ),
 195                      array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
 196                  );
 197              } elseif ( 'download' === $offer_key ) {
 198                  $offer['download'] = esc_url( $value );
 199              } else {
 200                  $offer[ $offer_key ] = esc_html( $value );
 201              }
 202          }
 203          $offer = (object) array_intersect_key(
 204              $offer,
 205              array_fill_keys(
 206                  array(
 207                      'response',
 208                      'download',
 209                      'locale',
 210                      'packages',
 211                      'current',
 212                      'version',
 213                      'php_version',
 214                      'mysql_version',
 215                      'new_bundled',
 216                      'partial_version',
 217                      'notify_email',
 218                      'support_email',
 219                      'new_files',
 220                  ),
 221                  ''
 222              )
 223          );
 224      }
 225  
 226      $updates                  = new stdClass();
 227      $updates->updates         = $offers;
 228      $updates->last_checked    = time();
 229      $updates->version_checked = $wp_version;
 230  
 231      if ( isset( $body['translations'] ) ) {
 232          $updates->translations = $body['translations'];
 233      }
 234  
 235      set_site_transient( 'update_core', $updates );
 236  
 237      if ( ! empty( $body['ttl'] ) ) {
 238          $ttl = (int) $body['ttl'];
 239  
 240          if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
 241              // Queue an event to re-run the update check in $ttl seconds.
 242              wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
 243          }
 244      }
 245  
 246      // Trigger background updates if running non-interactively, and we weren't called from the update handler.
 247      if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
 248          /**
 249           * Fires during wp_cron, starting the auto-update process.
 250           *
 251           * @since 3.9.0
 252           */
 253          do_action( 'wp_maybe_auto_update' );
 254      }
 255  }
 256  
 257  /**
 258   * Checks for available updates to plugins based on the latest versions hosted on WordPress.org.
 259   *
 260   * Despite its name this function does not actually perform any updates, it only checks for available updates.
 261   *
 262   * A list of all plugins installed is sent to WP, along with the site locale.
 263   *
 264   * Checks against the WordPress server at api.wordpress.org. Will only check
 265   * if WordPress isn't installing.
 266   *
 267   * @since 2.3.0
 268   *
 269   * @global string $wp_version The WordPress version string.
 270   *
 271   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 272   */
 273  function wp_update_plugins( $extra_stats = array() ) {
 274      if ( wp_installing() ) {
 275          return;
 276      }
 277  
 278      // Include an unmodified $wp_version.
 279      require  ABSPATH . WPINC . '/version.php';
 280  
 281      // If running blog-side, bail unless we've not checked in the last 12 hours.
 282      if ( ! function_exists( 'get_plugins' ) ) {
 283          require_once ABSPATH . 'wp-admin/includes/plugin.php';
 284      }
 285  
 286      $plugins      = get_plugins();
 287      $translations = wp_get_installed_translations( 'plugins' );
 288  
 289      $active  = get_option( 'active_plugins', array() );
 290      $current = get_site_transient( 'update_plugins' );
 291  
 292      if ( ! is_object( $current ) ) {
 293          $current = new stdClass;
 294      }
 295  
 296      $updates               = new stdClass;
 297      $updates->last_checked = time();
 298      $updates->response     = array();
 299      $updates->translations = array();
 300      $updates->no_update    = array();
 301  
 302      $doing_cron = wp_doing_cron();
 303  
 304      // Check for update on a different schedule, depending on the page.
 305      switch ( current_filter() ) {
 306          case 'upgrader_process_complete':
 307              $timeout = 0;
 308              break;
 309          case 'load-update-core.php':
 310              $timeout = MINUTE_IN_SECONDS;
 311              break;
 312          case 'load-plugins.php':
 313          case 'load-update.php':
 314              $timeout = HOUR_IN_SECONDS;
 315              break;
 316          default:
 317              if ( $doing_cron ) {
 318                  $timeout = 2 * HOUR_IN_SECONDS;
 319              } else {
 320                  $timeout = 12 * HOUR_IN_SECONDS;
 321              }
 322      }
 323  
 324      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
 325  
 326      if ( $time_not_changed && ! $extra_stats ) {
 327          $plugin_changed = false;
 328  
 329          foreach ( $plugins as $file => $p ) {
 330              $updates->checked[ $file ] = $p['Version'];
 331  
 332              if ( ! isset( $current->checked[ $file ] ) || (string) $current->checked[ $file ] !== (string) $p['Version'] ) {
 333                  $plugin_changed = true;
 334              }
 335          }
 336  
 337          if ( isset( $current->response ) && is_array( $current->response ) ) {
 338              foreach ( $current->response as $plugin_file => $update_details ) {
 339                  if ( ! isset( $plugins[ $plugin_file ] ) ) {
 340                      $plugin_changed = true;
 341                      break;
 342                  }
 343              }
 344          }
 345  
 346          // Bail if we've checked recently and if nothing has changed.
 347          if ( ! $plugin_changed ) {
 348              return;
 349          }
 350      }
 351  
 352      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 353      $current->last_checked = time();
 354      set_site_transient( 'update_plugins', $current );
 355  
 356      $to_send = compact( 'plugins', 'active' );
 357  
 358      $locales = array_values( get_available_languages() );
 359  
 360      /**
 361       * Filters the locales requested for plugin translations.
 362       *
 363       * @since 3.7.0
 364       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 365       *
 366       * @param array $locales Plugin locales. Default is all available locales of the site.
 367       */
 368      $locales = apply_filters( 'plugins_update_check_locales', $locales );
 369      $locales = array_unique( $locales );
 370  
 371      if ( $doing_cron ) {
 372          $timeout = 30;
 373      } else {
 374          // Three seconds, plus one extra second for every 10 plugins.
 375          $timeout = 3 + (int) ( count( $plugins ) / 10 );
 376      }
 377  
 378      $options = array(
 379          'timeout'    => $timeout,
 380          'body'       => array(
 381              'plugins'      => wp_json_encode( $to_send ),
 382              'translations' => wp_json_encode( $translations ),
 383              'locale'       => wp_json_encode( $locales ),
 384              'all'          => wp_json_encode( true ),
 385          ),
 386          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 387      );
 388  
 389      if ( $extra_stats ) {
 390          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 391      }
 392  
 393      $url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
 394      $http_url = $url;
 395      $ssl      = wp_http_supports( array( 'ssl' ) );
 396  
 397      if ( $ssl ) {
 398          $url = set_url_scheme( $url, 'https' );
 399      }
 400  
 401      $raw_response = wp_remote_post( $url, $options );
 402  
 403      if ( $ssl && is_wp_error( $raw_response ) ) {
 404          trigger_error(
 405              sprintf(
 406                  /* translators: %s: Support forums URL. */
 407                  __( '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="%s">support forums</a>.' ),
 408                  __( 'https://wordpress.org/support/forums/' )
 409              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 410              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 411          );
 412          $raw_response = wp_remote_post( $http_url, $options );
 413      }
 414  
 415      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 416          return;
 417      }
 418  
 419      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 420  
 421      if ( $response && is_array( $response ) ) {
 422          $updates->response     = $response['plugins'];
 423          $updates->translations = $response['translations'];
 424          $updates->no_update    = $response['no_update'];
 425      }
 426  
 427      // Support updates for any plugins using the `Update URI` header field.
 428      foreach ( $plugins as $plugin_file => $plugin_data ) {
 429          if ( ! $plugin_data['UpdateURI'] || isset( $updates->response[ $plugin_file ] ) ) {
 430              continue;
 431          }
 432  
 433          $hostname = wp_parse_url( esc_url_raw( $plugin_data['UpdateURI'] ), PHP_URL_HOST );
 434  
 435          /**
 436           * Filters the update response for a given plugin hostname.
 437           *
 438           * The dynamic portion of the hook name, `$hostname`, refers to the hostname
 439           * of the URI specified in the `Update URI` header field.
 440           *
 441           * @since 5.8.0
 442           *
 443           * @param array|false $update {
 444           *     The plugin update data with the latest details. Default false.
 445           *
 446           *     @type string $id           Optional. ID of the plugin for update purposes, should be a URI
 447           *                                specified in the `Update URI` header field.
 448           *     @type string $slug         Slug of the plugin.
 449           *     @type string $version      The version of the plugin.
 450           *     @type string $url          The URL for details of the plugin.
 451           *     @type string $package      Optional. The update ZIP for the plugin.
 452           *     @type string $tested       Optional. The version of WordPress the plugin is tested against.
 453           *     @type string $requires_php Optional. The version of PHP which the plugin requires.
 454           *     @type bool   $autoupdate   Optional. Whether the plugin should automatically update.
 455           *     @type array  $icons        Optional. Array of plugin icons.
 456           *     @type array  $banners      Optional. Array of plugin banners.
 457           *     @type array  $banners_rtl  Optional. Array of plugin RTL banners.
 458           *     @type array  $translations {
 459           *         Optional. List of translation updates for the plugin.
 460           *
 461           *         @type string $language   The language the translation update is for.
 462           *         @type string $version    The version of the plugin this translation is for.
 463           *                                  This is not the version of the language file.
 464           *         @type string $updated    The update timestamp of the translation file.
 465           *                                  Should be a date in the `YYYY-MM-DD HH:MM:SS` format.
 466           *         @type string $package    The ZIP location containing the translation update.
 467           *         @type string $autoupdate Whether the translation should be automatically installed.
 468           *     }
 469           * }
 470           * @param array       $plugin_data      Plugin headers.
 471           * @param string      $plugin_file      Plugin filename.
 472           * @param array       $locales          Installed locales to look translations for.
 473           */
 474          $update = apply_filters( "update_plugins_{$hostname}", false, $plugin_data, $plugin_file, $locales );
 475  
 476          if ( ! $update ) {
 477              continue;
 478          }
 479  
 480          $update = (object) $update;
 481  
 482          // Is it valid? We require at least a version.
 483          if ( ! isset( $update->version ) ) {
 484              continue;
 485          }
 486  
 487          // These should remain constant.
 488          $update->id     = $plugin_data['UpdateURI'];
 489          $update->plugin = $plugin_file;
 490  
 491          // WordPress needs the version field specified as 'new_version'.
 492          if ( ! isset( $update->new_version ) ) {
 493              $update->new_version = $update->version;
 494          }
 495  
 496          // Handle any translation updates.
 497          if ( ! empty( $update->translations ) ) {
 498              foreach ( $update->translations as $translation ) {
 499                  if ( isset( $translation['language'], $translation['package'] ) ) {
 500                      $translation['type'] = 'plugin';
 501                      $translation['slug'] = isset( $update->slug ) ? $update->slug : $update->id;
 502  
 503                      $updates->translations[] = $translation;
 504                  }
 505              }
 506          }
 507  
 508          unset( $updates->no_update[ $plugin_file ], $updates->response[ $plugin_file ] );
 509  
 510          if ( version_compare( $update->new_version, $plugin_data['Version'], '>' ) ) {
 511              $updates->response[ $plugin_file ] = $update;
 512          } else {
 513              $updates->no_update[ $plugin_file ] = $update;
 514          }
 515      }
 516  
 517      $sanitize_plugin_update_payload = static function( &$item ) {
 518          $item = (object) $item;
 519  
 520          unset( $item->translations, $item->compatibility );
 521  
 522          return $item;
 523      };
 524  
 525      array_walk( $updates->response, $sanitize_plugin_update_payload );
 526      array_walk( $updates->no_update, $sanitize_plugin_update_payload );
 527  
 528      set_site_transient( 'update_plugins', $updates );
 529  }
 530  
 531  /**
 532   * Checks for available updates to themes based on the latest versions hosted on WordPress.org.
 533   *
 534   * Despite its name this function does not actually perform any updates, it only checks for available updates.
 535   *
 536   * A list of all themes installed is sent to WP, along with the site locale.
 537   *
 538   * Checks against the WordPress server at api.wordpress.org. Will only check
 539   * if WordPress isn't installing.
 540   *
 541   * @since 2.7.0
 542   *
 543   * @global string $wp_version The WordPress version string.
 544   *
 545   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 546   */
 547  function wp_update_themes( $extra_stats = array() ) {
 548      if ( wp_installing() ) {
 549          return;
 550      }
 551  
 552      // Include an unmodified $wp_version.
 553      require  ABSPATH . WPINC . '/version.php';
 554  
 555      $installed_themes = wp_get_themes();
 556      $translations     = wp_get_installed_translations( 'themes' );
 557  
 558      $last_update = get_site_transient( 'update_themes' );
 559  
 560      if ( ! is_object( $last_update ) ) {
 561          $last_update = new stdClass;
 562      }
 563  
 564      $themes  = array();
 565      $checked = array();
 566      $request = array();
 567  
 568      // Put slug of active theme into request.
 569      $request['active'] = get_option( 'stylesheet' );
 570  
 571      foreach ( $installed_themes as $theme ) {
 572          $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
 573  
 574          $themes[ $theme->get_stylesheet() ] = array(
 575              'Name'       => $theme->get( 'Name' ),
 576              'Title'      => $theme->get( 'Name' ),
 577              'Version'    => $theme->get( 'Version' ),
 578              'Author'     => $theme->get( 'Author' ),
 579              'Author URI' => $theme->get( 'AuthorURI' ),
 580              'Template'   => $theme->get_template(),
 581              'Stylesheet' => $theme->get_stylesheet(),
 582          );
 583      }
 584  
 585      $doing_cron = wp_doing_cron();
 586  
 587      // Check for update on a different schedule, depending on the page.
 588      switch ( current_filter() ) {
 589          case 'upgrader_process_complete':
 590              $timeout = 0;
 591              break;
 592          case 'load-update-core.php':
 593              $timeout = MINUTE_IN_SECONDS;
 594              break;
 595          case 'load-themes.php':
 596          case 'load-update.php':
 597              $timeout = HOUR_IN_SECONDS;
 598              break;
 599          default:
 600              if ( $doing_cron ) {
 601                  $timeout = 2 * HOUR_IN_SECONDS;
 602              } else {
 603                  $timeout = 12 * HOUR_IN_SECONDS;
 604              }
 605      }
 606  
 607      $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
 608  
 609      if ( $time_not_changed && ! $extra_stats ) {
 610          $theme_changed = false;
 611  
 612          foreach ( $checked as $slug => $v ) {
 613              if ( ! isset( $last_update->checked[ $slug ] ) || (string) $last_update->checked[ $slug ] !== (string) $v ) {
 614                  $theme_changed = true;
 615              }
 616          }
 617  
 618          if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
 619              foreach ( $last_update->response as $slug => $update_details ) {
 620                  if ( ! isset( $checked[ $slug ] ) ) {
 621                      $theme_changed = true;
 622                      break;
 623                  }
 624              }
 625          }
 626  
 627          // Bail if we've checked recently and if nothing has changed.
 628          if ( ! $theme_changed ) {
 629              return;
 630          }
 631      }
 632  
 633      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 634      $last_update->last_checked = time();
 635      set_site_transient( 'update_themes', $last_update );
 636  
 637      $request['themes'] = $themes;
 638  
 639      $locales = array_values( get_available_languages() );
 640  
 641      /**
 642       * Filters the locales requested for theme translations.
 643       *
 644       * @since 3.7.0
 645       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 646       *
 647       * @param array $locales Theme locales. Default is all available locales of the site.
 648       */
 649      $locales = apply_filters( 'themes_update_check_locales', $locales );
 650      $locales = array_unique( $locales );
 651  
 652      if ( $doing_cron ) {
 653          $timeout = 30;
 654      } else {
 655          // Three seconds, plus one extra second for every 10 themes.
 656          $timeout = 3 + (int) ( count( $themes ) / 10 );
 657      }
 658  
 659      $options = array(
 660          'timeout'    => $timeout,
 661          'body'       => array(
 662              'themes'       => wp_json_encode( $request ),
 663              'translations' => wp_json_encode( $translations ),
 664              'locale'       => wp_json_encode( $locales ),
 665          ),
 666          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 667      );
 668  
 669      if ( $extra_stats ) {
 670          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 671      }
 672  
 673      $url      = 'http://api.wordpress.org/themes/update-check/1.1/';
 674      $http_url = $url;
 675      $ssl      = wp_http_supports( array( 'ssl' ) );
 676  
 677      if ( $ssl ) {
 678          $url = set_url_scheme( $url, 'https' );
 679      }
 680  
 681      $raw_response = wp_remote_post( $url, $options );
 682  
 683      if ( $ssl && is_wp_error( $raw_response ) ) {
 684          trigger_error(
 685              sprintf(
 686                  /* translators: %s: Support forums URL. */
 687                  __( '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="%s">support forums</a>.' ),
 688                  __( 'https://wordpress.org/support/forums/' )
 689              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 690              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 691          );
 692          $raw_response = wp_remote_post( $http_url, $options );
 693      }
 694  
 695      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 696          return;
 697      }
 698  
 699      $new_update               = new stdClass;
 700      $new_update->last_checked = time();
 701      $new_update->checked      = $checked;
 702  
 703      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 704  
 705      if ( is_array( $response ) ) {
 706          $new_update->response     = $response['themes'];
 707          $new_update->no_update    = $response['no_update'];
 708          $new_update->translations = $response['translations'];
 709      }
 710  
 711      set_site_transient( 'update_themes', $new_update );
 712  }
 713  
 714  /**
 715   * Performs WordPress automatic background updates.
 716   *
 717   * Updates WordPress core plus any plugins and themes that have automatic updates enabled.
 718   *
 719   * @since 3.7.0
 720   */
 721  function wp_maybe_auto_update() {
 722      include_once  ABSPATH . 'wp-admin/includes/admin.php';
 723      require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
 724  
 725      $upgrader = new WP_Automatic_Updater;
 726      $upgrader->run();
 727  }
 728  
 729  /**
 730   * Retrieves a list of all language updates available.
 731   *
 732   * @since 3.7.0
 733   *
 734   * @return object[] Array of translation objects that have available updates.
 735   */
 736  function wp_get_translation_updates() {
 737      $updates    = array();
 738      $transients = array(
 739          'update_core'    => 'core',
 740          'update_plugins' => 'plugin',
 741          'update_themes'  => 'theme',
 742      );
 743  
 744      foreach ( $transients as $transient => $type ) {
 745          $transient = get_site_transient( $transient );
 746  
 747          if ( empty( $transient->translations ) ) {
 748              continue;
 749          }
 750  
 751          foreach ( $transient->translations as $translation ) {
 752              $updates[] = (object) $translation;
 753          }
 754      }
 755  
 756      return $updates;
 757  }
 758  
 759  /**
 760   * Collect counts and UI strings for available updates
 761   *
 762   * @since 3.3.0
 763   *
 764   * @return array
 765   */
 766  function wp_get_update_data() {
 767      $counts = array(
 768          'plugins'      => 0,
 769          'themes'       => 0,
 770          'wordpress'    => 0,
 771          'translations' => 0,
 772      );
 773  
 774      $plugins = current_user_can( 'update_plugins' );
 775  
 776      if ( $plugins ) {
 777          $update_plugins = get_site_transient( 'update_plugins' );
 778  
 779          if ( ! empty( $update_plugins->response ) ) {
 780              $counts['plugins'] = count( $update_plugins->response );
 781          }
 782      }
 783  
 784      $themes = current_user_can( 'update_themes' );
 785  
 786      if ( $themes ) {
 787          $update_themes = get_site_transient( 'update_themes' );
 788  
 789          if ( ! empty( $update_themes->response ) ) {
 790              $counts['themes'] = count( $update_themes->response );
 791          }
 792      }
 793  
 794      $core = current_user_can( 'update_core' );
 795  
 796      if ( $core && function_exists( 'get_core_updates' ) ) {
 797          $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
 798  
 799          if ( ! empty( $update_wordpress )
 800              && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
 801              && current_user_can( 'update_core' )
 802          ) {
 803              $counts['wordpress'] = 1;
 804          }
 805      }
 806  
 807      if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
 808          $counts['translations'] = 1;
 809      }
 810  
 811      $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
 812      $titles          = array();
 813  
 814      if ( $counts['wordpress'] ) {
 815          /* translators: %d: Number of available WordPress updates. */
 816          $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
 817      }
 818  
 819      if ( $counts['plugins'] ) {
 820          /* translators: %d: Number of available plugin updates. */
 821          $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
 822      }
 823  
 824      if ( $counts['themes'] ) {
 825          /* translators: %d: Number of available theme updates. */
 826          $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
 827      }
 828  
 829      if ( $counts['translations'] ) {
 830          $titles['translations'] = __( 'Translation Updates' );
 831      }
 832  
 833      $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
 834  
 835      $update_data = array(
 836          'counts' => $counts,
 837          'title'  => $update_title,
 838      );
 839      /**
 840       * Filters the returned array of update data for plugins, themes, and WordPress core.
 841       *
 842       * @since 3.5.0
 843       *
 844       * @param array $update_data {
 845       *     Fetched update data.
 846       *
 847       *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
 848       *     @type string  $update_title Titles of available updates.
 849       * }
 850       * @param array $titles An array of update counts and UI strings for available updates.
 851       */
 852      return apply_filters( 'wp_get_update_data', $update_data, $titles );
 853  }
 854  
 855  /**
 856   * Determines whether core should be updated.
 857   *
 858   * @since 2.8.0
 859   *
 860   * @global string $wp_version The WordPress version string.
 861   */
 862  function _maybe_update_core() {
 863      // Include an unmodified $wp_version.
 864      require  ABSPATH . WPINC . '/version.php';
 865  
 866      $current = get_site_transient( 'update_core' );
 867  
 868      if ( isset( $current->last_checked, $current->version_checked )
 869          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 870          && $current->version_checked === $wp_version
 871      ) {
 872          return;
 873      }
 874  
 875      wp_version_check();
 876  }
 877  /**
 878   * Check the last time plugins were run before checking plugin versions.
 879   *
 880   * This might have been backported to WordPress 2.6.1 for performance reasons.
 881   * This is used for the wp-admin to check only so often instead of every page
 882   * load.
 883   *
 884   * @since 2.7.0
 885   * @access private
 886   */
 887  function _maybe_update_plugins() {
 888      $current = get_site_transient( 'update_plugins' );
 889  
 890      if ( isset( $current->last_checked )
 891          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 892      ) {
 893          return;
 894      }
 895  
 896      wp_update_plugins();
 897  }
 898  
 899  /**
 900   * Check themes versions only after a duration of time.
 901   *
 902   * This is for performance reasons to make sure that on the theme version
 903   * checker is not run on every page load.
 904   *
 905   * @since 2.7.0
 906   * @access private
 907   */
 908  function _maybe_update_themes() {
 909      $current = get_site_transient( 'update_themes' );
 910  
 911      if ( isset( $current->last_checked )
 912          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 913      ) {
 914          return;
 915      }
 916  
 917      wp_update_themes();
 918  }
 919  
 920  /**
 921   * Schedule core, theme, and plugin update checks.
 922   *
 923   * @since 3.1.0
 924   */
 925  function wp_schedule_update_checks() {
 926      if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
 927          wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
 928      }
 929  
 930      if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
 931          wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
 932      }
 933  
 934      if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
 935          wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
 936      }
 937  }
 938  
 939  /**
 940   * Clear existing update caches for plugins, themes, and core.
 941   *
 942   * @since 4.1.0
 943   */
 944  function wp_clean_update_cache() {
 945      if ( function_exists( 'wp_clean_plugins_cache' ) ) {
 946          wp_clean_plugins_cache();
 947      } else {
 948          delete_site_transient( 'update_plugins' );
 949      }
 950  
 951      wp_clean_themes_cache();
 952  
 953      delete_site_transient( 'update_core' );
 954  }
 955  
 956  if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
 957      return;
 958  }
 959  
 960  add_action( 'admin_init', '_maybe_update_core' );
 961  add_action( 'wp_version_check', 'wp_version_check' );
 962  
 963  add_action( 'load-plugins.php', 'wp_update_plugins' );
 964  add_action( 'load-update.php', 'wp_update_plugins' );
 965  add_action( 'load-update-core.php', 'wp_update_plugins' );
 966  add_action( 'admin_init', '_maybe_update_plugins' );
 967  add_action( 'wp_update_plugins', 'wp_update_plugins' );
 968  
 969  add_action( 'load-themes.php', 'wp_update_themes' );
 970  add_action( 'load-update.php', 'wp_update_themes' );
 971  add_action( 'load-update-core.php', 'wp_update_themes' );
 972  add_action( 'admin_init', '_maybe_update_themes' );
 973  add_action( 'wp_update_themes', 'wp_update_themes' );
 974  
 975  add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
 976  
 977  add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
 978  
 979  add_action( 'init', 'wp_schedule_update_checks' );


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1