Make WordPress Core

Ticket #10788: functions.php

File functions.php, 114.9 KB (added by nacin, 17 years ago)

First pass on potential overhaul of options/transients.

Line 
1<?php
2/**
3 * Main WordPress API
4 *
5 * @package WordPress
6 */
7
8/**
9 * Converts MySQL DATETIME field to user specified date format.
10 *
11 * If $dateformatstring has 'G' value, then gmmktime() function will be used to
12 * make the time. If $dateformatstring is set to 'U', then mktime() function
13 * will be used to make the time.
14 *
15 * The $translate will only be used, if it is set to true and it is by default
16 * and if the $wp_locale object has the month and weekday set.
17 *
18 * @since 0.71
19 *
20 * @param string $dateformatstring Either 'G', 'U', or php date format.
21 * @param string $mysqlstring Time from mysql DATETIME field.
22 * @param bool $translate Optional. Default is true. Will switch format to locale.
23 * @return string Date formated by $dateformatstring or locale (if available).
24 */
25function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
26 global $wp_locale;
27 $m = $mysqlstring;
28 if ( empty( $m ) )
29 return false;
30
31 if( 'G' == $dateformatstring ) {
32 return strtotime( $m . ' +0000' );
33 }
34
35 $i = strtotime( $m );
36
37 if( 'U' == $dateformatstring )
38 return $i;
39
40 if ( $translate)
41 return date_i18n( $dateformatstring, $i );
42 else
43 return date( $dateformatstring, $i );
44}
45
46/**
47 * Retrieve the current time based on specified type.
48 *
49 * The 'mysql' type will return the time in the format for MySQL DATETIME field.
50 * The 'timestamp' type will return the current timestamp.
51 *
52 * If $gmt is set to either '1' or 'true', then both types will use GMT time.
53 * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
54 *
55 * @since 1.0.0
56 *
57 * @param string $type Either 'mysql' or 'timestamp'.
58 * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
59 * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
60 */
61function current_time( $type, $gmt = 0 ) {
62 switch ( $type ) {
63 case 'mysql':
64 return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
65 break;
66 case 'timestamp':
67 return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * 3600 );
68 break;
69 }
70}
71
72/**
73 * Retrieve the date in localized format, based on timestamp.
74 *
75 * If the locale specifies the locale month and weekday, then the locale will
76 * take over the format for the date. If it isn't, then the date format string
77 * will be used instead.
78 *
79 * @since 0.71
80 *
81 * @param string $dateformatstring Format to display the date.
82 * @param int $unixtimestamp Optional. Unix timestamp.
83 * @param bool $gmt Optional, default is false. Whether to convert to GMT for time.
84 * @return string The date, translated if locale specifies it.
85 */
86function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
87 global $wp_locale;
88 $i = $unixtimestamp;
89 // Sanity check for PHP 5.1.0-
90 if ( false === $i || intval($i) < 0 ) {
91 if ( ! $gmt )
92 $i = current_time( 'timestamp' );
93 else
94 $i = time();
95 // we should not let date() interfere with our
96 // specially computed timestamp
97 $gmt = true;
98 }
99
100 // store original value for language with untypical grammars
101 // see http://core.trac.wordpress.org/ticket/9396
102 $req_format = $dateformatstring;
103
104 $datefunc = $gmt? 'gmdate' : 'date';
105
106 if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
107 $datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) );
108 $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
109 $dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
110 $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
111 $datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
112 $datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) );
113 $dateformatstring = ' '.$dateformatstring;
114 $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
115 $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
116 $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
117 $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
118 $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
119 $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
120
121 $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
122 }
123 $j = @$datefunc( $dateformatstring, $i );
124 // allow plugins to redo this entirely for languages with untypical grammars
125 $j = apply_filters('date_i18n', $j, $req_format, $i, $gmt);
126 return $j;
127}
128
129/**
130 * Convert number to format based on the locale.
131 *
132 * @since 2.3.0
133 *
134 * @param mixed $number The number to convert based on locale.
135 * @param int $decimals Precision of the number of decimal places.
136 * @return string Converted number in string format.
137 */
138function number_format_i18n( $number, $decimals = null ) {
139 global $wp_locale;
140 // let the user override the precision only
141 $decimals = ( is_null( $decimals ) ) ? $wp_locale->number_format['decimals'] : intval( $decimals );
142
143 $num = number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
144
145 // let the user translate digits from latin to localized language
146 return apply_filters( 'number_format_i18n', $num );
147}
148
149/**
150 * Convert number of bytes largest unit bytes will fit into.
151 *
152 * It is easier to read 1kB than 1024 bytes and 1MB than 1048576 bytes. Converts
153 * number of bytes to human readable number by taking the number of that unit
154 * that the bytes will go into it. Supports TB value.
155 *
156 * Please note that integers in PHP are limited to 32 bits, unless they are on
157 * 64 bit architecture, then they have 64 bit size. If you need to place the
158 * larger size then what PHP integer type will hold, then use a string. It will
159 * be converted to a double, which should always have 64 bit length.
160 *
161 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
162 * @link http://en.wikipedia.org/wiki/Byte
163 *
164 * @since 2.3.0
165 *
166 * @param int|string $bytes Number of bytes. Note max integer size for integers.
167 * @param int $decimals Precision of number of decimal places.
168 * @return bool|string False on failure. Number string on success.
169 */
170function size_format( $bytes, $decimals = null ) {
171 $quant = array(
172 // ========================= Origin ====
173 'TB' => 1099511627776, // pow( 1024, 4)
174 'GB' => 1073741824, // pow( 1024, 3)
175 'MB' => 1048576, // pow( 1024, 2)
176 'kB' => 1024, // pow( 1024, 1)
177 'B ' => 1, // pow( 1024, 0)
178 );
179
180 foreach ( $quant as $unit => $mag )
181 if ( doubleval($bytes) >= $mag )
182 return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
183
184 return false;
185}
186
187/**
188 * Get the week start and end from the datetime or date string from mysql.
189 *
190 * @since 0.71
191 *
192 * @param string $mysqlstring Date or datetime field type from mysql.
193 * @param int $start_of_week Optional. Start of the week as an integer.
194 * @return array Keys are 'start' and 'end'.
195 */
196function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
197 $my = substr( $mysqlstring, 0, 4 ); // Mysql string Year
198 $mm = substr( $mysqlstring, 8, 2 ); // Mysql string Month
199 $md = substr( $mysqlstring, 5, 2 ); // Mysql string day
200 $day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day.
201 $weekday = date( 'w', $day ); // The day of the week from the timestamp
202 $i = 86400; // One day
203 if( !is_numeric($start_of_week) )
204 $start_of_week = get_option( 'start_of_week' );
205
206 if ( $weekday < $start_of_week )
207 $weekday = 7 - $start_of_week - $weekday;
208
209 while ( $weekday > $start_of_week ) {
210 $weekday = date( 'w', $day );
211 if ( $weekday < $start_of_week )
212 $weekday = 7 - $start_of_week - $weekday;
213
214 $day -= 86400;
215 $i = 0;
216 }
217 $week['start'] = $day + 86400 - $i;
218 $week['end'] = $week['start'] + 604799;
219 return $week;
220}
221
222/**
223 * Unserialize value only if it was serialized.
224 *
225 * @since 2.0.0
226 *
227 * @param string $original Maybe unserialized original, if is needed.
228 * @return mixed Unserialized data can be any type.
229 */
230function maybe_unserialize( $original ) {
231 if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
232 return @unserialize( $original );
233 return $original;
234}
235
236/**
237 * Check value to find if it was serialized.
238 *
239 * If $data is not an string, then returned value will always be false.
240 * Serialized data is always a string.
241 *
242 * @since 2.0.5
243 *
244 * @param mixed $data Value to check to see if was serialized.
245 * @return bool False if not serialized and true if it was.
246 */
247function is_serialized( $data ) {
248 // if it isn't a string, it isn't serialized
249 if ( !is_string( $data ) )
250 return false;
251 $data = trim( $data );
252 if ( 'N;' == $data )
253 return true;
254 if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
255 return false;
256 switch ( $badions[1] ) {
257 case 'a' :
258 case 'O' :
259 case 's' :
260 if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
261 return true;
262 break;
263 case 'b' :
264 case 'i' :
265 case 'd' :
266 if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
267 return true;
268 break;
269 }
270 return false;
271}
272
273/**
274 * Check whether serialized data is of string type.
275 *
276 * @since 2.0.5
277 *
278 * @param mixed $data Serialized data
279 * @return bool False if not a serialized string, true if it is.
280 */
281function is_serialized_string( $data ) {
282 // if it isn't a string, it isn't a serialized string
283 if ( !is_string( $data ) )
284 return false;
285 $data = trim( $data );
286 if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
287 return true;
288 return false;
289}
290
291/**
292 * Retrieve option value based on option name.
293 *
294 * If the option does not exist or does not have a value, then the return value
295 * will be false. This is useful to check whether you need to install an option
296 * and is commonly used during installation of plugin options and to test
297 * whether upgrading is required.
298 *
299 * You can "short-circuit" the retrieval of the option from the database for
300 * your plugin or core options that aren't protected. You can do so by hooking
301 * into the 'pre_option_$option' with the $option being replaced by the option
302 * name. You should not try to override special options, but you will not be
303 * prevented from doing so.
304 *
305 * There is a second filter called 'option_$option' with the $option being
306 * replaced with the option name. This gives the value as the only parameter.
307 *
308 * If the option was serialized, when the option was added and, or updated, then
309 * it will be unserialized, when it is returned.
310 *
311 * @since 1.5.0
312 * @package WordPress
313 * @subpackage Option
314 * @uses apply_filters() Calls 'pre_option_$option' null to allow overwriting
315 * the option value in a plugin.
316 * @uses apply_filters() Calls 'option_$option' with the option name value.
317 *
318 * @param string $option Name of option to retrieve. Should already be SQL-escaped
319 * @param mixed $default Optional value to return if option doesn't exist
320 * @return mixed Value set for the option.
321 */
322function get_option( $option, $default = false ) {
323 global $wpdb;
324
325 // Allow plugins to short-circuit options.
326 $pre = apply_filters( 'pre_option_' . $option, null );
327 if ( null !== $pre )
328 return $pre;
329
330 // prevent non-existent options from triggering multiple queries
331 $notoptions = wp_cache_get( 'notoptions', 'options' );
332 if ( isset( $notoptions[$option] ) )
333 return $default;
334
335 $alloptions = wp_load_alloptions();
336
337 if ( isset( $alloptions[$option] ) ) {
338 $value = $alloptions[$option];
339 } else {
340 $value = wp_cache_get( $option, 'options' );
341
342 if ( false === $value ) {
343 if ( defined( 'WP_INSTALLING' ) )
344 $suppress = $wpdb->suppress_errors();
345 // expected_slashed ($option)
346 $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$option' LIMIT 1" );
347 if ( defined( 'WP_INSTALLING' ) )
348 $wpdb->suppress_errors($suppress);
349
350 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
351 if ( is_object( $row ) ) {
352 $value = $row->option_value;
353 wp_cache_add( $option, $value, 'options' );
354 } else { // option does not exist, so we must cache its non-existence
355 $notoptions[$option] = true;
356 wp_cache_set( 'notoptions', $notoptions, 'options' );
357 return $default;
358 }
359 }
360 }
361
362 // If home is not set use siteurl.
363 if ( 'home' == $option && '' == $value )
364 return get_option( 'siteurl' );
365
366 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) )
367 $value = untrailingslashit( $value );
368
369 return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
370}
371
372/**
373 * Protect WordPress special option from being modified.
374 *
375 * Will die if $option is in protected list. Protected options are 'alloptions'
376 * and 'notoptions' options.
377 *
378 * @since 2.2.0
379 * @package WordPress
380 * @subpackage Option
381 *
382 * @param string $option Option name.
383 */
384function wp_protect_special_option( $option ) {
385 $protected = array( 'alloptions', 'notoptions' );
386 if ( in_array( $option, $protected ) )
387 die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
388}
389
390/**
391 * Print option value after sanitizing for forms.
392 *
393 * @uses attr Sanitizes value.
394 * @since 1.5.0
395 * @package WordPress
396 * @subpackage Option
397 *
398 * @param string $option Option name.
399 */
400function form_option( $option ) {
401 echo esc_attr(get_option( $option ) );
402}
403
404/**
405 * Retrieve all autoload options or all options, if no autoloaded ones exist.
406 *
407 * This is different from wp_load_alloptions() in that this function does not
408 * cache its results and will retrieve all options from the database every time
409 *
410 * it is called.
411 *
412 * @since 1.0.0
413 * @package WordPress
414 * @subpackage Option
415 * @uses apply_filters() Calls 'pre_option_$optionname' hook with option value as parameter.
416 * @uses apply_filters() Calls 'all_options' on options list.
417 *
418 * @return array List of all options.
419 */
420function get_alloptions() {
421 global $wpdb;
422 $show = $wpdb->hide_errors();
423 if ( !$options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
424 $options = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
425 $wpdb->show_errors($show);
426
427 foreach ( (array) $options as $option ) {
428 // "When trying to design a foolproof system,
429 // never underestimate the ingenuity of the fools :)" -- Dougal
430 if ( in_array( $option->option_name, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) )
431 $option->option_value = untrailingslashit( $option->option_value );
432 $value = maybe_unserialize( $option->option_value );
433 $all_options->{$option->option_name} = apply_filters( 'pre_option_' . $option->option_name, $value );
434 }
435 return apply_filters( 'all_options', $all_options );
436}
437
438/**
439 * Loads and caches all autoloaded options, if available or all options.
440 *
441 * This is different from get_alloptions(), in that this function will cache the
442 * options and will return the cached options when called again.
443 *
444 * @since 2.2.0
445 * @package WordPress
446 * @subpackage Option
447 *
448 * @return array List all options.
449 */
450function wp_load_alloptions() {
451 global $wpdb;
452
453 $alloptions = wp_cache_get( 'alloptions', 'options' );
454
455 if ( !$alloptions ) {
456 $suppress = $wpdb->suppress_errors();
457 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
458 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
459 $wpdb->suppress_errors($suppress);
460 $alloptions = array();
461 foreach ( (array) $alloptions_db as $o )
462 $alloptions[$o->option_name] = $o->option_value;
463 wp_cache_add( 'alloptions', $alloptions, 'options' );
464 }
465 return $alloptions;
466}
467
468/**
469 * Update the value of an option that was already added.
470 *
471 * You do not need to serialize values, if the value needs to be serialize, then
472 * it will be serialized before it is inserted into the database. Remember,
473 * resources can not be serialized or added as an option.
474 *
475 * If the option does not exist, then the option will be added with the option
476 * value, but you will not be able to set whether it is autoloaded. If you want
477 * to set whether an option autoloaded, then you need to use the add_option().
478 *
479 * Before the option is updated, then the filter named
480 * 'pre_update_option_$option', with the $option as the $option parameter value,
481 * will be called. The hook should accept two parameters, the first is the new
482 * and the second is the old value. Whatever is returned will be used as the new
483 * value.
484 *
485 * After the value has been updated the action named 'update_option_$option'
486 * will be called. This action receives two parameters the first being the old
487 * value and the second the new value.
488 *
489 * @since 1.0.0
490 * @package WordPress
491 * @subpackage Option
492 * @uses apply_filters() calls 'pre_update_option_$option' to allow overwriting
493 * the option value in a plugin.
494 *
495 * @param string $option Option name. Expected to not be SQL-escaped
496 * @param mixed $value Option value.
497 * @return bool False if value was not updated and true if value was updated.
498 */
499function update_option( $option, $value ) {
500 global $wpdb;
501
502 wp_protect_special_option( $option );
503
504 $safe_option = esc_sql( $option );
505 $new_value = sanitize_option( $option, $value );
506 $old_value = get_option( $safe_option );
507 $new_value = apply_filters( 'pre_update_option_' . $option, $new_value, $old_value );
508
509 // If the new and old values are the same, no need to update.
510 if ( $new_value === $old_value )
511 return false;
512
513 if ( false === $old_value )
514 return add_option( $option, $new_value );
515
516 $notoptions = wp_cache_get( 'notoptions', 'options' );
517 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
518 unset( $notoptions[$option] );
519 wp_cache_set( 'notoptions', $notoptions, 'options' );
520 }
521
522 $_new_value = $new_value;
523 $new_value = maybe_serialize( $new_value );
524
525 do_action( 'update_option', $option, $old_value, $new_value );
526 $alloptions = wp_load_alloptions();
527 if ( isset( $alloptions[$option] ) ) {
528 $alloptions[$option] = $new_value;
529 wp_cache_set( 'alloptions', $alloptions, 'options' );
530 } else {
531 wp_cache_set( $option, $new_value, 'options' );
532 }
533
534 $wpdb->update($wpdb->options, array('option_value' => $new_value), array('option_name' => $option) );
535
536 if ( $wpdb->rows_affected == 1 ) {
537 do_action( "update_option_{$option}", $old_value, $_new_value );
538 do_action( 'updated_option', $option, $old_value, $_new_value );
539 return true;
540 }
541 return false;
542}
543
544/**
545 * Add a new option.
546 *
547 * You do not need to serialize values. If the value needs to be serialized, then
548 * it will be serialized before it is inserted into the database. Remember,
549 * resources can not be serialized or added as an option.
550 *
551 * You can create options without values and then add values later. Does not
552 * check whether the option has already been added, but does check that you
553 * aren't adding a protected WordPress option. Care should be taken to not name
554 * options, the same as the ones which are protected and to not add options
555 * that were already added.
556 *
557 * The filter named 'add_option_$option', with the $option being
558 * replaced with the option's name, will be called. The hook should accept two
559 * parameters, the first is the option name, and the second is the value.
560 *
561 * @package WordPress
562 * @subpackage Option
563 * @since 1.0.0
564 * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
565 * @uses apply_filters() Calls 'pre_add_option_$option' to allow overwriting
566 * the option value in a plugin.
567 *
568 * @param string $option Name of option to add. Expects to not be SQL escaped.
569 * @param mixed $value Optional. Option value, can be anything.
570 * @param mixed $deprecated Optional. Description. Not used anymore.
571 * @param bool $autoload Optional. Whether to load the option when WordPress starts up. Default is enabled.
572 * @return bool False if option was not added and true if option was added.
573 */
574function add_option( $option, $value = '', $deprecated = '', $autoload = true ) {
575 global $wpdb;
576
577 wp_protect_special_option( $option );
578 $safe_name = esc_sql( $option );
579
580 $value = sanitize_option( $option, $value );
581 $value = apply_filters( 'pre_add_option_' . $option, $value );
582
583 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
584 $notoptions = wp_cache_get( 'notoptions', 'options' );
585 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
586 if ( false !== get_option( $safe_name ) )
587 return false;
588
589 $value = maybe_serialize( $value );
590 $autoload = ( $autoload && 'no' != $autoload ) ? true : false;
591 do_action( 'add_option', $option, $value );
592 if ( $autoload ) {
593 $alloptions = wp_load_alloptions();
594 $alloptions[$option] = $value;
595 wp_cache_set( 'alloptions', $alloptions, 'options' );
596 } else {
597 wp_cache_set( $option, $value, 'options' );
598 }
599
600 // This option exists now
601 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
602 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
603 unset( $notoptions[$option] );
604 wp_cache_set( 'notoptions', $notoptions, 'options' );
605 }
606
607 $wpdb->insert($wpdb->options, array('option_name' => $option, 'option_value' => $value, 'autoload' => ( $autoload ) ? 'yes' : 'no' ) );
608
609 do_action( "add_option_{$option}", $option, $value );
610 do_action( 'added_option', $option, $value );
611
612 return true;
613}
614
615/**
616 * Removes option by name. Prevents removal of protected WordPress options.
617 *
618 * @package WordPress
619 * @subpackage Option
620 * @since 1.2.0
621 *
622 * @param string $option Name of option to remove. Expected to be SQL-escaped.
623 * @return bool True, if succeed. False, if failure.
624 */
625function delete_option( $option ) {
626 global $wpdb;
627
628 wp_protect_special_option( $option );
629
630 // Get the ID, if no ID then return
631 // expected_slashed ($name)
632 $option = $wpdb->get_row( "SELECT autoload FROM $wpdb->options WHERE option_name = '$option'" );
633 if ( is_null($option) )
634 return false;
635 do_action( 'delete_option', $option );
636 // expected_slashed ($name)
637 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$option'" );
638 if ( 'yes' == $option->autoload ) {
639 $alloptions = wp_load_alloptions();
640 if ( isset( $alloptions[$option] ) ) {
641 unset( $alloptions[$option] );
642 wp_cache_set( 'alloptions', $alloptions, 'options' );
643 }
644 } else {
645 wp_cache_delete( $option, 'options' );
646 }
647 do_action( "delete_option_{$option}", $option );
648 do_action( 'deleted_option', $option );
649 return true;
650}
651
652/**
653 * Delete a transient
654 *
655 * @since 2.8.0
656 * @package WordPress
657 * @subpackage Transient
658 *
659 * @param string $transient Transient name. Expected to not be SQL-escaped
660 * @return bool true if successful, false otherwise
661 */
662function delete_transient($transient) {
663 global $_wp_using_ext_object_cache;
664
665 $_transient = $transient;
666 do_action( 'delete_transient', $transient );
667 if ( $_wp_using_ext_object_cache ) {
668 $result = wp_cache_delete( $transient, 'transient' );
669 } else {
670 $result = delete_option( '_transient_' . esc_sql( $transient ) );
671 }
672
673 if ( $result ) {
674 do_action( "delete_transient_{$transient}", $transient );
675 do_action( 'deleted_transient', $transient );
676 }
677 return $result;
678}
679
680/**
681 * Get the value of a transient
682 *
683 * If the transient does not exist or does not have a value, then the return value
684 * will be false.
685 *
686 * @since 2.8.0
687 * @package WordPress
688 * @subpackage Transient
689 *
690 * @param string $transient Transient name. Expected to not be SQL-escaped
691 * @return mixed Value of transient
692 */
693function get_transient($transient) {
694 global $_wp_using_ext_object_cache;
695
696 $pre = apply_filters( 'pre_transient_' . $transient, null );
697 if ( null !== $pre )
698 return $pre;
699
700 if ( $_wp_using_ext_object_cache ) {
701 $value = wp_cache_get($transient, 'transient');
702 } else {
703 $safe_transient = esc_sql( $transient );
704 $transient_option = '_transient_' . $safe_transient;
705 // If option is not in alloptions, it is not autoloaded and thus has a timeout
706 $alloptions = wp_load_alloptions();
707 if ( !isset( $alloptions[$transient_option] ) ) {
708 $transient_timeout = '_transient_timeout_' . $safe_transient;
709 if ( get_option( $transient_timeout ) < time() ) {
710 delete_option( $transient_option );
711 delete_option( $transient_timeout );
712 return false;
713 }
714 }
715
716 $value = get_option( $transient_option );
717 }
718
719 return apply_filters('transient_' . $transient, $value);
720}
721
722/**
723 * Set/update the value of a transient
724 *
725 * You do not need to serialize values, if the value needs to be serialize, then
726 * it will be serialized before it is set.
727 *
728 * @since 2.8.0
729 * @package WordPress
730 * @subpackage Transient
731 * @uses apply_filters() Calls 'pre_set_transient_$transient' null to allow overwriting
732 * the transient value in a plugin.
733 * @uses apply_filters() Calls 'pre_set_transient_expiration_$transient' null to allow overwriting
734 * the transient expiration in a plugin.
735 *
736 * @param string $transient Transient name. Expected to not be SQL-escaped
737 * @param mixed $value Transient value.
738 * @param int $expiration Time until expiration in seconds, default 0
739 * @return bool False if value was not set and true if value was set.
740 */
741function set_transient($transient, $value, $expiration = 0) {
742 global $_wp_using_ext_object_cache;
743
744 $value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration );
745 $expiration = apply_filters( 'pre_set_transient_expiration_' . $transient, $expiration, $value );
746
747 do_action( 'set_transient', $transient, $value, $expiration );
748 if ( $_wp_using_ext_object_cache ) {
749 $result = wp_cache_set($transient, $value, 'transient', $expiration);
750 } else {
751 $transient_timeout = '_transient_timeout_' . $transient;
752 $transient = '_transient_' . $transient;
753 $safe_transient = esc_sql($transient);
754 if ( false === get_option( $safe_transient ) ) {
755 $autoload = 'yes';
756 if ( 0 != $expiration ) {
757 $autoload = 'no';
758 add_option( $transient_timeout, time() + $expiration, '', 'no' );
759 }
760 $result = add_option( $transient, $value, '', $autoload );
761 } else {
762 if ( 0 != $expiration )
763 update_option( $transient_timeout, time() + $expiration );
764 $result = update_option( $transient, $value );
765 }
766 }
767 if ( $result ) {
768 do_action( "set_transient_{$transient}", $transient, $value, $expiration );
769 do_action( 'setted_transient', $transient, $value, $expiration );
770 }
771 return $result;
772}
773
774/**
775 * Saves and restores user interface settings stored in a cookie.
776 *
777 * Checks if the current user-settings cookie is updated and stores it. When no
778 * cookie exists (different browser used), adds the last saved cookie restoring
779 * the settings.
780 *
781 * @package WordPress
782 * @subpackage Option
783 * @since 2.7.0
784 */
785function wp_user_settings() {
786
787 if ( ! is_admin() )
788 return;
789
790 if ( defined('DOING_AJAX') )
791 return;
792
793 if ( ! $user = wp_get_current_user() )
794 return;
795
796 $settings = get_user_option( 'user-settings', $user->ID, false );
797
798 if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
799 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
800
801 if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
802 if ( $cookie == $settings )
803 return;
804
805 $last_time = (int) get_user_option( 'user-settings-time', $user->ID, false );
806 $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
807
808 if ( $saved > $last_time ) {
809 update_user_option( $user->ID, 'user-settings', $cookie, false );
810 update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
811 return;
812 }
813 }
814 }
815
816 setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
817 setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
818 $_COOKIE['wp-settings-' . $user->ID] = $settings;
819}
820
821/**
822 * Retrieve user interface setting value based on setting name.
823 *
824 * @package WordPress
825 * @subpackage Option
826 * @since 2.7.0
827 *
828 * @param string $name The name of the setting.
829 * @param string $default Optional default value to return when $name is not set.
830 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
831 */
832function get_user_setting( $name, $default = false ) {
833
834 $all = get_all_user_settings();
835
836 return isset($all[$name]) ? $all[$name] : $default;
837}
838
839/**
840 * Add or update user interface setting.
841 *
842 * Both $name and $value can contain only ASCII letters, numbers and underscores.
843 * This function has to be used before any output has started as it calls setcookie().
844 *
845 * @package WordPress
846 * @subpackage Option
847 * @since 2.8.0
848 *
849 * @param string $name The name of the setting.
850 * @param string $value The value for the setting.
851 * @return bool true if set successfully/false if not.
852 */
853function set_user_setting( $name, $value ) {
854
855 if ( headers_sent() )
856 return false;
857
858 $all = get_all_user_settings();
859 $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
860
861 if ( empty($name) )
862 return false;
863
864 $all[$name] = $value;
865
866 return wp_set_all_user_settings($all);
867}
868
869/**
870 * Delete user interface settings.
871 *
872 * Deleting settings would reset them to the defaults.
873 * This function has to be used before any output has started as it calls setcookie().
874 *
875 * @package WordPress
876 * @subpackage Option
877 * @since 2.7.0
878 *
879 * @param mixed $names The name or array of names of the setting to be deleted.
880 * @return bool true if deleted successfully/false if not.
881 */
882function delete_user_setting( $names ) {
883
884 if ( headers_sent() )
885 return false;
886
887 $all = get_all_user_settings();
888 $names = (array) $names;
889
890 foreach ( $names as $name ) {
891 if ( isset($all[$name]) ) {
892 unset($all[$name]);
893 $deleted = true;
894 }
895 }
896
897 if ( isset($deleted) )
898 return wp_set_all_user_settings($all);
899
900 return false;
901}
902
903/**
904 * Retrieve all user interface settings.
905 *
906 * @package WordPress
907 * @subpackage Option
908 * @since 2.7.0
909 *
910 * @return array the last saved user settings or empty array.
911 */
912function get_all_user_settings() {
913 global $_updated_user_settings;
914
915 if ( ! $user = wp_get_current_user() )
916 return array();
917
918 if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
919 return $_updated_user_settings;
920
921 $all = array();
922 if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
923 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
924
925 if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
926 parse_str($cookie, $all);
927
928 } else {
929 $option = get_user_option('user-settings', $user->ID);
930 if ( $option && is_string($option) )
931 parse_str( $option, $all );
932 }
933
934 return $all;
935}
936
937/**
938 * Private. Set all user interface settings.
939 *
940 * @package WordPress
941 * @subpackage Option
942 * @since 2.8.0
943 *
944 */
945function wp_set_all_user_settings($all) {
946 global $_updated_user_settings;
947
948 if ( ! $user = wp_get_current_user() )
949 return false;
950
951 $_updated_user_settings = $all;
952 $settings = '';
953 foreach ( $all as $k => $v ) {
954 $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
955 $settings .= $k . '=' . $v . '&';
956 }
957
958 $settings = rtrim($settings, '&');
959
960 update_user_option( $user->ID, 'user-settings', $settings, false );
961 update_user_option( $user->ID, 'user-settings-time', time(), false );
962
963 return true;
964}
965
966/**
967 * Delete the user settings of the current user.
968 *
969 * @package WordPress
970 * @subpackage Option
971 * @since 2.7.0
972 */
973function delete_all_user_settings() {
974 if ( ! $user = wp_get_current_user() )
975 return;
976
977 update_user_option( $user->ID, 'user-settings', '', false );
978 setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
979}
980
981/**
982 * Serialize data, if needed.
983 *
984 * @since 2.0.5
985 *
986 * @param mixed $data Data that might be serialized.
987 * @return mixed A scalar data
988 */
989function maybe_serialize( $data ) {
990 if ( is_array( $data ) || is_object( $data ) )
991 return serialize( $data );
992
993 if ( is_serialized( $data ) )
994 return serialize( $data );
995
996 return $data;
997}
998
999/**
1000 * Retrieve post title from XMLRPC XML.
1001 *
1002 * If the title element is not part of the XML, then the default post title from
1003 * the $post_default_title will be used instead.
1004 *
1005 * @package WordPress
1006 * @subpackage XMLRPC
1007 * @since 0.71
1008 *
1009 * @global string $post_default_title Default XMLRPC post title.
1010 *
1011 * @param string $content XMLRPC XML Request content
1012 * @return string Post title
1013 */
1014function xmlrpc_getposttitle( $content ) {
1015 global $post_default_title;
1016 if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
1017 $post_title = $matchtitle[1];
1018 } else {
1019 $post_title = $post_default_title;
1020 }
1021 return $post_title;
1022}
1023
1024/**
1025 * Retrieve the post category or categories from XMLRPC XML.
1026 *
1027 * If the category element is not found, then the default post category will be
1028 * used. The return type then would be what $post_default_category. If the
1029 * category is found, then it will always be an array.
1030 *
1031 * @package WordPress
1032 * @subpackage XMLRPC
1033 * @since 0.71
1034 *
1035 * @global string $post_default_category Default XMLRPC post category.
1036 *
1037 * @param string $content XMLRPC XML Request content
1038 * @return string|array List of categories or category name.
1039 */
1040function xmlrpc_getpostcategory( $content ) {
1041 global $post_default_category;
1042 if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
1043 $post_category = trim( $matchcat[1], ',' );
1044 $post_category = explode( ',', $post_category );
1045 } else {
1046 $post_category = $post_default_category;
1047 }
1048 return $post_category;
1049}
1050
1051/**
1052 * XMLRPC XML content without title and category elements.
1053 *
1054 * @package WordPress
1055 * @subpackage XMLRPC
1056 * @since 0.71
1057 *
1058 * @param string $content XMLRPC XML Request content
1059 * @return string XMLRPC XML Request content without title and category elements.
1060 */
1061function xmlrpc_removepostdata( $content ) {
1062 $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
1063 $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
1064 $content = trim( $content );
1065 return $content;
1066}
1067
1068/**
1069 * Open the file handle for debugging.
1070 *
1071 * This function is used for XMLRPC feature, but it is general purpose enough
1072 * to be used in anywhere.
1073 *
1074 * @see fopen() for mode options.
1075 * @package WordPress
1076 * @subpackage Debug
1077 * @since 0.71
1078 * @uses $debug Used for whether debugging is enabled.
1079 *
1080 * @param string $filename File path to debug file.
1081 * @param string $mode Same as fopen() mode parameter.
1082 * @return bool|resource File handle. False on failure.
1083 */
1084function debug_fopen( $filename, $mode ) {
1085 global $debug;
1086 if ( 1 == $debug ) {
1087 $fp = fopen( $filename, $mode );
1088 return $fp;
1089 } else {
1090 return false;
1091 }
1092}
1093
1094/**
1095 * Write contents to the file used for debugging.
1096 *
1097 * Technically, this can be used to write to any file handle when the global
1098 * $debug is set to 1 or true.
1099 *
1100 * @package WordPress
1101 * @subpackage Debug
1102 * @since 0.71
1103 * @uses $debug Used for whether debugging is enabled.
1104 *
1105 * @param resource $fp File handle for debugging file.
1106 * @param string $string Content to write to debug file.
1107 */
1108function debug_fwrite( $fp, $string ) {
1109 global $debug;
1110 if ( 1 == $debug )
1111 fwrite( $fp, $string );
1112}
1113
1114/**
1115 * Close the debugging file handle.
1116 *
1117 * Technically, this can be used to close any file handle when the global $debug
1118 * is set to 1 or true.
1119 *
1120 * @package WordPress
1121 * @subpackage Debug
1122 * @since 0.71
1123 * @uses $debug Used for whether debugging is enabled.
1124 *
1125 * @param resource $fp Debug File handle.
1126 */
1127function debug_fclose( $fp ) {
1128 global $debug;
1129 if ( 1 == $debug )
1130 fclose( $fp );
1131}
1132
1133/**
1134 * Check content for video and audio links to add as enclosures.
1135 *
1136 * Will not add enclosures that have already been added and will
1137 * remove enclosures that are no longer in the post. This is called as
1138 * pingbacks and trackbacks.
1139 *
1140 * @package WordPress
1141 * @since 1.5.0
1142 *
1143 * @uses $wpdb
1144 *
1145 * @param string $content Post Content
1146 * @param int $post_ID Post ID
1147 */
1148function do_enclose( $content, $post_ID ) {
1149 global $wpdb;
1150 include_once( ABSPATH . WPINC . '/class-IXR.php' );
1151
1152 $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' );
1153 $post_links = array();
1154 debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" );
1155
1156 $pung = get_enclosed( $post_ID );
1157
1158 $ltrs = '\w';
1159 $gunk = '/#~:.?+=&%@!\-';
1160 $punc = '.:?\-';
1161 $any = $ltrs . $gunk . $punc;
1162
1163 preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
1164
1165 debug_fwrite( $log, 'Post contents:' );
1166 debug_fwrite( $log, $content . "\n" );
1167
1168 foreach ( $pung as $link_test ) {
1169 if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
1170 $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 . '%') );
1171 do_action( 'delete_postmeta', $mid );
1172 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE post_id IN(%s)", implode( ',', $mid ) ) );
1173 do_action( 'deleted_postmeta', $mid );
1174 }
1175 }
1176
1177 foreach ( (array) $post_links_temp[0] as $link_test ) {
1178 if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
1179 $test = parse_url( $link_test );
1180 if ( isset( $test['query'] ) )
1181 $post_links[] = $link_test;
1182 elseif ( $test['path'] != '/' && $test['path'] != '' )
1183 $post_links[] = $link_test;
1184 }
1185 }
1186
1187 foreach ( (array) $post_links as $url ) {
1188 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 . '%' ) ) ) {
1189 if ( $headers = wp_get_http_headers( $url) ) {
1190 $len = (int) $headers['content-length'];
1191 $type = $headers['content-type'];
1192 $allowed_types = array( 'video', 'audio' );
1193 if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
1194 $meta_value = "$url\n$len\n$type\n";
1195 $wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
1196 do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
1197 }
1198 }
1199 }
1200 }
1201}
1202
1203/**
1204 * Perform a HTTP HEAD or GET request.
1205 *
1206 * If $file_path is a writable filename, this will do a GET request and write
1207 * the file to that path.
1208 *
1209 * @since 2.5.0
1210 *
1211 * @param string $url URL to fetch.
1212 * @param string|bool $file_path Optional. File path to write request to.
1213 * @param bool $deprecated Deprecated. Not used.
1214 * @return bool|string False on failure and string of headers if HEAD request.
1215 */
1216function wp_get_http( $url, $file_path = false, $deprecated = false ) {
1217 @set_time_limit( 60 );
1218
1219 $options = array();
1220 $options['redirection'] = 5;
1221
1222 if ( false == $file_path )
1223 $options['method'] = 'HEAD';
1224 else
1225 $options['method'] = 'GET';
1226
1227 $response = wp_remote_request($url, $options);
1228
1229 if ( is_wp_error( $response ) )
1230 return false;
1231
1232 $headers = wp_remote_retrieve_headers( $response );
1233 $headers['response'] = $response['response']['code'];
1234
1235 if ( false == $file_path )
1236 return $headers;
1237
1238 // GET request - write it to the supplied filename
1239 $out_fp = fopen($file_path, 'w');
1240 if ( !$out_fp )
1241 return $headers;
1242
1243 fwrite( $out_fp, $response['body']);
1244 fclose($out_fp);
1245
1246 return $headers;
1247}
1248
1249/**
1250 * Retrieve HTTP Headers from URL.
1251 *
1252 * @since 1.5.1
1253 *
1254 * @param string $url
1255 * @param bool $deprecated Not Used.
1256 * @return bool|string False on failure, headers on success.
1257 */
1258function wp_get_http_headers( $url, $deprecated = false ) {
1259 $response = wp_remote_head( $url );
1260
1261 if ( is_wp_error( $response ) )
1262 return false;
1263
1264 return wp_remote_retrieve_headers( $response );
1265}
1266
1267/**
1268 * Whether today is a new day.
1269 *
1270 * @since 0.71
1271 * @uses $day Today
1272 * @uses $previousday Previous day
1273 *
1274 * @return int 1 when new day, 0 if not a new day.
1275 */
1276function is_new_day() {
1277 global $day, $previousday;
1278 if ( $day != $previousday )
1279 return 1;
1280 else
1281 return 0;
1282}
1283
1284/**
1285 * Build URL query based on an associative and, or indexed array.
1286 *
1287 * This is a convenient function for easily building url queries. It sets the
1288 * separator to '&' and uses _http_build_query() function.
1289 *
1290 * @see _http_build_query() Used to build the query
1291 * @link http://us2.php.net/manual/en/function.http-build-query.php more on what
1292 * http_build_query() does.
1293 *
1294 * @since 2.3.0
1295 *
1296 * @param array $data URL-encode key/value pairs.
1297 * @return string URL encoded string
1298 */
1299function build_query( $data ) {
1300 return _http_build_query( $data, null, '&', '', false );
1301}
1302
1303/**
1304 * Retrieve a modified URL query string.
1305 *
1306 * You can rebuild the URL and append a new query variable to the URL query by
1307 * using this function. You can also retrieve the full URL with query data.
1308 *
1309 * Adding a single key & value or an associative array. Setting a key value to
1310 * emptystring removes the key. Omitting oldquery_or_uri uses the $_SERVER
1311 * value.
1312 *
1313 * @since 1.5.0
1314 *
1315 * @param mixed $param1 Either newkey or an associative_array
1316 * @param mixed $param2 Either newvalue or oldquery or uri
1317 * @param mixed $param3 Optional. Old query or uri
1318 * @return string New URL query string.
1319 */
1320function add_query_arg() {
1321 $ret = '';
1322 if ( is_array( func_get_arg(0) ) ) {
1323 if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
1324 $uri = $_SERVER['REQUEST_URI'];
1325 else
1326 $uri = @func_get_arg( 1 );
1327 } else {
1328 if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
1329 $uri = $_SERVER['REQUEST_URI'];
1330 else
1331 $uri = @func_get_arg( 2 );
1332 }
1333
1334 if ( $frag = strstr( $uri, '#' ) )
1335 $uri = substr( $uri, 0, -strlen( $frag ) );
1336 else
1337 $frag = '';
1338
1339 if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
1340 $protocol = $matches[0];
1341 $uri = substr( $uri, strlen( $protocol ) );
1342 } else {
1343 $protocol = '';
1344 }
1345
1346 if ( strpos( $uri, '?' ) !== false ) {
1347 $parts = explode( '?', $uri, 2 );
1348 if ( 1 == count( $parts ) ) {
1349 $base = '?';
1350 $query = $parts[0];
1351 } else {
1352 $base = $parts[0] . '?';
1353 $query = $parts[1];
1354 }
1355 } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
1356 $base = $uri . '?';
1357 $query = '';
1358 } else {
1359 $base = '';
1360 $query = $uri;
1361 }
1362
1363 wp_parse_str( $query, $qs );
1364 $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
1365 if ( is_array( func_get_arg( 0 ) ) ) {
1366 $kayvees = func_get_arg( 0 );
1367 $qs = array_merge( $qs, $kayvees );
1368 } else {
1369 $qs[func_get_arg( 0 )] = func_get_arg( 1 );
1370 }
1371
1372 foreach ( (array) $qs as $k => $v ) {
1373 if ( $v === false )
1374 unset( $qs[$k] );
1375 }
1376
1377 $ret = build_query( $qs );
1378 $ret = trim( $ret, '?' );
1379 $ret = preg_replace( '#=(&|$)#', '$1', $ret );
1380 $ret = $protocol . $base . $ret . $frag;
1381 $ret = rtrim( $ret, '?' );
1382 return $ret;
1383}
1384
1385/**
1386 * Removes an item or list from the query string.
1387 *
1388 * @since 1.5.0
1389 *
1390 * @param string|array $key Query key or keys to remove.
1391 * @param bool $query When false uses the $_SERVER value.
1392 * @return string New URL query string.
1393 */
1394function remove_query_arg( $key, $query=false ) {
1395 if ( is_array( $key ) ) { // removing multiple keys
1396 foreach ( $key as $k )
1397 $query = add_query_arg( $k, false, $query );
1398 return $query;
1399 }
1400 return add_query_arg( $key, false, $query );
1401}
1402
1403/**
1404 * Walks the array while sanitizing the contents.
1405 *
1406 * @uses $wpdb Used to sanitize values
1407 * @since 0.71
1408 *
1409 * @param array $array Array to used to walk while sanitizing contents.
1410 * @return array Sanitized $array.
1411 */
1412function add_magic_quotes( $array ) {
1413 global $wpdb;
1414
1415 foreach ( (array) $array as $k => $v ) {
1416 if ( is_array( $v ) ) {
1417 $array[$k] = add_magic_quotes( $v );
1418 } else {
1419 $array[$k] = esc_sql( $v );
1420 }
1421 }
1422 return $array;
1423}
1424
1425/**
1426 * HTTP request for URI to retrieve content.
1427 *
1428 * @since 1.5.1
1429 * @uses wp_remote_get()
1430 *
1431 * @param string $uri URI/URL of web page to retrieve.
1432 * @return bool|string HTTP content. False on failure.
1433 */
1434function wp_remote_fopen( $uri ) {
1435 $parsed_url = @parse_url( $uri );
1436
1437 if ( !$parsed_url || !is_array( $parsed_url ) )
1438 return false;
1439
1440 $options = array();
1441 $options['timeout'] = 10;
1442
1443 $response = wp_remote_get( $uri, $options );
1444
1445 if ( is_wp_error( $response ) )
1446 return false;
1447
1448 return $response['body'];
1449}
1450
1451/**
1452 * Setup the WordPress query.
1453 *
1454 * @since 2.0.0
1455 *
1456 * @param string $query_vars Default WP_Query arguments.
1457 */
1458function wp( $query_vars = '' ) {
1459 global $wp, $wp_query, $wp_the_query;
1460 $wp->main( $query_vars );
1461
1462 if( !isset($wp_the_query) )
1463 $wp_the_query = $wp_query;
1464}
1465
1466/**
1467 * Retrieve the description for the HTTP status.
1468 *
1469 * @since 2.3.0
1470 *
1471 * @param int $code HTTP status code.
1472 * @return string Empty string if not found, or description if found.
1473 */
1474function get_status_header_desc( $code ) {
1475 global $wp_header_to_desc;
1476
1477 $code = absint( $code );
1478
1479 if ( !isset( $wp_header_to_desc ) ) {
1480 $wp_header_to_desc = array(
1481 100 => 'Continue',
1482 101 => 'Switching Protocols',
1483 102 => 'Processing',
1484
1485 200 => 'OK',
1486 201 => 'Created',
1487 202 => 'Accepted',
1488 203 => 'Non-Authoritative Information',
1489 204 => 'No Content',
1490 205 => 'Reset Content',
1491 206 => 'Partial Content',
1492 207 => 'Multi-Status',
1493 226 => 'IM Used',
1494
1495 300 => 'Multiple Choices',
1496 301 => 'Moved Permanently',
1497 302 => 'Found',
1498 303 => 'See Other',
1499 304 => 'Not Modified',
1500 305 => 'Use Proxy',
1501 306 => 'Reserved',
1502 307 => 'Temporary Redirect',
1503
1504 400 => 'Bad Request',
1505 401 => 'Unauthorized',
1506 402 => 'Payment Required',
1507 403 => 'Forbidden',
1508 404 => 'Not Found',
1509 405 => 'Method Not Allowed',
1510 406 => 'Not Acceptable',
1511 407 => 'Proxy Authentication Required',
1512 408 => 'Request Timeout',
1513 409 => 'Conflict',
1514 410 => 'Gone',
1515 411 => 'Length Required',
1516 412 => 'Precondition Failed',
1517 413 => 'Request Entity Too Large',
1518 414 => 'Request-URI Too Long',
1519 415 => 'Unsupported Media Type',
1520 416 => 'Requested Range Not Satisfiable',
1521 417 => 'Expectation Failed',
1522 422 => 'Unprocessable Entity',
1523 423 => 'Locked',
1524 424 => 'Failed Dependency',
1525 426 => 'Upgrade Required',
1526
1527 500 => 'Internal Server Error',
1528 501 => 'Not Implemented',
1529 502 => 'Bad Gateway',
1530 503 => 'Service Unavailable',
1531 504 => 'Gateway Timeout',
1532 505 => 'HTTP Version Not Supported',
1533 506 => 'Variant Also Negotiates',
1534 507 => 'Insufficient Storage',
1535 510 => 'Not Extended'
1536 );
1537 }
1538
1539 if ( isset( $wp_header_to_desc[$code] ) )
1540 return $wp_header_to_desc[$code];
1541 else
1542 return '';
1543}
1544
1545/**
1546 * Set HTTP status header.
1547 *
1548 * @since 2.0.0
1549 * @uses apply_filters() Calls 'status_header' on status header string, HTTP
1550 * HTTP code, HTTP code description, and protocol string as separate
1551 * parameters.
1552 *
1553 * @param int $header HTTP status code
1554 * @return null Does not return anything.
1555 */
1556function status_header( $header ) {
1557 $text = get_status_header_desc( $header );
1558
1559 if ( empty( $text ) )
1560 return false;
1561
1562 $protocol = $_SERVER["SERVER_PROTOCOL"];
1563 if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
1564 $protocol = 'HTTP/1.0';
1565 $status_header = "$protocol $header $text";
1566 if ( function_exists( 'apply_filters' ) )
1567 $status_header = apply_filters( 'status_header', $status_header, $header, $text, $protocol );
1568
1569 return @header( $status_header, true, $header );
1570}
1571
1572/**
1573 * Gets the header information to prevent caching.
1574 *
1575 * The several different headers cover the different ways cache prevention is handled
1576 * by different browsers
1577 *
1578 * @since 2.8
1579 *
1580 * @uses apply_filters()
1581 * @return array The associative array of header names and field values.
1582 */
1583function wp_get_nocache_headers() {
1584 $headers = array(
1585 'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
1586 'Last-Modified' => gmdate( 'D, d M Y H:i:s' ) . ' GMT',
1587 'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
1588 'Pragma' => 'no-cache',
1589 );
1590
1591 if ( function_exists('apply_filters') ) {
1592 $headers = apply_filters('nocache_headers', $headers);
1593 }
1594 return $headers;
1595}
1596
1597/**
1598 * Sets the headers to prevent caching for the different browsers.
1599 *
1600 * Different browsers support different nocache headers, so several headers must
1601 * be sent so that all of them get the point that no caching should occur.
1602 *
1603 * @since 2.0.0
1604 * @uses wp_get_nocache_headers()
1605 */
1606function nocache_headers() {
1607 $headers = wp_get_nocache_headers();
1608 foreach( (array) $headers as $name => $field_value )
1609 @header("{$name}: {$field_value}");
1610}
1611
1612/**
1613 * Set the headers for caching for 10 days with JavaScript content type.
1614 *
1615 * @since 2.1.0
1616 */
1617function cache_javascript_headers() {
1618 $expiresOffset = 864000; // 10 days
1619 header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
1620 header( "Vary: Accept-Encoding" ); // Handle proxies
1621 header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
1622}
1623
1624/**
1625 * Retrieve the number of database queries during the WordPress execution.
1626 *
1627 * @since 2.0.0
1628 *
1629 * @return int Number of database queries
1630 */
1631function get_num_queries() {
1632 global $wpdb;
1633 return $wpdb->num_queries;
1634}
1635
1636/**
1637 * Whether input is yes or no. Must be 'y' to be true.
1638 *
1639 * @since 1.0.0
1640 *
1641 * @param string $yn Character string containing either 'y' or 'n'
1642 * @return bool True if yes, false on anything else
1643 */
1644function bool_from_yn( $yn ) {
1645 return ( strtolower( $yn ) == 'y' );
1646}
1647
1648/**
1649 * Loads the feed template from the use of an action hook.
1650 *
1651 * If the feed action does not have a hook, then the function will die with a
1652 * message telling the visitor that the feed is not valid.
1653 *
1654 * It is better to only have one hook for each feed.
1655 *
1656 * @since 2.1.0
1657 * @uses $wp_query Used to tell if the use a comment feed.
1658 * @uses do_action() Calls 'do_feed_$feed' hook, if a hook exists for the feed.
1659 */
1660function do_feed() {
1661 global $wp_query;
1662
1663 $feed = get_query_var( 'feed' );
1664
1665 // Remove the pad, if present.
1666 $feed = preg_replace( '/^_+/', '', $feed );
1667
1668 if ( $feed == '' || $feed == 'feed' )
1669 $feed = get_default_feed();
1670
1671 $hook = 'do_feed_' . $feed;
1672 if ( !has_action($hook) ) {
1673 $message = sprintf( __( 'ERROR: %s is not a valid feed template' ), esc_html($feed));
1674 wp_die($message);
1675 }
1676
1677 do_action( $hook, $wp_query->is_comment_feed );
1678}
1679
1680/**
1681 * Load the RDF RSS 0.91 Feed template.
1682 *
1683 * @since 2.1.0
1684 */
1685function do_feed_rdf() {
1686 load_template( ABSPATH . WPINC . '/feed-rdf.php' );
1687}
1688
1689/**
1690 * Load the RSS 1.0 Feed Template
1691 *
1692 * @since 2.1.0
1693 */
1694function do_feed_rss() {
1695 load_template( ABSPATH . WPINC . '/feed-rss.php' );
1696}
1697
1698/**
1699 * Load either the RSS2 comment feed or the RSS2 posts feed.
1700 *
1701 * @since 2.1.0
1702 *
1703 * @param bool $for_comments True for the comment feed, false for normal feed.
1704 */
1705function do_feed_rss2( $for_comments ) {
1706 if ( $for_comments )
1707 load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
1708 else
1709 load_template( ABSPATH . WPINC . '/feed-rss2.php' );
1710}
1711
1712/**
1713 * Load either Atom comment feed or Atom posts feed.
1714 *
1715 * @since 2.1.0
1716 *
1717 * @param bool $for_comments True for the comment feed, false for normal feed.
1718 */
1719function do_feed_atom( $for_comments ) {
1720 if ($for_comments)
1721 load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
1722 else
1723 load_template( ABSPATH . WPINC . '/feed-atom.php' );
1724}
1725
1726/**
1727 * Display the robot.txt file content.
1728 *
1729 * The echo content should be with usage of the permalinks or for creating the
1730 * robot.txt file.
1731 *
1732 * @since 2.1.0
1733 * @uses do_action() Calls 'do_robotstxt' hook for displaying robot.txt rules.
1734 */
1735function do_robots() {
1736 header( 'Content-Type: text/plain; charset=utf-8' );
1737
1738 do_action( 'do_robotstxt' );
1739
1740 if ( '0' == get_option( 'blog_public' ) ) {
1741 echo "User-agent: *\n";
1742 echo "Disallow: /\n";
1743 } else {
1744 echo "User-agent: *\n";
1745 echo "Disallow:\n";
1746 }
1747}
1748
1749/**
1750 * Test whether blog is already installed.
1751 *
1752 * The cache will be checked first. If you have a cache plugin, which saves the
1753 * cache values, then this will work. If you use the default WordPress cache,
1754 * and the database goes away, then you might have problems.
1755 *
1756 * Checks for the option siteurl for whether WordPress is installed.
1757 *
1758 * @since 2.1.0
1759 * @uses $wpdb
1760 *
1761 * @return bool Whether blog is already installed.
1762 */
1763function is_blog_installed() {
1764 global $wpdb;
1765
1766 // Check cache first. If options table goes away and we have true cached, oh well.
1767 if ( wp_cache_get( 'is_blog_installed' ) )
1768 return true;
1769
1770 $suppress = $wpdb->suppress_errors();
1771 $alloptions = wp_load_alloptions();
1772 // If siteurl is not set to autoload, check it specifically
1773 if ( !isset( $alloptions['siteurl'] ) )
1774 $installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
1775 else
1776 $installed = $alloptions['siteurl'];
1777 $wpdb->suppress_errors( $suppress );
1778
1779 $installed = !empty( $installed );
1780 wp_cache_set( 'is_blog_installed', $installed );
1781
1782 if ( $installed )
1783 return true;
1784
1785 $suppress = $wpdb->suppress_errors();
1786 $tables = $wpdb->get_col('SHOW TABLES');
1787 $wpdb->suppress_errors( $suppress );
1788
1789 // Loop over the WP tables. If none exist, then scratch install is allowed.
1790 // If one or more exist, suggest table repair since we got here because the options
1791 // table could not be accessed.
1792 foreach ($wpdb->tables as $table) {
1793 // If one of the WP tables exist, then we are in an insane state.
1794 if ( in_array($wpdb->prefix . $table, $tables) ) {
1795 // If visiting repair.php, return true and let it take over.
1796 if ( defined('WP_REPAIRING') )
1797 return true;
1798 // Die with a DB error.
1799 $wpdb->error = __('One or more database tables are unavailable. The database may need to be <a href="maint/repair.php?referrer=is_blog_installed">repaired</a>.');
1800 dead_db();
1801 }
1802 }
1803
1804 wp_cache_set( 'is_blog_installed', false );
1805
1806 return false;
1807}
1808
1809/**
1810 * Retrieve URL with nonce added to URL query.
1811 *
1812 * @package WordPress
1813 * @subpackage Security
1814 * @since 2.0.4
1815 *
1816 * @param string $actionurl URL to add nonce action
1817 * @param string $action Optional. Nonce action name
1818 * @return string URL with nonce action added.
1819 */
1820function wp_nonce_url( $actionurl, $action = -1 ) {
1821 $actionurl = str_replace( '&amp;', '&', $actionurl );
1822 return esc_html( add_query_arg( '_wpnonce', wp_create_nonce( $action ), $actionurl ) );
1823}
1824
1825/**
1826 * Retrieve or display nonce hidden field for forms.
1827 *
1828 * The nonce field is used to validate that the contents of the form came from
1829 * the location on the current site and not somewhere else. The nonce does not
1830 * offer absolute protection, but should protect against most cases. It is very
1831 * important to use nonce field in forms.
1832 *
1833 * If you set $echo to true and set $referer to true, then you will need to
1834 * retrieve the {@link wp_referer_field() wp referer field}. If you have the
1835 * $referer set to true and are echoing the nonce field, it will also echo the
1836 * referer field.
1837 *
1838 * The $action and $name are optional, but if you want to have better security,
1839 * it is strongly suggested to set those two parameters. It is easier to just
1840 * call the function without any parameters, because validation of the nonce
1841 * doesn't require any parameters, but since crackers know what the default is
1842 * it won't be difficult for them to find a way around your nonce and cause
1843 * damage.
1844 *
1845 * The input name will be whatever $name value you gave. The input value will be
1846 * the nonce creation value.
1847 *
1848 * @package WordPress
1849 * @subpackage Security
1850 * @since 2.0.4
1851 *
1852 * @param string $action Optional. Action name.
1853 * @param string $name Optional. Nonce name.
1854 * @param bool $referer Optional, default true. Whether to set the referer field for validation.
1855 * @param bool $echo Optional, default true. Whether to display or return hidden form field.
1856 * @return string Nonce field.
1857 */
1858function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
1859 $name = esc_attr( $name );
1860 $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
1861 if ( $echo )
1862 echo $nonce_field;
1863
1864 if ( $referer )
1865 wp_referer_field( $echo, 'previous' );
1866
1867 return $nonce_field;
1868}
1869
1870/**
1871 * Retrieve or display referer hidden field for forms.
1872 *
1873 * The referer link is the current Request URI from the server super global. The
1874 * input name is '_wp_http_referer', in case you wanted to check manually.
1875 *
1876 * @package WordPress
1877 * @subpackage Security
1878 * @since 2.0.4
1879 *
1880 * @param bool $echo Whether to echo or return the referer field.
1881 * @return string Referer field.
1882 */
1883function wp_referer_field( $echo = true) {
1884 $ref = esc_attr( $_SERVER['REQUEST_URI'] );
1885 $referer_field = '<input type="hidden" name="_wp_http_referer" value="'. $ref . '" />';
1886
1887 if ( $echo )
1888 echo $referer_field;
1889 return $referer_field;
1890}
1891
1892/**
1893 * Retrieve or display original referer hidden field for forms.
1894 *
1895 * The input name is '_wp_original_http_referer' and will be either the same
1896 * value of {@link wp_referer_field()}, if that was posted already or it will
1897 * be the current page, if it doesn't exist.
1898 *
1899 * @package WordPress
1900 * @subpackage Security
1901 * @since 2.0.4
1902 *
1903 * @param bool $echo Whether to echo the original http referer
1904 * @param string $jump_back_to Optional, default is 'current'. Can be 'previous' or page you want to jump back to.
1905 * @return string Original referer field.
1906 */
1907function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
1908 $jump_back_to = ( 'previous' == $jump_back_to ) ? wp_get_referer() : $_SERVER['REQUEST_URI'];
1909 $ref = ( wp_get_original_referer() ) ? wp_get_original_referer() : $jump_back_to;
1910 $orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( stripslashes( $ref ) ) . '" />';
1911 if ( $echo )
1912 echo $orig_referer_field;
1913 return $orig_referer_field;
1914}
1915
1916/**
1917 * Retrieve referer from '_wp_http_referer', HTTP referer, or current page respectively.
1918 *
1919 * @package WordPress
1920 * @subpackage Security
1921 * @since 2.0.4
1922 *
1923 * @return string|bool False on failure. Referer URL on success.
1924 */
1925function wp_get_referer() {
1926 $ref = '';
1927 if ( ! empty( $_REQUEST['_wp_http_referer'] ) )
1928 $ref = $_REQUEST['_wp_http_referer'];
1929 else if ( ! empty( $_SERVER['HTTP_REFERER'] ) )
1930 $ref = $_SERVER['HTTP_REFERER'];
1931
1932 if ( $ref !== $_SERVER['REQUEST_URI'] )
1933 return $ref;
1934 return false;
1935}
1936
1937/**
1938 * Retrieve original referer that was posted, if it exists.
1939 *
1940 * @package WordPress
1941 * @subpackage Security
1942 * @since 2.0.4
1943 *
1944 * @return string|bool False if no original referer or original referer if set.
1945 */
1946function wp_get_original_referer() {
1947 if ( !empty( $_REQUEST['_wp_original_http_referer'] ) )
1948 return $_REQUEST['_wp_original_http_referer'];
1949 return false;
1950}
1951
1952/**
1953 * Recursive directory creation based on full path.
1954 *
1955 * Will attempt to set permissions on folders.
1956 *
1957 * @since 2.0.1
1958 *
1959 * @param string $target Full path to attempt to create.
1960 * @return bool Whether the path was created or not. True if path already exists.
1961 */
1962function wp_mkdir_p( $target ) {
1963 // from php.net/mkdir user contributed notes
1964 $target = str_replace( '//', '/', $target );
1965 if ( file_exists( $target ) )
1966 return @is_dir( $target );
1967
1968 // Attempting to create the directory may clutter up our display.
1969 if ( @mkdir( $target ) ) {
1970 $stat = @stat( dirname( $target ) );
1971 $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
1972 @chmod( $target, $dir_perms );
1973 return true;
1974 } elseif ( is_dir( dirname( $target ) ) ) {
1975 return false;
1976 }
1977
1978 // If the above failed, attempt to create the parent node, then try again.
1979 if ( ( $target != '/' ) && ( wp_mkdir_p( dirname( $target ) ) ) )
1980 return wp_mkdir_p( $target );
1981
1982 return false;
1983}
1984
1985/**
1986 * Test if a give filesystem path is absolute ('/foo/bar', 'c:\windows').
1987 *
1988 * @since 2.5.0
1989 *
1990 * @param string $path File path
1991 * @return bool True if path is absolute, false is not absolute.
1992 */
1993function path_is_absolute( $path ) {
1994 // this is definitive if true but fails if $path does not exist or contains a symbolic link
1995 if ( realpath($path) == $path )
1996 return true;
1997
1998 if ( strlen($path) == 0 || $path{0} == '.' )
1999 return false;
2000
2001 // windows allows absolute paths like this
2002 if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
2003 return true;
2004
2005 // a path starting with / or \ is absolute; anything else is relative
2006 return (bool) preg_match('#^[/\\\\]#', $path);
2007}
2008
2009/**
2010 * Join two filesystem paths together (e.g. 'give me $path relative to $base').
2011 *
2012 * If the $path is absolute, then it the full path is returned.
2013 *
2014 * @since 2.5.0
2015 *
2016 * @param string $base
2017 * @param string $path
2018 * @return string The path with the base or absolute path.
2019 */
2020function path_join( $base, $path ) {
2021 if ( path_is_absolute($path) )
2022 return $path;
2023
2024 return rtrim($base, '/') . '/' . ltrim($path, '/');
2025}
2026
2027/**
2028 * Get an array containing the current upload directory's path and url.
2029 *
2030 * Checks the 'upload_path' option, which should be from the web root folder,
2031 * and if it isn't empty it will be used. If it is empty, then the path will be
2032 * 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will
2033 * override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.
2034 *
2035 * The upload URL path is set either by the 'upload_url_path' option or by using
2036 * the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.
2037 *
2038 * If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in
2039 * the administration settings panel), then the time will be used. The format
2040 * will be year first and then month.
2041 *
2042 * If the path couldn't be created, then an error will be returned with the key
2043 * 'error' containing the error message. The error suggests that the parent
2044 * directory is not writable by the server.
2045 *
2046 * On success, the returned array will have many indices:
2047 * 'path' - base directory and sub directory or full path to upload directory.
2048 * 'url' - base url and sub directory or absolute URL to upload directory.
2049 * 'subdir' - sub directory if uploads use year/month folders option is on.
2050 * 'basedir' - path without subdir.
2051 * 'baseurl' - URL path without subdir.
2052 * 'error' - set to false.
2053 *
2054 * @since 2.0.0
2055 * @uses apply_filters() Calls 'upload_dir' on returned array.
2056 *
2057 * @param string $time Optional. Time formatted in 'yyyy/mm'.
2058 * @return array See above for description.
2059 */
2060function wp_upload_dir( $time = null ) {
2061 $siteurl = get_option( 'siteurl' );
2062 $upload_path = get_option( 'upload_path' );
2063 $upload_path = trim($upload_path);
2064 if ( empty($upload_path) )
2065 $dir = WP_CONTENT_DIR . '/uploads';
2066 else
2067 $dir = $upload_path;
2068
2069 // $dir is absolute, $path is (maybe) relative to ABSPATH
2070 $dir = path_join( ABSPATH, $dir );
2071
2072 if ( !$url = get_option( 'upload_url_path' ) ) {
2073 if ( empty($upload_path) or ( $upload_path == $dir ) )
2074 $url = WP_CONTENT_URL . '/uploads';
2075 else
2076 $url = trailingslashit( $siteurl ) . $upload_path;
2077 }
2078
2079 if ( defined('UPLOADS') ) {
2080 $dir = ABSPATH . UPLOADS;
2081 $url = trailingslashit( $siteurl ) . UPLOADS;
2082 }
2083
2084 $bdir = $dir;
2085 $burl = $url;
2086
2087 $subdir = '';
2088 if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
2089 // Generate the yearly and monthly dirs
2090 if ( !$time )
2091 $time = current_time( 'mysql' );
2092 $y = substr( $time, 0, 4 );
2093 $m = substr( $time, 5, 2 );
2094 $subdir = "/$y/$m";
2095 }
2096
2097 $dir .= $subdir;
2098 $url .= $subdir;
2099
2100 $uploads = apply_filters( 'upload_dir', array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false ) );
2101
2102 // Make sure we have an uploads dir
2103 if ( ! wp_mkdir_p( $uploads['path'] ) ) {
2104 $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $uploads['path'] );
2105 return array( 'error' => $message );
2106 }
2107
2108 return $uploads;
2109}
2110
2111/**
2112 * Get a filename that is sanitized and unique for the given directory.
2113 *
2114 * If the filename is not unique, then a number will be added to the filename
2115 * before the extension, and will continue adding numbers until the filename is
2116 * unique.
2117 *
2118 * The callback must accept two parameters, the first one is the directory and
2119 * the second is the filename. The callback must be a function.
2120 *
2121 * @since 2.5
2122 *
2123 * @param string $dir
2124 * @param string $filename
2125 * @param string $unique_filename_callback Function name, must be a function.
2126 * @return string New filename, if given wasn't unique.
2127 */
2128function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
2129 // sanitize the file name before we begin processing
2130 $filename = sanitize_file_name($filename);
2131
2132 // separate the filename into a name and extension
2133 $info = pathinfo($filename);
2134 $ext = !empty($info['extension']) ? '.' . $info['extension'] : '';
2135 $name = basename($filename, $ext);
2136
2137 // edge case: if file is named '.ext', treat as an empty name
2138 if( $name === $ext )
2139 $name = '';
2140
2141 // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
2142 if ( $unique_filename_callback && function_exists( $unique_filename_callback ) ) {
2143 $filename = $unique_filename_callback( $dir, $name );
2144 } else {
2145 $number = '';
2146
2147 // change '.ext' to lower case
2148 if ( $ext && strtolower($ext) != $ext ) {
2149 $ext2 = strtolower($ext);
2150 $filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
2151
2152 // check for both lower and upper case extension or image sub-sizes may be overwritten
2153 while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
2154 $new_number = $number + 1;
2155 $filename = str_replace( "$number$ext", "$new_number$ext", $filename );
2156 $filename2 = str_replace( "$number$ext2", "$new_number$ext2", $filename2 );
2157 $number = $new_number;
2158 }
2159 return $filename2;
2160 }
2161
2162 while ( file_exists( $dir . "/$filename" ) ) {
2163 if ( '' == "$number$ext" )
2164 $filename = $filename . ++$number . $ext;
2165 else
2166 $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
2167 }
2168 }
2169
2170 return $filename;
2171}
2172
2173/**
2174 * Create a file in the upload folder with given content.
2175 *
2176 * If there is an error, then the key 'error' will exist with the error message.
2177 * If success, then the key 'file' will have the unique file path, the 'url' key
2178 * will have the link to the new file. and the 'error' key will be set to false.
2179 *
2180 * This function will not move an uploaded file to the upload folder. It will
2181 * create a new file with the content in $bits parameter. If you move the upload
2182 * file, read the content of the uploaded file, and then you can give the
2183 * filename and content to this function, which will add it to the upload
2184 * folder.
2185 *
2186 * The permissions will be set on the new file automatically by this function.
2187 *
2188 * @since 2.0.0
2189 *
2190 * @param string $name
2191 * @param null $deprecated Not used. Set to null.
2192 * @param mixed $bits File content
2193 * @param string $time Optional. Time formatted in 'yyyy/mm'.
2194 * @return array
2195 */
2196function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
2197 if ( empty( $name ) )
2198 return array( 'error' => __( 'Empty filename' ) );
2199
2200 $wp_filetype = wp_check_filetype( $name );
2201 if ( !$wp_filetype['ext'] )
2202 return array( 'error' => __( 'Invalid file type' ) );
2203
2204 $upload = wp_upload_dir( $time );
2205
2206 if ( $upload['error'] !== false )
2207 return $upload;
2208
2209 $filename = wp_unique_filename( $upload['path'], $name );
2210
2211 $new_file = $upload['path'] . "/$filename";
2212 if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
2213 $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), dirname( $new_file ) );
2214 return array( 'error' => $message );
2215 }
2216
2217 $ifp = @ fopen( $new_file, 'wb' );
2218 if ( ! $ifp )
2219 return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
2220
2221 @fwrite( $ifp, $bits );
2222 fclose( $ifp );
2223 // Set correct file permissions
2224 $stat = @ stat( dirname( $new_file ) );
2225 $perms = $stat['mode'] & 0007777;
2226 $perms = $perms & 0000666;
2227 @ chmod( $new_file, $perms );
2228
2229 // Compute the URL
2230 $url = $upload['url'] . "/$filename";
2231
2232 return array( 'file' => $new_file, 'url' => $url, 'error' => false );
2233}
2234
2235/**
2236 * Retrieve the file type based on the extension name.
2237 *
2238 * @package WordPress
2239 * @since 2.5.0
2240 * @uses apply_filters() Calls 'ext2type' hook on default supported types.
2241 *
2242 * @param string $ext The extension to search.
2243 * @return string|null The file type, example: audio, video, document, spreadsheet, etc. Null if not found.
2244 */
2245function wp_ext2type( $ext ) {
2246 $ext2type = apply_filters('ext2type', array(
2247 'audio' => array('aac','ac3','aif','aiff','mp1','mp2','mp3','m3a','m4a','m4b','ogg','ram','wav','wma'),
2248 'video' => array('asf','avi','divx','dv','mov','mpg','mpeg','mp4','mpv','ogm','qt','rm','vob','wmv', 'm4v'),
2249 'document' => array('doc','docx','pages','odt','rtf','pdf'),
2250 'spreadsheet' => array('xls','xlsx','numbers','ods'),
2251 'interactive' => array('ppt','pptx','key','odp','swf'),
2252 'text' => array('txt'),
2253 'archive' => array('tar','bz2','gz','cab','dmg','rar','sea','sit','sqx','zip'),
2254 'code' => array('css','html','php','js'),
2255 ));
2256 foreach ( $ext2type as $type => $exts )
2257 if ( in_array($ext, $exts) )
2258 return $type;
2259}
2260
2261/**
2262 * Retrieve the file type from the file name.
2263 *
2264 * You can optionally define the mime array, if needed.
2265 *
2266 * @since 2.0.4
2267 *
2268 * @param string $filename File name or path.
2269 * @param array $mimes Optional. Key is the file extension with value as the mime type.
2270 * @return array Values with extension first and mime type.
2271 */
2272function wp_check_filetype( $filename, $mimes = null ) {
2273 if ( empty($mimes) )
2274 $mimes = get_allowed_mime_types();
2275 $type = false;
2276 $ext = false;
2277
2278 foreach ( $mimes as $ext_preg => $mime_match ) {
2279 $ext_preg = '!\.(' . $ext_preg . ')$!i';
2280 if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
2281 $type = $mime_match;
2282 $ext = $ext_matches[1];
2283 break;
2284 }
2285 }
2286
2287 return compact( 'ext', 'type' );
2288}
2289
2290/**
2291 * Retrieve list of allowed mime types and file extensions.
2292 *
2293 * @since 2.8.6
2294 *
2295 * @return array Array of mime types keyed by the file extension regex corresponding to those types.
2296 */
2297function get_allowed_mime_types() {
2298 static $mimes = false;
2299
2300 if ( !$mimes ) {
2301 // Accepted MIME types are set here as PCRE unless provided.
2302 $mimes = apply_filters( 'upload_mimes', array(
2303 'jpg|jpeg|jpe' => 'image/jpeg',
2304 'gif' => 'image/gif',
2305 'png' => 'image/png',
2306 'bmp' => 'image/bmp',
2307 'tif|tiff' => 'image/tiff',
2308 'ico' => 'image/x-icon',
2309 'asf|asx|wax|wmv|wmx' => 'video/asf',
2310 'avi' => 'video/avi',
2311 'divx' => 'video/divx',
2312 'flv' => 'video/x-flv',
2313 'mov|qt' => 'video/quicktime',
2314 'mpeg|mpg|mpe' => 'video/mpeg',
2315 'txt|c|cc|h' => 'text/plain',
2316 'rtx' => 'text/richtext',
2317 'css' => 'text/css',
2318 'htm|html' => 'text/html',
2319 'mp3|m4a' => 'audio/mpeg',
2320 'mp4|m4v' => 'video/mp4',
2321 'ra|ram' => 'audio/x-realaudio',
2322 'wav' => 'audio/wav',
2323 'ogg' => 'audio/ogg',
2324 'mid|midi' => 'audio/midi',
2325 'wma' => 'audio/wma',
2326 'rtf' => 'application/rtf',
2327 'js' => 'application/javascript',
2328 'pdf' => 'application/pdf',
2329 'doc|docx' => 'application/msword',
2330 'pot|pps|ppt|pptx' => 'application/vnd.ms-powerpoint',
2331 'wri' => 'application/vnd.ms-write',
2332 'xla|xls|xlsx|xlt|xlw' => 'application/vnd.ms-excel',
2333 'mdb' => 'application/vnd.ms-access',
2334 'mpp' => 'application/vnd.ms-project',
2335 'swf' => 'application/x-shockwave-flash',
2336 'class' => 'application/java',
2337 'tar' => 'application/x-tar',
2338 'zip' => 'application/zip',
2339 'gz|gzip' => 'application/x-gzip',
2340 'exe' => 'application/x-msdownload',
2341 // openoffice formats
2342 'odt' => 'application/vnd.oasis.opendocument.text',
2343 'odp' => 'application/vnd.oasis.opendocument.presentation',
2344 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
2345 'odg' => 'application/vnd.oasis.opendocument.graphics',
2346 'odc' => 'application/vnd.oasis.opendocument.chart',
2347 'odb' => 'application/vnd.oasis.opendocument.database',
2348 'odf' => 'application/vnd.oasis.opendocument.formula',
2349 ) );
2350 }
2351
2352 return $mimes;
2353}
2354
2355/**
2356 * Retrieve nonce action "Are you sure" message.
2357 *
2358 * The action is split by verb and noun. The action format is as follows:
2359 * verb-action_extra. The verb is before the first dash and has the format of
2360 * letters and no spaces and numbers. The noun is after the dash and before the
2361 * underscore, if an underscore exists. The noun is also only letters.
2362 *
2363 * The filter will be called for any action, which is not defined by WordPress.
2364 * You may use the filter for your plugin to explain nonce actions to the user,
2365 * when they get the "Are you sure?" message. The filter is in the format of
2366 * 'explain_nonce_$verb-$noun' with the $verb replaced by the found verb and the
2367 * $noun replaced by the found noun. The two parameters that are given to the
2368 * hook are the localized "Are you sure you want to do this?" message with the
2369 * extra text (the text after the underscore).
2370 *
2371 * @package WordPress
2372 * @subpackage Security
2373 * @since 2.0.4
2374 *
2375 * @param string $action Nonce action.
2376 * @return string Are you sure message.
2377 */
2378function wp_explain_nonce( $action ) {
2379 if ( $action !== -1 && preg_match( '/([a-z]+)-([a-z]+)(_(.+))?/', $action, $matches ) ) {
2380 $verb = $matches[1];
2381 $noun = $matches[2];
2382
2383 $trans = array();
2384 $trans['update']['attachment'] = array( __( 'Your attempt to edit this attachment: &#8220;%s&#8221; has failed.' ), 'get_the_title' );
2385
2386 $trans['add']['category'] = array( __( 'Your attempt to add this category has failed.' ), false );
2387 $trans['delete']['category'] = array( __( 'Your attempt to delete this category: &#8220;%s&#8221; has failed.' ), 'get_cat_name' );
2388 $trans['update']['category'] = array( __( 'Your attempt to edit this category: &#8220;%s&#8221; has failed.' ), 'get_cat_name' );
2389
2390 $trans['delete']['comment'] = array( __( 'Your attempt to delete this comment: &#8220;%s&#8221; has failed.' ), 'use_id' );
2391 $trans['unapprove']['comment'] = array( __( 'Your attempt to unapprove this comment: &#8220;%s&#8221; has failed.' ), 'use_id' );
2392 $trans['approve']['comment'] = array( __( 'Your attempt to approve this comment: &#8220;%s&#8221; has failed.' ), 'use_id' );
2393 $trans['update']['comment'] = array( __( 'Your attempt to edit this comment: &#8220;%s&#8221; has failed.' ), 'use_id' );
2394 $trans['bulk']['comments'] = array( __( 'Your attempt to bulk modify comments has failed.' ), false );
2395 $trans['moderate']['comments'] = array( __( 'Your attempt to moderate comments has failed.' ), false );
2396
2397 $trans['add']['bookmark'] = array( __( 'Your attempt to add this link has failed.' ), false );
2398 $trans['delete']['bookmark'] = array( __( 'Your attempt to delete this link: &#8220;%s&#8221; has failed.' ), 'use_id' );
2399 $trans['update']['bookmark'] = array( __( 'Your attempt to edit this link: &#8220;%s&#8221; has failed.' ), 'use_id' );
2400 $trans['bulk']['bookmarks'] = array( __( 'Your attempt to bulk modify links has failed.' ), false );
2401
2402 $trans['add']['page'] = array( __( 'Your attempt to add this page has failed.' ), false );
2403 $trans['delete']['page'] = array( __( 'Your attempt to delete this page: &#8220;%s&#8221; has failed.' ), 'get_the_title' );
2404 $trans['update']['page'] = array( __( 'Your attempt to edit this page: &#8220;%s&#8221; has failed.' ), 'get_the_title' );
2405
2406 $trans['edit']['plugin'] = array( __( 'Your attempt to edit this plugin file: &#8220;%s&#8221; has failed.' ), 'use_id' );
2407 $trans['activate']['plugin'] = array( __( 'Your attempt to activate this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
2408 $trans['deactivate']['plugin'] = array( __( 'Your attempt to deactivate this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
2409 $trans['upgrade']['plugin'] = array( __( 'Your attempt to upgrade this plugin: &#8220;%s&#8221; has failed.' ), 'use_id' );
2410
2411 $trans['add']['post'] = array( __( 'Your attempt to add this post has failed.' ), false );
2412 $trans['delete']['post'] = array( __( 'Your attempt to delete this post: &#8220;%s&#8221; has failed.' ), 'get_the_title' );
2413 $trans['update']['post'] = array( __( 'Your attempt to edit this post: &#8220;%s&#8221; has failed.' ), 'get_the_title' );
2414
2415 $trans['add']['user'] = array( __( 'Your attempt to add this user has failed.' ), false );
2416 $trans['delete']['users'] = array( __( 'Your attempt to delete users has failed.' ), false );
2417 $trans['bulk']['users'] = array( __( 'Your attempt to bulk modify users has failed.' ), false );
2418 $trans['update']['user'] = array( __( 'Your attempt to edit this user: &#8220;%s&#8221; has failed.' ), 'get_the_author_meta', 'display_name' );
2419 $trans['update']['profile'] = array( __( 'Your attempt to modify the profile for: &#8220;%s&#8221; has failed.' ), 'get_the_author_meta', 'display_name' );
2420
2421 $trans['update']['options'] = array( __( 'Your attempt to edit your settings has failed.' ), false );
2422 $trans['update']['permalink'] = array( __( 'Your attempt to change your permalink structure to: %s has failed.' ), 'use_id' );
2423 $trans['edit']['file'] = array( __( 'Your attempt to edit this file: &#8220;%s&#8221; has failed.' ), 'use_id' );
2424 $trans['edit']['theme'] = array( __( 'Your attempt to edit this theme file: &#8220;%s&#8221; has failed.' ), 'use_id' );
2425 $trans['switch']['theme'] = array( __( 'Your attempt to switch to this theme: &#8220;%s&#8221; has failed.' ), 'use_id' );
2426
2427 $trans['log']['out'] = array( sprintf( __( 'You are attempting to log out of %s' ), get_bloginfo( 'sitename' ) ), false );
2428
2429 if ( isset( $trans[$verb][$noun] ) ) {
2430 if ( !empty( $trans[$verb][$noun][1] ) ) {
2431 $lookup = $trans[$verb][$noun][1];
2432 if ( isset($trans[$verb][$noun][2]) )
2433 $lookup_value = $trans[$verb][$noun][2];
2434 $object = $matches[4];
2435 if ( 'use_id' != $lookup ) {
2436 if ( isset( $lookup_value ) )
2437 $object = call_user_func( $lookup, $lookup_value, $object );
2438 else
2439 $object = call_user_func( $lookup, $object );
2440 }
2441 return sprintf( $trans[$verb][$noun][0], esc_html($object) );
2442 } else {
2443 return $trans[$verb][$noun][0];
2444 }
2445 }
2446
2447 return apply_filters( 'explain_nonce_' . $verb . '-' . $noun, __( 'Are you sure you want to do this?' ), isset($matches[4]) ? $matches[4] : '' );
2448 } else {
2449 return apply_filters( 'explain_nonce_' . $action, __( 'Are you sure you want to do this?' ) );
2450 }
2451}
2452
2453/**
2454 * Display "Are You Sure" message to confirm the action being taken.
2455 *
2456 * If the action has the nonce explain message, then it will be displayed along
2457 * with the "Are you sure?" message.
2458 *
2459 * @package WordPress
2460 * @subpackage Security
2461 * @since 2.0.4
2462 *
2463 * @param string $action The nonce action.
2464 */
2465function wp_nonce_ays( $action ) {
2466 $title = __( 'WordPress Failure Notice' );
2467 $html = esc_html( wp_explain_nonce( $action ) );
2468 if ( wp_get_referer() )
2469 $html .= "</p><p><a href='" . esc_url( remove_query_arg( 'updated', wp_get_referer() ) ) . "'>" . __( 'Please try again.' ) . "</a>";
2470 elseif ( 'log-out' == $action )
2471 $html .= "</p><p>" . sprintf( __( "Do you really want to <a href='%s'>log out</a>?"), wp_logout_url() );
2472
2473 wp_die( $html, $title);
2474}
2475
2476/**
2477 * Kill WordPress execution and display HTML message with error message.
2478 *
2479 * Call this function complements the die() PHP function. The difference is that
2480 * HTML will be displayed to the user. It is recommended to use this function
2481 * only, when the execution should not continue any further. It is not
2482 * recommended to call this function very often and try to handle as many errors
2483 * as possible siliently.
2484 *
2485 * @since 2.0.4
2486 *
2487 * @param string $message Error message.
2488 * @param string $title Error title.
2489 * @param string|array $args Optional arguements to control behaviour.
2490 */
2491function wp_die( $message, $title = '', $args = array() ) {
2492 global $wp_locale;
2493
2494 $defaults = array( 'response' => 500 );
2495 $r = wp_parse_args($args, $defaults);
2496
2497 $have_gettext = function_exists('__');
2498
2499 if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
2500 if ( empty( $title ) ) {
2501 $error_data = $message->get_error_data();
2502 if ( is_array( $error_data ) && isset( $error_data['title'] ) )
2503 $title = $error_data['title'];
2504 }
2505 $errors = $message->get_error_messages();
2506 switch ( count( $errors ) ) :
2507 case 0 :
2508 $message = '';
2509 break;
2510 case 1 :
2511 $message = "<p>{$errors[0]}</p>";
2512 break;
2513 default :
2514 $message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
2515 break;
2516 endswitch;
2517 } elseif ( is_string( $message ) ) {
2518 $message = "<p>$message</p>";
2519 }
2520
2521 if ( isset( $r['back_link'] ) && $r['back_link'] ) {
2522 $back_text = $have_gettext? __('&laquo; Back') : '&laquo; Back';
2523 $message .= "\n<p><a href='javascript:history.back()'>$back_text</p>";
2524 }
2525
2526 if ( defined( 'WP_SITEURL' ) && '' != WP_SITEURL )
2527 $admin_dir = WP_SITEURL . '/wp-admin/';
2528 elseif ( function_exists( 'get_bloginfo' ) && '' != get_bloginfo( 'wpurl' ) )
2529 $admin_dir = get_bloginfo( 'wpurl' ) . '/wp-admin/';
2530 elseif ( strpos( $_SERVER['PHP_SELF'], 'wp-admin' ) !== false )
2531 $admin_dir = '';
2532 else
2533 $admin_dir = 'wp-admin/';
2534
2535 if ( !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ) :
2536 if( !headers_sent() ){
2537 status_header( $r['response'] );
2538 nocache_headers();
2539 header( 'Content-Type: text/html; charset=utf-8' );
2540 }
2541
2542 if ( empty($title) ) {
2543 $title = $have_gettext? __('WordPress &rsaquo; Error') : 'WordPress &rsaquo; Error';
2544 }
2545
2546 $text_direction = 'ltr';
2547 if ( isset($r['text_direction']) && $r['text_direction'] == 'rtl' ) $text_direction = 'rtl';
2548 if ( ( $wp_locale ) && ( 'rtl' == $wp_locale->text_direction ) ) $text_direction = 'rtl';
2549?>
2550<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2551<html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) ) language_attributes(); ?>>
2552<head>
2553 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2554 <title><?php echo $title ?></title>
2555 <link rel="stylesheet" href="<?php echo $admin_dir; ?>css/install.css" type="text/css" />
2556<?php
2557if ( 'rtl' == $text_direction ) : ?>
2558 <link rel="stylesheet" href="<?php echo $admin_dir; ?>css/install-rtl.css" type="text/css" />
2559<?php endif; ?>
2560</head>
2561<body id="error-page">
2562<?php endif; ?>
2563 <?php echo $message; ?>
2564</body>
2565<!-- Ticket #8942, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono -->
2566</html>
2567<?php
2568 die();
2569}
2570
2571/**
2572 * Retrieve the WordPress home page URL.
2573 *
2574 * If the constant named 'WP_HOME' exists, then it willl be used and returned by
2575 * the function. This can be used to counter the redirection on your local
2576 * development environment.
2577 *
2578 * @access private
2579 * @package WordPress
2580 * @since 2.2.0
2581 *
2582 * @param string $url URL for the home location
2583 * @return string Homepage location.
2584 */
2585function _config_wp_home( $url = '' ) {
2586 if ( defined( 'WP_HOME' ) )
2587 return WP_HOME;
2588 return $url;
2589}
2590
2591/**
2592 * Retrieve the WordPress site URL.
2593 *
2594 * If the constant named 'WP_SITEURL' is defined, then the value in that
2595 * constant will always be returned. This can be used for debugging a site on
2596 * your localhost while not having to change the database to your URL.
2597 *
2598 * @access private
2599 * @package WordPress
2600 * @since 2.2.0
2601 *
2602 * @param string $url URL to set the WordPress site location.
2603 * @return string The WordPress Site URL
2604 */
2605function _config_wp_siteurl( $url = '' ) {
2606 if ( defined( 'WP_SITEURL' ) )
2607 return WP_SITEURL;
2608 return $url;
2609}
2610
2611/**
2612 * Set the localized direction for MCE plugin.
2613 *
2614 * Will only set the direction to 'rtl', if the WordPress locale has the text
2615 * direction set to 'rtl'.
2616 *
2617 * Fills in the 'directionality', 'plugins', and 'theme_advanced_button1' array
2618 * keys. These keys are then returned in the $input array.
2619 *
2620 * @access private
2621 * @package WordPress
2622 * @subpackage MCE
2623 * @since 2.1.0
2624 *
2625 * @param array $input MCE plugin array.
2626 * @return array Direction set for 'rtl', if needed by locale.
2627 */
2628function _mce_set_direction( $input ) {
2629 global $wp_locale;
2630
2631 if ( 'rtl' == $wp_locale->text_direction ) {
2632 $input['directionality'] = 'rtl';
2633 $input['plugins'] .= ',directionality';
2634 $input['theme_advanced_buttons1'] .= ',ltr';
2635 }
2636
2637 return $input;
2638}
2639
2640
2641/**
2642 * Convert smiley code to the icon graphic file equivalent.
2643 *
2644 * You can turn off smilies, by going to the write setting screen and unchecking
2645 * the box, or by setting 'use_smilies' option to false or removing the option.
2646 *
2647 * Plugins may override the default smiley list by setting the $wpsmiliestrans
2648 * to an array, with the key the code the blogger types in and the value the
2649 * image file.
2650 *
2651 * The $wp_smiliessearch global is for the regular expression and is set each
2652 * time the function is called.
2653 *
2654 * The full list of smilies can be found in the function and won't be listed in
2655 * the description. Probably should create a Codex page for it, so that it is
2656 * available.
2657 *
2658 * @global array $wpsmiliestrans
2659 * @global array $wp_smiliessearch
2660 * @since 2.2.0
2661 */
2662function smilies_init() {
2663 global $wpsmiliestrans, $wp_smiliessearch;
2664
2665 // don't bother setting up smilies if they are disabled
2666 if ( !get_option( 'use_smilies' ) )
2667 return;
2668
2669 if ( !isset( $wpsmiliestrans ) ) {
2670 $wpsmiliestrans = array(
2671 ':mrgreen:' => 'icon_mrgreen.gif',
2672 ':neutral:' => 'icon_neutral.gif',
2673 ':twisted:' => 'icon_twisted.gif',
2674 ':arrow:' => 'icon_arrow.gif',
2675 ':shock:' => 'icon_eek.gif',
2676 ':smile:' => 'icon_smile.gif',
2677 ':???:' => 'icon_confused.gif',
2678 ':cool:' => 'icon_cool.gif',
2679 ':evil:' => 'icon_evil.gif',
2680 ':grin:' => 'icon_biggrin.gif',
2681 ':idea:' => 'icon_idea.gif',
2682 ':oops:' => 'icon_redface.gif',
2683 ':razz:' => 'icon_razz.gif',
2684 ':roll:' => 'icon_rolleyes.gif',
2685 ':wink:' => 'icon_wink.gif',
2686 ':cry:' => 'icon_cry.gif',
2687 ':eek:' => 'icon_surprised.gif',
2688 ':lol:' => 'icon_lol.gif',
2689 ':mad:' => 'icon_mad.gif',
2690 ':sad:' => 'icon_sad.gif',
2691 '8-)' => 'icon_cool.gif',
2692 '8-O' => 'icon_eek.gif',
2693 ':-(' => 'icon_sad.gif',
2694 ':-)' => 'icon_smile.gif',
2695 ':-?' => 'icon_confused.gif',
2696 ':-D' => 'icon_biggrin.gif',
2697 ':-P' => 'icon_razz.gif',
2698 ':-o' => 'icon_surprised.gif',
2699 ':-x' => 'icon_mad.gif',
2700 ':-|' => 'icon_neutral.gif',
2701 ';-)' => 'icon_wink.gif',
2702 '8)' => 'icon_cool.gif',
2703 '8O' => 'icon_eek.gif',
2704 ':(' => 'icon_sad.gif',
2705 ':)' => 'icon_smile.gif',
2706 ':?' => 'icon_confused.gif',
2707 ':D' => 'icon_biggrin.gif',
2708 ':P' => 'icon_razz.gif',
2709 ':o' => 'icon_surprised.gif',
2710 ':x' => 'icon_mad.gif',
2711 ':|' => 'icon_neutral.gif',
2712 ';)' => 'icon_wink.gif',
2713 ':!:' => 'icon_exclaim.gif',
2714 ':?:' => 'icon_question.gif',
2715 );
2716 }
2717
2718 if (count($wpsmiliestrans) == 0) {
2719 return;
2720 }
2721
2722 /*
2723 * NOTE: we sort the smilies in reverse key order. This is to make sure
2724 * we match the longest possible smilie (:???: vs :?) as the regular
2725 * expression used below is first-match
2726 */
2727 krsort($wpsmiliestrans);
2728
2729 $wp_smiliessearch = '/(?:\s|^)';
2730
2731 $subchar = '';
2732 foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
2733 $firstchar = substr($smiley, 0, 1);
2734 $rest = substr($smiley, 1);
2735
2736 // new subpattern?
2737 if ($firstchar != $subchar) {
2738 if ($subchar != '') {
2739 $wp_smiliessearch .= ')|(?:\s|^)';
2740 }
2741 $subchar = $firstchar;
2742 $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
2743 } else {
2744 $wp_smiliessearch .= '|';
2745 }
2746 $wp_smiliessearch .= preg_quote($rest, '/');
2747 }
2748
2749 $wp_smiliessearch .= ')(?:\s|$)/m';
2750}
2751
2752/**
2753 * Merge user defined arguments into defaults array.
2754 *
2755 * This function is used throughout WordPress to allow for both string or array
2756 * to be merged into another array.
2757 *
2758 * @since 2.2.0
2759 *
2760 * @param string|array $args Value to merge with $defaults
2761 * @param array $defaults Array that serves as the defaults.
2762 * @return array Merged user defined values with defaults.
2763 */
2764function wp_parse_args( $args, $defaults = '' ) {
2765 if ( is_object( $args ) )
2766 $r = get_object_vars( $args );
2767 elseif ( is_array( $args ) )
2768 $r =& $args;
2769 else
2770 wp_parse_str( $args, $r );
2771
2772 if ( is_array( $defaults ) )
2773 return array_merge( $defaults, $r );
2774 return $r;
2775}
2776
2777/**
2778 * Determines if default embed handlers should be loaded.
2779 *
2780 * Checks to make sure that the embeds library hasn't already been loaded. If
2781 * it hasn't, then it will load the embeds library.
2782 *
2783 * @since 2.9
2784 */
2785function wp_maybe_load_embeds() {
2786 if ( ! apply_filters('load_default_embeds', true) )
2787 return;
2788 require_once( ABSPATH . WPINC . '/default-embeds.php' );
2789}
2790
2791/**
2792 * Determines if Widgets library should be loaded.
2793 *
2794 * Checks to make sure that the widgets library hasn't already been loaded. If
2795 * it hasn't, then it will load the widgets library and run an action hook.
2796 *
2797 * @since 2.2.0
2798 * @uses add_action() Calls '_admin_menu' hook with 'wp_widgets_add_menu' value.
2799 */
2800function wp_maybe_load_widgets() {
2801 if ( ! apply_filters('load_default_widgets', true) )
2802 return;
2803 require_once( ABSPATH . WPINC . '/default-widgets.php' );
2804 add_action( '_admin_menu', 'wp_widgets_add_menu' );
2805}
2806
2807/**
2808 * Append the Widgets menu to the themes main menu.
2809 *
2810 * @since 2.2.0
2811 * @uses $submenu The administration submenu list.
2812 */
2813function wp_widgets_add_menu() {
2814 global $submenu;
2815 $submenu['themes.php'][7] = array( __( 'Widgets' ), 'switch_themes', 'widgets.php' );
2816 ksort( $submenu['themes.php'], SORT_NUMERIC );
2817}
2818
2819/**
2820 * Flush all output buffers for PHP 5.2.
2821 *
2822 * Make sure all output buffers are flushed before our singletons our destroyed.
2823 *
2824 * @since 2.2.0
2825 */
2826function wp_ob_end_flush_all() {
2827 $levels = ob_get_level();
2828 for ($i=0; $i<$levels; $i++)
2829 ob_end_flush();
2830}
2831
2832/**
2833 * Load the correct database class file.
2834 *
2835 * This function is used to load the database class file either at runtime or by
2836 * wp-admin/setup-config.php We must globalise $wpdb to ensure that it is
2837 * defined globally by the inline code in wp-db.php.
2838 *
2839 * @since 2.5.0
2840 * @global $wpdb WordPress Database Object
2841 */
2842function require_wp_db() {
2843 global $wpdb;
2844 if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
2845 require_once( WP_CONTENT_DIR . '/db.php' );
2846 else
2847 require_once( ABSPATH . WPINC . '/wp-db.php' );
2848}
2849
2850/**
2851 * Load custom DB error or display WordPress DB error.
2852 *
2853 * If a file exists in the wp-content directory named db-error.php, then it will
2854 * be loaded instead of displaying the WordPress DB error. If it is not found,
2855 * then the WordPress DB error will be displayed instead.
2856 *
2857 * The WordPress DB error sets the HTTP status header to 500 to try to prevent
2858 * search engines from caching the message. Custom DB messages should do the
2859 * same.
2860 *
2861 * This function was backported to the the WordPress 2.3.2, but originally was
2862 * added in WordPress 2.5.0.
2863 *
2864 * @since 2.3.2
2865 * @uses $wpdb
2866 */
2867function dead_db() {
2868 global $wpdb;
2869
2870 // Load custom DB error template, if present.
2871 if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
2872 require_once( WP_CONTENT_DIR . '/db-error.php' );
2873 die();
2874 }
2875
2876 // If installing or in the admin, provide the verbose message.
2877 if ( defined('WP_INSTALLING') || defined('WP_ADMIN') )
2878 wp_die($wpdb->error);
2879
2880 // Otherwise, be terse.
2881 status_header( 500 );
2882 nocache_headers();
2883 header( 'Content-Type: text/html; charset=utf-8' );
2884?>
2885<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2886<html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) ) language_attributes(); ?>>
2887<head>
2888<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2889 <title>Database Error</title>
2890
2891</head>
2892<body>
2893 <h1>Error establishing a database connection</h1>
2894</body>
2895</html>
2896<?php
2897 die();
2898}
2899
2900/**
2901 * Converts value to nonnegative integer.
2902 *
2903 * @since 2.5.0
2904 *
2905 * @param mixed $maybeint Data you wish to have convered to an nonnegative integer
2906 * @return int An nonnegative integer
2907 */
2908function absint( $maybeint ) {
2909 return abs( intval( $maybeint ) );
2910}
2911
2912/**
2913 * Determines if the blog can be accessed over SSL.
2914 *
2915 * Determines if blog can be accessed over SSL by using cURL to access the site
2916 * using the https in the siteurl. Requires cURL extension to work correctly.
2917 *
2918 * @since 2.5.0
2919 *
2920 * @return bool Whether or not SSL access is available
2921 */
2922function url_is_accessable_via_ssl($url)
2923{
2924 if (in_array('curl', get_loaded_extensions())) {
2925 $ssl = preg_replace( '/^http:\/\//', 'https://', $url );
2926
2927 $ch = curl_init();
2928 curl_setopt($ch, CURLOPT_URL, $ssl);
2929 curl_setopt($ch, CURLOPT_FAILONERROR, true);
2930 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2931 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
2932 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
2933
2934 curl_exec($ch);
2935
2936 $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2937 curl_close ($ch);
2938
2939 if ($status == 200 || $status == 401) {
2940 return true;
2941 }
2942 }
2943 return false;
2944}
2945
2946/**
2947 * Secure URL, if available or the given URL.
2948 *
2949 * @since 2.5.0
2950 *
2951 * @param string $url Complete URL path with transport.
2952 * @return string Secure or regular URL path.
2953 */
2954function atom_service_url_filter($url)
2955{
2956 if ( url_is_accessable_via_ssl($url) )
2957 return preg_replace( '/^http:\/\//', 'https://', $url );
2958 else
2959 return $url;
2960}
2961
2962/**
2963 * Marks a function as deprecated and informs when it has been used.
2964 *
2965 * There is a hook deprecated_function_run that will be called that can be used
2966 * to get the backtrace up to what file and function called the deprecated
2967 * function.
2968 *
2969 * The current behavior is to trigger an user error if WP_DEBUG is true.
2970 *
2971 * This function is to be used in every function in depreceated.php
2972 *
2973 * @package WordPress
2974 * @package Debug
2975 * @since 2.5.0
2976 * @access private
2977 *
2978 * @uses do_action() Calls 'deprecated_function_run' and passes the function name and what to use instead.
2979 * @uses apply_filters() Calls 'deprecated_function_trigger_error' and expects boolean value of true to do trigger or false to not trigger error.
2980 *
2981 * @param string $function The function that was called
2982 * @param string $version The version of WordPress that deprecated the function
2983 * @param string $replacement Optional. The function that should have been called
2984 */
2985function _deprecated_function($function, $version, $replacement=null) {
2986
2987 do_action('deprecated_function_run', $function, $replacement);
2988
2989 // Allow plugin to filter the output error trigger
2990 if( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true )) {
2991 if( !is_null($replacement) )
2992 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
2993 else
2994 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
2995 }
2996}
2997
2998/**
2999 * Marks a file as deprecated and informs when it has been used.
3000 *
3001 * There is a hook deprecated_file_included that will be called that can be used
3002 * to get the backtrace up to what file and function included the deprecated
3003 * file.
3004 *
3005 * The current behavior is to trigger an user error if WP_DEBUG is true.
3006 *
3007 * This function is to be used in every file that is depreceated
3008 *
3009 * @package WordPress
3010 * @package Debug
3011 * @since 2.5.0
3012 * @access private
3013 *
3014 * @uses do_action() Calls 'deprecated_file_included' and passes the file name and what to use instead.
3015 * @uses apply_filters() Calls 'deprecated_file_trigger_error' and expects boolean value of true to do trigger or false to not trigger error.
3016 *
3017 * @param string $file The file that was included
3018 * @param string $version The version of WordPress that deprecated the function
3019 * @param string $replacement Optional. The function that should have been called
3020 */
3021function _deprecated_file($file, $version, $replacement=null) {
3022
3023 do_action('deprecated_file_included', $file, $replacement);
3024
3025 // Allow plugin to filter the output error trigger
3026 if( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
3027 if( !is_null($replacement) )
3028 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) );
3029 else
3030 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $file, $version ) );
3031 }
3032}
3033
3034/**
3035 * Is the server running earlier than 1.5.0 version of lighttpd
3036 *
3037 * @since 2.5.0
3038 *
3039 * @return bool Whether the server is running lighttpd < 1.5.0
3040 */
3041function is_lighttpd_before_150() {
3042 $server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
3043 $server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
3044 return 'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
3045}
3046
3047/**
3048 * Does the specified module exist in the apache config?
3049 *
3050 * @since 2.5.0
3051 *
3052 * @param string $mod e.g. mod_rewrite
3053 * @param bool $default The default return value if the module is not found
3054 * @return bool
3055 */
3056function apache_mod_loaded($mod, $default = false) {
3057 global $is_apache;
3058
3059 if ( !$is_apache )
3060 return false;
3061
3062 if ( function_exists('apache_get_modules') ) {
3063 $mods = apache_get_modules();
3064 if ( in_array($mod, $mods) )
3065 return true;
3066 } elseif ( function_exists('phpinfo') ) {
3067 ob_start();
3068 phpinfo(8);
3069 $phpinfo = ob_get_clean();
3070 if ( false !== strpos($phpinfo, $mod) )
3071 return true;
3072 }
3073 return $default;
3074}
3075
3076/**
3077 * File validates against allowed set of defined rules.
3078 *
3079 * A return value of '1' means that the $file contains either '..' or './'. A
3080 * return value of '2' means that the $file contains ':' after the first
3081 * character. A return value of '3' means that the file is not in the allowed
3082 * files list.
3083 *
3084 * @since 1.2.0
3085 *
3086 * @param string $file File path.
3087 * @param array $allowed_files List of allowed files.
3088 * @return int 0 means nothing is wrong, greater than 0 means something was wrong.
3089 */
3090function validate_file( $file, $allowed_files = '' ) {
3091 if ( false !== strpos( $file, '..' ))
3092 return 1;
3093
3094 if ( false !== strpos( $file, './' ))
3095 return 1;
3096
3097 if (':' == substr( $file, 1, 1 ))
3098 return 2;
3099
3100 if (!empty ( $allowed_files ) && (!in_array( $file, $allowed_files ) ) )
3101 return 3;
3102
3103 return 0;
3104}
3105
3106/**
3107 * Determine if SSL is used.
3108 *
3109 * @since 2.6.0
3110 *
3111 * @return bool True if SSL, false if not used.
3112 */
3113function is_ssl() {
3114 if ( isset($_SERVER['HTTPS']) ) {
3115 if ( 'on' == strtolower($_SERVER['HTTPS']) )
3116 return true;
3117 if ( '1' == $_SERVER['HTTPS'] )
3118 return true;
3119 } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
3120 return true;
3121 }
3122 return false;
3123}
3124
3125/**
3126 * Whether SSL login should be forced.
3127 *
3128 * @since 2.6.0
3129 *
3130 * @param string|bool $force Optional.
3131 * @return bool True if forced, false if not forced.
3132 */
3133function force_ssl_login( $force = null ) {
3134 static $forced = false;
3135
3136 if ( !is_null( $force ) ) {
3137 $old_forced = $forced;
3138 $forced = $force;
3139 return $old_forced;
3140 }
3141
3142 return $forced;
3143}
3144
3145/**
3146 * Whether to force SSL used for the Administration Panels.
3147 *
3148 * @since 2.6.0
3149 *
3150 * @param string|bool $force
3151 * @return bool True if forced, false if not forced.
3152 */
3153function force_ssl_admin( $force = null ) {
3154 static $forced = false;
3155
3156 if ( !is_null( $force ) ) {
3157 $old_forced = $forced;
3158 $forced = $force;
3159 return $old_forced;
3160 }
3161
3162 return $forced;
3163}
3164
3165/**
3166 * Guess the URL for the site.
3167 *
3168 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
3169 * directory.
3170 *
3171 * @since 2.6.0
3172 *
3173 * @return string
3174 */
3175function wp_guess_url() {
3176 if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
3177 $url = WP_SITEURL;
3178 } else {
3179 $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
3180 $url = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
3181 }
3182 return $url;
3183}
3184
3185/**
3186 * Suspend cache invalidation.
3187 *
3188 * Turns cache invalidation on and off. Useful during imports where you don't wont to do invalidations
3189 * every time a post is inserted. Callers must be sure that what they are doing won't lead to an inconsistent
3190 * cache when invalidation is suspended.
3191 *
3192 * @since 2.7.0
3193 *
3194 * @param bool $suspend Whether to suspend or enable cache invalidation
3195 * @return bool The current suspend setting
3196 */
3197function wp_suspend_cache_invalidation($suspend = true) {
3198 global $_wp_suspend_cache_invalidation;
3199
3200 $current_suspend = $_wp_suspend_cache_invalidation;
3201 $_wp_suspend_cache_invalidation = $suspend;
3202 return $current_suspend;
3203}
3204
3205/**
3206 * Retrieve site option value based on option name.
3207 *
3208 * @see get_option()
3209 * @package WordPress
3210 * @subpackage Option
3211 * @since 2.8.0
3212 * @uses apply_filters() Calls 'pre_site_option_$option' null to allow overwriting
3213 * the option value in a plugin.
3214 * @uses apply_filters() Calls 'site_option_$option' with the option name value.
3215 *
3216 * @param string $option Name of option to retrieve. Should already be SQL-escaped
3217 * @param mixed $default Optional value to return if option doesn't exist
3218 * @param use_cache Used only in WordPress MU.
3219 * @return mixed Value set for the option.
3220 */
3221function get_site_option( $option, $default = false, $use_cache = true ) {
3222 // Allow plugins to short-circuit site options.
3223 $pre = apply_filters( 'pre_site_option_' . $option, null );
3224 if ( null !== $pre )
3225 return $pre;
3226
3227 $value = get_option( $option, $default ) ;
3228
3229 return apply_filters( 'site_option_' . $option, $value );
3230}
3231
3232/**
3233 * Add a new site option.
3234 *
3235 * @see add_option()
3236 * @package WordPress
3237 * @subpackage Option
3238 * @since 2.8.0
3239 * @link http://alex.vort-x.net/blog/ Thanks Alex Stapleton
3240 * @uses apply_filters() Calls 'pre_add_option_$option' to allow overwriting
3241 * the option value in a plugin.
3242 *
3243 * @param string $option Name of option to add. Expects to not be SQL escaped.
3244 * @param mixed $value Optional. Option value, can be anything.
3245 * @return bool False if option was not added and true if option was added.
3246 */
3247function add_site_option( $option, $value ) {
3248 $value = apply_filters( 'pre_add_site_option_' . $option, $value );
3249 do_action( 'add_site_option', $option, $value );
3250 $result = add_option( $option, $value );
3251 if ( $result ) {
3252 do_action( "add_site_option_{$option}", $option, $value );
3253 do_action( 'added_site_option', $option, $value );
3254 }
3255 return $result;
3256}
3257
3258/**
3259 * Removes site option by name.
3260 *
3261 * @see delete_option()
3262 * @package WordPress
3263 * @subpackage Option
3264 * @since 2.8.0
3265 *
3266 * @param string $option Name of option to remove. Expected to be SQL-escaped.
3267 * @return bool True, if succeed. False, if failure.
3268 */
3269function delete_site_option( $option ) {
3270 do_action( 'delete_site_option', $option );
3271 $result = delete_option( $option );
3272 if ( $result ) {
3273 do_action( "delete_site_option_{$option}", $option );
3274 do_action( 'deleted_site_option', $option );
3275 }
3276 return $result;
3277}
3278
3279/**
3280 * Update the value of a site option that was already added.
3281 *
3282 * @see update_option()
3283 * @since 2.8.0
3284 * @package WordPress
3285 * @subpackage Option
3286 * @uses apply_filters() calls 'pre_update_site_option_$option' to allow overwriting
3287 * the option value in a plugin.
3288 * @param string $option Option name. Expected to not be SQL-escaped
3289 * @param mixed $value Option value.
3290 * @return bool False if value was not updated and true if value was updated.
3291 */
3292function update_site_option( $option, $value ) {
3293 $old_value = get_site_option( $option );
3294 $new_value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
3295
3296 if ( $new_value === $old_value )
3297 return false;
3298
3299 if ( false === $old_value )
3300 return add_site_option( $option, $new_value );
3301
3302 do_action( 'update_site_option', $option, $old_value, $new_value );
3303 $result = update_option( $option, $new_value );
3304 if ( $result ) {
3305 do_action( "update_site_option_{$option}", $option, $old_value, $new_value );
3306 do_action( 'updated_site_option', $option, $old_value, $new_value );
3307 }
3308 return $result;
3309}
3310
3311/**
3312 * Delete a site transient
3313 *
3314 * @see delete_transient()
3315 * @since 2.9.0
3316 * @package WordPress
3317 * @subpackage Transient
3318 *
3319 * @param string $transient Transient name. Expected to not be SQL-escaped
3320 * @return bool true if successful, false otherwise
3321 */
3322function delete_site_transient($transient) {
3323 global $_wp_using_ext_object_cache;
3324
3325 do_action( 'delete_site_transient', $transient );
3326 if ( $_wp_using_ext_object_cache ) {
3327 $result = wp_cache_delete( $transient, 'site-transient' );
3328 } else {
3329 $result = delete_site_option( '_site_transient_' . esc_sql( $transient ) );
3330 }
3331
3332 if ( $result ) {
3333 do_action( "delete_site_transient_{$transient}", $transient );
3334 do_action( 'deleted_site_transient', $transient );
3335 }
3336 return $result;
3337}
3338
3339/**
3340 * Get the value of a site transient.
3341 *
3342 * If the transient does not exist or does not have a value, then the return value
3343 * will be false.
3344 *
3345 * @see get_transient()
3346 * @since 2.9.0
3347 * @package WordPress
3348 * @subpackage Transient
3349 * @uses apply_filters() calls 'pre_site_transient_$transient' and 'site_transient_$transient'
3350 to allow overwriting the transient value in a plugin.
3351 *
3352 * @param string $transient Transient name. Expected to not be SQL-escaped
3353 * @return mixed Value of transient
3354 */
3355function get_site_transient($transient) {
3356 global $_wp_using_ext_object_cache;
3357
3358 $pre = apply_filters( 'pre_site_transient_' . $transient, null );
3359 if ( null !== $pre )
3360 return $pre;
3361
3362 if ( $_wp_using_ext_object_cache ) {
3363 $value = wp_cache_get( $transient, 'site-transient' );
3364 } else {
3365 $safe_transient = esc_sql( $transient );
3366 $transient_option = '_site_transient_' . $safe_transient;
3367 $transient_timeout = '_site_transient_timeout_' . $safe_transient;
3368 if ( get_site_option( $transient_timeout ) < time() ) {
3369 delete_site_option( $transient_option );
3370 delete_site_option( $transient_timeout );
3371 return false;
3372 }
3373
3374 $value = get_site_option( $transient_option );
3375 }
3376
3377 return apply_filters( 'site_transient_' . $transient, $value );
3378}
3379
3380/**
3381 * Set/update the value of a site transient
3382 *
3383 * You do not need to serialize values, if the value needs to be serialize, then
3384 * it will be serialized before it is set.
3385 *
3386 * @see set_transient()
3387 * @since 2.9.0
3388 * @package WordPress
3389 * @subpackage Transient
3390 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' null to allow overwriting
3391 * the transient value in a plugin.
3392 * @uses apply_filters() Calls 'pre_set_site_transient_expiration_$transient' null to allow overwriting
3393 * the transient expiration in a plugin.
3394 *
3395 * @param string $transient Transient name. Expected to not be SQL-escaped
3396 * @param mixed $value Transient value.
3397 * @param int $expiration Time until expiration in seconds, default 0
3398 * @return bool False if value was not set and true if value was set.
3399 */
3400function set_site_transient($transient, $value, $expiration = 0) {
3401 global $_wp_using_ext_object_cache;
3402
3403 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value, $expiration );
3404 $expiration = apply_filters( 'pre_set_site_transient_expiration_' . $transient, $expiration, $value );
3405
3406 do_action( 'set_site_transient', $transient, $value, $expiration );
3407 if ( $_wp_using_ext_object_cache ) {
3408 $result = wp_cache_set($transient, $value, 'site-transient', $expiration);
3409 } else {
3410 $transient_timeout = '_site_transient_timeout_' . $transient;
3411 $transient = '_site_transient_' . $transient;
3412 $safe_transient = esc_sql($transient);
3413 if ( false === get_site_option( $safe_transient ) ) {
3414 if ( 0 != $expiration )
3415 add_site_option( $transient_timeout, time() + $expiration );
3416 $result = add_site_option($transient, $value);
3417 } else {
3418 if ( 0 != $expiration )
3419 update_site_option( $transient_timeout, time() + $expiration );
3420 $result = update_site_option( $transient, $value );
3421 }
3422 }
3423 if ( $result ) {
3424 do_action( "set_site_transient_{$transient}", $transient, $value, $expiration );
3425 do_action( 'setted_site_transient', $transient, $value, $expiration );
3426 }
3427 return $result;
3428}
3429
3430/**
3431 * gmt_offset modification for smart timezone handling
3432 *
3433 * Overrides the gmt_offset option if we have a timezone_string available
3434 */
3435function wp_timezone_override_offset() {
3436 if ( !wp_timezone_supported() ) {
3437 return false;
3438 }
3439 if ( !$timezone_string = get_option( 'timezone_string' ) ) {
3440 return false;
3441 }
3442
3443 @date_default_timezone_set( $timezone_string );
3444 $timezone_object = timezone_open( $timezone_string );
3445 $datetime_object = date_create();
3446 if ( false === $timezone_object || false === $datetime_object ) {
3447 return false;
3448 }
3449 return round( timezone_offset_get( $timezone_object, $datetime_object ) / 3600, 2 );
3450}
3451
3452/**
3453 * Check for PHP timezone support
3454 */
3455function wp_timezone_supported() {
3456 $support = false;
3457 if (
3458 function_exists( 'date_default_timezone_set' ) &&
3459 function_exists( 'timezone_identifiers_list' ) &&
3460 function_exists( 'timezone_open' ) &&
3461 function_exists( 'timezone_offset_get' )
3462 ) {
3463 $support = true;
3464 }
3465 return apply_filters( 'timezone_support', $support );
3466}
3467
3468function _wp_timezone_choice_usort_callback( $a, $b ) {
3469 // Don't use translated versions of Etc
3470 if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
3471 // Make the order of these more like the old dropdown
3472 if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
3473 return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
3474 }
3475 if ( 'UTC' === $a['city'] ) {
3476 if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
3477 return 1;
3478 }
3479 return -1;
3480 }
3481 if ( 'UTC' === $b['city'] ) {
3482 if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
3483 return -1;
3484 }
3485 return 1;
3486 }
3487 return strnatcasecmp( $a['city'], $b['city'] );
3488 }
3489 if ( $a['t_continent'] == $b['t_continent'] ) {
3490 if ( $a['t_city'] == $b['t_city'] ) {
3491 return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
3492 }
3493 return strnatcasecmp( $a['t_city'], $b['t_city'] );
3494 } else {
3495 // Force Etc to the bottom of the list
3496 if ( 'Etc' === $a['continent'] ) {
3497 return 1;
3498 }
3499 if ( 'Etc' === $b['continent'] ) {
3500 return -1;
3501 }
3502 return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
3503 }
3504}
3505
3506/**
3507 * Gives a nicely formatted list of timezone strings // temporary! Not in final
3508 *
3509 * @param $selected_zone string Selected Zone
3510 *
3511 */
3512function wp_timezone_choice( $selected_zone ) {
3513 static $mo_loaded = false;
3514
3515 $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific', 'Etc' );
3516
3517 // Load translations for continents and cities
3518 if ( !$mo_loaded ) {
3519 $locale = get_locale();
3520 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo';
3521 load_textdomain( 'continents-cities', $mofile );
3522 $mo_loaded = true;
3523 }
3524
3525 $zonen = array();
3526 foreach ( timezone_identifiers_list() as $zone ) {
3527 $zone = explode( '/', $zone );
3528 if ( !in_array( $zone[0], $continents ) ) {
3529 continue;
3530 }
3531 if ( 'Etc' === $zone[0] && in_array( $zone[1], array( 'UCT', 'GMT', 'GMT0', 'GMT+0', 'GMT-0', 'Greenwich', 'Universal', 'Zulu' ) ) ) {
3532 continue;
3533 }
3534
3535 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
3536 $exists = array(
3537 0 => ( isset( $zone[0] ) && $zone[0] ) ? true : false,
3538 1 => ( isset( $zone[1] ) && $zone[1] ) ? true : false,
3539 2 => ( isset( $zone[2] ) && $zone[2] ) ? true : false
3540 );
3541 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ) ? true : false;
3542 $exists[4] = ( $exists[1] && $exists[3] ) ? true : false;
3543 $exists[5] = ( $exists[2] && $exists[3] ) ? true : false;
3544
3545 $zonen[] = array(
3546 'continent' => ( $exists[0] ? $zone[0] : '' ),
3547 'city' => ( $exists[1] ? $zone[1] : '' ),
3548 'subcity' => ( $exists[2] ? $zone[2] : '' ),
3549 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
3550 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
3551 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
3552 );
3553 }
3554 usort( $zonen, '_wp_timezone_choice_usort_callback' );
3555
3556 $structure = array();
3557
3558 if ( empty( $selected_zone ) ) {
3559 $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
3560 }
3561
3562 foreach ( $zonen as $key => $zone ) {
3563 // Build value in an array to join later
3564 $value = array( $zone['continent'] );
3565
3566 if ( empty( $zone['city'] ) ) {
3567 // It's at the continent level (generally won't happen)
3568 $display = $zone['t_continent'];
3569 } else {
3570 // It's inside a continent group
3571
3572 // Continent optgroup
3573 if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
3574 $label = ( 'Etc' === $zone['continent'] ) ? __( 'Manual offsets' ) : $zone['t_continent'];
3575 $structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
3576 }
3577
3578 // Add the city to the value
3579 $value[] = $zone['city'];
3580 if ( 'Etc' === $zone['continent'] ) {
3581 if ( 'UTC' === $zone['city'] ) {
3582 $display = '';
3583 } else {
3584 $display = str_replace( 'GMT', '', $zone['city'] );
3585 $display = strtr( $display, '+-', '-+' ) . ':00';
3586 }
3587 $display = sprintf( __( 'UTC %s' ), $display );
3588 } else {
3589 $display = $zone['t_city'];
3590 if ( !empty( $zone['subcity'] ) ) {
3591 // Add the subcity to the value
3592 $value[] = $zone['subcity'];
3593 $display .= ' - ' . $zone['t_subcity'];
3594 }
3595 }
3596 }
3597
3598 // Build the value
3599 $value = join( '/', $value );
3600 $selected = '';
3601 if ( $value === $selected_zone ) {
3602 $selected = 'selected="selected" ';
3603 }
3604 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
3605
3606 // Close continent optgroup
3607 if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) {
3608 $structure[] = '</optgroup>';
3609 }
3610 }
3611
3612 return join( "\n", $structure );
3613}
3614
3615/**
3616 * Strip close comment and close php tags from file headers used by WP
3617 * See http://core.trac.wordpress.org/ticket/8497
3618 *
3619 * @since 2.8
3620**/
3621function _cleanup_header_comment($str) {
3622 return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
3623}
3624
3625/**
3626 * Permanently deletes posts, pages, attachments, and comments which have been in the trash for EMPTY_TRASH_DAYS.
3627 *
3628 * @since 2.9.0
3629 *
3630 * @return void
3631 */
3632function wp_scheduled_delete() {
3633 global $wpdb;
3634
3635 $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
3636
3637 $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
3638
3639 foreach ( (array) $posts_to_delete as $post ) {
3640 wp_delete_post($post['post_id']);
3641 }
3642
3643 $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A);
3644
3645 foreach ( (array) $comments_to_delete as $comment ) {
3646 wp_delete_comment($comment['comment_id']);
3647 }
3648}
3649
3650/**
3651 * Parse the file contents to retrieve its metadata.
3652 *
3653 * Searches for metadata for a file, such as a plugin or theme. Each piece of
3654 * metadata must be on its own line. For a field spanning multple lines, it
3655 * must not have any newlines or only parts of it will be displayed.
3656 *
3657 * Some users have issues with opening large files and manipulating the contents
3658 * for want is usually the first 1kiB or 2kiB. This function stops pulling in
3659 * the file contents when it has all of the required data.
3660 *
3661 * The first 8kiB of the file will be pulled in and if the file data is not
3662 * within that first 8kiB, then the author should correct their plugin file
3663 * and move the data headers to the top.
3664 *
3665 * The file is assumed to have permissions to allow for scripts to read
3666 * the file. This is not checked however and the file is only opened for
3667 * reading.
3668 *
3669 * @since 2.9.0
3670 *
3671 * @param string $file Path to the file
3672 * @param bool $markup If the returned data should have HTML markup applied
3673 * @param string $context If specified adds filter hook "extra_<$context>_headers"
3674 */
3675function get_file_data( $file, $default_headers, $context = '' ) {
3676 // We don't need to write to the file, so just open for reading.
3677 $fp = fopen( $file, 'r' );
3678
3679 // Pull only the first 8kiB of the file in.
3680 $file_data = fread( $fp, 8192 );
3681
3682 // PHP will close file handle, but we are good citizens.
3683 fclose( $fp );
3684
3685 if( $context != '' ) {
3686 $extra_headers = apply_filters( "extra_$context".'_headers', array() );
3687
3688 $extra_headers = array_flip( $extra_headers );
3689 foreach( $extra_headers as $key=>$value ) {
3690 $extra_headers[$key] = $key;
3691 }
3692 $all_headers = array_merge($extra_headers, $default_headers);
3693 } else {
3694 $all_headers = $default_headers;
3695 }
3696
3697
3698 foreach ( $all_headers as $field => $regex ) {
3699 preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
3700 if ( !empty( ${$field} ) )
3701 ${$field} = _cleanup_header_comment( ${$field}[1] );
3702 else
3703 ${$field} = '';
3704 }
3705
3706 $file_data = compact( array_keys( $all_headers ) );
3707
3708 return $file_data;
3709}
3710/*
3711 * Used internally to tidy up the search terms
3712 *
3713 * @private
3714 * @since 2.9.0
3715 */
3716function _search_terms_tidy($t) {
3717 return trim($t, "\"\'\n\r ");
3718}
3719?>