8 require( ABSPATH . WPINC . '/option.php' );
11 * Convert given date string into a different format.
13 * $format should be either a PHP date format string, e.g. 'U' for a Unix
14 * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
16 * If $translate is true then the given date and format string will
17 * be passed to date_i18n() for translation.
21 * @param string $format Format of the date to return.
22 * @param string $date Date string to convert.
23 * @param bool $translate Whether the return date should be translated. Default true.
24 * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
26 function mysql2date( $format, $date, $translate = true ) {
31 return strtotime( $date . ' +0000' );
33 $i = strtotime( $date );
39 return date_i18n( $format, $i );
41 return date( $format, $i );
45 * Retrieve the current time based on specified type.
47 * The 'mysql' type will return the time in the format for MySQL DATETIME field.
48 * The 'timestamp' type will return the current timestamp.
49 * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
51 * If $gmt is set to either '1' or 'true', then both types will use GMT time.
52 * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
56 * @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
57 * format string (e.g. 'Y-m-d').
58 * @param int|bool $gmt Optional. Whether to use GMT timezone. Default false.
59 * @return int|string Integer if $type is 'timestamp', string otherwise.
61 function current_time( $type, $gmt = 0 ) {
64 return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
66 return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
68 return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
73 * Retrieve the date in localized format, based on timestamp.
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.
81 * @global WP_Locale $wp_locale
83 * @param string $dateformatstring Format to display the date.
84 * @param bool|int $unixtimestamp Optional. Unix timestamp. Default false.
85 * @param bool $gmt Optional. Whether to use GMT timezone. Default false.
87 * @return string The date, translated if locale specifies it.
89 function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
95 $i = current_time( 'timestamp' );
98 // we should not let date() interfere with our
99 // specially computed timestamp
104 * Store original value for language with untypical grammars.
105 * See https://core.trac.wordpress.org/ticket/9396
107 $req_format = $dateformatstring;
109 $datefunc = $gmt? 'gmdate' : 'date';
111 if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
112 $datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) );
113 $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
114 $dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
115 $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
116 $datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
117 $datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) );
118 $dateformatstring = ' '.$dateformatstring;
119 $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
120 $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
121 $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
122 $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
123 $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
124 $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
126 $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
128 $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
129 $timezone_formats_re = implode( '|', $timezone_formats );
130 if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
131 $timezone_string = get_option( 'timezone_string' );
132 if ( $timezone_string ) {
133 $timezone_object = timezone_open( $timezone_string );
134 $date_object = date_create( null, $timezone_object );
135 foreach ( $timezone_formats as $timezone_format ) {
136 if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
137 $formatted = date_format( $date_object, $timezone_format );
138 $dateformatstring = ' '.$dateformatstring;
139 $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
140 $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
145 $j = @$datefunc( $dateformatstring, $i );
148 * Filters the date formatted based on the locale.
152 * @param string $j Formatted date string.
153 * @param string $req_format Format to display the date.
154 * @param int $i Unix timestamp.
155 * @param bool $gmt Whether to convert to GMT for time. Default false.
157 $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
162 * Determines if the date should be declined.
164 * If the locale specifies that month names require a genitive case in certain
165 * formats (like 'j F Y'), the month name will be replaced with a correct form.
169 * @param string $date Formatted date string.
170 * @return string The date, declined if locale specifies it.
172 function wp_maybe_decline_date( $date ) {
175 // i18n functions are not available in SHORTINIT mode
176 if ( ! function_exists( '_x' ) ) {
180 /* translators: If months in your language require a genitive case,
181 * translate this to 'on'. Do not translate into your own language.
183 if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
184 // Match a format like 'j F Y' or 'j. F'
185 if ( @preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) {
186 $months = $wp_locale->month;
187 $months_genitive = $wp_locale->month_genitive;
189 foreach ( $months as $key => $month ) {
190 $months[ $key ] = '# ' . $month . '( |$)#u';
193 foreach ( $months_genitive as $key => $month ) {
194 $months_genitive[ $key ] = ' ' . $month . '$1';
197 $date = preg_replace( $months, $months_genitive, $date );
201 // Used for locale-specific rules
202 $locale = get_locale();
204 if ( 'ca' === $locale ) {
205 // " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
206 $date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
213 * Convert float number to format based on the locale.
217 * @global WP_Locale $wp_locale
219 * @param float $number The number to convert based on locale.
220 * @param int $decimals Optional. Precision of the number of decimal places. Default 0.
221 * @return string Converted number in string format.
223 function number_format_i18n( $number, $decimals = 0 ) {
226 if ( isset( $wp_locale ) ) {
227 $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
229 $formatted = number_format( $number, absint( $decimals ) );
233 * Filters the number formatted based on the locale.
237 * @param string $formatted Converted number in string format.
239 return apply_filters( 'number_format_i18n', $formatted );
243 * Convert number of bytes largest unit bytes will fit into.
245 * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
246 * number of bytes to human readable number by taking the number of that unit
247 * that the bytes will go into it. Supports TB value.
249 * Please note that integers in PHP are limited to 32 bits, unless they are on
250 * 64 bit architecture, then they have 64 bit size. If you need to place the
251 * larger size then what PHP integer type will hold, then use a string. It will
252 * be converted to a double, which should always have 64 bit length.
254 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
258 * @param int|string $bytes Number of bytes. Note max integer size for integers.
259 * @param int $decimals Optional. Precision of number of decimal places. Default 0.
260 * @return string|false False on failure. Number string on success.
262 function size_format( $bytes, $decimals = 0 ) {
271 if ( 0 === $bytes ) {
272 return number_format_i18n( 0, $decimals ) . ' B';
275 foreach ( $quant as $unit => $mag ) {
276 if ( doubleval( $bytes ) >= $mag ) {
277 return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
285 * Get the week start and end from the datetime or date string from MySQL.
289 * @param string $mysqlstring Date or datetime field type from MySQL.
290 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
291 * @return array Keys are 'start' and 'end'.
293 function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
294 // MySQL string year.
295 $my = substr( $mysqlstring, 0, 4 );
297 // MySQL string month.
298 $mm = substr( $mysqlstring, 8, 2 );
301 $md = substr( $mysqlstring, 5, 2 );
303 // The timestamp for MySQL string day.
304 $day = mktime( 0, 0, 0, $md, $mm, $my );
306 // The day of the week from the timestamp.
307 $weekday = date( 'w', $day );
309 if ( !is_numeric($start_of_week) )
310 $start_of_week = get_option( 'start_of_week' );
312 if ( $weekday < $start_of_week )
315 // The most recent week start day on or before $day.
316 $start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week );
318 // $start + 1 week - 1 second.
319 $end = $start + WEEK_IN_SECONDS - 1;
320 return compact( 'start', 'end' );
324 * Unserialize value only if it was serialized.
328 * @param string $original Maybe unserialized original, if is needed.
329 * @return mixed Unserialized data can be any type.
331 function maybe_unserialize( $original ) {
332 if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
333 return @unserialize( $original );
338 * Check value to find if it was serialized.
340 * If $data is not an string, then returned value will always be false.
341 * Serialized data is always a string.
345 * @param string $data Value to check to see if was serialized.
346 * @param bool $strict Optional. Whether to be strict about the end of the string. Default true.
347 * @return bool False if not serialized and true if it was.
349 function is_serialized( $data, $strict = true ) {
350 // if it isn't a string, it isn't serialized.
351 if ( ! is_string( $data ) ) {
354 $data = trim( $data );
355 if ( 'N;' == $data ) {
358 if ( strlen( $data ) < 4 ) {
361 if ( ':' !== $data[1] ) {
365 $lastc = substr( $data, -1 );
366 if ( ';' !== $lastc && '}' !== $lastc ) {
370 $semicolon = strpos( $data, ';' );
371 $brace = strpos( $data, '}' );
372 // Either ; or } must exist.
373 if ( false === $semicolon && false === $brace )
375 // But neither must be in the first X characters.
376 if ( false !== $semicolon && $semicolon < 3 )
378 if ( false !== $brace && $brace < 4 )
385 if ( '"' !== substr( $data, -2, 1 ) ) {
388 } elseif ( false === strpos( $data, '"' ) ) {
391 // or else fall through
394 return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
398 $end = $strict ? '$' : '';
399 return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data );
405 * Check whether serialized data is of string type.
409 * @param string $data Serialized data.
410 * @return bool False if not a serialized string, true if it is.
412 function is_serialized_string( $data ) {
413 // if it isn't a string, it isn't a serialized string.
414 if ( ! is_string( $data ) ) {
417 $data = trim( $data );
418 if ( strlen( $data ) < 4 ) {
420 } elseif ( ':' !== $data[1] ) {
422 } elseif ( ';' !== substr( $data, -1 ) ) {
424 } elseif ( $data[0] !== 's' ) {
426 } elseif ( '"' !== substr( $data, -2, 1 ) ) {
434 * Serialize data, if needed.
438 * @param string|array|object $data Data that might be serialized.
439 * @return mixed A scalar data
441 function maybe_serialize( $data ) {
442 if ( is_array( $data ) || is_object( $data ) )
443 return serialize( $data );
445 // Double serialization is required for backward compatibility.
446 // See https://core.trac.wordpress.org/ticket/12930
447 // Also the world will end. See WP 3.6.1.
448 if ( is_serialized( $data, false ) )
449 return serialize( $data );
455 * Retrieve post title from XMLRPC XML.
457 * If the title element is not part of the XML, then the default post title from
458 * the $post_default_title will be used instead.
462 * @global string $post_default_title Default XML-RPC post title.
464 * @param string $content XMLRPC XML Request content
465 * @return string Post title
467 function xmlrpc_getposttitle( $content ) {
468 global $post_default_title;
469 if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
470 $post_title = $matchtitle[1];
472 $post_title = $post_default_title;
478 * Retrieve the post category or categories from XMLRPC XML.
480 * If the category element is not found, then the default post category will be
481 * used. The return type then would be what $post_default_category. If the
482 * category is found, then it will always be an array.
486 * @global string $post_default_category Default XML-RPC post category.
488 * @param string $content XMLRPC XML Request content
489 * @return string|array List of categories or category name.
491 function xmlrpc_getpostcategory( $content ) {
492 global $post_default_category;
493 if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
494 $post_category = trim( $matchcat[1], ',' );
495 $post_category = explode( ',', $post_category );
497 $post_category = $post_default_category;
499 return $post_category;
503 * XMLRPC XML content without title and category elements.
507 * @param string $content XML-RPC XML Request content.
508 * @return string XMLRPC XML Request content without title and category elements.
510 function xmlrpc_removepostdata( $content ) {
511 $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
512 $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
513 $content = trim( $content );
518 * Use RegEx to extract URLs from arbitrary content.
522 * @param string $content Content to extract URLs from.
523 * @return array URLs found in passed string.
525 function wp_extract_urls( $content ) {
528 . "(?:([\w-]+:)?//?)"
534 . "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
543 $post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
545 return array_values( $post_links );
549 * Check content for video and audio links to add as enclosures.
551 * Will not add enclosures that have already been added and will
552 * remove enclosures that are no longer in the post. This is called as
553 * pingbacks and trackbacks.
557 * @global wpdb $wpdb WordPress database abstraction object.
559 * @param string $content Post Content.
560 * @param int $post_ID Post ID.
562 function do_enclose( $content, $post_ID ) {
565 //TODO: Tidy this ghetto code up and make the debug code optional
566 include_once( ABSPATH . WPINC . '/class-IXR.php' );
568 $post_links = array();
570 $pung = get_enclosed( $post_ID );
572 $post_links_temp = wp_extract_urls( $content );
574 foreach ( $pung as $link_test ) {
575 if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
576 $mids = $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, $wpdb->esc_like( $link_test ) . '%') );
577 foreach ( $mids as $mid )
578 delete_metadata_by_mid( 'post', $mid );
582 foreach ( (array) $post_links_temp as $link_test ) {
583 if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
584 $test = @parse_url( $link_test );
585 if ( false === $test )
587 if ( isset( $test['query'] ) )
588 $post_links[] = $link_test;
589 elseif ( isset($test['path']) && ( $test['path'] != '/' ) && ($test['path'] != '' ) )
590 $post_links[] = $link_test;
595 * Filters the list of enclosure links before querying the database.
597 * Allows for the addition and/or removal of potential enclosures to save
598 * to postmeta before checking the database for existing enclosures.
602 * @param array $post_links An array of enclosure links.
603 * @param int $post_ID Post ID.
605 $post_links = apply_filters( 'enclosure_links', $post_links, $post_ID );
607 foreach ( (array) $post_links as $url ) {
608 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, $wpdb->esc_like( $url ) . '%' ) ) ) {
610 if ( $headers = wp_get_http_headers( $url) ) {
611 $len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
612 $type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
613 $allowed_types = array( 'video', 'audio' );
615 // Check to see if we can figure out the mime type from
617 $url_parts = @parse_url( $url );
618 if ( false !== $url_parts ) {
619 $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
620 if ( !empty( $extension ) ) {
621 foreach ( wp_get_mime_types() as $exts => $mime ) {
622 if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
630 if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
631 add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );
639 * Retrieve HTTP Headers from URL.
643 * @param string $url URL to retrieve HTTP headers from.
644 * @param bool $deprecated Not Used.
645 * @return bool|string False on failure, headers on success.
647 function wp_get_http_headers( $url, $deprecated = false ) {
648 if ( !empty( $deprecated ) )
649 _deprecated_argument( __FUNCTION__, '2.7.0' );
651 $response = wp_safe_remote_head( $url );
653 if ( is_wp_error( $response ) )
656 return wp_remote_retrieve_headers( $response );
660 * Whether the publish date of the current post in the loop is different from the
661 * publish date of the previous post in the loop.
665 * @global string $currentday The day of the current post in the loop.
666 * @global string $previousday The day of the previous post in the loop.
668 * @return int 1 when new day, 0 if not a new day.
670 function is_new_day() {
671 global $currentday, $previousday;
672 if ( $currentday != $previousday )
679 * Build URL query based on an associative and, or indexed array.
681 * This is a convenient function for easily building url queries. It sets the
682 * separator to '&' and uses _http_build_query() function.
686 * @see _http_build_query() Used to build the query
687 * @link https://secure.php.net/manual/en/function.http-build-query.php for more on what
688 * http_build_query() does.
690 * @param array $data URL-encode key/value pairs.
691 * @return string URL-encoded string.
693 function build_query( $data ) {
694 return _http_build_query( $data, null, '&', '', false );
698 * From php.net (modified by Mark Jaquith to behave like the native PHP5 function).
703 * @see https://secure.php.net/manual/en/function.http-build-query.php
705 * @param array|object $data An array or object of data. Converted to array.
706 * @param string $prefix Optional. Numeric index. If set, start parameter numbering with it.
708 * @param string $sep Optional. Argument separator; defaults to 'arg_separator.output'.
710 * @param string $key Optional. Used to prefix key name. Default empty.
711 * @param bool $urlencode Optional. Whether to use urlencode() in the result. Default true.
713 * @return string The query string.
715 function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
718 foreach ( (array) $data as $k => $v ) {
721 if ( is_int($k) && $prefix != null )
724 $k = $key . '%5B' . $k . '%5D';
727 elseif ( $v === false )
730 if ( is_array($v) || is_object($v) )
731 array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
732 elseif ( $urlencode )
733 array_push($ret, $k.'='.urlencode($v));
735 array_push($ret, $k.'='.$v);
739 $sep = ini_get('arg_separator.output');
741 return implode($sep, $ret);
745 * Retrieves a modified URL query string.
747 * You can rebuild the URL and append query variables to the URL query by using this function.
748 * There are two ways to use this function; either a single key and value, or an associative array.
750 * Using a single key and value:
752 * add_query_arg( 'key', 'value', 'http://example.com' );
754 * Using an associative array:
756 * add_query_arg( array(
757 * 'key1' => 'value1',
758 * 'key2' => 'value2',
759 * ), 'http://example.com' );
761 * Omitting the URL from either use results in the current URL being used
762 * (the value of `$_SERVER['REQUEST_URI']`).
764 * Values are expected to be encoded appropriately with urlencode() or rawurlencode().
766 * Setting any query variable's value to boolean false removes the key (see remove_query_arg()).
768 * Important: The return value of add_query_arg() is not escaped by default. Output should be
769 * late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting
774 * @param string|array $key Either a query variable key, or an associative array of query variables.
775 * @param string $value Optional. Either a query variable value, or a URL to act upon.
776 * @param string $url Optional. A URL to act upon.
777 * @return string New URL query string (unescaped).
779 function add_query_arg() {
780 $args = func_get_args();
781 if ( is_array( $args[0] ) ) {
782 if ( count( $args ) < 2 || false === $args[1] )
783 $uri = $_SERVER['REQUEST_URI'];
787 if ( count( $args ) < 3 || false === $args[2] )
788 $uri = $_SERVER['REQUEST_URI'];
793 if ( $frag = strstr( $uri, '#' ) )
794 $uri = substr( $uri, 0, -strlen( $frag ) );
798 if ( 0 === stripos( $uri, 'http://' ) ) {
799 $protocol = 'http://';
800 $uri = substr( $uri, 7 );
801 } elseif ( 0 === stripos( $uri, 'https://' ) ) {
802 $protocol = 'https://';
803 $uri = substr( $uri, 8 );
808 if ( strpos( $uri, '?' ) !== false ) {
809 list( $base, $query ) = explode( '?', $uri, 2 );
811 } elseif ( $protocol || strpos( $uri, '=' ) === false ) {
819 wp_parse_str( $query, $qs );
820 $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
821 if ( is_array( $args[0] ) ) {
822 foreach ( $args[0] as $k => $v ) {
826 $qs[ $args[0] ] = $args[1];
829 foreach ( $qs as $k => $v ) {
834 $ret = build_query( $qs );
835 $ret = trim( $ret, '?' );
836 $ret = preg_replace( '#=(&|$)#', '$1', $ret );
837 $ret = $protocol . $base . $ret . $frag;
838 $ret = rtrim( $ret, '?' );
843 * Removes an item or items from a query string.
847 * @param string|array $key Query key or keys to remove.
848 * @param bool|string $query Optional. When false uses the current URL. Default false.
849 * @return string New URL query string.
851 function remove_query_arg( $key, $query = false ) {
852 if ( is_array( $key ) ) { // removing multiple keys
853 foreach ( $key as $k )
854 $query = add_query_arg( $k, false, $query );
857 return add_query_arg( $key, false, $query );
861 * Returns an array of single-use query variable names that can be removed from a URL.
865 * @return array An array of parameters to remove from the URL.
867 function wp_removable_query_args() {
868 $removable_query_args = array(
877 'hotkeys_highlight_first',
878 'hotkeys_highlight_last',
891 'wp-post-new-reload',
895 * Filters the list of query variables to remove.
899 * @param array $removable_query_args An array of query variables to remove from a URL.
901 return apply_filters( 'removable_query_args', $removable_query_args );
905 * Walks the array while sanitizing the contents.
909 * @param array $array Array to walk while sanitizing contents.
910 * @return array Sanitized $array.
912 function add_magic_quotes( $array ) {
913 foreach ( (array) $array as $k => $v ) {
914 if ( is_array( $v ) ) {
915 $array[$k] = add_magic_quotes( $v );
917 $array[$k] = addslashes( $v );
924 * HTTP request for URI to retrieve content.
928 * @see wp_safe_remote_get()
930 * @param string $uri URI/URL of web page to retrieve.
931 * @return false|string HTTP content. False on failure.
933 function wp_remote_fopen( $uri ) {
934 $parsed_url = @parse_url( $uri );
936 if ( !$parsed_url || !is_array( $parsed_url ) )
940 $options['timeout'] = 10;
942 $response = wp_safe_remote_get( $uri, $options );
944 if ( is_wp_error( $response ) )
947 return wp_remote_retrieve_body( $response );
951 * Set up the WordPress query.
955 * @global WP $wp_locale
956 * @global WP_Query $wp_query
957 * @global WP_Query $wp_the_query
959 * @param string|array $query_vars Default WP_Query arguments.
961 function wp( $query_vars = '' ) {
962 global $wp, $wp_query, $wp_the_query;
963 $wp->main( $query_vars );
965 if ( !isset($wp_the_query) )
966 $wp_the_query = $wp_query;
970 * Retrieve the description for the HTTP status.
974 * @global array $wp_header_to_desc
976 * @param int $code HTTP status code.
977 * @return string Empty string if not found, or description if found.
979 function get_status_header_desc( $code ) {
980 global $wp_header_to_desc;
982 $code = absint( $code );
984 if ( !isset( $wp_header_to_desc ) ) {
985 $wp_header_to_desc = array(
987 101 => 'Switching Protocols',
993 203 => 'Non-Authoritative Information',
995 205 => 'Reset Content',
996 206 => 'Partial Content',
997 207 => 'Multi-Status',
1000 300 => 'Multiple Choices',
1001 301 => 'Moved Permanently',
1004 304 => 'Not Modified',
1007 307 => 'Temporary Redirect',
1008 308 => 'Permanent Redirect',
1010 400 => 'Bad Request',
1011 401 => 'Unauthorized',
1012 402 => 'Payment Required',
1015 405 => 'Method Not Allowed',
1016 406 => 'Not Acceptable',
1017 407 => 'Proxy Authentication Required',
1018 408 => 'Request Timeout',
1021 411 => 'Length Required',
1022 412 => 'Precondition Failed',
1023 413 => 'Request Entity Too Large',
1024 414 => 'Request-URI Too Long',
1025 415 => 'Unsupported Media Type',
1026 416 => 'Requested Range Not Satisfiable',
1027 417 => 'Expectation Failed',
1028 418 => 'I\'m a teapot',
1029 421 => 'Misdirected Request',
1030 422 => 'Unprocessable Entity',
1032 424 => 'Failed Dependency',
1033 426 => 'Upgrade Required',
1034 428 => 'Precondition Required',
1035 429 => 'Too Many Requests',
1036 431 => 'Request Header Fields Too Large',
1037 451 => 'Unavailable For Legal Reasons',
1039 500 => 'Internal Server Error',
1040 501 => 'Not Implemented',
1041 502 => 'Bad Gateway',
1042 503 => 'Service Unavailable',
1043 504 => 'Gateway Timeout',
1044 505 => 'HTTP Version Not Supported',
1045 506 => 'Variant Also Negotiates',
1046 507 => 'Insufficient Storage',
1047 510 => 'Not Extended',
1048 511 => 'Network Authentication Required',
1052 if ( isset( $wp_header_to_desc[$code] ) )
1053 return $wp_header_to_desc[$code];
1059 * Set HTTP status header.
1062 * @since 4.4.0 Added the `$description` parameter.
1064 * @see get_status_header_desc()
1066 * @param int $code HTTP status code.
1067 * @param string $description Optional. A custom description for the HTTP status.
1069 function status_header( $code, $description = '' ) {
1070 if ( ! $description ) {
1071 $description = get_status_header_desc( $code );
1074 if ( empty( $description ) ) {
1078 $protocol = wp_get_server_protocol();
1079 $status_header = "$protocol $code $description";
1080 if ( function_exists( 'apply_filters' ) )
1083 * Filters an HTTP status header.
1087 * @param string $status_header HTTP status header.
1088 * @param int $code HTTP status code.
1089 * @param string $description Description for the status code.
1090 * @param string $protocol Server protocol.
1092 $status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol );
1094 @header( $status_header, true, $code );
1098 * Get the header information to prevent caching.
1100 * The several different headers cover the different ways cache prevention
1101 * is handled by different browsers
1105 * @return array The associative array of header names and field values.
1107 function wp_get_nocache_headers() {
1109 'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
1110 'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
1113 if ( function_exists('apply_filters') ) {
1115 * Filters the cache-controlling headers.
1119 * @see wp_get_nocache_headers()
1121 * @param array $headers {
1122 * Header names and field values.
1124 * @type string $Expires Expires header.
1125 * @type string $Cache-Control Cache-Control header.
1128 $headers = (array) apply_filters( 'nocache_headers', $headers );
1130 $headers['Last-Modified'] = false;
1135 * Set the headers to prevent caching for the different browsers.
1137 * Different browsers support different nocache headers, so several
1138 * headers must be sent so that all of them get the point that no
1139 * caching should occur.
1143 * @see wp_get_nocache_headers()
1145 function nocache_headers() {
1146 $headers = wp_get_nocache_headers();
1148 unset( $headers['Last-Modified'] );
1150 // In PHP 5.3+, make sure we are not sending a Last-Modified header.
1151 if ( function_exists( 'header_remove' ) ) {
1152 @header_remove( 'Last-Modified' );
1154 // In PHP 5.2, send an empty Last-Modified header, but only as a
1155 // last resort to override a header already sent. #WP23021
1156 foreach ( headers_list() as $header ) {
1157 if ( 0 === stripos( $header, 'Last-Modified' ) ) {
1158 $headers['Last-Modified'] = '';
1164 foreach ( $headers as $name => $field_value )
1165 @header("{$name}: {$field_value}");
1169 * Set the headers for caching for 10 days with JavaScript content type.
1173 function cache_javascript_headers() {
1174 $expiresOffset = 10 * DAY_IN_SECONDS;
1176 header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
1177 header( "Vary: Accept-Encoding" ); // Handle proxies
1178 header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
1182 * Retrieve the number of database queries during the WordPress execution.
1186 * @global wpdb $wpdb WordPress database abstraction object.
1188 * @return int Number of database queries.
1190 function get_num_queries() {
1192 return $wpdb->num_queries;
1196 * Whether input is yes or no.
1198 * Must be 'y' to be true.
1202 * @param string $yn Character string containing either 'y' (yes) or 'n' (no).
1203 * @return bool True if yes, false on anything else.
1205 function bool_from_yn( $yn ) {
1206 return ( strtolower( $yn ) == 'y' );
1210 * Load the feed template from the use of an action hook.
1212 * If the feed action does not have a hook, then the function will die with a
1213 * message telling the visitor that the feed is not valid.
1215 * It is better to only have one hook for each feed.
1219 * @global WP_Query $wp_query Used to tell if the use a comment feed.
1221 function do_feed() {
1224 $feed = get_query_var( 'feed' );
1226 // Remove the pad, if present.
1227 $feed = preg_replace( '/^_+/', '', $feed );
1229 if ( $feed == '' || $feed == 'feed' )
1230 $feed = get_default_feed();
1232 if ( ! has_action( "do_feed_{$feed}" ) ) {
1233 wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) );
1237 * Fires once the given feed is loaded.
1239 * The dynamic portion of the hook name, `$feed`, refers to the feed template name.
1240 * Possible values include: 'rdf', 'rss', 'rss2', and 'atom'.
1243 * @since 4.4.0 The `$feed` parameter was added.
1245 * @param bool $is_comment_feed Whether the feed is a comment feed.
1246 * @param string $feed The feed name.
1248 do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed );
1252 * Load the RDF RSS 0.91 Feed template.
1256 * @see load_template()
1258 function do_feed_rdf() {
1259 load_template( ABSPATH . WPINC . '/feed-rdf.php' );
1263 * Load the RSS 1.0 Feed Template.
1267 * @see load_template()
1269 function do_feed_rss() {
1270 load_template( ABSPATH . WPINC . '/feed-rss.php' );
1274 * Load either the RSS2 comment feed or the RSS2 posts feed.
1278 * @see load_template()
1280 * @param bool $for_comments True for the comment feed, false for normal feed.
1282 function do_feed_rss2( $for_comments ) {
1283 if ( $for_comments )
1284 load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
1286 load_template( ABSPATH . WPINC . '/feed-rss2.php' );
1290 * Load either Atom comment feed or Atom posts feed.
1294 * @see load_template()
1296 * @param bool $for_comments True for the comment feed, false for normal feed.
1298 function do_feed_atom( $for_comments ) {
1300 load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
1302 load_template( ABSPATH . WPINC . '/feed-atom.php' );
1306 * Display the robots.txt file content.
1308 * The echo content should be with usage of the permalinks or for creating the
1313 function do_robots() {
1314 header( 'Content-Type: text/plain; charset=utf-8' );
1317 * Fires when displaying the robots.txt file.
1321 do_action( 'do_robotstxt' );
1323 $output = "User-agent: *\n";
1324 $public = get_option( 'blog_public' );
1325 if ( '0' == $public ) {
1326 $output .= "Disallow: /\n";
1328 $site_url = parse_url( site_url() );
1329 $path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
1330 $output .= "Disallow: $path/wp-admin/\n";
1331 $output .= "Allow: $path/wp-admin/admin-ajax.php\n";
1335 * Filters the robots.txt output.
1339 * @param string $output Robots.txt output.
1340 * @param bool $public Whether the site is considered "public".
1342 echo apply_filters( 'robots_txt', $output, $public );
1346 * Test whether WordPress is already installed.
1348 * The cache will be checked first. If you have a cache plugin, which saves
1349 * the cache values, then this will work. If you use the default WordPress
1350 * cache, and the database goes away, then you might have problems.
1352 * Checks for the 'siteurl' option for whether WordPress is installed.
1356 * @global wpdb $wpdb WordPress database abstraction object.
1358 * @return bool Whether the site is already installed.
1360 function is_blog_installed() {
1364 * Check cache first. If options table goes away and we have true
1367 if ( wp_cache_get( 'is_blog_installed' ) )
1370 $suppress = $wpdb->suppress_errors();
1371 if ( ! wp_installing() ) {
1372 $alloptions = wp_load_alloptions();
1374 // If siteurl is not set to autoload, check it specifically
1375 if ( !isset( $alloptions['siteurl'] ) )
1376 $installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
1378 $installed = $alloptions['siteurl'];
1379 $wpdb->suppress_errors( $suppress );
1381 $installed = !empty( $installed );
1382 wp_cache_set( 'is_blog_installed', $installed );
1387 // If visiting repair.php, return true and let it take over.
1388 if ( defined( 'WP_REPAIRING' ) )
1391 $suppress = $wpdb->suppress_errors();
1394 * Loop over the WP tables. If none exist, then scratch install is allowed.
1395 * If one or more exist, suggest table repair since we got here because the
1396 * options table could not be accessed.
1398 $wp_tables = $wpdb->tables();
1399 foreach ( $wp_tables as $table ) {
1400 // The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
1401 if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
1403 if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table )
1406 if ( ! $wpdb->get_results( "DESCRIBE $table;" ) )
1409 // One or more tables exist. We are insane.
1411 wp_load_translations_early();
1413 // Die with a DB error.
1414 $wpdb->error = sprintf( __( 'One or more database tables are unavailable. The database may need to be <a href="%s">repaired</a>.' ), 'maint/repair.php?referrer=is_blog_installed' );
1418 $wpdb->suppress_errors( $suppress );
1420 wp_cache_set( 'is_blog_installed', false );
1426 * Retrieve URL with nonce added to URL query.
1430 * @param string $actionurl URL to add nonce action.
1431 * @param int|string $action Optional. Nonce action name. Default -1.
1432 * @param string $name Optional. Nonce name. Default '_wpnonce'.
1433 * @return string Escaped URL with nonce action added.
1435 function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
1436 $actionurl = str_replace( '&', '&', $actionurl );
1437 return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
1441 * Retrieve or display nonce hidden field for forms.
1443 * The nonce field is used to validate that the contents of the form came from
1444 * the location on the current site and not somewhere else. The nonce does not
1445 * offer absolute protection, but should protect against most cases. It is very
1446 * important to use nonce field in forms.
1448 * The $action and $name are optional, but if you want to have better security,
1449 * it is strongly suggested to set those two parameters. It is easier to just
1450 * call the function without any parameters, because validation of the nonce
1451 * doesn't require any parameters, but since crackers know what the default is
1452 * it won't be difficult for them to find a way around your nonce and cause
1455 * The input name will be whatever $name value you gave. The input value will be
1456 * the nonce creation value.
1460 * @param int|string $action Optional. Action name. Default -1.
1461 * @param string $name Optional. Nonce name. Default '_wpnonce'.
1462 * @param bool $referer Optional. Whether to set the referer field for validation. Default true.
1463 * @param bool $echo Optional. Whether to display or return hidden form field. Default true.
1464 * @return string Nonce field HTML markup.
1466 function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
1467 $name = esc_attr( $name );
1468 $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
1471 $nonce_field .= wp_referer_field( false );
1476 return $nonce_field;
1480 * Retrieve or display referer hidden field for forms.
1482 * The referer link is the current Request URI from the server super global. The
1483 * input name is '_wp_http_referer', in case you wanted to check manually.
1487 * @param bool $echo Optional. Whether to echo or return the referer field. Default true.
1488 * @return string Referer field HTML markup.
1490 function wp_referer_field( $echo = true ) {
1491 $referer_field = '<input type="hidden" name="_wp_http_referer" value="'. esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />';
1494 echo $referer_field;
1495 return $referer_field;
1499 * Retrieve or display original referer hidden field for forms.
1501 * The input name is '_wp_original_http_referer' and will be either the same
1502 * value of wp_referer_field(), if that was posted already or it will be the
1503 * current page, if it doesn't exist.
1507 * @param bool $echo Optional. Whether to echo the original http referer. Default true.
1508 * @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to.
1509 * Default 'current'.
1510 * @return string Original referer field.
1512 function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
1513 if ( ! $ref = wp_get_original_referer() ) {
1514 $ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] );
1516 $orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( $ref ) . '" />';
1518 echo $orig_referer_field;
1519 return $orig_referer_field;
1523 * Retrieve referer from '_wp_http_referer' or HTTP referer.
1525 * If it's the same as the current request URL, will return false.
1529 * @return false|string False on failure. Referer URL on success.
1531 function wp_get_referer() {
1532 if ( ! function_exists( 'wp_validate_redirect' ) ) {
1536 $ref = wp_get_raw_referer();
1538 if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) && $ref !== home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) ) {
1539 return wp_validate_redirect( $ref, false );
1546 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
1548 * Do not use for redirects, use wp_get_referer() instead.
1552 * @return string|false Referer URL on success, false on failure.
1554 function wp_get_raw_referer() {
1555 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
1556 return wp_unslash( $_REQUEST['_wp_http_referer'] );
1557 } else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
1558 return wp_unslash( $_SERVER['HTTP_REFERER'] );
1565 * Retrieve original referer that was posted, if it exists.
1569 * @return string|false False if no original referer or original referer if set.
1571 function wp_get_original_referer() {
1572 if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) )
1573 return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
1578 * Recursive directory creation based on full path.
1580 * Will attempt to set permissions on folders.
1584 * @param string $target Full path to attempt to create.
1585 * @return bool Whether the path was created. True if path already exists.
1587 function wp_mkdir_p( $target ) {
1590 // Strip the protocol.
1591 if ( wp_is_stream( $target ) ) {
1592 list( $wrapper, $target ) = explode( '://', $target, 2 );
1595 // From php.net/mkdir user contributed notes.
1596 $target = str_replace( '//', '/', $target );
1598 // Put the wrapper back on the target.
1599 if ( $wrapper !== null ) {
1600 $target = $wrapper . '://' . $target;
1604 * Safe mode fails with a trailing slash under certain PHP versions.
1605 * Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
1607 $target = rtrim($target, '/');
1608 if ( empty($target) )
1611 if ( file_exists( $target ) )
1612 return @is_dir( $target );
1614 // We need to find the permissions of the parent folder that exists and inherit that.
1615 $target_parent = dirname( $target );
1616 while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
1617 $target_parent = dirname( $target_parent );
1620 // Get the permission bits.
1621 if ( $stat = @stat( $target_parent ) ) {
1622 $dir_perms = $stat['mode'] & 0007777;
1627 if ( @mkdir( $target, $dir_perms, true ) ) {
1630 * If a umask is set that modifies $dir_perms, we'll have to re-set
1631 * the $dir_perms correctly with chmod()
1633 if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
1634 $folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
1635 for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) {
1636 @chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
1647 * Test if a give filesystem path is absolute.
1649 * For example, '/foo/bar', or 'c:\windows'.
1653 * @param string $path File path.
1654 * @return bool True if path is absolute, false is not absolute.
1656 function path_is_absolute( $path ) {
1658 * This is definitive if true but fails if $path does not exist or contains
1661 if ( realpath($path) == $path )
1664 if ( strlen($path) == 0 || $path[0] == '.' )
1667 // Windows allows absolute paths like this.
1668 if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
1671 // A path starting with / or \ is absolute; anything else is relative.
1672 return ( $path[0] == '/' || $path[0] == '\\' );
1676 * Join two filesystem paths together.
1678 * For example, 'give me $path relative to $base'. If the $path is absolute,
1679 * then it the full path is returned.
1683 * @param string $base Base path.
1684 * @param string $path Path relative to $base.
1685 * @return string The path with the base or absolute path.
1687 function path_join( $base, $path ) {
1688 if ( path_is_absolute($path) )
1691 return rtrim($base, '/') . '/' . ltrim($path, '/');
1695 * Normalize a filesystem path.
1697 * On windows systems, replaces backslashes with forward slashes
1698 * and forces upper-case drive letters.
1699 * Allows for two leading slashes for Windows network shares, but
1700 * ensures that all other duplicate slashes are reduced to a single.
1703 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
1704 * @since 4.5.0 Allows for Windows network shares.
1706 * @param string $path Path to normalize.
1707 * @return string Normalized path.
1709 function wp_normalize_path( $path ) {
1710 $path = str_replace( '\\', '/', $path );
1711 $path = preg_replace( '|(?<=.)/+|', '/', $path );
1712 if ( ':' === substr( $path, 1, 1 ) ) {
1713 $path = ucfirst( $path );
1719 * Determine a writable directory for temporary files.
1721 * Function's preference is the return value of sys_get_temp_dir(),
1722 * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
1723 * before finally defaulting to /tmp/
1725 * In the event that this function does not find a writable location,
1726 * It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.
1730 * @staticvar string $temp
1732 * @return string Writable temporary directory.
1734 function get_temp_dir() {
1736 if ( defined('WP_TEMP_DIR') )
1737 return trailingslashit(WP_TEMP_DIR);
1740 return trailingslashit( $temp );
1742 if ( function_exists('sys_get_temp_dir') ) {
1743 $temp = sys_get_temp_dir();
1744 if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1745 return trailingslashit( $temp );
1748 $temp = ini_get('upload_tmp_dir');
1749 if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1750 return trailingslashit( $temp );
1752 $temp = WP_CONTENT_DIR . '/';
1753 if ( is_dir( $temp ) && wp_is_writable( $temp ) )
1760 * Determine if a directory is writable.
1762 * This function is used to work around certain ACL issues in PHP primarily
1763 * affecting Windows Servers.
1767 * @see win_is_writable()
1769 * @param string $path Path to check for write-ability.
1770 * @return bool Whether the path is writable.
1772 function wp_is_writable( $path ) {
1773 if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) )
1774 return win_is_writable( $path );
1776 return @is_writable( $path );
1780 * Workaround for Windows bug in is_writable() function
1782 * PHP has issues with Windows ACL's for determine if a
1783 * directory is writable or not, this works around them by
1784 * checking the ability to open files rather than relying
1785 * upon PHP to interprate the OS ACL.
1789 * @see https://bugs.php.net/bug.php?id=27609
1790 * @see https://bugs.php.net/bug.php?id=30931
1792 * @param string $path Windows path to check for write-ability.
1793 * @return bool Whether the path is writable.
1795 function win_is_writable( $path ) {
1797 if ( $path[strlen( $path ) - 1] == '/' ) { // if it looks like a directory, check a random file within the directory
1798 return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
1799 } elseif ( is_dir( $path ) ) { // If it's a directory (and not a file) check a random file within the directory
1800 return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
1802 // check tmp file for read/write capabilities
1803 $should_delete_tmp_file = !file_exists( $path );
1804 $f = @fopen( $path, 'a' );
1808 if ( $should_delete_tmp_file )
1814 * Retrieves uploads directory information.
1816 * Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory.
1817 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases
1818 * when not uploading files.
1822 * @see wp_upload_dir()
1824 * @return array See wp_upload_dir() for description.
1826 function wp_get_upload_dir() {
1827 return wp_upload_dir( null, false );
1831 * Get an array containing the current upload directory's path and url.
1833 * Checks the 'upload_path' option, which should be from the web root folder,
1834 * and if it isn't empty it will be used. If it is empty, then the path will be
1835 * 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will
1836 * override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.
1838 * The upload URL path is set either by the 'upload_url_path' option or by using
1839 * the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.
1841 * If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in
1842 * the administration settings panel), then the time will be used. The format
1843 * will be year first and then month.
1845 * If the path couldn't be created, then an error will be returned with the key
1846 * 'error' containing the error message. The error suggests that the parent
1847 * directory is not writable by the server.
1849 * On success, the returned array will have many indices:
1850 * 'path' - base directory and sub directory or full path to upload directory.
1851 * 'url' - base url and sub directory or absolute URL to upload directory.
1852 * 'subdir' - sub directory if uploads use year/month folders option is on.
1853 * 'basedir' - path without subdir.
1854 * 'baseurl' - URL path without subdir.
1855 * 'error' - false or error message.
1858 * @uses _wp_upload_dir()
1860 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1861 * @param bool $create_dir Optional. Whether to check and create the uploads directory.
1862 * Default true for backward compatibility.
1863 * @param bool $refresh_cache Optional. Whether to refresh the cache. Default false.
1864 * @return array See above for description.
1866 function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
1867 static $cache = array(), $tested_paths = array();
1869 $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
1871 if ( $refresh_cache || empty( $cache[ $key ] ) ) {
1872 $cache[ $key ] = _wp_upload_dir( $time );
1876 * Filters the uploads directory data.
1880 * @param array $uploads Array of upload directory data with keys of 'path',
1881 * 'url', 'subdir, 'basedir', and 'error'.
1883 $uploads = apply_filters( 'upload_dir', $cache[ $key ] );
1885 if ( $create_dir ) {
1886 $path = $uploads['path'];
1888 if ( array_key_exists( $path, $tested_paths ) ) {
1889 $uploads['error'] = $tested_paths[ $path ];
1891 if ( ! wp_mkdir_p( $path ) ) {
1892 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
1893 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
1895 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
1898 $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), esc_html( $error_path ) );
1901 $tested_paths[ $path ] = $uploads['error'];
1909 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
1913 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1914 * @return array See wp_upload_dir()
1916 function _wp_upload_dir( $time = null ) {
1917 $siteurl = get_option( 'siteurl' );
1918 $upload_path = trim( get_option( 'upload_path' ) );
1920 if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
1921 $dir = WP_CONTENT_DIR . '/uploads';
1922 } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
1923 // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
1924 $dir = path_join( ABSPATH, $upload_path );
1926 $dir = $upload_path;
1929 if ( !$url = get_option( 'upload_url_path' ) ) {
1930 if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
1931 $url = WP_CONTENT_URL . '/uploads';
1933 $url = trailingslashit( $siteurl ) . $upload_path;
1937 * Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
1938 * We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
1940 if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
1941 $dir = ABSPATH . UPLOADS;
1942 $url = trailingslashit( $siteurl ) . UPLOADS;
1945 // If multisite (and if not the main site in a post-MU network)
1946 if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
1948 if ( ! get_site_option( 'ms_files_rewriting' ) ) {
1950 * If ms-files rewriting is disabled (networks created post-3.5), it is fairly
1951 * straightforward: Append sites/%d if we're not on the main site (for post-MU
1952 * networks). (The extra directory prevents a four-digit ID from conflicting with
1953 * a year-based directory for the main site. But if a MU-era network has disabled
1954 * ms-files rewriting manually, they don't need the extra directory, as they never
1955 * had wp-content/uploads for the main site.)
1958 if ( defined( 'MULTISITE' ) )
1959 $ms_dir = '/sites/' . get_current_blog_id();
1961 $ms_dir = '/' . get_current_blog_id();
1966 } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
1968 * Handle the old-form ms-files.php rewriting if the network still has that enabled.
1969 * When ms-files rewriting is enabled, then we only listen to UPLOADS when:
1970 * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used
1972 * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect
1973 * the original blog ID.
1975 * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
1976 * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
1977 * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
1978 * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
1981 if ( defined( 'BLOGUPLOADDIR' ) )
1982 $dir = untrailingslashit( BLOGUPLOADDIR );
1984 $dir = ABSPATH . UPLOADS;
1985 $url = trailingslashit( $siteurl ) . 'files';
1993 if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
1994 // Generate the yearly and monthly dirs
1996 $time = current_time( 'mysql' );
1997 $y = substr( $time, 0, 4 );
1998 $m = substr( $time, 5, 2 );
2008 'subdir' => $subdir,
2009 'basedir' => $basedir,
2010 'baseurl' => $baseurl,
2016 * Get a filename that is sanitized and unique for the given directory.
2018 * If the filename is not unique, then a number will be added to the filename
2019 * before the extension, and will continue adding numbers until the filename is
2022 * The callback is passed three parameters, the first one is the directory, the
2023 * second is the filename, and the third is the extension.
2027 * @param string $dir Directory.
2028 * @param string $filename File name.
2029 * @param callable $unique_filename_callback Callback. Default null.
2030 * @return string New filename, if given wasn't unique.
2032 function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
2033 // Sanitize the file name before we begin processing.
2034 $filename = sanitize_file_name($filename);
2036 // Separate the filename into a name and extension.
2037 $info = pathinfo($filename);
2038 $ext = !empty($info['extension']) ? '.' . $info['extension'] : '';
2039 $name = basename($filename, $ext);
2041 // Edge case: if file is named '.ext', treat as an empty name.
2042 if ( $name === $ext )
2046 * Increment the file number until we have a unique file to save in $dir.
2047 * Use callback if supplied.
2049 if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
2050 $filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
2054 // Change '.ext' to lower case.
2055 if ( $ext && strtolower($ext) != $ext ) {
2056 $ext2 = strtolower($ext);
2057 $filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
2059 // Check for both lower and upper case extension or image sub-sizes may be overwritten.
2060 while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
2061 $new_number = $number + 1;
2062 $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename );
2063 $filename2 = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 );
2064 $number = $new_number;
2068 * Filters the result when generating a unique file name.
2072 * @param string $filename Unique file name.
2073 * @param string $ext File extension, eg. ".png".
2074 * @param string $dir Directory path.
2075 * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
2077 return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback );
2080 while ( file_exists( $dir . "/$filename" ) ) {
2081 if ( '' == "$number$ext" ) {
2082 $filename = "$filename-" . ++$number;
2084 $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-" . ++$number . $ext, $filename );
2089 /** This filter is documented in wp-includes/functions.php */
2090 return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback );
2094 * Create a file in the upload folder with given content.
2096 * If there is an error, then the key 'error' will exist with the error message.
2097 * If success, then the key 'file' will have the unique file path, the 'url' key
2098 * will have the link to the new file. and the 'error' key will be set to false.
2100 * This function will not move an uploaded file to the upload folder. It will
2101 * create a new file with the content in $bits parameter. If you move the upload
2102 * file, read the content of the uploaded file, and then you can give the
2103 * filename and content to this function, which will add it to the upload
2106 * The permissions will be set on the new file automatically by this function.
2110 * @param string $name Filename.
2111 * @param null|string $deprecated Never used. Set to null.
2112 * @param mixed $bits File content
2113 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
2116 function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
2117 if ( !empty( $deprecated ) )
2118 _deprecated_argument( __FUNCTION__, '2.0.0' );
2120 if ( empty( $name ) )
2121 return array( 'error' => __( 'Empty filename' ) );
2123 $wp_filetype = wp_check_filetype( $name );
2124 if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) )
2125 return array( 'error' => __( 'Invalid file type' ) );
2127 $upload = wp_upload_dir( $time );
2129 if ( $upload['error'] !== false )
2133 * Filters whether to treat the upload bits as an error.
2135 * Passing a non-array to the filter will effectively short-circuit preparing
2136 * the upload bits, returning that value instead.
2140 * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
2142 $upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );
2143 if ( !is_array( $upload_bits_error ) ) {
2144 $upload[ 'error' ] = $upload_bits_error;
2148 $filename = wp_unique_filename( $upload['path'], $name );
2150 $new_file = $upload['path'] . "/$filename";
2151 if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
2152 if ( 0 === strpos( $upload['basedir'], ABSPATH ) )
2153 $error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];
2155 $error_path = basename( $upload['basedir'] ) . $upload['subdir'];
2157 $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );
2158 return array( 'error' => $message );
2161 $ifp = @ fopen( $new_file, 'wb' );
2163 return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
2165 @fwrite( $ifp, $bits );
2169 // Set correct file permissions
2170 $stat = @ stat( dirname( $new_file ) );
2171 $perms = $stat['mode'] & 0007777;
2172 $perms = $perms & 0000666;
2173 @ chmod( $new_file, $perms );
2177 $url = $upload['url'] . "/$filename";
2179 /** This filter is documented in wp-admin/includes/file.php */
2180 return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false ), 'sideload' );
2184 * Retrieve the file type based on the extension name.
2188 * @param string $ext The extension to search.
2189 * @return string|void The file type, example: audio, video, document, spreadsheet, etc.
2191 function wp_ext2type( $ext ) {
2192 $ext = strtolower( $ext );
2194 $ext2type = wp_get_ext_types();
2195 foreach ( $ext2type as $type => $exts )
2196 if ( in_array( $ext, $exts ) )
2201 * Retrieve the file type from the file name.
2203 * You can optionally define the mime array, if needed.
2207 * @param string $filename File name or path.
2208 * @param array $mimes Optional. Key is the file extension with value as the mime type.
2209 * @return array Values with extension first and mime type.
2211 function wp_check_filetype( $filename, $mimes = null ) {
2212 if ( empty($mimes) )
2213 $mimes = get_allowed_mime_types();
2217 foreach ( $mimes as $ext_preg => $mime_match ) {
2218 $ext_preg = '!\.(' . $ext_preg . ')$!i';
2219 if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
2220 $type = $mime_match;
2221 $ext = $ext_matches[1];
2226 return compact( 'ext', 'type' );
2230 * Attempt to determine the real file type of a file.
2232 * If unable to, the file name extension will be used to determine type.
2234 * If it's determined that the extension does not match the file's real type,
2235 * then the "proper_filename" value will be set with a proper filename and extension.
2237 * Currently this function only supports renaming images validated via wp_get_image_mime().
2241 * @param string $file Full path to the file.
2242 * @param string $filename The name of the file (may differ from $file due to $file being
2243 * in a tmp directory).
2244 * @param array $mimes Optional. Key is the file extension with value as the mime type.
2245 * @return array Values for the extension, MIME, and either a corrected filename or false
2246 * if original $filename is valid.
2248 function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
2249 $proper_filename = false;
2251 // Do basic extension validation and MIME mapping
2252 $wp_filetype = wp_check_filetype( $filename, $mimes );
2253 $ext = $wp_filetype['ext'];
2254 $type = $wp_filetype['type'];
2256 // We can't do any further validation without a file to work with
2257 if ( ! file_exists( $file ) ) {
2258 return compact( 'ext', 'type', 'proper_filename' );
2261 // Validate image types.
2262 if ( $type && 0 === strpos( $type, 'image/' ) ) {
2264 // Attempt to figure out what type of image it actually is
2265 $real_mime = wp_get_image_mime( $file );
2267 if ( ! $real_mime ) {
2268 $type = $ext = false;
2269 } elseif ( $real_mime != $type ) {
2271 * Filters the list mapping image mime types to their respective extensions.
2275 * @param array $mime_to_ext Array of image mime types and their matching extensions.
2277 $mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
2278 'image/jpeg' => 'jpg',
2279 'image/png' => 'png',
2280 'image/gif' => 'gif',
2281 'image/bmp' => 'bmp',
2282 'image/tiff' => 'tif',
2285 // Replace whatever is after the last period in the filename with the correct extension
2286 if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
2287 $filename_parts = explode( '.', $filename );
2288 array_pop( $filename_parts );
2289 $filename_parts[] = $mime_to_ext[ $real_mime ];
2290 $new_filename = implode( '.', $filename_parts );
2292 if ( $new_filename != $filename ) {
2293 $proper_filename = $new_filename; // Mark that it changed
2295 // Redefine the extension / MIME
2296 $wp_filetype = wp_check_filetype( $new_filename, $mimes );
2297 $ext = $wp_filetype['ext'];
2298 $type = $wp_filetype['type'];
2300 $type = $ext = false;
2303 } elseif ( function_exists( 'finfo_file' ) ) {
2304 // Use finfo_file if available to validate non-image files.
2305 $finfo = finfo_open( FILEINFO_MIME_TYPE );
2306 $real_mime = finfo_file( $finfo, $file );
2307 finfo_close( $finfo );
2309 // If the extension does not match the file's real type, return false.
2310 if ( $real_mime !== $type ) {
2311 $type = $ext = false;
2316 * Filters the "real" file type of the given file.
2320 * @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
2321 * 'proper_filename' keys.
2322 * @param string $file Full path to the file.
2323 * @param string $filename The name of the file (may differ from $file due to
2324 * $file being in a tmp directory).
2325 * @param array $mimes Key is the file extension with value as the mime type.
2327 return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
2331 * Returns the real mime type of an image file.
2333 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
2337 * @param string $file Full path to the file.
2338 * @return string|false The actual mime type or false if the type cannot be determined.
2340 function wp_get_image_mime( $file ) {
2342 * Use exif_imagetype() to check the mimetype if available or fall back to
2343 * getimagesize() if exif isn't avaialbe. If either function throws an Exception
2344 * we assume the file could not be validated.
2347 if ( is_callable( 'exif_imagetype' ) ) {
2348 $mime = image_type_to_mime_type( exif_imagetype( $file ) );
2349 } elseif ( function_exists( 'getimagesize' ) ) {
2350 $imagesize = getimagesize( $file );
2351 $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
2355 } catch ( Exception $e ) {
2363 * Retrieve list of mime types and file extensions.
2366 * @since 4.2.0 Support was added for GIMP (xcf) files.
2368 * @return array Array of mime types keyed by the file extension regex corresponding to those types.
2370 function wp_get_mime_types() {
2372 * Filters the list of mime types and file extensions.
2374 * This filter should be used to add, not remove, mime types. To remove
2375 * mime types, use the {@see 'upload_mimes'} filter.
2379 * @param array $wp_get_mime_types Mime types keyed by the file extension regex
2380 * corresponding to those types.
2382 return apply_filters( 'mime_types', array(
2384 'jpg|jpeg|jpe' => 'image/jpeg',
2385 'gif' => 'image/gif',
2386 'png' => 'image/png',
2387 'bmp' => 'image/bmp',
2388 'tiff|tif' => 'image/tiff',
2389 'ico' => 'image/x-icon',
2391 'asf|asx' => 'video/x-ms-asf',
2392 'wmv' => 'video/x-ms-wmv',
2393 'wmx' => 'video/x-ms-wmx',
2394 'wm' => 'video/x-ms-wm',
2395 'avi' => 'video/avi',
2396 'divx' => 'video/divx',
2397 'flv' => 'video/x-flv',
2398 'mov|qt' => 'video/quicktime',
2399 'mpeg|mpg|mpe' => 'video/mpeg',
2400 'mp4|m4v' => 'video/mp4',
2401 'ogv' => 'video/ogg',
2402 'webm' => 'video/webm',
2403 'mkv' => 'video/x-matroska',
2404 '3gp|3gpp' => 'video/3gpp', // Can also be audio
2405 '3g2|3gp2' => 'video/3gpp2', // Can also be audio
2407 'txt|asc|c|cc|h|srt' => 'text/plain',
2408 'csv' => 'text/csv',
2409 'tsv' => 'text/tab-separated-values',
2410 'ics' => 'text/calendar',
2411 'rtx' => 'text/richtext',
2412 'css' => 'text/css',
2413 'htm|html' => 'text/html',
2414 'vtt' => 'text/vtt',
2415 'dfxp' => 'application/ttaf+xml',
2417 'mp3|m4a|m4b' => 'audio/mpeg',
2418 'ra|ram' => 'audio/x-realaudio',
2419 'wav' => 'audio/wav',
2420 'ogg|oga' => 'audio/ogg',
2421 'mid|midi' => 'audio/midi',
2422 'wma' => 'audio/x-ms-wma',
2423 'wax' => 'audio/x-ms-wax',
2424 'mka' => 'audio/x-matroska',
2425 // Misc application formats.
2426 'rtf' => 'application/rtf',
2427 'js' => 'application/javascript',
2428 'pdf' => 'application/pdf',
2429 'swf' => 'application/x-shockwave-flash',
2430 'class' => 'application/java',
2431 'tar' => 'application/x-tar',
2432 'zip' => 'application/zip',
2433 'gz|gzip' => 'application/x-gzip',
2434 'rar' => 'application/rar',
2435 '7z' => 'application/x-7z-compressed',
2436 'exe' => 'application/x-msdownload',
2437 'psd' => 'application/octet-stream',
2438 'xcf' => 'application/octet-stream',
2439 // MS Office formats.
2440 'doc' => 'application/msword',
2441 'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
2442 'wri' => 'application/vnd.ms-write',
2443 'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
2444 'mdb' => 'application/vnd.ms-access',
2445 'mpp' => 'application/vnd.ms-project',
2446 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
2447 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
2448 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
2449 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
2450 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
2451 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
2452 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
2453 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
2454 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
2455 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
2456 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
2457 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
2458 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
2459 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
2460 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
2461 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
2462 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
2463 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
2464 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
2465 'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
2466 'oxps' => 'application/oxps',
2467 'xps' => 'application/vnd.ms-xpsdocument',
2468 // OpenOffice formats.
2469 'odt' => 'application/vnd.oasis.opendocument.text',
2470 'odp' => 'application/vnd.oasis.opendocument.presentation',
2471 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
2472 'odg' => 'application/vnd.oasis.opendocument.graphics',
2473 'odc' => 'application/vnd.oasis.opendocument.chart',
2474 'odb' => 'application/vnd.oasis.opendocument.database',
2475 'odf' => 'application/vnd.oasis.opendocument.formula',
2476 // WordPerfect formats.
2477 'wp|wpd' => 'application/wordperfect',
2479 'key' => 'application/vnd.apple.keynote',
2480 'numbers' => 'application/vnd.apple.numbers',
2481 'pages' => 'application/vnd.apple.pages',
2486 * Retrieves the list of common file extensions and their types.
2490 * @return array Array of file extensions types keyed by the type of file.
2492 function wp_get_ext_types() {
2495 * Filters file type based on the extension name.
2499 * @see wp_ext2type()
2501 * @param array $ext2type Multi-dimensional array with extensions for a default set
2504 return apply_filters( 'ext2type', array(
2505 'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ),
2506 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
2507 'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
2508 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ),
2509 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ),
2510 'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
2511 'text' => array( 'asc', 'csv', 'tsv', 'txt' ),
2512 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ),
2513 'code' => array( 'css', 'htm', 'html', 'php', 'js' ),
2518 * Retrieve list of allowed mime types and file extensions.
2522 * @param int|WP_User $user Optional. User to check. Defaults to current user.
2523 * @return array Array of mime types keyed by the file extension regex corresponding
2526 function get_allowed_mime_types( $user = null ) {
2527 $t = wp_get_mime_types();
2529 unset( $t['swf'], $t['exe'] );
2530 if ( function_exists( 'current_user_can' ) )
2531 $unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
2533 if ( empty( $unfiltered ) )
2534 unset( $t['htm|html'] );
2537 * Filters list of allowed mime types and file extensions.
2541 * @param array $t Mime types keyed by the file extension regex corresponding to
2542 * those types. 'swf' and 'exe' removed from full list. 'htm|html' also
2543 * removed depending on '$user' capabilities.
2544 * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user).
2546 return apply_filters( 'upload_mimes', $t, $user );
2550 * Display "Are You Sure" message to confirm the action being taken.
2552 * If the action has the nonce explain message, then it will be displayed
2553 * along with the "Are you sure?" message.
2557 * @param string $action The nonce action.
2559 function wp_nonce_ays( $action ) {
2560 if ( 'log-out' == $action ) {
2561 $html = sprintf( __( 'You are attempting to log out of %s' ), get_bloginfo( 'name' ) ) . '</p><p>';
2562 $redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
2563 $html .= sprintf( __( "Do you really want to <a href='%s'>log out</a>?"), wp_logout_url( $redirect_to ) );
2565 $html = __( 'Are you sure you want to do this?' );
2566 if ( wp_get_referer() )
2567 $html .= "</p><p><a href='" . esc_url( remove_query_arg( 'updated', wp_get_referer() ) ) . "'>" . __( 'Please try again.' ) . "</a>";
2570 wp_die( $html, __( 'WordPress Failure Notice' ), 403 );
2574 * Kill WordPress execution and display HTML message with error message.
2576 * This function complements the `die()` PHP function. The difference is that
2577 * HTML will be displayed to the user. It is recommended to use this function
2578 * only when the execution should not continue any further. It is not recommended
2579 * to call this function very often, and try to handle as many errors as possible
2580 * silently or more gracefully.
2582 * As a shorthand, the desired HTTP response code may be passed as an integer to
2583 * the `$title` parameter (the default title would apply) or the `$args` parameter.
2586 * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
2587 * an integer to be used as the response code.
2589 * @param string|WP_Error $message Optional. Error message. If this is a WP_Error object,
2590 * the error's messages are used. Default empty.
2591 * @param string|int $title Optional. Error title. If `$message` is a `WP_Error` object,
2592 * error data with the key 'title' may be used to specify the title.
2593 * If `$title` is an integer, then it is treated as the response
2594 * code. Default empty.
2595 * @param string|array|int $args {
2596 * Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
2597 * as the response code. Default empty array.
2599 * @type int $response The HTTP response code. Default 500.
2600 * @type bool $back_link Whether to include a link to go back. Default false.
2601 * @type string $text_direction The text direction. This is only useful internally, when WordPress
2602 * is still loading and the site's locale is not set up yet. Accepts 'rtl'.
2603 * Default is the value of is_rtl().
2606 function wp_die( $message = '', $title = '', $args = array() ) {
2608 if ( is_int( $args ) ) {
2609 $args = array( 'response' => $args );
2610 } elseif ( is_int( $title ) ) {
2611 $args = array( 'response' => $title );
2615 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
2617 * Filters the callback for killing WordPress execution for Ajax requests.
2621 * @param callable $function Callback function name.
2623 $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
2624 } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
2626 * Filters the callback for killing WordPress execution for XML-RPC requests.
2630 * @param callable $function Callback function name.
2632 $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
2635 * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
2639 * @param callable $function Callback function name.
2641 $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
2644 call_user_func( $function, $message, $title, $args );
2648 * Kills WordPress execution and display HTML message with error message.
2650 * This is the default handler for wp_die if you want a custom one for your
2651 * site then you can overload using the {@see 'wp_die_handler'} filter in wp_die().
2656 * @param string $message Error message.
2657 * @param string $title Optional. Error title. Default empty.
2658 * @param string|array $args Optional. Arguments to control behavior. Default empty array.
2660 function _default_wp_die_handler( $message, $title = '', $args = array() ) {
2661 $defaults = array( 'response' => 500 );
2662 $r = wp_parse_args($args, $defaults);
2664 $have_gettext = function_exists('__');
2666 if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
2667 if ( empty( $title ) ) {
2668 $error_data = $message->get_error_data();
2669 if ( is_array( $error_data ) && isset( $error_data['title'] ) )
2670 $title = $error_data['title'];
2672 $errors = $message->get_error_messages();
2673 switch ( count( $errors ) ) {
2678 $message = "<p>{$errors[0]}</p>";
2681 $message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
2684 } elseif ( is_string( $message ) ) {
2685 $message = "<p>$message</p>";
2688 if ( isset( $r['back_link'] ) && $r['back_link'] ) {
2689 $back_text = $have_gettext? __('« Back') : '« Back';
2690 $message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";
2693 if ( ! did_action( 'admin_head' ) ) :
2694 if ( !headers_sent() ) {
2695 status_header( $r['response'] );
2697 header( 'Content-Type: text/html; charset=utf-8' );
2700 if ( empty($title) )
2701 $title = $have_gettext ? __('WordPress › Error') : 'WordPress › Error';
2703 $text_direction = 'ltr';
2704 if ( isset($r['text_direction']) && 'rtl' == $r['text_direction'] )
2705 $text_direction = 'rtl';
2706 elseif ( function_exists( 'is_rtl' ) && is_rtl() )
2707 $text_direction = 'rtl';
2710 <!-- Ticket #11289, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono
2712 <html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) ) language_attributes(); else echo "dir='$text_direction'"; ?>>
2714 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2715 <meta name="viewport" content="width=device-width">
2717 if ( function_exists( 'wp_no_robots' ) ) {
2721 <title><?php echo $title ?></title>
2722 <style type="text/css">
2724 background: #f1f1f1;
2729 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
2733 -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2734 box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2737 border-bottom: 1px solid #dadada;
2743 padding-bottom: 7px;
2751 margin: 25px 0 20px;
2754 font-family: Consolas, Monaco, monospace;
2757 margin-bottom: 10px;
2771 0 0 2px 1px rgba(30, 140, 190, .8);
2774 0 0 2px 1px rgba(30, 140, 190, .8);
2778 background: #f7f7f7;
2779 border: 1px solid #ccc;
2781 display: inline-block;
2782 text-decoration: none;
2787 padding: 0 10px 1px;
2789 -webkit-border-radius: 3px;
2790 -webkit-appearance: none;
2792 white-space: nowrap;
2793 -webkit-box-sizing: border-box;
2794 -moz-box-sizing: border-box;
2795 box-sizing: border-box;
2797 -webkit-box-shadow: 0 1px 0 #ccc;
2798 box-shadow: 0 1px 0 #ccc;
2799 vertical-align: top;
2802 .button.button-large {
2805 padding: 0 12px 2px;
2810 background: #fafafa;
2816 border-color: #5b9dd9;
2817 -webkit-box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2818 box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2825 -webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2826 box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2827 -webkit-transform: translateY(1px);
2828 -ms-transform: translateY(1px);
2829 transform: translateY(1px);
2833 if ( 'rtl' == $text_direction ) {
2834 echo 'body { font-family: Tahoma, Arial; }';
2839 <body id="error-page">
2840 <?php endif; // ! did_action( 'admin_head' ) ?>
2841 <?php echo $message; ?>
2849 * Kill WordPress execution and display XML message with error message.
2851 * This is the handler for wp_die when processing XMLRPC requests.
2856 * @global wp_xmlrpc_server $wp_xmlrpc_server
2858 * @param string $message Error message.
2859 * @param string $title Optional. Error title. Default empty.
2860 * @param string|array $args Optional. Arguments to control behavior. Default empty array.
2862 function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
2863 global $wp_xmlrpc_server;
2864 $defaults = array( 'response' => 500 );
2866 $r = wp_parse_args($args, $defaults);
2868 if ( $wp_xmlrpc_server ) {
2869 $error = new IXR_Error( $r['response'] , $message);
2870 $wp_xmlrpc_server->output( $error->getXml() );
2876 * Kill WordPress ajax execution.
2878 * This is the handler for wp_die when processing Ajax requests.
2883 * @param string $message Optional. Response to print. Default empty.
2885 function _ajax_wp_die_handler( $message = '' ) {
2886 if ( is_scalar( $message ) )
2887 die( (string) $message );
2892 * Kill WordPress execution.
2894 * This is the handler for wp_die when processing APP requests.
2899 * @param string $message Optional. Response to print. Default empty.
2901 function _scalar_wp_die_handler( $message = '' ) {
2902 if ( is_scalar( $message ) )
2903 die( (string) $message );
2908 * Encode a variable into JSON, with some sanity checks.
2912 * @param mixed $data Variable (usually an array or object) to encode as JSON.
2913 * @param int $options Optional. Options to be passed to json_encode(). Default 0.
2914 * @param int $depth Optional. Maximum depth to walk through $data. Must be
2915 * greater than 0. Default 512.
2916 * @return string|false The JSON encoded string, or false if it cannot be encoded.
2918 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
2920 * json_encode() has had extra params added over the years.
2921 * $options was added in 5.3, and $depth in 5.5.
2922 * We need to make sure we call it with the correct arguments.
2924 if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
2925 $args = array( $data, $options, $depth );
2926 } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
2927 $args = array( $data, $options );
2929 $args = array( $data );
2932 // Prepare the data for JSON serialization.
2933 $args[0] = _wp_json_prepare_data( $data );
2935 $json = @call_user_func_array( 'json_encode', $args );
2937 // If json_encode() was successful, no need to do more sanity checking.
2938 // ... unless we're in an old version of PHP, and json_encode() returned
2939 // a string containing 'null'. Then we need to do more sanity checking.
2940 if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) {
2945 $args[0] = _wp_json_sanity_check( $data, $depth );
2946 } catch ( Exception $e ) {
2950 return call_user_func_array( 'json_encode', $args );
2954 * Perform sanity checks on data that shall be encoded to JSON.
2960 * @see wp_json_encode()
2962 * @param mixed $data Variable (usually an array or object) to encode as JSON.
2963 * @param int $depth Maximum depth to walk through $data. Must be greater than 0.
2964 * @return mixed The sanitized data that shall be encoded to JSON.
2966 function _wp_json_sanity_check( $data, $depth ) {
2968 throw new Exception( 'Reached depth limit' );
2971 if ( is_array( $data ) ) {
2973 foreach ( $data as $id => $el ) {
2974 // Don't forget to sanitize the ID!
2975 if ( is_string( $id ) ) {
2976 $clean_id = _wp_json_convert_string( $id );
2981 // Check the element type, so that we're only recursing if we really have to.
2982 if ( is_array( $el ) || is_object( $el ) ) {
2983 $output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
2984 } elseif ( is_string( $el ) ) {
2985 $output[ $clean_id ] = _wp_json_convert_string( $el );
2987 $output[ $clean_id ] = $el;
2990 } elseif ( is_object( $data ) ) {
2991 $output = new stdClass;
2992 foreach ( $data as $id => $el ) {
2993 if ( is_string( $id ) ) {
2994 $clean_id = _wp_json_convert_string( $id );
2999 if ( is_array( $el ) || is_object( $el ) ) {
3000 $output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
3001 } elseif ( is_string( $el ) ) {
3002 $output->$clean_id = _wp_json_convert_string( $el );
3004 $output->$clean_id = $el;
3007 } elseif ( is_string( $data ) ) {
3008 return _wp_json_convert_string( $data );
3017 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
3023 * @see _wp_json_sanity_check()
3025 * @staticvar bool $use_mb
3027 * @param string $string The string which is to be converted.
3028 * @return string The checked string.
3030 function _wp_json_convert_string( $string ) {
3031 static $use_mb = null;
3032 if ( is_null( $use_mb ) ) {
3033 $use_mb = function_exists( 'mb_convert_encoding' );
3037 $encoding = mb_detect_encoding( $string, mb_detect_order(), true );
3039 return mb_convert_encoding( $string, 'UTF-8', $encoding );
3041 return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
3044 return wp_check_invalid_utf8( $string, true );
3049 * Prepares response data to be serialized to JSON.
3051 * This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
3057 * @param mixed $data Native representation.
3058 * @return bool|int|float|null|string|array Data ready for `json_encode()`.
3060 function _wp_json_prepare_data( $data ) {
3061 if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) {
3065 switch ( gettype( $data ) ) {
3071 // These values can be passed through.
3075 // Arrays must be mapped in case they also return objects.
3076 return array_map( '_wp_json_prepare_data', $data );
3079 // If this is an incomplete object (__PHP_Incomplete_Class), bail.
3080 if ( ! is_object( $data ) ) {
3084 if ( $data instanceof JsonSerializable ) {
3085 $data = $data->jsonSerialize();
3087 $data = get_object_vars( $data );
3090 // Now, pass the array (or whatever was returned from jsonSerialize through).
3091 return _wp_json_prepare_data( $data );
3099 * Send a JSON response back to an Ajax request.
3103 * @param mixed $response Variable (usually an array or object) to encode as JSON,
3104 * then print and die.
3106 function wp_send_json( $response ) {
3107 @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
3108 echo wp_json_encode( $response );
3109 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
3116 * Send a JSON response back to an Ajax request, indicating success.
3120 * @param mixed $data Data to encode as JSON, then print and die.
3122 function wp_send_json_success( $data = null ) {
3123 $response = array( 'success' => true );
3125 if ( isset( $data ) )
3126 $response['data'] = $data;
3128 wp_send_json( $response );
3132 * Send a JSON response back to an Ajax request, indicating failure.
3134 * If the `$data` parameter is a WP_Error object, the errors
3135 * within the object are processed and output as an array of error
3136 * codes and corresponding messages. All other types are output
3137 * without further processing.
3140 * @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
3142 * @param mixed $data Data to encode as JSON, then print and die.
3144 function wp_send_json_error( $data = null ) {
3145 $response = array( 'success' => false );
3147 if ( isset( $data ) ) {
3148 if ( is_wp_error( $data ) ) {
3150 foreach ( $data->errors as $code => $messages ) {
3151 foreach ( $messages as $message ) {
3152 $result[] = array( 'code' => $code, 'message' => $message );
3156 $response['data'] = $result;
3158 $response['data'] = $data;
3162 wp_send_json( $response );
3166 * Checks that a JSONP callback is a valid JavaScript callback.
3168 * Only allows alphanumeric characters and the dot character in callback
3169 * function names. This helps to mitigate XSS attacks caused by directly
3170 * outputting user input.
3174 * @param string $callback Supplied JSONP callback function.
3175 * @return bool True if valid callback, otherwise false.
3177 function wp_check_jsonp_callback( $callback ) {
3178 if ( ! is_string( $callback ) ) {
3182 $jsonp_callback = preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count );
3184 return 0 === $illegal_char_count;
3188 * Retrieve the WordPress home page URL.
3190 * If the constant named 'WP_HOME' exists, then it will be used and returned
3191 * by the function. This can be used to counter the redirection on your local
3192 * development environment.
3199 * @param string $url URL for the home location.
3200 * @return string Homepage location.
3202 function _config_wp_home( $url = '' ) {
3203 if ( defined( 'WP_HOME' ) )
3204 return untrailingslashit( WP_HOME );
3209 * Retrieve the WordPress site URL.
3211 * If the constant named 'WP_SITEURL' is defined, then the value in that
3212 * constant will always be returned. This can be used for debugging a site
3213 * on your localhost while not having to change the database to your URL.
3220 * @param string $url URL to set the WordPress site location.
3221 * @return string The WordPress Site URL.
3223 function _config_wp_siteurl( $url = '' ) {
3224 if ( defined( 'WP_SITEURL' ) )
3225 return untrailingslashit( WP_SITEURL );
3230 * Set the localized direction for MCE plugin.
3232 * Will only set the direction to 'rtl', if the WordPress locale has
3233 * the text direction set to 'rtl'.
3235 * Fills in the 'directionality' setting, enables the 'directionality'
3236 * plugin, and adds the 'ltr' button to 'toolbar1', formerly
3237 * 'theme_advanced_buttons1' array keys. These keys are then returned
3238 * in the $mce_init (TinyMCE settings) array.
3243 * @param array $mce_init MCE settings array.
3244 * @return array Direction set for 'rtl', if needed by locale.
3246 function _mce_set_direction( $mce_init ) {
3248 $mce_init['directionality'] = 'rtl';
3249 $mce_init['rtl_ui'] = true;
3251 if ( ! empty( $mce_init['plugins'] ) && strpos( $mce_init['plugins'], 'directionality' ) === false ) {
3252 $mce_init['plugins'] .= ',directionality';
3255 if ( ! empty( $mce_init['toolbar1'] ) && ! preg_match( '/\bltr\b/', $mce_init['toolbar1'] ) ) {
3256 $mce_init['toolbar1'] .= ',ltr';
3265 * Convert smiley code to the icon graphic file equivalent.
3267 * You can turn off smilies, by going to the write setting screen and unchecking
3268 * the box, or by setting 'use_smilies' option to false or removing the option.
3270 * Plugins may override the default smiley list by setting the $wpsmiliestrans
3271 * to an array, with the key the code the blogger types in and the value the
3274 * The $wp_smiliessearch global is for the regular expression and is set each
3275 * time the function is called.
3277 * The full list of smilies can be found in the function and won't be listed in
3278 * the description. Probably should create a Codex page for it, so that it is
3281 * @global array $wpsmiliestrans
3282 * @global array $wp_smiliessearch
3286 function smilies_init() {
3287 global $wpsmiliestrans, $wp_smiliessearch;
3289 // don't bother setting up smilies if they are disabled
3290 if ( !get_option( 'use_smilies' ) )
3293 if ( !isset( $wpsmiliestrans ) ) {
3294 $wpsmiliestrans = array(
3295 ':mrgreen:' => 'mrgreen.png',
3296 ':neutral:' => "\xf0\x9f\x98\x90",
3297 ':twisted:' => "\xf0\x9f\x98\x88",
3298 ':arrow:' => "\xe2\x9e\xa1",
3299 ':shock:' => "\xf0\x9f\x98\xaf",
3300 ':smile:' => "\xf0\x9f\x99\x82",
3301 ':???:' => "\xf0\x9f\x98\x95",
3302 ':cool:' => "\xf0\x9f\x98\x8e",
3303 ':evil:' => "\xf0\x9f\x91\xbf",
3304 ':grin:' => "\xf0\x9f\x98\x80",
3305 ':idea:' => "\xf0\x9f\x92\xa1",
3306 ':oops:' => "\xf0\x9f\x98\xb3",
3307 ':razz:' => "\xf0\x9f\x98\x9b",
3308 ':roll:' => "\xf0\x9f\x99\x84",
3309 ':wink:' => "\xf0\x9f\x98\x89",
3310 ':cry:' => "\xf0\x9f\x98\xa5",
3311 ':eek:' => "\xf0\x9f\x98\xae",
3312 ':lol:' => "\xf0\x9f\x98\x86",
3313 ':mad:' => "\xf0\x9f\x98\xa1",
3314 ':sad:' => "\xf0\x9f\x99\x81",
3315 '8-)' => "\xf0\x9f\x98\x8e",
3316 '8-O' => "\xf0\x9f\x98\xaf",
3317 ':-(' => "\xf0\x9f\x99\x81",
3318 ':-)' => "\xf0\x9f\x99\x82",
3319 ':-?' => "\xf0\x9f\x98\x95",
3320 ':-D' => "\xf0\x9f\x98\x80",
3321 ':-P' => "\xf0\x9f\x98\x9b",
3322 ':-o' => "\xf0\x9f\x98\xae",
3323 ':-x' => "\xf0\x9f\x98\xa1",
3324 ':-|' => "\xf0\x9f\x98\x90",
3325 ';-)' => "\xf0\x9f\x98\x89",
3326 // This one transformation breaks regular text with frequency.
3327 // '8)' => "\xf0\x9f\x98\x8e",
3328 '8O' => "\xf0\x9f\x98\xaf",
3329 ':(' => "\xf0\x9f\x99\x81",
3330 ':)' => "\xf0\x9f\x99\x82",
3331 ':?' => "\xf0\x9f\x98\x95",
3332 ':D' => "\xf0\x9f\x98\x80",
3333 ':P' => "\xf0\x9f\x98\x9b",
3334 ':o' => "\xf0\x9f\x98\xae",
3335 ':x' => "\xf0\x9f\x98\xa1",
3336 ':|' => "\xf0\x9f\x98\x90",
3337 ';)' => "\xf0\x9f\x98\x89",
3338 ':!:' => "\xe2\x9d\x97",
3339 ':?:' => "\xe2\x9d\x93",
3343 if (count($wpsmiliestrans) == 0) {
3348 * NOTE: we sort the smilies in reverse key order. This is to make sure
3349 * we match the longest possible smilie (:???: vs :?) as the regular
3350 * expression used below is first-match
3352 krsort($wpsmiliestrans);
3354 $spaces = wp_spaces_regexp();
3356 // Begin first "subpattern"
3357 $wp_smiliessearch = '/(?<=' . $spaces . '|^)';
3360 foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
3361 $firstchar = substr($smiley, 0, 1);
3362 $rest = substr($smiley, 1);
3365 if ($firstchar != $subchar) {
3366 if ($subchar != '') {
3367 $wp_smiliessearch .= ')(?=' . $spaces . '|$)'; // End previous "subpattern"
3368 $wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern"
3370 $subchar = $firstchar;
3371 $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
3373 $wp_smiliessearch .= '|';
3375 $wp_smiliessearch .= preg_quote($rest, '/');
3378 $wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
3383 * Merge user defined arguments into defaults array.
3385 * This function is used throughout WordPress to allow for both string or array
3386 * to be merged into another array.
3390 * @param string|array $args Value to merge with $defaults
3391 * @param array $defaults Optional. Array that serves as the defaults. Default empty.
3392 * @return array Merged user defined values with defaults.
3394 function wp_parse_args( $args, $defaults = '' ) {
3395 if ( is_object( $args ) )
3396 $r = get_object_vars( $args );
3397 elseif ( is_array( $args ) )
3400 wp_parse_str( $args, $r );
3402 if ( is_array( $defaults ) )
3403 return array_merge( $defaults, $r );
3408 * Clean up an array, comma- or space-separated list of IDs.
3412 * @param array|string $list List of ids.
3413 * @return array Sanitized array of IDs.
3415 function wp_parse_id_list( $list ) {
3416 if ( !is_array($list) )
3417 $list = preg_split('/[\s,]+/', $list);
3419 return array_unique(array_map('absint', $list));
3423 * Extract a slice of an array, given a list of keys.
3427 * @param array $array The original array.
3428 * @param array $keys The list of keys.
3429 * @return array The array slice.
3431 function wp_array_slice_assoc( $array, $keys ) {
3433 foreach ( $keys as $key )
3434 if ( isset( $array[ $key ] ) )
3435 $slice[ $key ] = $array[ $key ];
3441 * Determines if the variable is a numeric-indexed array.
3445 * @param mixed $data Variable to check.
3446 * @return bool Whether the variable is a list.
3448 function wp_is_numeric_array( $data ) {
3449 if ( ! is_array( $data ) ) {
3453 $keys = array_keys( $data );
3454 $string_keys = array_filter( $keys, 'is_string' );
3455 return count( $string_keys ) === 0;
3459 * Filters a list of objects, based on a set of key => value arguments.
3463 * @param array $list An array of objects to filter
3464 * @param array $args Optional. An array of key => value arguments to match
3465 * against each object. Default empty array.
3466 * @param string $operator Optional. The logical operation to perform. 'or' means
3467 * only one element from the array needs to match; 'and'
3468 * means all elements must match; 'not' means no elements may
3469 * match. Default 'and'.
3470 * @param bool|string $field A field from the object to place instead of the entire object.
3472 * @return array A list of objects or object fields.
3474 function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
3475 if ( ! is_array( $list ) )
3478 $list = wp_list_filter( $list, $args, $operator );
3481 $list = wp_list_pluck( $list, $field );
3487 * Filters a list of objects, based on a set of key => value arguments.
3491 * @param array $list An array of objects to filter.
3492 * @param array $args Optional. An array of key => value arguments to match
3493 * against each object. Default empty array.
3494 * @param string $operator Optional. The logical operation to perform. 'AND' means
3495 * all elements from the array must match. 'OR' means only
3496 * one element needs to match. 'NOT' means no elements may
3497 * match. Default 'AND'.
3498 * @return array Array of found values.
3500 function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
3501 if ( ! is_array( $list ) )
3504 if ( empty( $args ) )
3507 $operator = strtoupper( $operator );
3508 $count = count( $args );
3509 $filtered = array();
3511 foreach ( $list as $key => $obj ) {
3512 $to_match = (array) $obj;
3515 foreach ( $args as $m_key => $m_value ) {
3516 if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
3520 if ( ( 'AND' == $operator && $matched == $count )
3521 || ( 'OR' == $operator && $matched > 0 )
3522 || ( 'NOT' == $operator && 0 == $matched ) ) {
3523 $filtered[$key] = $obj;
3531 * Pluck a certain field out of each object in a list.
3533 * This has the same functionality and prototype of
3534 * array_column() (PHP 5.5) but also supports objects.
3537 * @since 4.0.0 $index_key parameter added.
3539 * @param array $list List of objects or arrays
3540 * @param int|string $field Field from the object to place instead of the entire object
3541 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
3543 * @return array Array of found values. If `$index_key` is set, an array of found values with keys
3544 * corresponding to `$index_key`. If `$index_key` is null, array keys from the original
3545 * `$list` will be preserved in the results.
3547 function wp_list_pluck( $list, $field, $index_key = null ) {
3548 if ( ! $index_key ) {
3550 * This is simple. Could at some point wrap array_column()
3551 * if we knew we had an array of arrays.
3553 foreach ( $list as $key => $value ) {
3554 if ( is_object( $value ) ) {
3555 $list[ $key ] = $value->$field;
3557 $list[ $key ] = $value[ $field ];
3564 * When index_key is not set for a particular item, push the value
3565 * to the end of the stack. This is how array_column() behaves.
3568 foreach ( $list as $value ) {
3569 if ( is_object( $value ) ) {
3570 if ( isset( $value->$index_key ) ) {
3571 $newlist[ $value->$index_key ] = $value->$field;
3573 $newlist[] = $value->$field;
3576 if ( isset( $value[ $index_key ] ) ) {
3577 $newlist[ $value[ $index_key ] ] = $value[ $field ];
3579 $newlist[] = $value[ $field ];
3588 * Determines if Widgets library should be loaded.
3590 * Checks to make sure that the widgets library hasn't already been loaded.
3591 * If it hasn't, then it will load the widgets library and run an action hook.
3595 function wp_maybe_load_widgets() {
3597 * Filters whether to load the Widgets library.
3599 * Passing a falsey value to the filter will effectively short-circuit
3600 * the Widgets library from loading.
3604 * @param bool $wp_maybe_load_widgets Whether to load the Widgets library.
3607 if ( ! apply_filters( 'load_default_widgets', true ) ) {
3611 require_once( ABSPATH . WPINC . '/default-widgets.php' );
3613 add_action( '_admin_menu', 'wp_widgets_add_menu' );
3617 * Append the Widgets menu to the themes main menu.
3621 * @global array $submenu
3623 function wp_widgets_add_menu() {
3626 if ( ! current_theme_supports( 'widgets' ) )
3629 $submenu['themes.php'][7] = array( __( 'Widgets' ), 'edit_theme_options', 'widgets.php' );
3630 ksort( $submenu['themes.php'], SORT_NUMERIC );
3634 * Flush all output buffers for PHP 5.2.
3636 * Make sure all output buffers are flushed before our singletons are destroyed.
3640 function wp_ob_end_flush_all() {
3641 $levels = ob_get_level();
3642 for ($i=0; $i<$levels; $i++)
3647 * Load custom DB error or display WordPress DB error.
3649 * If a file exists in the wp-content directory named db-error.php, then it will
3650 * be loaded instead of displaying the WordPress DB error. If it is not found,
3651 * then the WordPress DB error will be displayed instead.
3653 * The WordPress DB error sets the HTTP status header to 500 to try to prevent
3654 * search engines from caching the message. Custom DB messages should do the
3657 * This function was backported to WordPress 2.3.2, but originally was added
3658 * in WordPress 2.5.0.
3662 * @global wpdb $wpdb WordPress database abstraction object.
3664 function dead_db() {
3667 wp_load_translations_early();
3669 // Load custom DB error template, if present.
3670 if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
3671 require_once( WP_CONTENT_DIR . '/db-error.php' );
3675 // If installing or in the admin, provide the verbose message.
3676 if ( wp_installing() || defined( 'WP_ADMIN' ) )
3677 wp_die($wpdb->error);
3679 // Otherwise, be terse.
3680 status_header( 500 );
3682 header( 'Content-Type: text/html; charset=utf-8' );
3685 <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
3687 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3688 <title><?php _e( 'Database Error' ); ?></title>
3692 <h1><?php _e( 'Error establishing a database connection' ); ?></h1>
3700 * Convert a value to non-negative integer.
3704 * @param mixed $maybeint Data you wish to have converted to a non-negative integer.
3705 * @return int A non-negative integer.
3707 function absint( $maybeint ) {
3708 return abs( intval( $maybeint ) );
3712 * Mark a function as deprecated and inform when it has been used.
3714 * There is a {@see 'hook deprecated_function_run'} that will be called that can be used
3715 * to get the backtrace up to what file and function called the deprecated
3718 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3720 * This function is to be used in every function that is deprecated.
3725 * @param string $function The function that was called.
3726 * @param string $version The version of WordPress that deprecated the function.
3727 * @param string $replacement Optional. The function that should have been called. Default null.
3729 function _deprecated_function( $function, $version, $replacement = null ) {
3732 * Fires when a deprecated function is called.
3736 * @param string $function The function that was called.
3737 * @param string $replacement The function that should have been called.
3738 * @param string $version The version of WordPress that deprecated the function.
3740 do_action( 'deprecated_function_run', $function, $replacement, $version );
3743 * Filters whether to trigger an error for deprecated functions.
3747 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3749 if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
3750 if ( function_exists( '__' ) ) {
3751 if ( ! is_null( $replacement ) )
3752 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
3754 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
3756 if ( ! is_null( $replacement ) )
3757 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
3759 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
3765 * Marks a constructor as deprecated and informs when it has been used.
3767 * Similar to _deprecated_function(), but with different strings. Used to
3768 * remove PHP4 style constructors.
3770 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3772 * This function is to be used in every PHP4 style constructor method that is deprecated.
3775 * @since 4.5.0 Added the `$parent_class` parameter.
3779 * @param string $class The class containing the deprecated constructor.
3780 * @param string $version The version of WordPress that deprecated the function.
3781 * @param string $parent_class Optional. The parent class calling the deprecated constructor.
3782 * Default empty string.
3784 function _deprecated_constructor( $class, $version, $parent_class = '' ) {
3787 * Fires when a deprecated constructor is called.
3790 * @since 4.5.0 Added the `$parent_class` parameter.
3792 * @param string $class The class containing the deprecated constructor.
3793 * @param string $version The version of WordPress that deprecated the function.
3794 * @param string $parent_class The parent class calling the deprecated constructor.
3796 do_action( 'deprecated_constructor_run', $class, $version, $parent_class );
3799 * Filters whether to trigger an error for deprecated functions.
3801 * `WP_DEBUG` must be true in addition to the filter evaluating to true.
3805 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3807 if ( WP_DEBUG && apply_filters( 'deprecated_constructor_trigger_error', true ) ) {
3808 if ( function_exists( '__' ) ) {
3809 if ( ! empty( $parent_class ) ) {
3810 /* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: __construct() method */
3811 trigger_error( sprintf( __( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.' ),
3812 $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3814 /* translators: 1: PHP class name, 2: version number, 3: __construct() method */
3815 trigger_error( sprintf( __( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
3816 $class, $version, '<pre>__construct()</pre>' ) );
3819 if ( ! empty( $parent_class ) ) {
3820 trigger_error( sprintf( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.',
3821 $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3823 trigger_error( sprintf( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
3824 $class, $version, '<pre>__construct()</pre>' ) );
3832 * Mark a file as deprecated and inform when it has been used.
3834 * There is a hook {@see 'deprecated_file_included'} that will be called that can be used
3835 * to get the backtrace up to what file and function included the deprecated
3838 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3840 * This function is to be used in every file that is deprecated.
3845 * @param string $file The file that was included.
3846 * @param string $version The version of WordPress that deprecated the file.
3847 * @param string $replacement Optional. The file that should have been included based on ABSPATH.
3849 * @param string $message Optional. A message regarding the change. Default empty.
3851 function _deprecated_file( $file, $version, $replacement = null, $message = '' ) {
3854 * Fires when a deprecated file is called.
3858 * @param string $file The file that was called.
3859 * @param string $replacement The file that should have been included based on ABSPATH.
3860 * @param string $version The version of WordPress that deprecated the file.
3861 * @param string $message A message regarding the change.
3863 do_action( 'deprecated_file_included', $file, $replacement, $version, $message );
3866 * Filters whether to trigger an error for deprecated files.
3870 * @param bool $trigger Whether to trigger the error for deprecated files. Default true.
3872 if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
3873 $message = empty( $message ) ? '' : ' ' . $message;
3874 if ( function_exists( '__' ) ) {
3875 if ( ! is_null( $replacement ) )
3876 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) . $message );
3878 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $file, $version ) . $message );
3880 if ( ! is_null( $replacement ) )
3881 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $file, $version, $replacement ) . $message );
3883 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
3888 * Mark a function argument as deprecated and inform when it has been used.
3890 * This function is to be used whenever a deprecated function argument is used.
3891 * Before this function is called, the argument must be checked for whether it was
3892 * used by comparing it to its default value or evaluating whether it is empty.
3895 * if ( ! empty( $deprecated ) ) {
3896 * _deprecated_argument( __FUNCTION__, '3.0.0' );
3900 * There is a hook deprecated_argument_run that will be called that can be used
3901 * to get the backtrace up to what file and function used the deprecated
3904 * The current behavior is to trigger a user error if WP_DEBUG is true.
3909 * @param string $function The function that was called.
3910 * @param string $version The version of WordPress that deprecated the argument used.
3911 * @param string $message Optional. A message regarding the change. Default null.
3913 function _deprecated_argument( $function, $version, $message = null ) {
3916 * Fires when a deprecated argument is called.
3920 * @param string $function The function that was called.
3921 * @param string $message A message regarding the change.
3922 * @param string $version The version of WordPress that deprecated the argument used.
3924 do_action( 'deprecated_argument_run', $function, $message, $version );
3927 * Filters whether to trigger an error for deprecated arguments.
3931 * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true.
3933 if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
3934 if ( function_exists( '__' ) ) {
3935 if ( ! is_null( $message ) )
3936 trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s'), $function, $version, $message ) );
3938 trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
3940 if ( ! is_null( $message ) )
3941 trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) );
3943 trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
3949 * Marks a deprecated action or filter hook as deprecated and throws a notice.
3951 * Use the {@see 'deprecated_hook_run'} action to get the backtrace describing where
3952 * the deprecated hook was called.
3954 * Default behavior is to trigger a user error if `WP_DEBUG` is true.
3956 * This function is called by the do_action_deprecated() and apply_filters_deprecated()
3957 * functions, and so generally does not need to be called directly.
3962 * @param string $hook The hook that was used.
3963 * @param string $version The version of WordPress that deprecated the hook.
3964 * @param string $replacement Optional. The hook that should have been used.
3965 * @param string $message Optional. A message regarding the change.
3967 function _deprecated_hook( $hook, $version, $replacement = null, $message = null ) {
3969 * Fires when a deprecated hook is called.
3973 * @param string $hook The hook that was called.
3974 * @param string $replacement The hook that should be used as a replacement.
3975 * @param string $version The version of WordPress that deprecated the argument used.
3976 * @param string $message A message regarding the change.
3978 do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message );
3981 * Filters whether to trigger deprecated hook errors.
3985 * @param bool $trigger Whether to trigger deprecated hook errors. Requires
3986 * `WP_DEBUG` to be defined true.
3988 if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) {
3989 $message = empty( $message ) ? '' : ' ' . $message;
3990 if ( ! is_null( $replacement ) ) {
3991 trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $hook, $version, $replacement ) . $message );
3993 trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $hook, $version ) . $message );
3999 * Mark something as being incorrectly called.
4001 * There is a hook {@see 'doing_it_wrong_run'} that will be called that can be used
4002 * to get the backtrace up to what file and function called the deprecated
4005 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
4010 * @param string $function The function that was called.
4011 * @param string $message A message explaining what has been done incorrectly.
4012 * @param string $version The version of WordPress where the message was added.
4014 function _doing_it_wrong( $function, $message, $version ) {
4017 * Fires when the given function is being used incorrectly.
4021 * @param string $function The function that was called.
4022 * @param string $message A message explaining what has been done incorrectly.
4023 * @param string $version The version of WordPress where the message was added.
4025 do_action( 'doing_it_wrong_run', $function, $message, $version );
4028 * Filters whether to trigger an error for _doing_it_wrong() calls.
4032 * @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true.
4034 if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
4035 if ( function_exists( '__' ) ) {
4036 $version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
4037 /* translators: %s: Codex URL */
4038 $message .= ' ' . sprintf( __( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
4039 __( 'https://codex.wordpress.org/Debugging_in_WordPress' )
4041 trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
4043 $version = is_null( $version ) ? '' : sprintf( '(This message was added in version %s.)', $version );
4044 $message .= sprintf( ' Please see <a href="%s">Debugging in WordPress</a> for more information.',
4045 'https://codex.wordpress.org/Debugging_in_WordPress'
4047 trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
4053 * Is the server running earlier than 1.5.0 version of lighttpd?
4057 * @return bool Whether the server is running lighttpd < 1.5.0.
4059 function is_lighttpd_before_150() {
4060 $server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
4061 $server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
4062 return 'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
4066 * Does the specified module exist in the Apache config?
4070 * @global bool $is_apache
4072 * @param string $mod The module, e.g. mod_rewrite.
4073 * @param bool $default Optional. The default return value if the module is not found. Default false.
4074 * @return bool Whether the specified module is loaded.
4076 function apache_mod_loaded($mod, $default = false) {
4082 if ( function_exists( 'apache_get_modules' ) ) {
4083 $mods = apache_get_modules();
4084 if ( in_array($mod, $mods) )
4086 } elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) {
4089 $phpinfo = ob_get_clean();
4090 if ( false !== strpos($phpinfo, $mod) )
4097 * Check if IIS 7+ supports pretty permalinks.
4101 * @global bool $is_iis7
4103 * @return bool Whether IIS7 supports permalinks.
4105 function iis7_supports_permalinks() {
4108 $supports_permalinks = false;
4110 /* First we check if the DOMDocument class exists. If it does not exist, then we cannot
4111 * easily update the xml configuration file, hence we just bail out and tell user that
4112 * pretty permalinks cannot be used.
4114 * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
4115 * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
4116 * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
4117 * via ISAPI then pretty permalinks will not work.
4119 $supports_permalinks = class_exists( 'DOMDocument', false ) && isset($_SERVER['IIS_UrlRewriteModule']) && ( PHP_SAPI == 'cgi-fcgi' );
4123 * Filters whether IIS 7+ supports pretty permalinks.
4127 * @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false.
4129 return apply_filters( 'iis7_supports_permalinks', $supports_permalinks );
4133 * File validates against allowed set of defined rules.
4135 * A return value of '1' means that the $file contains either '..' or './'. A
4136 * return value of '2' means that the $file contains ':' after the first
4137 * character. A return value of '3' means that the file is not in the allowed
4142 * @param string $file File path.
4143 * @param array $allowed_files List of allowed files.
4144 * @return int 0 means nothing is wrong, greater than 0 means something was wrong.
4146 function validate_file( $file, $allowed_files = '' ) {
4147 if ( false !== strpos( $file, '..' ) )
4150 if ( false !== strpos( $file, './' ) )
4153 if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files ) )
4156 if (':' == substr( $file, 1, 1 ) )
4163 * Whether to force SSL used for the Administration Screens.
4167 * @staticvar bool $forced
4169 * @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
4170 * @return bool True if forced, false if not forced.
4172 function force_ssl_admin( $force = null ) {
4173 static $forced = false;
4175 if ( !is_null( $force ) ) {
4176 $old_forced = $forced;
4185 * Guess the URL for the site.
4187 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
4192 * @return string The guessed URL.
4194 function wp_guess_url() {
4195 if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
4198 $abspath_fix = str_replace( '\\', '/', ABSPATH );
4199 $script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );
4201 // The request is for the admin
4202 if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
4203 $path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] );
4205 // The request is for a file in ABSPATH
4206 } elseif ( $script_filename_dir . '/' == $abspath_fix ) {
4207 // Strip off any file/query params in the path
4208 $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );
4211 if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
4212 // Request is hitting a file inside ABSPATH
4213 $directory = str_replace( ABSPATH, '', $script_filename_dir );
4214 // Strip off the sub directory, and any file/query params
4215 $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] );
4216 } elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
4217 // Request is hitting a file above ABSPATH
4218 $subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
4219 // Strip off any file/query params from the path, appending the sub directory to the install
4220 $path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory;
4222 $path = $_SERVER['REQUEST_URI'];
4226 $schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet
4227 $url = $schema . $_SERVER['HTTP_HOST'] . $path;
4230 return rtrim($url, '/');
4234 * Temporarily suspend cache additions.
4236 * Stops more data being added to the cache, but still allows cache retrieval.
4237 * This is useful for actions, such as imports, when a lot of data would otherwise
4238 * be almost uselessly added to the cache.
4240 * Suspension lasts for a single page load at most. Remember to call this
4241 * function again if you wish to re-enable cache adds earlier.
4245 * @staticvar bool $_suspend
4247 * @param bool $suspend Optional. Suspends additions if true, re-enables them if false.
4248 * @return bool The current suspend setting
4250 function wp_suspend_cache_addition( $suspend = null ) {
4251 static $_suspend = false;
4253 if ( is_bool( $suspend ) )
4254 $_suspend = $suspend;
4260 * Suspend cache invalidation.
4262 * Turns cache invalidation on and off. Useful during imports where you don't wont to do
4263 * invalidations every time a post is inserted. Callers must be sure that what they are
4264 * doing won't lead to an inconsistent cache when invalidation is suspended.
4268 * @global bool $_wp_suspend_cache_invalidation
4270 * @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true.
4271 * @return bool The current suspend setting.
4273 function wp_suspend_cache_invalidation( $suspend = true ) {
4274 global $_wp_suspend_cache_invalidation;
4276 $current_suspend = $_wp_suspend_cache_invalidation;
4277 $_wp_suspend_cache_invalidation = $suspend;
4278 return $current_suspend;
4282 * Determine whether a site is the main site of the current network.
4286 * @global object $current_site
4288 * @param int $site_id Optional. Site ID to test. Defaults to current site.
4289 * @return bool True if $site_id is the main site of the network, or if not
4290 * running Multisite.
4292 function is_main_site( $site_id = null ) {
4293 // This is the current network's information; 'site' is old terminology.
4294 global $current_site;
4296 if ( ! is_multisite() )
4300 $site_id = get_current_blog_id();
4302 return (int) $site_id === (int) $current_site->blog_id;
4306 * Determine whether a network is the main network of the Multisite install.
4310 * @param int $network_id Optional. Network ID to test. Defaults to current network.
4311 * @return bool True if $network_id is the main network, or if not running Multisite.
4313 function is_main_network( $network_id = null ) {
4314 if ( ! is_multisite() ) {
4318 $current_network_id = (int) get_current_site()->id;
4320 if ( null === $network_id ) {
4321 $network_id = $current_network_id;
4324 $network_id = (int) $network_id;
4326 return ( $network_id === get_main_network_id() );
4330 * Get the main network ID.
4334 * @global wpdb $wpdb WordPress database abstraction object.
4336 * @return int The ID of the main network.
4338 function get_main_network_id() {
4341 if ( ! is_multisite() ) {
4345 $current_site = get_current_site();
4347 if ( defined( 'PRIMARY_NETWORK_ID' ) ) {
4348 $main_network_id = PRIMARY_NETWORK_ID;
4349 } elseif ( isset( $current_site->id ) && 1 === (int) $current_site->id ) {
4350 // If the current network has an ID of 1, assume it is the main network.
4351 $main_network_id = 1;
4353 $main_network_id = wp_cache_get( 'primary_network_id', 'site-options' );
4355 if ( false === $main_network_id ) {
4356 $main_network_id = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1" );
4357 wp_cache_add( 'primary_network_id', $main_network_id, 'site-options' );
4362 * Filters the main network ID.
4366 * @param int $main_network_id The ID of the main network.
4368 return (int) apply_filters( 'get_main_network_id', $main_network_id );
4372 * Determine whether global terms are enabled.
4376 * @staticvar bool $global_terms
4378 * @return bool True if multisite and global terms enabled.
4380 function global_terms_enabled() {
4381 if ( ! is_multisite() )
4384 static $global_terms = null;
4385 if ( is_null( $global_terms ) ) {
4388 * Filters whether global terms are enabled.
4390 * Passing a non-null value to the filter will effectively short-circuit the function,
4391 * returning the value of the 'global_terms_enabled' site option instead.
4395 * @param null $enabled Whether global terms are enabled.
4397 $filter = apply_filters( 'global_terms_enabled', null );
4398 if ( ! is_null( $filter ) )
4399 $global_terms = (bool) $filter;
4401 $global_terms = (bool) get_site_option( 'global_terms_enabled', false );
4403 return $global_terms;
4407 * gmt_offset modification for smart timezone handling.
4409 * Overrides the gmt_offset option if we have a timezone_string available.
4413 * @return float|false Timezone GMT offset, false otherwise.
4415 function wp_timezone_override_offset() {
4416 if ( !$timezone_string = get_option( 'timezone_string' ) ) {
4420 $timezone_object = timezone_open( $timezone_string );
4421 $datetime_object = date_create();
4422 if ( false === $timezone_object || false === $datetime_object ) {
4425 return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 );
4429 * Sort-helper for timezones.
4438 function _wp_timezone_choice_usort_callback( $a, $b ) {
4439 // Don't use translated versions of Etc
4440 if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
4441 // Make the order of these more like the old dropdown
4442 if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4443 return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
4445 if ( 'UTC' === $a['city'] ) {
4446 if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4451 if ( 'UTC' === $b['city'] ) {
4452 if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
4457 return strnatcasecmp( $a['city'], $b['city'] );
4459 if ( $a['t_continent'] == $b['t_continent'] ) {
4460 if ( $a['t_city'] == $b['t_city'] ) {
4461 return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
4463 return strnatcasecmp( $a['t_city'], $b['t_city'] );
4465 // Force Etc to the bottom of the list
4466 if ( 'Etc' === $a['continent'] ) {
4469 if ( 'Etc' === $b['continent'] ) {
4472 return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
4477 * Gives a nicely-formatted list of timezone strings.
4481 * @staticvar bool $mo_loaded
4483 * @param string $selected_zone Selected timezone.
4486 function wp_timezone_choice( $selected_zone ) {
4487 static $mo_loaded = false;
4489 $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
4491 // Load translations for continents and cities
4492 if ( !$mo_loaded ) {
4493 $locale = get_locale();
4494 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo';
4495 load_textdomain( 'continents-cities', $mofile );
4500 foreach ( timezone_identifiers_list() as $zone ) {
4501 $zone = explode( '/', $zone );
4502 if ( !in_array( $zone[0], $continents ) ) {
4506 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
4508 0 => ( isset( $zone[0] ) && $zone[0] ),
4509 1 => ( isset( $zone[1] ) && $zone[1] ),
4510 2 => ( isset( $zone[2] ) && $zone[2] ),
4512 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
4513 $exists[4] = ( $exists[1] && $exists[3] );
4514 $exists[5] = ( $exists[2] && $exists[3] );
4517 'continent' => ( $exists[0] ? $zone[0] : '' ),
4518 'city' => ( $exists[1] ? $zone[1] : '' ),
4519 'subcity' => ( $exists[2] ? $zone[2] : '' ),
4520 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
4521 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
4522 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
4525 usort( $zonen, '_wp_timezone_choice_usort_callback' );
4527 $structure = array();
4529 if ( empty( $selected_zone ) ) {
4530 $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
4533 foreach ( $zonen as $key => $zone ) {
4534 // Build value in an array to join later
4535 $value = array( $zone['continent'] );
4537 if ( empty( $zone['city'] ) ) {
4538 // It's at the continent level (generally won't happen)
4539 $display = $zone['t_continent'];
4541 // It's inside a continent group
4543 // Continent optgroup
4544 if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
4545 $label = $zone['t_continent'];
4546 $structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
4549 // Add the city to the value
4550 $value[] = $zone['city'];
4552 $display = $zone['t_city'];
4553 if ( !empty( $zone['subcity'] ) ) {
4554 // Add the subcity to the value
4555 $value[] = $zone['subcity'];
4556 $display .= ' - ' . $zone['t_subcity'];
4561 $value = join( '/', $value );
4563 if ( $value === $selected_zone ) {
4564 $selected = 'selected="selected" ';
4566 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
4568 // Close continent optgroup
4569 if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) {
4570 $structure[] = '</optgroup>';
4575 $structure[] = '<optgroup label="'. esc_attr__( 'UTC' ) .'">';
4577 if ( 'UTC' === $selected_zone )
4578 $selected = 'selected="selected" ';
4579 $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __('UTC') . '</option>';
4580 $structure[] = '</optgroup>';
4582 // Do manual UTC offsets
4583 $structure[] = '<optgroup label="'. esc_attr__( 'Manual Offsets' ) .'">';
4584 $offset_range = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5,
4585 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14);
4586 foreach ( $offset_range as $offset ) {
4588 $offset_name = '+' . $offset;
4590 $offset_name = (string) $offset;
4592 $offset_value = $offset_name;
4593 $offset_name = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset_name);
4594 $offset_name = 'UTC' . $offset_name;
4595 $offset_value = 'UTC' . $offset_value;
4597 if ( $offset_value === $selected_zone )
4598 $selected = 'selected="selected" ';
4599 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . "</option>";
4602 $structure[] = '</optgroup>';
4604 return join( "\n", $structure );
4608 * Strip close comment and close php tags from file headers used by WP.
4613 * @see https://core.trac.wordpress.org/ticket/8497
4615 * @param string $str Header comment to clean up.
4618 function _cleanup_header_comment( $str ) {
4619 return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
4623 * Permanently delete comments or posts of any type that have held a status
4624 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
4626 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
4630 * @global wpdb $wpdb WordPress database abstraction object.
4632 function wp_scheduled_delete() {
4635 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
4637 $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);
4639 foreach ( (array) $posts_to_delete as $post ) {
4640 $post_id = (int) $post['post_id'];
4644 $del_post = get_post($post_id);
4646 if ( !$del_post || 'trash' != $del_post->post_status ) {
4647 delete_post_meta($post_id, '_wp_trash_meta_status');
4648 delete_post_meta($post_id, '_wp_trash_meta_time');
4650 wp_delete_post($post_id);
4654 $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);
4656 foreach ( (array) $comments_to_delete as $comment ) {
4657 $comment_id = (int) $comment['comment_id'];
4661 $del_comment = get_comment($comment_id);
4663 if ( !$del_comment || 'trash' != $del_comment->comment_approved ) {
4664 delete_comment_meta($comment_id, '_wp_trash_meta_time');
4665 delete_comment_meta($comment_id, '_wp_trash_meta_status');
4667 wp_delete_comment( $del_comment );
4673 * Retrieve metadata from a file.
4675 * Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
4676 * Each piece of metadata must be on its own line. Fields can not span multiple
4677 * lines, the value will get cut at the end of the first line.
4679 * If the file data is not within that first 8kiB, then the author should correct
4680 * their plugin file and move the data headers to the top.
4682 * @link https://codex.wordpress.org/File_Header
4686 * @param string $file Path to the file.
4687 * @param array $default_headers List of headers, in the format array('HeaderKey' => 'Header Name').
4688 * @param string $context Optional. If specified adds filter hook {@see 'extra_$context_headers'}.
4690 * @return array Array of file headers in `HeaderKey => Header Value` format.
4692 function get_file_data( $file, $default_headers, $context = '' ) {
4693 // We don't need to write to the file, so just open for reading.
4694 $fp = fopen( $file, 'r' );
4696 // Pull only the first 8kiB of the file in.
4697 $file_data = fread( $fp, 8192 );
4699 // PHP will close file handle, but we are good citizens.
4702 // Make sure we catch CR-only line endings.
4703 $file_data = str_replace( "\r", "\n", $file_data );
4706 * Filters extra file headers by context.
4708 * The dynamic portion of the hook name, `$context`, refers to
4709 * the context where extra headers might be loaded.
4713 * @param array $extra_context_headers Empty array by default.
4715 if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
4716 $extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
4717 $all_headers = array_merge( $extra_headers, (array) $default_headers );
4719 $all_headers = $default_headers;
4722 foreach ( $all_headers as $field => $regex ) {
4723 if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
4724 $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
4726 $all_headers[ $field ] = '';
4729 return $all_headers;
4735 * Useful for returning true to filters easily.
4739 * @see __return_false()
4741 * @return true True.
4743 function __return_true() {
4750 * Useful for returning false to filters easily.
4754 * @see __return_true()
4756 * @return false False.
4758 function __return_false() {
4765 * Useful for returning 0 to filters easily.
4771 function __return_zero() {
4776 * Returns an empty array.
4778 * Useful for returning an empty array to filters easily.
4782 * @return array Empty array.
4784 function __return_empty_array() {
4791 * Useful for returning null to filters easily.
4795 * @return null Null value.
4797 function __return_null() {
4802 * Returns an empty string.
4804 * Useful for returning an empty string to filters easily.
4808 * @see __return_null()
4810 * @return string Empty string.
4812 function __return_empty_string() {
4817 * Send a HTTP header to disable content type sniffing in browsers which support it.
4821 * @see https://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
4822 * @see https://src.chromium.org/viewvc/chrome?view=rev&revision=6985
4824 function send_nosniff_header() {
4825 @header( 'X-Content-Type-Options: nosniff' );
4829 * Return a MySQL expression for selecting the week number based on the start_of_week option.
4834 * @param string $column Database column.
4835 * @return string SQL clause.
4837 function _wp_mysql_week( $column ) {
4838 switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
4840 return "WEEK( $column, 1 )";
4846 return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
4849 return "WEEK( $column, 0 )";
4854 * Find hierarchy loops using a callback function that maps object IDs to parent IDs.
4859 * @param callable $callback Function that accepts ( ID, $callback_args ) and outputs parent_ID.
4860 * @param int $start The ID to start the loop check at.
4861 * @param int $start_parent The parent_ID of $start to use instead of calling $callback( $start ).
4862 * Use null to always use $callback
4863 * @param array $callback_args Optional. Additional arguments to send to $callback.
4864 * @return array IDs of all members of loop.
4866 function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
4867 $override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
4869 if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
4872 return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
4876 * Use the "The Tortoise and the Hare" algorithm to detect loops.
4878 * For every step of the algorithm, the hare takes two steps and the tortoise one.
4879 * If the hare ever laps the tortoise, there must be a loop.
4884 * @param callable $callback Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
4885 * @param int $start The ID to start the loop check at.
4886 * @param array $override Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback.
4887 * Default empty array.
4888 * @param array $callback_args Optional. Additional arguments to send to $callback. Default empty array.
4889 * @param bool $_return_loop Optional. Return loop members or just detect presence of loop? Only set
4890 * to true if you already know the given $start is part of a loop (otherwise
4891 * the returned array might include branches). Default false.
4892 * @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if
4895 function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
4896 $tortoise = $hare = $evanescent_hare = $start;
4899 // Set evanescent_hare to one past hare
4900 // Increment hare two steps
4904 ( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
4906 ( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
4908 if ( $_return_loop )
4909 $return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
4911 // tortoise got lapped - must be a loop
4912 if ( $tortoise == $evanescent_hare || $tortoise == $hare )
4913 return $_return_loop ? $return : $tortoise;
4915 // Increment tortoise by one step
4916 $tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
4923 * Send a HTTP header to limit rendering of pages to same origin iframes.
4927 * @see https://developer.mozilla.org/en/the_x-frame-options_response_header
4929 function send_frame_options_header() {
4930 @header( 'X-Frame-Options: SAMEORIGIN' );
4934 * Retrieve a list of protocols to allow in HTML attributes.
4937 * @since 4.3.0 Added 'webcal' to the protocols array.
4942 * @staticvar array $protocols
4944 * @return array Array of allowed protocols. Defaults to an array containing 'http', 'https',
4945 * 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet',
4946 * 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', and 'webcal'.
4948 function wp_allowed_protocols() {
4949 static $protocols = array();
4951 if ( empty( $protocols ) ) {
4952 $protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal' );
4955 * Filters the list of protocols allowed in HTML attributes.
4959 * @param array $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more.
4961 $protocols = apply_filters( 'kses_allowed_protocols', $protocols );
4968 * Return a comma-separated string of functions that have been called to get
4969 * to the current point in code.
4973 * @see https://core.trac.wordpress.org/ticket/19589
4975 * @param string $ignore_class Optional. A class to ignore all function calls within - useful
4976 * when you want to just give info about the callee. Default null.
4977 * @param int $skip_frames Optional. A number of stack frames to skip - useful for unwinding
4978 * back to the source of the issue. Default 0.
4979 * @param bool $pretty Optional. Whether or not you want a comma separated string or raw
4980 * array returned. Default true.
4981 * @return string|array Either a string containing a reversed comma separated trace or an array
4982 * of individual calls.
4984 function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
4985 if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) )
4986 $trace = debug_backtrace( false );
4988 $trace = debug_backtrace();
4991 $check_class = ! is_null( $ignore_class );
4992 $skip_frames++; // skip this function
4994 foreach ( $trace as $call ) {
4995 if ( $skip_frames > 0 ) {
4997 } elseif ( isset( $call['class'] ) ) {
4998 if ( $check_class && $ignore_class == $call['class'] )
4999 continue; // Filter out calls
5001 $caller[] = "{$call['class']}{$call['type']}{$call['function']}";
5003 if ( in_array( $call['function'], array( 'do_action', 'apply_filters' ) ) ) {
5004 $caller[] = "{$call['function']}('{$call['args'][0]}')";
5005 } elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) {
5006 $caller[] = $call['function'] . "('" . str_replace( array( WP_CONTENT_DIR, ABSPATH ) , '', $call['args'][0] ) . "')";
5008 $caller[] = $call['function'];
5013 return join( ', ', array_reverse( $caller ) );
5019 * Retrieve ids that are not already present in the cache.
5024 * @param array $object_ids ID list.
5025 * @param string $cache_key The cache bucket to check against.
5027 * @return array List of ids not present in the cache.
5029 function _get_non_cached_ids( $object_ids, $cache_key ) {
5031 foreach ( $object_ids as $id ) {
5033 if ( !wp_cache_get( $id, $cache_key ) ) {
5042 * Test if the current device has the capability to upload files.
5047 * @return bool Whether the device is able to upload files.
5049 function _device_can_upload() {
5050 if ( ! wp_is_mobile() )
5053 $ua = $_SERVER['HTTP_USER_AGENT'];
5055 if ( strpos($ua, 'iPhone') !== false
5056 || strpos($ua, 'iPad') !== false
5057 || strpos($ua, 'iPod') !== false ) {
5058 return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' );
5065 * Test if a given path is a stream URL
5067 * @param string $path The resource path or URL.
5068 * @return bool True if the path is a stream URL.
5070 function wp_is_stream( $path ) {
5071 $wrappers = stream_get_wrappers();
5072 $wrappers_re = '(' . join('|', $wrappers) . ')';
5074 return preg_match( "!^$wrappers_re://!", $path ) === 1;
5078 * Test if the supplied date is valid for the Gregorian calendar.
5084 * @param int $month Month number.
5085 * @param int $day Day number.
5086 * @param int $year Year number.
5087 * @param string $source_date The date to filter.
5088 * @return bool True if valid date, false if not valid date.
5090 function wp_checkdate( $month, $day, $year, $source_date ) {
5092 * Filters whether the given date is valid for the Gregorian calendar.
5096 * @param bool $checkdate Whether the given date is valid.
5097 * @param string $source_date Date to check.
5099 return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
5103 * Load the auth check for monitoring whether the user is still logged in.
5105 * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' );
5107 * This is disabled for certain screens where a login screen could cause an
5108 * inconvenient interruption. A filter called {@see 'wp_auth_check_load'} can be used
5109 * for fine-grained control.
5113 function wp_auth_check_load() {
5114 if ( ! is_admin() && ! is_user_logged_in() )
5117 if ( defined( 'IFRAME_REQUEST' ) )
5120 $screen = get_current_screen();
5121 $hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
5122 $show = ! in_array( $screen->id, $hidden );
5125 * Filters whether to load the authentication check.
5127 * Passing a falsey value to the filter will effectively short-circuit
5128 * loading the authentication check.
5132 * @param bool $show Whether to load the authentication check.
5133 * @param WP_Screen $screen The current screen object.
5135 if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
5136 wp_enqueue_style( 'wp-auth-check' );
5137 wp_enqueue_script( 'wp-auth-check' );
5139 add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
5140 add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
5145 * Output the HTML that shows the wp-login dialog when the user is no longer logged in.
5149 function wp_auth_check_html() {
5150 $login_url = wp_login_url();
5151 $current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
5152 $same_domain = ( strpos( $login_url, $current_domain ) === 0 );
5155 * Filters whether the authentication check originated at the same domain.
5159 * @param bool $same_domain Whether the authentication check originated at the same domain.
5161 $same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
5162 $wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
5165 <div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
5166 <div id="wp-auth-check-bg"></div>
5167 <div id="wp-auth-check">
5168 <button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></button>
5171 if ( $same_domain ) {
5173 <div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( add_query_arg( array( 'interim-login' => 1 ), $login_url ) ); ?>"></div>
5178 <div class="wp-auth-fallback">
5179 <p><b class="wp-auth-fallback-expired" tabindex="0"><?php _e('Session expired'); ?></b></p>
5180 <p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php _e('Please log in again.'); ?></a>
5181 <?php _e('The login page will open in a new window. After logging in you can close it and return to this page.'); ?></p>
5189 * Check whether a user is still logged in, for the heartbeat.
5191 * Send a result that shows a log-in box if the user is no longer logged in,
5192 * or if their cookie is within the grace period.
5196 * @global int $login_grace_period
5198 * @param array $response The Heartbeat response.
5199 * @return array $response The Heartbeat response with 'wp-auth-check' value set.
5201 function wp_auth_check( $response ) {
5202 $response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] );
5207 * Return RegEx body to liberally match an opening HTML tag.
5209 * Matches an opening HTML tag that:
5210 * 1. Is self-closing or
5211 * 2. Has no body but has a closing tag of the same name or
5212 * 3. Contains a body and a closing tag of the same name
5214 * Note: this RegEx does not balance inner tags and does not attempt
5215 * to produce valid HTML
5219 * @param string $tag An HTML tag name. Example: 'video'.
5220 * @return string Tag RegEx.
5222 function get_tag_regex( $tag ) {
5223 if ( empty( $tag ) )
5225 return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
5229 * Retrieve a canonical form of the provided charset appropriate for passing to PHP
5230 * functions such as htmlspecialchars() and charset html attributes.
5235 * @see https://core.trac.wordpress.org/ticket/23688
5237 * @param string $charset A charset name.
5238 * @return string The canonical form of the charset.
5240 function _canonical_charset( $charset ) {
5241 if ( 'UTF-8' === $charset || 'utf-8' === $charset || 'utf8' === $charset ||
5242 'UTF8' === $charset )
5245 if ( 'ISO-8859-1' === $charset || 'iso-8859-1' === $charset ||
5246 'iso8859-1' === $charset || 'ISO8859-1' === $charset )
5247 return 'ISO-8859-1';
5253 * Set the mbstring internal encoding to a binary safe encoding when func_overload
5256 * When mbstring.func_overload is in use for multi-byte encodings, the results from
5257 * strlen() and similar functions respect the utf8 characters, causing binary data
5258 * to return incorrect lengths.
5260 * This function overrides the mbstring encoding to a binary-safe encoding, and
5261 * resets it to the users expected encoding afterwards through the
5262 * `reset_mbstring_encoding` function.
5264 * It is safe to recursively call this function, however each
5265 * `mbstring_binary_safe_encoding()` call must be followed up with an equal number
5266 * of `reset_mbstring_encoding()` calls.
5270 * @see reset_mbstring_encoding()
5272 * @staticvar array $encodings
5273 * @staticvar bool $overloaded
5275 * @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
5278 function mbstring_binary_safe_encoding( $reset = false ) {
5279 static $encodings = array();
5280 static $overloaded = null;
5282 if ( is_null( $overloaded ) )
5283 $overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 );
5285 if ( false === $overloaded )
5289 $encoding = mb_internal_encoding();
5290 array_push( $encodings, $encoding );
5291 mb_internal_encoding( 'ISO-8859-1' );
5294 if ( $reset && $encodings ) {
5295 $encoding = array_pop( $encodings );
5296 mb_internal_encoding( $encoding );
5301 * Reset the mbstring internal encoding to a users previously set encoding.
5303 * @see mbstring_binary_safe_encoding()
5307 function reset_mbstring_encoding() {
5308 mbstring_binary_safe_encoding( true );
5312 * Filter/validate a variable as a boolean.
5314 * Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`.
5318 * @param mixed $var Boolean value to validate.
5319 * @return bool Whether the value is validated.
5321 function wp_validate_boolean( $var ) {
5322 if ( is_bool( $var ) ) {
5326 if ( is_string( $var ) && 'false' === strtolower( $var ) ) {
5338 * @param string $file The path to the file to delete.
5340 function wp_delete_file( $file ) {
5342 * Filters the path of the file to delete.
5346 * @param string $medium Path to the file to delete.
5348 $delete = apply_filters( 'wp_delete_file', $file );
5349 if ( ! empty( $delete ) ) {
5355 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
5357 * This prevents reusing the same tab for a preview when the user has navigated away.
5361 function wp_post_preview_js() {
5364 if ( ! is_preview() || empty( $post ) ) {
5368 // Has to match the window name used in post_submit_meta_box()
5369 $name = 'wp-preview-' . (int) $post->ID;
5374 var query = document.location.search;
5376 if ( query && query.indexOf( 'preview=true' ) !== -1 ) {
5377 window.name = '<?php echo $name; ?>';
5380 if ( window.addEventListener ) {
5381 window.addEventListener( 'unload', function() { window.name = ''; }, false );
5389 * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339.
5391 * Explicitly strips timezones, as datetimes are not saved with any timezone
5392 * information. Including any information on the offset could be misleading.
5396 * @param string $date_string Date string to parse and format.
5397 * @return string Date formatted for ISO8601/RFC3339.
5399 function mysql_to_rfc3339( $date_string ) {
5400 $formatted = mysql2date( 'c', $date_string, false );
5402 // Strip timezone information
5403 return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
5407 * Attempts to raise the PHP memory limit for memory intensive processes.
5409 * Only allows raising the existing limit and prevents lowering it.
5413 * @param string $context Optional. Context in which the function is called. Accepts either 'admin',
5414 * 'image', or an arbitrary other context. If an arbitrary context is passed,
5415 * the similarly arbitrary {@see '{$context}_memory_limit'} filter will be
5416 * invoked. Default 'admin'.
5417 * @return bool|int|string The limit that was set or false on failure.
5419 function wp_raise_memory_limit( $context = 'admin' ) {
5420 // Exit early if the limit cannot be changed.
5421 if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
5425 $current_limit = @ini_get( 'memory_limit' );
5426 $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
5428 if ( -1 === $current_limit_int ) {
5432 $wp_max_limit = WP_MAX_MEMORY_LIMIT;
5433 $wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
5434 $filtered_limit = $wp_max_limit;
5436 switch ( $context ) {
5439 * Filters the maximum memory limit available for administration screens.
5441 * This only applies to administrators, who may require more memory for tasks
5442 * like updates. Memory limits when processing images (uploaded or edited by
5443 * users of any role) are handled separately.
5445 * The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory
5446 * limit available when in the administration back end. The default is 256M
5447 * (256 megabytes of memory) or the original `memory_limit` php.ini value if
5451 * @since 4.6.0 The default now takes the original `memory_limit` into account.
5453 * @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer
5454 * (bytes), or a shorthand string notation, such as '256M'.
5456 $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
5461 * Filters the memory limit allocated for image manipulation.
5464 * @since 4.6.0 The default now takes the original `memory_limit` into account.
5466 * @param int|string $filtered_limit Maximum memory limit to allocate for images.
5467 * Default `WP_MAX_MEMORY_LIMIT` or the original
5468 * php.ini `memory_limit`, whichever is higher.
5469 * Accepts an integer (bytes), or a shorthand string
5470 * notation, such as '256M'.
5472 $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
5477 * Filters the memory limit allocated for arbitrary contexts.
5479 * The dynamic portion of the hook name, `$context`, refers to an arbitrary
5480 * context passed on calling the function. This allows for plugins to define
5481 * their own contexts for raising the memory limit.
5485 * @param int|string $filtered_limit Maximum memory limit to allocate for images.
5486 * Default '256M' or the original php.ini `memory_limit`,
5487 * whichever is higher. Accepts an integer (bytes), or a
5488 * shorthand string notation, such as '256M'.
5490 $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
5494 $filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
5496 if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
5497 if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
5498 return $filtered_limit;
5502 } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
5503 if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
5504 return $wp_max_limit;