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


Generated: Wed Aug 12 01:00:03 2020 Cross-referenced by PHPXref 0.7.1