PageRenderTime 93ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/functions.php

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

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