PageRenderTime 74ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/functions.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 4300 lines | 2176 code | 476 blank | 1648 comment | 519 complexity | 9bf6be16656df5268f8f5cb79836d83f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Main WordPress API
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Converts MySQL DATETIME field to user specified date format.
  9. *
  10. * If $dateformatstring has 'G' value, then gmmktime() function will be used to
  11. * make the time. If $dateformatstring is set to 'U', then mktime() function
  12. * will be used to make the time.
  13. *
  14. * The $translate will only be used, if it is set to true and it is by default
  15. * and if the $wp_locale object has the month and weekday set.
  16. *
  17. * @since 0.71
  18. *
  19. * @param string $dateformatstring Either 'G', 'U', or php date format.
  20. * @param string $mysqlstring Time from mysql DATETIME field.
  21. * @param bool $translate Optional. Default is true. Will switch format to locale.
  22. * @return string Date formated by $dateformatstring or locale (if available).
  23. */
  24. function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
  25. $m = $mysqlstring;
  26. if ( empty( $m ) )
  27. return false;
  28. if ( 'G' == $dateformatstring ) {
  29. return strtotime( $m . ' +0000' );
  30. }
  31. $i = strtotime( $m );
  32. if ( 'U' == $dateformatstring )
  33. return $i;
  34. if ( $translate)
  35. return date_i18n( $dateformatstring, $i );
  36. else
  37. return date( $dateformatstring, $i );
  38. }
  39. /**
  40. * Retrieve the current time based on specified type.
  41. *
  42. * The 'mysql' type will return the time in the format for MySQL DATETIME field.
  43. * The 'timestamp' type will return the current timestamp.
  44. *
  45. * If $gmt is set to either '1' or 'true', then both types will use GMT time.
  46. * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
  47. *
  48. * @since 1.0.0
  49. *
  50. * @param string $type Either 'mysql' or 'timestamp'.
  51. * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
  52. * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
  53. */
  54. function current_time( $type, $gmt = 0 ) {
  55. switch ( $type ) {
  56. case 'mysql':
  57. return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
  58. break;
  59. case 'timestamp':
  60. return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
  61. break;
  62. }
  63. }
  64. /**
  65. * Retrieve the date in localized format, based on timestamp.
  66. *
  67. * If the locale specifies the locale month and weekday, then the locale will
  68. * take over the format for the date. If it isn't, then the date format string
  69. * will be used instead.
  70. *
  71. * @since 0.71
  72. *
  73. * @param string $dateformatstring Format to display the date.
  74. * @param int $unixtimestamp Optional. Unix timestamp.
  75. * @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
  76. * @return string The date, translated if locale specifies it.
  77. */
  78. function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
  79. global $wp_locale;
  80. $i = $unixtimestamp;
  81. // Sanity check for PHP 5.1.0-
  82. if ( false === $i || intval($i) < 0 ) {
  83. if ( ! $gmt )
  84. $i = current_time( 'timestamp' );
  85. else
  86. $i = time();
  87. // we should not let date() interfere with our
  88. // specially computed timestamp
  89. $gmt = true;
  90. }
  91. // store original value for language with untypical grammars
  92. // see http://core.trac.wordpress.org/ticket/9396
  93. $req_format = $dateformatstring;
  94. $datefunc = $gmt? 'gmdate' : 'date';
  95. if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
  96. $datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) );
  97. $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
  98. $dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
  99. $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
  100. $datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
  101. $datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) );
  102. $dateformatstring = ' '.$dateformatstring;
  103. $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
  104. $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
  105. $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
  106. $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
  107. $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
  108. $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
  109. $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
  110. }
  111. $j = @$datefunc( $dateformatstring, $i );
  112. // allow plugins to redo this entirely for languages with untypical grammars
  113. $j = apply_filters('date_i18n', $j, $req_format, $i, $gmt);
  114. return $j;
  115. }
  116. /**
  117. * Convert integer number to format based on the locale.
  118. *
  119. * @since 2.3.0
  120. *
  121. * @param int $number The number to convert based on locale.
  122. * @param int $decimals Precision of the number of decimal places.
  123. * @return string Converted number in string format.
  124. */
  125. function number_format_i18n( $number, $decimals = 0 ) {
  126. global $wp_locale;
  127. $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
  128. return apply_filters( 'number_format_i18n', $formatted );
  129. }
  130. /**
  131. * Convert number of bytes largest unit bytes will fit into.
  132. *
  133. * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
  134. * number of bytes to human readable number by taking the number of that unit
  135. * that the bytes will go into it. Supports TB value.
  136. *
  137. * Please note that integers in PHP are limited to 32 bits, unless they are on
  138. * 64 bit architecture, then they have 64 bit size. If you need to place the
  139. * larger size then what PHP integer type will hold, then use a string. It will
  140. * be converted to a double, which should always have 64 bit length.
  141. *
  142. * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
  143. * @link http://en.wikipedia.org/wiki/Byte
  144. *
  145. * @since 2.3.0
  146. *
  147. * @param int|string $bytes Number of bytes. Note max integer size for integers.
  148. * @param int $decimals Precision of number of decimal places. Deprecated.
  149. * @return bool|string False on failure. Number string on success.
  150. */
  151. function size_format( $bytes, $decimals = 0 ) {
  152. $quant = array(
  153. // ========================= Origin ====
  154. 'TB' => 1099511627776, // pow( 1024, 4)
  155. 'GB' => 1073741824, // pow( 1024, 3)
  156. 'MB' => 1048576, // pow( 1024, 2)
  157. 'kB' => 1024, // pow( 1024, 1)
  158. 'B ' => 1, // pow( 1024, 0)
  159. );
  160. foreach ( $quant as $unit => $mag )
  161. if ( doubleval($bytes) >= $mag )
  162. return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
  163. return false;
  164. }
  165. /**
  166. * Get the week start and end from the datetime or date string from mysql.
  167. *
  168. * @since 0.71
  169. *
  170. * @param string $mysqlstring Date or datetime field type from mysql.
  171. * @param int $start_of_week Optional. Start of the week as an integer.
  172. * @return array Keys are 'start' and 'end'.
  173. */
  174. function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
  175. $my = substr( $mysqlstring, 0, 4 ); // Mysql string Year
  176. $mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month
  177. $md = substr( $mysqlstring, 5, 2 ); // Mysql string day
  178. $day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
  179. $weekday = date( 'w', $day ); // The day of the week from the timestamp
  180. if ( !is_numeric($start_of_week) )
  181. $start_of_week = get_option( 'start_of_week' );
  182. if ( $weekday < $start_of_week )
  183. $weekday += 7;
  184. $start = $day - 86400 * ( $weekday - $start_of_week ); // The most recent week start day on or before $day
  185. $end = $start + 604799; // $start + 7 days - 1 second
  186. return compact( 'start', 'end' );
  187. }
  188. /**
  189. * Unserialize value only if it was serialized.
  190. *
  191. * @since 2.0.0
  192. *
  193. * @param string $original Maybe unserialized original, if is needed.
  194. * @return mixed Unserialized data can be any type.
  195. */
  196. function maybe_unserialize( $original ) {
  197. if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
  198. return @unserialize( $original );
  199. return $original;
  200. }
  201. /**
  202. * Check value to find if it was serialized.
  203. *
  204. * If $data is not an string, then returned value will always be false.
  205. * Serialized data is always a string.
  206. *
  207. * @since 2.0.5
  208. *
  209. * @param mixed $data Value to check to see if was serialized.
  210. * @return bool False if not serialized and true if it was.
  211. */
  212. function is_serialized( $data ) {
  213. // if it isn't a string, it isn't serialized
  214. if ( !is_string( $data ) )
  215. return false;
  216. $data = trim( $data );
  217. if ( 'N;' == $data )
  218. return true;
  219. if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
  220. return false;
  221. switch ( $badions[1] ) {
  222. case 'a' :
  223. case 'O' :
  224. case 's' :
  225. if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
  226. return true;
  227. break;
  228. case 'b' :
  229. case 'i' :
  230. case 'd' :
  231. if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
  232. return true;
  233. break;
  234. }
  235. return false;
  236. }
  237. /**
  238. * Check whether serialized data is of string type.
  239. *
  240. * @since 2.0.5
  241. *
  242. * @param mixed $data Serialized data
  243. * @return bool False if not a serialized string, true if it is.
  244. */
  245. function is_serialized_string( $data ) {
  246. // if it isn't a string, it isn't a serialized string
  247. if ( !is_string( $data ) )
  248. return false;
  249. $data = trim( $data );
  250. if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
  251. return true;
  252. return false;
  253. }
  254. /**
  255. * Retrieve option value based on name of option.
  256. *
  257. * If the option does not exist or does not have a value, then the return value
  258. * will be false. This is useful to check whether you need to install an option
  259. * and is commonly used during installation of plugin options and to test
  260. * whether upgrading is required.
  261. *
  262. * If the option was serialized then it will be unserialized when it is returned.
  263. *
  264. * @since 1.5.0
  265. * @package WordPress
  266. * @subpackage Option
  267. * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
  268. * Any value other than false will "short-circuit" the retrieval of the option
  269. * and return the returned value. You should not try to override special options,
  270. * but you will not be prevented from doing so.
  271. * @uses apply_filters() Calls 'option_$option', after checking the option, with
  272. * the option value.
  273. *
  274. * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  275. * @return mixed Value set for the option.
  276. */
  277. function get_option( $option, $default = false ) {
  278. global $wpdb;
  279. // Allow plugins to short-circuit options.
  280. $pre = apply_filters( 'pre_option_' . $option, false );
  281. if ( false !== $pre )
  282. return $pre;
  283. $option = trim($option);
  284. if ( empty($option) )
  285. return false;
  286. if ( defined( 'WP_SETUP_CONFIG' ) )
  287. return false;
  288. if ( ! defined( 'WP_INSTALLING' ) ) {
  289. // prevent non-existent options from triggering multiple queries
  290. $notoptions = wp_cache_get( 'notoptions', 'options' );
  291. if ( isset( $notoptions[$option] ) )
  292. return $default;
  293. $alloptions = wp_load_alloptions();
  294. if ( isset( $alloptions[$option] ) ) {
  295. $value = $alloptions[$option];
  296. } else {
  297. $value = wp_cache_get( $option, 'options' );
  298. if ( false === $value ) {
  299. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
  300. // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  301. if ( is_object( $row ) ) {
  302. $value = $row->option_value;
  303. wp_cache_add( $option, $value, 'options' );
  304. } else { // option does not exist, so we must cache its non-existence
  305. $notoptions[$option] = true;
  306. wp_cache_set( 'notoptions', $notoptions, 'options' );
  307. return $default;
  308. }
  309. }
  310. }
  311. } else {
  312. $suppress = $wpdb->suppress_errors();
  313. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
  314. $wpdb->suppress_errors( $suppress );
  315. if ( is_object( $row ) )
  316. $value = $row->option_value;
  317. else
  318. return $default;
  319. }
  320. // If home is not set use siteurl.
  321. if ( 'home' == $option && '' == $value )
  322. return get_option( 'siteurl' );
  323. if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
  324. $value = untrailingslashit( $value );
  325. return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
  326. }
  327. /**
  328. * Protect WordPress special option from being modified.
  329. *
  330. * Will die if $option is in protected list. Protected options are 'alloptions'
  331. * and 'notoptions' options.
  332. *
  333. * @since 2.2.0
  334. * @package WordPress
  335. * @subpackage Option
  336. *
  337. * @param string $option Option name.
  338. */
  339. function wp_protect_special_option( $option ) {
  340. $protected = array( 'alloptions', 'notoptions' );
  341. if ( in_array( $option, $protected ) )
  342. wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
  343. }
  344. /**
  345. * Print option value after sanitizing for forms.
  346. *
  347. * @uses attr Sanitizes value.
  348. * @since 1.5.0
  349. * @package WordPress
  350. * @subpackage Option
  351. *
  352. * @param string $option Option name.
  353. */
  354. function form_option( $option ) {
  355. echo esc_attr( get_option( $option ) );
  356. }
  357. /**
  358. * Loads and caches all autoloaded options, if available or all options.
  359. *
  360. * @since 2.2.0
  361. * @package WordPress
  362. * @subpackage Option
  363. *
  364. * @return array List of all options.
  365. */
  366. function wp_load_alloptions() {
  367. global $wpdb;
  368. if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
  369. $alloptions = wp_cache_get( 'alloptions', 'options' );
  370. else
  371. $alloptions = false;
  372. if ( !$alloptions ) {
  373. $suppress = $wpdb->suppress_errors();
  374. if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
  375. $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
  376. $wpdb->suppress_errors($suppress);
  377. $alloptions = array();
  378. foreach ( (array) $alloptions_db as $o )
  379. $alloptions[$o->option_name] = $o->option_value;
  380. if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
  381. wp_cache_add( 'alloptions', $alloptions, 'options' );
  382. }
  383. return $alloptions;
  384. }
  385. /**
  386. * Loads and caches certain often requested site options if is_multisite() and a peristent cache is not being used.
  387. *
  388. * @since 3.0.0
  389. * @package WordPress
  390. * @subpackage Option
  391. *
  392. * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
  393. */
  394. function wp_load_core_site_options( $site_id = null ) {
  395. global $wpdb, $_wp_using_ext_object_cache;
  396. if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
  397. return;
  398. if ( empty($site_id) )
  399. $site_id = $wpdb->siteid;
  400. $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' );
  401. $core_options_in = "'" . implode("', '", $core_options) . "'";
  402. $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) );
  403. foreach ( $options as $option ) {
  404. $key = $option->meta_key;
  405. $cache_key = "{$site_id}:$key";
  406. $option->meta_value = maybe_unserialize( $option->meta_value );
  407. wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
  408. }
  409. }
  410. /**
  411. * Update the value of an option that was already added.
  412. *
  413. * You do not need to serialize values. If the value needs to be serialized, then
  414. * it will be serialized before it is inserted into the database. Remember,
  415. * resources can not be serialized or added as an option.
  416. *
  417. * If the option does not exist, then the option will be added with the option
  418. * value, but you will not be able to set whether it is autoloaded. If you want
  419. * to set whether an option is autoloaded, then you need to use the add_option().
  420. *
  421. * @since 1.0.0
  422. * @package WordPress
  423. * @subpackage Option
  424. *
  425. * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
  426. * option value to be stored.
  427. * @uses do_action() Calls 'update_option' hook before updating the option.
  428. * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
  429. *
  430. * @param string $option Option name. Expected to not be SQL-escaped.
  431. * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
  432. * @return bool False if value was not updated and true if value was updated.
  433. */
  434. function update_option( $option, $newvalue ) {
  435. global $wpdb;
  436. $option = trim($option);
  437. if ( empty($option) )
  438. return false;
  439. wp_protect_special_option( $option );
  440. if ( is_object($newvalue) )
  441. $newvalue = wp_clone($newvalue);
  442. $newvalue = sanitize_option( $option, $newvalue );
  443. $oldvalue = get_option( $option );
  444. $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
  445. // If the new and old values are the same, no need to update.
  446. if ( $newvalue === $oldvalue )
  447. return false;
  448. if ( false === $oldvalue )
  449. return add_option( $option, $newvalue );
  450. $notoptions = wp_cache_get( 'notoptions', 'options' );
  451. if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  452. unset( $notoptions[$option] );
  453. wp_cache_set( 'notoptions', $notoptions, 'options' );
  454. }
  455. $_newvalue = $newvalue;
  456. $newvalue = maybe_serialize( $newvalue );
  457. do_action( 'update_option', $option, $oldvalue, $_newvalue );
  458. if ( ! defined( 'WP_INSTALLING' ) ) {
  459. $alloptions = wp_load_alloptions();
  460. if ( isset( $alloptions[$option] ) ) {
  461. $alloptions[$option] = $_newvalue;
  462. wp_cache_set( 'alloptions', $alloptions, 'options' );
  463. } else {
  464. wp_cache_set( $option, $_newvalue, 'options' );
  465. }
  466. }
  467. $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
  468. if ( $result ) {
  469. do_action( "update_option_{$option}", $oldvalue, $_newvalue );
  470. do_action( 'updated_option', $option, $oldvalue, $_newvalue );
  471. return true;
  472. }
  473. return false;
  474. }
  475. /**
  476. * Add a new option.
  477. *
  478. * You do not need to serialize values. If the value needs to be serialized, then
  479. * it will be serialized before it is inserted into the database. Remember,
  480. * resources can not be serialized or added as an option.
  481. *
  482. * You can create options without values and then add values later. Does not
  483. * check whether the option has already been added, but does check that you
  484. * aren't adding a protected WordPress option. Care should be taken to not name
  485. * options the same as the ones which are protected and to not add options
  486. * that were already added.
  487. *
  488. * @package WordPress
  489. * @subpackage Option
  490. * @since 1.0.0
  491. * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
  492. *
  493. * @uses do_action() Calls 'add_option' hook before adding the option.
  494. * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
  495. *
  496. * @param string $option Name of option to add. Expected to not be SQL-escaped.
  497. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  498. * @param mixed $deprecated Optional. Description. Not used anymore.
  499. * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
  500. * @return null returns when finished.
  501. */
  502. function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
  503. global $wpdb;
  504. if ( !empty( $deprecated ) )
  505. _deprecated_argument( __FUNCTION__, '2.3' );
  506. $option = trim($option);
  507. if ( empty($option) )
  508. return false;
  509. wp_protect_special_option( $option );
  510. if ( is_object($value) )
  511. $value = wp_clone($value);
  512. $value = sanitize_option( $option, $value );
  513. // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
  514. $notoptions = wp_cache_get( 'notoptions', 'options' );
  515. if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
  516. if ( false !== get_option( $option ) )
  517. return;
  518. $_value = $value;
  519. $value = maybe_serialize( $value );
  520. $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
  521. do_action( 'add_option', $option, $_value );
  522. if ( ! defined( 'WP_INSTALLING' ) ) {
  523. if ( 'yes' == $autoload ) {
  524. $alloptions = wp_load_alloptions();
  525. $alloptions[$option] = $value;
  526. wp_cache_set( 'alloptions', $alloptions, 'options' );
  527. } else {
  528. wp_cache_set( $option, $value, 'options' );
  529. }
  530. }
  531. // This option exists now
  532. $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
  533. if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  534. unset( $notoptions[$option] );
  535. wp_cache_set( 'notoptions', $notoptions, 'options' );
  536. }
  537. $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 ) );
  538. if ( $result ) {
  539. do_action( "add_option_{$option}", $option, $_value );
  540. do_action( 'added_option', $option, $_value );
  541. return true;
  542. }
  543. return false;
  544. }
  545. /**
  546. * Removes option by name. Prevents removal of protected WordPress options.
  547. *
  548. * @package WordPress
  549. * @subpackage Option
  550. * @since 1.2.0
  551. *
  552. * @uses do_action() Calls 'delete_option' hook before option is deleted.
  553. * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
  554. *
  555. * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  556. * @return bool True, if option is successfully deleted. False on failure.
  557. */
  558. function delete_option( $option ) {
  559. global $wpdb;
  560. wp_protect_special_option( $option );
  561. // Get the ID, if no ID then return
  562. $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
  563. if ( is_null( $row ) )
  564. return false;
  565. do_action( 'delete_option', $option );
  566. $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $option) );
  567. if ( ! defined( 'WP_INSTALLING' ) ) {
  568. if ( 'yes' == $row->autoload ) {
  569. $alloptions = wp_load_alloptions();
  570. if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
  571. unset( $alloptions[$option] );
  572. wp_cache_set( 'alloptions', $alloptions, 'options' );
  573. }
  574. } else {
  575. wp_cache_delete( $option, 'options' );
  576. }
  577. }
  578. if ( $result ) {
  579. do_action( "delete_option_$option", $option );
  580. do_action( 'deleted_option', $option );
  581. return true;
  582. }
  583. return false;
  584. }
  585. /**
  586. * Delete a transient
  587. *
  588. * @since 2.8.0
  589. * @package WordPress
  590. * @subpackage Transient
  591. *
  592. * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
  593. * @uses do_action() Calls 'deleted_transient' hook on success.
  594. *
  595. * @param string $transient Transient name. Expected to not be SQL-escaped.
  596. * @return bool true if successful, false otherwise
  597. */
  598. function delete_transient( $transient ) {
  599. global $_wp_using_ext_object_cache;
  600. do_action( 'delete_transient_' . $transient, $transient );
  601. if ( $_wp_using_ext_object_cache ) {
  602. $result = wp_cache_delete( $transient, 'transient' );
  603. } else {
  604. $option_timeout = '_transient_timeout_' . $transient;
  605. $option = '_transient_' . $transient;
  606. $result = delete_option( $option );
  607. if ( $result )
  608. delete_option( $option_timeout );
  609. }
  610. if ( $result )
  611. do_action( 'deleted_transient', $transient );
  612. return $result;
  613. }
  614. /**
  615. * Get the value of a transient
  616. *
  617. * If the transient does not exist or does not have a value, then the return value
  618. * will be false.
  619. *
  620. * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
  621. * Any value other than false will "short-circuit" the retrieval of the transient
  622. * and return the returned value.
  623. * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
  624. * the transient value.
  625. *
  626. * @since 2.8.0
  627. * @package WordPress
  628. * @subpackage Transient
  629. *
  630. * @param string $transient Transient name. Expected to not be SQL-escaped
  631. * @return mixed Value of transient
  632. */
  633. function get_transient( $transient ) {
  634. global $_wp_using_ext_object_cache;
  635. $pre = apply_filters( 'pre_transient_' . $transient, false );
  636. if ( false !== $pre )
  637. return $pre;
  638. if ( $_wp_using_ext_object_cache ) {
  639. $value = wp_cache_get( $transient, 'transient' );
  640. } else {
  641. $transient_option = '_transient_' . $transient;
  642. if ( ! defined( 'WP_INSTALLING' ) ) {
  643. // If option is not in alloptions, it is not autoloaded and thus has a timeout
  644. $alloptions = wp_load_alloptions();
  645. if ( !isset( $alloptions[$transient_option] ) ) {
  646. $transient_timeout = '_transient_timeout_' . $transient;
  647. if ( get_option( $transient_timeout ) < time() ) {
  648. delete_option( $transient_option );
  649. delete_option( $transient_timeout );
  650. return false;
  651. }
  652. }
  653. }
  654. $value = get_option( $transient_option );
  655. }
  656. return apply_filters( 'transient_' . $transient, $value );
  657. }
  658. /**
  659. * Set/update the value of a transient
  660. *
  661. * You do not need to serialize values. If the value needs to be serialized, then
  662. * it will be serialized before it is set.
  663. *
  664. * @since 2.8.0
  665. * @package WordPress
  666. * @subpackage Transient
  667. *
  668. * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
  669. * transient value to be stored.
  670. * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
  671. *
  672. * @param string $transient Transient name. Expected to not be SQL-escaped.
  673. * @param mixed $value Transient value. Expected to not be SQL-escaped.
  674. * @param int $expiration Time until expiration in seconds, default 0
  675. * @return bool False if value was not set and true if value was set.
  676. */
  677. function set_transient( $transient, $value, $expiration = 0 ) {
  678. global $_wp_using_ext_object_cache;
  679. $value = apply_filters( 'pre_set_transient_' . $transient, $value );
  680. if ( $_wp_using_ext_object_cache ) {
  681. $result = wp_cache_set( $transient, $value, 'transient', $expiration );
  682. } else {
  683. $transient_timeout = '_transient_timeout_' . $transient;
  684. $transient = '_transient_' . $transient;
  685. if ( false === get_option( $transient ) ) {
  686. $autoload = 'yes';
  687. if ( $expiration ) {
  688. $autoload = 'no';
  689. add_option( $transient_timeout, time() + $expiration, '', 'no' );
  690. }
  691. $result = add_option( $transient, $value, '', $autoload );
  692. } else {
  693. if ( $expiration )
  694. update_option( $transient_timeout, time() + $expiration );
  695. $result = update_option( $transient, $value );
  696. }
  697. }
  698. if ( $result ) {
  699. do_action( 'set_transient_' . $transient );
  700. do_action( 'setted_transient', $transient );
  701. }
  702. return $result;
  703. }
  704. /**
  705. * Saves and restores user interface settings stored in a cookie.
  706. *
  707. * Checks if the current user-settings cookie is updated and stores it. When no
  708. * cookie exists (different browser used), adds the last saved cookie restoring
  709. * the settings.
  710. *
  711. * @package WordPress
  712. * @subpackage Option
  713. * @since 2.7.0
  714. */
  715. function wp_user_settings() {
  716. if ( ! is_admin() )
  717. return;
  718. if ( defined('DOING_AJAX') )
  719. return;
  720. if ( ! $user = wp_get_current_user() )
  721. return;
  722. $settings = get_user_option( 'user-settings', $user->ID );
  723. if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
  724. $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
  725. if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
  726. if ( $cookie == $settings )
  727. return;
  728. $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
  729. $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
  730. if ( $saved > $last_time ) {
  731. update_user_option( $user->ID, 'user-settings', $cookie, false );
  732. update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
  733. return;
  734. }
  735. }
  736. }
  737. setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
  738. setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
  739. $_COOKIE['wp-settings-' . $user->ID] = $settings;
  740. }
  741. /**
  742. * Retrieve user interface setting value based on setting name.
  743. *
  744. * @package WordPress
  745. * @subpackage Option
  746. * @since 2.7.0
  747. *
  748. * @param string $name The name of the setting.
  749. * @param string $default Optional default value to return when $name is not set.
  750. * @return mixed the last saved user setting or the default value/false if it doesn't exist.
  751. */
  752. function get_user_setting( $name, $default = false ) {
  753. $all = get_all_user_settings();
  754. return isset($all[$name]) ? $all[$name] : $default;
  755. }
  756. /**
  757. * Add or update user interface setting.
  758. *
  759. * Both $name and $value can contain only ASCII letters, numbers and underscores.
  760. * This function has to be used before any output has started as it calls setcookie().
  761. *
  762. * @package WordPress
  763. * @subpackage Option
  764. * @since 2.8.0
  765. *
  766. * @param string $name The name of the setting.
  767. * @param string $value The value for the setting.
  768. * @return bool true if set successfully/false if not.
  769. */
  770. function set_user_setting( $name, $value ) {
  771. if ( headers_sent() )
  772. return false;
  773. $all = get_all_user_settings();
  774. $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
  775. if ( empty($name) )
  776. return false;
  777. $all[$name] = $value;
  778. return wp_set_all_user_settings($all);
  779. }
  780. /**
  781. * Delete user interface settings.
  782. *
  783. * Deleting settings would reset them to the defaults.
  784. * This function has to be used before any output has started as it calls setcookie().
  785. *
  786. * @package WordPress
  787. * @subpackage Option
  788. * @since 2.7.0
  789. *
  790. * @param mixed $names The name or array of names of the setting to be deleted.
  791. * @return bool true if deleted successfully/false if not.
  792. */
  793. function delete_user_setting( $names ) {
  794. if ( headers_sent() )
  795. return false;
  796. $all = get_all_user_settings();
  797. $names = (array) $names;
  798. foreach ( $names as $name ) {
  799. if ( isset($all[$name]) ) {
  800. unset($all[$name]);
  801. $deleted = true;
  802. }
  803. }
  804. if ( isset($deleted) )
  805. return wp_set_all_user_settings($all);
  806. return false;
  807. }
  808. /**
  809. * Retrieve all user interface settings.
  810. *
  811. * @package WordPress
  812. * @subpackage Option
  813. * @since 2.7.0
  814. *
  815. * @return array the last saved user settings or empty array.
  816. */
  817. function get_all_user_settings() {
  818. global $_updated_user_settings;
  819. if ( ! $user = wp_get_current_user() )
  820. return array();
  821. if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
  822. return $_updated_user_settings;
  823. $all = array();
  824. if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
  825. $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
  826. if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
  827. parse_str($cookie, $all);
  828. } else {
  829. $option = get_user_option('user-settings', $user->ID);
  830. if ( $option && is_string($option) )
  831. parse_str( $option, $all );
  832. }
  833. return $all;
  834. }
  835. /**
  836. * Private. Set all user interface settings.
  837. *
  838. * @package WordPress
  839. * @subpackage Option
  840. * @since 2.8.0
  841. *
  842. * @param unknown $all
  843. * @return bool
  844. */
  845. function wp_set_all_user_settings($all) {
  846. global $_updated_user_settings;
  847. if ( ! $user = wp_get_current_user() )
  848. return false;
  849. $_updated_user_settings = $all;
  850. $settings = '';
  851. foreach ( $all as $k => $v ) {
  852. $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
  853. $settings .= $k . '=' . $v . '&';
  854. }
  855. $settings = rtrim($settings, '&');
  856. update_user_option( $user->ID, 'user-settings', $settings, false );
  857. update_user_option( $user->ID, 'user-settings-time', time(), false );
  858. return true;
  859. }
  860. /**
  861. * Delete the user settings of the current user.
  862. *
  863. * @package WordPress
  864. * @subpackage Option
  865. * @since 2.7.0
  866. */
  867. function delete_all_user_settings() {
  868. if ( ! $user = wp_get_current_user() )
  869. return;
  870. update_user_option( $user->ID, 'user-settings', '', false );
  871. setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
  872. }
  873. /**
  874. * Serialize data, if needed.
  875. *
  876. * @since 2.0.5
  877. *
  878. * @param mixed $data Data that might be serialized.
  879. * @return mixed A scalar data
  880. */
  881. function maybe_serialize( $data ) {
  882. if ( is_array( $data ) || is_object( $data ) )
  883. return serialize( $data );
  884. if ( is_serialized( $data ) )
  885. return serialize( $data );
  886. return $data;
  887. }
  888. /**
  889. * Retrieve post title from XMLRPC XML.
  890. *
  891. * If the title element is not part of the XML, then the default post title from
  892. * the $post_default_title will be used instead.
  893. *
  894. * @package WordPress
  895. * @subpackage XMLRPC
  896. * @since 0.71
  897. *
  898. * @global string $post_default_title Default XMLRPC post title.
  899. *
  900. * @param string $content XMLRPC XML Request content
  901. * @return string Post title
  902. */
  903. function xmlrpc_getposttitle( $content ) {
  904. global $post_default_title;
  905. if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
  906. $post_title = $matchtitle[1];
  907. } else {
  908. $post_title = $post_default_title;
  909. }
  910. return $post_title;
  911. }
  912. /**
  913. * Retrieve the post category or categories from XMLRPC XML.
  914. *
  915. * If the category element is not found, then the default post category will be
  916. * used. The return type then would be what $post_default_category. If the
  917. * category is found, then it will always be an array.
  918. *
  919. * @package WordPress
  920. * @subpackage XMLRPC
  921. * @since 0.71
  922. *
  923. * @global string $post_default_category Default XMLRPC post category.
  924. *
  925. * @param string $content XMLRPC XML Request content
  926. * @return string|array List of categories or category name.
  927. */
  928. function xmlrpc_getpostcategory( $content ) {
  929. global $post_default_category;
  930. if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
  931. $post_category = trim( $matchcat[1], ',' );
  932. $post_category = explode( ',', $post_category );
  933. } else {
  934. $post_category = $post_default_category;
  935. }
  936. return $post_category;
  937. }
  938. /**
  939. * XMLRPC XML content without title and category elements.
  940. *
  941. * @package WordPress
  942. * @subpackage XMLRPC
  943. * @since 0.71
  944. *
  945. * @param string $content XMLRPC XML Request content
  946. * @return string XMLRPC XML Request content without title and category elements.
  947. */
  948. function xmlrpc_removepostdata( $content ) {
  949. $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
  950. $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
  951. $content = trim( $content );
  952. return $content;
  953. }
  954. /**
  955. * Open the file handle for debugging.
  956. *
  957. * This function is used for XMLRPC feature, but it is general purpose enough
  958. * to be used in anywhere.
  959. *
  960. * @see fopen() for mode options.
  961. * @package WordPress
  962. * @subpackage Debug
  963. * @since 0.71
  964. * @uses $debug Used for whether debugging is enabled.
  965. *
  966. * @param string $filename File path to debug file.
  967. * @param string $mode Same as fopen() mode parameter.
  968. * @return bool|resource File handle. False on failure.
  969. */
  970. function debug_fopen( $filename, $mode ) {
  971. global $debug;
  972. if ( 1 == $debug ) {
  973. $fp = fopen( $filename, $mode );
  974. return $fp;
  975. } else {
  976. return false;
  977. }
  978. }
  979. /**
  980. * Write contents to the file used for debugging.
  981. *
  982. * Technically, this can be used to write to any file handle when the global
  983. * $debug is set to 1 or true.
  984. *
  985. * @package WordPress
  986. * @subpackage Debug
  987. * @since 0.71
  988. * @uses $debug Used for whether debugging is enabled.
  989. *
  990. * @param resource $fp File handle for debugging file.
  991. * @param string $string Content to write to debug file.
  992. */
  993. function debug_fwrite( $fp, $string ) {
  994. global $debug;
  995. if ( 1 == $debug )
  996. fwrite( $fp, $string );
  997. }
  998. /**
  999. * Close the debugging file handle.
  1000. *
  1001. * Technically, this can be used to close any file handle when the global $debug
  1002. * is set to 1 or true.
  1003. *
  1004. * @package WordPress
  1005. * @subpackage Debug
  1006. * @since 0.71
  1007. * @uses $debug Used for whether debugging is enabled.
  1008. *
  1009. * @param resource $fp Debug File handle.
  1010. */
  1011. function debug_fclose( $fp ) {
  1012. global $debug;
  1013. if ( 1 == $debug )
  1014. fclose( $fp );
  1015. }
  1016. /**
  1017. * Check content for video and audio links to add as enclosures.
  1018. *
  1019. * Will not add enclosures that have already been added and will
  1020. * remove enclosures that are no longer in the post. This is called as
  1021. * pingbacks and trackbacks.
  1022. *
  1023. * @package WordPress
  1024. * @since 1.5.0
  1025. *
  1026. * @uses $wpdb
  1027. *
  1028. * @param string $content Post Content
  1029. * @param int $post_ID Post ID
  1030. */
  1031. function do_enclose( $content, $post_ID ) {
  1032. global $wpdb;
  1033. include_once( ABSPATH . WPINC . '/class-IXR.php' );
  1034. $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' );
  1035. $post_links = array();
  1036. debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" );
  1037. $pung = get_enclosed( $post_ID );
  1038. $ltrs = '\w';
  1039. $gunk = '/#~:.?+=&%@!\-';
  1040. $punc = '.:?\-';
  1041. $any = $ltrs . $gunk . $punc;
  1042. preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
  1043. debug_fwrite( $log, 'Post contents:' );
  1044. debug_fwrite( $log, $content . "\n" );
  1045. foreach ( $pung as $link_test ) {
  1046. if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
  1047. $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 . '%') );
  1048. do_action( 'delete_postmeta', $mid );
  1049. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
  1050. do_action( 'deleted_postmeta', $mid );
  1051. }
  1052. }
  1053. foreach ( (array) $post_links_temp[0] as $link_test ) {
  1054. if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
  1055. $test = @parse_url( $link_test );
  1056. if ( false === $test )
  1057. continue;
  1058. if ( isset( $test['query'] ) )
  1059. $post_links[] = $link_test;
  1060. elseif ( $test['path'] != '/' && $test['path'] != '' )
  1061. $post_links[] = $link_test;
  1062. }
  1063. }
  1064. foreach ( (array) $post_links as $url ) {
  1065. 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 . '%' ) ) ) {
  1066. if ( $headers = wp_get_http_headers( $url) ) {
  1067. $len = (int) $headers['content-length'];
  1068. $type = $headers['content-type'];
  1069. $allowed_types = array( 'video', 'audio' );
  1070. // Check to see if we can figure out the mime type from
  1071. // the extension
  1072. $url_parts = @parse_url( $url );
  1073. if ( false !== $url_parts ) {
  1074. $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
  1075. if ( !empty( $extension ) ) {
  1076. foreach ( get_allowed_mime_types( ) as $exts => $mime ) {
  1077. if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
  1078. $type = $mime;
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
  1085. $meta_value = "$url\n$len\n$type\n";
  1086. $wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
  1087. do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. /**
  1094. * Perform a HTTP HEAD or GET request.
  1095. *
  1096. * If $file_path is a writable filename, this will do a GET request and write
  1097. * the file to that path.
  1098. *
  1099. * @since 2.5.0
  1100. *
  1101. * @param string $url URL to fetch.
  1102. * @param string|bool $file_path Optional. File path to write request to.
  1103. * @param int $red (private) The number of Redirects followed, Upon 5 being hit, returns false.
  1104. * @return bool|string False on failure and string of headers if HEAD request.
  1105. */
  1106. function wp_get_http( $url, $file_path = false, $red = 1 ) {
  1107. @set_time_limit( 60 );
  1108. if ( $red > 5 )
  1109. return false;
  1110. $options = array();
  1111. $options['redirection'] = 5;
  1112. if ( false == $file_path )
  1113. $options['method'] = 'HEAD';
  1114. else
  1115. $options['method'] = 'GET';
  1116. $response = wp_remote_request($url, $options);
  1117. if ( is_wp_error( $response ) )
  1118. return false;
  1119. $headers = wp_remote_retrieve_headers( $response );
  1120. $headers['response'] = $response['response']['code'];
  1121. // WP_HTTP no longer follows redirects for HEAD requests.
  1122. if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {
  1123. return wp_get_http( $headers['location'], $file_path, ++$red );
  1124. }
  1125. if ( false == $file_path )
  1126. return $headers;
  1127. // GET request - write it to the supplied filename
  1128. $out_fp = fopen($file_path, 'w');
  1129. if ( !$out_fp )
  1130. return $headers;
  1131. fwrite( $out_fp, $response['body']);
  1132. fclose($out_fp);
  1133. clearstatcache();
  1134. return $headers;
  1135. }
  1136. /**
  1137. * Retrieve HTTP Headers from URL.
  1138. *
  1139. * @since 1.5.1
  1140. *
  1141. * @param string $url
  1142. * @param bool $deprecated Not Used.
  1143. * @return bool|string False on failure, headers on success.
  1144. */
  1145. function wp_get_http_headers( $url, $deprecated = false ) {
  1146. if ( !empty( $deprecated ) )
  1147. _deprecated_argument( __FUNCTION__, '2.7' );
  1148. $response = wp_remote_head( $url );
  1149. if ( is_wp_error( $response ) )
  1150. return false;
  1151. return wp_remote_retrieve_headers( $response );
  1152. }
  1153. /**
  1154. * Whether today is a new day.
  1155. *
  1156. * @since 0.71
  1157. * @uses $day Today
  1158. * @uses $previousday Previous day
  1159. *
  1160. * @return int 1 when new day, 0 if not a new day.
  1161. */
  1162. function is_new_day() {
  1163. global $day, $previousday;
  1164. if ( $day != $previousday )
  1165. return 1;
  1166. else
  1167. return 0;
  1168. }
  1169. /**
  1170. * Build URL query based on an associative and, or indexed array.
  1171. *
  1172. * This is a convenient function for easily building url queries. It sets the
  1173. * separator to '&' and uses _http_build_query() function.
  1174. *
  1175. * @see _http_build_query() Used to build the query
  1176. * @link http://us2.php.net/manual/en/function.http-build-query.php more on what
  1177. * http_build_query() does.
  1178. *
  1179. * @since 2.3.0
  1180. *
  1181. * @param array $data URL-encode key/value pairs.
  1182. * @return string URL encoded string
  1183. */
  1184. function build_query( $data ) {
  1185. return _http_build_query( $data, null, '&', '', false );
  1186. }
  1187. /**
  1188. * Retrieve a modified URL query string.
  1189. *
  1190. * You can rebuild the URL and append a new query variable to the URL query by
  1191. * using this function. You can also retrieve the full URL with query data.
  1192. *
  1193. * Adding a single key & value or an associative array. Setting a key value to
  1194. * emptystring removes the key. Omitting oldquery_or_uri uses the $_SERVER
  1195. * value.
  1196. *
  1197. * @since 1.5.0
  1198. *
  1199. * @param mixed $param1 Either newkey or an associative_array
  1200. * @param mixed $param2 Either newvalue or oldquery or uri
  1201. * @param mixed $param3 Optional. Old query or uri
  1202. * @return string New URL query string.
  1203. */
  1204. function add_query_arg() {
  1205. $ret = '';
  1206. if ( is_array( func_get_arg(0) ) ) {
  1207. if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
  1208. $uri = $_SERVER['REQUEST_URI'];
  1209. else
  1210. $uri = @func_get_arg( 1 );
  1211. } else {
  1212. if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
  1213. $uri = $_SERVER['REQUEST_URI'];
  1214. else
  1215. $uri = @func_get_arg( 2 );
  1216. }
  1217. if ( $frag = strstr( $uri, '#' ) )
  1218. $uri = substr( $uri, 0, -strlen( $frag ) );
  1219. else
  1220. $frag = '';
  1221. if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
  1222. $protocol = $matches[0];
  1223. $uri = substr( $uri, strlen( $protocol ) );
  1224. } else {
  1225. $protocol = '';
  1226. }
  1227. if ( strpos( $uri, '?' ) !== false ) {
  1228. $parts = explode( '?', $uri, 2 );
  1229. if ( 1 == count( $parts ) ) {
  1230. $base = '?';
  1231. $query = $parts[0];
  1232. } else {
  1233. $base = $parts[0] . '?';
  1234. $query = $parts[1];
  1235. }
  1236. } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
  1237. $base = $uri . '?';
  1238. $query = '';
  1239. } else {
  1240. $base = '';
  1241. $query = $uri;
  1242. }
  1243. wp_parse_str( $query, $qs );
  1244. $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
  1245. if ( is_array( func_get_arg( 0 ) ) ) {
  1246. $kayvees = func_get_arg( 0 );
  1247. $qs = array_merge( $qs, $kayvees );
  1248. } else {
  1249. $qs[func_get_arg( 0 )] = func_get_arg( 1 );
  1250. }
  1251. foreach ( (array) $qs as $k => $v ) {
  1252. if ( $v === false )
  1253. unset( $qs[$k] );
  1254. }
  1255. $ret = build_query( $qs );
  1256. $ret = trim( $ret, '?' );
  1257. $ret = preg_replace( '#=(&|$)#', '$1', $ret );
  1258. $ret = $protocol . $base . $ret . $frag;
  1259. $ret = rtrim( $ret, '?' );
  1260. return $ret;
  1261. }
  1262. /**
  1263. * Removes an item or list from the query string.
  1264. *
  1265. * @since 1.5.0
  1266. *
  1267. * @param string|array $key Query key or keys to remove.
  1268. * @param bool $query When false uses the $_SERVER value.
  1269. * @return string New URL query string.
  1270. */
  1271. function remove_query_arg( $key, $query=false ) {
  1272. if ( is_array( $key ) ) { // removing multiple keys
  1273. foreach ( $key as $k )
  1274. $query = add_query_arg( $k, false, $query );
  1275. return $query;
  1276. }
  1277. return add_query_arg( $key, false, $query );
  1278. }
  1279. /**
  1280. * Walks the array while sanitizing the contents.
  1281. *
  1282. * @since 0.71
  1283. *
  1284. * @param array $array Array to used to walk while sanitizing contents.
  1285. * @return array Sanitized $array.
  1286. */
  1287. function add_magic_quotes( $array ) {
  1288. foreach ( (array) $array as $k => $v ) {
  1289. if ( is_array( $v ) ) {
  1290. $array[$k] = add_magic_quotes( $v );
  1291. } else {
  1292. $array[$k] = addslashes( $v );
  1293. }
  1294. }
  1295. return $array;
  1296. }
  1297. /**
  1298. * HTTP request for URI to retrieve content.
  1299. *
  1300. * @since 1.5.1
  1301. * @uses wp_remote_get()
  1302. *
  1303. * @param string $uri URI/URL of web page to retrieve.
  1304. * @return bool|string HTTP content. False on failure.
  1305. */
  1306. function wp_remote_fopen( $uri ) {
  1307. $parsed_url = @parse_url( $uri );
  1308. if ( !$parsed_url || !is_array( $parsed_url ) )
  1309. return false;
  1310. $options = array();
  1311. $options['timeout'] = 10;
  1312. $response = wp_remote_get( $uri, $options );
  1313. if ( is_wp_error( $response ) )
  1314. return false;
  1315. return $response['body'];
  1316. }
  1317. /**
  1318. * Set up the WordPress query.
  1319. *
  1320. * @since 2.0.0
  1321. *
  1322. * @param string $query_vars Default WP_Query arguments.
  1323. */
  1324. function wp( $query_vars = '' ) {
  1325. global $wp, $wp_query, $wp_the_query;
  1326. $wp->main( $query_vars );
  1327. if ( !isset($wp_the_query) )
  1328. $wp_the_query = $wp_query;
  1329. }
  1330. /**
  1331. * Retrieve the description for the HTTP status.
  1332. *
  1333. * @since 2.3.0
  1334. *
  1335. * @param int $code HTTP status code.
  1336. * @return string Empty string if not found, or description if found.
  1337. */
  1338. function get_status_header_desc( $code ) {
  1339. global $wp_header_to_desc;
  1340. $code = absint( $code );
  1341. if ( !isset( $wp_header_to_desc ) ) {
  1342. $wp_header_to_desc = array(
  1343. 100 => 'Continue',
  1344. 101 => 'Switching Protocols',
  1345. 102 => 'Processing',
  1346. 200 => 'OK',
  1347. 201 => 'Created',
  1348. 202 => 'Accepted',
  1349. 203 => 'Non-Authoritative Information',
  1350. 204 => 'No Content',
  1351. 205 => 'Reset Content',
  1352. 206 => 'Partial Content',
  1353. 207 => 'Multi-Status',
  1354. 226 => 'IM Used',
  1355. 300 => 'Multiple Choices',
  1356. 301 => 'Moved Permanently',
  1357. 302 => 'Found',
  1358. 303 => 'See Other',
  1359. 304 => 'Not Modified',
  1360. 305 => 'Use Proxy',
  1361. 306 => 'Reserved',
  1362. 307 => 'Temporary Redirect',
  1363. 400 => 'Bad Request',
  1364. 401 => 'Unauthorized',
  1365. 402 => 'Payment Required',
  1366. 403 => 'Forbidden',
  1367. 404 => 'Not Found',
  1368. 405 => 'Method Not Allowed',
  1369. 406 => 'Not Acceptable',
  1370. 407 => 'Proxy Authentication Required',
  1371. 408 => 'Request Timeout',
  1372. 409 => 'Conflict',
  1373. 410 => 'Gone',
  1374. 411 => 'Length Required',
  1375. 412 => 'Precondition Failed',
  1376. 413 => 'Request Entity Too Large',
  1377. 414 => 'Request-URI Too Long',
  1378. 415 => 'Unsupported Media Type',
  1379. 416 => 'Requested Range Not Satisfiable',
  1380. 417 => 'Expectation Failed',
  1381. 422 => 'Unprocessable Entity',
  1382. 423 => 'Locked',
  1383. 424 => 'Failed Dependency',
  1384. 426 => 'Upgrade Required',
  1385. 500 => 'Internal Server Error',
  1386. 501 => 'Not Implemented',
  1387. 502 => 'Bad Gateway',
  1388. 503 => 'Service Unavailable',
  1389. 504 => 'Gateway Timeout',
  1390. 505 => 'HTTP Version Not Supported',
  1391. 506 => 'Variant Also Negotiates',
  1392. 507 => 'Insufficient Storage',
  1393. 510 => 'Not Extended'
  1394. );
  1395. }
  1396. if ( isset( $wp_header_to_desc[$code] ) )
  1397. return $wp_header_to_desc[$code];
  1398. else
  1399. return '';
  1400. }
  1401. /**
  1402. * Set HTTP status header.
  1403. *
  1404. * @since 2.0.0
  1405. * @uses apply_filters() Calls 'status_header' on status header string, HTTP
  1406. * HTTP code, HTTP code description, and protocol string as separate
  1407. * parameters.
  1408. *
  1409. * @param int $header HTTP status code
  1410. * @return unknown
  1411. */
  1412. function status_header( $header ) {
  1413. $text = get_status_header_desc( $header );
  1414. if ( empty( $text ) )
  1415. return false;
  1416. $protocol = $_SERVER["SERVER_PROTOCOL"];
  1417. if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
  1418. $protocol = 'HTTP/1.0';
  1419. $status_header = "$protocol $header $text";
  1420. if ( function_exists( 'apply_filters' ) )
  1421. $status_header = apply_filters( 'status_header', $status_header, $header, $text, $protocol );
  1422. return @header( $status_header, true, $header );
  1423. }
  1424. /**
  1425. * Gets the header information to prevent caching.
  1426. *
  1427. * The several different headers cover the different ways cache prevention is handled
  1428. * by different browsers
  1429. *
  1430. * @since 2.8
  1431. *
  1432. * @uses apply_filters()
  1433. * @return array The associative array of header names and field values.
  1434. */
  1435. function wp_get_nocache_headers() {
  1436. $headers = array(
  1437. 'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
  1438. 'Last-Modified' => gmdate( 'D, d M Y H:i:s' ) . ' GMT',
  1439. 'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
  1440. 'Pragma' => 'no-cache',
  1441. );
  1442. if ( function_exists('apply_filters') ) {
  1443. $headers = apply_filters('nocache_headers', $headers);
  1444. }
  1445. return $headers;
  1446. }
  1447. /**
  1448. * Sets the headers to prevent caching for the different browsers.
  1449. *
  1450. * Different browsers support different nocache headers, so several headers must
  1451. * be sent so that all of them get the point that no caching should occur.
  1452. *
  1453. * @since 2.0.0
  1454. * @uses wp_get_nocache_headers()
  1455. */
  1456. function nocache_headers() {
  1457. $headers = wp_get_nocache_headers();
  1458. foreach( (array) $headers as $name => $field_value )
  1459. @header("{$name}: {$field_value}");
  1460. }
  1461. /**
  1462. * Set the headers for caching for 10 days with JavaScript content type.
  1463. *
  1464. * @since 2.1.0
  1465. */
  1466. function cache_javascript_headers() {
  1467. $expiresOffset = 864000; // 10 days
  1468. header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
  1469. header( "Vary: Accept-Encoding" ); // Handle proxies
  1470. header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
  1471. }
  1472. /**
  1473. * Retrieve the number of database queries during the WordPress execution.
  1474. *
  1475. * @since 2.0.0
  1476. *
  1477. * @return int Number of database queries
  1478. */
  1479. function get_num_queries() {
  1480. global $wpdb;
  1481. return $wpdb->num_queries;
  1482. }
  1483. /**
  1484. * Whether input is yes…

Large files files are truncated, but you can click here to view the full file