<?php
function get_plugins($checked = null) {
	$obj = get_transient('sem_update_plugins');

	if ( !is_object($obj) ) {
		$obj = new stdClass;
		$obj->last_checked = false;
		$obj->checked = array();
		$obj->response = array();
	}

	if ( in_array(current_filter(), array('load-plugins.php', 'load-tools_page_sem-tools')) ) {
		$timeout = 3600;
	} else {
		$timeout = 43200;
	}
	
	if ( is_array($checked) && $checked && $obj->checked && $obj->checked != $checked ) {
		delete_transient('sem_update_plugins');
		delete_transient('update_plugins');
		return false;
	}
	
	if ( ( $obj->last_checked >= time() - $timeout ) || $_POST )
		return $obj->response;
	
	global $wp_version;
	
	if ( !function_exists('get_plugins') )
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	
	$obj->last_checked = time();
	set_transient('sem_update_plugins', $obj);
	
	$url = version_checker_server;
	
	$to_check = get_plugins();
	$check = array();
	
	foreach ( $to_check as $file => $plugin )
		$check[$file] = $plugin['Version'];
	
	$obj->checked = $check;
	set_transient('sem_update_plugins', $obj);
	
	$body = array(
		'check' => $check,
		'locale' => apply_filters('core_version_check_locale', get_locale()),
		);
	
	$options = array(
		'timeout' => 3,
		'body' => $body,
		'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url'),
		);
	
	$cache_id = md5(serialize(array($url, $options)));
	$raw_response = wp_cache_get($cache_id, 'sem_api');
	
	if ( $raw_response === false ) {
		$raw_response = wp_remote_post($url, $options);
		wp_cache_set($cache_id, $raw_response, 'sem_api');
	}
	
	if ( is_wp_error($raw_response) )
		set_transient('sem_api_error', $raw_response->get_error_messages());
	else
		delete_transient('sem_api_error');
	
	if ( is_wp_error($raw_response) || 200 != $raw_response['response']['code'] )
		$response = false;
	else
		$response = @unserialize($raw_response['body']);
	
	if ( $response !== false ) { // keep old response in case of error
		$obj->response = $response;
		set_transient('sem_update_plugins', $obj);
	}
	
	return $obj;
}

?>