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 * Filter 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}\.? \w+#u', $date ) ) {
186 $months = $wp_locale->month;
188 foreach ( $months as $key => $month ) {
189 $months[ $key ] = '#' . $month . '#';
192 $date = preg_replace( $months, $wp_locale->month_genitive, $date );
196 // Used for locale-specific rules
197 $locale = get_locale();
199 if ( 'ca' === $locale ) {
200 // " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
201 $date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
208 * Convert float number to format based on the locale.
212 * @global WP_Locale $wp_locale
214 * @param float $number The number to convert based on locale.
215 * @param int $decimals Optional. Precision of the number of decimal places. Default 0.
216 * @return string Converted number in string format.
218 function number_format_i18n( $number, $decimals = 0 ) {
221 if ( isset( $wp_locale ) ) {
222 $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
224 $formatted = number_format( $number, absint( $decimals ) );
228 * Filter the number formatted based on the locale.
232 * @param string $formatted Converted number in string format.
234 return apply_filters( 'number_format_i18n', $formatted );
238 * Convert number of bytes largest unit bytes will fit into.
240 * It is easier to read 1 kB than 1024 bytes and 1 MB than 1048576 bytes. Converts
241 * number of bytes to human readable number by taking the number of that unit
242 * that the bytes will go into it. Supports TB value.
244 * Please note that integers in PHP are limited to 32 bits, unless they are on
245 * 64 bit architecture, then they have 64 bit size. If you need to place the
246 * larger size then what PHP integer type will hold, then use a string. It will
247 * be converted to a double, which should always have 64 bit length.
249 * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
253 * @param int|string $bytes Number of bytes. Note max integer size for integers.
254 * @param int $decimals Optional. Precision of number of decimal places. Default 0.
255 * @return string|false False on failure. Number string on success.
257 function size_format( $bytes, $decimals = 0 ) {
266 foreach ( $quant as $unit => $mag ) {
267 if ( doubleval( $bytes ) >= $mag ) {
268 return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
276 * Get the week start and end from the datetime or date string from MySQL.
280 * @param string $mysqlstring Date or datetime field type from MySQL.
281 * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
282 * @return array Keys are 'start' and 'end'.
284 function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
285 // MySQL string year.
286 $my = substr( $mysqlstring, 0, 4 );
288 // MySQL string month.
289 $mm = substr( $mysqlstring, 8, 2 );
292 $md = substr( $mysqlstring, 5, 2 );
294 // The timestamp for MySQL string day.
295 $day = mktime( 0, 0, 0, $md, $mm, $my );
297 // The day of the week from the timestamp.
298 $weekday = date( 'w', $day );
300 if ( !is_numeric($start_of_week) )
301 $start_of_week = get_option( 'start_of_week' );
303 if ( $weekday < $start_of_week )
306 // The most recent week start day on or before $day.
307 $start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week );
309 // $start + 1 week - 1 second.
310 $end = $start + WEEK_IN_SECONDS - 1;
311 return compact( 'start', 'end' );
315 * Unserialize value only if it was serialized.
319 * @param string $original Maybe unserialized original, if is needed.
320 * @return mixed Unserialized data can be any type.
322 function maybe_unserialize( $original ) {
323 if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
324 return @unserialize( $original );
329 * Check value to find if it was serialized.
331 * If $data is not an string, then returned value will always be false.
332 * Serialized data is always a string.
336 * @param string $data Value to check to see if was serialized.
337 * @param bool $strict Optional. Whether to be strict about the end of the string. Default true.
338 * @return bool False if not serialized and true if it was.
340 function is_serialized( $data, $strict = true ) {
341 // if it isn't a string, it isn't serialized.
342 if ( ! is_string( $data ) ) {
345 $data = trim( $data );
346 if ( 'N;' == $data ) {
349 if ( strlen( $data ) < 4 ) {
352 if ( ':' !== $data[1] ) {
356 $lastc = substr( $data, -1 );
357 if ( ';' !== $lastc && '}' !== $lastc ) {
361 $semicolon = strpos( $data, ';' );
362 $brace = strpos( $data, '}' );
363 // Either ; or } must exist.
364 if ( false === $semicolon && false === $brace )
366 // But neither must be in the first X characters.
367 if ( false !== $semicolon && $semicolon < 3 )
369 if ( false !== $brace && $brace < 4 )
376 if ( '"' !== substr( $data, -2, 1 ) ) {
379 } elseif ( false === strpos( $data, '"' ) ) {
382 // or else fall through
385 return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
389 $end = $strict ? '$' : '';
390 return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data );
396 * Check whether serialized data is of string type.
400 * @param string $data Serialized data.
401 * @return bool False if not a serialized string, true if it is.
403 function is_serialized_string( $data ) {
404 // if it isn't a string, it isn't a serialized string.
405 if ( ! is_string( $data ) ) {
408 $data = trim( $data );
409 if ( strlen( $data ) < 4 ) {
411 } elseif ( ':' !== $data[1] ) {
413 } elseif ( ';' !== substr( $data, -1 ) ) {
415 } elseif ( $data[0] !== 's' ) {
417 } elseif ( '"' !== substr( $data, -2, 1 ) ) {
425 * Serialize data, if needed.
429 * @param string|array|object $data Data that might be serialized.
430 * @return mixed A scalar data
432 function maybe_serialize( $data ) {
433 if ( is_array( $data ) || is_object( $data ) )
434 return serialize( $data );
436 // Double serialization is required for backward compatibility.
437 // See https://core.trac.wordpress.org/ticket/12930
438 // Also the world will end. See WP 3.6.1.
439 if ( is_serialized( $data, false ) )
440 return serialize( $data );
446 * Retrieve post title from XMLRPC XML.
448 * If the title element is not part of the XML, then the default post title from
449 * the $post_default_title will be used instead.
453 * @global string $post_default_title Default XML-RPC post title.
455 * @param string $content XMLRPC XML Request content
456 * @return string Post title
458 function xmlrpc_getposttitle( $content ) {
459 global $post_default_title;
460 if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
461 $post_title = $matchtitle[1];
463 $post_title = $post_default_title;
469 * Retrieve the post category or categories from XMLRPC XML.
471 * If the category element is not found, then the default post category will be
472 * used. The return type then would be what $post_default_category. If the
473 * category is found, then it will always be an array.
477 * @global string $post_default_category Default XML-RPC post category.
479 * @param string $content XMLRPC XML Request content
480 * @return string|array List of categories or category name.
482 function xmlrpc_getpostcategory( $content ) {
483 global $post_default_category;
484 if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
485 $post_category = trim( $matchcat[1], ',' );
486 $post_category = explode( ',', $post_category );
488 $post_category = $post_default_category;
490 return $post_category;
494 * XMLRPC XML content without title and category elements.
498 * @param string $content XML-RPC XML Request content.
499 * @return string XMLRPC XML Request content without title and category elements.
501 function xmlrpc_removepostdata( $content ) {
502 $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
503 $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
504 $content = trim( $content );
509 * Use RegEx to extract URLs from arbitrary content.
513 * @param string $content Content to extract URLs from.
514 * @return array URLs found in passed string.
516 function wp_extract_urls( $content ) {
519 . "(?:([\w-]+:)?//?)"
525 . "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
534 $post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
536 return array_values( $post_links );
540 * Check content for video and audio links to add as enclosures.
542 * Will not add enclosures that have already been added and will
543 * remove enclosures that are no longer in the post. This is called as
544 * pingbacks and trackbacks.
548 * @global wpdb $wpdb WordPress database abstraction object.
550 * @param string $content Post Content.
551 * @param int $post_ID Post ID.
553 function do_enclose( $content, $post_ID ) {
556 //TODO: Tidy this ghetto code up and make the debug code optional
557 include_once( ABSPATH . WPINC . '/class-IXR.php' );
559 $post_links = array();
561 $pung = get_enclosed( $post_ID );
563 $post_links_temp = wp_extract_urls( $content );
565 foreach ( $pung as $link_test ) {
566 if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
567 $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 ) . '%') );
568 foreach ( $mids as $mid )
569 delete_metadata_by_mid( 'post', $mid );
573 foreach ( (array) $post_links_temp as $link_test ) {
574 if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
575 $test = @parse_url( $link_test );
576 if ( false === $test )
578 if ( isset( $test['query'] ) )
579 $post_links[] = $link_test;
580 elseif ( isset($test['path']) && ( $test['path'] != '/' ) && ($test['path'] != '' ) )
581 $post_links[] = $link_test;
586 * Filter the list of enclosure links before querying the database.
588 * Allows for the addition and/or removal of potential enclosures to save
589 * to postmeta before checking the database for existing enclosures.
593 * @param array $post_links An array of enclosure links.
594 * @param int $post_ID Post ID.
596 $post_links = apply_filters( 'enclosure_links', $post_links, $post_ID );
598 foreach ( (array) $post_links as $url ) {
599 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 ) . '%' ) ) ) {
601 if ( $headers = wp_get_http_headers( $url) ) {
602 $len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
603 $type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
604 $allowed_types = array( 'video', 'audio' );
606 // Check to see if we can figure out the mime type from
608 $url_parts = @parse_url( $url );
609 if ( false !== $url_parts ) {
610 $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
611 if ( !empty( $extension ) ) {
612 foreach ( wp_get_mime_types() as $exts => $mime ) {
613 if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
621 if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
622 add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );
630 * Retrieve HTTP Headers from URL.
634 * @param string $url URL to retrieve HTTP headers from.
635 * @param bool $deprecated Not Used.
636 * @return bool|string False on failure, headers on success.
638 function wp_get_http_headers( $url, $deprecated = false ) {
639 if ( !empty( $deprecated ) )
640 _deprecated_argument( __FUNCTION__, '2.7' );
642 $response = wp_safe_remote_head( $url );
644 if ( is_wp_error( $response ) )
647 return wp_remote_retrieve_headers( $response );
651 * Whether the publish date of the current post in the loop is different from the
652 * publish date of the previous post in the loop.
656 * @global string $currentday The day of the current post in the loop.
657 * @global string $previousday The day of the previous post in the loop.
659 * @return int 1 when new day, 0 if not a new day.
661 function is_new_day() {
662 global $currentday, $previousday;
663 if ( $currentday != $previousday )
670 * Build URL query based on an associative and, or indexed array.
672 * This is a convenient function for easily building url queries. It sets the
673 * separator to '&' and uses _http_build_query() function.
677 * @see _http_build_query() Used to build the query
678 * @link http://us2.php.net/manual/en/function.http-build-query.php for more on what
679 * http_build_query() does.
681 * @param array $data URL-encode key/value pairs.
682 * @return string URL-encoded string.
684 function build_query( $data ) {
685 return _http_build_query( $data, null, '&', '', false );
689 * From php.net (modified by Mark Jaquith to behave like the native PHP5 function).
694 * @see http://us1.php.net/manual/en/function.http-build-query.php
696 * @param array|object $data An array or object of data. Converted to array.
697 * @param string $prefix Optional. Numeric index. If set, start parameter numbering with it.
699 * @param string $sep Optional. Argument separator; defaults to 'arg_separator.output'.
701 * @param string $key Optional. Used to prefix key name. Default empty.
702 * @param bool $urlencode Optional. Whether to use urlencode() in the result. Default true.
704 * @return string The query string.
706 function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
709 foreach ( (array) $data as $k => $v ) {
712 if ( is_int($k) && $prefix != null )
715 $k = $key . '%5B' . $k . '%5D';
718 elseif ( $v === false )
721 if ( is_array($v) || is_object($v) )
722 array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
723 elseif ( $urlencode )
724 array_push($ret, $k.'='.urlencode($v));
726 array_push($ret, $k.'='.$v);
730 $sep = ini_get('arg_separator.output');
732 return implode($sep, $ret);
736 * Retrieves a modified URL query string.
738 * You can rebuild the URL and append query variables to the URL query by using this function.
739 * There are two ways to use this function; either a single key and value, or an associative array.
741 * Using a single key and value:
743 * add_query_arg( 'key', 'value', 'http://example.com' );
745 * Using an associative array:
747 * add_query_arg( array(
748 * 'key1' => 'value1',
749 * 'key2' => 'value2',
750 * ), 'http://example.com' );
752 * Omitting the URL from either use results in the current URL being used
753 * (the value of `$_SERVER['REQUEST_URI']`).
755 * Values are expected to be encoded appropriately with urlencode() or rawurlencode().
757 * Setting any query variable's value to boolean false removes the key (see remove_query_arg()).
759 * Important: The return value of add_query_arg() is not escaped by default. Output should be
760 * late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting
765 * @param string|array $key Either a query variable key, or an associative array of query variables.
766 * @param string $value Optional. Either a query variable value, or a URL to act upon.
767 * @param string $url Optional. A URL to act upon.
768 * @return string New URL query string (unescaped).
770 function add_query_arg() {
771 $args = func_get_args();
772 if ( is_array( $args[0] ) ) {
773 if ( count( $args ) < 2 || false === $args[1] )
774 $uri = $_SERVER['REQUEST_URI'];
778 if ( count( $args ) < 3 || false === $args[2] )
779 $uri = $_SERVER['REQUEST_URI'];
784 if ( $frag = strstr( $uri, '#' ) )
785 $uri = substr( $uri, 0, -strlen( $frag ) );
789 if ( 0 === stripos( $uri, 'http://' ) ) {
790 $protocol = 'http://';
791 $uri = substr( $uri, 7 );
792 } elseif ( 0 === stripos( $uri, 'https://' ) ) {
793 $protocol = 'https://';
794 $uri = substr( $uri, 8 );
799 if ( strpos( $uri, '?' ) !== false ) {
800 list( $base, $query ) = explode( '?', $uri, 2 );
802 } elseif ( $protocol || strpos( $uri, '=' ) === false ) {
810 wp_parse_str( $query, $qs );
811 $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
812 if ( is_array( $args[0] ) ) {
813 foreach ( $args[0] as $k => $v ) {
817 $qs[ $args[0] ] = $args[1];
820 foreach ( $qs as $k => $v ) {
825 $ret = build_query( $qs );
826 $ret = trim( $ret, '?' );
827 $ret = preg_replace( '#=(&|$)#', '$1', $ret );
828 $ret = $protocol . $base . $ret . $frag;
829 $ret = rtrim( $ret, '?' );
834 * Removes an item or items from a query string.
838 * @param string|array $key Query key or keys to remove.
839 * @param bool|string $query Optional. When false uses the current URL. Default false.
840 * @return string New URL query string.
842 function remove_query_arg( $key, $query = false ) {
843 if ( is_array( $key ) ) { // removing multiple keys
844 foreach ( $key as $k )
845 $query = add_query_arg( $k, false, $query );
848 return add_query_arg( $key, false, $query );
852 * Returns an array of single-use query variable names that can be removed from a URL.
856 * @return array An array of parameters to remove from the URL.
858 function wp_removable_query_args() {
859 $removable_query_args = array(
880 'wp-post-new-reload',
884 * Filter the list of query variables to remove.
888 * @param array $removable_query_args An array of query variables to remove from a URL.
890 return apply_filters( 'removable_query_args', $removable_query_args );
894 * Walks the array while sanitizing the contents.
898 * @param array $array Array to walk while sanitizing contents.
899 * @return array Sanitized $array.
901 function add_magic_quotes( $array ) {
902 foreach ( (array) $array as $k => $v ) {
903 if ( is_array( $v ) ) {
904 $array[$k] = add_magic_quotes( $v );
906 $array[$k] = addslashes( $v );
913 * HTTP request for URI to retrieve content.
917 * @see wp_safe_remote_get()
919 * @param string $uri URI/URL of web page to retrieve.
920 * @return false|string HTTP content. False on failure.
922 function wp_remote_fopen( $uri ) {
923 $parsed_url = @parse_url( $uri );
925 if ( !$parsed_url || !is_array( $parsed_url ) )
929 $options['timeout'] = 10;
931 $response = wp_safe_remote_get( $uri, $options );
933 if ( is_wp_error( $response ) )
936 return wp_remote_retrieve_body( $response );
940 * Set up the WordPress query.
944 * @global WP $wp_locale
945 * @global WP_Query $wp_query
946 * @global WP_Query $wp_the_query
948 * @param string|array $query_vars Default WP_Query arguments.
950 function wp( $query_vars = '' ) {
951 global $wp, $wp_query, $wp_the_query;
952 $wp->main( $query_vars );
954 if ( !isset($wp_the_query) )
955 $wp_the_query = $wp_query;
959 * Retrieve the description for the HTTP status.
963 * @global array $wp_header_to_desc
965 * @param int $code HTTP status code.
966 * @return string Empty string if not found, or description if found.
968 function get_status_header_desc( $code ) {
969 global $wp_header_to_desc;
971 $code = absint( $code );
973 if ( !isset( $wp_header_to_desc ) ) {
974 $wp_header_to_desc = array(
976 101 => 'Switching Protocols',
982 203 => 'Non-Authoritative Information',
984 205 => 'Reset Content',
985 206 => 'Partial Content',
986 207 => 'Multi-Status',
989 300 => 'Multiple Choices',
990 301 => 'Moved Permanently',
993 304 => 'Not Modified',
996 307 => 'Temporary Redirect',
997 308 => 'Permanent Redirect',
999 400 => 'Bad Request',
1000 401 => 'Unauthorized',
1001 402 => 'Payment Required',
1004 405 => 'Method Not Allowed',
1005 406 => 'Not Acceptable',
1006 407 => 'Proxy Authentication Required',
1007 408 => 'Request Timeout',
1010 411 => 'Length Required',
1011 412 => 'Precondition Failed',
1012 413 => 'Request Entity Too Large',
1013 414 => 'Request-URI Too Long',
1014 415 => 'Unsupported Media Type',
1015 416 => 'Requested Range Not Satisfiable',
1016 417 => 'Expectation Failed',
1017 418 => 'I\'m a teapot',
1018 421 => 'Misdirected Request',
1019 422 => 'Unprocessable Entity',
1021 424 => 'Failed Dependency',
1022 426 => 'Upgrade Required',
1023 428 => 'Precondition Required',
1024 429 => 'Too Many Requests',
1025 431 => 'Request Header Fields Too Large',
1026 451 => 'Unavailable For Legal Reasons',
1028 500 => 'Internal Server Error',
1029 501 => 'Not Implemented',
1030 502 => 'Bad Gateway',
1031 503 => 'Service Unavailable',
1032 504 => 'Gateway Timeout',
1033 505 => 'HTTP Version Not Supported',
1034 506 => 'Variant Also Negotiates',
1035 507 => 'Insufficient Storage',
1036 510 => 'Not Extended',
1037 511 => 'Network Authentication Required',
1041 if ( isset( $wp_header_to_desc[$code] ) )
1042 return $wp_header_to_desc[$code];
1048 * Set HTTP status header.
1051 * @since 4.4.0 Added the `$description` parameter.
1053 * @see get_status_header_desc()
1055 * @param int $code HTTP status code.
1056 * @param string $description Optional. A custom description for the HTTP status.
1058 function status_header( $code, $description = '' ) {
1059 if ( ! $description ) {
1060 $description = get_status_header_desc( $code );
1063 if ( empty( $description ) ) {
1067 $protocol = wp_get_server_protocol();
1068 $status_header = "$protocol $code $description";
1069 if ( function_exists( 'apply_filters' ) )
1072 * Filter an HTTP status header.
1076 * @param string $status_header HTTP status header.
1077 * @param int $code HTTP status code.
1078 * @param string $description Description for the status code.
1079 * @param string $protocol Server protocol.
1081 $status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol );
1083 @header( $status_header, true, $code );
1087 * Get the header information to prevent caching.
1089 * The several different headers cover the different ways cache prevention
1090 * is handled by different browsers
1094 * @return array The associative array of header names and field values.
1096 function wp_get_nocache_headers() {
1098 'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
1099 'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
1100 'Pragma' => 'no-cache',
1103 if ( function_exists('apply_filters') ) {
1105 * Filter the cache-controlling headers.
1109 * @see wp_get_nocache_headers()
1111 * @param array $headers {
1112 * Header names and field values.
1114 * @type string $Expires Expires header.
1115 * @type string $Cache-Control Cache-Control header.
1116 * @type string $Pragma Pragma header.
1119 $headers = (array) apply_filters( 'nocache_headers', $headers );
1121 $headers['Last-Modified'] = false;
1126 * Set the headers to prevent caching for the different browsers.
1128 * Different browsers support different nocache headers, so several
1129 * headers must be sent so that all of them get the point that no
1130 * caching should occur.
1134 * @see wp_get_nocache_headers()
1136 function nocache_headers() {
1137 $headers = wp_get_nocache_headers();
1139 unset( $headers['Last-Modified'] );
1141 // In PHP 5.3+, make sure we are not sending a Last-Modified header.
1142 if ( function_exists( 'header_remove' ) ) {
1143 @header_remove( 'Last-Modified' );
1145 // In PHP 5.2, send an empty Last-Modified header, but only as a
1146 // last resort to override a header already sent. #WP23021
1147 foreach ( headers_list() as $header ) {
1148 if ( 0 === stripos( $header, 'Last-Modified' ) ) {
1149 $headers['Last-Modified'] = '';
1155 foreach ( $headers as $name => $field_value )
1156 @header("{$name}: {$field_value}");
1160 * Set the headers for caching for 10 days with JavaScript content type.
1164 function cache_javascript_headers() {
1165 $expiresOffset = 10 * DAY_IN_SECONDS;
1167 header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
1168 header( "Vary: Accept-Encoding" ); // Handle proxies
1169 header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
1173 * Retrieve the number of database queries during the WordPress execution.
1177 * @global wpdb $wpdb WordPress database abstraction object.
1179 * @return int Number of database queries.
1181 function get_num_queries() {
1183 return $wpdb->num_queries;
1187 * Whether input is yes or no.
1189 * Must be 'y' to be true.
1193 * @param string $yn Character string containing either 'y' (yes) or 'n' (no).
1194 * @return bool True if yes, false on anything else.
1196 function bool_from_yn( $yn ) {
1197 return ( strtolower( $yn ) == 'y' );
1201 * Load the feed template from the use of an action hook.
1203 * If the feed action does not have a hook, then the function will die with a
1204 * message telling the visitor that the feed is not valid.
1206 * It is better to only have one hook for each feed.
1210 * @global WP_Query $wp_query Used to tell if the use a comment feed.
1212 function do_feed() {
1215 $feed = get_query_var( 'feed' );
1217 // Remove the pad, if present.
1218 $feed = preg_replace( '/^_+/', '', $feed );
1220 if ( $feed == '' || $feed == 'feed' )
1221 $feed = get_default_feed();
1223 if ( ! has_action( "do_feed_{$feed}" ) ) {
1224 wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) );
1228 * Fires once the given feed is loaded.
1230 * The dynamic portion of the hook name, `$feed`, refers to the feed template name.
1231 * Possible values include: 'rdf', 'rss', 'rss2', and 'atom'.
1234 * @since 4.4.0 The `$feed` parameter was added.
1236 * @param bool $is_comment_feed Whether the feed is a comment feed.
1237 * @param string $feed The feed name.
1239 do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed );
1243 * Load the RDF RSS 0.91 Feed template.
1247 * @see load_template()
1249 function do_feed_rdf() {
1250 load_template( ABSPATH . WPINC . '/feed-rdf.php' );
1254 * Load the RSS 1.0 Feed Template.
1258 * @see load_template()
1260 function do_feed_rss() {
1261 load_template( ABSPATH . WPINC . '/feed-rss.php' );
1265 * Load either the RSS2 comment feed or the RSS2 posts feed.
1269 * @see load_template()
1271 * @param bool $for_comments True for the comment feed, false for normal feed.
1273 function do_feed_rss2( $for_comments ) {
1274 if ( $for_comments )
1275 load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
1277 load_template( ABSPATH . WPINC . '/feed-rss2.php' );
1281 * Load either Atom comment feed or Atom posts feed.
1285 * @see load_template()
1287 * @param bool $for_comments True for the comment feed, false for normal feed.
1289 function do_feed_atom( $for_comments ) {
1291 load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
1293 load_template( ABSPATH . WPINC . '/feed-atom.php' );
1297 * Display the robots.txt file content.
1299 * The echo content should be with usage of the permalinks or for creating the
1304 function do_robots() {
1305 header( 'Content-Type: text/plain; charset=utf-8' );
1308 * Fires when displaying the robots.txt file.
1312 do_action( 'do_robotstxt' );
1314 $output = "User-agent: *\n";
1315 $public = get_option( 'blog_public' );
1316 if ( '0' == $public ) {
1317 $output .= "Disallow: /\n";
1319 $site_url = parse_url( site_url() );
1320 $path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
1321 $output .= "Disallow: $path/wp-admin/\n";
1322 $output .= "Allow: $path/wp-admin/admin-ajax.php\n";
1326 * Filter the robots.txt output.
1330 * @param string $output Robots.txt output.
1331 * @param bool $public Whether the site is considered "public".
1333 echo apply_filters( 'robots_txt', $output, $public );
1337 * Test whether WordPress is already installed.
1339 * The cache will be checked first. If you have a cache plugin, which saves
1340 * the cache values, then this will work. If you use the default WordPress
1341 * cache, and the database goes away, then you might have problems.
1343 * Checks for the 'siteurl' option for whether WordPress is installed.
1347 * @global wpdb $wpdb WordPress database abstraction object.
1349 * @return bool Whether the site is already installed.
1351 function is_blog_installed() {
1355 * Check cache first. If options table goes away and we have true
1358 if ( wp_cache_get( 'is_blog_installed' ) )
1361 $suppress = $wpdb->suppress_errors();
1362 if ( ! wp_installing() ) {
1363 $alloptions = wp_load_alloptions();
1365 // If siteurl is not set to autoload, check it specifically
1366 if ( !isset( $alloptions['siteurl'] ) )
1367 $installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
1369 $installed = $alloptions['siteurl'];
1370 $wpdb->suppress_errors( $suppress );
1372 $installed = !empty( $installed );
1373 wp_cache_set( 'is_blog_installed', $installed );
1378 // If visiting repair.php, return true and let it take over.
1379 if ( defined( 'WP_REPAIRING' ) )
1382 $suppress = $wpdb->suppress_errors();
1385 * Loop over the WP tables. If none exist, then scratch install is allowed.
1386 * If one or more exist, suggest table repair since we got here because the
1387 * options table could not be accessed.
1389 $wp_tables = $wpdb->tables();
1390 foreach ( $wp_tables as $table ) {
1391 // The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
1392 if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
1394 if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table )
1397 if ( ! $wpdb->get_results( "DESCRIBE $table;" ) )
1400 // One or more tables exist. We are insane.
1402 wp_load_translations_early();
1404 // Die with a DB error.
1405 $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' );
1409 $wpdb->suppress_errors( $suppress );
1411 wp_cache_set( 'is_blog_installed', false );
1417 * Retrieve URL with nonce added to URL query.
1421 * @param string $actionurl URL to add nonce action.
1422 * @param int|string $action Optional. Nonce action name. Default -1.
1423 * @param string $name Optional. Nonce name. Default '_wpnonce'.
1424 * @return string Escaped URL with nonce action added.
1426 function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
1427 $actionurl = str_replace( '&', '&', $actionurl );
1428 return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
1432 * Retrieve or display nonce hidden field for forms.
1434 * The nonce field is used to validate that the contents of the form came from
1435 * the location on the current site and not somewhere else. The nonce does not
1436 * offer absolute protection, but should protect against most cases. It is very
1437 * important to use nonce field in forms.
1439 * The $action and $name are optional, but if you want to have better security,
1440 * it is strongly suggested to set those two parameters. It is easier to just
1441 * call the function without any parameters, because validation of the nonce
1442 * doesn't require any parameters, but since crackers know what the default is
1443 * it won't be difficult for them to find a way around your nonce and cause
1446 * The input name will be whatever $name value you gave. The input value will be
1447 * the nonce creation value.
1451 * @param int|string $action Optional. Action name. Default -1.
1452 * @param string $name Optional. Nonce name. Default '_wpnonce'.
1453 * @param bool $referer Optional. Whether to set the referer field for validation. Default true.
1454 * @param bool $echo Optional. Whether to display or return hidden form field. Default true.
1455 * @return string Nonce field HTML markup.
1457 function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
1458 $name = esc_attr( $name );
1459 $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
1462 $nonce_field .= wp_referer_field( false );
1467 return $nonce_field;
1471 * Retrieve or display referer hidden field for forms.
1473 * The referer link is the current Request URI from the server super global. The
1474 * input name is '_wp_http_referer', in case you wanted to check manually.
1478 * @param bool $echo Optional. Whether to echo or return the referer field. Default true.
1479 * @return string Referer field HTML markup.
1481 function wp_referer_field( $echo = true ) {
1482 $referer_field = '<input type="hidden" name="_wp_http_referer" value="'. esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />';
1485 echo $referer_field;
1486 return $referer_field;
1490 * Retrieve or display original referer hidden field for forms.
1492 * The input name is '_wp_original_http_referer' and will be either the same
1493 * value of wp_referer_field(), if that was posted already or it will be the
1494 * current page, if it doesn't exist.
1498 * @param bool $echo Optional. Whether to echo the original http referer. Default true.
1499 * @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to.
1500 * Default 'current'.
1501 * @return string Original referer field.
1503 function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
1504 if ( ! $ref = wp_get_original_referer() ) {
1505 $ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] );
1507 $orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( $ref ) . '" />';
1509 echo $orig_referer_field;
1510 return $orig_referer_field;
1514 * Retrieve referer from '_wp_http_referer' or HTTP referer.
1516 * If it's the same as the current request URL, will return false.
1520 * @return false|string False on failure. Referer URL on success.
1522 function wp_get_referer() {
1523 if ( ! function_exists( 'wp_validate_redirect' ) ) {
1527 $ref = wp_get_raw_referer();
1529 if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) && $ref !== home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) ) {
1530 return wp_validate_redirect( $ref, false );
1537 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
1539 * Do not use for redirects, use {@see wp_get_referer()} instead.
1543 * @return string|false Referer URL on success, false on failure.
1545 function wp_get_raw_referer() {
1546 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
1547 return wp_unslash( $_REQUEST['_wp_http_referer'] );
1548 } else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
1549 return wp_unslash( $_SERVER['HTTP_REFERER'] );
1556 * Retrieve original referer that was posted, if it exists.
1560 * @return string|false False if no original referer or original referer if set.
1562 function wp_get_original_referer() {
1563 if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) )
1564 return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
1569 * Recursive directory creation based on full path.
1571 * Will attempt to set permissions on folders.
1575 * @param string $target Full path to attempt to create.
1576 * @return bool Whether the path was created. True if path already exists.
1578 function wp_mkdir_p( $target ) {
1581 // Strip the protocol.
1582 if ( wp_is_stream( $target ) ) {
1583 list( $wrapper, $target ) = explode( '://', $target, 2 );
1586 // From php.net/mkdir user contributed notes.
1587 $target = str_replace( '//', '/', $target );
1589 // Put the wrapper back on the target.
1590 if ( $wrapper !== null ) {
1591 $target = $wrapper . '://' . $target;
1595 * Safe mode fails with a trailing slash under certain PHP versions.
1596 * Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
1598 $target = rtrim($target, '/');
1599 if ( empty($target) )
1602 if ( file_exists( $target ) )
1603 return @is_dir( $target );
1605 // We need to find the permissions of the parent folder that exists and inherit that.
1606 $target_parent = dirname( $target );
1607 while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
1608 $target_parent = dirname( $target_parent );
1611 // Get the permission bits.
1612 if ( $stat = @stat( $target_parent ) ) {
1613 $dir_perms = $stat['mode'] & 0007777;
1618 if ( @mkdir( $target, $dir_perms, true ) ) {
1621 * If a umask is set that modifies $dir_perms, we'll have to re-set
1622 * the $dir_perms correctly with chmod()
1624 if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
1625 $folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
1626 for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) {
1627 @chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
1638 * Test if a give filesystem path is absolute.
1640 * For example, '/foo/bar', or 'c:\windows'.
1644 * @param string $path File path.
1645 * @return bool True if path is absolute, false is not absolute.
1647 function path_is_absolute( $path ) {
1649 * This is definitive if true but fails if $path does not exist or contains
1652 if ( realpath($path) == $path )
1655 if ( strlen($path) == 0 || $path[0] == '.' )
1658 // Windows allows absolute paths like this.
1659 if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
1662 // A path starting with / or \ is absolute; anything else is relative.
1663 return ( $path[0] == '/' || $path[0] == '\\' );
1667 * Join two filesystem paths together.
1669 * For example, 'give me $path relative to $base'. If the $path is absolute,
1670 * then it the full path is returned.
1674 * @param string $base Base path.
1675 * @param string $path Path relative to $base.
1676 * @return string The path with the base or absolute path.
1678 function path_join( $base, $path ) {
1679 if ( path_is_absolute($path) )
1682 return rtrim($base, '/') . '/' . ltrim($path, '/');
1686 * Normalize a filesystem path.
1688 * On windows systems, replaces backslashes with forward slashes
1689 * and forces upper-case drive letters.
1690 * Allows for two leading slashes for Windows network shares, but
1691 * ensures that all other duplicate slashes are reduced to a single.
1694 * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
1695 * @since 4.5.0 Allows for Windows network shares.
1697 * @param string $path Path to normalize.
1698 * @return string Normalized path.
1700 function wp_normalize_path( $path ) {
1701 $path = str_replace( '\\', '/', $path );
1702 $path = preg_replace( '|(?<=.)/+|', '/', $path );
1703 if ( ':' === substr( $path, 1, 1 ) ) {
1704 $path = ucfirst( $path );
1710 * Determine a writable directory for temporary files.
1712 * Function's preference is the return value of sys_get_temp_dir(),
1713 * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
1714 * before finally defaulting to /tmp/
1716 * In the event that this function does not find a writable location,
1717 * It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.
1721 * @staticvar string $temp
1723 * @return string Writable temporary directory.
1725 function get_temp_dir() {
1727 if ( defined('WP_TEMP_DIR') )
1728 return trailingslashit(WP_TEMP_DIR);
1731 return trailingslashit( $temp );
1733 if ( function_exists('sys_get_temp_dir') ) {
1734 $temp = sys_get_temp_dir();
1735 if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1736 return trailingslashit( $temp );
1739 $temp = ini_get('upload_tmp_dir');
1740 if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1741 return trailingslashit( $temp );
1743 $temp = WP_CONTENT_DIR . '/';
1744 if ( is_dir( $temp ) && wp_is_writable( $temp ) )
1751 * Determine if a directory is writable.
1753 * This function is used to work around certain ACL issues in PHP primarily
1754 * affecting Windows Servers.
1758 * @see win_is_writable()
1760 * @param string $path Path to check for write-ability.
1761 * @return bool Whether the path is writable.
1763 function wp_is_writable( $path ) {
1764 if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) )
1765 return win_is_writable( $path );
1767 return @is_writable( $path );
1771 * Workaround for Windows bug in is_writable() function
1773 * PHP has issues with Windows ACL's for determine if a
1774 * directory is writable or not, this works around them by
1775 * checking the ability to open files rather than relying
1776 * upon PHP to interprate the OS ACL.
1780 * @see http://bugs.php.net/bug.php?id=27609
1781 * @see http://bugs.php.net/bug.php?id=30931
1783 * @param string $path Windows path to check for write-ability.
1784 * @return bool Whether the path is writable.
1786 function win_is_writable( $path ) {
1788 if ( $path[strlen( $path ) - 1] == '/' ) { // if it looks like a directory, check a random file within the directory
1789 return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
1790 } elseif ( is_dir( $path ) ) { // If it's a directory (and not a file) check a random file within the directory
1791 return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
1793 // check tmp file for read/write capabilities
1794 $should_delete_tmp_file = !file_exists( $path );
1795 $f = @fopen( $path, 'a' );
1799 if ( $should_delete_tmp_file )
1805 * Retrieves uploads directory information.
1807 * Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory.
1808 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases
1809 * when not uploading files.
1813 * @see wp_upload_dir()
1815 * @return array See wp_upload_dir() for description.
1817 function wp_get_upload_dir() {
1818 return wp_upload_dir( null, false );
1822 * Get an array containing the current upload directory's path and url.
1824 * Checks the 'upload_path' option, which should be from the web root folder,
1825 * and if it isn't empty it will be used. If it is empty, then the path will be
1826 * 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will
1827 * override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.
1829 * The upload URL path is set either by the 'upload_url_path' option or by using
1830 * the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.
1832 * If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in
1833 * the administration settings panel), then the time will be used. The format
1834 * will be year first and then month.
1836 * If the path couldn't be created, then an error will be returned with the key
1837 * 'error' containing the error message. The error suggests that the parent
1838 * directory is not writable by the server.
1840 * On success, the returned array will have many indices:
1841 * 'path' - base directory and sub directory or full path to upload directory.
1842 * 'url' - base url and sub directory or absolute URL to upload directory.
1843 * 'subdir' - sub directory if uploads use year/month folders option is on.
1844 * 'basedir' - path without subdir.
1845 * 'baseurl' - URL path without subdir.
1846 * 'error' - false or error message.
1849 * @uses _wp_upload_dir()
1851 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1852 * @param bool $create_dir Optional. Whether to check and create the uploads directory. Default true (backwards compatible).
1853 * @param bool $refresh_cache Optional. Whether to refresh the cache. Default false.
1854 * @return array See above for description.
1856 function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
1857 static $cache = array(), $tested_paths = array();
1859 $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
1861 if ( $refresh_cache || empty( $cache[ $key ] ) ) {
1862 $cache[ $key ] = _wp_upload_dir( $time );
1866 * Filter the uploads directory data.
1870 * @param array $uploads Array of upload directory data with keys of 'path',
1871 * 'url', 'subdir, 'basedir', and 'error'.
1873 $uploads = apply_filters( 'upload_dir', $cache[ $key ] );
1875 if ( $create_dir ) {
1876 $path = $uploads['path'];
1878 if ( array_key_exists( $path, $tested_paths ) ) {
1879 $uploads['error'] = $tested_paths[ $path ];
1881 if ( ! wp_mkdir_p( $path ) ) {
1882 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
1883 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
1885 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
1888 $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), esc_html( $error_path ) );
1891 $tested_paths[ $path ] = $uploads['error'];
1899 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
1903 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1904 * @return array See wp_upload_dir()
1906 function _wp_upload_dir( $time = null ) {
1907 $siteurl = get_option( 'siteurl' );
1908 $upload_path = trim( get_option( 'upload_path' ) );
1910 if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
1911 $dir = WP_CONTENT_DIR . '/uploads';
1912 } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
1913 // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
1914 $dir = path_join( ABSPATH, $upload_path );
1916 $dir = $upload_path;
1919 if ( !$url = get_option( 'upload_url_path' ) ) {
1920 if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
1921 $url = WP_CONTENT_URL . '/uploads';
1923 $url = trailingslashit( $siteurl ) . $upload_path;
1927 * Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
1928 * We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
1930 if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
1931 $dir = ABSPATH . UPLOADS;
1932 $url = trailingslashit( $siteurl ) . UPLOADS;
1935 // If multisite (and if not the main site in a post-MU network)
1936 if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
1938 if ( ! get_site_option( 'ms_files_rewriting' ) ) {
1940 * If ms-files rewriting is disabled (networks created post-3.5), it is fairly
1941 * straightforward: Append sites/%d if we're not on the main site (for post-MU
1942 * networks). (The extra directory prevents a four-digit ID from conflicting with
1943 * a year-based directory for the main site. But if a MU-era network has disabled
1944 * ms-files rewriting manually, they don't need the extra directory, as they never
1945 * had wp-content/uploads for the main site.)
1948 if ( defined( 'MULTISITE' ) )
1949 $ms_dir = '/sites/' . get_current_blog_id();
1951 $ms_dir = '/' . get_current_blog_id();
1956 } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
1958 * Handle the old-form ms-files.php rewriting if the network still has that enabled.
1959 * When ms-files rewriting is enabled, then we only listen to UPLOADS when:
1960 * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used
1962 * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect
1963 * the original blog ID.
1965 * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
1966 * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
1967 * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
1968 * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
1971 if ( defined( 'BLOGUPLOADDIR' ) )
1972 $dir = untrailingslashit( BLOGUPLOADDIR );
1974 $dir = ABSPATH . UPLOADS;
1975 $url = trailingslashit( $siteurl ) . 'files';
1983 if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
1984 // Generate the yearly and monthly dirs
1986 $time = current_time( 'mysql' );
1987 $y = substr( $time, 0, 4 );
1988 $m = substr( $time, 5, 2 );
1998 'subdir' => $subdir,
1999 'basedir' => $basedir,
2000 'baseurl' => $baseurl,
2006 * Get a filename that is sanitized and unique for the given directory.
2008 * If the filename is not unique, then a number will be added to the filename
2009 * before the extension, and will continue adding numbers until the filename is
2012 * The callback is passed three parameters, the first one is the directory, the
2013 * second is the filename, and the third is the extension.
2017 * @param string $dir Directory.
2018 * @param string $filename File name.
2019 * @param callable $unique_filename_callback Callback. Default null.
2020 * @return string New filename, if given wasn't unique.
2022 function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
2023 // Sanitize the file name before we begin processing.
2024 $filename = sanitize_file_name($filename);
2026 // Separate the filename into a name and extension.
2027 $info = pathinfo($filename);
2028 $ext = !empty($info['extension']) ? '.' . $info['extension'] : '';
2029 $name = basename($filename, $ext);
2031 // Edge case: if file is named '.ext', treat as an empty name.
2032 if ( $name === $ext )
2036 * Increment the file number until we have a unique file to save in $dir.
2037 * Use callback if supplied.
2039 if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
2040 $filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
2044 // Change '.ext' to lower case.
2045 if ( $ext && strtolower($ext) != $ext ) {
2046 $ext2 = strtolower($ext);
2047 $filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
2049 // Check for both lower and upper case extension or image sub-sizes may be overwritten.
2050 while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
2051 $new_number = $number + 1;
2052 $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename );
2053 $filename2 = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 );
2054 $number = $new_number;
2058 * Filter the result when generating a unique file name.
2062 * @param string $filename Unique file name.
2063 * @param string $ext File extension, eg. ".png".
2064 * @param string $dir Directory path.
2065 * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
2067 return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback );
2070 while ( file_exists( $dir . "/$filename" ) ) {
2071 if ( '' == "$number$ext" ) {
2072 $filename = "$filename-" . ++$number;
2074 $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-" . ++$number . $ext, $filename );
2079 /** This filter is documented in wp-includes/functions.php */
2080 return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback );
2084 * Create a file in the upload folder with given content.
2086 * If there is an error, then the key 'error' will exist with the error message.
2087 * If success, then the key 'file' will have the unique file path, the 'url' key
2088 * will have the link to the new file. and the 'error' key will be set to false.
2090 * This function will not move an uploaded file to the upload folder. It will
2091 * create a new file with the content in $bits parameter. If you move the upload
2092 * file, read the content of the uploaded file, and then you can give the
2093 * filename and content to this function, which will add it to the upload
2096 * The permissions will be set on the new file automatically by this function.
2100 * @param string $name Filename.
2101 * @param null|string $deprecated Never used. Set to null.
2102 * @param mixed $bits File content
2103 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
2106 function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
2107 if ( !empty( $deprecated ) )
2108 _deprecated_argument( __FUNCTION__, '2.0' );
2110 if ( empty( $name ) )
2111 return array( 'error' => __( 'Empty filename' ) );
2113 $wp_filetype = wp_check_filetype( $name );
2114 if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) )
2115 return array( 'error' => __( 'Invalid file type' ) );
2117 $upload = wp_upload_dir( $time );
2119 if ( $upload['error'] !== false )
2123 * Filter whether to treat the upload bits as an error.
2125 * Passing a non-array to the filter will effectively short-circuit preparing
2126 * the upload bits, returning that value instead.
2130 * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
2132 $upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );
2133 if ( !is_array( $upload_bits_error ) ) {
2134 $upload[ 'error' ] = $upload_bits_error;
2138 $filename = wp_unique_filename( $upload['path'], $name );
2140 $new_file = $upload['path'] . "/$filename";
2141 if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
2142 if ( 0 === strpos( $upload['basedir'], ABSPATH ) )
2143 $error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];
2145 $error_path = basename( $upload['basedir'] ) . $upload['subdir'];
2147 $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );
2148 return array( 'error' => $message );
2151 $ifp = @ fopen( $new_file, 'wb' );
2153 return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
2155 @fwrite( $ifp, $bits );
2159 // Set correct file permissions
2160 $stat = @ stat( dirname( $new_file ) );
2161 $perms = $stat['mode'] & 0007777;
2162 $perms = $perms & 0000666;
2163 @ chmod( $new_file, $perms );
2167 $url = $upload['url'] . "/$filename";
2169 /** This filter is documented in wp-admin/includes/file.php */
2170 return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false ), 'sideload' );
2174 * Retrieve the file type based on the extension name.
2178 * @param string $ext The extension to search.
2179 * @return string|void The file type, example: audio, video, document, spreadsheet, etc.
2181 function wp_ext2type( $ext ) {
2182 $ext = strtolower( $ext );
2185 * Filter file type based on the extension name.
2189 * @see wp_ext2type()
2191 * @param array $ext2type Multi-dimensional array with extensions for a default set
2194 $ext2type = apply_filters( 'ext2type', array(
2195 'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ),
2196 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
2197 'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ),
2198 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ),
2199 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ),
2200 'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
2201 'text' => array( 'asc', 'csv', 'tsv', 'txt' ),
2202 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ),
2203 'code' => array( 'css', 'htm', 'html', 'php', 'js' ),
2206 foreach ( $ext2type as $type => $exts )
2207 if ( in_array( $ext, $exts ) )
2212 * Retrieve the file type from the file name.
2214 * You can optionally define the mime array, if needed.
2218 * @param string $filename File name or path.
2219 * @param array $mimes Optional. Key is the file extension with value as the mime type.
2220 * @return array Values with extension first and mime type.
2222 function wp_check_filetype( $filename, $mimes = null ) {
2223 if ( empty($mimes) )
2224 $mimes = get_allowed_mime_types();
2228 foreach ( $mimes as $ext_preg => $mime_match ) {
2229 $ext_preg = '!\.(' . $ext_preg . ')$!i';
2230 if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
2231 $type = $mime_match;
2232 $ext = $ext_matches[1];
2237 return compact( 'ext', 'type' );
2241 * Attempt to determine the real file type of a file.
2243 * If unable to, the file name extension will be used to determine type.
2245 * If it's determined that the extension does not match the file's real type,
2246 * then the "proper_filename" value will be set with a proper filename and extension.
2248 * Currently this function only supports validating images known to getimagesize().
2252 * @param string $file Full path to the file.
2253 * @param string $filename The name of the file (may differ from $file due to $file being
2254 * in a tmp directory).
2255 * @param array $mimes Optional. Key is the file extension with value as the mime type.
2256 * @return array Values for the extension, MIME, and either a corrected filename or false
2257 * if original $filename is valid.
2259 function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
2260 $proper_filename = false;
2262 // Do basic extension validation and MIME mapping
2263 $wp_filetype = wp_check_filetype( $filename, $mimes );
2264 $ext = $wp_filetype['ext'];
2265 $type = $wp_filetype['type'];
2267 // We can't do any further validation without a file to work with
2268 if ( ! file_exists( $file ) ) {
2269 return compact( 'ext', 'type', 'proper_filename' );
2272 // We're able to validate images using GD
2273 if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
2275 // Attempt to figure out what type of image it actually is
2276 $imgstats = @getimagesize( $file );
2278 // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
2279 if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
2281 * Filter the list mapping image mime types to their respective extensions.
2285 * @param array $mime_to_ext Array of image mime types and their matching extensions.
2287 $mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
2288 'image/jpeg' => 'jpg',
2289 'image/png' => 'png',
2290 'image/gif' => 'gif',
2291 'image/bmp' => 'bmp',
2292 'image/tiff' => 'tif',
2295 // Replace whatever is after the last period in the filename with the correct extension
2296 if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) {
2297 $filename_parts = explode( '.', $filename );
2298 array_pop( $filename_parts );
2299 $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
2300 $new_filename = implode( '.', $filename_parts );
2302 if ( $new_filename != $filename ) {
2303 $proper_filename = $new_filename; // Mark that it changed
2305 // Redefine the extension / MIME
2306 $wp_filetype = wp_check_filetype( $new_filename, $mimes );
2307 $ext = $wp_filetype['ext'];
2308 $type = $wp_filetype['type'];
2314 * Filter the "real" file type of the given file.
2318 * @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
2319 * 'proper_filename' keys.
2320 * @param string $file Full path to the file.
2321 * @param string $filename The name of the file (may differ from $file due to
2322 * $file being in a tmp directory).
2323 * @param array $mimes Key is the file extension with value as the mime type.
2325 return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
2329 * Retrieve list of mime types and file extensions.
2332 * @since 4.2.0 Support was added for GIMP (xcf) files.
2334 * @return array Array of mime types keyed by the file extension regex corresponding to those types.
2336 function wp_get_mime_types() {
2338 * Filter the list of mime types and file extensions.
2340 * This filter should be used to add, not remove, mime types. To remove
2341 * mime types, use the 'upload_mimes' filter.
2345 * @param array $wp_get_mime_types Mime types keyed by the file extension regex
2346 * corresponding to those types.
2348 return apply_filters( 'mime_types', array(
2350 'jpg|jpeg|jpe' => 'image/jpeg',
2351 'gif' => 'image/gif',
2352 'png' => 'image/png',
2353 'bmp' => 'image/bmp',
2354 'tiff|tif' => 'image/tiff',
2355 'ico' => 'image/x-icon',
2357 'asf|asx' => 'video/x-ms-asf',
2358 'wmv' => 'video/x-ms-wmv',
2359 'wmx' => 'video/x-ms-wmx',
2360 'wm' => 'video/x-ms-wm',
2361 'avi' => 'video/avi',
2362 'divx' => 'video/divx',
2363 'flv' => 'video/x-flv',
2364 'mov|qt' => 'video/quicktime',
2365 'mpeg|mpg|mpe' => 'video/mpeg',
2366 'mp4|m4v' => 'video/mp4',
2367 'ogv' => 'video/ogg',
2368 'webm' => 'video/webm',
2369 'mkv' => 'video/x-matroska',
2370 '3gp|3gpp' => 'video/3gpp', // Can also be audio
2371 '3g2|3gp2' => 'video/3gpp2', // Can also be audio
2373 'txt|asc|c|cc|h|srt' => 'text/plain',
2374 'csv' => 'text/csv',
2375 'tsv' => 'text/tab-separated-values',
2376 'ics' => 'text/calendar',
2377 'rtx' => 'text/richtext',
2378 'css' => 'text/css',
2379 'htm|html' => 'text/html',
2380 'vtt' => 'text/vtt',
2381 'dfxp' => 'application/ttaf+xml',
2383 'mp3|m4a|m4b' => 'audio/mpeg',
2384 'ra|ram' => 'audio/x-realaudio',
2385 'wav' => 'audio/wav',
2386 'ogg|oga' => 'audio/ogg',
2387 'mid|midi' => 'audio/midi',
2388 'wma' => 'audio/x-ms-wma',
2389 'wax' => 'audio/x-ms-wax',
2390 'mka' => 'audio/x-matroska',
2391 // Misc application formats.
2392 'rtf' => 'application/rtf',
2393 'js' => 'application/javascript',
2394 'pdf' => 'application/pdf',
2395 'swf' => 'application/x-shockwave-flash',
2396 'class' => 'application/java',
2397 'tar' => 'application/x-tar',
2398 'zip' => 'application/zip',
2399 'gz|gzip' => 'application/x-gzip',
2400 'rar' => 'application/rar',
2401 '7z' => 'application/x-7z-compressed',
2402 'exe' => 'application/x-msdownload',
2403 'psd' => 'application/octet-stream',
2404 'xcf' => 'application/octet-stream',
2405 // MS Office formats.
2406 'doc' => 'application/msword',
2407 'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
2408 'wri' => 'application/vnd.ms-write',
2409 'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
2410 'mdb' => 'application/vnd.ms-access',
2411 'mpp' => 'application/vnd.ms-project',
2412 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
2413 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
2414 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
2415 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
2416 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
2417 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
2418 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
2419 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
2420 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
2421 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
2422 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
2423 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
2424 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
2425 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
2426 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
2427 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
2428 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
2429 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
2430 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
2431 'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
2432 'oxps' => 'application/oxps',
2433 'xps' => 'application/vnd.ms-xpsdocument',
2434 // OpenOffice formats.
2435 'odt' => 'application/vnd.oasis.opendocument.text',
2436 'odp' => 'application/vnd.oasis.opendocument.presentation',
2437 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
2438 'odg' => 'application/vnd.oasis.opendocument.graphics',
2439 'odc' => 'application/vnd.oasis.opendocument.chart',
2440 'odb' => 'application/vnd.oasis.opendocument.database',
2441 'odf' => 'application/vnd.oasis.opendocument.formula',
2442 // WordPerfect formats.
2443 'wp|wpd' => 'application/wordperfect',
2445 'key' => 'application/vnd.apple.keynote',
2446 'numbers' => 'application/vnd.apple.numbers',
2447 'pages' => 'application/vnd.apple.pages',
2451 * Retrieve list of allowed mime types and file extensions.
2455 * @param int|WP_User $user Optional. User to check. Defaults to current user.
2456 * @return array Array of mime types keyed by the file extension regex corresponding
2459 function get_allowed_mime_types( $user = null ) {
2460 $t = wp_get_mime_types();
2462 unset( $t['swf'], $t['exe'] );
2463 if ( function_exists( 'current_user_can' ) )
2464 $unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
2466 if ( empty( $unfiltered ) )
2467 unset( $t['htm|html'] );
2470 * Filter list of allowed mime types and file extensions.
2474 * @param array $t Mime types keyed by the file extension regex corresponding to
2475 * those types. 'swf' and 'exe' removed from full list. 'htm|html' also
2476 * removed depending on '$user' capabilities.
2477 * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user).
2479 return apply_filters( 'upload_mimes', $t, $user );
2483 * Display "Are You Sure" message to confirm the action being taken.
2485 * If the action has the nonce explain message, then it will be displayed
2486 * along with the "Are you sure?" message.
2490 * @param string $action The nonce action.
2492 function wp_nonce_ays( $action ) {
2493 if ( 'log-out' == $action ) {
2494 $html = sprintf( __( 'You are attempting to log out of %s' ), get_bloginfo( 'name' ) ) . '</p><p>';
2495 $redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
2496 $html .= sprintf( __( "Do you really want to <a href='%s'>log out</a>?"), wp_logout_url( $redirect_to ) );
2498 $html = __( 'Are you sure you want to do this?' );
2499 if ( wp_get_referer() )
2500 $html .= "</p><p><a href='" . esc_url( remove_query_arg( 'updated', wp_get_referer() ) ) . "'>" . __( 'Please try again.' ) . "</a>";
2503 wp_die( $html, __( 'WordPress Failure Notice' ), 403 );
2507 * Kill WordPress execution and display HTML message with error message.
2509 * This function complements the `die()` PHP function. The difference is that
2510 * HTML will be displayed to the user. It is recommended to use this function
2511 * only when the execution should not continue any further. It is not recommended
2512 * to call this function very often, and try to handle as many errors as possible
2513 * silently or more gracefully.
2515 * As a shorthand, the desired HTTP response code may be passed as an integer to
2516 * the `$title` parameter (the default title would apply) or the `$args` parameter.
2519 * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
2520 * an integer to be used as the response code.
2522 * @param string|WP_Error $message Optional. Error message. If this is a {@see WP_Error} object,
2523 * the error's messages are used. Default empty.
2524 * @param string|int $title Optional. Error title. If `$message` is a `WP_Error` object,
2525 * error data with the key 'title' may be used to specify the title.
2526 * If `$title` is an integer, then it is treated as the response
2527 * code. Default empty.
2528 * @param string|array|int $args {
2529 * Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
2530 * as the response code. Default empty array.
2532 * @type int $response The HTTP response code. Default 500.
2533 * @type bool $back_link Whether to include a link to go back. Default false.
2534 * @type string $text_direction The text direction. This is only useful internally, when WordPress
2535 * is still loading and the site's locale is not set up yet. Accepts 'rtl'.
2536 * Default is the value of {@see is_rtl()}.
2539 function wp_die( $message = '', $title = '', $args = array() ) {
2541 if ( is_int( $args ) ) {
2542 $args = array( 'response' => $args );
2543 } elseif ( is_int( $title ) ) {
2544 $args = array( 'response' => $title );
2548 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
2550 * Filter callback for killing WordPress execution for AJAX requests.
2554 * @param callable $function Callback function name.
2556 $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
2557 } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
2559 * Filter callback for killing WordPress execution for XML-RPC requests.
2563 * @param callable $function Callback function name.
2565 $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
2568 * Filter callback for killing WordPress execution for all non-AJAX, non-XML-RPC requests.
2572 * @param callable $function Callback function name.
2574 $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
2577 call_user_func( $function, $message, $title, $args );
2581 * Kill WordPress execution and display HTML message with error message.
2583 * This is the default handler for wp_die if you want a custom one for your
2584 * site then you can overload using the wp_die_handler filter in wp_die
2589 * @param string $message Error message.
2590 * @param string $title Optional. Error title. Default empty.
2591 * @param string|array $args Optional. Arguments to control behavior. Default empty array.
2593 function _default_wp_die_handler( $message, $title = '', $args = array() ) {
2594 $defaults = array( 'response' => 500 );
2595 $r = wp_parse_args($args, $defaults);
2597 $have_gettext = function_exists('__');
2599 if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
2600 if ( empty( $title ) ) {
2601 $error_data = $message->get_error_data();
2602 if ( is_array( $error_data ) && isset( $error_data['title'] ) )
2603 $title = $error_data['title'];
2605 $errors = $message->get_error_messages();
2606 switch ( count( $errors ) ) {
2611 $message = "<p>{$errors[0]}</p>";
2614 $message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
2617 } elseif ( is_string( $message ) ) {
2618 $message = "<p>$message</p>";
2621 if ( isset( $r['back_link'] ) && $r['back_link'] ) {
2622 $back_text = $have_gettext? __('« Back') : '« Back';
2623 $message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";
2626 if ( ! did_action( 'admin_head' ) ) :
2627 if ( !headers_sent() ) {
2628 status_header( $r['response'] );
2630 header( 'Content-Type: text/html; charset=utf-8' );
2633 if ( empty($title) )
2634 $title = $have_gettext ? __('WordPress › Error') : 'WordPress › Error';
2636 $text_direction = 'ltr';
2637 if ( isset($r['text_direction']) && 'rtl' == $r['text_direction'] )
2638 $text_direction = 'rtl';
2639 elseif ( function_exists( 'is_rtl' ) && is_rtl() )
2640 $text_direction = 'rtl';
2643 <!-- 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
2645 <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'"; ?>>
2647 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2648 <meta name="viewport" content="width=device-width">
2649 <title><?php echo $title ?></title>
2650 <style type="text/css">
2652 background: #f1f1f1;
2657 font-family: "Open Sans", sans-serif;
2661 -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2662 box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2665 border-bottom: 1px solid #dadada;
2668 font: 24px "Open Sans", sans-serif;
2671 padding-bottom: 7px;
2679 margin: 25px 0 20px;
2682 font-family: Consolas, Monaco, monospace;
2685 margin-bottom: 10px;
2699 0 0 2px 1px rgba(30, 140, 190, .8);
2702 0 0 2px 1px rgba(30, 140, 190, .8);
2706 background: #f7f7f7;
2707 border: 1px solid #ccc;
2709 display: inline-block;
2710 text-decoration: none;
2715 padding: 0 10px 1px;
2717 -webkit-border-radius: 3px;
2718 -webkit-appearance: none;
2720 white-space: nowrap;
2721 -webkit-box-sizing: border-box;
2722 -moz-box-sizing: border-box;
2723 box-sizing: border-box;
2725 -webkit-box-shadow: 0 1px 0 #ccc;
2726 box-shadow: 0 1px 0 #ccc;
2727 vertical-align: top;
2730 .button.button-large {
2733 padding: 0 12px 2px;
2738 background: #fafafa;
2744 border-color: #5b9dd9;
2745 -webkit-box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2746 box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2753 -webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2754 box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2755 -webkit-transform: translateY(1px);
2756 -ms-transform: translateY(1px);
2757 transform: translateY(1px);
2761 if ( 'rtl' == $text_direction ) {
2762 echo 'body { font-family: Tahoma, Arial; }';
2767 <body id="error-page">
2768 <?php endif; // ! did_action( 'admin_head' ) ?>
2769 <?php echo $message; ?>
2777 * Kill WordPress execution and display XML message with error message.
2779 * This is the handler for wp_die when processing XMLRPC requests.
2784 * @global wp_xmlrpc_server $wp_xmlrpc_server
2786 * @param string $message Error message.
2787 * @param string $title Optional. Error title. Default empty.
2788 * @param string|array $args Optional. Arguments to control behavior. Default empty array.
2790 function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
2791 global $wp_xmlrpc_server;
2792 $defaults = array( 'response' => 500 );
2794 $r = wp_parse_args($args, $defaults);
2796 if ( $wp_xmlrpc_server ) {
2797 $error = new IXR_Error( $r['response'] , $message);
2798 $wp_xmlrpc_server->output( $error->getXml() );
2804 * Kill WordPress ajax execution.
2806 * This is the handler for wp_die when processing Ajax requests.
2811 * @param string $message Optional. Response to print. Default empty.
2813 function _ajax_wp_die_handler( $message = '' ) {
2814 if ( is_scalar( $message ) )
2815 die( (string) $message );
2820 * Kill WordPress execution.
2822 * This is the handler for wp_die when processing APP requests.
2827 * @param string $message Optional. Response to print. Default empty.
2829 function _scalar_wp_die_handler( $message = '' ) {
2830 if ( is_scalar( $message ) )
2831 die( (string) $message );
2836 * Encode a variable into JSON, with some sanity checks.
2840 * @param mixed $data Variable (usually an array or object) to encode as JSON.
2841 * @param int $options Optional. Options to be passed to json_encode(). Default 0.
2842 * @param int $depth Optional. Maximum depth to walk through $data. Must be
2843 * greater than 0. Default 512.
2844 * @return string|false The JSON encoded string, or false if it cannot be encoded.
2846 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
2848 * json_encode() has had extra params added over the years.
2849 * $options was added in 5.3, and $depth in 5.5.
2850 * We need to make sure we call it with the correct arguments.
2852 if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
2853 $args = array( $data, $options, $depth );
2854 } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
2855 $args = array( $data, $options );
2857 $args = array( $data );
2860 // Prepare the data for JSON serialization.
2861 $data = _wp_json_prepare_data( $data );
2863 $json = @call_user_func_array( 'json_encode', $args );
2865 // If json_encode() was successful, no need to do more sanity checking.
2866 // ... unless we're in an old version of PHP, and json_encode() returned
2867 // a string containing 'null'. Then we need to do more sanity checking.
2868 if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) {
2873 $args[0] = _wp_json_sanity_check( $data, $depth );
2874 } catch ( Exception $e ) {
2878 return call_user_func_array( 'json_encode', $args );
2882 * Perform sanity checks on data that shall be encoded to JSON.
2888 * @see wp_json_encode()
2890 * @param mixed $data Variable (usually an array or object) to encode as JSON.
2891 * @param int $depth Maximum depth to walk through $data. Must be greater than 0.
2892 * @return mixed The sanitized data that shall be encoded to JSON.
2894 function _wp_json_sanity_check( $data, $depth ) {
2896 throw new Exception( 'Reached depth limit' );
2899 if ( is_array( $data ) ) {
2901 foreach ( $data as $id => $el ) {
2902 // Don't forget to sanitize the ID!
2903 if ( is_string( $id ) ) {
2904 $clean_id = _wp_json_convert_string( $id );
2909 // Check the element type, so that we're only recursing if we really have to.
2910 if ( is_array( $el ) || is_object( $el ) ) {
2911 $output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
2912 } elseif ( is_string( $el ) ) {
2913 $output[ $clean_id ] = _wp_json_convert_string( $el );
2915 $output[ $clean_id ] = $el;
2918 } elseif ( is_object( $data ) ) {
2919 $output = new stdClass;
2920 foreach ( $data as $id => $el ) {
2921 if ( is_string( $id ) ) {
2922 $clean_id = _wp_json_convert_string( $id );
2927 if ( is_array( $el ) || is_object( $el ) ) {
2928 $output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
2929 } elseif ( is_string( $el ) ) {
2930 $output->$clean_id = _wp_json_convert_string( $el );
2932 $output->$clean_id = $el;
2935 } elseif ( is_string( $data ) ) {
2936 return _wp_json_convert_string( $data );
2945 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
2951 * @see _wp_json_sanity_check()
2953 * @staticvar bool $use_mb
2955 * @param string $string The string which is to be converted.
2956 * @return string The checked string.
2958 function _wp_json_convert_string( $string ) {
2959 static $use_mb = null;
2960 if ( is_null( $use_mb ) ) {
2961 $use_mb = function_exists( 'mb_convert_encoding' );
2965 $encoding = mb_detect_encoding( $string, mb_detect_order(), true );
2967 return mb_convert_encoding( $string, 'UTF-8', $encoding );
2969 return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
2972 return wp_check_invalid_utf8( $string, true );
2977 * Prepares response data to be serialized to JSON.
2979 * This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
2985 * @param mixed $data Native representation.
2986 * @return bool|int|float|null|string|array Data ready for `json_encode()`.
2988 function _wp_json_prepare_data( $data ) {
2989 if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) {
2993 switch ( gettype( $data ) ) {
2999 // These values can be passed through.
3003 // Arrays must be mapped in case they also return objects.
3004 return array_map( '_wp_json_prepare_data', $data );
3007 // If this is an incomplete object (__PHP_Incomplete_Class), bail.
3008 if ( ! is_object( $data ) ) {
3012 if ( $data instanceof JsonSerializable ) {
3013 $data = $data->jsonSerialize();
3015 $data = get_object_vars( $data );
3018 // Now, pass the array (or whatever was returned from jsonSerialize through).
3019 return _wp_json_prepare_data( $data );
3027 * Send a JSON response back to an Ajax request.
3031 * @param mixed $response Variable (usually an array or object) to encode as JSON,
3032 * then print and die.
3034 function wp_send_json( $response ) {
3035 @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
3036 echo wp_json_encode( $response );
3037 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
3044 * Send a JSON response back to an Ajax request, indicating success.
3048 * @param mixed $data Data to encode as JSON, then print and die.
3050 function wp_send_json_success( $data = null ) {
3051 $response = array( 'success' => true );
3053 if ( isset( $data ) )
3054 $response['data'] = $data;
3056 wp_send_json( $response );
3060 * Send a JSON response back to an Ajax request, indicating failure.
3062 * If the `$data` parameter is a {@see WP_Error} object, the errors
3063 * within the object are processed and output as an array of error
3064 * codes and corresponding messages. All other types are output
3065 * without further processing.
3068 * @since 4.1.0 The `$data` parameter is now processed if a {@see WP_Error}
3069 * object is passed in.
3071 * @param mixed $data Data to encode as JSON, then print and die.
3073 function wp_send_json_error( $data = null ) {
3074 $response = array( 'success' => false );
3076 if ( isset( $data ) ) {
3077 if ( is_wp_error( $data ) ) {
3079 foreach ( $data->errors as $code => $messages ) {
3080 foreach ( $messages as $message ) {
3081 $result[] = array( 'code' => $code, 'message' => $message );
3085 $response['data'] = $result;
3087 $response['data'] = $data;
3091 wp_send_json( $response );
3095 * Retrieve the WordPress home page URL.
3097 * If the constant named 'WP_HOME' exists, then it will be used and returned
3098 * by the function. This can be used to counter the redirection on your local
3099 * development environment.
3106 * @param string $url URL for the home location.
3107 * @return string Homepage location.
3109 function _config_wp_home( $url = '' ) {
3110 if ( defined( 'WP_HOME' ) )
3111 return untrailingslashit( WP_HOME );
3116 * Retrieve the WordPress site URL.
3118 * If the constant named 'WP_SITEURL' is defined, then the value in that
3119 * constant will always be returned. This can be used for debugging a site
3120 * on your localhost while not having to change the database to your URL.
3127 * @param string $url URL to set the WordPress site location.
3128 * @return string The WordPress Site URL.
3130 function _config_wp_siteurl( $url = '' ) {
3131 if ( defined( 'WP_SITEURL' ) )
3132 return untrailingslashit( WP_SITEURL );
3137 * Set the localized direction for MCE plugin.
3139 * Will only set the direction to 'rtl', if the WordPress locale has
3140 * the text direction set to 'rtl'.
3142 * Fills in the 'directionality' setting, enables the 'directionality'
3143 * plugin, and adds the 'ltr' button to 'toolbar1', formerly
3144 * 'theme_advanced_buttons1' array keys. These keys are then returned
3145 * in the $input (TinyMCE settings) array.
3150 * @param array $input MCE settings array.
3151 * @return array Direction set for 'rtl', if needed by locale.
3153 function _mce_set_direction( $input ) {
3155 $input['directionality'] = 'rtl';
3157 if ( ! empty( $input['plugins'] ) && strpos( $input['plugins'], 'directionality' ) === false ) {
3158 $input['plugins'] .= ',directionality';
3161 if ( ! empty( $input['toolbar1'] ) && ! preg_match( '/\bltr\b/', $input['toolbar1'] ) ) {
3162 $input['toolbar1'] .= ',ltr';
3171 * Convert smiley code to the icon graphic file equivalent.
3173 * You can turn off smilies, by going to the write setting screen and unchecking
3174 * the box, or by setting 'use_smilies' option to false or removing the option.
3176 * Plugins may override the default smiley list by setting the $wpsmiliestrans
3177 * to an array, with the key the code the blogger types in and the value the
3180 * The $wp_smiliessearch global is for the regular expression and is set each
3181 * time the function is called.
3183 * The full list of smilies can be found in the function and won't be listed in
3184 * the description. Probably should create a Codex page for it, so that it is
3187 * @global array $wpsmiliestrans
3188 * @global array $wp_smiliessearch
3192 function smilies_init() {
3193 global $wpsmiliestrans, $wp_smiliessearch;
3195 // don't bother setting up smilies if they are disabled
3196 if ( !get_option( 'use_smilies' ) )
3199 if ( !isset( $wpsmiliestrans ) ) {
3200 $wpsmiliestrans = array(
3201 ':mrgreen:' => 'mrgreen.png',
3202 ':neutral:' => "\xf0\x9f\x98\x90",
3203 ':twisted:' => "\xf0\x9f\x98\x88",
3204 ':arrow:' => "\xe2\x9e\xa1",
3205 ':shock:' => "\xf0\x9f\x98\xaf",
3206 ':smile:' => "\xf0\x9f\x99\x82",
3207 ':???:' => "\xf0\x9f\x98\x95",
3208 ':cool:' => "\xf0\x9f\x98\x8e",
3209 ':evil:' => "\xf0\x9f\x91\xbf",
3210 ':grin:' => "\xf0\x9f\x98\x80",
3211 ':idea:' => "\xf0\x9f\x92\xa1",
3212 ':oops:' => "\xf0\x9f\x98\xb3",
3213 ':razz:' => "\xf0\x9f\x98\x9b",
3214 ':roll:' => 'rolleyes.png',
3215 ':wink:' => "\xf0\x9f\x98\x89",
3216 ':cry:' => "\xf0\x9f\x98\xa5",
3217 ':eek:' => "\xf0\x9f\x98\xae",
3218 ':lol:' => "\xf0\x9f\x98\x86",
3219 ':mad:' => "\xf0\x9f\x98\xa1",
3220 ':sad:' => "\xf0\x9f\x99\x81",
3221 '8-)' => "\xf0\x9f\x98\x8e",
3222 '8-O' => "\xf0\x9f\x98\xaf",
3223 ':-(' => "\xf0\x9f\x99\x81",
3224 ':-)' => "\xf0\x9f\x99\x82",
3225 ':-?' => "\xf0\x9f\x98\x95",
3226 ':-D' => "\xf0\x9f\x98\x80",
3227 ':-P' => "\xf0\x9f\x98\x9b",
3228 ':-o' => "\xf0\x9f\x98\xae",
3229 ':-x' => "\xf0\x9f\x98\xa1",
3230 ':-|' => "\xf0\x9f\x98\x90",
3231 ';-)' => "\xf0\x9f\x98\x89",
3232 // This one transformation breaks regular text with frequency.
3233 // '8)' => "\xf0\x9f\x98\x8e",
3234 '8O' => "\xf0\x9f\x98\xaf",
3235 ':(' => "\xf0\x9f\x99\x81",
3236 ':)' => "\xf0\x9f\x99\x82",
3237 ':?' => "\xf0\x9f\x98\x95",
3238 ':D' => "\xf0\x9f\x98\x80",
3239 ':P' => "\xf0\x9f\x98\x9b",
3240 ':o' => "\xf0\x9f\x98\xae",
3241 ':x' => "\xf0\x9f\x98\xa1",
3242 ':|' => "\xf0\x9f\x98\x90",
3243 ';)' => "\xf0\x9f\x98\x89",
3244 ':!:' => "\xe2\x9d\x97",
3245 ':?:' => "\xe2\x9d\x93",
3249 if (count($wpsmiliestrans) == 0) {
3254 * NOTE: we sort the smilies in reverse key order. This is to make sure
3255 * we match the longest possible smilie (:???: vs :?) as the regular
3256 * expression used below is first-match
3258 krsort($wpsmiliestrans);
3260 $spaces = wp_spaces_regexp();
3262 // Begin first "subpattern"
3263 $wp_smiliessearch = '/(?<=' . $spaces . '|^)';
3266 foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
3267 $firstchar = substr($smiley, 0, 1);
3268 $rest = substr($smiley, 1);
3271 if ($firstchar != $subchar) {
3272 if ($subchar != '') {
3273 $wp_smiliessearch .= ')(?=' . $spaces . '|$)'; // End previous "subpattern"
3274 $wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern"
3276 $subchar = $firstchar;
3277 $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
3279 $wp_smiliessearch .= '|';
3281 $wp_smiliessearch .= preg_quote($rest, '/');
3284 $wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
3289 * Merge user defined arguments into defaults array.
3291 * This function is used throughout WordPress to allow for both string or array
3292 * to be merged into another array.
3296 * @param string|array $args Value to merge with $defaults
3297 * @param array $defaults Optional. Array that serves as the defaults. Default empty.
3298 * @return array Merged user defined values with defaults.
3300 function wp_parse_args( $args, $defaults = '' ) {
3301 if ( is_object( $args ) )
3302 $r = get_object_vars( $args );
3303 elseif ( is_array( $args ) )
3306 wp_parse_str( $args, $r );
3308 if ( is_array( $defaults ) )
3309 return array_merge( $defaults, $r );
3314 * Clean up an array, comma- or space-separated list of IDs.
3318 * @param array|string $list List of ids.
3319 * @return array Sanitized array of IDs.
3321 function wp_parse_id_list( $list ) {
3322 if ( !is_array($list) )
3323 $list = preg_split('/[\s,]+/', $list);
3325 return array_unique(array_map('absint', $list));
3329 * Extract a slice of an array, given a list of keys.
3333 * @param array $array The original array.
3334 * @param array $keys The list of keys.
3335 * @return array The array slice.
3337 function wp_array_slice_assoc( $array, $keys ) {
3339 foreach ( $keys as $key )
3340 if ( isset( $array[ $key ] ) )
3341 $slice[ $key ] = $array[ $key ];
3347 * Determines if the variable is a numeric-indexed array.
3351 * @param mixed $data Variable to check.
3352 * @return bool Whether the variable is a list.
3354 function wp_is_numeric_array( $data ) {
3355 if ( ! is_array( $data ) ) {
3359 $keys = array_keys( $data );
3360 $string_keys = array_filter( $keys, 'is_string' );
3361 return count( $string_keys ) === 0;
3365 * Filters a list of objects, based on a set of key => value arguments.
3369 * @param array $list An array of objects to filter
3370 * @param array $args Optional. An array of key => value arguments to match
3371 * against each object. Default empty array.
3372 * @param string $operator Optional. The logical operation to perform. 'or' means
3373 * only one element from the array needs to match; 'and'
3374 * means all elements must match; 'not' means no elements may
3375 * match. Default 'and'.
3376 * @param bool|string $field A field from the object to place instead of the entire object.
3378 * @return array A list of objects or object fields.
3380 function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
3381 if ( ! is_array( $list ) )
3384 $list = wp_list_filter( $list, $args, $operator );
3387 $list = wp_list_pluck( $list, $field );
3393 * Filters a list of objects, based on a set of key => value arguments.
3397 * @param array $list An array of objects to filter.
3398 * @param array $args Optional. An array of key => value arguments to match
3399 * against each object. Default empty array.
3400 * @param string $operator Optional. The logical operation to perform. 'AND' means
3401 * all elements from the array must match. 'OR' means only
3402 * one element needs to match. 'NOT' means no elements may
3403 * match. Default 'AND'.
3404 * @return array Array of found values.
3406 function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
3407 if ( ! is_array( $list ) )
3410 if ( empty( $args ) )
3413 $operator = strtoupper( $operator );
3414 $count = count( $args );
3415 $filtered = array();
3417 foreach ( $list as $key => $obj ) {
3418 $to_match = (array) $obj;
3421 foreach ( $args as $m_key => $m_value ) {
3422 if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
3426 if ( ( 'AND' == $operator && $matched == $count )
3427 || ( 'OR' == $operator && $matched > 0 )
3428 || ( 'NOT' == $operator && 0 == $matched ) ) {
3429 $filtered[$key] = $obj;
3437 * Pluck a certain field out of each object in a list.
3439 * This has the same functionality and prototype of
3440 * array_column() (PHP 5.5) but also supports objects.
3443 * @since 4.0.0 $index_key parameter added.
3445 * @param array $list List of objects or arrays
3446 * @param int|string $field Field from the object to place instead of the entire object
3447 * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
3449 * @return array Array of found values. If `$index_key` is set, an array of found values with keys
3450 * corresponding to `$index_key`. If `$index_key` is null, array keys from the original
3451 * `$list` will be preserved in the results.
3453 function wp_list_pluck( $list, $field, $index_key = null ) {
3454 if ( ! $index_key ) {
3456 * This is simple. Could at some point wrap array_column()
3457 * if we knew we had an array of arrays.
3459 foreach ( $list as $key => $value ) {
3460 if ( is_object( $value ) ) {
3461 $list[ $key ] = $value->$field;
3463 $list[ $key ] = $value[ $field ];
3470 * When index_key is not set for a particular item, push the value
3471 * to the end of the stack. This is how array_column() behaves.
3474 foreach ( $list as $value ) {
3475 if ( is_object( $value ) ) {
3476 if ( isset( $value->$index_key ) ) {
3477 $newlist[ $value->$index_key ] = $value->$field;
3479 $newlist[] = $value->$field;
3482 if ( isset( $value[ $index_key ] ) ) {
3483 $newlist[ $value[ $index_key ] ] = $value[ $field ];
3485 $newlist[] = $value[ $field ];
3494 * Determines if Widgets library should be loaded.
3496 * Checks to make sure that the widgets library hasn't already been loaded.
3497 * If it hasn't, then it will load the widgets library and run an action hook.
3501 function wp_maybe_load_widgets() {
3503 * Filter whether to load the Widgets library.
3505 * Passing a falsey value to the filter will effectively short-circuit
3506 * the Widgets library from loading.
3510 * @param bool $wp_maybe_load_widgets Whether to load the Widgets library.
3513 if ( ! apply_filters( 'load_default_widgets', true ) ) {
3517 require_once( ABSPATH . WPINC . '/default-widgets.php' );
3519 add_action( '_admin_menu', 'wp_widgets_add_menu' );
3523 * Append the Widgets menu to the themes main menu.
3527 * @global array $submenu
3529 function wp_widgets_add_menu() {
3532 if ( ! current_theme_supports( 'widgets' ) )
3535 $submenu['themes.php'][7] = array( __( 'Widgets' ), 'edit_theme_options', 'widgets.php' );
3536 ksort( $submenu['themes.php'], SORT_NUMERIC );
3540 * Flush all output buffers for PHP 5.2.
3542 * Make sure all output buffers are flushed before our singletons are destroyed.
3546 function wp_ob_end_flush_all() {
3547 $levels = ob_get_level();
3548 for ($i=0; $i<$levels; $i++)
3553 * Load custom DB error or display WordPress DB error.
3555 * If a file exists in the wp-content directory named db-error.php, then it will
3556 * be loaded instead of displaying the WordPress DB error. If it is not found,
3557 * then the WordPress DB error will be displayed instead.
3559 * The WordPress DB error sets the HTTP status header to 500 to try to prevent
3560 * search engines from caching the message. Custom DB messages should do the
3563 * This function was backported to WordPress 2.3.2, but originally was added
3564 * in WordPress 2.5.0.
3568 * @global wpdb $wpdb WordPress database abstraction object.
3570 function dead_db() {
3573 wp_load_translations_early();
3575 // Load custom DB error template, if present.
3576 if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
3577 require_once( WP_CONTENT_DIR . '/db-error.php' );
3581 // If installing or in the admin, provide the verbose message.
3582 if ( wp_installing() || defined( 'WP_ADMIN' ) )
3583 wp_die($wpdb->error);
3585 // Otherwise, be terse.
3586 status_header( 500 );
3588 header( 'Content-Type: text/html; charset=utf-8' );
3591 <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
3593 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3594 <title><?php _e( 'Database Error' ); ?></title>
3598 <h1><?php _e( 'Error establishing a database connection' ); ?></h1>
3606 * Convert a value to non-negative integer.
3610 * @param mixed $maybeint Data you wish to have converted to a non-negative integer.
3611 * @return int A non-negative integer.
3613 function absint( $maybeint ) {
3614 return abs( intval( $maybeint ) );
3618 * Mark a function as deprecated and inform when it has been used.
3620 * There is a hook deprecated_function_run that will be called that can be used
3621 * to get the backtrace up to what file and function called the deprecated
3624 * The current behavior is to trigger a user error if WP_DEBUG is true.
3626 * This function is to be used in every function that is deprecated.
3631 * @param string $function The function that was called.
3632 * @param string $version The version of WordPress that deprecated the function.
3633 * @param string $replacement Optional. The function that should have been called. Default null.
3635 function _deprecated_function( $function, $version, $replacement = null ) {
3638 * Fires when a deprecated function is called.
3642 * @param string $function The function that was called.
3643 * @param string $replacement The function that should have been called.
3644 * @param string $version The version of WordPress that deprecated the function.
3646 do_action( 'deprecated_function_run', $function, $replacement, $version );
3649 * Filter whether to trigger an error for deprecated functions.
3653 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3655 if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
3656 if ( function_exists( '__' ) ) {
3657 if ( ! is_null( $replacement ) )
3658 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
3660 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
3662 if ( ! is_null( $replacement ) )
3663 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
3665 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
3671 * Marks a constructor as deprecated and informs when it has been used.
3673 * Similar to _deprecated_function(), but with different strings. Used to
3674 * remove PHP4 style constructors.
3676 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3678 * This function is to be used in every PHP4 style constructor method that is deprecated.
3681 * @since 4.5.0 Added the `$parent_class` parameter.
3685 * @param string $class The class containing the deprecated constructor.
3686 * @param string $version The version of WordPress that deprecated the function.
3687 * @param string $parent_class Optional. The parent class calling the deprecated constructor.
3688 * Default empty string.
3690 function _deprecated_constructor( $class, $version, $parent_class = '' ) {
3693 * Fires when a deprecated constructor is called.
3696 * @since 4.5.0 Added the `$parent_class` parameter.
3698 * @param string $class The class containing the deprecated constructor.
3699 * @param string $version The version of WordPress that deprecated the function.
3700 * @param string $parent_class The parent class calling the deprecated constructor.
3702 do_action( 'deprecated_constructor_run', $class, $version, $parent_class );
3705 * Filter whether to trigger an error for deprecated functions.
3707 * `WP_DEBUG` must be true in addition to the filter evaluating to true.
3711 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3713 if ( WP_DEBUG && apply_filters( 'deprecated_constructor_trigger_error', true ) ) {
3714 if ( function_exists( '__' ) ) {
3715 if ( ! empty( $parent_class ) ) {
3716 /* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: __construct() method */
3717 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.' ),
3718 $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3720 /* translators: 1: PHP class name, 2: version number, 3: __construct() method */
3721 trigger_error( sprintf( __( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
3722 $class, $version, '<pre>__construct()</pre>' ) );
3725 if ( ! empty( $parent_class ) ) {
3726 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.',
3727 $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3729 trigger_error( sprintf( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
3730 $class, $version, '<pre>__construct()</pre>' ) );
3738 * Mark a file as deprecated and inform when it has been used.
3740 * There is a hook deprecated_file_included that will be called that can be used
3741 * to get the backtrace up to what file and function included the deprecated
3744 * The current behavior is to trigger a user error if WP_DEBUG is true.
3746 * This function is to be used in every file that is deprecated.
3751 * @param string $file The file that was included.
3752 * @param string $version The version of WordPress that deprecated the file.
3753 * @param string $replacement Optional. The file that should have been included based on ABSPATH.
3755 * @param string $message Optional. A message regarding the change. Default empty.
3757 function _deprecated_file( $file, $version, $replacement = null, $message = '' ) {
3760 * Fires when a deprecated file is called.
3764 * @param string $file The file that was called.
3765 * @param string $replacement The file that should have been included based on ABSPATH.
3766 * @param string $version The version of WordPress that deprecated the file.
3767 * @param string $message A message regarding the change.
3769 do_action( 'deprecated_file_included', $file, $replacement, $version, $message );
3772 * Filter whether to trigger an error for deprecated files.
3776 * @param bool $trigger Whether to trigger the error for deprecated files. Default true.
3778 if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
3779 $message = empty( $message ) ? '' : ' ' . $message;
3780 if ( function_exists( '__' ) ) {
3781 if ( ! is_null( $replacement ) )
3782 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) . $message );
3784 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $file, $version ) . $message );
3786 if ( ! is_null( $replacement ) )
3787 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $file, $version, $replacement ) . $message );
3789 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
3794 * Mark a function argument as deprecated and inform when it has been used.
3796 * This function is to be used whenever a deprecated function argument is used.
3797 * Before this function is called, the argument must be checked for whether it was
3798 * used by comparing it to its default value or evaluating whether it is empty.
3801 * if ( ! empty( $deprecated ) ) {
3802 * _deprecated_argument( __FUNCTION__, '3.0' );
3806 * There is a hook deprecated_argument_run that will be called that can be used
3807 * to get the backtrace up to what file and function used the deprecated
3810 * The current behavior is to trigger a user error if WP_DEBUG is true.
3815 * @param string $function The function that was called.
3816 * @param string $version The version of WordPress that deprecated the argument used.
3817 * @param string $message Optional. A message regarding the change. Default null.
3819 function _deprecated_argument( $function, $version, $message = null ) {
3822 * Fires when a deprecated argument is called.
3826 * @param string $function The function that was called.
3827 * @param string $message A message regarding the change.
3828 * @param string $version The version of WordPress that deprecated the argument used.
3830 do_action( 'deprecated_argument_run', $function, $message, $version );
3833 * Filter whether to trigger an error for deprecated arguments.
3837 * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true.
3839 if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
3840 if ( function_exists( '__' ) ) {
3841 if ( ! is_null( $message ) )
3842 trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s'), $function, $version, $message ) );
3844 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 ) );
3846 if ( ! is_null( $message ) )
3847 trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) );
3849 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 ) );
3855 * Mark something as being incorrectly called.
3857 * There is a hook doing_it_wrong_run that will be called that can be used
3858 * to get the backtrace up to what file and function called the deprecated
3861 * The current behavior is to trigger a user error if WP_DEBUG is true.
3866 * @param string $function The function that was called.
3867 * @param string $message A message explaining what has been done incorrectly.
3868 * @param string $version The version of WordPress where the message was added.
3870 function _doing_it_wrong( $function, $message, $version ) {
3873 * Fires when the given function is being used incorrectly.
3877 * @param string $function The function that was called.
3878 * @param string $message A message explaining what has been done incorrectly.
3879 * @param string $version The version of WordPress where the message was added.
3881 do_action( 'doing_it_wrong_run', $function, $message, $version );
3884 * Filter whether to trigger an error for _doing_it_wrong() calls.
3888 * @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true.
3890 if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
3891 if ( function_exists( '__' ) ) {
3892 $version = is_null( $version ) ? '' : sprintf( __( '(This message was added in version %s.)' ), $version );
3893 /* translators: %s: Codex URL */
3894 $message .= ' ' . sprintf( __( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
3895 __( 'https://codex.wordpress.org/Debugging_in_WordPress' )
3897 trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
3899 $version = is_null( $version ) ? '' : sprintf( '(This message was added in version %s.)', $version );
3900 $message .= sprintf( ' Please see <a href="%s">Debugging in WordPress</a> for more information.',
3901 'https://codex.wordpress.org/Debugging_in_WordPress'
3903 trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
3909 * Is the server running earlier than 1.5.0 version of lighttpd?
3913 * @return bool Whether the server is running lighttpd < 1.5.0.
3915 function is_lighttpd_before_150() {
3916 $server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
3917 $server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
3918 return 'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
3922 * Does the specified module exist in the Apache config?
3926 * @global bool $is_apache
3928 * @param string $mod The module, e.g. mod_rewrite.
3929 * @param bool $default Optional. The default return value if the module is not found. Default false.
3930 * @return bool Whether the specified module is loaded.
3932 function apache_mod_loaded($mod, $default = false) {
3938 if ( function_exists( 'apache_get_modules' ) ) {
3939 $mods = apache_get_modules();
3940 if ( in_array($mod, $mods) )
3942 } elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) {
3945 $phpinfo = ob_get_clean();
3946 if ( false !== strpos($phpinfo, $mod) )
3953 * Check if IIS 7+ supports pretty permalinks.
3957 * @global bool $is_iis7
3959 * @return bool Whether IIS7 supports permalinks.
3961 function iis7_supports_permalinks() {
3964 $supports_permalinks = false;
3966 /* First we check if the DOMDocument class exists. If it does not exist, then we cannot
3967 * easily update the xml configuration file, hence we just bail out and tell user that
3968 * pretty permalinks cannot be used.
3970 * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
3971 * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
3972 * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
3973 * via ISAPI then pretty permalinks will not work.
3975 $supports_permalinks = class_exists( 'DOMDocument', false ) && isset($_SERVER['IIS_UrlRewriteModule']) && ( PHP_SAPI == 'cgi-fcgi' );
3979 * Filter whether IIS 7+ supports pretty permalinks.
3983 * @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false.
3985 return apply_filters( 'iis7_supports_permalinks', $supports_permalinks );
3989 * File validates against allowed set of defined rules.
3991 * A return value of '1' means that the $file contains either '..' or './'. A
3992 * return value of '2' means that the $file contains ':' after the first
3993 * character. A return value of '3' means that the file is not in the allowed
3998 * @param string $file File path.
3999 * @param array $allowed_files List of allowed files.
4000 * @return int 0 means nothing is wrong, greater than 0 means something was wrong.
4002 function validate_file( $file, $allowed_files = '' ) {
4003 if ( false !== strpos( $file, '..' ) )
4006 if ( false !== strpos( $file, './' ) )
4009 if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files ) )
4012 if (':' == substr( $file, 1, 1 ) )
4019 * Determine if SSL is used.
4023 * @return bool True if SSL, false if not used.
4026 if ( isset($_SERVER['HTTPS']) ) {
4027 if ( 'on' == strtolower($_SERVER['HTTPS']) )
4029 if ( '1' == $_SERVER['HTTPS'] )
4031 } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
4038 * Whether to force SSL used for the Administration Screens.
4042 * @staticvar bool $forced
4044 * @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
4045 * @return bool True if forced, false if not forced.
4047 function force_ssl_admin( $force = null ) {
4048 static $forced = false;
4050 if ( !is_null( $force ) ) {
4051 $old_forced = $forced;
4060 * Guess the URL for the site.
4062 * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
4067 * @return string The guessed URL.
4069 function wp_guess_url() {
4070 if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
4073 $abspath_fix = str_replace( '\\', '/', ABSPATH );
4074 $script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );
4076 // The request is for the admin
4077 if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
4078 $path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] );
4080 // The request is for a file in ABSPATH
4081 } elseif ( $script_filename_dir . '/' == $abspath_fix ) {
4082 // Strip off any file/query params in the path
4083 $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );
4086 if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
4087 // Request is hitting a file inside ABSPATH
4088 $directory = str_replace( ABSPATH, '', $script_filename_dir );
4089 // Strip off the sub directory, and any file/query params
4090 $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] );
4091 } elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
4092 // Request is hitting a file above ABSPATH
4093 $subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
4094 // Strip off any file/query params from the path, appending the sub directory to the install
4095 $path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory;
4097 $path = $_SERVER['REQUEST_URI'];
4101 $schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet
4102 $url = $schema . $_SERVER['HTTP_HOST'] . $path;
4105 return rtrim($url, '/');
4109 * Temporarily suspend cache additions.
4111 * Stops more data being added to the cache, but still allows cache retrieval.
4112 * This is useful for actions, such as imports, when a lot of data would otherwise
4113 * be almost uselessly added to the cache.
4115 * Suspension lasts for a single page load at most. Remember to call this
4116 * function again if you wish to re-enable cache adds earlier.
4120 * @staticvar bool $_suspend
4122 * @param bool $suspend Optional. Suspends additions if true, re-enables them if false.
4123 * @return bool The current suspend setting
4125 function wp_suspend_cache_addition( $suspend = null ) {
4126 static $_suspend = false;
4128 if ( is_bool( $suspend ) )
4129 $_suspend = $suspend;
4135 * Suspend cache invalidation.
4137 * Turns cache invalidation on and off. Useful during imports where you don't wont to do
4138 * invalidations every time a post is inserted. Callers must be sure that what they are
4139 * doing won't lead to an inconsistent cache when invalidation is suspended.
4143 * @global bool $_wp_suspend_cache_invalidation
4145 * @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true.
4146 * @return bool The current suspend setting.
4148 function wp_suspend_cache_invalidation( $suspend = true ) {
4149 global $_wp_suspend_cache_invalidation;
4151 $current_suspend = $_wp_suspend_cache_invalidation;
4152 $_wp_suspend_cache_invalidation = $suspend;
4153 return $current_suspend;
4157 * Determine whether a site is the main site of the current network.
4161 * @global object $current_site
4163 * @param int $site_id Optional. Site ID to test. Defaults to current site.
4164 * Defaults to current site.
4165 * @return bool True if $site_id is the main site of the network, or if not
4166 * running Multisite.
4168 function is_main_site( $site_id = null ) {
4169 // This is the current network's information; 'site' is old terminology.
4170 global $current_site;
4172 if ( ! is_multisite() )
4176 $site_id = get_current_blog_id();
4178 return (int) $site_id === (int) $current_site->blog_id;
4182 * Determine whether a network is the main network of the Multisite install.
4186 * @param int $network_id Optional. Network ID to test. Defaults to current network.
4187 * @return bool True if $network_id is the main network, or if not running Multisite.
4189 function is_main_network( $network_id = null ) {
4190 if ( ! is_multisite() ) {
4194 $current_network_id = (int) get_current_site()->id;
4196 if ( null === $network_id ) {
4197 $network_id = $current_network_id;
4200 $network_id = (int) $network_id;
4202 return ( $network_id === get_main_network_id() );
4206 * Get the main network ID.
4210 * @global wpdb $wpdb WordPress database abstraction object.
4212 * @return int The ID of the main network.
4214 function get_main_network_id() {
4217 if ( ! is_multisite() ) {
4221 if ( defined( 'PRIMARY_NETWORK_ID' ) ) {
4222 $main_network_id = PRIMARY_NETWORK_ID;
4223 } elseif ( 1 === (int) get_current_site()->id ) {
4224 // If the current network has an ID of 1, assume it is the main network.
4225 $main_network_id = 1;
4227 $main_network_id = wp_cache_get( 'primary_network_id', 'site-options' );
4229 if ( false === $main_network_id ) {
4230 $main_network_id = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1" );
4231 wp_cache_add( 'primary_network_id', $main_network_id, 'site-options' );
4236 * Filter the main network ID.
4240 * @param int $main_network_id The ID of the main network.
4242 return (int) apply_filters( 'get_main_network_id', $main_network_id );
4246 * Determine whether global terms are enabled.
4250 * @staticvar bool $global_terms
4252 * @return bool True if multisite and global terms enabled.
4254 function global_terms_enabled() {
4255 if ( ! is_multisite() )
4258 static $global_terms = null;
4259 if ( is_null( $global_terms ) ) {
4262 * Filter whether global terms are enabled.
4264 * Passing a non-null value to the filter will effectively short-circuit the function,
4265 * returning the value of the 'global_terms_enabled' site option instead.
4269 * @param null $enabled Whether global terms are enabled.
4271 $filter = apply_filters( 'global_terms_enabled', null );
4272 if ( ! is_null( $filter ) )
4273 $global_terms = (bool) $filter;
4275 $global_terms = (bool) get_site_option( 'global_terms_enabled', false );
4277 return $global_terms;
4281 * gmt_offset modification for smart timezone handling.
4283 * Overrides the gmt_offset option if we have a timezone_string available.
4287 * @return float|false Timezone GMT offset, false otherwise.
4289 function wp_timezone_override_offset() {
4290 if ( !$timezone_string = get_option( 'timezone_string' ) ) {
4294 $timezone_object = timezone_open( $timezone_string );
4295 $datetime_object = date_create();
4296 if ( false === $timezone_object || false === $datetime_object ) {
4299 return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 );
4303 * Sort-helper for timezones.
4312 function _wp_timezone_choice_usort_callback( $a, $b ) {
4313 // Don't use translated versions of Etc
4314 if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
4315 // Make the order of these more like the old dropdown
4316 if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4317 return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
4319 if ( 'UTC' === $a['city'] ) {
4320 if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4325 if ( 'UTC' === $b['city'] ) {
4326 if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
4331 return strnatcasecmp( $a['city'], $b['city'] );
4333 if ( $a['t_continent'] == $b['t_continent'] ) {
4334 if ( $a['t_city'] == $b['t_city'] ) {
4335 return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
4337 return strnatcasecmp( $a['t_city'], $b['t_city'] );
4339 // Force Etc to the bottom of the list
4340 if ( 'Etc' === $a['continent'] ) {
4343 if ( 'Etc' === $b['continent'] ) {
4346 return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
4351 * Gives a nicely-formatted list of timezone strings.
4355 * @staticvar bool $mo_loaded
4357 * @param string $selected_zone Selected timezone.
4360 function wp_timezone_choice( $selected_zone ) {
4361 static $mo_loaded = false;
4363 $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
4365 // Load translations for continents and cities
4366 if ( !$mo_loaded ) {
4367 $locale = get_locale();
4368 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale . '.mo';
4369 load_textdomain( 'continents-cities', $mofile );
4374 foreach ( timezone_identifiers_list() as $zone ) {
4375 $zone = explode( '/', $zone );
4376 if ( !in_array( $zone[0], $continents ) ) {
4380 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
4382 0 => ( isset( $zone[0] ) && $zone[0] ),
4383 1 => ( isset( $zone[1] ) && $zone[1] ),
4384 2 => ( isset( $zone[2] ) && $zone[2] ),
4386 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
4387 $exists[4] = ( $exists[1] && $exists[3] );
4388 $exists[5] = ( $exists[2] && $exists[3] );
4391 'continent' => ( $exists[0] ? $zone[0] : '' ),
4392 'city' => ( $exists[1] ? $zone[1] : '' ),
4393 'subcity' => ( $exists[2] ? $zone[2] : '' ),
4394 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
4395 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
4396 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
4399 usort( $zonen, '_wp_timezone_choice_usort_callback' );
4401 $structure = array();
4403 if ( empty( $selected_zone ) ) {
4404 $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
4407 foreach ( $zonen as $key => $zone ) {
4408 // Build value in an array to join later
4409 $value = array( $zone['continent'] );
4411 if ( empty( $zone['city'] ) ) {
4412 // It's at the continent level (generally won't happen)
4413 $display = $zone['t_continent'];
4415 // It's inside a continent group
4417 // Continent optgroup
4418 if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
4419 $label = $zone['t_continent'];
4420 $structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
4423 // Add the city to the value
4424 $value[] = $zone['city'];
4426 $display = $zone['t_city'];
4427 if ( !empty( $zone['subcity'] ) ) {
4428 // Add the subcity to the value
4429 $value[] = $zone['subcity'];
4430 $display .= ' - ' . $zone['t_subcity'];
4435 $value = join( '/', $value );
4437 if ( $value === $selected_zone ) {
4438 $selected = 'selected="selected" ';
4440 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
4442 // Close continent optgroup
4443 if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) {
4444 $structure[] = '</optgroup>';
4449 $structure[] = '<optgroup label="'. esc_attr__( 'UTC' ) .'">';
4451 if ( 'UTC' === $selected_zone )
4452 $selected = 'selected="selected" ';
4453 $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __('UTC') . '</option>';
4454 $structure[] = '</optgroup>';
4456 // Do manual UTC offsets
4457 $structure[] = '<optgroup label="'. esc_attr__( 'Manual Offsets' ) .'">';
4458 $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,
4459 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);
4460 foreach ( $offset_range as $offset ) {
4462 $offset_name = '+' . $offset;
4464 $offset_name = (string) $offset;
4466 $offset_value = $offset_name;
4467 $offset_name = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset_name);
4468 $offset_name = 'UTC' . $offset_name;
4469 $offset_value = 'UTC' . $offset_value;
4471 if ( $offset_value === $selected_zone )
4472 $selected = 'selected="selected" ';
4473 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . "</option>";
4476 $structure[] = '</optgroup>';
4478 return join( "\n", $structure );
4482 * Strip close comment and close php tags from file headers used by WP.
4487 * @see https://core.trac.wordpress.org/ticket/8497
4489 * @param string $str Header comment to clean up.
4492 function _cleanup_header_comment( $str ) {
4493 return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
4497 * Permanently delete comments or posts of any type that have held a status
4498 * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
4500 * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
4504 * @global wpdb $wpdb WordPress database abstraction object.
4506 function wp_scheduled_delete() {
4509 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
4511 $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);
4513 foreach ( (array) $posts_to_delete as $post ) {
4514 $post_id = (int) $post['post_id'];
4518 $del_post = get_post($post_id);
4520 if ( !$del_post || 'trash' != $del_post->post_status ) {
4521 delete_post_meta($post_id, '_wp_trash_meta_status');
4522 delete_post_meta($post_id, '_wp_trash_meta_time');
4524 wp_delete_post($post_id);
4528 $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);
4530 foreach ( (array) $comments_to_delete as $comment ) {
4531 $comment_id = (int) $comment['comment_id'];
4535 $del_comment = get_comment($comment_id);
4537 if ( !$del_comment || 'trash' != $del_comment->comment_approved ) {
4538 delete_comment_meta($comment_id, '_wp_trash_meta_time');
4539 delete_comment_meta($comment_id, '_wp_trash_meta_status');
4541 wp_delete_comment( $del_comment );
4547 * Retrieve metadata from a file.
4549 * Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
4550 * Each piece of metadata must be on its own line. Fields can not span multiple
4551 * lines, the value will get cut at the end of the first line.
4553 * If the file data is not within that first 8kiB, then the author should correct
4554 * their plugin file and move the data headers to the top.
4556 * @link https://codex.wordpress.org/File_Header
4560 * @param string $file Path to the file.
4561 * @param array $default_headers List of headers, in the format array('HeaderKey' => 'Header Name').
4562 * @param string $context Optional. If specified adds filter hook "extra_{$context}_headers".
4564 * @return array Array of file headers in `HeaderKey => Header Value` format.
4566 function get_file_data( $file, $default_headers, $context = '' ) {
4567 // We don't need to write to the file, so just open for reading.
4568 $fp = fopen( $file, 'r' );
4570 // Pull only the first 8kiB of the file in.
4571 $file_data = fread( $fp, 8192 );
4573 // PHP will close file handle, but we are good citizens.
4576 // Make sure we catch CR-only line endings.
4577 $file_data = str_replace( "\r", "\n", $file_data );
4580 * Filter extra file headers by context.
4582 * The dynamic portion of the hook name, `$context`, refers to
4583 * the context where extra headers might be loaded.
4587 * @param array $extra_context_headers Empty array by default.
4589 if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
4590 $extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
4591 $all_headers = array_merge( $extra_headers, (array) $default_headers );
4593 $all_headers = $default_headers;
4596 foreach ( $all_headers as $field => $regex ) {
4597 if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
4598 $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
4600 $all_headers[ $field ] = '';
4603 return $all_headers;
4609 * Useful for returning true to filters easily.
4613 * @see __return_false()
4615 * @return true True.
4617 function __return_true() {
4624 * Useful for returning false to filters easily.
4628 * @see __return_true()
4630 * @return false False.
4632 function __return_false() {
4639 * Useful for returning 0 to filters easily.
4645 function __return_zero() {
4650 * Returns an empty array.
4652 * Useful for returning an empty array to filters easily.
4656 * @return array Empty array.
4658 function __return_empty_array() {
4665 * Useful for returning null to filters easily.
4669 * @return null Null value.
4671 function __return_null() {
4676 * Returns an empty string.
4678 * Useful for returning an empty string to filters easily.
4682 * @see __return_null()
4684 * @return string Empty string.
4686 function __return_empty_string() {
4691 * Send a HTTP header to disable content type sniffing in browsers which support it.
4695 * @see http://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
4696 * @see http://src.chromium.org/viewvc/chrome?view=rev&revision=6985
4698 function send_nosniff_header() {
4699 @header( 'X-Content-Type-Options: nosniff' );
4703 * Return a MySQL expression for selecting the week number based on the start_of_week option.
4708 * @param string $column Database column.
4709 * @return string SQL clause.
4711 function _wp_mysql_week( $column ) {
4712 switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
4714 return "WEEK( $column, 1 )";
4720 return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
4723 return "WEEK( $column, 0 )";
4728 * Find hierarchy loops using a callback function that maps object IDs to parent IDs.
4733 * @param callable $callback Function that accepts ( ID, $callback_args ) and outputs parent_ID.
4734 * @param int $start The ID to start the loop check at.
4735 * @param int $start_parent The parent_ID of $start to use instead of calling $callback( $start ).
4736 * Use null to always use $callback
4737 * @param array $callback_args Optional. Additional arguments to send to $callback.
4738 * @return array IDs of all members of loop.
4740 function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
4741 $override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
4743 if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
4746 return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
4750 * Use the "The Tortoise and the Hare" algorithm to detect loops.
4752 * For every step of the algorithm, the hare takes two steps and the tortoise one.
4753 * If the hare ever laps the tortoise, there must be a loop.
4758 * @param callable $callback Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
4759 * @param int $start The ID to start the loop check at.
4760 * @param array $override Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback.
4761 * Default empty array.
4762 * @param array $callback_args Optional. Additional arguments to send to $callback. Default empty array.
4763 * @param bool $_return_loop Optional. Return loop members or just detect presence of loop? Only set
4764 * to true if you already know the given $start is part of a loop (otherwise
4765 * the returned array might include branches). Default false.
4766 * @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if
4769 function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
4770 $tortoise = $hare = $evanescent_hare = $start;
4773 // Set evanescent_hare to one past hare
4774 // Increment hare two steps
4778 ( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
4780 ( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
4782 if ( $_return_loop )
4783 $return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
4785 // tortoise got lapped - must be a loop
4786 if ( $tortoise == $evanescent_hare || $tortoise == $hare )
4787 return $_return_loop ? $return : $tortoise;
4789 // Increment tortoise by one step
4790 $tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
4797 * Send a HTTP header to limit rendering of pages to same origin iframes.
4801 * @see https://developer.mozilla.org/en/the_x-frame-options_response_header
4803 function send_frame_options_header() {
4804 @header( 'X-Frame-Options: SAMEORIGIN' );
4808 * Retrieve a list of protocols to allow in HTML attributes.
4811 * @since 4.3.0 Added 'webcal' to the protocols array.
4816 * @staticvar array $protocols
4818 * @return array Array of allowed protocols. Defaults to an array containing 'http', 'https',
4819 * 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet',
4820 * 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', and 'webcal'.
4822 function wp_allowed_protocols() {
4823 static $protocols = array();
4825 if ( empty( $protocols ) ) {
4826 $protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal' );
4829 * Filter the list of protocols allowed in HTML attributes.
4833 * @param array $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more.
4835 $protocols = apply_filters( 'kses_allowed_protocols', $protocols );
4842 * Return a comma-separated string of functions that have been called to get
4843 * to the current point in code.
4847 * @see https://core.trac.wordpress.org/ticket/19589
4849 * @param string $ignore_class Optional. A class to ignore all function calls within - useful
4850 * when you want to just give info about the callee. Default null.
4851 * @param int $skip_frames Optional. A number of stack frames to skip - useful for unwinding
4852 * back to the source of the issue. Default 0.
4853 * @param bool $pretty Optional. Whether or not you want a comma separated string or raw
4854 * array returned. Default true.
4855 * @return string|array Either a string containing a reversed comma separated trace or an array
4856 * of individual calls.
4858 function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
4859 if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) )
4860 $trace = debug_backtrace( false );
4862 $trace = debug_backtrace();
4865 $check_class = ! is_null( $ignore_class );
4866 $skip_frames++; // skip this function
4868 foreach ( $trace as $call ) {
4869 if ( $skip_frames > 0 ) {
4871 } elseif ( isset( $call['class'] ) ) {
4872 if ( $check_class && $ignore_class == $call['class'] )
4873 continue; // Filter out calls
4875 $caller[] = "{$call['class']}{$call['type']}{$call['function']}";
4877 if ( in_array( $call['function'], array( 'do_action', 'apply_filters' ) ) ) {
4878 $caller[] = "{$call['function']}('{$call['args'][0]}')";
4879 } elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) {
4880 $caller[] = $call['function'] . "('" . str_replace( array( WP_CONTENT_DIR, ABSPATH ) , '', $call['args'][0] ) . "')";
4882 $caller[] = $call['function'];
4887 return join( ', ', array_reverse( $caller ) );
4893 * Retrieve ids that are not already present in the cache.
4898 * @param array $object_ids ID list.
4899 * @param string $cache_key The cache bucket to check against.
4901 * @return array List of ids not present in the cache.
4903 function _get_non_cached_ids( $object_ids, $cache_key ) {
4905 foreach ( $object_ids as $id ) {
4907 if ( !wp_cache_get( $id, $cache_key ) ) {
4916 * Test if the current device has the capability to upload files.
4921 * @return bool Whether the device is able to upload files.
4923 function _device_can_upload() {
4924 if ( ! wp_is_mobile() )
4927 $ua = $_SERVER['HTTP_USER_AGENT'];
4929 if ( strpos($ua, 'iPhone') !== false
4930 || strpos($ua, 'iPad') !== false
4931 || strpos($ua, 'iPod') !== false ) {
4932 return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' );
4939 * Test if a given path is a stream URL
4941 * @param string $path The resource path or URL.
4942 * @return bool True if the path is a stream URL.
4944 function wp_is_stream( $path ) {
4945 $wrappers = stream_get_wrappers();
4946 $wrappers_re = '(' . join('|', $wrappers) . ')';
4948 return preg_match( "!^$wrappers_re://!", $path ) === 1;
4952 * Test if the supplied date is valid for the Gregorian calendar.
4958 * @param int $month Month number.
4959 * @param int $day Day number.
4960 * @param int $year Year number.
4961 * @param string $source_date The date to filter.
4962 * @return bool True if valid date, false if not valid date.
4964 function wp_checkdate( $month, $day, $year, $source_date ) {
4966 * Filter whether the given date is valid for the Gregorian calendar.
4970 * @param bool $checkdate Whether the given date is valid.
4971 * @param string $source_date Date to check.
4973 return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
4977 * Load the auth check for monitoring whether the user is still logged in.
4979 * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' );
4981 * This is disabled for certain screens where a login screen could cause an
4982 * inconvenient interruption. A filter called wp_auth_check_load can be used
4983 * for fine-grained control.
4987 function wp_auth_check_load() {
4988 if ( ! is_admin() && ! is_user_logged_in() )
4991 if ( defined( 'IFRAME_REQUEST' ) )
4994 $screen = get_current_screen();
4995 $hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
4996 $show = ! in_array( $screen->id, $hidden );
4999 * Filter whether to load the authentication check.
5001 * Passing a falsey value to the filter will effectively short-circuit
5002 * loading the authentication check.
5006 * @param bool $show Whether to load the authentication check.
5007 * @param WP_Screen $screen The current screen object.
5009 if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
5010 wp_enqueue_style( 'wp-auth-check' );
5011 wp_enqueue_script( 'wp-auth-check' );
5013 add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
5014 add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
5019 * Output the HTML that shows the wp-login dialog when the user is no longer logged in.
5023 function wp_auth_check_html() {
5024 $login_url = wp_login_url();
5025 $current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
5026 $same_domain = ( strpos( $login_url, $current_domain ) === 0 );
5029 * Filter whether the authentication check originated at the same domain.
5033 * @param bool $same_domain Whether the authentication check originated at the same domain.
5035 $same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
5036 $wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
5039 <div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
5040 <div id="wp-auth-check-bg"></div>
5041 <div id="wp-auth-check">
5042 <button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></button>
5045 if ( $same_domain ) {
5047 <div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( add_query_arg( array( 'interim-login' => 1 ), $login_url ) ); ?>"></div>
5052 <div class="wp-auth-fallback">
5053 <p><b class="wp-auth-fallback-expired" tabindex="0"><?php _e('Session expired'); ?></b></p>
5054 <p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php _e('Please log in again.'); ?></a>
5055 <?php _e('The login page will open in a new window. After logging in you can close it and return to this page.'); ?></p>
5063 * Check whether a user is still logged in, for the heartbeat.
5065 * Send a result that shows a log-in box if the user is no longer logged in,
5066 * or if their cookie is within the grace period.
5070 * @global int $login_grace_period
5072 * @param array $response The Heartbeat response.
5073 * @return array $response The Heartbeat response with 'wp-auth-check' value set.
5075 function wp_auth_check( $response ) {
5076 $response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] );
5081 * Return RegEx body to liberally match an opening HTML tag.
5083 * Matches an opening HTML tag that:
5084 * 1. Is self-closing or
5085 * 2. Has no body but has a closing tag of the same name or
5086 * 3. Contains a body and a closing tag of the same name
5088 * Note: this RegEx does not balance inner tags and does not attempt
5089 * to produce valid HTML
5093 * @param string $tag An HTML tag name. Example: 'video'.
5094 * @return string Tag RegEx.
5096 function get_tag_regex( $tag ) {
5097 if ( empty( $tag ) )
5099 return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
5103 * Retrieve a canonical form of the provided charset appropriate for passing to PHP
5104 * functions such as htmlspecialchars() and charset html attributes.
5109 * @see https://core.trac.wordpress.org/ticket/23688
5111 * @param string $charset A charset name.
5112 * @return string The canonical form of the charset.
5114 function _canonical_charset( $charset ) {
5115 if ( 'UTF-8' === $charset || 'utf-8' === $charset || 'utf8' === $charset ||
5116 'UTF8' === $charset )
5119 if ( 'ISO-8859-1' === $charset || 'iso-8859-1' === $charset ||
5120 'iso8859-1' === $charset || 'ISO8859-1' === $charset )
5121 return 'ISO-8859-1';
5127 * Set the mbstring internal encoding to a binary safe encoding when func_overload
5130 * When mbstring.func_overload is in use for multi-byte encodings, the results from
5131 * strlen() and similar functions respect the utf8 characters, causing binary data
5132 * to return incorrect lengths.
5134 * This function overrides the mbstring encoding to a binary-safe encoding, and
5135 * resets it to the users expected encoding afterwards through the
5136 * `reset_mbstring_encoding` function.
5138 * It is safe to recursively call this function, however each
5139 * `mbstring_binary_safe_encoding()` call must be followed up with an equal number
5140 * of `reset_mbstring_encoding()` calls.
5144 * @see reset_mbstring_encoding()
5146 * @staticvar array $encodings
5147 * @staticvar bool $overloaded
5149 * @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
5152 function mbstring_binary_safe_encoding( $reset = false ) {
5153 static $encodings = array();
5154 static $overloaded = null;
5156 if ( is_null( $overloaded ) )
5157 $overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 );
5159 if ( false === $overloaded )
5163 $encoding = mb_internal_encoding();
5164 array_push( $encodings, $encoding );
5165 mb_internal_encoding( 'ISO-8859-1' );
5168 if ( $reset && $encodings ) {
5169 $encoding = array_pop( $encodings );
5170 mb_internal_encoding( $encoding );
5175 * Reset the mbstring internal encoding to a users previously set encoding.
5177 * @see mbstring_binary_safe_encoding()
5181 function reset_mbstring_encoding() {
5182 mbstring_binary_safe_encoding( true );
5186 * Filter/validate a variable as a boolean.
5188 * Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`.
5192 * @param mixed $var Boolean value to validate.
5193 * @return bool Whether the value is validated.
5195 function wp_validate_boolean( $var ) {
5196 if ( is_bool( $var ) ) {
5200 if ( is_string( $var ) && 'false' === strtolower( $var ) ) {
5212 * @param string $file The path to the file to delete.
5214 function wp_delete_file( $file ) {
5216 * Filter the path of the file to delete.
5220 * @param string $medium Path to the file to delete.
5222 $delete = apply_filters( 'wp_delete_file', $file );
5223 if ( ! empty( $delete ) ) {
5229 * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
5231 * This prevents reusing the same tab for a preview when the user has navigated away.
5235 function wp_post_preview_js() {
5238 if ( ! is_preview() || empty( $post ) ) {
5242 // Has to match the window name used in post_submit_meta_box()
5243 $name = 'wp-preview-' . (int) $post->ID;
5248 var query = document.location.search;
5250 if ( query && query.indexOf( 'preview=true' ) !== -1 ) {
5251 window.name = '<?php echo $name; ?>';
5254 if ( window.addEventListener ) {
5255 window.addEventListener( 'unload', function() { window.name = ''; }, false );
5263 * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339.
5265 * Explicitly strips timezones, as datetimes are not saved with any timezone
5266 * information. Including any information on the offset could be misleading.
5270 * @param string $date_string Date string to parse and format.
5271 * @return string Date formatted for ISO8601/RFC3339.
5273 function mysql_to_rfc3339( $date_string ) {
5274 $formatted = mysql2date( 'c', $date_string, false );
5276 // Strip timezone information
5277 return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );