PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/msw/dev/wp-includes/functions.php

https://github.com/chrissiebrodigan/USC
PHP | 4167 lines | 2084 code | 462 blank | 1621 comment | 506 complexity | 9b145a6b8ac53328f68c9c8fc89417a5 MD5 | raw file

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

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