Advertisement
anthonyluth

beta-functions

Jun 23rd, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.30 KB | None | 0 0
  1. <?php
  2. /**
  3. * Main WordPress API
  4. *
  5. * @package WordPress
  6. */
  7.  
  8. /**
  9. * Converts MySQL DATETIME field to user specified date format.
  10. *
  11. * If $dateformatstring has 'G' value, then gmmktime() function will be used to
  12. * make the time. If $dateformatstring is set to 'U', then mktime() function
  13. * will be used to make the time.
  14. *
  15. * The $translate will only be used, if it is set to true and it is by default
  16. * and if the $wp_locale object has the month and weekday set.
  17. *
  18. * @since 0.71
  19. *
  20. * @param string $dateformatstring Either 'G', 'U', or php date format.
  21. * @param string $mysqlstring Time from mysql DATETIME field.
  22. * @param bool $translate Optional. Default is true. Will switch format to locale.
  23. * @return string Date formated by $dateformatstring or locale (if available).
  24. */
  25. function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
  26. $m = $mysqlstring;
  27. if ( empty( $m ) )
  28. return false;
  29.  
  30. if ( 'G' == $dateformatstring )
  31. return strtotime( $m . ' +0000' );
  32.  
  33. $i = strtotime( $m );
  34.  
  35. if ( 'U' == $dateformatstring )
  36. return $i;
  37.  
  38. if ( $translate )
  39. return date_i18n( $dateformatstring, $i );
  40. else
  41. return date( $dateformatstring, $i );
  42. }
  43.  
  44. /**
  45. * Retrieve the current time based on specified type.
  46. *
  47. * The 'mysql' type will return the time in the format for MySQL DATETIME field.
  48. * The 'timestamp' type will return the current timestamp.
  49. *
  50. * If $gmt is set to either '1' or 'true', then both types will use GMT time.
  51. * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
  52. *
  53. * @since 1.0.0
  54. *
  55. * @param string $type Either 'mysql' or 'timestamp'.
  56. * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
  57. * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
  58. */
  59. function current_time( $type, $gmt = 0 ) {
  60. switch ( $type ) {
  61. case 'mysql':
  62. return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
  63. break;
  64. case 'timestamp':
  65. return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
  66. break;
  67. }
  68. }
  69.  
  70. /**
  71. * Retrieve the date in localized format, based on timestamp.
  72. *
  73. * If the locale specifies the locale month and weekday, then the locale will
  74. * take over the format for the date. If it isn't, then the date format string
  75. * will be used instead.
  76. *
  77. * @since 0.71
  78. *
  79. * @param string $dateformatstring Format to display the date.
  80. * @param int $unixtimestamp Optional. Unix timestamp.
  81. * @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
  82. * @return string The date, translated if locale specifies it.
  83. */
  84. function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
  85. global $wp_locale;
  86. $i = $unixtimestamp;
  87. // Sanity check for PHP 5.1.0-
  88. if ( false === $i || intval($i) < 0 ) {
  89. if ( ! $gmt )
  90. $i = current_time( 'timestamp' );
  91. else
  92. $i = time();
  93. // we should not let date() interfere with our
  94. // specially computed timestamp
  95. $gmt = true;
  96. }
  97.  
  98. // store original value for language with untypical grammars
  99. // see http://core.trac.wordpress.org/ticket/9396
  100. $req_format = $dateformatstring;
  101.  
  102. $datefunc = $gmt? 'gmdate' : 'date';
  103.  
  104. if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
  105. $datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) );
  106. $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
  107. $dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
  108. $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
  109. $datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
  110. $datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) );
  111. $dateformatstring = ' '.$dateformatstring;
  112. $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
  113. $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
  114. $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
  115. $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
  116. $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
  117. $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
  118.  
  119. $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
  120. }
  121. $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
  122. $timezone_formats_re = implode( '|', $timezone_formats );
  123. if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) && wp_timezone_supported() ) {
  124. $timezone_string = get_option( 'timezone_string' );
  125. if ( $timezone_string ) {
  126. $timezone_object = timezone_open( $timezone_string );
  127. $date_object = date_create( null, $timezone_object );
  128. foreach( $timezone_formats as $timezone_format ) {
  129. if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
  130. $formatted = date_format( $date_object, $timezone_format );
  131. $dateformatstring = ' '.$dateformatstring;
  132. $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
  133. $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
  134. }
  135. }
  136. }
  137. }
  138. $j = @$datefunc( $dateformatstring, $i );
  139. // allow plugins to redo this entirely for languages with untypical grammars
  140. $j = apply_filters('date_i18n', $j, $req_format, $i, $gmt);
  141. return $j;
  142. }
  143.  
  144. /**
  145. * Convert integer number to format based on the locale.
  146. *
  147. * @since 2.3.0
  148. *
  149. * @param int $number The number to convert based on locale.
  150. * @param int $decimals Precision of the number of decimal places.
  151. * @return string Converted number in string format.
  152. */
  153. function number_format_i18n( $number, $decimals = 0 ) {
  154. global $wp_locale;
  155. $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
  156. return apply_filters( 'number_format_i18n', $formatted );
  157. }
  158.  
  159. /**
  160. * Convert number of bytes largest unit bytes will fit into.
  161. *
  162. * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
  163. * number of bytes to human readable number by taking the number of that unit
  164. * that the bytes will go into it. Supports TB value.
  165. *
  166. * Please note that integers in PHP are limited to 32 bits, unless they are on
  167. * 64 bit architecture, then they have 64 bit size. If you need to place the
  168. * larger size then what PHP integer type will hold, then use a string. It will
  169. * be converted to a double, which should always have 64 bit length.
  170. *
  171. * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
  172. * @link http://en.wikipedia.org/wiki/Byte
  173. *
  174. * @since 2.3.0
  175. *
  176. * @param int|string $bytes Number of bytes. Note max integer size for integers.
  177. * @param int $decimals Precision of number of decimal places. Deprecated.
  178. * @return bool|string False on failure. Number string on success.
  179. */
  180. function size_format( $bytes, $decimals = 0 ) {
  181. $quant = array(
  182. // ========================= Origin ====
  183. 'TB' => 1099511627776, // pow( 1024, 4)
  184. 'GB' => 1073741824, // pow( 1024, 3)
  185. 'MB' => 1048576, // pow( 1024, 2)
  186. 'kB' => 1024, // pow( 1024, 1)
  187. 'B ' => 1, // pow( 1024, 0)
  188. );
  189. foreach ( $quant as $unit => $mag )
  190. if ( doubleval($bytes) >= $mag )
  191. return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
  192.  
  193. return false;
  194. }
  195.  
  196. /**
  197. * Get the week start and end from the datetime or date string from mysql.
  198. *
  199. * @since 0.71
  200. *
  201. * @param string $mysqlstring Date or datetime field type from mysql.
  202. * @param int $start_of_week Optional. Start of the week as an integer.
  203. * @return array Keys are 'start' and 'end'.
  204. */
  205. function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
  206. $my = substr( $mysqlstring, 0, 4 ); // Mysql string Year
  207. $mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month
  208. $md = substr( $mysqlstring, 5, 2 ); // Mysql string day
  209. $day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
  210. $weekday = date( 'w', $day ); // The day of the week from the timestamp
  211. if ( !is_numeric($start_of_week) )
  212. $start_of_week = get_option( 'start_of_week' );
  213.  
  214. if ( $weekday < $start_of_week )
  215. $weekday += 7;
  216.  
  217. $start = $day - 86400 * ( $weekday - $start_of_week ); // The most recent week start day on or before $day
  218. $end = $start + 604799; // $start + 7 days - 1 second
  219. return compact( 'start', 'end' );
  220. }
  221.  
  222. /**
  223. * Unserialize value only if it was serialized.
  224. *
  225. * @since 2.0.0
  226. *
  227. * @param string $original Maybe unserialized original, if is needed.
  228. * @return mixed Unserialized data can be any type.
  229. */
  230. function maybe_unserialize( $original ) {
  231. if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
  232. return @unserialize( $original );
  233. return $original;
  234. }
  235.  
  236. /**
  237. * Check value to find if it was serialized.
  238. *
  239. * If $data is not an string, then returned value will always be false.
  240. * Serialized data is always a string.
  241. *
  242. * @since 2.0.5
  243. *
  244. * @param mixed $data Value to check to see if was serialized.
  245. * @return bool False if not serialized and true if it was.
  246. */
  247. function is_serialized( $data ) {
  248. // if it isn't a string, it isn't serialized
  249. if ( ! is_string( $data ) )
  250. return false;
  251. $data = trim( $data );
  252. if ( 'N;' == $data )
  253. return true;
  254. $length = strlen( $data );
  255. if ( $length < 4 )
  256. return false;
  257. if ( ':' !== $data[1] )
  258. return false;
  259. $lastc = $data[$length-1];
  260. if ( ';' !== $lastc && '}' !== $lastc )
  261. return false;
  262. $token = $data[0];
  263. switch ( $token ) {
  264. case 's' :
  265. if ( '"' !== $data[$length-2] )
  266. return false;
  267. case 'a' :
  268. case 'O' :
  269. return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
  270. case 'b' :
  271. case 'i' :
  272. case 'd' :
  273. return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
  274. }
  275. return false;
  276. }
  277.  
  278. /**
  279. * Check whether serialized data is of string type.
  280. *
  281. * @since 2.0.5
  282. *
  283. * @param mixed $data Serialized data
  284. * @return bool False if not a serialized string, true if it is.
  285. */
  286. function is_serialized_string( $data ) {
  287. // if it isn't a string, it isn't a serialized string
  288. if ( !is_string( $data ) )
  289. return false;
  290. $data = trim( $data );
  291. if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
  292. return true;
  293. return false;
  294. }
  295.  
  296. /**
  297. * Retrieve option value based on name of option.
  298. *
  299. * If the option does not exist or does not have a value, then the return value
  300. * will be false. This is useful to check whether you need to install an option
  301. * and is commonly used during installation of plugin options and to test
  302. * whether upgrading is required.
  303. *
  304. * If the option was serialized then it will be unserialized when it is returned.
  305. *
  306. * @since 1.5.0
  307. * @package WordPress
  308. * @subpackage Option
  309. * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
  310. * Any value other than false will "short-circuit" the retrieval of the option
  311. * and return the returned value. You should not try to override special options,
  312. * but you will not be prevented from doing so.
  313. * @uses apply_filters() Calls 'option_$option', after checking the option, with
  314. * the option value.
  315. *
  316. * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  317. * @return mixed Value set for the option.
  318. */
  319. function get_option( $option, $default = false ) {
  320. global $wpdb;
  321.  
  322. // Allow plugins to short-circuit options.
  323. $pre = apply_filters( 'pre_option_' . $option, false );
  324. if ( false !== $pre )
  325. return $pre;
  326.  
  327. $option = trim($option);
  328. if ( empty($option) )
  329. return false;
  330.  
  331. if ( defined( 'WP_SETUP_CONFIG' ) )
  332. return false;
  333.  
  334. if ( ! defined( 'WP_INSTALLING' ) ) {
  335. // prevent non-existent options from triggering multiple queries
  336. $notoptions = wp_cache_get( 'notoptions', 'options' );
  337. if ( isset( $notoptions[$option] ) )
  338. return $default;
  339.  
  340. $alloptions = wp_load_alloptions();
  341.  
  342. if ( isset( $alloptions[$option] ) ) {
  343. $value = $alloptions[$option];
  344. } else {
  345. $value = wp_cache_get( $option, 'options' );
  346.  
  347. if ( false === $value ) {
  348. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
  349.  
  350. // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  351. if ( is_object( $row ) ) {
  352. $value = $row->option_value;
  353. wp_cache_add( $option, $value, 'options' );
  354. } else { // option does not exist, so we must cache its non-existence
  355. $notoptions[$option] = true;
  356. wp_cache_set( 'notoptions', $notoptions, 'options' );
  357. return $default;
  358. }
  359. }
  360. }
  361. } else {
  362. $suppress = $wpdb->suppress_errors();
  363. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
  364. $wpdb->suppress_errors( $suppress );
  365. if ( is_object( $row ) )
  366. $value = $row->option_value;
  367. else
  368. return $default;
  369. }
  370.  
  371. // If home is not set use siteurl.
  372. if ( 'home' == $option && '' == $value )
  373. return get_option( 'siteurl' );
  374.  
  375. if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
  376. $value = untrailingslashit( $value );
  377.  
  378. return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
  379. }
  380.  
  381. /**
  382. * Protect WordPress special option from being modified.
  383. *
  384. * Will die if $option is in protected list. Protected options are 'alloptions'
  385. * and 'notoptions' options.
  386. *
  387. * @since 2.2.0
  388. * @package WordPress
  389. * @subpackage Option
  390. *
  391. * @param string $option Option name.
  392. */
  393. function wp_protect_special_option( $option ) {
  394. $protected = array( 'alloptions', 'notoptions' );
  395. if ( in_array( $option, $protected ) )
  396. wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
  397. }
  398.  
  399. /**
  400. * Print option value after sanitizing for forms.
  401. *
  402. * @uses attr Sanitizes value.
  403. * @since 1.5.0
  404. * @package WordPress
  405. * @subpackage Option
  406. *
  407. * @param string $option Option name.
  408. */
  409. function form_option( $option ) {
  410. echo esc_attr( get_option( $option ) );
  411. }
  412.  
  413. /**
  414. * Loads and caches all autoloaded options, if available or all options.
  415. *
  416. * @since 2.2.0
  417. * @package WordPress
  418. * @subpackage Option
  419. *
  420. * @return array List of all options.
  421. */
  422. function wp_load_alloptions() {
  423. global $wpdb;
  424.  
  425. if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
  426. $alloptions = wp_cache_get( 'alloptions', 'options' );
  427. else
  428. $alloptions = false;
  429.  
  430. if ( !$alloptions ) {
  431. $suppress = $wpdb->suppress_errors();
  432. if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
  433. $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
  434. $wpdb->suppress_errors($suppress);
  435. $alloptions = array();
  436. foreach ( (array) $alloptions_db as $o ) {
  437. $alloptions[$o->option_name] = $o->option_value;
  438. }
  439. if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
  440. wp_cache_add( 'alloptions', $alloptions, 'options' );
  441. }
  442.  
  443. return $alloptions;
  444. }
  445.  
  446. /**
  447. * Loads and caches certain often requested site options if is_multisite() and a peristent cache is not being used.
  448. *
  449. * @since 3.0.0
  450. * @package WordPress
  451. * @subpackage Option
  452. *
  453. * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
  454. */
  455. function wp_load_core_site_options( $site_id = null ) {
  456. global $wpdb, $_wp_using_ext_object_cache;
  457.  
  458. if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
  459. return;
  460.  
  461. if ( empty($site_id) )
  462. $site_id = $wpdb->siteid;
  463.  
  464. $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' );
  465.  
  466. $core_options_in = "'" . implode("', '", $core_options) . "'";
  467. $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) );
  468.  
  469. foreach ( $options as $option ) {
  470. $key = $option->meta_key;
  471. $cache_key = "{$site_id}:$key";
  472. $option->meta_value = maybe_unserialize( $option->meta_value );
  473.  
  474. wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
  475. }
  476. }
  477.  
  478. /**
  479. * Update the value of an option that was already added.
  480. *
  481. * You do not need to serialize values. If the value needs to be serialized, then
  482. * it will be serialized before it is inserted into the database. Remember,
  483. * resources can not be serialized or added as an option.
  484. *
  485. * If the option does not exist, then the option will be added with the option
  486. * value, but you will not be able to set whether it is autoloaded. If you want
  487. * to set whether an option is autoloaded, then you need to use the add_option().
  488. *
  489. * @since 1.0.0
  490. * @package WordPress
  491. * @subpackage Option
  492. *
  493. * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
  494. * option value to be stored.
  495. * @uses do_action() Calls 'update_option' hook before updating the option.
  496. * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
  497. *
  498. * @param string $option Option name. Expected to not be SQL-escaped.
  499. * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
  500. * @return bool False if value was not updated and true if value was updated.
  501. */
  502. function update_option( $option, $newvalue ) {
  503. global $wpdb;
  504.  
  505. $option = trim($option);
  506. if ( empty($option) )
  507. return false;
  508.  
  509. wp_protect_special_option( $option );
  510.  
  511. if ( is_object($newvalue) )
  512. $newvalue = wp_clone($newvalue);
  513.  
  514. $newvalue = sanitize_option( $option, $newvalue );
  515. $oldvalue = get_option( $option );
  516. $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
  517.  
  518. // If the new and old values are the same, no need to update.
  519. if ( $newvalue === $oldvalue )
  520. return false;
  521.  
  522. if ( false === $oldvalue )
  523. return add_option( $option, $newvalue );
  524.  
  525. $notoptions = wp_cache_get( 'notoptions', 'options' );
  526. if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  527. unset( $notoptions[$option] );
  528. wp_cache_set( 'notoptions', $notoptions, 'options' );
  529. }
  530.  
  531. $_newvalue = $newvalue;
  532. $newvalue = maybe_serialize( $newvalue );
  533.  
  534. do_action( 'update_option', $option, $oldvalue, $_newvalue );
  535. if ( ! defined( 'WP_INSTALLING' ) ) {
  536. $alloptions = wp_load_alloptions();
  537. if ( isset( $alloptions[$option] ) ) {
  538. $alloptions[$option] = $_newvalue;
  539. wp_cache_set( 'alloptions', $alloptions, 'options' );
  540. } else {
  541. wp_cache_set( $option, $_newvalue, 'options' );
  542. }
  543. }
  544.  
  545. $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
  546.  
  547. if ( $result ) {
  548. do_action( "update_option_{$option}", $oldvalue, $_newvalue );
  549. do_action( 'updated_option', $option, $oldvalue, $_newvalue );
  550. return true;
  551. }
  552. return false;
  553. }
  554.  
  555. /**
  556. * Add a new option.
  557. *
  558. * You do not need to serialize values. If the value needs to be serialized, then
  559. * it will be serialized before it is inserted into the database. Remember,
  560. * resources can not be serialized or added as an option.
  561. *
  562. * You can create options without values and then add values later. Does not
  563. * check whether the option has already been added, but does check that you
  564. * aren't adding a protected WordPress option. Care should be taken to not name
  565. * options the same as the ones which are protected and to not add options
  566. * that were already added.
  567. *
  568. * @package WordPress
  569. * @subpackage Option
  570. * @since 1.0.0
  571. *
  572. * @uses do_action() Calls 'add_option' hook before adding the option.
  573. * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
  574. *
  575. * @param string $option Name of option to add. Expected to not be SQL-escaped.
  576. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  577. * @param mixed $deprecated Optional. Description. Not used anymore.
  578. * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
  579. * @return null returns when finished.
  580. */
  581. function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
  582. global $wpdb;
  583.  
  584. if ( !empty( $deprecated ) )
  585. _deprecated_argument( __FUNCTION__, '2.3' );
  586.  
  587. $option = trim($option);
  588. if ( empty($option) )
  589. return false;
  590.  
  591. wp_protect_special_option( $option );
  592.  
  593. if ( is_object($value) )
  594. $value = wp_clone($value);
  595.  
  596. $value = sanitize_option( $option, $value );
  597.  
  598. // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
  599. $notoptions = wp_cache_get( 'notoptions', 'options' );
  600. if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
  601. if ( false !== get_option( $option ) )
  602. return;
  603.  
  604. $_value = $value;
  605. $value = maybe_serialize( $value );
  606. $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
  607. do_action( 'add_option', $option, $_value );
  608. if ( ! defined( 'WP_INSTALLING' ) ) {
  609. if ( 'yes' == $autoload ) {
  610. $alloptions = wp_load_alloptions();
  611. $alloptions[$option] = $value;
  612. wp_cache_set( 'alloptions', $alloptions, 'options' );
  613. } else {
  614. wp_cache_set( $option, $value, 'options' );
  615. }
  616. }
  617.  
  618. // This option exists now
  619. $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
  620. if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  621. unset( $notoptions[$option] );
  622. wp_cache_set( 'notoptions', $notoptions, 'options' );
  623. }
  624.  
  625. $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) );
  626.  
  627. if ( $result ) {
  628. do_action( "add_option_{$option}", $option, $_value );
  629. do_action( 'added_option', $option, $_value );
  630. return true;
  631. }
  632. return false;
  633. }
  634.  
  635. /**
  636. * Removes option by name. Prevents removal of protected WordPress options.
  637. *
  638. * @package WordPress
  639. * @subpackage Option
  640. * @since 1.2.0
  641. *
  642. * @uses do_action() Calls 'delete_option' hook before option is deleted.
  643. * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
  644. *
  645. * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  646. * @return bool True, if option is successfully deleted. False on failure.
  647. */
  648. function delete_option( $option ) {
  649. global $wpdb;
  650.  
  651. wp_protect_special_option( $option );
  652.  
  653. // Get the ID, if no ID then return
  654. $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
  655. if ( is_null( $row ) )
  656. return false;
  657. do_action( 'delete_option', $option );
  658. $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $option) );
  659. if ( ! defined( 'WP_INSTALLING' ) ) {
  660. if ( 'yes' == $row->autoload ) {
  661. $alloptions = wp_load_alloptions();
  662. if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
  663. unset( $alloptions[$option] );
  664. wp_cache_set( 'alloptions', $alloptions, 'options' );
  665. }
  666. } else {
  667. wp_cache_delete( $option, 'options' );
  668. }
  669. }
  670. if ( $result ) {
  671. do_action( "delete_option_$option", $option );
  672. do_action( 'deleted_option', $option );
  673. return true;
  674. }
  675. return false;
  676. }
  677.  
  678. /**
  679. * Delete a transient
  680. *
  681. * @since 2.8.0
  682. * @package WordPress
  683. * @subpackage Transient
  684. *
  685. * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
  686. * @uses do_action() Calls 'deleted_transient' hook on success.
  687. *
  688. * @param string $transient Transient name. Expected to not be SQL-escaped.
  689. * @return bool true if successful, false otherwise
  690. */
  691. function delete_transient( $transient ) {
  692. global $_wp_using_ext_object_cache;
  693.  
  694. do_action( 'delete_transient_' . $transient, $transient );
  695.  
  696. if ( $_wp_using_ext_object_cache ) {
  697. $result = wp_cache_delete( $transient, 'transient' );
  698. } else {
  699. $option_timeout = '_transient_timeout_' . $transient;
  700. $option = '_transient_' . $transient;
  701. $result = delete_option( $option );
  702. if ( $result )
  703. delete_option( $option_timeout );
  704. }
  705.  
  706. if ( $result )
  707. do_action( 'deleted_transient', $transient );
  708. return $result;
  709. }
  710.  
  711. /**
  712. * Get the value of a transient
  713. *
  714. * If the transient does not exist or does not have a value, then the return value
  715. * will be false.
  716. *
  717. * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
  718. * Any value other than false will "short-circuit" the retrieval of the transient
  719. * and return the returned value.
  720. * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
  721. * the transient value.
  722. *
  723. * @since 2.8.0
  724. * @package WordPress
  725. * @subpackage Transient
  726. *
  727. * @param string $transient Transient name. Expected to not be SQL-escaped
  728. * @return mixed Value of transient
  729. */
  730. function get_transient( $transient ) {
  731. global $_wp_using_ext_object_cache;
  732.  
  733. $pre = apply_filters( 'pre_transient_' . $transient, false );
  734. if ( false !== $pre )
  735. return $pre;
  736.  
  737. if ( $_wp_using_ext_object_cache ) {
  738. $value = wp_cache_get( $transient, 'transient' );
  739. } else {
  740. $transient_option = '_transient_' . $transient;
  741. if ( ! defined( 'WP_INSTALLING' ) ) {
  742. // If option is not in alloptions, it is not autoloaded and thus has a timeout
  743. $alloptions = wp_load_alloptions();
  744. if ( !isset( $alloptions[$transient_option] ) ) {
  745. $transient_timeout = '_transient_timeout_' . $transient;
  746. if ( get_option( $transient_timeout ) < time() ) {
  747. delete_option( $transient_option );
  748. delete_option( $transient_timeout );
  749. return false;
  750. }
  751. }
  752. }
  753.  
  754. $value = get_option( $transient_option );
  755. }
  756.  
  757. return apply_filters( 'transient_' . $transient, $value );
  758. }
  759.  
  760. /**
  761. * Set/update the value of a transient
  762. *
  763. * You do not need to serialize values. If the value needs to be serialized, then
  764. * it will be serialized before it is set.
  765. *
  766. * @since 2.8.0
  767. * @package WordPress
  768. * @subpackage Transient
  769. *
  770. * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
  771. * transient value to be stored.
  772. * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
  773. *
  774. * @param string $transient Transient name. Expected to not be SQL-escaped.
  775. * @param mixed $value Transient value. Expected to not be SQL-escaped.
  776. * @param int $expiration Time until expiration in seconds, default 0
  777. * @return bool False if value was not set and true if value was set.
  778. */
  779. function set_transient( $transient, $value, $expiration = 0 ) {
  780. global $_wp_using_ext_object_cache;
  781.  
  782. $value = apply_filters( 'pre_set_transient_' . $transient, $value );
  783.  
  784. if ( $_wp_using_ext_object_cache ) {
  785. $result = wp_cache_set( $transient, $value, 'transient', $expiration );
  786. } else {
  787. $transient_timeout = '_transient_timeout_' . $transient;
  788. $transient = '_transient_' . $transient;
  789. if ( false === get_option( $transient ) ) {
  790. $autoload = 'yes';
  791. if ( $expiration ) {
  792. $autoload = 'no';
  793. add_option( $transient_timeout, time() + $expiration, '', 'no' );
  794. }
  795. $result = add_option( $transient, $value, '', $autoload );
  796. } else {
  797. if ( $expiration )
  798. update_option( $transient_timeout, time() + $expiration );
  799. $result = update_option( $transient, $value );
  800. }
  801. }
  802. if ( $result ) {
  803. do_action( 'set_transient_' . $transient );
  804. do_action( 'setted_transient', $transient );
  805. }
  806. return $result;
  807. }
  808.  
  809. /**
  810. * Saves and restores user interface settings stored in a cookie.
  811. *
  812. * Checks if the current user-settings cookie is updated and stores it. When no
  813. * cookie exists (different browser used), adds the last saved cookie restoring
  814. * the settings.
  815. *
  816. * @package WordPress
  817. * @subpackage Option
  818. * @since 2.7.0
  819. */
  820. function wp_user_settings() {
  821.  
  822. if ( ! is_admin() )
  823. return;
  824.  
  825. if ( defined('DOING_AJAX') )
  826. return;
  827.  
  828. if ( ! $user = wp_get_current_user() )
  829. return;
  830.  
  831. $settings = get_user_option( 'user-settings', $user->ID );
  832.  
  833. if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
  834. $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
  835.  
  836. if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
  837. if ( $cookie == $settings )
  838. return;
  839.  
  840. $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
  841. $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
  842.  
  843. if ( $saved > $last_time ) {
  844. update_user_option( $user->ID, 'user-settings', $cookie, false );
  845. update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
  846. return;
  847. }
  848. }
  849. }
  850.  
  851. setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
  852. setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
  853. $_COOKIE['wp-settings-' . $user->ID] = $settings;
  854. }
  855.  
  856. /**
  857. * Retrieve user interface setting value based on setting name.
  858. *
  859. * @package WordPress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement