3 * A simple set of functions to check our version 1.0 update service
10 * wp_version_check() - Check WordPress version against the newest version.
12 * The WordPress version, PHP version, and Locale is sent. Checks against the WordPress server at
13 * api.wordpress.org server. Will only check if PHP has fsockopen enabled and WordPress isn't installing.
17 * @uses $wp_version Used to check against the newest WordPress version.
19 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
21 function wp_version_check() {
22 if ( !function_exists('fsockopen') || strpos($_SERVER['PHP_SELF'], 'install.php') !== false || defined('WP_INSTALLING') )
26 $php_version = phpversion();
28 $current = get_option( 'update_core' );
29 $locale = get_locale();
32 isset( $current->last_checked ) &&
33 43200 > ( time() - $current->last_checked ) &&
34 $current->version_checked == $wp_version
39 $new_option->last_checked = time(); // this gets set whether we get a response or not, so if something is down or misconfigured it won't delay the page load for more than 3 seconds, twice a day
40 $new_option->version_checked = $wp_version;
42 $http_request = "GET /core/version-check/1.1/?version=$wp_version&php=$php_version&locale=$locale HTTP/1.0\r\n";
43 $http_request .= "Host: api.wordpress.org\r\n";
44 $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
45 $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
46 $http_request .= "\r\n";
49 if ( false !== ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3 ) ) && is_resource($fs) ) {
50 fwrite( $fs, $http_request );
51 while ( !feof( $fs ) )
52 $response .= fgets( $fs, 1160 ); // One TCP-IP packet
55 $response = explode("\r\n\r\n", $response, 2);
56 if ( !preg_match( '|HTTP/.*? 200|', $response[0] ) )
59 $body = trim( $response[1] );
60 $body = str_replace(array("\r\n", "\r"), "\n", $body);
62 $returns = explode("\n", $body);
64 $new_option->response = attribute_escape( $returns[0] );
65 if ( isset( $returns[1] ) )
66 $new_option->url = clean_url( $returns[1] );
67 if ( isset( $returns[2] ) )
68 $new_option->current = attribute_escape( $returns[2] );
70 update_option( 'update_core', $new_option );
73 add_action( 'init', 'wp_version_check' );