X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/a7152b1f4c81650e5ba96ef07f9aa9c0fbc7752b..refs/tags/wordpress-3.2-scripts:/wp-includes/functions.php
diff --git a/wp-includes/functions.php b/wp-includes/functions.php
index 8cd9ba79..7a682ef3 100644
--- a/wp-includes/functions.php
+++ b/wp-includes/functions.php
@@ -27,19 +27,18 @@ function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
if ( empty( $m ) )
return false;
- if ( 'G' == $dateformatstring ) {
+ if ( 'G' == $dateformatstring )
return strtotime( $m . ' +0000' );
- }
$i = strtotime( $m );
if ( 'U' == $dateformatstring )
return $i;
- if ( $translate)
- return date_i18n( $dateformatstring, $i );
+ if ( $translate )
+ return date_i18n( $dateformatstring, $i );
else
- return date( $dateformatstring, $i );
+ return date( $dateformatstring, $i );
}
/**
@@ -85,8 +84,8 @@ function current_time( $type, $gmt = 0 ) {
function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
global $wp_locale;
$i = $unixtimestamp;
- // Sanity check for PHP 5.1.0-
- if ( false === $i || intval($i) < 0 ) {
+
+ if ( false === $i ) {
if ( ! $gmt )
$i = current_time( 'timestamp' );
else
@@ -119,6 +118,23 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
}
+ $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
+ $timezone_formats_re = implode( '|', $timezone_formats );
+ if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
+ $timezone_string = get_option( 'timezone_string' );
+ if ( $timezone_string ) {
+ $timezone_object = timezone_open( $timezone_string );
+ $date_object = date_create( null, $timezone_object );
+ foreach( $timezone_formats as $timezone_format ) {
+ if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
+ $formatted = date_format( $date_object, $timezone_format );
+ $dateformatstring = ' '.$dateformatstring;
+ $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
+ $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
+ }
+ }
+ }
+ }
$j = @$datefunc( $dateformatstring, $i );
// allow plugins to redo this entirely for languages with untypical grammars
$j = apply_filters('date_i18n', $j, $req_format, $i, $gmt);
@@ -230,26 +246,31 @@ function maybe_unserialize( $original ) {
*/
function is_serialized( $data ) {
// if it isn't a string, it isn't serialized
- if ( !is_string( $data ) )
+ if ( ! is_string( $data ) )
return false;
$data = trim( $data );
- if ( 'N;' == $data )
+ if ( 'N;' == $data )
return true;
- if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
+ $length = strlen( $data );
+ if ( $length < 4 )
return false;
- switch ( $badions[1] ) {
+ if ( ':' !== $data[1] )
+ return false;
+ $lastc = $data[$length-1];
+ if ( ';' !== $lastc && '}' !== $lastc )
+ return false;
+ $token = $data[0];
+ switch ( $token ) {
+ case 's' :
+ if ( '"' !== $data[$length-2] )
+ return false;
case 'a' :
case 'O' :
- case 's' :
- if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
- return true;
- break;
+ return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
case 'b' :
case 'i' :
case 'd' :
- if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
- return true;
- break;
+ return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
}
return false;
}
@@ -267,9 +288,19 @@ function is_serialized_string( $data ) {
if ( !is_string( $data ) )
return false;
$data = trim( $data );
- if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
+ $length = strlen( $data );
+ if ( $length < 4 )
+ return false;
+ elseif ( ':' !== $data[1] )
+ return false;
+ elseif ( ';' !== $data[$length-1] )
+ return false;
+ elseif ( $data[0] !== 's' )
+ return false;
+ elseif ( '"' !== $data[$length-2] )
+ return false;
+ else
return true;
- return false;
}
/**
@@ -412,10 +443,11 @@ function wp_load_alloptions() {
$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
$wpdb->suppress_errors($suppress);
$alloptions = array();
- foreach ( (array) $alloptions_db as $o )
+ foreach ( (array) $alloptions_db as $o ) {
$alloptions[$o->option_name] = $o->option_value;
- if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
- wp_cache_add( 'alloptions', $alloptions, 'options' );
+ }
+ if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
+ wp_cache_add( 'alloptions', $alloptions, 'options' );
}
return $alloptions;
@@ -439,7 +471,7 @@ function wp_load_core_site_options( $site_id = null ) {
if ( empty($site_id) )
$site_id = $wpdb->siteid;
- $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'dashboard_blog', 'can_compress_scripts', 'global_terms_enabled' );
+ $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled' );
$core_options_in = "'" . implode("', '", $core_options) . "'";
$options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) );
@@ -487,7 +519,7 @@ function update_option( $option, $newvalue ) {
wp_protect_special_option( $option );
if ( is_object($newvalue) )
- $newvalue = wp_clone($newvalue);
+ $newvalue = clone $newvalue;
$newvalue = sanitize_option( $option, $newvalue );
$oldvalue = get_option( $option );
@@ -546,7 +578,6 @@ function update_option( $option, $newvalue ) {
* @package WordPress
* @subpackage Option
* @since 1.0.0
- * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
*
* @uses do_action() Calls 'add_option' hook before adding the option.
* @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
@@ -570,7 +601,7 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
wp_protect_special_option( $option );
if ( is_object($value) )
- $value = wp_clone($value);
+ $value = clone $value;
$value = sanitize_option( $option, $value );
@@ -670,7 +701,7 @@ function delete_option( $option ) {
function delete_transient( $transient ) {
global $_wp_using_ext_object_cache;
- do_action( 'delete_transient_' . $transient, $transient );
+ do_action( 'delete_transient_' . $transient, $transient );
if ( $_wp_using_ext_object_cache ) {
$result = wp_cache_delete( $transient, 'transient' );
@@ -758,7 +789,7 @@ function get_transient( $transient ) {
function set_transient( $transient, $value, $expiration = 0 ) {
global $_wp_using_ext_object_cache;
- $value = apply_filters( 'pre_set_transient_' . $transient, $value );
+ $value = apply_filters( 'pre_set_transient_' . $transient, $value );
if ( $_wp_using_ext_object_cache ) {
$result = wp_cache_set( $transient, $value, 'transient', $expiration );
@@ -1163,6 +1194,8 @@ function debug_fclose( $fp ) {
*/
function do_enclose( $content, $post_ID ) {
global $wpdb;
+
+ //TODO: Tidy this ghetto code up and make the debug code optional
include_once( ABSPATH . WPINC . '/class-IXR.php' );
$log = debug_fopen( ABSPATH . 'enclosures.log', 'a' );
@@ -1183,7 +1216,7 @@ function do_enclose( $content, $post_ID ) {
foreach ( $pung as $link_test ) {
if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
- $mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
+ $mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $link_test ) . '%') );
do_action( 'delete_postmeta', $mid );
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
do_action( 'deleted_postmeta', $mid );
@@ -1197,13 +1230,13 @@ function do_enclose( $content, $post_ID ) {
continue;
if ( isset( $test['query'] ) )
$post_links[] = $link_test;
- elseif ( $test['path'] != '/' && $test['path'] != '' )
+ elseif ( isset($test['path']) && ( $test['path'] != '/' ) && ($test['path'] != '' ) )
$post_links[] = $link_test;
}
}
foreach ( (array) $post_links as $url ) {
- if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url . '%' ) ) ) {
+ if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $url ) . '%' ) ) ) {
if ( $headers = wp_get_http_headers( $url) ) {
$len = (int) $headers['content-length'];
@@ -1268,7 +1301,7 @@ function wp_get_http( $url, $file_path = false, $red = 1 ) {
return false;
$headers = wp_remote_retrieve_headers( $response );
- $headers['response'] = $response['response']['code'];
+ $headers['response'] = wp_remote_retrieve_response_code( $response );
// WP_HTTP no longer follows redirects for HEAD requests.
if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {
@@ -1283,7 +1316,7 @@ function wp_get_http( $url, $file_path = false, $red = 1 ) {
if ( !$out_fp )
return $headers;
- fwrite( $out_fp, $response['body']);
+ fwrite( $out_fp, wp_remote_retrieve_body( $response ) );
fclose($out_fp);
clearstatcache();
@@ -1321,8 +1354,8 @@ function wp_get_http_headers( $url, $deprecated = false ) {
* @return int 1 when new day, 0 if not a new day.
*/
function is_new_day() {
- global $day, $previousday;
- if ( $day != $previousday )
+ global $currentday, $previousday;
+ if ( $currentday != $previousday )
return 1;
else
return 0;
@@ -1347,6 +1380,36 @@ function build_query( $data ) {
return _http_build_query( $data, null, '&', '', false );
}
+// from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
+function _http_build_query($data, $prefix=null, $sep=null, $key='', $urlencode=true) {
+ $ret = array();
+
+ foreach ( (array) $data as $k => $v ) {
+ if ( $urlencode)
+ $k = urlencode($k);
+ if ( is_int($k) && $prefix != null )
+ $k = $prefix.$k;
+ if ( !empty($key) )
+ $k = $key . '%5B' . $k . '%5D';
+ if ( $v === NULL )
+ continue;
+ elseif ( $v === FALSE )
+ $v = '0';
+
+ if ( is_array($v) || is_object($v) )
+ array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
+ elseif ( $urlencode )
+ array_push($ret, $k.'='.urlencode($v));
+ else
+ array_push($ret, $k.'='.$v);
+ }
+
+ if ( NULL === $sep )
+ $sep = ini_get('arg_separator.output');
+
+ return implode($sep, $ret);
+}
+
/**
* Retrieve a modified URL query string.
*
@@ -1489,7 +1552,7 @@ function wp_remote_fopen( $uri ) {
if ( is_wp_error( $response ) )
return false;
- return $response['body'];
+ return wp_remote_retrieve_body( $response );
}
/**
@@ -1619,7 +1682,7 @@ function status_header( $header ) {
* The several different headers cover the different ways cache prevention is handled
* by different browsers
*
- * @since 2.8
+ * @since 2.8.0
*
* @uses apply_filters()
* @return array The associative array of header names and field values.
@@ -1633,7 +1696,7 @@ function wp_get_nocache_headers() {
);
if ( function_exists('apply_filters') ) {
- $headers = apply_filters('nocache_headers', $headers);
+ $headers = (array) apply_filters('nocache_headers', $headers);
}
return $headers;
}
@@ -1649,7 +1712,7 @@ function wp_get_nocache_headers() {
*/
function nocache_headers() {
$headers = wp_get_nocache_headers();
- foreach( (array) $headers as $name => $field_value )
+ foreach( $headers as $name => $field_value )
@header("{$name}: {$field_value}");
}
@@ -1887,11 +1950,6 @@ function wp_nonce_url( $actionurl, $action = -1 ) {
* offer absolute protection, but should protect against most cases. It is very
* important to use nonce field in forms.
*
- * If you set $echo to true and set $referer to true, then you will need to
- * retrieve the {@link wp_referer_field() wp referer field}. If you have the
- * $referer set to true and are echoing the nonce field, it will also echo the
- * referer field.
- *
* The $action and $name are optional, but if you want to have better security,
* it is strongly suggested to set those two parameters. It is easier to just
* call the function without any parameters, because validation of the nonce
@@ -1915,11 +1973,12 @@ function wp_nonce_url( $actionurl, $action = -1 ) {
function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
$name = esc_attr( $name );
$nonce_field = '';
- if ( $echo )
- echo $nonce_field;
if ( $referer )
- wp_referer_field( $echo );
+ $nonce_field .= wp_referer_field( false );
+
+ if ( $echo )
+ echo $nonce_field;
return $nonce_field;
}
@@ -2058,7 +2117,7 @@ function path_is_absolute( $path ) {
if ( realpath($path) == $path )
return true;
- if ( strlen($path) == 0 || $path{0} == '.' )
+ if ( strlen($path) == 0 || $path[0] == '.' )
return false;
// windows allows absolute paths like this
@@ -2087,6 +2146,42 @@ function path_join( $base, $path ) {
return rtrim($base, '/') . '/' . ltrim($path, '/');
}
+/**
+ * Determines a writable directory for temporary files.
+ * Function's preference is to WP_CONTENT_DIR followed by the return value of sys_get_temp_dir()
, before finally defaulting to /tmp/
+ *
+ * In the event that this function does not find a writable location, It may be overridden by the WP_TEMP_DIR
constant in your wp-config.php
file.
+ *
+ * @since 2.5.0
+ *
+ * @return string Writable temporary directory
+ */
+function get_temp_dir() {
+ static $temp;
+ if ( defined('WP_TEMP_DIR') )
+ return trailingslashit(WP_TEMP_DIR);
+
+ if ( $temp )
+ return trailingslashit($temp);
+
+ $temp = WP_CONTENT_DIR . '/';
+ if ( is_dir($temp) && @is_writable($temp) )
+ return $temp;
+
+ if ( function_exists('sys_get_temp_dir') ) {
+ $temp = sys_get_temp_dir();
+ if ( @is_writable($temp) )
+ return trailingslashit($temp);
+ }
+
+ $temp = ini_get('upload_tmp_dir');
+ if ( is_dir($temp) && @is_writable($temp) )
+ return trailingslashit($temp);
+
+ $temp = '/tmp/';
+ return $temp;
+}
+
/**
* Get an array containing the current upload directory's path and url.
*
@@ -2190,14 +2285,14 @@ function wp_upload_dir( $time = null ) {
* before the extension, and will continue adding numbers until the filename is
* unique.
*
- * The callback must accept two parameters, the first one is the directory and
- * the second is the filename. The callback must be a function.
+ * The callback is passed three parameters, the first one is the directory, the
+ * second is the filename, and the third is the extension.
*
- * @since 2.5
+ * @since 2.5.0
*
* @param string $dir
* @param string $filename
- * @param string $unique_filename_callback Function name, must be a function.
+ * @param mixed $unique_filename_callback Callback.
* @return string New filename, if given wasn't unique.
*/
function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
@@ -2213,9 +2308,9 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null )
if ( $name === $ext )
$name = '';
- // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
+ // Increment the file number until we have a unique file to save in $dir. Use callback if supplied.
if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
- $filename = $unique_filename_callback( $dir, $name );
+ $filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
} else {
$number = '';
@@ -2470,6 +2565,7 @@ function get_allowed_mime_types() {
'txt|asc|c|cc|h' => 'text/plain',
'csv' => 'text/csv',
'tsv' => 'text/tab-separated-values',
+ 'ics' => 'text/calendar',
'rtx' => 'text/richtext',
'css' => 'text/css',
'htm|html' => 'text/html',
@@ -2573,7 +2669,7 @@ function wp_explain_nonce( $action ) {
$trans['edit']['plugin'] = array( __( 'Your attempt to edit this plugin file: “%s” has failed.' ), 'use_id' );
$trans['activate']['plugin'] = array( __( 'Your attempt to activate this plugin: “%s” has failed.' ), 'use_id' );
$trans['deactivate']['plugin'] = array( __( 'Your attempt to deactivate this plugin: “%s” has failed.' ), 'use_id' );
- $trans['upgrade']['plugin'] = array( __( 'Your attempt to upgrade this plugin: “%s” has failed.' ), 'use_id' );
+ $trans['upgrade']['plugin'] = array( __( 'Your attempt to update this plugin: “%s” has failed.' ), 'use_id' );
$trans['add']['post'] = array( __( 'Your attempt to add this post has failed.' ), false );
$trans['delete']['post'] = array( __( 'Your attempt to delete this post: “%s” has failed.' ), 'get_the_title' );
@@ -2657,9 +2753,12 @@ function wp_nonce_ays( $action ) {
* @param string|array $args Optional arguements to control behaviour.
*/
function wp_die( $message, $title = '', $args = array() ) {
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
+ die('-1');
+
if ( function_exists( 'apply_filters' ) ) {
$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler');
- }else {
+ } else {
$function = '_default_wp_die_handler';
}
@@ -2739,7 +2838,7 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) {
?>
->
+>