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. 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') || 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 );
72 add_action( 'init', 'wp_version_check' );
75 * wp_update_plugins() - Check plugin versions against the latest versions hosted on WordPress.org.
77 * The WordPress version, PHP version, and Locale is sent along with a list of all plugins installed.
78 * Checks against the WordPress server at api.wordpress.org.
79 * Will only check if PHP has fsockopen enabled and WordPress isn't installing.
83 * @uses $wp_version Used to notidy the WordPress version.
85 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
87 function wp_update_plugins() {
90 if ( !function_exists('fsockopen') || defined('WP_INSTALLING') )
93 // If running blog-side, bail unless we've not checked in the last 12 hours
94 if ( !function_exists( 'get_plugins' ) )
95 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
97 $plugins = get_plugins();
98 $active = get_option( 'active_plugins' );
99 $current = get_option( 'update_plugins' );
102 $new_option->last_checked = time();
103 $time_not_changed = isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked );
105 $plugin_changed = false;
106 foreach ( $plugins as $file => $p ) {
107 $new_option->checked[ $file ] = $p['Version'];
109 if ( !isset( $current->checked[ $file ] ) ) {
110 $plugin_changed = true;
114 if ( strval($current->checked[ $file ]) !== strval($p['Version']) )
115 $plugin_changed = true;
118 foreach ( (array) $current->response as $plugin_file => $update_details ) {
119 if ( ! isset($plugins[ $plugin_file ]) ) {
120 $plugin_changed = true;
124 // Bail if we've checked in the last 12 hours and if nothing has changed
125 if ( $time_not_changed && !$plugin_changed )
128 $to_send->plugins = $plugins;
129 $to_send->active = $active;
130 $send = serialize( $to_send );
132 $request = 'plugins=' . urlencode( $send );
133 $http_request = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
134 $http_request .= "Host: api.wordpress.org\r\n";
135 $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
136 $http_request .= "Content-Length: " . strlen($request) . "\r\n";
137 $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
138 $http_request .= "\r\n";
139 $http_request .= $request;
142 if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
143 fwrite($fs, $http_request);
146 $response .= fgets($fs, 1160); // One TCP-IP packet
148 $response = explode("\r\n\r\n", $response, 2);
151 $response = unserialize( $response[1] );
154 $new_option->response = $response;
156 update_option( 'update_plugins', $new_option );
159 function _maybe_update_plugins() {
160 $current = get_option( 'update_plugins' );
161 if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
166 add_action( 'load-plugins.php', 'wp_update_plugins' );
167 add_action( 'admin_init', '_maybe_update_plugins' );
168 add_action( 'wp_update_plugins', 'wp_update_plugins' );
170 if ( !wp_next_scheduled('wp_update_plugins') )
171 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');