HTTP API updates and fixes. Props sivel. see #8702

git-svn-id: http://svn.automattic.com/wordpress/trunk@10507 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-02-05 20:46:39 +00:00
parent b5f68df374
commit 80a6b13f81
5 changed files with 133 additions and 41 deletions

View File

@ -624,6 +624,79 @@ function delete_option( $name ) {
return true; return true;
} }
/**
* Delete a transient
*
* @since 2.8.0
* @package WordPress
* @subpackage Transient
*
* @param string $transient Transient name. Expected to not be SQL-escaped
* @return bool true if successful, false otherwise
*/
function delete_transient($transient) {
global $_wp_using_ext_object_cache, $wpdb;
if ( $_wp_using_ext_object_cache ) {
return wp_cache_delete($transient, 'transient');
} else {
$transient = $wpdb->escape($transient);
return delete_option($transient);
}
}
/**
* Get the value of a transient
*
* If the transient does not exist or does not have a value, then the return value
* will be false.
*
* @since 2.8.0
* @package WordPress
* @subpackage Transient
*
* @param string $transient Transient name. Expected to not be SQL-escaped
* @return mixed Value of transient
*/
function get_transient($transient) {
global $_wp_using_ext_object_cache, $wpdb;
if ( $_wp_using_ext_object_cache ) {
return wp_cache_get($transient, 'transient');
} else {
$transient = $wpdb->escape($transient);
return get_option($transient);
}
}
/**
* Set/update the value of a transient
*
* You do not need to serialize values, if the value needs to be serialize, then
* it will be serialized before it is set.
*
* @since 2.8.0
* @package WordPress
* @subpackage Transient
*
* @param string $transient Transient name. Expected to not be SQL-escaped
* @param mixed $value Transient value.
* @return bool False if value was not set and true if value was set.
*/
function set_transient($transient, $value) {
global $_wp_using_ext_object_cache, $wpdb;
if ( $_wp_using_ext_object_cache ) {
return wp_cache_set($transient, $value, 'transient');
} else {
$safe_transient = $wpdb->escape($transient);
if ( false === get_option( $safe_transient ) )
return add_option($transient, $value, '', 'no');
else
return update_option($transient, $value);
}
}
/** /**
* Saves and restores user interface settings stored in a cookie. * Saves and restores user interface settings stored in a cookie.
* *

View File

@ -237,10 +237,10 @@ class WP_Http {
} else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) { } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
$working_transport['streams'] = new WP_Http_Streams(); $working_transport['streams'] = new WP_Http_Streams();
$blocking_transport[] = &$working_transport['streams']; $blocking_transport[] = &$working_transport['streams'];
} else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) ) { } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) && ( isset($args['ssl']) && !$args['ssl'] ) ) {
$working_transport['fopen'] = new WP_Http_Fopen(); $working_transport['fopen'] = new WP_Http_Fopen();
$blocking_transport[] = &$working_transport['fopen']; $blocking_transport[] = &$working_transport['fopen'];
} else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) { } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) && ( isset($args['ssl']) && !$args['ssl'] ) ) {
$working_transport['fsockopen'] = new WP_Http_Fsockopen(); $working_transport['fsockopen'] = new WP_Http_Fsockopen();
$blocking_transport[] = &$working_transport['fsockopen']; $blocking_transport[] = &$working_transport['fsockopen'];
} }
@ -282,15 +282,18 @@ class WP_Http {
if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) { if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) {
$working_transport['exthttp'] = new WP_Http_ExtHttp(); $working_transport['exthttp'] = new WP_Http_ExtHttp();
$blocking_transport[] = &$working_transport['exthttp']; $blocking_transport[] = &$working_transport['exthttp'];
} else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) ) {
$working_transport['curl'] = new WP_Http_Curl();
$blocking_transport[] = &$working_transport['curl'];
} else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) { } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) {
$working_transport['streams'] = new WP_Http_Streams(); $working_transport['streams'] = new WP_Http_Streams();
$blocking_transport[] = &$working_transport['streams']; $blocking_transport[] = &$working_transport['streams'];
} else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) { } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) && ( isset($args['ssl']) && !$args['ssl'] ) ) {
$working_transport['fsockopen'] = new WP_Http_Fsockopen(); $working_transport['fsockopen'] = new WP_Http_Fsockopen();
$blocking_transport[] = &$working_transport['fsockopen']; $blocking_transport[] = &$working_transport['fsockopen'];
} }
foreach ( array('streams', 'fsockopen', 'exthttp') as $transport ) { foreach ( array('curl', 'streams', 'fsockopen', 'exthttp') as $transport ) {
if ( isset($working_transport[$transport]) ) if ( isset($working_transport[$transport]) )
$nonblocking_transport[] = &$working_transport[$transport]; $nonblocking_transport[] = &$working_transport[$transport];
} }
@ -358,17 +361,27 @@ class WP_Http {
'timeout' => apply_filters( 'http_request_timeout', 5), 'timeout' => apply_filters( 'http_request_timeout', 5),
'redirection' => apply_filters( 'http_request_redirection_count', 5), 'redirection' => apply_filters( 'http_request_redirection_count', 5),
'httpversion' => apply_filters( 'http_request_version', '1.0'), 'httpversion' => apply_filters( 'http_request_version', '1.0'),
'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ), 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ),
'blocking' => true, 'blocking' => true,
'headers' => array(), 'headers' => array(),
'body' => null, 'body' => null,
'compress' => false, 'compress' => false,
'decompress' => true 'decompress' => true,
'sslverify' => true
); );
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
$r = apply_filters( 'http_request_args', $r, $url ); $r = apply_filters( 'http_request_args', $r, $url );
$arrURL = parse_url($url);
// Determine if this is a https call and pass that on to the transport functions
// so that we can blacklist the transports that do not support ssl verification
if ( $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl' )
$r['ssl'] = true;
else
$r['ssl'] = false;
if ( is_null( $r['headers'] ) ) if ( is_null( $r['headers'] ) )
$r['headers'] = array(); $r['headers'] = array();
@ -927,7 +940,11 @@ class WP_Http_Streams {
'max_redirects' => $r['redirection'], 'max_redirects' => $r['redirection'],
'protocol_version' => (float) $r['httpversion'], 'protocol_version' => (float) $r['httpversion'],
'header' => $strHeaders, 'header' => $strHeaders,
'timeout' => $r['timeout'] 'timeout' => $r['timeout'],
'ssl' => array(
'verify_peer' => apply_filters('https_ssl_verify', $r['sslverify']),
'verify_host' => apply_filters('https_ssl_verify', $r['sslverify'])
)
) )
); );
@ -1060,6 +1077,10 @@ class WP_Http_ExtHTTP {
'redirect' => $r['redirection'], 'redirect' => $r['redirection'],
'useragent' => $r['user-agent'], 'useragent' => $r['user-agent'],
'headers' => $r['headers'], 'headers' => $r['headers'],
'ssl' => array(
'verifypeer' => apply_filters('https_ssl_verify', $r['sslverify']),
'verifyhost' => apply_filters('https_ssl_verify', $r['sslverify'])
)
); );
if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts
@ -1153,28 +1174,31 @@ class WP_Http_Curl {
$r['timeout'] = 1; $r['timeout'] = 1;
$handle = curl_init(); $handle = curl_init();
curl_setopt( $handle, CURLOPT_URL, $url); curl_setopt( $handle, CURLOPT_URL, $url);
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
// The cURL extension requires that the option be set for the HEAD to curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, apply_filters('https_ssl_verify', $r['sslverify']) );
// work properly. curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, apply_filters('https_ssl_verify', $r['sslverify']) );
if ( 'HEAD' === $r['method'] ) {
curl_setopt( $handle, CURLOPT_NOBODY, true );
}
if ( true === $r['blocking'] ) {
curl_setopt( $handle, CURLOPT_HEADER, true );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
} else {
curl_setopt( $handle, CURLOPT_HEADER, false );
curl_setopt( $handle, CURLOPT_NOBODY, true );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 0 );
}
curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] ); curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 1 ); curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] );
curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] ); curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );
curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] ); curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] );
switch ( $r['method'] ) {
case 'HEAD':
curl_setopt( $handle, CURLOPT_NOBODY, true );
break;
case 'POST':
curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] );
break;
}
if ( true === $r['blocking'] )
curl_setopt( $handle, CURLOPT_HEADER, true );
else
curl_setopt( $handle, CURLOPT_HEADER, false );
// The option doesn't work with safe mode or when open_basedir is set. // The option doesn't work with safe mode or when open_basedir is set.
if ( !ini_get('safe_mode') && !ini_get('open_basedir') ) if ( !ini_get('safe_mode') && !ini_get('open_basedir') )
curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );

View File

@ -1596,11 +1596,11 @@ class WP_Rewrite {
* @return array Rewrite rules. * @return array Rewrite rules.
*/ */
function wp_rewrite_rules() { function wp_rewrite_rules() {
$this->rules = get_option('rewrite_rules'); $this->rules = get_transient('rewrite_rules');
if ( empty($this->rules) ) { if ( empty($this->rules) ) {
$this->matches = 'matches'; $this->matches = 'matches';
$this->rewrite_rules(); $this->rewrite_rules();
update_option('rewrite_rules', $this->rules); set_transient('rewrite_rules', $this->rules);
} }
return $this->rules; return $this->rules;
@ -1783,7 +1783,7 @@ class WP_Rewrite {
* @access public * @access public
*/ */
function flush_rules() { function flush_rules() {
delete_option('rewrite_rules'); delete_transient('rewrite_rules');
$this->wp_rewrite_rules(); $this->wp_rewrite_rules();
if ( function_exists('save_mod_rewrite_rules') ) if ( function_exists('save_mod_rewrite_rules') )
save_mod_rewrite_rules(); save_mod_rewrite_rules();

View File

@ -714,14 +714,8 @@ class RSSCache {
$cache_option = 'rss_' . $this->file_name( $url ); $cache_option = 'rss_' . $this->file_name( $url );
$cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
// shouldn't these be using get_option() ? set_transient($cache_option, $rss);
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $cache_option ) ) ) set_transient($cache_timestamp, time() );
add_option($cache_option, '', '', 'no');
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $cache_timestamp ) ) )
add_option($cache_timestamp, '', '', 'no');
update_option($cache_option, $rss);
update_option($cache_timestamp, time() );
return $cache_option; return $cache_option;
} }
@ -736,15 +730,13 @@ class RSSCache {
$this->ERROR = ""; $this->ERROR = "";
$cache_option = 'rss_' . $this->file_name( $url ); $cache_option = 'rss_' . $this->file_name( $url );
if ( ! get_option( $cache_option ) ) { if ( ! $rss = get_transient( $cache_option ) ) {
$this->debug( $this->debug(
"Cache doesn't contain: $url (cache option: $cache_option)" "Cache doesn't contain: $url (cache option: $cache_option)"
); );
return 0; return 0;
} }
$rss = get_option( $cache_option );
return $rss; return $rss;
} }
@ -760,7 +752,7 @@ class RSSCache {
$cache_option = $this->file_name( $url ); $cache_option = $this->file_name( $url );
$cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
if ( $mtime = get_option($cache_timestamp) ) { if ( $mtime = get_transient($cache_timestamp) ) {
// find how long ago the file was added to the cache // find how long ago the file was added to the cache
// and whether that is longer then MAX_AGE // and whether that is longer then MAX_AGE
$age = time() - $mtime; $age = time() - $mtime;

View File

@ -257,10 +257,13 @@ $prefix = $wpdb->set_prefix($table_prefix);
if ( is_wp_error($prefix) ) if ( is_wp_error($prefix) )
wp_die(/*WP_I18N_BAD_PREFIX*/'<strong>ERROR</strong>: <code>$table_prefix</code> in <code>wp-config.php</code> can only contain numbers, letters, and underscores.'/*/WP_I18N_BAD_PREFIX*/); wp_die(/*WP_I18N_BAD_PREFIX*/'<strong>ERROR</strong>: <code>$table_prefix</code> in <code>wp-config.php</code> can only contain numbers, letters, and underscores.'/*/WP_I18N_BAD_PREFIX*/);
if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) {
require_once (WP_CONTENT_DIR . '/object-cache.php'); require_once (WP_CONTENT_DIR . '/object-cache.php');
else $_wp_using_ext_object_cache = true;
} else {
require_once (ABSPATH . WPINC . '/cache.php'); require_once (ABSPATH . WPINC . '/cache.php');
$_wp_using_ext_object_cache = false;
}
wp_cache_init(); wp_cache_init();
if ( function_exists('wp_cache_add_global_groups') ) { if ( function_exists('wp_cache_add_global_groups') ) {