wp_version_check

Check WordPress version against the newest version.

Description

wp_version_check( (array) $extra_stats = array(), (bool) $force_check = false ); 

The WordPress version, PHP version, and Locale is sent. Checks against the WordPress server at api.wordpress.org server. Will only check if WordPress isn't installing.

Parameters (2)

0. $extra_stats — Optional. (array) => array()
Extra statistics to report to the WordPress.org API.
1. $force_check — Optional. (bool) => false
Whether to bypass the transient cache and force a fresh update check. Defaults to false, true if $extra_stats is set.

Usage

  1. if ( !function_exists( 'wp_version_check' ) ) { 
  2. require_once ABSPATH . WPINC . '/update.php'; 
  3.  
  4. // Extra statistics to report to the WordPress.org API. 
  5. $extra_stats = array(); 
  6.  
  7. // Whether to bypass the transient cache and force a fresh update check. Defaults to false, true if $extra_stats is set. 
  8. $force_check = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_version_check($extra_stats, $force_check); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/update.php  
  1. function wp_version_check( $extra_stats = array(), $force_check = false ) { 
  2. if ( wp_installing() ) { 
  3. return; 
  4.  
  5. global $wpdb, $wp_local_package; 
  6. // include an unmodified $wp_version 
  7. include( ABSPATH . WPINC . '/version.php' ); 
  8. $php_version = phpversion(); 
  9.  
  10. $current = get_site_transient( 'update_core' ); 
  11. $translations = wp_get_installed_translations( 'core' ); 
  12.  
  13. // Invalidate the transient when $wp_version changes 
  14. if ( is_object( $current ) && $wp_version != $current->version_checked ) 
  15. $current = false; 
  16.  
  17. if ( ! is_object($current) ) { 
  18. $current = new stdClass; 
  19. $current->updates = array(); 
  20. $current->version_checked = $wp_version; 
  21.  
  22. if ( ! empty( $extra_stats ) ) 
  23. $force_check = true; 
  24.  
  25. // Wait 60 seconds between multiple version check requests 
  26. $timeout = 60; 
  27. $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked ); 
  28. if ( ! $force_check && $time_not_changed ) { 
  29. return; 
  30.  
  31. /** 
  32. * Filters the locale requested for WordPress core translations. 
  33. * @since 2.8.0 
  34. * @param string $locale Current locale. 
  35. */ 
  36.  
  37. // Update last_checked for current to prevent multiple blockingrequestsif request hangs 
  38. $current->last_checked = time(); 
  39. set_site_transient( 'update_core', $current ); 
  40.  
  41. if ( method_exists( $wpdb, 'db_version' ) ) 
  42. $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version()); 
  43. else 
  44. $mysql_version = 'N/A'; 
  45.  
  46. if ( is_multisite() ) { 
  47. $user_count = get_user_count(); 
  48. $num_blogs = get_blog_count(); 
  49. $wp_install = network_site_url(); 
  50. $multisite_enabled = 1; 
  51. } else { 
  52. $user_count = count_users(); 
  53. $user_count = $user_count['total_users']; 
  54. $multisite_enabled = 0; 
  55. $num_blogs = 1; 
  56. $wp_install = home_url( '/' ); 
  57.  
  58. $query = array( 
  59. 'version' => $wp_version,  
  60. 'php' => $php_version,  
  61. 'locale' => $locale,  
  62. 'mysql' => $mysql_version,  
  63. 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',  
  64. 'blogs' => $num_blogs,  
  65. 'users' => $user_count,  
  66. 'multisite_enabled' => $multisite_enabled,  
  67. 'initial_db_version' => get_site_option( 'initial_db_version' ),  
  68. ); 
  69.  
  70. $post_body = array( 
  71. translations => wp_json_encode( $translations ),  
  72. ); 
  73.  
  74. if ( is_array( $extra_stats ) ) 
  75. $post_body = array_merge( $post_body, $extra_stats ); 
  76.  
  77. $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' ); 
  78. if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) 
  79. $url = set_url_scheme( $url, 'https' ); 
  80.  
  81. $options = array( 
  82. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),  
  83. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),  
  84. 'headers' => array( 
  85. 'wp_install' => $wp_install,  
  86. 'wp_blog' => home_url( '/' ) 
  87. ),  
  88. 'body' => $post_body,  
  89. ); 
  90.  
  91. $response = wp_remote_post( $url, $options ); 
  92. if ( $ssl && is_wp_error( $response ) ) { 
  93. trigger_error( 
  94. sprintf( 
  95. /** translators: %s: support forums URL */ 
  96. __( 'An unexpectederroroccurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),  
  97. __( 'https://wordpress.org/support/' ) 
  98. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),  
  99. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE 
  100. ); 
  101. $response = wp_remote_post( $http_url, $options ); 
  102.  
  103. if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { 
  104. return; 
  105.  
  106. $body = trim( wp_remote_retrieve_body( $response ) ); 
  107. $body = json_decode( $body, true ); 
  108.  
  109. if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) { 
  110. return; 
  111.  
  112. $offers = $body['offers']; 
  113.  
  114. foreach ( $offers as &$offer ) { 
  115. foreach ( $offer as $offer_key => $value ) { 
  116. if ( 'packages' == $offer_key ) 
  117. $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),  
  118. array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) ); 
  119. elseif ( 'download' == $offer_key ) 
  120. $offer['download'] = esc_url( $value ); 
  121. else 
  122. $offer[ $offer_key ] = esc_html( $value ); 
  123. $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',  
  124. 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email', 'support_email', 'new_files' ), '' ) ); 
  125.  
  126. $updates = new stdClass(); 
  127. $updates->updates = $offers; 
  128. $updates->last_checked = time(); 
  129. $updates->version_checked = $wp_version; 
  130.  
  131. if ( isset( $body[translations] ) ) 
  132. $updates->translations = $body[translations]; 
  133.  
  134. set_site_transient( 'update_core', $updates ); 
  135.  
  136. if ( ! empty( $body['ttl'] ) ) { 
  137. $ttl = (int) $body['ttl']; 
  138. if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) { 
  139. // Queue an event to re-run the update check in $ttl seconds. 
  140. wp_schedule_single_event( time() + $ttl, 'wp_version_check' ); 
  141.  
  142. // Trigger background updates if running non-interactively, and we weren't called from the update handler. 
  143. if ( defined( 'DOING_CRON' ) && DOING_CRON && ! doing_action( 'wp_maybe_auto_update' ) ) {