web/wp-includes/update.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     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. Checks against the
       
    13  * WordPress server at api.wordpress.org server. Will only check if WordPress
       
    14  * isn't installing.
       
    15  *
       
    16  * @package WordPress
       
    17  * @since 2.3.0
       
    18  * @uses $wp_version Used to check against the newest WordPress version.
       
    19  *
       
    20  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
       
    21  */
       
    22 function wp_version_check() {
       
    23 	if ( defined('WP_INSTALLING') )
       
    24 		return;
       
    25 
       
    26 	global $wp_version, $wpdb, $wp_local_package;
       
    27 	$php_version = phpversion();
       
    28 
       
    29 	$current = get_transient( 'update_core' );
       
    30 	if ( ! is_object($current) ) {
       
    31 		$current = new stdClass;
       
    32 		$current->updates = array();
       
    33 		$current->version_checked = $wp_version;
       
    34 	}
       
    35 
       
    36 	$locale = apply_filters( 'core_version_check_locale', get_locale() );
       
    37 
       
    38 	// Update last_checked for current to prevent multiple blocking requests if request hangs
       
    39 	$current->last_checked = time();
       
    40 	set_transient( 'update_core', $current );
       
    41 
       
    42 	if ( method_exists( $wpdb, 'db_version' ) )
       
    43 		$mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version($wpdb->users));
       
    44 	else
       
    45 		$mysql_version = 'N/A';
       
    46 	$local_package = isset( $wp_local_package )? $wp_local_package : '';
       
    47 	$url = "http://api.wordpress.org/core/version-check/1.3/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package";
       
    48 
       
    49 	$options = array(
       
    50 		'timeout' => 3,
       
    51 		'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
       
    52 	);
       
    53 
       
    54 	$response = wp_remote_get($url, $options);
       
    55 
       
    56 	if ( is_wp_error( $response ) )
       
    57 		return false;
       
    58 
       
    59 	if ( 200 != $response['response']['code'] )
       
    60 		return false;
       
    61 
       
    62 	$body = trim( $response['body'] );
       
    63 	$body = str_replace(array("\r\n", "\r"), "\n", $body);
       
    64 	$new_options = array();
       
    65 	foreach( explode( "\n\n", $body ) as $entry) {
       
    66 		$returns = explode("\n", $entry);
       
    67 		$new_option = new stdClass();
       
    68 		$new_option->response = esc_attr( $returns[0] );
       
    69 		if ( isset( $returns[1] ) )
       
    70 			$new_option->url = esc_url( $returns[1] );
       
    71 		if ( isset( $returns[2] ) )
       
    72 			$new_option->package = esc_url( $returns[2] );
       
    73 		if ( isset( $returns[3] ) )
       
    74 			$new_option->current = esc_attr( $returns[3] );
       
    75 		if ( isset( $returns[4] ) )
       
    76 			$new_option->locale = esc_attr( $returns[4] );
       
    77 		$new_options[] = $new_option;
       
    78 	}
       
    79 
       
    80 	$updates = new stdClass();
       
    81 	$updates->updates = $new_options;
       
    82 	$updates->last_checked = time();
       
    83 	$updates->version_checked = $wp_version;
       
    84 	set_transient( 'update_core',  $updates);
       
    85 }
       
    86 
       
    87 /**
       
    88  * Check plugin versions against the latest versions hosted on WordPress.org.
       
    89  *
       
    90  * The WordPress version, PHP version, and Locale is sent along with a list of
       
    91  * all plugins installed. Checks against the WordPress server at
       
    92  * api.wordpress.org. Will only check if WordPress isn't installing.
       
    93  *
       
    94  * @package WordPress
       
    95  * @since 2.3.0
       
    96  * @uses $wp_version Used to notidy the WordPress version.
       
    97  *
       
    98  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
       
    99  */
       
   100 function wp_update_plugins() {
       
   101 	global $wp_version;
       
   102 
       
   103 	if ( defined('WP_INSTALLING') )
       
   104 		return false;
       
   105 
       
   106 	// If running blog-side, bail unless we've not checked in the last 12 hours
       
   107 	if ( !function_exists( 'get_plugins' ) )
       
   108 		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
       
   109 
       
   110 	$plugins = get_plugins();
       
   111 	$active  = get_option( 'active_plugins' );
       
   112 	$current = get_transient( 'update_plugins' );
       
   113 	if ( ! is_object($current) )
       
   114 		$current = new stdClass;
       
   115 
       
   116 	$new_option = new stdClass;
       
   117 	$new_option->last_checked = time();
       
   118 	$timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
       
   119 	$time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
       
   120 
       
   121 	$plugin_changed = false;
       
   122 	foreach ( $plugins as $file => $p ) {
       
   123 		$new_option->checked[ $file ] = $p['Version'];
       
   124 
       
   125 		if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
       
   126 			$plugin_changed = true;
       
   127 	}
       
   128 
       
   129 	if ( isset ( $current->response ) && is_array( $current->response ) ) {
       
   130 		foreach ( $current->response as $plugin_file => $update_details ) {
       
   131 			if ( ! isset($plugins[ $plugin_file ]) ) {
       
   132 				$plugin_changed = true;
       
   133 				break;
       
   134 			}
       
   135 		}
       
   136 	}
       
   137 
       
   138 	// Bail if we've checked in the last 12 hours and if nothing has changed
       
   139 	if ( $time_not_changed && !$plugin_changed )
       
   140 		return false;
       
   141 
       
   142 	// Update last_checked for current to prevent multiple blocking requests if request hangs
       
   143 	$current->last_checked = time();
       
   144 	set_transient( 'update_plugins', $current );
       
   145 
       
   146 	$to_send = (object)compact('plugins', 'active');
       
   147 
       
   148 	$options = array(
       
   149 		'timeout' => 3,
       
   150 		'body' => array( 'plugins' => serialize( $to_send ) ),
       
   151 		'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
       
   152 	);
       
   153 
       
   154 	$raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
       
   155 
       
   156 	if ( is_wp_error( $raw_response ) )
       
   157 		return false;
       
   158 
       
   159 	if( 200 != $raw_response['response']['code'] )
       
   160 		return false;
       
   161 
       
   162 	$response = unserialize( $raw_response['body'] );
       
   163 
       
   164 	if ( false !== $response )
       
   165 		$new_option->response = $response;
       
   166 	else
       
   167 		$new_option->response = array();
       
   168 
       
   169 	set_transient( 'update_plugins', $new_option );
       
   170 }
       
   171 
       
   172 /**
       
   173  * Check theme versions against the latest versions hosted on WordPress.org.
       
   174  *
       
   175  * A list of all themes installed in sent to WP. Checks against the
       
   176  * WordPress server at api.wordpress.org. Will only check if WordPress isn't
       
   177  * installing.
       
   178  *
       
   179  * @package WordPress
       
   180  * @since 2.7.0
       
   181  * @uses $wp_version Used to notidy the WordPress version.
       
   182  *
       
   183  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
       
   184  */
       
   185 function wp_update_themes( ) {
       
   186 	global $wp_version;
       
   187 
       
   188 	if( defined( 'WP_INSTALLING' ) )
       
   189 		return false;
       
   190 
       
   191 	if( !function_exists( 'get_themes' ) )
       
   192 		require_once( ABSPATH . 'wp-includes/theme.php' );
       
   193 
       
   194 	$installed_themes = get_themes( );
       
   195 	$current_theme = get_transient( 'update_themes' );
       
   196 	if ( ! is_object($current_theme) )
       
   197 		$current_theme = new stdClass;
       
   198 
       
   199 	$new_option = new stdClass;
       
   200 	$new_option->last_checked = time( );
       
   201 	$timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
       
   202 	$time_not_changed = isset( $current_theme->last_checked ) && $timeout > ( time( ) - $current_theme->last_checked );
       
   203 
       
   204 	if( $time_not_changed )
       
   205 		return false;
       
   206 
       
   207 	// Update last_checked for current to prevent multiple blocking requests if request hangs
       
   208 	$current_theme->last_checked = time();
       
   209 	set_transient( 'update_themes', $current_theme );
       
   210 
       
   211 	$current_theme->template = get_option( 'template' );
       
   212 
       
   213 	$themes = array( );
       
   214 	$themes['current_theme'] = (array) $current_theme;
       
   215 	foreach( (array) $installed_themes as $theme_title => $theme ) {
       
   216 		$themes[$theme['Stylesheet']] = array( );
       
   217 
       
   218 		foreach( (array) $theme as $key => $value ) {
       
   219 			$themes[$theme['Stylesheet']][$key] = $value;
       
   220 		}
       
   221 	}
       
   222 
       
   223 	$options = array(
       
   224 		'timeout'		=> 3,
       
   225 		'body'			=> array( 'themes' => serialize( $themes ) ),
       
   226 		'user-agent'	=> 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
       
   227 	);
       
   228 
       
   229 	$raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
       
   230 
       
   231 	if( is_wp_error( $raw_response ) )
       
   232 		return false;
       
   233 
       
   234 	if( 200 != $raw_response['response']['code'] )
       
   235 		return false;
       
   236 
       
   237 	$response = unserialize( $raw_response['body'] );
       
   238 	if( $response )
       
   239 		$new_option->response = $response;
       
   240 
       
   241 	set_transient( 'update_themes', $new_option );
       
   242 }
       
   243 
       
   244 function _maybe_update_core() {
       
   245 	global $wp_version;
       
   246 
       
   247 	$current = get_transient( 'update_core' );
       
   248 
       
   249 	if ( isset( $current->last_checked ) &&
       
   250 		43200 > ( time() - $current->last_checked ) &&
       
   251 		isset( $current->version_checked ) &&
       
   252 		$current->version_checked == $wp_version )
       
   253 		return;
       
   254 
       
   255 	wp_version_check();
       
   256 }
       
   257 /**
       
   258  * Check the last time plugins were run before checking plugin versions.
       
   259  *
       
   260  * This might have been backported to WordPress 2.6.1 for performance reasons.
       
   261  * This is used for the wp-admin to check only so often instead of every page
       
   262  * load.
       
   263  *
       
   264  * @since 2.7.0
       
   265  * @access private
       
   266  */
       
   267 function _maybe_update_plugins() {
       
   268 	$current = get_transient( 'update_plugins' );
       
   269 	if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
       
   270 		return;
       
   271 	wp_update_plugins();
       
   272 }
       
   273 
       
   274 /**
       
   275  * Check themes versions only after a duration of time.
       
   276  *
       
   277  * This is for performance reasons to make sure that on the theme version
       
   278  * checker is not run on every page load.
       
   279  *
       
   280  * @since 2.7.0
       
   281  * @access private
       
   282  */
       
   283 function _maybe_update_themes( ) {
       
   284 	$current = get_transient( 'update_themes' );
       
   285 	if( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
       
   286 		return;
       
   287 
       
   288 	wp_update_themes( );
       
   289 }
       
   290 
       
   291 add_action( 'admin_init', '_maybe_update_core' );
       
   292 add_action( 'wp_version_check', 'wp_version_check' );
       
   293 
       
   294 add_action( 'load-plugins.php', 'wp_update_plugins' );
       
   295 add_action( 'load-update.php', 'wp_update_plugins' );
       
   296 add_action( 'admin_init', '_maybe_update_plugins' );
       
   297 add_action( 'wp_update_plugins', 'wp_update_plugins' );
       
   298 
       
   299 add_action( 'load-themes.php', 'wp_update_themes' );
       
   300 add_action( 'load-update.php', 'wp_update_themes' );
       
   301 add_action( 'admin_init', '_maybe_update_themes' );
       
   302 add_action( 'wp_update_themes', 'wp_update_themes' );
       
   303 
       
   304 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
       
   305 	wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
       
   306 
       
   307 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
       
   308 	wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
       
   309 
       
   310 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
       
   311 	wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
       
   312 
       
   313 ?>