X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/54fb5972b908f9c2b16cd82cee580bcf61565873..8a06f4f9392d1ac373442f82ee40428a3cb81395:/wp-includes/functions.php
diff --git a/wp-includes/functions.php b/wp-includes/functions.php
index 83a00ef0..fb3aaf0f 100644
--- a/wp-includes/functions.php
+++ b/wp-includes/functions.php
@@ -1,2107 +1,996 @@
escape($_COOKIE[USER_COOKIE]);
- return $wpdb->get_var("SELECT $field FROM $wpdb->users WHERE user_login = '$user'");
-}
+ $i = mktime(
+ (int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ),
+ (int) substr( $m, 5, 2 ), (int) substr( $m, 8, 2 ), (int) substr( $m, 0, 4 )
+ );
+
+ if( 'U' == $dateformatstring )
+ return $i;
-function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
- global $month, $weekday, $month_abbrev, $weekday_abbrev;
- $m = $mysqlstring;
- if ( empty($m) ) {
- return false;
- }
- $i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
-
if ( -1 == $i || false == $i )
$i = 0;
- if ( !empty($month) && !empty($weekday) && $translate ) {
- $datemonth = $month[date('m', $i)];
- $datemonth_abbrev = $month_abbrev[$datemonth];
- $dateweekday = $weekday[date('w', $i)];
- $dateweekday_abbrev = $weekday_abbrev[$dateweekday];
- $dateformatstring = ' '.$dateformatstring;
- $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
- $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
- $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
- $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
+ if ( !empty( $wp_locale->month ) && !empty( $wp_locale->weekday ) && $translate ) {
+ $datemonth = $wp_locale->get_month( date( 'm', $i ) );
+ $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
+ $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
+ $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
+ $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
+ $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
+ $dateformatstring = ' ' . $dateformatstring;
+ $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
+
+ $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
+ }
+ $j = @date( $dateformatstring, $i );
+
+ /*
+ if ( !$j ) // for debug purposes
+ echo $i." ".$mysqlstring;
+ */
- $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
- }
- $j = @date($dateformatstring, $i);
- if ( !$j ) {
- // for debug purposes
- // echo $i." ".$mysqlstring;
- }
return $j;
}
-function current_time($type, $gmt = 0) {
- switch ($type) {
+/**
+ * Retrieve the current time based on specified type.
+ *
+ * The 'mysql' type will return the time in the format for MySQL DATETIME field.
+ * The 'timestamp' type will return the current timestamp.
+ *
+ * If the $gmt is set to either '1' or 'true', then both types will use the
+ * GMT offset in the WordPress option to add the GMT offset to the time.
+ *
+ * @since 1.0.0
+ *
+ * @param string $type Either 'mysql' or 'timestamp'.
+ * @param int|bool $gmt Optional. Whether to use $gmt offset. Default is false.
+ * @return unknown
+ */
+function current_time( $type, $gmt = 0 ) {
+ switch ( $type ) {
case 'mysql':
- if ( $gmt ) $d = gmdate('Y-m-d H:i:s');
- else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
- return $d;
+ return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
break;
case 'timestamp':
- if ( $gmt ) $d = time();
- else $d = time() + (get_settings('gmt_offset') * 3600);
- return $d;
+ return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
break;
}
}
-function date_i18n($dateformatstring, $unixtimestamp) {
- global $month, $weekday, $month_abbrev, $weekday_abbrev;
+/**
+ * Retrieve the date in localized format, based on timestamp.
+ *
+ * If the locale specifies the locale month and weekday, then the locale will
+ * take over the format for the date. If it isn't, then the date format string
+ * will be used instead.
+ *
+ * @since 0.71
+ *
+ * @param string $dateformatstring Format to display the date
+ * @param int $unixtimestamp Unix timestamp
+ * @return string The date, translated if locale specifies it.
+ */
+function date_i18n( $dateformatstring, $unixtimestamp ) {
+ global $wp_locale;
$i = $unixtimestamp;
- if ( (!empty($month)) && (!empty($weekday)) ) {
- $datemonth = $month[date('m', $i)];
- $datemonth_abbrev = $month_abbrev[$datemonth];
- $dateweekday = $weekday[date('w', $i)];
- $dateweekday_abbrev = $weekday_abbrev[$dateweekday];
+ // Sanity check for PHP 5.1.0-
+ if ( -1 == $i )
+ $i = false;
+
+ if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
+ $datemonth = $wp_locale->get_month( date( 'm', $i ) );
+ $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
+ $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
+ $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
+ $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
+ $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
$dateformatstring = ' '.$dateformatstring;
- $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
- $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
- $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
- $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
- $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
+ $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
+ $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
+
+ $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
}
- $j = @date($dateformatstring, $i);
+ $j = @date( $dateformatstring, $i );
return $j;
- }
+}
+
+/**
+ * Convert number to format based on the locale.
+ *
+ * @since 2.3.0
+ *
+ * @param mixed $number The number to convert based on locale.
+ * @param int $decimals Precision of the number of decimal places.
+ * @return string Converted number in string format.
+ */
+function number_format_i18n( $number, $decimals = null ) {
+ global $wp_locale;
+ // let the user override the precision only
+ $decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals );
+
+ return number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
+}
+
+/**
+ * Convert number of bytes largest unit bytes will fit into.
+ *
+ * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
+ * number of bytes to human readable number by taking the number of that unit
+ * that the bytes will go into it. Supports TB value.
+ *
+ * Please note that integers in PHP are limited to 32 bits, unless they are on
+ * 64 bit architecture, then they have 64 bit size. If you need to place the
+ * larger size then what PHP integer type will hold, then use a string. It will
+ * be converted to a double, which should always have 64 bit length.
+ *
+ * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
+ * @link http://en.wikipedia.org/wiki/Byte
+ *
+ * @since 2.3.0
+ *
+ * @param int|string $bytes Number of bytes. Note max integer size for integers.
+ * @param int $decimals Precision of number of decimal places.
+ * @return unknown
+ */
+function size_format( $bytes, $decimals = null ) {
+ $quant = array(
+ // ========================= Origin ====
+ 'TB' => 1099511627776, // pow( 1024, 4)
+ 'GB' => 1073741824, // pow( 1024, 3)
+ 'MB' => 1048576, // pow( 1024, 2)
+ 'kB' => 1024, // pow( 1024, 1)
+ 'B ' => 1, // pow( 1024, 0)
+ );
+
+ foreach ( $quant as $unit => $mag )
+ if ( doubleval($bytes) >= $mag )
+ return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
+
+ return false;
+}
+
-function get_weekstartend($mysqlstring, $start_of_week) {
- $my = substr($mysqlstring,0,4);
- $mm = substr($mysqlstring,8,2);
- $md = substr($mysqlstring,5,2);
- $day = mktime(0,0,0, $md, $mm, $my);
- $weekday = date('w',$day);
+function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
+ $my = substr( $mysqlstring, 0, 4 );
+ $mm = substr( $mysqlstring, 8, 2 );
+ $md = substr( $mysqlstring, 5, 2 );
+ $day = mktime( 0, 0, 0, $md, $mm, $my );
+ $weekday = date( 'w', $day );
$i = 86400;
+ if( !is_numeric($start_of_week) )
+ $start_of_week = get_option( 'start_of_week' );
- if ( $weekday < get_settings('start_of_week') )
- $weekday = 7 - (get_settings('start_of_week') - $weekday);
+ if ( $weekday < $start_of_week )
+ $weekday = 7 - $start_of_week - $weekday;
- while ($weekday > get_settings('start_of_week')) {
- $weekday = date('w',$day);
- if ( $weekday < get_settings('start_of_week') )
- $weekday = 7 - (get_settings('start_of_week') - $weekday);
+ while ( $weekday > $start_of_week ) {
+ $weekday = date( 'w', $day );
+ if ( $weekday < $start_of_week )
+ $weekday = 7 - $start_of_week - $weekday;
- $day = $day - 86400;
+ $day -= 86400;
$i = 0;
}
$week['start'] = $day + 86400 - $i;
- // $week['end'] = $day - $i + 691199;
$week['end'] = $week['start'] + 604799;
return $week;
}
-function get_lastpostdate($timezone = 'server') {
- global $cache_lastpostdate, $pagenow, $wpdb;
- $add_seconds_blog = get_settings('gmt_offset') * 3600;
- $add_seconds_server = date('Z');
- $now = current_time('mysql', 1);
- if ( !isset($cache_lastpostdate[$timezone]) ) {
- switch(strtolower($timezone)) {
- case 'gmt':
- $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
- break;
- case 'blog':
- $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
- break;
- case 'server':
- $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
- break;
- }
- $cache_lastpostdate[$timezone] = $lastpostdate;
- } else {
- $lastpostdate = $cache_lastpostdate[$timezone];
- }
- return $lastpostdate;
-}
-
-function get_lastpostmodified($timezone = 'server') {
- global $cache_lastpostmodified, $pagenow, $wpdb;
- $add_seconds_blog = get_settings('gmt_offset') * 3600;
- $add_seconds_server = date('Z');
- $now = current_time('mysql', 1);
- if ( !isset($cache_lastpostmodified[$timezone]) ) {
- switch(strtolower($timezone)) {
- case 'gmt':
- $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
- break;
- case 'blog':
- $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
- break;
- case 'server':
- $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
- break;
- }
- $lastpostdate = get_lastpostdate($timezone);
- if ( $lastpostdate > $lastpostmodified ) {
- $lastpostmodified = $lastpostdate;
- }
- $cache_lastpostmodified[$timezone] = $lastpostmodified;
- } else {
- $lastpostmodified = $cache_lastpostmodified[$timezone];
+/**
+ * Unserialize value only if it was serialized.
+ *
+ * @since 2.0.0
+ *
+ * @param string $original Maybe unserialized original, if is needed.
+ * @return mixed Unserialized data can be any type.
+ */
+function maybe_unserialize( $original ) {
+ if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
+ if ( false !== $gm = @unserialize( $original ) )
+ return $gm;
+ return $original;
+}
+
+/**
+ * Check value to find if it was serialized.
+ *
+ * If $data is not an string, then returned value will always be false.
+ * Serialized data is always a string.
+ *
+ * @since 2.0.5
+ *
+ * @param mixed $data Value to check to see if was serialized.
+ * @return bool False if not serialized and true if it was.
+ */
+function is_serialized( $data ) {
+ // if it isn't a string, it isn't serialized
+ if ( !is_string( $data ) )
+ return false;
+ $data = trim( $data );
+ if ( 'N;' == $data )
+ return true;
+ if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
+ return false;
+ switch ( $badions[1] ) {
+ case 'a' :
+ case 'O' :
+ case 's' :
+ if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
+ return true;
+ break;
+ case 'b' :
+ case 'i' :
+ case 'd' :
+ if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
+ return true;
+ break;
}
- return $lastpostmodified;
+ return false;
}
-function user_pass_ok($user_login,$user_pass) {
- global $cache_userdata;
- if ( empty($cache_userdata[$user_login]) ) {
- $userdata = get_userdatabylogin($user_login);
- } else {
- $userdata = $cache_userdata[$user_login];
- }
- return (md5($user_pass) == $userdata->user_pass);
+/**
+ * Check whether serialized data is of string type.
+ *
+ * @since 2.0.5
+ *
+ * @param mixed $data Serialized data
+ * @return bool False if not a serialized string, true if it is.
+ */
+function is_serialized_string( $data ) {
+ // if it isn't a string, it isn't a serialized string
+ if ( !is_string( $data ) )
+ return false;
+ $data = trim( $data );
+ if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
+ return true;
+ return false;
}
-
-function get_usernumposts($userid) {
+/**
+ * Retrieve option value based on setting name.
+ *
+ * If the option does not exist or does not have a value, then the return value
+ * will be false. This is useful to check whether you need to install an option
+ * and is commonly used during installation of plugin options and to test
+ * whether upgrading is required.
+ *
+ * You can "short-circuit" the retrieval of the option from the database for
+ * your plugin or core options that aren't protected. You can do so by hooking
+ * into the 'pre_option_$option' with the $option being replaced by the option
+ * name. You should not try to override special options, but you will not be
+ * prevented from doing so.
+ *
+ * There is a second filter called 'option_$option' with the $option being
+ * replaced with the option name. This gives the value as the only parameter.
+ *
+ * @since 1.5.0
+ * @package WordPress
+ * @subpackage Option
+ * @uses apply_filters() Calls 'pre_option_$optionname' false to allow
+ * overwriting the option value in a plugin.
+ * @uses apply_filters() Calls 'option_$optionname' with the option name value.
+ *
+ * @param string $setting Name of option to retrieve. Should already be SQL-escaped
+ * @return mixed Value set for the option.
+ */
+function get_option( $setting ) {
global $wpdb;
- return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_status = 'publish'");
-}
-
-// examine a url (supposedly from this blog) and try to
-// determine the post ID it represents.
-function url_to_postid($url) {
- global $wp_rewrite;
+ // Allow plugins to short-circuit options.
+ $pre = apply_filters( 'pre_option_' . $setting, false );
+ if ( false !== $pre )
+ return $pre;
- // First, check to see if there is a 'p=N' or 'page_id=N' to match against
- preg_match('#[?&](p|page_id)=(\d+)#', $url, $values);
- $id = intval($values[2]);
- if ( $id ) return $id;
-
- // Check to see if we are using rewrite rules
- $rewrite = $wp_rewrite->wp_rewrite_rules();
-
- // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
- if ( empty($rewrite) )
- return 0;
-
- // $url cleanup by Mark Jaquith
- // This fixes things like #anchors, ?query=strings, missing 'www.',
- // added 'www.', or added 'index.php/' that will mess up our WP_Query
- // and return a false negative
-
- // Get rid of the #anchor
- $url_split = explode('#', $url);
- $url = $url_split[0];
-
- // Get rid of URI ?query=string
- $url_split = explode('?', $url);
- $url = $url_split[0];
-
- // Add 'www.' if it is absent and should be there
- if ( false !== strpos(get_settings('home'), '://www.') && false === strpos($url, '://www.') )
- $url = str_replace('://', '://www.', $url);
-
- // Strip 'www.' if it is present and shouldn't be
- if ( false === strpos(get_settings('home'), '://www.') )
- $url = str_replace('://www.', '://', $url);
+ // prevent non-existent options from triggering multiple queries
+ $notoptions = wp_cache_get( 'notoptions', 'options' );
+ if ( isset( $notoptions[$setting] ) )
+ return false;
- // Strip 'index.php/' if we're not using path info permalinks
- if ( false === strpos($rewrite, 'index.php/') )
- $url = str_replace('index.php/', '', $url);
+ $alloptions = wp_load_alloptions();
- if ( false !== strpos($url, get_settings('home')) ) {
- // Chop off http://domain.com
- $url = str_replace(get_settings('home'), '', $url);
+ if ( isset( $alloptions[$setting] ) ) {
+ $value = $alloptions[$setting];
} else {
- // Chop off /path/to/blog
- $home_path = parse_url(get_settings('home'));
- $home_path = $home_path['path'];
- $url = str_replace($home_path, '', $url);
- }
-
- // Trim leading and lagging slashes
- $url = trim($url, '/');
-
- $request = $url;
-
- // Done with cleanup
-
- // Look for matches.
- $request_match = $request;
- foreach ($rewrite as $match => $query) {
- // If the requesting file is the anchor of the match, prepend it
- // to the path info.
- if ( (! empty($url)) && (strpos($match, $url) === 0) ) {
- $request_match = $url . '/' . $request;
- }
-
- if ( preg_match("!^$match!", $request_match, $matches) ) {
- // Got a match.
- // Trim the query of everything up to the '?'.
- $query = preg_replace("!^.+\?!", '', $query);
-
- // Substitute the substring matches into the query.
- eval("\$query = \"$query\";");
- $query = new WP_Query($query);
- if ( $query->is_single || $query->is_page )
- return $query->post->ID;
- else
- return 0;
- }
- }
- return 0;
-}
-
-
-function maybe_unserialize($original) {
- if ( false !== $gm = @ unserialize($original) )
- return $gm;
- else
- return $original;
-}
-
-/* Options functions */
-
-function get_settings($setting) {
- global $wpdb;
-
- $value = wp_cache_get($setting, 'options');
-
- if ( false === $value ) {
- if ( defined('WP_INSTALLING') )
- $wpdb->hide_errors();
- $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
- if ( defined('WP_INSTALLING') )
- $wpdb->show_errors();
-
- if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
- $value = $row->option_value;
- wp_cache_set($setting, $value, 'options');
- } else {
- return false;
+ $value = wp_cache_get( $setting, 'options' );
+
+ if ( false === $value ) {
+ if ( defined( 'WP_INSTALLING' ) )
+ $suppress = $wpdb->suppress_errors();
+ // expected_slashed ($setting)
+ $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1" );
+ if ( defined( 'WP_INSTALLING' ) )
+ $wpdb->suppress_errors($suppress);
+
+ if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
+ $value = $row->option_value;
+ wp_cache_add( $setting, $value, 'options' );
+ } else { // option does not exist, so we must cache its non-existence
+ $notoptions[$setting] = true;
+ wp_cache_set( 'notoptions', $notoptions, 'options' );
+ return false;
+ }
}
}
// If home is not set use siteurl.
if ( 'home' == $setting && '' == $value )
- return get_settings('siteurl');
-
- if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
- $value = preg_replace('|/+$|', '', $value);
-
- return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
-}
-
-function get_option($option) {
- return get_settings($option);
-}
-
-function get_user_option( $option, $user = 0 ) {
- global $wpdb;
-
- if ( empty($user) )
- $user = wp_get_current_user();
- else
- $user = get_userdata($user);
-
- if ( isset( $user->{$wpdb->prefix . $option} ) ) // Blog specific
- return $user->{$wpdb->prefix . $option};
- elseif ( isset( $user->{$option} ) ) // User specific and cross-blog
- return $user->{$option};
- else // Blog global
- return get_option( $option );
-}
-
-function form_option($option) {
- echo htmlspecialchars( get_option($option), ENT_QUOTES );
-}
-
+ return get_option( 'siteurl' );
+
+ if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) )
+ $value = untrailingslashit( $value );
+
+ return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) );
+}
+
+/**
+ * Protect WordPress special option from being modified.
+ *
+ * Will die if $option is in protected list.
+ *
+ * @since 2.2.0
+ * @package WordPress
+ * @subpackage Option
+ *
+ * @param string $option Option name.
+ */
+function wp_protect_special_option( $option ) {
+ $protected = array( 'alloptions', 'notoptions' );
+ if ( in_array( $option, $protected ) )
+ die( sprintf( __( '%s is a protected WP option and may not be modified' ), wp_specialchars( $option ) ) );
+}
+
+/**
+ * Print option value after sanitizing for forms.
+ *
+ * @uses attribute_escape Sanitizes value.
+ * @since 1.5.0
+ * @package WordPress
+ * @subpackage Option
+ *
+ * @param string $option Option name.
+ */
+function form_option( $option ) {
+ echo attribute_escape (get_option( $option ) );
+}
+
+/**
+ * Retrieve all autoload options or all options, if no autoloaded ones exist.
+ *
+ * This is different from wp_load_alloptions(), in this that function does not
+ * cache all options and will retrieve all options from the database every time
+ * it is called.
+ *
+ * @since 1.0.0
+ * @package WordPress
+ * @subpackage Option
+ * @uses apply_filters() Calls 'pre_option_$optionname' hook with option value as parameter.
+ * @uses apply_filters() Calls 'all_options' on options list.
+ *
+ * @return array List of all options.
+ */
function get_alloptions() {
global $wpdb, $wp_queries;
- $wpdb->hide_errors();
- if ( !$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'") ) {
- $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
- }
- $wpdb->show_errors();
+ $show = $wpdb->hide_errors();
+ if ( !$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
+ $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
+ $wpdb->show_errors($show);
- foreach ($options as $option) {
+ foreach ( $options as $option ) {
// "When trying to design a foolproof system,
// never underestimate the ingenuity of the fools :)" -- Dougal
- if ( 'siteurl' == $option->option_name )
- $option->option_value = preg_replace('|/+$|', '', $option->option_value);
- if ( 'home' == $option->option_name )
- $option->option_value = preg_replace('|/+$|', '', $option->option_value);
- if ( 'category_base' == $option->option_name )
- $option->option_value = preg_replace('|/+$|', '', $option->option_value);
- $value = maybe_unserialize($option->option_value);
- $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value);
- }
- return apply_filters('all_options', $all_options);
-}
+ if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) )
+ $option->option_value = untrailingslashit( $option->option_value );
+ $value = maybe_unserialize( $option->option_value );
+ $all_options->{$option->option_name} = apply_filters( 'pre_option_' . $option->option_name, $value );
+ }
+ return apply_filters( 'all_options', $all_options );
+}
+
+/**
+ * Loads and caches all autoloaded options, if available or all options.
+ *
+ * This is different from get_alloptions(), in that this function will cache the
+ * options and will return the cached options when called again.
+ *
+ * @since 2.2.0
+ * @package WordPress
+ * @subpackage Option
+ *
+ * @return array List all options.
+ */
+function wp_load_alloptions() {
+ global $wpdb;
-function update_option($option_name, $newvalue) {
+ $alloptions = wp_cache_get( 'alloptions', 'options' );
+
+ if ( !$alloptions ) {
+ $suppress = $wpdb->suppress_errors();
+ if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
+ $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 )
+ $alloptions[$o->option_name] = $o->option_value;
+ wp_cache_add( 'alloptions', $alloptions, 'options' );
+ }
+ return $alloptions;
+}
+
+/**
+ * Update the value of an option that was already added.
+ *
+ * If the option does not exist, then the option will be added with the option
+ * value, but you will not be able to set whether it is autoloaded. If you want
+ * to set whether an option autoloaded, then you need to use the add_option().
+ *
+ * When the option is updated, then the filter named
+ * 'update_option_$option_name', with the $option_name as the $option_name
+ * parameter value, will be called. The hook should accept two parameters, the
+ * first is the old parameter and the second is the new parameter.
+ *
+ * @since 1.0.0
+ * @package WordPress
+ * @subpackage Option
+ *
+ * @param string $option_name Option name. Expected to not be SQL-escaped
+ * @param mixed $newvalue Option value.
+ * @return bool False if value was not updated and true if value was updated.
+ */
+function update_option( $option_name, $newvalue ) {
global $wpdb;
- if ( is_string($newvalue) )
- $newvalue = trim($newvalue);
+ wp_protect_special_option( $option_name );
+
+ $safe_option_name = $wpdb->escape( $option_name );
+ $newvalue = sanitize_option( $option_name, $newvalue );
+
+ $oldvalue = get_option( $safe_option_name );
+
+ $newvalue = apply_filters( 'pre_update_option_' . $option_name, $newvalue, $oldvalue );
// If the new and old values are the same, no need to update.
- $oldvalue = get_option($option_name);
- if ( $newvalue == $oldvalue ) {
+ if ( $newvalue === $oldvalue )
return false;
- }
if ( false === $oldvalue ) {
- add_option($option_name, $newvalue);
+ add_option( $option_name, $newvalue );
return true;
}
+ $notoptions = wp_cache_get( 'notoptions', 'options' );
+ if ( is_array( $notoptions ) && isset( $notoptions[$option_name] ) ) {
+ unset( $notoptions[$option_name] );
+ wp_cache_set( 'notoptions', $notoptions, 'options' );
+ }
+
$_newvalue = $newvalue;
- if ( is_array($newvalue) || is_object($newvalue) )
- $newvalue = serialize($newvalue);
+ $newvalue = maybe_serialize( $newvalue );
- wp_cache_set($option_name, $newvalue, 'options');
+ $alloptions = wp_load_alloptions();
+ if ( isset( $alloptions[$option_name] ) ) {
+ $alloptions[$option_name] = $newvalue;
+ wp_cache_set( 'alloptions', $alloptions, 'options' );
+ } else {
+ wp_cache_set( $option_name, $newvalue, 'options' );
+ }
- $newvalue = $wpdb->escape($newvalue);
- $option_name = $wpdb->escape($option_name);
- $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
+ $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name ) );
if ( $wpdb->rows_affected == 1 ) {
- do_action("update_option_{$option_name}", array('old'=>$oldvalue, 'new'=>$_newvalue));
+ do_action( "update_option_{$option_name}", $oldvalue, $_newvalue );
return true;
}
return false;
}
-function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
- global $wpdb;
- if ( !$global )
- $option_name = $wpdb->prefix . $option_name;
- return update_usermeta( $user_id, $option_name, $newvalue );
-}
-
-// thx Alex Stapleton, http://alex.vort-x.net/blog/
-function add_option($name, $value = '', $description = '', $autoload = 'yes') {
+/**
+ * Add a new option.
+ *
+ * You can create options without values and then add values later. Does not
+ * check whether the option has already been added, but does check that you
+ * aren't adding a protected WordPress option. Care should be taken to not name
+ * options, the same as the ones which are protected and to not add options
+ * that were already added.
+ *
+ * The filter named 'add_option_$optionname', with the $optionname being
+ * replaced with the option's name, will be called. The hook should accept two
+ * parameters, the first is the option name, and the second is the value.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since 1.0.0
+ * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
+ *
+ * @param string $name Option name to add. Expects to NOT be SQL escaped.
+ * @param mixed $value Optional. Option value, can be anything.
+ * @param mixed $deprecated Optional. Description. Not used anymore.
+ * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
+ * @return null returns when finished.
+ */
+function add_option( $name, $value = '', $deprecated = '', $autoload = 'yes' ) {
global $wpdb;
- // Make sure the option doesn't already exist
- if ( false !== get_option($name) )
- return;
-
- if ( is_array($value) || is_object($value) )
- $value = serialize($value);
-
- wp_cache_set($name, $value, 'options');
-
- $name = $wpdb->escape($name);
- $value = $wpdb->escape($value);
- $description = $wpdb->escape($description);
- $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
-
- return;
-}
-
-function delete_option($name) {
- global $wpdb;
- // Get the ID, if no ID then return
- $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
- if ( !$option_id ) return false;
- $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
- wp_cache_delete($name, 'options');
- return true;
-}
-
-function add_post_meta($post_id, $key, $value, $unique = false) {
- global $wpdb, $post_meta_cache;
-
- if ( $unique ) {
- if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
-= '$key' AND post_id = '$post_id'") ) {
- return false;
- }
- }
-
- $original = $value;
- if ( is_array($value) || is_object($value) )
- $value = $wpdb->escape(serialize($value));
-
- $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
-
- $post_meta_cache['$post_id'][$key][] = $original;
-
- return true;
-}
-
-function delete_post_meta($post_id, $key, $value = '') {
- global $wpdb, $post_meta_cache;
+ wp_protect_special_option( $name );
+ $safe_name = $wpdb->escape( $name );
+ $value = sanitize_option( $name, $value );
- if ( empty($value) ) {
- $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
-post_id = '$post_id' AND meta_key = '$key'");
- } else {
- $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
-post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
- }
+ // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
+ $notoptions = wp_cache_get( 'notoptions', 'options' );
+ if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) )
+ if ( false !== get_option( $safe_name ) )
+ return;
- if ( !$meta_id )
- return false;
+ $value = maybe_serialize( $value );
+ $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
- if ( empty($value) ) {
- $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
-AND meta_key = '$key'");
- unset($post_meta_cache['$post_id'][$key]);
+ if ( 'yes' == $autoload ) {
+ $alloptions = wp_load_alloptions();
+ $alloptions[$name] = $value;
+ wp_cache_set( 'alloptions', $alloptions, 'options' );
} else {
- $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
-AND meta_key = '$key' AND meta_value = '$value'");
- $cache_key = $post_meta_cache['$post_id'][$key];
- if ($cache_key) foreach ( $cache_key as $index => $data )
- if ( $data == $value )
- unset($post_meta_cache['$post_id'][$key][$index]);
- }
-
- unset($post_meta_cache['$post_id'][$key]);
-
- return true;
-}
-
-function get_post_meta($post_id, $key, $single = false) {
- global $wpdb, $post_meta_cache;
-
- if ( isset($post_meta_cache[$post_id][$key]) ) {
- if ( $single ) {
- return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
- } else {
- return maybe_unserialize( $post_meta_cache[$post_id][$key] );
- }
+ wp_cache_set( $name, $value, 'options' );
}
- $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
-
- $values = array();
- if ( $metalist ) {
- foreach ($metalist as $metarow) {
- $values[] = $metarow[0];
- }
+ // This option exists now
+ $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
+ if ( is_array( $notoptions ) && isset( $notoptions[$name] ) ) {
+ unset( $notoptions[$name] );
+ wp_cache_set( 'notoptions', $notoptions, 'options' );
}
- if ( $single ) {
- if ( count($values) ) {
- $return = maybe_unserialize( $values[0] );
- } else {
- return '';
- }
- } else {
- $return = $values;
- }
+ $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload ) );
- return maybe_unserialize($return);
+ do_action( "add_option_{$name}", $name, $value );
+ return;
}
-function update_post_meta($post_id, $key, $value, $prev_value = '') {
- global $wpdb, $post_meta_cache;
-
- $original_value = $value;
- if ( is_array($value) || is_object($value) )
- $value = $wpdb->escape(serialize($value));
+/**
+ * Removes option by name and prevents removal of protected WordPress options.
+ *
+ * @package WordPress
+ * @subpackage Option
+ * @since unknown
+ *
+ * @param string $name Option name to remove.
+ * @return bool True, if succeed. False, if failure.
+ */
+function delete_option( $name ) {
+ global $wpdb;
- $original_prev = $prev_value;
- if ( is_array($prev_value) || is_object($prev_value) )
- $prev_value = $wpdb->escape(serialize($prev_value));
+ wp_protect_special_option( $name );
- if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
-= '$key' AND post_id = '$post_id'") ) {
+ // Get the ID, if no ID then return
+ // expected_slashed ($name)
+ $option = $wpdb->get_row( "SELECT option_id, autoload FROM $wpdb->options WHERE option_name = '$name'" );
+ if ( is_null($option) || !$option->option_id )
return false;
- }
-
- if ( empty($prev_value) ) {
- $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
-meta_key = '$key' AND post_id = '$post_id'");
- $cache_key = $post_meta_cache['$post_id'][$key];
- if ( !empty($cache_key) )
- foreach ($cache_key as $index => $data)
- $post_meta_cache['$post_id'][$key][$index] = $original_value;
+ // expected_slashed ($name)
+ $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'" );
+ if ( 'yes' == $option->autoload ) {
+ $alloptions = wp_load_alloptions();
+ if ( isset( $alloptions[$name] ) ) {
+ unset( $alloptions[$name] );
+ wp_cache_set( 'alloptions', $alloptions, 'options' );
+ }
} else {
- $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
-meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
- $cache_key = $post_meta_cache['$post_id'][$key];
- if ( !empty($cache_key) )
- foreach ($cache_key as $index => $data)
- if ( $data == $original_prev )
- $post_meta_cache['$post_id'][$key][$index] = $original_value;
+ wp_cache_delete( $name, 'options' );
}
-
return true;
}
-// Deprecated. Use get_post().
-function get_postdata($postid) {
- $post = &get_post($postid);
-
- $postdata = array (
- 'ID' => $post->ID,
- 'Author_ID' => $post->post_author,
- 'Date' => $post->post_date,
- 'Content' => $post->post_content,
- 'Excerpt' => $post->post_excerpt,
- 'Title' => $post->post_title,
- 'Category' => $post->post_category,
- 'post_status' => $post->post_status,
- 'comment_status' => $post->comment_status,
- 'ping_status' => $post->ping_status,
- 'post_password' => $post->post_password,
- 'to_ping' => $post->to_ping,
- 'pinged' => $post->pinged,
- 'post_name' => $post->post_name
- );
-
- return $postdata;
-}
+/**
+ * Serialize data, if needed.
+ *
+ * @param mixed $data Data that might be serialized.
+ * @return mixed A scalar data
+ */
+function maybe_serialize( $data ) {
+ if ( is_array( $data ) || is_object( $data ) )
+ return serialize( $data );
-// Retrieves post data given a post ID or post object.
-// Handles post caching.
-function &get_post(&$post, $output = OBJECT) {
- global $post_cache, $wpdb;
+ if ( is_serialized( $data ) )
+ return serialize( $data );
- if ( empty($post) ) {
- if ( isset($GLOBALS['post']) )
- $_post = & $GLOBALS['post'];
- else
- $_post = null;
- } elseif ( is_object($post) ) {
- if ( 'static' == $post->post_status )
- return get_page($post, $output);
- if ( !isset($post_cache[$post->ID]) )
- $post_cache[$post->ID] = &$post;
- $_post = & $post_cache[$post->ID];
- } else {
- if ( $_post = wp_cache_get($post, 'pages') )
- return get_page($_post, $output);
- elseif ( isset($post_cache[$post]) )
- $_post = & $post_cache[$post];
- else {
- $query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
- $_post = & $wpdb->get_row($query);
- if ( 'static' == $_post->post_status )
- return get_page($_post, $output);
- $post_cache[$post] = & $_post;
- }
- }
+ return $data;
+}
- if ( defined(WP_IMPORTING) )
- unset($post_cache);
- if ( $output == OBJECT ) {
- return $_post;
- } elseif ( $output == ARRAY_A ) {
- return get_object_vars($_post);
- } elseif ( $output == ARRAY_N ) {
- return array_values(get_object_vars($_post));
- } else {
- return $_post;
+function make_url_footnote( $content ) {
+ preg_match_all( '/(.+?)<\/a>/', $content, $matches );
+ $j = 0;
+ for ( $i=0; $i
" . wp_explain_nonce($action) . "
\n\t\n\t{$errors[0]}
"; + break; + default : + $message = "$message
"; + } + + if ( defined( 'WP_SITEURL' ) && '' != WP_SITEURL ) + $admin_dir = WP_SITEURL . '/wp-admin/'; + elseif ( function_exists( 'get_bloginfo' ) && '' != get_bloginfo( 'wpurl' ) ) + $admin_dir = get_bloginfo( 'wpurl' ) . '/wp-admin/'; + elseif ( strpos( $_SERVER['PHP_SELF'], 'wp-admin' ) !== false ) + $admin_dir = ''; + else + $admin_dir = 'wp-admin/'; + + if ( !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ) : + if( !headers_sent() ){ + status_header( 500 ); + nocache_headers(); + header( 'Content-Type: text/html; charset=utf-8' ); } - $html .= "