]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions.php
WordPress 4.7.2-scripts
[autoinstalls/wordpress.git] / wp-includes / functions.php
1 <?php
2 /**
3  * Main WordPress API
4  *
5  * @package WordPress
6  */
7
8 require( ABSPATH . WPINC . '/option.php' );
9
10 /**
11  * Convert given date string into a different format.
12  *
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.
15  *
16  * If $translate is true then the given date and format string will
17  * be passed to date_i18n() for translation.
18  *
19  * @since 0.71
20  *
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.
25  */
26 function mysql2date( $format, $date, $translate = true ) {
27         if ( empty( $date ) )
28                 return false;
29
30         if ( 'G' == $format )
31                 return strtotime( $date . ' +0000' );
32
33         $i = strtotime( $date );
34
35         if ( 'U' == $format )
36                 return $i;
37
38         if ( $translate )
39                 return date_i18n( $format, $i );
40         else
41                 return date( $format, $i );
42 }
43
44 /**
45  * Retrieve the current time based on specified type.
46  *
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').
50  *
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.
53  *
54  * @since 1.0.0
55  *
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.
60  */
61 function current_time( $type, $gmt = 0 ) {
62         switch ( $type ) {
63                 case 'mysql':
64                         return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
65                 case 'timestamp':
66                         return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
67                 default:
68                         return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
69         }
70 }
71
72 /**
73  * Retrieve the date in localized format, based on timestamp.
74  *
75  * If the locale specifies the locale month and weekday, then the locale will
76  * take over the format for the date. If it isn't, then the date format string
77  * will be used instead.
78  *
79  * @since 0.71
80  *
81  * @global WP_Locale $wp_locale
82  *
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.
86  *
87  * @return string The date, translated if locale specifies it.
88  */
89 function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
90         global $wp_locale;
91         $i = $unixtimestamp;
92
93         if ( false === $i ) {
94                 $i = current_time( 'timestamp', $gmt );
95         }
96
97         /*
98          * Store original value for language with untypical grammars.
99          * See https://core.trac.wordpress.org/ticket/9396
100          */
101         $req_format = $dateformatstring;
102
103         if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
104                 $datemonth = $wp_locale->get_month( date( 'm', $i ) );
105                 $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
106                 $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
107                 $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
108                 $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
109                 $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
110                 $dateformatstring = ' '.$dateformatstring;
111                 $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
112                 $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
113                 $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
114                 $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
115                 $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
116                 $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
117
118                 $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
119         }
120         $timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
121         $timezone_formats_re = implode( '|', $timezone_formats );
122         if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
123                 $timezone_string = get_option( 'timezone_string' );
124                 if ( $timezone_string ) {
125                         $timezone_object = timezone_open( $timezone_string );
126                         $date_object = date_create( null, $timezone_object );
127                         foreach ( $timezone_formats as $timezone_format ) {
128                                 if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
129                                         $formatted = date_format( $date_object, $timezone_format );
130                                         $dateformatstring = ' '.$dateformatstring;
131                                         $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
132                                         $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
133                                 }
134                         }
135                 }
136         }
137         $j = @date( $dateformatstring, $i );
138
139         /**
140          * Filters the date formatted based on the locale.
141          *
142          * @since 2.8.0
143          *
144          * @param string $j          Formatted date string.
145          * @param string $req_format Format to display the date.
146          * @param int    $i          Unix timestamp.
147          * @param bool   $gmt        Whether to convert to GMT for time. Default false.
148          */
149         $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
150         return $j;
151 }
152
153 /**
154  * Determines if the date should be declined.
155  *
156  * If the locale specifies that month names require a genitive case in certain
157  * formats (like 'j F Y'), the month name will be replaced with a correct form.
158  *
159  * @since 4.4.0
160  *
161  * @param string $date Formatted date string.
162  * @return string The date, declined if locale specifies it.
163  */
164 function wp_maybe_decline_date( $date ) {
165         global $wp_locale;
166
167         // i18n functions are not available in SHORTINIT mode
168         if ( ! function_exists( '_x' ) ) {
169                 return $date;
170         }
171
172         /* translators: If months in your language require a genitive case,
173          * translate this to 'on'. Do not translate into your own language.
174          */
175         if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
176                 // Match a format like 'j F Y' or 'j. F'
177                 if ( @preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) {
178                         $months          = $wp_locale->month;
179                         $months_genitive = $wp_locale->month_genitive;
180
181                         foreach ( $months as $key => $month ) {
182                                 $months[ $key ] = '# ' . $month . '( |$)#u';
183                         }
184
185                         foreach ( $months_genitive as $key => $month ) {
186                                 $months_genitive[ $key ] = ' ' . $month . '$1';
187                         }
188
189                         $date = preg_replace( $months, $months_genitive, $date );
190                 }
191         }
192
193         // Used for locale-specific rules
194         $locale = get_locale();
195
196         if ( 'ca' === $locale ) {
197                 // " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
198                 $date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
199         }
200
201         return $date;
202 }
203
204 /**
205  * Convert float number to format based on the locale.
206  *
207  * @since 2.3.0
208  *
209  * @global WP_Locale $wp_locale
210  *
211  * @param float $number   The number to convert based on locale.
212  * @param int   $decimals Optional. Precision of the number of decimal places. Default 0.
213  * @return string Converted number in string format.
214  */
215 function number_format_i18n( $number, $decimals = 0 ) {
216         global $wp_locale;
217
218         if ( isset( $wp_locale ) ) {
219                 $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
220         } else {
221                 $formatted = number_format( $number, absint( $decimals ) );
222         }
223
224         /**
225          * Filters the number formatted based on the locale.
226          *
227          * @since  2.8.0
228          *
229          * @param string $formatted Converted number in string format.
230          */
231         return apply_filters( 'number_format_i18n', $formatted );
232 }
233
234 /**
235  * Convert number of bytes largest unit bytes will fit into.
236  *
237  * It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts
238  * number of bytes to human readable number by taking the number of that unit
239  * that the bytes will go into it. Supports TB value.
240  *
241  * Please note that integers in PHP are limited to 32 bits, unless they are on
242  * 64 bit architecture, then they have 64 bit size. If you need to place the
243  * larger size then what PHP integer type will hold, then use a string. It will
244  * be converted to a double, which should always have 64 bit length.
245  *
246  * Technically the correct unit names for powers of 1024 are KiB, MiB etc.
247  *
248  * @since 2.3.0
249  *
250  * @param int|string $bytes    Number of bytes. Note max integer size for integers.
251  * @param int        $decimals Optional. Precision of number of decimal places. Default 0.
252  * @return string|false False on failure. Number string on success.
253  */
254 function size_format( $bytes, $decimals = 0 ) {
255         $quant = array(
256                 'TB' => TB_IN_BYTES,
257                 'GB' => GB_IN_BYTES,
258                 'MB' => MB_IN_BYTES,
259                 'KB' => KB_IN_BYTES,
260                 'B'  => 1,
261         );
262
263         if ( 0 === $bytes ) {
264                 return number_format_i18n( 0, $decimals ) . ' B';
265         }
266
267         foreach ( $quant as $unit => $mag ) {
268                 if ( doubleval( $bytes ) >= $mag ) {
269                         return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
270                 }
271         }
272
273         return false;
274 }
275
276 /**
277  * Get the week start and end from the datetime or date string from MySQL.
278  *
279  * @since 0.71
280  *
281  * @param string     $mysqlstring   Date or datetime field type from MySQL.
282  * @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string.
283  * @return array Keys are 'start' and 'end'.
284  */
285 function get_weekstartend( $mysqlstring, $start_of_week = '' ) {
286         // MySQL string year.
287         $my = substr( $mysqlstring, 0, 4 );
288
289         // MySQL string month.
290         $mm = substr( $mysqlstring, 8, 2 );
291
292         // MySQL string day.
293         $md = substr( $mysqlstring, 5, 2 );
294
295         // The timestamp for MySQL string day.
296         $day = mktime( 0, 0, 0, $md, $mm, $my );
297
298         // The day of the week from the timestamp.
299         $weekday = date( 'w', $day );
300
301         if ( !is_numeric($start_of_week) )
302                 $start_of_week = get_option( 'start_of_week' );
303
304         if ( $weekday < $start_of_week )
305                 $weekday += 7;
306
307         // The most recent week start day on or before $day.
308         $start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week );
309
310         // $start + 1 week - 1 second.
311         $end = $start + WEEK_IN_SECONDS - 1;
312         return compact( 'start', 'end' );
313 }
314
315 /**
316  * Unserialize value only if it was serialized.
317  *
318  * @since 2.0.0
319  *
320  * @param string $original Maybe unserialized original, if is needed.
321  * @return mixed Unserialized data can be any type.
322  */
323 function maybe_unserialize( $original ) {
324         if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
325                 return @unserialize( $original );
326         return $original;
327 }
328
329 /**
330  * Check value to find if it was serialized.
331  *
332  * If $data is not an string, then returned value will always be false.
333  * Serialized data is always a string.
334  *
335  * @since 2.0.5
336  *
337  * @param string $data   Value to check to see if was serialized.
338  * @param bool   $strict Optional. Whether to be strict about the end of the string. Default true.
339  * @return bool False if not serialized and true if it was.
340  */
341 function is_serialized( $data, $strict = true ) {
342         // if it isn't a string, it isn't serialized.
343         if ( ! is_string( $data ) ) {
344                 return false;
345         }
346         $data = trim( $data );
347         if ( 'N;' == $data ) {
348                 return true;
349         }
350         if ( strlen( $data ) < 4 ) {
351                 return false;
352         }
353         if ( ':' !== $data[1] ) {
354                 return false;
355         }
356         if ( $strict ) {
357                 $lastc = substr( $data, -1 );
358                 if ( ';' !== $lastc && '}' !== $lastc ) {
359                         return false;
360                 }
361         } else {
362                 $semicolon = strpos( $data, ';' );
363                 $brace     = strpos( $data, '}' );
364                 // Either ; or } must exist.
365                 if ( false === $semicolon && false === $brace )
366                         return false;
367                 // But neither must be in the first X characters.
368                 if ( false !== $semicolon && $semicolon < 3 )
369                         return false;
370                 if ( false !== $brace && $brace < 4 )
371                         return false;
372         }
373         $token = $data[0];
374         switch ( $token ) {
375                 case 's' :
376                         if ( $strict ) {
377                                 if ( '"' !== substr( $data, -2, 1 ) ) {
378                                         return false;
379                                 }
380                         } elseif ( false === strpos( $data, '"' ) ) {
381                                 return false;
382                         }
383                         // or else fall through
384                 case 'a' :
385                 case 'O' :
386                         return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
387                 case 'b' :
388                 case 'i' :
389                 case 'd' :
390                         $end = $strict ? '$' : '';
391                         return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data );
392         }
393         return false;
394 }
395
396 /**
397  * Check whether serialized data is of string type.
398  *
399  * @since 2.0.5
400  *
401  * @param string $data Serialized data.
402  * @return bool False if not a serialized string, true if it is.
403  */
404 function is_serialized_string( $data ) {
405         // if it isn't a string, it isn't a serialized string.
406         if ( ! is_string( $data ) ) {
407                 return false;
408         }
409         $data = trim( $data );
410         if ( strlen( $data ) < 4 ) {
411                 return false;
412         } elseif ( ':' !== $data[1] ) {
413                 return false;
414         } elseif ( ';' !== substr( $data, -1 ) ) {
415                 return false;
416         } elseif ( $data[0] !== 's' ) {
417                 return false;
418         } elseif ( '"' !== substr( $data, -2, 1 ) ) {
419                 return false;
420         } else {
421                 return true;
422         }
423 }
424
425 /**
426  * Serialize data, if needed.
427  *
428  * @since 2.0.5
429  *
430  * @param string|array|object $data Data that might be serialized.
431  * @return mixed A scalar data
432  */
433 function maybe_serialize( $data ) {
434         if ( is_array( $data ) || is_object( $data ) )
435                 return serialize( $data );
436
437         // Double serialization is required for backward compatibility.
438         // See https://core.trac.wordpress.org/ticket/12930
439         // Also the world will end. See WP 3.6.1.
440         if ( is_serialized( $data, false ) )
441                 return serialize( $data );
442
443         return $data;
444 }
445
446 /**
447  * Retrieve post title from XMLRPC XML.
448  *
449  * If the title element is not part of the XML, then the default post title from
450  * the $post_default_title will be used instead.
451  *
452  * @since 0.71
453  *
454  * @global string $post_default_title Default XML-RPC post title.
455  *
456  * @param string $content XMLRPC XML Request content
457  * @return string Post title
458  */
459 function xmlrpc_getposttitle( $content ) {
460         global $post_default_title;
461         if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
462                 $post_title = $matchtitle[1];
463         } else {
464                 $post_title = $post_default_title;
465         }
466         return $post_title;
467 }
468
469 /**
470  * Retrieve the post category or categories from XMLRPC XML.
471  *
472  * If the category element is not found, then the default post category will be
473  * used. The return type then would be what $post_default_category. If the
474  * category is found, then it will always be an array.
475  *
476  * @since 0.71
477  *
478  * @global string $post_default_category Default XML-RPC post category.
479  *
480  * @param string $content XMLRPC XML Request content
481  * @return string|array List of categories or category name.
482  */
483 function xmlrpc_getpostcategory( $content ) {
484         global $post_default_category;
485         if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
486                 $post_category = trim( $matchcat[1], ',' );
487                 $post_category = explode( ',', $post_category );
488         } else {
489                 $post_category = $post_default_category;
490         }
491         return $post_category;
492 }
493
494 /**
495  * XMLRPC XML content without title and category elements.
496  *
497  * @since 0.71
498  *
499  * @param string $content XML-RPC XML Request content.
500  * @return string XMLRPC XML Request content without title and category elements.
501  */
502 function xmlrpc_removepostdata( $content ) {
503         $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
504         $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
505         $content = trim( $content );
506         return $content;
507 }
508
509 /**
510  * Use RegEx to extract URLs from arbitrary content.
511  *
512  * @since 3.7.0
513  *
514  * @param string $content Content to extract URLs from.
515  * @return array URLs found in passed string.
516  */
517 function wp_extract_urls( $content ) {
518         preg_match_all(
519                 "#([\"']?)("
520                         . "(?:([\w-]+:)?//?)"
521                         . "[^\s()<>]+"
522                         . "[.]"
523                         . "(?:"
524                                 . "\([\w\d]+\)|"
525                                 . "(?:"
526                                         . "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
527                                         . "(?:[:]\d+)?/?"
528                                 . ")+"
529                         . ")"
530                 . ")\\1#",
531                 $content,
532                 $post_links
533         );
534
535         $post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) );
536
537         return array_values( $post_links );
538 }
539
540 /**
541  * Check content for video and audio links to add as enclosures.
542  *
543  * Will not add enclosures that have already been added and will
544  * remove enclosures that are no longer in the post. This is called as
545  * pingbacks and trackbacks.
546  *
547  * @since 1.5.0
548  *
549  * @global wpdb $wpdb WordPress database abstraction object.
550  *
551  * @param string $content Post Content.
552  * @param int    $post_ID Post ID.
553  */
554 function do_enclose( $content, $post_ID ) {
555         global $wpdb;
556
557         //TODO: Tidy this ghetto code up and make the debug code optional
558         include_once( ABSPATH . WPINC . '/class-IXR.php' );
559
560         $post_links = array();
561
562         $pung = get_enclosed( $post_ID );
563
564         $post_links_temp = wp_extract_urls( $content );
565
566         foreach ( $pung as $link_test ) {
567                 if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
568                         $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 ) . '%') );
569                         foreach ( $mids as $mid )
570                                 delete_metadata_by_mid( 'post', $mid );
571                 }
572         }
573
574         foreach ( (array) $post_links_temp as $link_test ) {
575                 if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
576                         $test = @parse_url( $link_test );
577                         if ( false === $test )
578                                 continue;
579                         if ( isset( $test['query'] ) )
580                                 $post_links[] = $link_test;
581                         elseif ( isset($test['path']) && ( $test['path'] != '/' ) &&  ($test['path'] != '' ) )
582                                 $post_links[] = $link_test;
583                 }
584         }
585
586         /**
587          * Filters the list of enclosure links before querying the database.
588          *
589          * Allows for the addition and/or removal of potential enclosures to save
590          * to postmeta before checking the database for existing enclosures.
591          *
592          * @since 4.4.0
593          *
594          * @param array $post_links An array of enclosure links.
595          * @param int   $post_ID    Post ID.
596          */
597         $post_links = apply_filters( 'enclosure_links', $post_links, $post_ID );
598
599         foreach ( (array) $post_links as $url ) {
600                 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
602                         if ( $headers = wp_get_http_headers( $url) ) {
603                                 $len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
604                                 $type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
605                                 $allowed_types = array( 'video', 'audio' );
606
607                                 // Check to see if we can figure out the mime type from
608                                 // the extension
609                                 $url_parts = @parse_url( $url );
610                                 if ( false !== $url_parts ) {
611                                         $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
612                                         if ( !empty( $extension ) ) {
613                                                 foreach ( wp_get_mime_types() as $exts => $mime ) {
614                                                         if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
615                                                                 $type = $mime;
616                                                                 break;
617                                                         }
618                                                 }
619                                         }
620                                 }
621
622                                 if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
623                                         add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );
624                                 }
625                         }
626                 }
627         }
628 }
629
630 /**
631  * Retrieve HTTP Headers from URL.
632  *
633  * @since 1.5.1
634  *
635  * @param string $url        URL to retrieve HTTP headers from.
636  * @param bool   $deprecated Not Used.
637  * @return bool|string False on failure, headers on success.
638  */
639 function wp_get_http_headers( $url, $deprecated = false ) {
640         if ( !empty( $deprecated ) )
641                 _deprecated_argument( __FUNCTION__, '2.7.0' );
642
643         $response = wp_safe_remote_head( $url );
644
645         if ( is_wp_error( $response ) )
646                 return false;
647
648         return wp_remote_retrieve_headers( $response );
649 }
650
651 /**
652  * Whether the publish date of the current post in the loop is different from the
653  * publish date of the previous post in the loop.
654  *
655  * @since 0.71
656  *
657  * @global string $currentday  The day of the current post in the loop.
658  * @global string $previousday The day of the previous post in the loop.
659  *
660  * @return int 1 when new day, 0 if not a new day.
661  */
662 function is_new_day() {
663         global $currentday, $previousday;
664         if ( $currentday != $previousday )
665                 return 1;
666         else
667                 return 0;
668 }
669
670 /**
671  * Build URL query based on an associative and, or indexed array.
672  *
673  * This is a convenient function for easily building url queries. It sets the
674  * separator to '&' and uses _http_build_query() function.
675  *
676  * @since 2.3.0
677  *
678  * @see _http_build_query() Used to build the query
679  * @link https://secure.php.net/manual/en/function.http-build-query.php for more on what
680  *               http_build_query() does.
681  *
682  * @param array $data URL-encode key/value pairs.
683  * @return string URL-encoded string.
684  */
685 function build_query( $data ) {
686         return _http_build_query( $data, null, '&', '', false );
687 }
688
689 /**
690  * From php.net (modified by Mark Jaquith to behave like the native PHP5 function).
691  *
692  * @since 3.2.0
693  * @access private
694  *
695  * @see https://secure.php.net/manual/en/function.http-build-query.php
696  *
697  * @param array|object  $data       An array or object of data. Converted to array.
698  * @param string        $prefix     Optional. Numeric index. If set, start parameter numbering with it.
699  *                                  Default null.
700  * @param string        $sep        Optional. Argument separator; defaults to 'arg_separator.output'.
701  *                                  Default null.
702  * @param string        $key        Optional. Used to prefix key name. Default empty.
703  * @param bool          $urlencode  Optional. Whether to use urlencode() in the result. Default true.
704  *
705  * @return string The query string.
706  */
707 function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
708         $ret = array();
709
710         foreach ( (array) $data as $k => $v ) {
711                 if ( $urlencode)
712                         $k = urlencode($k);
713                 if ( is_int($k) && $prefix != null )
714                         $k = $prefix.$k;
715                 if ( !empty($key) )
716                         $k = $key . '%5B' . $k . '%5D';
717                 if ( $v === null )
718                         continue;
719                 elseif ( $v === false )
720                         $v = '0';
721
722                 if ( is_array($v) || is_object($v) )
723                         array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
724                 elseif ( $urlencode )
725                         array_push($ret, $k.'='.urlencode($v));
726                 else
727                         array_push($ret, $k.'='.$v);
728         }
729
730         if ( null === $sep )
731                 $sep = ini_get('arg_separator.output');
732
733         return implode($sep, $ret);
734 }
735
736 /**
737  * Retrieves a modified URL query string.
738  *
739  * You can rebuild the URL and append query variables to the URL query by using this function.
740  * There are two ways to use this function; either a single key and value, or an associative array.
741  *
742  * Using a single key and value:
743  *
744  *     add_query_arg( 'key', 'value', 'http://example.com' );
745  *
746  * Using an associative array:
747  *
748  *     add_query_arg( array(
749  *         'key1' => 'value1',
750  *         'key2' => 'value2',
751  *     ), 'http://example.com' );
752  *
753  * Omitting the URL from either use results in the current URL being used
754  * (the value of `$_SERVER['REQUEST_URI']`).
755  *
756  * Values are expected to be encoded appropriately with urlencode() or rawurlencode().
757  *
758  * Setting any query variable's value to boolean false removes the key (see remove_query_arg()).
759  *
760  * Important: The return value of add_query_arg() is not escaped by default. Output should be
761  * late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting
762  * (XSS) attacks.
763  *
764  * @since 1.5.0
765  *
766  * @param string|array $key   Either a query variable key, or an associative array of query variables.
767  * @param string       $value Optional. Either a query variable value, or a URL to act upon.
768  * @param string       $url   Optional. A URL to act upon.
769  * @return string New URL query string (unescaped).
770  */
771 function add_query_arg() {
772         $args = func_get_args();
773         if ( is_array( $args[0] ) ) {
774                 if ( count( $args ) < 2 || false === $args[1] )
775                         $uri = $_SERVER['REQUEST_URI'];
776                 else
777                         $uri = $args[1];
778         } else {
779                 if ( count( $args ) < 3 || false === $args[2] )
780                         $uri = $_SERVER['REQUEST_URI'];
781                 else
782                         $uri = $args[2];
783         }
784
785         if ( $frag = strstr( $uri, '#' ) )
786                 $uri = substr( $uri, 0, -strlen( $frag ) );
787         else
788                 $frag = '';
789
790         if ( 0 === stripos( $uri, 'http://' ) ) {
791                 $protocol = 'http://';
792                 $uri = substr( $uri, 7 );
793         } elseif ( 0 === stripos( $uri, 'https://' ) ) {
794                 $protocol = 'https://';
795                 $uri = substr( $uri, 8 );
796         } else {
797                 $protocol = '';
798         }
799
800         if ( strpos( $uri, '?' ) !== false ) {
801                 list( $base, $query ) = explode( '?', $uri, 2 );
802                 $base .= '?';
803         } elseif ( $protocol || strpos( $uri, '=' ) === false ) {
804                 $base = $uri . '?';
805                 $query = '';
806         } else {
807                 $base = '';
808                 $query = $uri;
809         }
810
811         wp_parse_str( $query, $qs );
812         $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
813         if ( is_array( $args[0] ) ) {
814                 foreach ( $args[0] as $k => $v ) {
815                         $qs[ $k ] = $v;
816                 }
817         } else {
818                 $qs[ $args[0] ] = $args[1];
819         }
820
821         foreach ( $qs as $k => $v ) {
822                 if ( $v === false )
823                         unset( $qs[$k] );
824         }
825
826         $ret = build_query( $qs );
827         $ret = trim( $ret, '?' );
828         $ret = preg_replace( '#=(&|$)#', '$1', $ret );
829         $ret = $protocol . $base . $ret . $frag;
830         $ret = rtrim( $ret, '?' );
831         return $ret;
832 }
833
834 /**
835  * Removes an item or items from a query string.
836  *
837  * @since 1.5.0
838  *
839  * @param string|array $key   Query key or keys to remove.
840  * @param bool|string  $query Optional. When false uses the current URL. Default false.
841  * @return string New URL query string.
842  */
843 function remove_query_arg( $key, $query = false ) {
844         if ( is_array( $key ) ) { // removing multiple keys
845                 foreach ( $key as $k )
846                         $query = add_query_arg( $k, false, $query );
847                 return $query;
848         }
849         return add_query_arg( $key, false, $query );
850 }
851
852 /**
853  * Returns an array of single-use query variable names that can be removed from a URL.
854  *
855  * @since 4.4.0
856  *
857  * @return array An array of parameters to remove from the URL.
858  */
859 function wp_removable_query_args() {
860         $removable_query_args = array(
861                 'activate',
862                 'activated',
863                 'approved',
864                 'deactivate',
865                 'deleted',
866                 'disabled',
867                 'enabled',
868                 'error',
869                 'hotkeys_highlight_first',
870                 'hotkeys_highlight_last',
871                 'locked',
872                 'message',
873                 'same',
874                 'saved',
875                 'settings-updated',
876                 'skipped',
877                 'spammed',
878                 'trashed',
879                 'unspammed',
880                 'untrashed',
881                 'update',
882                 'updated',
883                 'wp-post-new-reload',
884         );
885
886         /**
887          * Filters the list of query variables to remove.
888          *
889          * @since 4.2.0
890          *
891          * @param array $removable_query_args An array of query variables to remove from a URL.
892          */
893         return apply_filters( 'removable_query_args', $removable_query_args );
894 }
895
896 /**
897  * Walks the array while sanitizing the contents.
898  *
899  * @since 0.71
900  *
901  * @param array $array Array to walk while sanitizing contents.
902  * @return array Sanitized $array.
903  */
904 function add_magic_quotes( $array ) {
905         foreach ( (array) $array as $k => $v ) {
906                 if ( is_array( $v ) ) {
907                         $array[$k] = add_magic_quotes( $v );
908                 } else {
909                         $array[$k] = addslashes( $v );
910                 }
911         }
912         return $array;
913 }
914
915 /**
916  * HTTP request for URI to retrieve content.
917  *
918  * @since 1.5.1
919  *
920  * @see wp_safe_remote_get()
921  *
922  * @param string $uri URI/URL of web page to retrieve.
923  * @return false|string HTTP content. False on failure.
924  */
925 function wp_remote_fopen( $uri ) {
926         $parsed_url = @parse_url( $uri );
927
928         if ( !$parsed_url || !is_array( $parsed_url ) )
929                 return false;
930
931         $options = array();
932         $options['timeout'] = 10;
933
934         $response = wp_safe_remote_get( $uri, $options );
935
936         if ( is_wp_error( $response ) )
937                 return false;
938
939         return wp_remote_retrieve_body( $response );
940 }
941
942 /**
943  * Set up the WordPress query.
944  *
945  * @since 2.0.0
946  *
947  * @global WP       $wp_locale
948  * @global WP_Query $wp_query
949  * @global WP_Query $wp_the_query
950  *
951  * @param string|array $query_vars Default WP_Query arguments.
952  */
953 function wp( $query_vars = '' ) {
954         global $wp, $wp_query, $wp_the_query;
955         $wp->main( $query_vars );
956
957         if ( !isset($wp_the_query) )
958                 $wp_the_query = $wp_query;
959 }
960
961 /**
962  * Retrieve the description for the HTTP status.
963  *
964  * @since 2.3.0
965  *
966  * @global array $wp_header_to_desc
967  *
968  * @param int $code HTTP status code.
969  * @return string Empty string if not found, or description if found.
970  */
971 function get_status_header_desc( $code ) {
972         global $wp_header_to_desc;
973
974         $code = absint( $code );
975
976         if ( !isset( $wp_header_to_desc ) ) {
977                 $wp_header_to_desc = array(
978                         100 => 'Continue',
979                         101 => 'Switching Protocols',
980                         102 => 'Processing',
981
982                         200 => 'OK',
983                         201 => 'Created',
984                         202 => 'Accepted',
985                         203 => 'Non-Authoritative Information',
986                         204 => 'No Content',
987                         205 => 'Reset Content',
988                         206 => 'Partial Content',
989                         207 => 'Multi-Status',
990                         226 => 'IM Used',
991
992                         300 => 'Multiple Choices',
993                         301 => 'Moved Permanently',
994                         302 => 'Found',
995                         303 => 'See Other',
996                         304 => 'Not Modified',
997                         305 => 'Use Proxy',
998                         306 => 'Reserved',
999                         307 => 'Temporary Redirect',
1000                         308 => 'Permanent Redirect',
1001
1002                         400 => 'Bad Request',
1003                         401 => 'Unauthorized',
1004                         402 => 'Payment Required',
1005                         403 => 'Forbidden',
1006                         404 => 'Not Found',
1007                         405 => 'Method Not Allowed',
1008                         406 => 'Not Acceptable',
1009                         407 => 'Proxy Authentication Required',
1010                         408 => 'Request Timeout',
1011                         409 => 'Conflict',
1012                         410 => 'Gone',
1013                         411 => 'Length Required',
1014                         412 => 'Precondition Failed',
1015                         413 => 'Request Entity Too Large',
1016                         414 => 'Request-URI Too Long',
1017                         415 => 'Unsupported Media Type',
1018                         416 => 'Requested Range Not Satisfiable',
1019                         417 => 'Expectation Failed',
1020                         418 => 'I\'m a teapot',
1021                         421 => 'Misdirected Request',
1022                         422 => 'Unprocessable Entity',
1023                         423 => 'Locked',
1024                         424 => 'Failed Dependency',
1025                         426 => 'Upgrade Required',
1026                         428 => 'Precondition Required',
1027                         429 => 'Too Many Requests',
1028                         431 => 'Request Header Fields Too Large',
1029                         451 => 'Unavailable For Legal Reasons',
1030
1031                         500 => 'Internal Server Error',
1032                         501 => 'Not Implemented',
1033                         502 => 'Bad Gateway',
1034                         503 => 'Service Unavailable',
1035                         504 => 'Gateway Timeout',
1036                         505 => 'HTTP Version Not Supported',
1037                         506 => 'Variant Also Negotiates',
1038                         507 => 'Insufficient Storage',
1039                         510 => 'Not Extended',
1040                         511 => 'Network Authentication Required',
1041                 );
1042         }
1043
1044         if ( isset( $wp_header_to_desc[$code] ) )
1045                 return $wp_header_to_desc[$code];
1046         else
1047                 return '';
1048 }
1049
1050 /**
1051  * Set HTTP status header.
1052  *
1053  * @since 2.0.0
1054  * @since 4.4.0 Added the `$description` parameter.
1055  *
1056  * @see get_status_header_desc()
1057  *
1058  * @param int    $code        HTTP status code.
1059  * @param string $description Optional. A custom description for the HTTP status.
1060  */
1061 function status_header( $code, $description = '' ) {
1062         if ( ! $description ) {
1063                 $description = get_status_header_desc( $code );
1064         }
1065
1066         if ( empty( $description ) ) {
1067                 return;
1068         }
1069
1070         $protocol = wp_get_server_protocol();
1071         $status_header = "$protocol $code $description";
1072         if ( function_exists( 'apply_filters' ) )
1073
1074                 /**
1075                  * Filters an HTTP status header.
1076                  *
1077                  * @since 2.2.0
1078                  *
1079                  * @param string $status_header HTTP status header.
1080                  * @param int    $code          HTTP status code.
1081                  * @param string $description   Description for the status code.
1082                  * @param string $protocol      Server protocol.
1083                  */
1084                 $status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol );
1085
1086         @header( $status_header, true, $code );
1087 }
1088
1089 /**
1090  * Get the header information to prevent caching.
1091  *
1092  * The several different headers cover the different ways cache prevention
1093  * is handled by different browsers
1094  *
1095  * @since 2.8.0
1096  *
1097  * @return array The associative array of header names and field values.
1098  */
1099 function wp_get_nocache_headers() {
1100         $headers = array(
1101                 'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
1102                 'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
1103         );
1104
1105         if ( function_exists('apply_filters') ) {
1106                 /**
1107                  * Filters the cache-controlling headers.
1108                  *
1109                  * @since 2.8.0
1110                  *
1111                  * @see wp_get_nocache_headers()
1112                  *
1113                  * @param array $headers {
1114                  *     Header names and field values.
1115                  *
1116                  *     @type string $Expires       Expires header.
1117                  *     @type string $Cache-Control Cache-Control header.
1118                  * }
1119                  */
1120                 $headers = (array) apply_filters( 'nocache_headers', $headers );
1121         }
1122         $headers['Last-Modified'] = false;
1123         return $headers;
1124 }
1125
1126 /**
1127  * Set the headers to prevent caching for the different browsers.
1128  *
1129  * Different browsers support different nocache headers, so several
1130  * headers must be sent so that all of them get the point that no
1131  * caching should occur.
1132  *
1133  * @since 2.0.0
1134  *
1135  * @see wp_get_nocache_headers()
1136  */
1137 function nocache_headers() {
1138         $headers = wp_get_nocache_headers();
1139
1140         unset( $headers['Last-Modified'] );
1141
1142         // In PHP 5.3+, make sure we are not sending a Last-Modified header.
1143         if ( function_exists( 'header_remove' ) ) {
1144                 @header_remove( 'Last-Modified' );
1145         } else {
1146                 // In PHP 5.2, send an empty Last-Modified header, but only as a
1147                 // last resort to override a header already sent. #WP23021
1148                 foreach ( headers_list() as $header ) {
1149                         if ( 0 === stripos( $header, 'Last-Modified' ) ) {
1150                                 $headers['Last-Modified'] = '';
1151                                 break;
1152                         }
1153                 }
1154         }
1155
1156         foreach ( $headers as $name => $field_value )
1157                 @header("{$name}: {$field_value}");
1158 }
1159
1160 /**
1161  * Set the headers for caching for 10 days with JavaScript content type.
1162  *
1163  * @since 2.1.0
1164  */
1165 function cache_javascript_headers() {
1166         $expiresOffset = 10 * DAY_IN_SECONDS;
1167
1168         header( "Content-Type: text/javascript; charset=" . get_bloginfo( 'charset' ) );
1169         header( "Vary: Accept-Encoding" ); // Handle proxies
1170         header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . " GMT" );
1171 }
1172
1173 /**
1174  * Retrieve the number of database queries during the WordPress execution.
1175  *
1176  * @since 2.0.0
1177  *
1178  * @global wpdb $wpdb WordPress database abstraction object.
1179  *
1180  * @return int Number of database queries.
1181  */
1182 function get_num_queries() {
1183         global $wpdb;
1184         return $wpdb->num_queries;
1185 }
1186
1187 /**
1188  * Whether input is yes or no.
1189  *
1190  * Must be 'y' to be true.
1191  *
1192  * @since 1.0.0
1193  *
1194  * @param string $yn Character string containing either 'y' (yes) or 'n' (no).
1195  * @return bool True if yes, false on anything else.
1196  */
1197 function bool_from_yn( $yn ) {
1198         return ( strtolower( $yn ) == 'y' );
1199 }
1200
1201 /**
1202  * Load the feed template from the use of an action hook.
1203  *
1204  * If the feed action does not have a hook, then the function will die with a
1205  * message telling the visitor that the feed is not valid.
1206  *
1207  * It is better to only have one hook for each feed.
1208  *
1209  * @since 2.1.0
1210  *
1211  * @global WP_Query $wp_query Used to tell if the use a comment feed.
1212  */
1213 function do_feed() {
1214         global $wp_query;
1215
1216         // Determine if we are looking at the main comment feed
1217         $is_main_comments_feed = ( $wp_query->is_comment_feed() && ! $wp_query->is_singular() );
1218
1219         /*
1220          * Check the queried object for the existence of posts if it is not a feed for an archive,
1221          * search result, or main comments. By checking for the absense of posts we can prevent rendering the feed
1222          * templates at invalid endpoints. e.g.) /wp-content/plugins/feed/
1223          */
1224         if ( ! $wp_query->have_posts() && ! ( $wp_query->is_archive() || $wp_query->is_search() || $is_main_comments_feed ) ) {
1225                 wp_die( __( 'ERROR: This is not a valid feed.' ), '', array( 'response' => 404 ) );
1226         }
1227
1228         $feed = get_query_var( 'feed' );
1229
1230         // Remove the pad, if present.
1231         $feed = preg_replace( '/^_+/', '', $feed );
1232
1233         if ( $feed == '' || $feed == 'feed' )
1234                 $feed = get_default_feed();
1235
1236         if ( ! has_action( "do_feed_{$feed}" ) ) {
1237                 wp_die( __( 'ERROR: This is not a valid feed template.' ), '', array( 'response' => 404 ) );
1238         }
1239
1240         /**
1241          * Fires once the given feed is loaded.
1242          *
1243          * The dynamic portion of the hook name, `$feed`, refers to the feed template name.
1244          * Possible values include: 'rdf', 'rss', 'rss2', and 'atom'.
1245          *
1246          * @since 2.1.0
1247          * @since 4.4.0 The `$feed` parameter was added.
1248          *
1249          * @param bool   $is_comment_feed Whether the feed is a comment feed.
1250          * @param string $feed            The feed name.
1251          */
1252         do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed );
1253 }
1254
1255 /**
1256  * Load the RDF RSS 0.91 Feed template.
1257  *
1258  * @since 2.1.0
1259  *
1260  * @see load_template()
1261  */
1262 function do_feed_rdf() {
1263         load_template( ABSPATH . WPINC . '/feed-rdf.php' );
1264 }
1265
1266 /**
1267  * Load the RSS 1.0 Feed Template.
1268  *
1269  * @since 2.1.0
1270  *
1271  * @see load_template()
1272  */
1273 function do_feed_rss() {
1274         load_template( ABSPATH . WPINC . '/feed-rss.php' );
1275 }
1276
1277 /**
1278  * Load either the RSS2 comment feed or the RSS2 posts feed.
1279  *
1280  * @since 2.1.0
1281  *
1282  * @see load_template()
1283  *
1284  * @param bool $for_comments True for the comment feed, false for normal feed.
1285  */
1286 function do_feed_rss2( $for_comments ) {
1287         if ( $for_comments )
1288                 load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
1289         else
1290                 load_template( ABSPATH . WPINC . '/feed-rss2.php' );
1291 }
1292
1293 /**
1294  * Load either Atom comment feed or Atom posts feed.
1295  *
1296  * @since 2.1.0
1297  *
1298  * @see load_template()
1299  *
1300  * @param bool $for_comments True for the comment feed, false for normal feed.
1301  */
1302 function do_feed_atom( $for_comments ) {
1303         if ($for_comments)
1304                 load_template( ABSPATH . WPINC . '/feed-atom-comments.php');
1305         else
1306                 load_template( ABSPATH . WPINC . '/feed-atom.php' );
1307 }
1308
1309 /**
1310  * Display the robots.txt file content.
1311  *
1312  * The echo content should be with usage of the permalinks or for creating the
1313  * robots.txt file.
1314  *
1315  * @since 2.1.0
1316  */
1317 function do_robots() {
1318         header( 'Content-Type: text/plain; charset=utf-8' );
1319
1320         /**
1321          * Fires when displaying the robots.txt file.
1322          *
1323          * @since 2.1.0
1324          */
1325         do_action( 'do_robotstxt' );
1326
1327         $output = "User-agent: *\n";
1328         $public = get_option( 'blog_public' );
1329         if ( '0' == $public ) {
1330                 $output .= "Disallow: /\n";
1331         } else {
1332                 $site_url = parse_url( site_url() );
1333                 $path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : '';
1334                 $output .= "Disallow: $path/wp-admin/\n";
1335                 $output .= "Allow: $path/wp-admin/admin-ajax.php\n";
1336         }
1337
1338         /**
1339          * Filters the robots.txt output.
1340          *
1341          * @since 3.0.0
1342          *
1343          * @param string $output Robots.txt output.
1344          * @param bool   $public Whether the site is considered "public".
1345          */
1346         echo apply_filters( 'robots_txt', $output, $public );
1347 }
1348
1349 /**
1350  * Test whether WordPress is already installed.
1351  *
1352  * The cache will be checked first. If you have a cache plugin, which saves
1353  * the cache values, then this will work. If you use the default WordPress
1354  * cache, and the database goes away, then you might have problems.
1355  *
1356  * Checks for the 'siteurl' option for whether WordPress is installed.
1357  *
1358  * @since 2.1.0
1359  *
1360  * @global wpdb $wpdb WordPress database abstraction object.
1361  *
1362  * @return bool Whether the site is already installed.
1363  */
1364 function is_blog_installed() {
1365         global $wpdb;
1366
1367         /*
1368          * Check cache first. If options table goes away and we have true
1369          * cached, oh well.
1370          */
1371         if ( wp_cache_get( 'is_blog_installed' ) )
1372                 return true;
1373
1374         $suppress = $wpdb->suppress_errors();
1375         if ( ! wp_installing() ) {
1376                 $alloptions = wp_load_alloptions();
1377         }
1378         // If siteurl is not set to autoload, check it specifically
1379         if ( !isset( $alloptions['siteurl'] ) )
1380                 $installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
1381         else
1382                 $installed = $alloptions['siteurl'];
1383         $wpdb->suppress_errors( $suppress );
1384
1385         $installed = !empty( $installed );
1386         wp_cache_set( 'is_blog_installed', $installed );
1387
1388         if ( $installed )
1389                 return true;
1390
1391         // If visiting repair.php, return true and let it take over.
1392         if ( defined( 'WP_REPAIRING' ) )
1393                 return true;
1394
1395         $suppress = $wpdb->suppress_errors();
1396
1397         /*
1398          * Loop over the WP tables. If none exist, then scratch install is allowed.
1399          * If one or more exist, suggest table repair since we got here because the
1400          * options table could not be accessed.
1401          */
1402         $wp_tables = $wpdb->tables();
1403         foreach ( $wp_tables as $table ) {
1404                 // The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
1405                 if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
1406                         continue;
1407                 if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table )
1408                         continue;
1409
1410                 if ( ! $wpdb->get_results( "DESCRIBE $table;" ) )
1411                         continue;
1412
1413                 // One or more tables exist. We are insane.
1414
1415                 wp_load_translations_early();
1416
1417                 // Die with a DB error.
1418                 $wpdb->error = sprintf(
1419                         /* translators: %s: database repair URL */
1420                         __( 'One or more database tables are unavailable. The database may need to be <a href="%s">repaired</a>.' ),
1421                         'maint/repair.php?referrer=is_blog_installed'
1422                 );
1423
1424                 dead_db();
1425         }
1426
1427         $wpdb->suppress_errors( $suppress );
1428
1429         wp_cache_set( 'is_blog_installed', false );
1430
1431         return false;
1432 }
1433
1434 /**
1435  * Retrieve URL with nonce added to URL query.
1436  *
1437  * @since 2.0.4
1438  *
1439  * @param string     $actionurl URL to add nonce action.
1440  * @param int|string $action    Optional. Nonce action name. Default -1.
1441  * @param string     $name      Optional. Nonce name. Default '_wpnonce'.
1442  * @return string Escaped URL with nonce action added.
1443  */
1444 function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
1445         $actionurl = str_replace( '&amp;', '&', $actionurl );
1446         return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
1447 }
1448
1449 /**
1450  * Retrieve or display nonce hidden field for forms.
1451  *
1452  * The nonce field is used to validate that the contents of the form came from
1453  * the location on the current site and not somewhere else. The nonce does not
1454  * offer absolute protection, but should protect against most cases. It is very
1455  * important to use nonce field in forms.
1456  *
1457  * The $action and $name are optional, but if you want to have better security,
1458  * it is strongly suggested to set those two parameters. It is easier to just
1459  * call the function without any parameters, because validation of the nonce
1460  * doesn't require any parameters, but since crackers know what the default is
1461  * it won't be difficult for them to find a way around your nonce and cause
1462  * damage.
1463  *
1464  * The input name will be whatever $name value you gave. The input value will be
1465  * the nonce creation value.
1466  *
1467  * @since 2.0.4
1468  *
1469  * @param int|string $action  Optional. Action name. Default -1.
1470  * @param string     $name    Optional. Nonce name. Default '_wpnonce'.
1471  * @param bool       $referer Optional. Whether to set the referer field for validation. Default true.
1472  * @param bool       $echo    Optional. Whether to display or return hidden form field. Default true.
1473  * @return string Nonce field HTML markup.
1474  */
1475 function wp_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
1476         $name = esc_attr( $name );
1477         $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />';
1478
1479         if ( $referer )
1480                 $nonce_field .= wp_referer_field( false );
1481
1482         if ( $echo )
1483                 echo $nonce_field;
1484
1485         return $nonce_field;
1486 }
1487
1488 /**
1489  * Retrieve or display referer hidden field for forms.
1490  *
1491  * The referer link is the current Request URI from the server super global. The
1492  * input name is '_wp_http_referer', in case you wanted to check manually.
1493  *
1494  * @since 2.0.4
1495  *
1496  * @param bool $echo Optional. Whether to echo or return the referer field. Default true.
1497  * @return string Referer field HTML markup.
1498  */
1499 function wp_referer_field( $echo = true ) {
1500         $referer_field = '<input type="hidden" name="_wp_http_referer" value="'. esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />';
1501
1502         if ( $echo )
1503                 echo $referer_field;
1504         return $referer_field;
1505 }
1506
1507 /**
1508  * Retrieve or display original referer hidden field for forms.
1509  *
1510  * The input name is '_wp_original_http_referer' and will be either the same
1511  * value of wp_referer_field(), if that was posted already or it will be the
1512  * current page, if it doesn't exist.
1513  *
1514  * @since 2.0.4
1515  *
1516  * @param bool   $echo         Optional. Whether to echo the original http referer. Default true.
1517  * @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to.
1518  *                             Default 'current'.
1519  * @return string Original referer field.
1520  */
1521 function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) {
1522         if ( ! $ref = wp_get_original_referer() ) {
1523                 $ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] );
1524         }
1525         $orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( $ref ) . '" />';
1526         if ( $echo )
1527                 echo $orig_referer_field;
1528         return $orig_referer_field;
1529 }
1530
1531 /**
1532  * Retrieve referer from '_wp_http_referer' or HTTP referer.
1533  *
1534  * If it's the same as the current request URL, will return false.
1535  *
1536  * @since 2.0.4
1537  *
1538  * @return false|string False on failure. Referer URL on success.
1539  */
1540 function wp_get_referer() {
1541         if ( ! function_exists( 'wp_validate_redirect' ) ) {
1542                 return false;
1543         }
1544
1545         $ref = wp_get_raw_referer();
1546
1547         if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) && $ref !== home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) ) {
1548                 return wp_validate_redirect( $ref, false );
1549         }
1550
1551         return false;
1552 }
1553
1554 /**
1555  * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
1556  *
1557  * Do not use for redirects, use wp_get_referer() instead.
1558  *
1559  * @since 4.5.0
1560  *
1561  * @return string|false Referer URL on success, false on failure.
1562  */
1563 function wp_get_raw_referer() {
1564         if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
1565                 return wp_unslash( $_REQUEST['_wp_http_referer'] );
1566         } else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
1567                 return wp_unslash( $_SERVER['HTTP_REFERER'] );
1568         }
1569
1570         return false;
1571 }
1572
1573 /**
1574  * Retrieve original referer that was posted, if it exists.
1575  *
1576  * @since 2.0.4
1577  *
1578  * @return string|false False if no original referer or original referer if set.
1579  */
1580 function wp_get_original_referer() {
1581         if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) )
1582                 return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
1583         return false;
1584 }
1585
1586 /**
1587  * Recursive directory creation based on full path.
1588  *
1589  * Will attempt to set permissions on folders.
1590  *
1591  * @since 2.0.1
1592  *
1593  * @param string $target Full path to attempt to create.
1594  * @return bool Whether the path was created. True if path already exists.
1595  */
1596 function wp_mkdir_p( $target ) {
1597         $wrapper = null;
1598
1599         // Strip the protocol.
1600         if ( wp_is_stream( $target ) ) {
1601                 list( $wrapper, $target ) = explode( '://', $target, 2 );
1602         }
1603
1604         // From php.net/mkdir user contributed notes.
1605         $target = str_replace( '//', '/', $target );
1606
1607         // Put the wrapper back on the target.
1608         if ( $wrapper !== null ) {
1609                 $target = $wrapper . '://' . $target;
1610         }
1611
1612         /*
1613          * Safe mode fails with a trailing slash under certain PHP versions.
1614          * Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
1615          */
1616         $target = rtrim($target, '/');
1617         if ( empty($target) )
1618                 $target = '/';
1619
1620         if ( file_exists( $target ) )
1621                 return @is_dir( $target );
1622
1623         // We need to find the permissions of the parent folder that exists and inherit that.
1624         $target_parent = dirname( $target );
1625         while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
1626                 $target_parent = dirname( $target_parent );
1627         }
1628
1629         // Get the permission bits.
1630         if ( $stat = @stat( $target_parent ) ) {
1631                 $dir_perms = $stat['mode'] & 0007777;
1632         } else {
1633                 $dir_perms = 0777;
1634         }
1635
1636         if ( @mkdir( $target, $dir_perms, true ) ) {
1637
1638                 /*
1639                  * If a umask is set that modifies $dir_perms, we'll have to re-set
1640                  * the $dir_perms correctly with chmod()
1641                  */
1642                 if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
1643                         $folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
1644                         for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) {
1645                                 @chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
1646                         }
1647                 }
1648
1649                 return true;
1650         }
1651
1652         return false;
1653 }
1654
1655 /**
1656  * Test if a give filesystem path is absolute.
1657  *
1658  * For example, '/foo/bar', or 'c:\windows'.
1659  *
1660  * @since 2.5.0
1661  *
1662  * @param string $path File path.
1663  * @return bool True if path is absolute, false is not absolute.
1664  */
1665 function path_is_absolute( $path ) {
1666         /*
1667          * This is definitive if true but fails if $path does not exist or contains
1668          * a symbolic link.
1669          */
1670         if ( realpath($path) == $path )
1671                 return true;
1672
1673         if ( strlen($path) == 0 || $path[0] == '.' )
1674                 return false;
1675
1676         // Windows allows absolute paths like this.
1677         if ( preg_match('#^[a-zA-Z]:\\\\#', $path) )
1678                 return true;
1679
1680         // A path starting with / or \ is absolute; anything else is relative.
1681         return ( $path[0] == '/' || $path[0] == '\\' );
1682 }
1683
1684 /**
1685  * Join two filesystem paths together.
1686  *
1687  * For example, 'give me $path relative to $base'. If the $path is absolute,
1688  * then it the full path is returned.
1689  *
1690  * @since 2.5.0
1691  *
1692  * @param string $base Base path.
1693  * @param string $path Path relative to $base.
1694  * @return string The path with the base or absolute path.
1695  */
1696 function path_join( $base, $path ) {
1697         if ( path_is_absolute($path) )
1698                 return $path;
1699
1700         return rtrim($base, '/') . '/' . ltrim($path, '/');
1701 }
1702
1703 /**
1704  * Normalize a filesystem path.
1705  *
1706  * On windows systems, replaces backslashes with forward slashes
1707  * and forces upper-case drive letters.
1708  * Allows for two leading slashes for Windows network shares, but
1709  * ensures that all other duplicate slashes are reduced to a single.
1710  *
1711  * @since 3.9.0
1712  * @since 4.4.0 Ensures upper-case drive letters on Windows systems.
1713  * @since 4.5.0 Allows for Windows network shares.
1714  *
1715  * @param string $path Path to normalize.
1716  * @return string Normalized path.
1717  */
1718 function wp_normalize_path( $path ) {
1719         $path = str_replace( '\\', '/', $path );
1720         $path = preg_replace( '|(?<=.)/+|', '/', $path );
1721         if ( ':' === substr( $path, 1, 1 ) ) {
1722                 $path = ucfirst( $path );
1723         }
1724         return $path;
1725 }
1726
1727 /**
1728  * Determine a writable directory for temporary files.
1729  *
1730  * Function's preference is the return value of sys_get_temp_dir(),
1731  * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
1732  * before finally defaulting to /tmp/
1733  *
1734  * In the event that this function does not find a writable location,
1735  * It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.
1736  *
1737  * @since 2.5.0
1738  *
1739  * @staticvar string $temp
1740  *
1741  * @return string Writable temporary directory.
1742  */
1743 function get_temp_dir() {
1744         static $temp = '';
1745         if ( defined('WP_TEMP_DIR') )
1746                 return trailingslashit(WP_TEMP_DIR);
1747
1748         if ( $temp )
1749                 return trailingslashit( $temp );
1750
1751         if ( function_exists('sys_get_temp_dir') ) {
1752                 $temp = sys_get_temp_dir();
1753                 if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1754                         return trailingslashit( $temp );
1755         }
1756
1757         $temp = ini_get('upload_tmp_dir');
1758         if ( @is_dir( $temp ) && wp_is_writable( $temp ) )
1759                 return trailingslashit( $temp );
1760
1761         $temp = WP_CONTENT_DIR . '/';
1762         if ( is_dir( $temp ) && wp_is_writable( $temp ) )
1763                 return $temp;
1764
1765         return '/tmp/';
1766 }
1767
1768 /**
1769  * Determine if a directory is writable.
1770  *
1771  * This function is used to work around certain ACL issues in PHP primarily
1772  * affecting Windows Servers.
1773  *
1774  * @since 3.6.0
1775  *
1776  * @see win_is_writable()
1777  *
1778  * @param string $path Path to check for write-ability.
1779  * @return bool Whether the path is writable.
1780  */
1781 function wp_is_writable( $path ) {
1782         if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) )
1783                 return win_is_writable( $path );
1784         else
1785                 return @is_writable( $path );
1786 }
1787
1788 /**
1789  * Workaround for Windows bug in is_writable() function
1790  *
1791  * PHP has issues with Windows ACL's for determine if a
1792  * directory is writable or not, this works around them by
1793  * checking the ability to open files rather than relying
1794  * upon PHP to interprate the OS ACL.
1795  *
1796  * @since 2.8.0
1797  *
1798  * @see https://bugs.php.net/bug.php?id=27609
1799  * @see https://bugs.php.net/bug.php?id=30931
1800  *
1801  * @param string $path Windows path to check for write-ability.
1802  * @return bool Whether the path is writable.
1803  */
1804 function win_is_writable( $path ) {
1805
1806         if ( $path[strlen( $path ) - 1] == '/' ) { // if it looks like a directory, check a random file within the directory
1807                 return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp');
1808         } elseif ( is_dir( $path ) ) { // If it's a directory (and not a file) check a random file within the directory
1809                 return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' );
1810         }
1811         // check tmp file for read/write capabilities
1812         $should_delete_tmp_file = !file_exists( $path );
1813         $f = @fopen( $path, 'a' );
1814         if ( $f === false )
1815                 return false;
1816         fclose( $f );
1817         if ( $should_delete_tmp_file )
1818                 unlink( $path );
1819         return true;
1820 }
1821
1822 /**
1823  * Retrieves uploads directory information.
1824  *
1825  * Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory.
1826  * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases
1827  * when not uploading files.
1828  *
1829  * @since 4.5.0
1830  *
1831  * @see wp_upload_dir()
1832  *
1833  * @return array See wp_upload_dir() for description.
1834  */
1835 function wp_get_upload_dir() {
1836         return wp_upload_dir( null, false );
1837 }
1838
1839 /**
1840  * Get an array containing the current upload directory's path and url.
1841  *
1842  * Checks the 'upload_path' option, which should be from the web root folder,
1843  * and if it isn't empty it will be used. If it is empty, then the path will be
1844  * 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will
1845  * override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.
1846  *
1847  * The upload URL path is set either by the 'upload_url_path' option or by using
1848  * the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.
1849  *
1850  * If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in
1851  * the administration settings panel), then the time will be used. The format
1852  * will be year first and then month.
1853  *
1854  * If the path couldn't be created, then an error will be returned with the key
1855  * 'error' containing the error message. The error suggests that the parent
1856  * directory is not writable by the server.
1857  *
1858  * On success, the returned array will have many indices:
1859  * 'path' - base directory and sub directory or full path to upload directory.
1860  * 'url' - base url and sub directory or absolute URL to upload directory.
1861  * 'subdir' - sub directory if uploads use year/month folders option is on.
1862  * 'basedir' - path without subdir.
1863  * 'baseurl' - URL path without subdir.
1864  * 'error' - false or error message.
1865  *
1866  * @since 2.0.0
1867  * @uses _wp_upload_dir()
1868  *
1869  * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1870  * @param bool   $create_dir Optional. Whether to check and create the uploads directory.
1871  *                           Default true for backward compatibility.
1872  * @param bool   $refresh_cache Optional. Whether to refresh the cache. Default false.
1873  * @return array See above for description.
1874  */
1875 function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
1876         static $cache = array(), $tested_paths = array();
1877
1878         $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
1879
1880         if ( $refresh_cache || empty( $cache[ $key ] ) ) {
1881                 $cache[ $key ] = _wp_upload_dir( $time );
1882         }
1883
1884         /**
1885          * Filters the uploads directory data.
1886          *
1887          * @since 2.0.0
1888          *
1889          * @param array $uploads Array of upload directory data with keys of 'path',
1890          *                       'url', 'subdir, 'basedir', and 'error'.
1891          */
1892         $uploads = apply_filters( 'upload_dir', $cache[ $key ] );
1893
1894         if ( $create_dir ) {
1895                 $path = $uploads['path'];
1896
1897                 if ( array_key_exists( $path, $tested_paths ) ) {
1898                         $uploads['error'] = $tested_paths[ $path ];
1899                 } else {
1900                         if ( ! wp_mkdir_p( $path ) ) {
1901                                 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
1902                                         $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
1903                                 } else {
1904                                         $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
1905                                 }
1906
1907                                 $uploads['error'] = sprintf(
1908                                         /* translators: %s: directory path */
1909                                         __( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
1910                                         esc_html( $error_path )
1911                                 );
1912                         }
1913
1914                         $tested_paths[ $path ] = $uploads['error'];
1915                 }
1916         }
1917
1918         return $uploads;
1919 }
1920
1921 /**
1922  * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
1923  *
1924  * @access private
1925  *
1926  * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
1927  * @return array See wp_upload_dir()
1928  */
1929 function _wp_upload_dir( $time = null ) {
1930         $siteurl = get_option( 'siteurl' );
1931         $upload_path = trim( get_option( 'upload_path' ) );
1932
1933         if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
1934                 $dir = WP_CONTENT_DIR . '/uploads';
1935         } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
1936                 // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
1937                 $dir = path_join( ABSPATH, $upload_path );
1938         } else {
1939                 $dir = $upload_path;
1940         }
1941
1942         if ( !$url = get_option( 'upload_url_path' ) ) {
1943                 if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
1944                         $url = WP_CONTENT_URL . '/uploads';
1945                 else
1946                         $url = trailingslashit( $siteurl ) . $upload_path;
1947         }
1948
1949         /*
1950          * Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
1951          * We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
1952          */
1953         if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
1954                 $dir = ABSPATH . UPLOADS;
1955                 $url = trailingslashit( $siteurl ) . UPLOADS;
1956         }
1957
1958         // If multisite (and if not the main site in a post-MU network)
1959         if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
1960
1961                 if ( ! get_site_option( 'ms_files_rewriting' ) ) {
1962                         /*
1963                          * If ms-files rewriting is disabled (networks created post-3.5), it is fairly
1964                          * straightforward: Append sites/%d if we're not on the main site (for post-MU
1965                          * networks). (The extra directory prevents a four-digit ID from conflicting with
1966                          * a year-based directory for the main site. But if a MU-era network has disabled
1967                          * ms-files rewriting manually, they don't need the extra directory, as they never
1968                          * had wp-content/uploads for the main site.)
1969                          */
1970
1971                         if ( defined( 'MULTISITE' ) )
1972                                 $ms_dir = '/sites/' . get_current_blog_id();
1973                         else
1974                                 $ms_dir = '/' . get_current_blog_id();
1975
1976                         $dir .= $ms_dir;
1977                         $url .= $ms_dir;
1978
1979                 } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
1980                         /*
1981                          * Handle the old-form ms-files.php rewriting if the network still has that enabled.
1982                          * When ms-files rewriting is enabled, then we only listen to UPLOADS when:
1983                          * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used
1984                          *    there, and
1985                          * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect
1986                          *    the original blog ID.
1987                          *
1988                          * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
1989                          * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
1990                          * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
1991                          * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
1992                          */
1993
1994                         if ( defined( 'BLOGUPLOADDIR' ) )
1995                                 $dir = untrailingslashit( BLOGUPLOADDIR );
1996                         else
1997                                 $dir = ABSPATH . UPLOADS;
1998                         $url = trailingslashit( $siteurl ) . 'files';
1999                 }
2000         }
2001
2002         $basedir = $dir;
2003         $baseurl = $url;
2004
2005         $subdir = '';
2006         if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
2007                 // Generate the yearly and monthly dirs
2008                 if ( !$time )
2009                         $time = current_time( 'mysql' );
2010                 $y = substr( $time, 0, 4 );
2011                 $m = substr( $time, 5, 2 );
2012                 $subdir = "/$y/$m";
2013         }
2014
2015         $dir .= $subdir;
2016         $url .= $subdir;
2017
2018         return array(
2019                 'path'    => $dir,
2020                 'url'     => $url,
2021                 'subdir'  => $subdir,
2022                 'basedir' => $basedir,
2023                 'baseurl' => $baseurl,
2024                 'error'   => false,
2025         );
2026 }
2027
2028 /**
2029  * Get a filename that is sanitized and unique for the given directory.
2030  *
2031  * If the filename is not unique, then a number will be added to the filename
2032  * before the extension, and will continue adding numbers until the filename is
2033  * unique.
2034  *
2035  * The callback is passed three parameters, the first one is the directory, the
2036  * second is the filename, and the third is the extension.
2037  *
2038  * @since 2.5.0
2039  *
2040  * @param string   $dir                      Directory.
2041  * @param string   $filename                 File name.
2042  * @param callable $unique_filename_callback Callback. Default null.
2043  * @return string New filename, if given wasn't unique.
2044  */
2045 function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) {
2046         // Sanitize the file name before we begin processing.
2047         $filename = sanitize_file_name($filename);
2048
2049         // Separate the filename into a name and extension.
2050         $ext = pathinfo( $filename, PATHINFO_EXTENSION );
2051         $name = pathinfo( $filename, PATHINFO_BASENAME );
2052         if ( $ext ) {
2053                 $ext = '.' . $ext;
2054         }
2055
2056         // Edge case: if file is named '.ext', treat as an empty name.
2057         if ( $name === $ext ) {
2058                 $name = '';
2059         }
2060
2061         /*
2062          * Increment the file number until we have a unique file to save in $dir.
2063          * Use callback if supplied.
2064          */
2065         if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
2066                 $filename = call_user_func( $unique_filename_callback, $dir, $name, $ext );
2067         } else {
2068                 $number = '';
2069
2070                 // Change '.ext' to lower case.
2071                 if ( $ext && strtolower($ext) != $ext ) {
2072                         $ext2 = strtolower($ext);
2073                         $filename2 = preg_replace( '|' . preg_quote($ext) . '$|', $ext2, $filename );
2074
2075                         // Check for both lower and upper case extension or image sub-sizes may be overwritten.
2076                         while ( file_exists($dir . "/$filename") || file_exists($dir . "/$filename2") ) {
2077                                 $new_number = $number + 1;
2078                                 $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename );
2079                                 $filename2 = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 );
2080                                 $number = $new_number;
2081                         }
2082
2083                         /**
2084                          * Filters the result when generating a unique file name.
2085                          *
2086                          * @since 4.5.0
2087                          *
2088                          * @param string        $filename                 Unique file name.
2089                          * @param string        $ext                      File extension, eg. ".png".
2090                          * @param string        $dir                      Directory path.
2091                          * @param callable|null $unique_filename_callback Callback function that generates the unique file name.
2092                          */
2093                         return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback );
2094                 }
2095
2096                 while ( file_exists( $dir . "/$filename" ) ) {
2097                         if ( '' == "$number$ext" ) {
2098                                 $filename = "$filename-" . ++$number;
2099                         } else {
2100                                 $filename = str_replace( array( "-$number$ext", "$number$ext" ), "-" . ++$number . $ext, $filename );
2101                         }
2102                 }
2103         }
2104
2105         /** This filter is documented in wp-includes/functions.php */
2106         return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback );
2107 }
2108
2109 /**
2110  * Create a file in the upload folder with given content.
2111  *
2112  * If there is an error, then the key 'error' will exist with the error message.
2113  * If success, then the key 'file' will have the unique file path, the 'url' key
2114  * will have the link to the new file. and the 'error' key will be set to false.
2115  *
2116  * This function will not move an uploaded file to the upload folder. It will
2117  * create a new file with the content in $bits parameter. If you move the upload
2118  * file, read the content of the uploaded file, and then you can give the
2119  * filename and content to this function, which will add it to the upload
2120  * folder.
2121  *
2122  * The permissions will be set on the new file automatically by this function.
2123  *
2124  * @since 2.0.0
2125  *
2126  * @param string       $name       Filename.
2127  * @param null|string  $deprecated Never used. Set to null.
2128  * @param mixed        $bits       File content
2129  * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
2130  * @return array
2131  */
2132 function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
2133         if ( !empty( $deprecated ) )
2134                 _deprecated_argument( __FUNCTION__, '2.0.0' );
2135
2136         if ( empty( $name ) )
2137                 return array( 'error' => __( 'Empty filename' ) );
2138
2139         $wp_filetype = wp_check_filetype( $name );
2140         if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) )
2141                 return array( 'error' => __( 'Invalid file type' ) );
2142
2143         $upload = wp_upload_dir( $time );
2144
2145         if ( $upload['error'] !== false )
2146                 return $upload;
2147
2148         /**
2149          * Filters whether to treat the upload bits as an error.
2150          *
2151          * Passing a non-array to the filter will effectively short-circuit preparing
2152          * the upload bits, returning that value instead.
2153          *
2154          * @since 3.0.0
2155          *
2156          * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
2157          */
2158         $upload_bits_error = apply_filters( 'wp_upload_bits', array( 'name' => $name, 'bits' => $bits, 'time' => $time ) );
2159         if ( !is_array( $upload_bits_error ) ) {
2160                 $upload[ 'error' ] = $upload_bits_error;
2161                 return $upload;
2162         }
2163
2164         $filename = wp_unique_filename( $upload['path'], $name );
2165
2166         $new_file = $upload['path'] . "/$filename";
2167         if ( ! wp_mkdir_p( dirname( $new_file ) ) ) {
2168                 if ( 0 === strpos( $upload['basedir'], ABSPATH ) )
2169                         $error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir'];
2170                 else
2171                         $error_path = basename( $upload['basedir'] ) . $upload['subdir'];
2172
2173                 $message = sprintf(
2174                         /* translators: %s: directory path */
2175                         __( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
2176                         $error_path
2177                 );
2178                 return array( 'error' => $message );
2179         }
2180
2181         $ifp = @ fopen( $new_file, 'wb' );
2182         if ( ! $ifp )
2183                 return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
2184
2185         @fwrite( $ifp, $bits );
2186         fclose( $ifp );
2187         clearstatcache();
2188
2189         // Set correct file permissions
2190         $stat = @ stat( dirname( $new_file ) );
2191         $perms = $stat['mode'] & 0007777;
2192         $perms = $perms & 0000666;
2193         @ chmod( $new_file, $perms );
2194         clearstatcache();
2195
2196         // Compute the URL
2197         $url = $upload['url'] . "/$filename";
2198
2199         /** This filter is documented in wp-admin/includes/file.php */
2200         return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false ), 'sideload' );
2201 }
2202
2203 /**
2204  * Retrieve the file type based on the extension name.
2205  *
2206  * @since 2.5.0
2207  *
2208  * @param string $ext The extension to search.
2209  * @return string|void The file type, example: audio, video, document, spreadsheet, etc.
2210  */
2211 function wp_ext2type( $ext ) {
2212         $ext = strtolower( $ext );
2213
2214         $ext2type = wp_get_ext_types();
2215         foreach ( $ext2type as $type => $exts )
2216                 if ( in_array( $ext, $exts ) )
2217                         return $type;
2218 }
2219
2220 /**
2221  * Retrieve the file type from the file name.
2222  *
2223  * You can optionally define the mime array, if needed.
2224  *
2225  * @since 2.0.4
2226  *
2227  * @param string $filename File name or path.
2228  * @param array  $mimes    Optional. Key is the file extension with value as the mime type.
2229  * @return array Values with extension first and mime type.
2230  */
2231 function wp_check_filetype( $filename, $mimes = null ) {
2232         if ( empty($mimes) )
2233                 $mimes = get_allowed_mime_types();
2234         $type = false;
2235         $ext = false;
2236
2237         foreach ( $mimes as $ext_preg => $mime_match ) {
2238                 $ext_preg = '!\.(' . $ext_preg . ')$!i';
2239                 if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
2240                         $type = $mime_match;
2241                         $ext = $ext_matches[1];
2242                         break;
2243                 }
2244         }
2245
2246         return compact( 'ext', 'type' );
2247 }
2248
2249 /**
2250  * Attempt to determine the real file type of a file.
2251  *
2252  * If unable to, the file name extension will be used to determine type.
2253  *
2254  * If it's determined that the extension does not match the file's real type,
2255  * then the "proper_filename" value will be set with a proper filename and extension.
2256  *
2257  * Currently this function only supports renaming images validated via wp_get_image_mime().
2258  *
2259  * @since 3.0.0
2260  *
2261  * @param string $file     Full path to the file.
2262  * @param string $filename The name of the file (may differ from $file due to $file being
2263  *                         in a tmp directory).
2264  * @param array   $mimes   Optional. Key is the file extension with value as the mime type.
2265  * @return array Values for the extension, MIME, and either a corrected filename or false
2266  *               if original $filename is valid.
2267  */
2268 function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
2269         $proper_filename = false;
2270
2271         // Do basic extension validation and MIME mapping
2272         $wp_filetype = wp_check_filetype( $filename, $mimes );
2273         $ext = $wp_filetype['ext'];
2274         $type = $wp_filetype['type'];
2275
2276         // We can't do any further validation without a file to work with
2277         if ( ! file_exists( $file ) ) {
2278                 return compact( 'ext', 'type', 'proper_filename' );
2279         }
2280
2281         // Validate image types.
2282         if ( $type && 0 === strpos( $type, 'image/' ) ) {
2283
2284                 // Attempt to figure out what type of image it actually is
2285                 $real_mime = wp_get_image_mime( $file );
2286
2287                 if ( ! $real_mime ) {
2288                         $type = $ext = false;
2289                 } elseif ( $real_mime != $type ) {
2290                         /**
2291                          * Filters the list mapping image mime types to their respective extensions.
2292                          *
2293                          * @since 3.0.0
2294                          *
2295                          * @param  array $mime_to_ext Array of image mime types and their matching extensions.
2296                          */
2297                         $mime_to_ext = apply_filters( 'getimagesize_mimes_to_exts', array(
2298                                 'image/jpeg' => 'jpg',
2299                                 'image/png'  => 'png',
2300                                 'image/gif'  => 'gif',
2301                                 'image/bmp'  => 'bmp',
2302                                 'image/tiff' => 'tif',
2303                         ) );
2304
2305                         // Replace whatever is after the last period in the filename with the correct extension
2306                         if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
2307                                 $filename_parts = explode( '.', $filename );
2308                                 array_pop( $filename_parts );
2309                                 $filename_parts[] = $mime_to_ext[ $real_mime ];
2310                                 $new_filename = implode( '.', $filename_parts );
2311
2312                                 if ( $new_filename != $filename ) {
2313                                         $proper_filename = $new_filename; // Mark that it changed
2314                                 }
2315                                 // Redefine the extension / MIME
2316                                 $wp_filetype = wp_check_filetype( $new_filename, $mimes );
2317                                 $ext = $wp_filetype['ext'];
2318                                 $type = $wp_filetype['type'];
2319                         } else {
2320                                 $type = $ext = false;
2321                         }
2322                 }
2323         } elseif ( function_exists( 'finfo_file' ) ) {
2324                 // Use finfo_file if available to validate non-image files.
2325                 $finfo = finfo_open( FILEINFO_MIME_TYPE );
2326                 $real_mime = finfo_file( $finfo, $file );
2327                 finfo_close( $finfo );
2328
2329                 // If the extension does not match the file's real type, return false.
2330                 if ( $real_mime !== $type ) {
2331                         $type = $ext = false;
2332                 }
2333         }
2334
2335         /**
2336          * Filters the "real" file type of the given file.
2337          *
2338          * @since 3.0.0
2339          *
2340          * @param array  $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
2341          *                                          'proper_filename' keys.
2342          * @param string $file                      Full path to the file.
2343          * @param string $filename                  The name of the file (may differ from $file due to
2344          *                                          $file being in a tmp directory).
2345          * @param array  $mimes                     Key is the file extension with value as the mime type.
2346          */
2347         return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
2348 }
2349
2350 /**
2351  * Returns the real mime type of an image file.
2352  *
2353  * This depends on exif_imagetype() or getimagesize() to determine real mime types.
2354  *
2355  * @since 4.7.1
2356  *
2357  * @param string $file Full path to the file.
2358  * @return string|false The actual mime type or false if the type cannot be determined.
2359  */
2360 function wp_get_image_mime( $file ) {
2361         /*
2362          * Use exif_imagetype() to check the mimetype if available or fall back to
2363          * getimagesize() if exif isn't avaialbe. If either function throws an Exception
2364          * we assume the file could not be validated.
2365          */
2366         try {
2367                 if ( is_callable( 'exif_imagetype' ) ) {
2368                         $mime = image_type_to_mime_type( exif_imagetype( $file ) );
2369                 } elseif ( function_exists( 'getimagesize' ) ) {
2370                         $imagesize = getimagesize( $file );
2371                         $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
2372                 } else {
2373                         $mime = false;
2374                 }
2375         } catch ( Exception $e ) {
2376                 $mime = false;
2377         }
2378
2379         return $mime;
2380 }
2381
2382 /**
2383  * Retrieve list of mime types and file extensions.
2384  *
2385  * @since 3.5.0
2386  * @since 4.2.0 Support was added for GIMP (xcf) files.
2387  *
2388  * @return array Array of mime types keyed by the file extension regex corresponding to those types.
2389  */
2390 function wp_get_mime_types() {
2391         /**
2392          * Filters the list of mime types and file extensions.
2393          *
2394          * This filter should be used to add, not remove, mime types. To remove
2395          * mime types, use the {@see 'upload_mimes'} filter.
2396          *
2397          * @since 3.5.0
2398          *
2399          * @param array $wp_get_mime_types Mime types keyed by the file extension regex
2400          *                                 corresponding to those types.
2401          */
2402         return apply_filters( 'mime_types', array(
2403         // Image formats.
2404         'jpg|jpeg|jpe' => 'image/jpeg',
2405         'gif' => 'image/gif',
2406         'png' => 'image/png',
2407         'bmp' => 'image/bmp',
2408         'tiff|tif' => 'image/tiff',
2409         'ico' => 'image/x-icon',
2410         // Video formats.
2411         'asf|asx' => 'video/x-ms-asf',
2412         'wmv' => 'video/x-ms-wmv',
2413         'wmx' => 'video/x-ms-wmx',
2414         'wm' => 'video/x-ms-wm',
2415         'avi' => 'video/avi',
2416         'divx' => 'video/divx',
2417         'flv' => 'video/x-flv',
2418         'mov|qt' => 'video/quicktime',
2419         'mpeg|mpg|mpe' => 'video/mpeg',
2420         'mp4|m4v' => 'video/mp4',
2421         'ogv' => 'video/ogg',
2422         'webm' => 'video/webm',
2423         'mkv' => 'video/x-matroska',
2424         '3gp|3gpp' => 'video/3gpp', // Can also be audio
2425         '3g2|3gp2' => 'video/3gpp2', // Can also be audio
2426         // Text formats.
2427         'txt|asc|c|cc|h|srt' => 'text/plain',
2428         'csv' => 'text/csv',
2429         'tsv' => 'text/tab-separated-values',
2430         'ics' => 'text/calendar',
2431         'rtx' => 'text/richtext',
2432         'css' => 'text/css',
2433         'htm|html' => 'text/html',
2434         'vtt' => 'text/vtt',
2435         'dfxp' => 'application/ttaf+xml',
2436         // Audio formats.
2437         'mp3|m4a|m4b' => 'audio/mpeg',
2438         'ra|ram' => 'audio/x-realaudio',
2439         'wav' => 'audio/wav',
2440         'ogg|oga' => 'audio/ogg',
2441         'mid|midi' => 'audio/midi',
2442         'wma' => 'audio/x-ms-wma',
2443         'wax' => 'audio/x-ms-wax',
2444         'mka' => 'audio/x-matroska',
2445         // Misc application formats.
2446         'rtf' => 'application/rtf',
2447         'js' => 'application/javascript',
2448         'pdf' => 'application/pdf',
2449         'swf' => 'application/x-shockwave-flash',
2450         'class' => 'application/java',
2451         'tar' => 'application/x-tar',
2452         'zip' => 'application/zip',
2453         'gz|gzip' => 'application/x-gzip',
2454         'rar' => 'application/rar',
2455         '7z' => 'application/x-7z-compressed',
2456         'exe' => 'application/x-msdownload',
2457         'psd' => 'application/octet-stream',
2458         'xcf' => 'application/octet-stream',
2459         // MS Office formats.
2460         'doc' => 'application/msword',
2461         'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
2462         'wri' => 'application/vnd.ms-write',
2463         'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
2464         'mdb' => 'application/vnd.ms-access',
2465         'mpp' => 'application/vnd.ms-project',
2466         'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
2467         'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
2468         'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
2469         'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
2470         'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
2471         'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
2472         'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
2473         'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
2474         'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
2475         'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
2476         'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
2477         'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
2478         'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
2479         'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
2480         'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
2481         'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
2482         'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
2483         'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
2484         'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
2485         'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
2486         'oxps' => 'application/oxps',
2487         'xps' => 'application/vnd.ms-xpsdocument',
2488         // OpenOffice formats.
2489         'odt' => 'application/vnd.oasis.opendocument.text',
2490         'odp' => 'application/vnd.oasis.opendocument.presentation',
2491         'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
2492         'odg' => 'application/vnd.oasis.opendocument.graphics',
2493         'odc' => 'application/vnd.oasis.opendocument.chart',
2494         'odb' => 'application/vnd.oasis.opendocument.database',
2495         'odf' => 'application/vnd.oasis.opendocument.formula',
2496         // WordPerfect formats.
2497         'wp|wpd' => 'application/wordperfect',
2498         // iWork formats.
2499         'key' => 'application/vnd.apple.keynote',
2500         'numbers' => 'application/vnd.apple.numbers',
2501         'pages' => 'application/vnd.apple.pages',
2502         ) );
2503 }
2504
2505 /**
2506  * Retrieves the list of common file extensions and their types.
2507  *
2508  * @since 4.6.0
2509  *
2510  * @return array Array of file extensions types keyed by the type of file.
2511  */
2512 function wp_get_ext_types() {
2513
2514         /**
2515          * Filters file type based on the extension name.
2516          *
2517          * @since 2.5.0
2518          *
2519          * @see wp_ext2type()
2520          *
2521          * @param array $ext2type Multi-dimensional array with extensions for a default set
2522          *                        of file types.
2523          */
2524         return apply_filters( 'ext2type', array(
2525                 'image'       => array( 'jpg', 'jpeg', 'jpe',  'gif',  'png',  'bmp',   'tif',  'tiff', 'ico' ),
2526                 'audio'       => array( 'aac', 'ac3',  'aif',  'aiff', 'm3a',  'm4a',   'm4b',  'mka',  'mp1',  'mp2',  'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
2527                 'video'       => array( '3g2',  '3gp', '3gpp', 'asf', 'avi',  'divx', 'dv',   'flv',  'm4v',   'mkv',  'mov',  'mp4',  'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt',  'rm', 'vob', 'wmv' ),
2528                 'document'    => array( 'doc', 'docx', 'docm', 'dotm', 'odt',  'pages', 'pdf',  'xps',  'oxps', 'rtf',  'wp', 'wpd', 'psd', 'xcf' ),
2529                 'spreadsheet' => array( 'numbers',     'ods',  'xls',  'xlsx', 'xlsm',  'xlsb' ),
2530                 'interactive' => array( 'swf', 'key',  'ppt',  'pptx', 'pptm', 'pps',   'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
2531                 'text'        => array( 'asc', 'csv',  'tsv',  'txt' ),
2532                 'archive'     => array( 'bz2', 'cab',  'dmg',  'gz',   'rar',  'sea',   'sit',  'sqx',  'tar',  'tgz',  'zip', '7z' ),
2533                 'code'        => array( 'css', 'htm',  'html', 'php',  'js' ),
2534         ) );
2535 }
2536
2537 /**
2538  * Retrieve list of allowed mime types and file extensions.
2539  *
2540  * @since 2.8.6
2541  *
2542  * @param int|WP_User $user Optional. User to check. Defaults to current user.
2543  * @return array Array of mime types keyed by the file extension regex corresponding
2544  *               to those types.
2545  */
2546 function get_allowed_mime_types( $user = null ) {
2547         $t = wp_get_mime_types();
2548
2549         unset( $t['swf'], $t['exe'] );
2550         if ( function_exists( 'current_user_can' ) )
2551                 $unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
2552
2553         if ( empty( $unfiltered ) )
2554                 unset( $t['htm|html'] );
2555
2556         /**
2557          * Filters list of allowed mime types and file extensions.
2558          *
2559          * @since 2.0.0
2560          *
2561          * @param array            $t    Mime types keyed by the file extension regex corresponding to
2562          *                               those types. 'swf' and 'exe' removed from full list. 'htm|html' also
2563          *                               removed depending on '$user' capabilities.
2564          * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user).
2565          */
2566         return apply_filters( 'upload_mimes', $t, $user );
2567 }
2568
2569 /**
2570  * Display "Are You Sure" message to confirm the action being taken.
2571  *
2572  * If the action has the nonce explain message, then it will be displayed
2573  * along with the "Are you sure?" message.
2574  *
2575  * @since 2.0.4
2576  *
2577  * @param string $action The nonce action.
2578  */
2579 function wp_nonce_ays( $action ) {
2580         if ( 'log-out' == $action ) {
2581                 $html = sprintf(
2582                         /* translators: %s: site name */
2583                         __( 'You are attempting to log out of %s' ),
2584                         get_bloginfo( 'name' )
2585                 );
2586                 $html .= '</p><p>';
2587                 $redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
2588                 $html .= sprintf(
2589                         /* translators: %s: logout URL */
2590                         __( 'Do you really want to <a href="%s">log out</a>?' ),
2591                         wp_logout_url( $redirect_to )
2592                 );
2593         } else {
2594                 $html = __( 'Are you sure you want to do this?' );
2595                 if ( wp_get_referer() ) {
2596                         $html .= '</p><p>';
2597                         $html .= sprintf( '<a href="%s">%s</a>',
2598                                 esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
2599                                 __( 'Please try again.' )
2600                         );
2601                 }
2602         }
2603
2604         wp_die( $html, __( 'WordPress Failure Notice' ), 403 );
2605 }
2606
2607 /**
2608  * Kill WordPress execution and display HTML message with error message.
2609  *
2610  * This function complements the `die()` PHP function. The difference is that
2611  * HTML will be displayed to the user. It is recommended to use this function
2612  * only when the execution should not continue any further. It is not recommended
2613  * to call this function very often, and try to handle as many errors as possible
2614  * silently or more gracefully.
2615  *
2616  * As a shorthand, the desired HTTP response code may be passed as an integer to
2617  * the `$title` parameter (the default title would apply) or the `$args` parameter.
2618  *
2619  * @since 2.0.4
2620  * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
2621  *              an integer to be used as the response code.
2622  *
2623  * @param string|WP_Error  $message Optional. Error message. If this is a WP_Error object,
2624  *                                  and not an Ajax or XML-RPC request, the error's messages are used.
2625  *                                  Default empty.
2626  * @param string|int       $title   Optional. Error title. If `$message` is a `WP_Error` object,
2627  *                                  error data with the key 'title' may be used to specify the title.
2628  *                                  If `$title` is an integer, then it is treated as the response
2629  *                                  code. Default empty.
2630  * @param string|array|int $args {
2631  *     Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
2632  *     as the response code. Default empty array.
2633  *
2634  *     @type int    $response       The HTTP response code. Default 200 for Ajax requests, 500 otherwise.
2635  *     @type bool   $back_link      Whether to include a link to go back. Default false.
2636  *     @type string $text_direction The text direction. This is only useful internally, when WordPress
2637  *                                  is still loading and the site's locale is not set up yet. Accepts 'rtl'.
2638  *                                  Default is the value of is_rtl().
2639  * }
2640  */
2641 function wp_die( $message = '', $title = '', $args = array() ) {
2642
2643         if ( is_int( $args ) ) {
2644                 $args = array( 'response' => $args );
2645         } elseif ( is_int( $title ) ) {
2646                 $args  = array( 'response' => $title );
2647                 $title = '';
2648         }
2649
2650         if ( wp_doing_ajax() ) {
2651                 /**
2652                  * Filters the callback for killing WordPress execution for Ajax requests.
2653                  *
2654                  * @since 3.4.0
2655                  *
2656                  * @param callable $function Callback function name.
2657                  */
2658                 $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
2659         } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
2660                 /**
2661                  * Filters the callback for killing WordPress execution for XML-RPC requests.
2662                  *
2663                  * @since 3.4.0
2664                  *
2665                  * @param callable $function Callback function name.
2666                  */
2667                 $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
2668         } else {
2669                 /**
2670                  * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
2671                  *
2672                  * @since 3.0.0
2673                  *
2674                  * @param callable $function Callback function name.
2675                  */
2676                 $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
2677         }
2678
2679         call_user_func( $function, $message, $title, $args );
2680 }
2681
2682 /**
2683  * Kills WordPress execution and display HTML message with error message.
2684  *
2685  * This is the default handler for wp_die if you want a custom one for your
2686  * site then you can overload using the {@see 'wp_die_handler'} filter in wp_die().
2687  *
2688  * @since 3.0.0
2689  * @access private
2690  *
2691  * @param string|WP_Error $message Error message or WP_Error object.
2692  * @param string          $title   Optional. Error title. Default empty.
2693  * @param string|array    $args    Optional. Arguments to control behavior. Default empty array.
2694  */
2695 function _default_wp_die_handler( $message, $title = '', $args = array() ) {
2696         $defaults = array( 'response' => 500 );
2697         $r = wp_parse_args($args, $defaults);
2698
2699         $have_gettext = function_exists('__');
2700
2701         if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
2702                 if ( empty( $title ) ) {
2703                         $error_data = $message->get_error_data();
2704                         if ( is_array( $error_data ) && isset( $error_data['title'] ) )
2705                                 $title = $error_data['title'];
2706                 }
2707                 $errors = $message->get_error_messages();
2708                 switch ( count( $errors ) ) {
2709                 case 0 :
2710                         $message = '';
2711                         break;
2712                 case 1 :
2713                         $message = "<p>{$errors[0]}</p>";
2714                         break;
2715                 default :
2716                         $message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
2717                         break;
2718                 }
2719         } elseif ( is_string( $message ) ) {
2720                 $message = "<p>$message</p>";
2721         }
2722
2723         if ( isset( $r['back_link'] ) && $r['back_link'] ) {
2724                 $back_text = $have_gettext? __('&laquo; Back') : '&laquo; Back';
2725                 $message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";
2726         }
2727
2728         if ( ! did_action( 'admin_head' ) ) :
2729                 if ( !headers_sent() ) {
2730                         status_header( $r['response'] );
2731                         nocache_headers();
2732                         header( 'Content-Type: text/html; charset=utf-8' );
2733                 }
2734
2735                 if ( empty($title) )
2736                         $title = $have_gettext ? __('WordPress &rsaquo; Error') : 'WordPress &rsaquo; Error';
2737
2738                 $text_direction = 'ltr';
2739                 if ( isset($r['text_direction']) && 'rtl' == $r['text_direction'] )
2740                         $text_direction = 'rtl';
2741                 elseif ( function_exists( 'is_rtl' ) && is_rtl() )
2742                         $text_direction = 'rtl';
2743 ?>
2744 <!DOCTYPE html>
2745 <!-- 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
2746 -->
2747 <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'"; ?>>
2748 <head>
2749         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2750         <meta name="viewport" content="width=device-width">
2751         <?php
2752         if ( function_exists( 'wp_no_robots' ) ) {
2753                 wp_no_robots();
2754         }
2755         ?>
2756         <title><?php echo $title ?></title>
2757         <style type="text/css">
2758                 html {
2759                         background: #f1f1f1;
2760                 }
2761                 body {
2762                         background: #fff;
2763                         color: #444;
2764                         font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
2765                         margin: 2em auto;
2766                         padding: 1em 2em;
2767                         max-width: 700px;
2768                         -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2769                         box-shadow: 0 1px 3px rgba(0,0,0,0.13);
2770                 }
2771                 h1 {
2772                         border-bottom: 1px solid #dadada;
2773                         clear: both;
2774                         color: #666;
2775                         font-size: 24px;
2776                         margin: 30px 0 0 0;
2777                         padding: 0;
2778                         padding-bottom: 7px;
2779                 }
2780                 #error-page {
2781                         margin-top: 50px;
2782                 }
2783                 #error-page p {
2784                         font-size: 14px;
2785                         line-height: 1.5;
2786                         margin: 25px 0 20px;
2787                 }
2788                 #error-page code {
2789                         font-family: Consolas, Monaco, monospace;
2790                 }
2791                 ul li {
2792                         margin-bottom: 10px;
2793                         font-size: 14px ;
2794                 }
2795                 a {
2796                         color: #0073aa;
2797                 }
2798                 a:hover,
2799                 a:active {
2800                         color: #00a0d2;
2801                 }
2802                 a:focus {
2803                         color: #124964;
2804                     -webkit-box-shadow:
2805                         0 0 0 1px #5b9dd9,
2806                                 0 0 2px 1px rgba(30, 140, 190, .8);
2807                     box-shadow:
2808                         0 0 0 1px #5b9dd9,
2809                                 0 0 2px 1px rgba(30, 140, 190, .8);
2810                         outline: none;
2811                 }
2812                 .button {
2813                         background: #f7f7f7;
2814                         border: 1px solid #ccc;
2815                         color: #555;
2816                         display: inline-block;
2817                         text-decoration: none;
2818                         font-size: 13px;
2819                         line-height: 26px;
2820                         height: 28px;
2821                         margin: 0;
2822                         padding: 0 10px 1px;
2823                         cursor: pointer;
2824                         -webkit-border-radius: 3px;
2825                         -webkit-appearance: none;
2826                         border-radius: 3px;
2827                         white-space: nowrap;
2828                         -webkit-box-sizing: border-box;
2829                         -moz-box-sizing:    border-box;
2830                         box-sizing:         border-box;
2831
2832                         -webkit-box-shadow: 0 1px 0 #ccc;
2833                         box-shadow: 0 1px 0 #ccc;
2834                         vertical-align: top;
2835                 }
2836
2837                 .button.button-large {
2838                         height: 30px;
2839                         line-height: 28px;
2840                         padding: 0 12px 2px;
2841                 }
2842
2843                 .button:hover,
2844                 .button:focus {
2845                         background: #fafafa;
2846                         border-color: #999;
2847                         color: #23282d;
2848                 }
2849
2850                 .button:focus  {
2851                         border-color: #5b9dd9;
2852                         -webkit-box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2853                         box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
2854                         outline: none;
2855                 }
2856
2857                 .button:active {
2858                         background: #eee;
2859                         border-color: #999;
2860                         -webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2861                         box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
2862                         -webkit-transform: translateY(1px);
2863                         -ms-transform: translateY(1px);
2864                         transform: translateY(1px);
2865                 }
2866
2867                 <?php
2868                 if ( 'rtl' == $text_direction ) {
2869                         echo 'body { font-family: Tahoma, Arial; }';
2870                 }
2871                 ?>
2872         </style>
2873 </head>
2874 <body id="error-page">
2875 <?php endif; // ! did_action( 'admin_head' ) ?>
2876         <?php echo $message; ?>
2877 </body>
2878 </html>
2879 <?php
2880         die();
2881 }
2882
2883 /**
2884  * Kill WordPress execution and display XML message with error message.
2885  *
2886  * This is the handler for wp_die when processing XMLRPC requests.
2887  *
2888  * @since 3.2.0
2889  * @access private
2890  *
2891  * @global wp_xmlrpc_server $wp_xmlrpc_server
2892  *
2893  * @param string       $message Error message.
2894  * @param string       $title   Optional. Error title. Default empty.
2895  * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
2896  */
2897 function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
2898         global $wp_xmlrpc_server;
2899         $defaults = array( 'response' => 500 );
2900
2901         $r = wp_parse_args($args, $defaults);
2902
2903         if ( $wp_xmlrpc_server ) {
2904                 $error = new IXR_Error( $r['response'] , $message);
2905                 $wp_xmlrpc_server->output( $error->getXml() );
2906         }
2907         die();
2908 }
2909
2910 /**
2911  * Kill WordPress ajax execution.
2912  *
2913  * This is the handler for wp_die when processing Ajax requests.
2914  *
2915  * @since 3.4.0
2916  * @access private
2917  *
2918  * @param string       $message Error message.
2919  * @param string       $title   Optional. Error title (unused). Default empty.
2920  * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
2921  */
2922 function _ajax_wp_die_handler( $message, $title = '', $args = array() ) {
2923         $defaults = array(
2924                 'response' => 200,
2925         );
2926         $r = wp_parse_args( $args, $defaults );
2927
2928         if ( ! headers_sent() && null !== $r['response'] ) {
2929                 status_header( $r['response'] );
2930         }
2931
2932         if ( is_scalar( $message ) )
2933                 die( (string) $message );
2934         die( '0' );
2935 }
2936
2937 /**
2938  * Kill WordPress execution.
2939  *
2940  * This is the handler for wp_die when processing APP requests.
2941  *
2942  * @since 3.4.0
2943  * @access private
2944  *
2945  * @param string $message Optional. Response to print. Default empty.
2946  */
2947 function _scalar_wp_die_handler( $message = '' ) {
2948         if ( is_scalar( $message ) )
2949                 die( (string) $message );
2950         die();
2951 }
2952
2953 /**
2954  * Encode a variable into JSON, with some sanity checks.
2955  *
2956  * @since 4.1.0
2957  *
2958  * @param mixed $data    Variable (usually an array or object) to encode as JSON.
2959  * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
2960  * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
2961  *                       greater than 0. Default 512.
2962  * @return string|false The JSON encoded string, or false if it cannot be encoded.
2963  */
2964 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
2965         /*
2966          * json_encode() has had extra params added over the years.
2967          * $options was added in 5.3, and $depth in 5.5.
2968          * We need to make sure we call it with the correct arguments.
2969          */
2970         if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
2971                 $args = array( $data, $options, $depth );
2972         } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
2973                 $args = array( $data, $options );
2974         } else {
2975                 $args = array( $data );
2976         }
2977
2978         // Prepare the data for JSON serialization.
2979         $args[0] = _wp_json_prepare_data( $data );
2980
2981         $json = @call_user_func_array( 'json_encode', $args );
2982
2983         // If json_encode() was successful, no need to do more sanity checking.
2984         // ... unless we're in an old version of PHP, and json_encode() returned
2985         // a string containing 'null'. Then we need to do more sanity checking.
2986         if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) )  {
2987                 return $json;
2988         }
2989
2990         try {
2991                 $args[0] = _wp_json_sanity_check( $data, $depth );
2992         } catch ( Exception $e ) {
2993                 return false;
2994         }
2995
2996         return call_user_func_array( 'json_encode', $args );
2997 }
2998
2999 /**
3000  * Perform sanity checks on data that shall be encoded to JSON.
3001  *
3002  * @ignore
3003  * @since 4.1.0
3004  * @access private
3005  *
3006  * @see wp_json_encode()
3007  *
3008  * @param mixed $data  Variable (usually an array or object) to encode as JSON.
3009  * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
3010  * @return mixed The sanitized data that shall be encoded to JSON.
3011  */
3012 function _wp_json_sanity_check( $data, $depth ) {
3013         if ( $depth < 0 ) {
3014                 throw new Exception( 'Reached depth limit' );
3015         }
3016
3017         if ( is_array( $data ) ) {
3018                 $output = array();
3019                 foreach ( $data as $id => $el ) {
3020                         // Don't forget to sanitize the ID!
3021                         if ( is_string( $id ) ) {
3022                                 $clean_id = _wp_json_convert_string( $id );
3023                         } else {
3024                                 $clean_id = $id;
3025                         }
3026
3027                         // Check the element type, so that we're only recursing if we really have to.
3028                         if ( is_array( $el ) || is_object( $el ) ) {
3029                                 $output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
3030                         } elseif ( is_string( $el ) ) {
3031                                 $output[ $clean_id ] = _wp_json_convert_string( $el );
3032                         } else {
3033                                 $output[ $clean_id ] = $el;
3034                         }
3035                 }
3036         } elseif ( is_object( $data ) ) {
3037                 $output = new stdClass;
3038                 foreach ( $data as $id => $el ) {
3039                         if ( is_string( $id ) ) {
3040                                 $clean_id = _wp_json_convert_string( $id );
3041                         } else {
3042                                 $clean_id = $id;
3043                         }
3044
3045                         if ( is_array( $el ) || is_object( $el ) ) {
3046                                 $output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
3047                         } elseif ( is_string( $el ) ) {
3048                                 $output->$clean_id = _wp_json_convert_string( $el );
3049                         } else {
3050                                 $output->$clean_id = $el;
3051                         }
3052                 }
3053         } elseif ( is_string( $data ) ) {
3054                 return _wp_json_convert_string( $data );
3055         } else {
3056                 return $data;
3057         }
3058
3059         return $output;
3060 }
3061
3062 /**
3063  * Convert a string to UTF-8, so that it can be safely encoded to JSON.
3064  *
3065  * @ignore
3066  * @since 4.1.0
3067  * @access private
3068  *
3069  * @see _wp_json_sanity_check()
3070  *
3071  * @staticvar bool $use_mb
3072  *
3073  * @param string $string The string which is to be converted.
3074  * @return string The checked string.
3075  */
3076 function _wp_json_convert_string( $string ) {
3077         static $use_mb = null;
3078         if ( is_null( $use_mb ) ) {
3079                 $use_mb = function_exists( 'mb_convert_encoding' );
3080         }
3081
3082         if ( $use_mb ) {
3083                 $encoding = mb_detect_encoding( $string, mb_detect_order(), true );
3084                 if ( $encoding ) {
3085                         return mb_convert_encoding( $string, 'UTF-8', $encoding );
3086                 } else {
3087                         return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
3088                 }
3089         } else {
3090                 return wp_check_invalid_utf8( $string, true );
3091         }
3092 }
3093
3094 /**
3095  * Prepares response data to be serialized to JSON.
3096  *
3097  * This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
3098  *
3099  * @ignore
3100  * @since 4.4.0
3101  * @access private
3102  *
3103  * @param mixed $data Native representation.
3104  * @return bool|int|float|null|string|array Data ready for `json_encode()`.
3105  */
3106 function _wp_json_prepare_data( $data ) {
3107         if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) {
3108                 return $data;
3109         }
3110
3111         switch ( gettype( $data ) ) {
3112                 case 'boolean':
3113                 case 'integer':
3114                 case 'double':
3115                 case 'string':
3116                 case 'NULL':
3117                         // These values can be passed through.
3118                         return $data;
3119
3120                 case 'array':
3121                         // Arrays must be mapped in case they also return objects.
3122                         return array_map( '_wp_json_prepare_data', $data );
3123
3124                 case 'object':
3125                         // If this is an incomplete object (__PHP_Incomplete_Class), bail.
3126                         if ( ! is_object( $data ) ) {
3127                                 return null;
3128                         }
3129
3130                         if ( $data instanceof JsonSerializable ) {
3131                                 $data = $data->jsonSerialize();
3132                         } else {
3133                                 $data = get_object_vars( $data );
3134                         }
3135
3136                         // Now, pass the array (or whatever was returned from jsonSerialize through).
3137                         return _wp_json_prepare_data( $data );
3138
3139                 default:
3140                         return null;
3141         }
3142 }
3143
3144 /**
3145  * Send a JSON response back to an Ajax request.
3146  *
3147  * @since 3.5.0
3148  * @since 4.7.0 The `$status_code` parameter was added.
3149  *
3150  * @param mixed $response    Variable (usually an array or object) to encode as JSON,
3151  *                           then print and die.
3152  * @param int   $status_code The HTTP status code to output.
3153  */
3154 function wp_send_json( $response, $status_code = null ) {
3155         @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
3156         if ( null !== $status_code ) {
3157                 status_header( $status_code );
3158         }
3159         echo wp_json_encode( $response );
3160
3161         if ( wp_doing_ajax() ) {
3162                 wp_die( '', '', array(
3163                         'response' => null,
3164                 ) );
3165         } else {
3166                 die;
3167         }
3168 }
3169
3170 /**
3171  * Send a JSON response back to an Ajax request, indicating success.
3172  *
3173  * @since 3.5.0
3174  * @since 4.7.0 The `$status_code` parameter was added.
3175  *
3176  * @param mixed $data        Data to encode as JSON, then print and die.
3177  * @param int   $status_code The HTTP status code to output.
3178  */
3179 function wp_send_json_success( $data = null, $status_code = null ) {
3180         $response = array( 'success' => true );
3181
3182         if ( isset( $data ) )
3183                 $response['data'] = $data;
3184
3185         wp_send_json( $response, $status_code );
3186 }
3187
3188 /**
3189  * Send a JSON response back to an Ajax request, indicating failure.
3190  *
3191  * If the `$data` parameter is a WP_Error object, the errors
3192  * within the object are processed and output as an array of error
3193  * codes and corresponding messages. All other types are output
3194  * without further processing.
3195  *
3196  * @since 3.5.0
3197  * @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
3198  * @since 4.7.0 The `$status_code` parameter was added.
3199  *
3200  * @param mixed $data        Data to encode as JSON, then print and die.
3201  * @param int   $status_code The HTTP status code to output.
3202  */
3203 function wp_send_json_error( $data = null, $status_code = null ) {
3204         $response = array( 'success' => false );
3205
3206         if ( isset( $data ) ) {
3207                 if ( is_wp_error( $data ) ) {
3208                         $result = array();
3209                         foreach ( $data->errors as $code => $messages ) {
3210                                 foreach ( $messages as $message ) {
3211                                         $result[] = array( 'code' => $code, 'message' => $message );
3212                                 }
3213                         }
3214
3215                         $response['data'] = $result;
3216                 } else {
3217                         $response['data'] = $data;
3218                 }
3219         }
3220
3221         wp_send_json( $response, $status_code );
3222 }
3223
3224 /**
3225  * Checks that a JSONP callback is a valid JavaScript callback.
3226  *
3227  * Only allows alphanumeric characters and the dot character in callback
3228  * function names. This helps to mitigate XSS attacks caused by directly
3229  * outputting user input.
3230  *
3231  * @since 4.6.0
3232  *
3233  * @param string $callback Supplied JSONP callback function.
3234  * @return bool True if valid callback, otherwise false.
3235  */
3236 function wp_check_jsonp_callback( $callback ) {
3237         if ( ! is_string( $callback ) ) {
3238                 return false;
3239         }
3240
3241         preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count );
3242
3243         return 0 === $illegal_char_count;
3244 }
3245
3246 /**
3247  * Retrieve the WordPress home page URL.
3248  *
3249  * If the constant named 'WP_HOME' exists, then it will be used and returned
3250  * by the function. This can be used to counter the redirection on your local
3251  * development environment.
3252  *
3253  * @since 2.2.0
3254  * @access private
3255  *
3256  * @see WP_HOME
3257  *
3258  * @param string $url URL for the home location.
3259  * @return string Homepage location.
3260  */
3261 function _config_wp_home( $url = '' ) {
3262         if ( defined( 'WP_HOME' ) )
3263                 return untrailingslashit( WP_HOME );
3264         return $url;
3265 }
3266
3267 /**
3268  * Retrieve the WordPress site URL.
3269  *
3270  * If the constant named 'WP_SITEURL' is defined, then the value in that
3271  * constant will always be returned. This can be used for debugging a site
3272  * on your localhost while not having to change the database to your URL.
3273  *
3274  * @since 2.2.0
3275  * @access private
3276  *
3277  * @see WP_SITEURL
3278  *
3279  * @param string $url URL to set the WordPress site location.
3280  * @return string The WordPress Site URL.
3281  */
3282 function _config_wp_siteurl( $url = '' ) {
3283         if ( defined( 'WP_SITEURL' ) )
3284                 return untrailingslashit( WP_SITEURL );
3285         return $url;
3286 }
3287
3288 /**
3289  * Delete the fresh site option.
3290  *
3291  * @since 4.7.0
3292  * @access private
3293  */
3294 function _delete_option_fresh_site() {
3295         update_option( 'fresh_site', 0 );
3296 }
3297
3298 /**
3299  * Set the localized direction for MCE plugin.
3300  *
3301  * Will only set the direction to 'rtl', if the WordPress locale has
3302  * the text direction set to 'rtl'.
3303  *
3304  * Fills in the 'directionality' setting, enables the 'directionality'
3305  * plugin, and adds the 'ltr' button to 'toolbar1', formerly
3306  * 'theme_advanced_buttons1' array keys. These keys are then returned
3307  * in the $mce_init (TinyMCE settings) array.
3308  *
3309  * @since 2.1.0
3310  * @access private
3311  *
3312  * @param array $mce_init MCE settings array.
3313  * @return array Direction set for 'rtl', if needed by locale.
3314  */
3315 function _mce_set_direction( $mce_init ) {
3316         if ( is_rtl() ) {
3317                 $mce_init['directionality'] = 'rtl';
3318                 $mce_init['rtl_ui'] = true;
3319
3320                 if ( ! empty( $mce_init['plugins'] ) && strpos( $mce_init['plugins'], 'directionality' ) === false ) {
3321                         $mce_init['plugins'] .= ',directionality';
3322                 }
3323
3324                 if ( ! empty( $mce_init['toolbar1'] ) && ! preg_match( '/\bltr\b/', $mce_init['toolbar1'] ) ) {
3325                         $mce_init['toolbar1'] .= ',ltr';
3326                 }
3327         }
3328
3329         return $mce_init;
3330 }
3331
3332
3333 /**
3334  * Convert smiley code to the icon graphic file equivalent.
3335  *
3336  * You can turn off smilies, by going to the write setting screen and unchecking
3337  * the box, or by setting 'use_smilies' option to false or removing the option.
3338  *
3339  * Plugins may override the default smiley list by setting the $wpsmiliestrans
3340  * to an array, with the key the code the blogger types in and the value the
3341  * image file.
3342  *
3343  * The $wp_smiliessearch global is for the regular expression and is set each
3344  * time the function is called.
3345  *
3346  * The full list of smilies can be found in the function and won't be listed in
3347  * the description. Probably should create a Codex page for it, so that it is
3348  * available.
3349  *
3350  * @global array $wpsmiliestrans
3351  * @global array $wp_smiliessearch
3352  *
3353  * @since 2.2.0
3354  */
3355 function smilies_init() {
3356         global $wpsmiliestrans, $wp_smiliessearch;
3357
3358         // don't bother setting up smilies if they are disabled
3359         if ( !get_option( 'use_smilies' ) )
3360                 return;
3361
3362         if ( !isset( $wpsmiliestrans ) ) {
3363                 $wpsmiliestrans = array(
3364                 ':mrgreen:' => 'mrgreen.png',
3365                 ':neutral:' => "\xf0\x9f\x98\x90",
3366                 ':twisted:' => "\xf0\x9f\x98\x88",
3367                   ':arrow:' => "\xe2\x9e\xa1",
3368                   ':shock:' => "\xf0\x9f\x98\xaf",
3369                   ':smile:' => "\xf0\x9f\x99\x82",
3370                     ':???:' => "\xf0\x9f\x98\x95",
3371                    ':cool:' => "\xf0\x9f\x98\x8e",
3372                    ':evil:' => "\xf0\x9f\x91\xbf",
3373                    ':grin:' => "\xf0\x9f\x98\x80",
3374                    ':idea:' => "\xf0\x9f\x92\xa1",
3375                    ':oops:' => "\xf0\x9f\x98\xb3",
3376                    ':razz:' => "\xf0\x9f\x98\x9b",
3377                    ':roll:' => "\xf0\x9f\x99\x84",
3378                    ':wink:' => "\xf0\x9f\x98\x89",
3379                     ':cry:' => "\xf0\x9f\x98\xa5",
3380                     ':eek:' => "\xf0\x9f\x98\xae",
3381                     ':lol:' => "\xf0\x9f\x98\x86",
3382                     ':mad:' => "\xf0\x9f\x98\xa1",
3383                     ':sad:' => "\xf0\x9f\x99\x81",
3384                       '8-)' => "\xf0\x9f\x98\x8e",
3385                       '8-O' => "\xf0\x9f\x98\xaf",
3386                       ':-(' => "\xf0\x9f\x99\x81",
3387                       ':-)' => "\xf0\x9f\x99\x82",
3388                       ':-?' => "\xf0\x9f\x98\x95",
3389                       ':-D' => "\xf0\x9f\x98\x80",
3390                       ':-P' => "\xf0\x9f\x98\x9b",
3391                       ':-o' => "\xf0\x9f\x98\xae",
3392                       ':-x' => "\xf0\x9f\x98\xa1",
3393                       ':-|' => "\xf0\x9f\x98\x90",
3394                       ';-)' => "\xf0\x9f\x98\x89",
3395                 // This one transformation breaks regular text with frequency.
3396                 //     '8)' => "\xf0\x9f\x98\x8e",
3397                        '8O' => "\xf0\x9f\x98\xaf",
3398                        ':(' => "\xf0\x9f\x99\x81",
3399                        ':)' => "\xf0\x9f\x99\x82",
3400                        ':?' => "\xf0\x9f\x98\x95",
3401                        ':D' => "\xf0\x9f\x98\x80",
3402                        ':P' => "\xf0\x9f\x98\x9b",
3403                        ':o' => "\xf0\x9f\x98\xae",
3404                        ':x' => "\xf0\x9f\x98\xa1",
3405                        ':|' => "\xf0\x9f\x98\x90",
3406                        ';)' => "\xf0\x9f\x98\x89",
3407                       ':!:' => "\xe2\x9d\x97",
3408                       ':?:' => "\xe2\x9d\x93",
3409                 );
3410         }
3411
3412         /**
3413          * Filters all the smilies.
3414          *
3415          * This filter must be added before `smilies_init` is run, as
3416          * it is normally only run once to setup the smilies regex.
3417          *
3418          * @since 4.7.0
3419          *
3420          * @param array $wpsmiliestrans List of the smilies.
3421          */
3422         $wpsmiliestrans = apply_filters('smilies', $wpsmiliestrans);
3423
3424         if (count($wpsmiliestrans) == 0) {
3425                 return;
3426         }
3427
3428         /*
3429          * NOTE: we sort the smilies in reverse key order. This is to make sure
3430          * we match the longest possible smilie (:???: vs :?) as the regular
3431          * expression used below is first-match
3432          */
3433         krsort($wpsmiliestrans);
3434
3435         $spaces = wp_spaces_regexp();
3436
3437         // Begin first "subpattern"
3438         $wp_smiliessearch = '/(?<=' . $spaces . '|^)';
3439
3440         $subchar = '';
3441         foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
3442                 $firstchar = substr($smiley, 0, 1);
3443                 $rest = substr($smiley, 1);
3444
3445                 // new subpattern?
3446                 if ($firstchar != $subchar) {
3447                         if ($subchar != '') {
3448                                 $wp_smiliessearch .= ')(?=' . $spaces . '|$)';  // End previous "subpattern"
3449                                 $wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern"
3450                         }
3451                         $subchar = $firstchar;
3452                         $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
3453                 } else {
3454                         $wp_smiliessearch .= '|';
3455                 }
3456                 $wp_smiliessearch .= preg_quote($rest, '/');
3457         }
3458
3459         $wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
3460
3461 }
3462
3463 /**
3464  * Merge user defined arguments into defaults array.
3465  *
3466  * This function is used throughout WordPress to allow for both string or array
3467  * to be merged into another array.
3468  *
3469  * @since 2.2.0
3470  * @since 2.3.0 `$args` can now also be an object.
3471  *
3472  * @param string|array|object $args     Value to merge with $defaults.
3473  * @param array               $defaults Optional. Array that serves as the defaults. Default empty.
3474  * @return array Merged user defined values with defaults.
3475  */
3476 function wp_parse_args( $args, $defaults = '' ) {
3477         if ( is_object( $args ) )
3478                 $r = get_object_vars( $args );
3479         elseif ( is_array( $args ) )
3480                 $r =& $args;
3481         else
3482                 wp_parse_str( $args, $r );
3483
3484         if ( is_array( $defaults ) )
3485                 return array_merge( $defaults, $r );
3486         return $r;
3487 }
3488
3489 /**
3490  * Clean up an array, comma- or space-separated list of IDs.
3491  *
3492  * @since 3.0.0
3493  *
3494  * @param array|string $list List of ids.
3495  * @return array Sanitized array of IDs.
3496  */
3497 function wp_parse_id_list( $list ) {
3498         if ( !is_array($list) )
3499                 $list = preg_split('/[\s,]+/', $list);
3500
3501         return array_unique(array_map('absint', $list));
3502 }
3503
3504 /**
3505  * Clean up an array, comma- or space-separated list of slugs.
3506  *
3507  * @since 4.7.0
3508  *
3509  * @param  array|string $list List of slugs.
3510  * @return array Sanitized array of slugs.
3511  */
3512 function wp_parse_slug_list( $list ) {
3513         if ( ! is_array( $list ) ) {
3514                 $list = preg_split( '/[\s,]+/', $list );
3515         }
3516
3517         foreach ( $list as $key => $value ) {
3518                 $list[ $key ] = sanitize_title( $value );
3519         }
3520
3521         return array_unique( $list );
3522 }
3523
3524 /**
3525  * Extract a slice of an array, given a list of keys.
3526  *
3527  * @since 3.1.0
3528  *
3529  * @param array $array The original array.
3530  * @param array $keys  The list of keys.
3531  * @return array The array slice.
3532  */
3533 function wp_array_slice_assoc( $array, $keys ) {
3534         $slice = array();
3535         foreach ( $keys as $key )
3536                 if ( isset( $array[ $key ] ) )
3537                         $slice[ $key ] = $array[ $key ];
3538
3539         return $slice;
3540 }
3541
3542 /**
3543  * Determines if the variable is a numeric-indexed array.
3544  *
3545  * @since 4.4.0
3546  *
3547  * @param mixed $data Variable to check.
3548  * @return bool Whether the variable is a list.
3549  */
3550 function wp_is_numeric_array( $data ) {
3551         if ( ! is_array( $data ) ) {
3552                 return false;
3553         }
3554
3555         $keys = array_keys( $data );
3556         $string_keys = array_filter( $keys, 'is_string' );
3557         return count( $string_keys ) === 0;
3558 }
3559
3560 /**
3561  * Filters a list of objects, based on a set of key => value arguments.
3562  *
3563  * @since 3.0.0
3564  * @since 4.7.0 Uses WP_List_Util class.
3565  *
3566  * @param array       $list     An array of objects to filter
3567  * @param array       $args     Optional. An array of key => value arguments to match
3568  *                              against each object. Default empty array.
3569  * @param string      $operator Optional. The logical operation to perform. 'or' means
3570  *                              only one element from the array needs to match; 'and'
3571  *                              means all elements must match; 'not' means no elements may
3572  *                              match. Default 'and'.
3573  * @param bool|string $field    A field from the object to place instead of the entire object.
3574  *                              Default false.
3575  * @return array A list of objects or object fields.
3576  */
3577 function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
3578         if ( ! is_array( $list ) ) {
3579                 return array();
3580         }
3581
3582         $util = new WP_List_Util( $list );
3583
3584         $util->filter( $args, $operator );
3585
3586         if ( $field ) {
3587                 $util->pluck( $field );
3588         }
3589
3590         return $util->get_output();
3591 }
3592
3593 /**
3594  * Filters a list of objects, based on a set of key => value arguments.
3595  *
3596  * @since 3.1.0
3597  * @since 4.7.0 Uses WP_List_Util class.
3598  *
3599  * @param array  $list     An array of objects to filter.
3600  * @param array  $args     Optional. An array of key => value arguments to match
3601  *                         against each object. Default empty array.
3602  * @param string $operator Optional. The logical operation to perform. 'AND' means
3603  *                         all elements from the array must match. 'OR' means only
3604  *                         one element needs to match. 'NOT' means no elements may
3605  *                         match. Default 'AND'.
3606  * @return array Array of found values.
3607  */
3608 function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
3609         if ( ! is_array( $list ) ) {
3610                 return array();
3611         }
3612
3613         $util = new WP_List_Util( $list );
3614         return $util->filter( $args, $operator );
3615 }
3616
3617 /**
3618  * Pluck a certain field out of each object in a list.
3619  *
3620  * This has the same functionality and prototype of
3621  * array_column() (PHP 5.5) but also supports objects.
3622  *
3623  * @since 3.1.0
3624  * @since 4.0.0 $index_key parameter added.
3625  * @since 4.7.0 Uses WP_List_Util class.
3626  *
3627  * @param array      $list      List of objects or arrays
3628  * @param int|string $field     Field from the object to place instead of the entire object
3629  * @param int|string $index_key Optional. Field from the object to use as keys for the new array.
3630  *                              Default null.
3631  * @return array Array of found values. If `$index_key` is set, an array of found values with keys
3632  *               corresponding to `$index_key`. If `$index_key` is null, array keys from the original
3633  *               `$list` will be preserved in the results.
3634  */
3635 function wp_list_pluck( $list, $field, $index_key = null ) {
3636         $util = new WP_List_Util( $list );
3637         return $util->pluck( $field, $index_key );
3638 }
3639
3640 /**
3641  * Sorts a list of objects, based on one or more orderby arguments.
3642  *
3643  * @since 4.7.0
3644  *
3645  * @param array        $list          An array of objects to filter.
3646  * @param string|array $orderby       Optional. Either the field name to order by or an array
3647  *                                    of multiple orderby fields as $orderby => $order.
3648  * @param string       $order         Optional. Either 'ASC' or 'DESC'. Only used if $orderby
3649  *                                    is a string.
3650  * @param bool         $preserve_keys Optional. Whether to preserve keys. Default false.
3651  * @return array The sorted array.
3652  */
3653 function wp_list_sort( $list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
3654         if ( ! is_array( $list ) ) {
3655                 return array();
3656         }
3657
3658         $util = new WP_List_Util( $list );
3659         return $util->sort( $orderby, $order, $preserve_keys );
3660 }
3661
3662 /**
3663  * Determines if Widgets library should be loaded.
3664  *
3665  * Checks to make sure that the widgets library hasn't already been loaded.
3666  * If it hasn't, then it will load the widgets library and run an action hook.
3667  *
3668  * @since 2.2.0
3669  */
3670 function wp_maybe_load_widgets() {
3671         /**
3672          * Filters whether to load the Widgets library.
3673          *
3674          * Passing a falsey value to the filter will effectively short-circuit
3675          * the Widgets library from loading.
3676          *
3677          * @since 2.8.0
3678          *
3679          * @param bool $wp_maybe_load_widgets Whether to load the Widgets library.
3680          *                                    Default true.
3681          */
3682         if ( ! apply_filters( 'load_default_widgets', true ) ) {
3683                 return;
3684         }
3685
3686         require_once( ABSPATH . WPINC . '/default-widgets.php' );
3687
3688         add_action( '_admin_menu', 'wp_widgets_add_menu' );
3689 }
3690
3691 /**
3692  * Append the Widgets menu to the themes main menu.
3693  *
3694  * @since 2.2.0
3695  *
3696  * @global array $submenu
3697  */
3698 function wp_widgets_add_menu() {
3699         global $submenu;
3700
3701         if ( ! current_theme_supports( 'widgets' ) )
3702                 return;
3703
3704         $submenu['themes.php'][7] = array( __( 'Widgets' ), 'edit_theme_options', 'widgets.php' );
3705         ksort( $submenu['themes.php'], SORT_NUMERIC );
3706 }
3707
3708 /**
3709  * Flush all output buffers for PHP 5.2.
3710  *
3711  * Make sure all output buffers are flushed before our singletons are destroyed.
3712  *
3713  * @since 2.2.0
3714  */
3715 function wp_ob_end_flush_all() {
3716         $levels = ob_get_level();
3717         for ($i=0; $i<$levels; $i++)
3718                 ob_end_flush();
3719 }
3720
3721 /**
3722  * Load custom DB error or display WordPress DB error.
3723  *
3724  * If a file exists in the wp-content directory named db-error.php, then it will
3725  * be loaded instead of displaying the WordPress DB error. If it is not found,
3726  * then the WordPress DB error will be displayed instead.
3727  *
3728  * The WordPress DB error sets the HTTP status header to 500 to try to prevent
3729  * search engines from caching the message. Custom DB messages should do the
3730  * same.
3731  *
3732  * This function was backported to WordPress 2.3.2, but originally was added
3733  * in WordPress 2.5.0.
3734  *
3735  * @since 2.3.2
3736  *
3737  * @global wpdb $wpdb WordPress database abstraction object.
3738  */
3739 function dead_db() {
3740         global $wpdb;
3741
3742         wp_load_translations_early();
3743
3744         // Load custom DB error template, if present.
3745         if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
3746                 require_once( WP_CONTENT_DIR . '/db-error.php' );
3747                 die();
3748         }
3749
3750         // If installing or in the admin, provide the verbose message.
3751         if ( wp_installing() || defined( 'WP_ADMIN' ) )
3752                 wp_die($wpdb->error);
3753
3754         // Otherwise, be terse.
3755         status_header( 500 );
3756         nocache_headers();
3757         header( 'Content-Type: text/html; charset=utf-8' );
3758 ?>
3759 <!DOCTYPE html>
3760 <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
3761 <head>
3762 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3763         <title><?php _e( 'Database Error' ); ?></title>
3764
3765 </head>
3766 <body>
3767         <h1><?php _e( 'Error establishing a database connection' ); ?></h1>
3768 </body>
3769 </html>
3770 <?php
3771         die();
3772 }
3773
3774 /**
3775  * Convert a value to non-negative integer.
3776  *
3777  * @since 2.5.0
3778  *
3779  * @param mixed $maybeint Data you wish to have converted to a non-negative integer.
3780  * @return int A non-negative integer.
3781  */
3782 function absint( $maybeint ) {
3783         return abs( intval( $maybeint ) );
3784 }
3785
3786 /**
3787  * Mark a function as deprecated and inform when it has been used.
3788  *
3789  * There is a {@see 'hook deprecated_function_run'} that will be called that can be used
3790  * to get the backtrace up to what file and function called the deprecated
3791  * function.
3792  *
3793  * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3794  *
3795  * This function is to be used in every function that is deprecated.
3796  *
3797  * @since 2.5.0
3798  * @access private
3799  *
3800  * @param string $function    The function that was called.
3801  * @param string $version     The version of WordPress that deprecated the function.
3802  * @param string $replacement Optional. The function that should have been called. Default null.
3803  */
3804 function _deprecated_function( $function, $version, $replacement = null ) {
3805
3806         /**
3807          * Fires when a deprecated function is called.
3808          *
3809          * @since 2.5.0
3810          *
3811          * @param string $function    The function that was called.
3812          * @param string $replacement The function that should have been called.
3813          * @param string $version     The version of WordPress that deprecated the function.
3814          */
3815         do_action( 'deprecated_function_run', $function, $replacement, $version );
3816
3817         /**
3818          * Filters whether to trigger an error for deprecated functions.
3819          *
3820          * @since 2.5.0
3821          *
3822          * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3823          */
3824         if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
3825                 if ( function_exists( '__' ) ) {
3826                         if ( ! is_null( $replacement ) ) {
3827                                 /* translators: 1: PHP function name, 2: version number, 3: alternative function name */
3828                                 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
3829                         } else {
3830                                 /* translators: 1: PHP function name, 2: version number */
3831                                 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
3832                         }
3833                 } else {
3834                         if ( ! is_null( $replacement ) ) {
3835                                 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
3836                         } else {
3837                                 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
3838                         }
3839                 }
3840         }
3841 }
3842
3843 /**
3844  * Marks a constructor as deprecated and informs when it has been used.
3845  *
3846  * Similar to _deprecated_function(), but with different strings. Used to
3847  * remove PHP4 style constructors.
3848  *
3849  * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3850  *
3851  * This function is to be used in every PHP4 style constructor method that is deprecated.
3852  *
3853  * @since 4.3.0
3854  * @since 4.5.0 Added the `$parent_class` parameter.
3855  *
3856  * @access private
3857  *
3858  * @param string $class        The class containing the deprecated constructor.
3859  * @param string $version      The version of WordPress that deprecated the function.
3860  * @param string $parent_class Optional. The parent class calling the deprecated constructor.
3861  *                             Default empty string.
3862  */
3863 function _deprecated_constructor( $class, $version, $parent_class = '' ) {
3864
3865         /**
3866          * Fires when a deprecated constructor is called.
3867          *
3868          * @since 4.3.0
3869          * @since 4.5.0 Added the `$parent_class` parameter.
3870          *
3871          * @param string $class        The class containing the deprecated constructor.
3872          * @param string $version      The version of WordPress that deprecated the function.
3873          * @param string $parent_class The parent class calling the deprecated constructor.
3874          */
3875         do_action( 'deprecated_constructor_run', $class, $version, $parent_class );
3876
3877         /**
3878          * Filters whether to trigger an error for deprecated functions.
3879          *
3880          * `WP_DEBUG` must be true in addition to the filter evaluating to true.
3881          *
3882          * @since 4.3.0
3883          *
3884          * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
3885          */
3886         if ( WP_DEBUG && apply_filters( 'deprecated_constructor_trigger_error', true ) ) {
3887                 if ( function_exists( '__' ) ) {
3888                         if ( ! empty( $parent_class ) ) {
3889                                 /* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: __construct() method */
3890                                 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.' ),
3891                                         $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3892                         } else {
3893                                 /* translators: 1: PHP class name, 2: version number, 3: __construct() method */
3894                                 trigger_error( sprintf( __( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
3895                                         $class, $version, '<pre>__construct()</pre>' ) );
3896                         }
3897                 } else {
3898                         if ( ! empty( $parent_class ) ) {
3899                                 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.',
3900                                         $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
3901                         } else {
3902                                 trigger_error( sprintf( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
3903                                         $class, $version, '<pre>__construct()</pre>' ) );
3904                         }
3905                 }
3906         }
3907
3908 }
3909
3910 /**
3911  * Mark a file as deprecated and inform when it has been used.
3912  *
3913  * There is a hook {@see 'deprecated_file_included'} that will be called that can be used
3914  * to get the backtrace up to what file and function included the deprecated
3915  * file.
3916  *
3917  * The current behavior is to trigger a user error if `WP_DEBUG` is true.
3918  *
3919  * This function is to be used in every file that is deprecated.
3920  *
3921  * @since 2.5.0
3922  * @access private
3923  *
3924  * @param string $file        The file that was included.
3925  * @param string $version     The version of WordPress that deprecated the file.
3926  * @param string $replacement Optional. The file that should have been included based on ABSPATH.
3927  *                            Default null.
3928  * @param string $message     Optional. A message regarding the change. Default empty.
3929  */
3930 function _deprecated_file( $file, $version, $replacement = null, $message = '' ) {
3931
3932         /**
3933          * Fires when a deprecated file is called.
3934          *
3935          * @since 2.5.0
3936          *
3937          * @param string $file        The file that was called.
3938          * @param string $replacement The file that should have been included based on ABSPATH.
3939          * @param string $version     The version of WordPress that deprecated the file.
3940          * @param string $message     A message regarding the change.
3941          */
3942         do_action( 'deprecated_file_included', $file, $replacement, $version, $message );
3943
3944         /**
3945          * Filters whether to trigger an error for deprecated files.
3946          *
3947          * @since 2.5.0
3948          *
3949          * @param bool $trigger Whether to trigger the error for deprecated files. Default true.
3950          */
3951         if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
3952                 $message = empty( $message ) ? '' : ' ' . $message;
3953                 if ( function_exists( '__' ) ) {
3954                         if ( ! is_null( $replacement ) ) {
3955                                 /* translators: 1: PHP file name, 2: version number, 3: alternative file name */
3956                                 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $file, $version, $replacement ) . $message );
3957                         } else {
3958                                 /* translators: 1: PHP file name, 2: version number */
3959                                 trigger_error( sprintf( __('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $file, $version ) . $message );
3960                         }
3961                 } else {
3962                         if ( ! is_null( $replacement ) ) {
3963                                 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $file, $version, $replacement ) . $message );
3964                         } else {
3965                                 trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
3966                         }
3967                 }
3968         }
3969 }
3970 /**
3971  * Mark a function argument as deprecated and inform when it has been used.
3972  *
3973  * This function is to be used whenever a deprecated function argument is used.
3974  * Before this function is called, the argument must be checked for whether it was
3975  * used by comparing it to its default value or evaluating whether it is empty.
3976  * For example:
3977  *
3978  *     if ( ! empty( $deprecated ) ) {
3979  *         _deprecated_argument( __FUNCTION__, '3.0.0' );
3980  *     }
3981  *
3982  *
3983  * There is a hook deprecated_argument_run that will be called that can be used
3984  * to get the backtrace up to what file and function used the deprecated
3985  * argument.
3986  *
3987  * The current behavior is to trigger a user error if WP_DEBUG is true.
3988  *
3989  * @since 3.0.0
3990  * @access private
3991  *
3992  * @param string $function The function that was called.
3993  * @param string $version  The version of WordPress that deprecated the argument used.
3994  * @param string $message  Optional. A message regarding the change. Default null.
3995  */
3996 function _deprecated_argument( $function, $version, $message = null ) {
3997
3998         /**
3999          * Fires when a deprecated argument is called.
4000          *
4001          * @since 3.0.0
4002          *
4003          * @param string $function The function that was called.
4004          * @param string $message  A message regarding the change.
4005          * @param string $version  The version of WordPress that deprecated the argument used.
4006          */
4007         do_action( 'deprecated_argument_run', $function, $message, $version );
4008
4009         /**
4010          * Filters whether to trigger an error for deprecated arguments.
4011          *
4012          * @since 3.0.0
4013          *
4014          * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true.
4015          */
4016         if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
4017                 if ( function_exists( '__' ) ) {
4018                         if ( ! is_null( $message ) ) {
4019                                 /* translators: 1: PHP function name, 2: version number, 3: optional message regarding the change */
4020                                 trigger_error( sprintf( __('%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s'), $function, $version, $message ) );
4021                         } else {
4022                                 /* translators: 1: PHP function name, 2: version number */
4023                                 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 ) );
4024                         }
4025                 } else {
4026                         if ( ! is_null( $message ) ) {
4027                                 trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) );
4028                         } else {
4029                                 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 ) );
4030                         }
4031                 }
4032         }
4033 }
4034
4035 /**
4036  * Marks a deprecated action or filter hook as deprecated and throws a notice.
4037  *
4038  * Use the {@see 'deprecated_hook_run'} action to get the backtrace describing where
4039  * the deprecated hook was called.
4040  *
4041  * Default behavior is to trigger a user error if `WP_DEBUG` is true.
4042  *
4043  * This function is called by the do_action_deprecated() and apply_filters_deprecated()
4044  * functions, and so generally does not need to be called directly.
4045  *
4046  * @since 4.6.0
4047  * @access private
4048  *
4049  * @param string $hook        The hook that was used.
4050  * @param string $version     The version of WordPress that deprecated the hook.
4051  * @param string $replacement Optional. The hook that should have been used.
4052  * @param string $message     Optional. A message regarding the change.
4053  */
4054 function _deprecated_hook( $hook, $version, $replacement = null, $message = null ) {
4055         /**
4056          * Fires when a deprecated hook is called.
4057          *
4058          * @since 4.6.0
4059          *
4060          * @param string $hook        The hook that was called.
4061          * @param string $replacement The hook that should be used as a replacement.
4062          * @param string $version     The version of WordPress that deprecated the argument used.
4063          * @param string $message     A message regarding the change.
4064          */
4065         do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message );
4066
4067         /**
4068          * Filters whether to trigger deprecated hook errors.
4069          *
4070          * @since 4.6.0
4071          *
4072          * @param bool $trigger Whether to trigger deprecated hook errors. Requires
4073          *                      `WP_DEBUG` to be defined true.
4074          */
4075         if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) {
4076                 $message = empty( $message ) ? '' : ' ' . $message;
4077                 if ( ! is_null( $replacement ) ) {
4078                         /* translators: 1: WordPress hook name, 2: version number, 3: alternative hook name */
4079                         trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $hook, $version, $replacement ) . $message );
4080                 } else {
4081                         /* translators: 1: WordPress hook name, 2: version number */
4082                         trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $hook, $version ) . $message );
4083                 }
4084         }
4085 }
4086
4087 /**
4088  * Mark something as being incorrectly called.
4089  *
4090  * There is a hook {@see 'doing_it_wrong_run'} that will be called that can be used
4091  * to get the backtrace up to what file and function called the deprecated
4092  * function.
4093  *
4094  * The current behavior is to trigger a user error if `WP_DEBUG` is true.
4095  *
4096  * @since 3.1.0
4097  * @access private
4098  *
4099  * @param string $function The function that was called.
4100  * @param string $message  A message explaining what has been done incorrectly.
4101  * @param string $version  The version of WordPress where the message was added.
4102  */
4103 function _doing_it_wrong( $function, $message, $version ) {
4104
4105         /**
4106          * Fires when the given function is being used incorrectly.
4107          *
4108          * @since 3.1.0
4109          *
4110          * @param string $function The function that was called.
4111          * @param string $message  A message explaining what has been done incorrectly.
4112          * @param string $version  The version of WordPress where the message was added.
4113          */
4114         do_action( 'doing_it_wrong_run', $function, $message, $version );
4115
4116         /**
4117          * Filters whether to trigger an error for _doing_it_wrong() calls.
4118          *
4119          * @since 3.1.0
4120          *
4121          * @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true.
4122          */
4123         if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
4124                 if ( function_exists( '__' ) ) {
4125                         if ( is_null( $version ) ) {
4126                                 $version = '';
4127                         } else {
4128                                 /* translators: %s: version number */
4129                                 $version = sprintf( __( '(This message was added in version %s.)' ), $version );
4130                         }
4131                         /* translators: %s: Codex URL */
4132                         $message .= ' ' . sprintf( __( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
4133                                 __( 'https://codex.wordpress.org/Debugging_in_WordPress' )
4134                         );
4135                         /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message */
4136                         trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
4137                 } else {
4138                         if ( is_null( $version ) ) {
4139                                 $version = '';
4140                         } else {
4141                                 $version = sprintf( '(This message was added in version %s.)', $version );
4142                         }
4143                         $message .= sprintf( ' Please see <a href="%s">Debugging in WordPress</a> for more information.',
4144                                 'https://codex.wordpress.org/Debugging_in_WordPress'
4145                         );
4146                         trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
4147                 }
4148         }
4149 }
4150
4151 /**
4152  * Is the server running earlier than 1.5.0 version of lighttpd?
4153  *
4154  * @since 2.5.0
4155  *
4156  * @return bool Whether the server is running lighttpd < 1.5.0.
4157  */
4158 function is_lighttpd_before_150() {
4159         $server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] )? $_SERVER['SERVER_SOFTWARE'] : '' );
4160         $server_parts[1] = isset( $server_parts[1] )? $server_parts[1] : '';
4161         return  'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' );
4162 }
4163
4164 /**
4165  * Does the specified module exist in the Apache config?
4166  *
4167  * @since 2.5.0
4168  *
4169  * @global bool $is_apache
4170  *
4171  * @param string $mod     The module, e.g. mod_rewrite.
4172  * @param bool   $default Optional. The default return value if the module is not found. Default false.
4173  * @return bool Whether the specified module is loaded.
4174  */
4175 function apache_mod_loaded($mod, $default = false) {
4176         global $is_apache;
4177
4178         if ( !$is_apache )
4179                 return false;
4180
4181         if ( function_exists( 'apache_get_modules' ) ) {
4182                 $mods = apache_get_modules();
4183                 if ( in_array($mod, $mods) )
4184                         return true;
4185         } elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) {
4186                         ob_start();
4187                         phpinfo(8);
4188                         $phpinfo = ob_get_clean();
4189                         if ( false !== strpos($phpinfo, $mod) )
4190                                 return true;
4191         }
4192         return $default;
4193 }
4194
4195 /**
4196  * Check if IIS 7+ supports pretty permalinks.
4197  *
4198  * @since 2.8.0
4199  *
4200  * @global bool $is_iis7
4201  *
4202  * @return bool Whether IIS7 supports permalinks.
4203  */
4204 function iis7_supports_permalinks() {
4205         global $is_iis7;
4206
4207         $supports_permalinks = false;
4208         if ( $is_iis7 ) {
4209                 /* First we check if the DOMDocument class exists. If it does not exist, then we cannot
4210                  * easily update the xml configuration file, hence we just bail out and tell user that
4211                  * pretty permalinks cannot be used.
4212                  *
4213                  * Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
4214                  * URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
4215                  * Lastly we make sure that PHP is running via FastCGI. This is important because if it runs
4216                  * via ISAPI then pretty permalinks will not work.
4217                  */
4218                 $supports_permalinks = class_exists( 'DOMDocument', false ) && isset($_SERVER['IIS_UrlRewriteModule']) && ( PHP_SAPI == 'cgi-fcgi' );
4219         }
4220
4221         /**
4222          * Filters whether IIS 7+ supports pretty permalinks.
4223          *
4224          * @since 2.8.0
4225          *
4226          * @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false.
4227          */
4228         return apply_filters( 'iis7_supports_permalinks', $supports_permalinks );
4229 }
4230
4231 /**
4232  * File validates against allowed set of defined rules.
4233  *
4234  * A return value of '1' means that the $file contains either '..' or './'. A
4235  * return value of '2' means that the $file contains ':' after the first
4236  * character. A return value of '3' means that the file is not in the allowed
4237  * files list.
4238  *
4239  * @since 1.2.0
4240  *
4241  * @param string $file File path.
4242  * @param array  $allowed_files List of allowed files.
4243  * @return int 0 means nothing is wrong, greater than 0 means something was wrong.
4244  */
4245 function validate_file( $file, $allowed_files = '' ) {
4246         if ( false !== strpos( $file, '..' ) )
4247                 return 1;
4248
4249         if ( false !== strpos( $file, './' ) )
4250                 return 1;
4251
4252         if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files ) )
4253                 return 3;
4254
4255         if (':' == substr( $file, 1, 1 ) )
4256                 return 2;
4257
4258         return 0;
4259 }
4260
4261 /**
4262  * Whether to force SSL used for the Administration Screens.
4263  *
4264  * @since 2.6.0
4265  *
4266  * @staticvar bool $forced
4267  *
4268  * @param string|bool $force Optional. Whether to force SSL in admin screens. Default null.
4269  * @return bool True if forced, false if not forced.
4270  */
4271 function force_ssl_admin( $force = null ) {
4272         static $forced = false;
4273
4274         if ( !is_null( $force ) ) {
4275                 $old_forced = $forced;
4276                 $forced = $force;
4277                 return $old_forced;
4278         }
4279
4280         return $forced;
4281 }
4282
4283 /**
4284  * Guess the URL for the site.
4285  *
4286  * Will remove wp-admin links to retrieve only return URLs not in the wp-admin
4287  * directory.
4288  *
4289  * @since 2.6.0
4290  *
4291  * @return string The guessed URL.
4292  */
4293 function wp_guess_url() {
4294         if ( defined('WP_SITEURL') && '' != WP_SITEURL ) {
4295                 $url = WP_SITEURL;
4296         } else {
4297                 $abspath_fix = str_replace( '\\', '/', ABSPATH );
4298                 $script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );
4299
4300                 // The request is for the admin
4301                 if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) {
4302                         $path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] );
4303
4304                 // The request is for a file in ABSPATH
4305                 } elseif ( $script_filename_dir . '/' == $abspath_fix ) {
4306                         // Strip off any file/query params in the path
4307                         $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );
4308
4309                 } else {
4310                         if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
4311                                 // Request is hitting a file inside ABSPATH
4312                                 $directory = str_replace( ABSPATH, '', $script_filename_dir );
4313                                 // Strip off the sub directory, and any file/query params
4314                                 $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] );
4315                         } elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) {
4316                                 // Request is hitting a file above ABSPATH
4317                                 $subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
4318                                 // Strip off any file/query params from the path, appending the sub directory to the install
4319                                 $path = preg_replace( '#/[^/]*$#i', '' , $_SERVER['REQUEST_URI'] ) . $subdirectory;
4320                         } else {
4321                                 $path = $_SERVER['REQUEST_URI'];
4322                         }
4323                 }
4324
4325                 $schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet
4326                 $url = $schema . $_SERVER['HTTP_HOST'] . $path;
4327         }
4328
4329         return rtrim($url, '/');
4330 }
4331
4332 /**
4333  * Temporarily suspend cache additions.
4334  *
4335  * Stops more data being added to the cache, but still allows cache retrieval.
4336  * This is useful for actions, such as imports, when a lot of data would otherwise
4337  * be almost uselessly added to the cache.
4338  *
4339  * Suspension lasts for a single page load at most. Remember to call this
4340  * function again if you wish to re-enable cache adds earlier.
4341  *
4342  * @since 3.3.0
4343  *
4344  * @staticvar bool $_suspend
4345  *
4346  * @param bool $suspend Optional. Suspends additions if true, re-enables them if false.
4347  * @return bool The current suspend setting
4348  */
4349 function wp_suspend_cache_addition( $suspend = null ) {
4350         static $_suspend = false;
4351
4352         if ( is_bool( $suspend ) )
4353                 $_suspend = $suspend;
4354
4355         return $_suspend;
4356 }
4357
4358 /**
4359  * Suspend cache invalidation.
4360  *
4361  * Turns cache invalidation on and off. Useful during imports where you don't wont to do
4362  * invalidations every time a post is inserted. Callers must be sure that what they are
4363  * doing won't lead to an inconsistent cache when invalidation is suspended.
4364  *
4365  * @since 2.7.0
4366  *
4367  * @global bool $_wp_suspend_cache_invalidation
4368  *
4369  * @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true.
4370  * @return bool The current suspend setting.
4371  */
4372 function wp_suspend_cache_invalidation( $suspend = true ) {
4373         global $_wp_suspend_cache_invalidation;
4374
4375         $current_suspend = $_wp_suspend_cache_invalidation;
4376         $_wp_suspend_cache_invalidation = $suspend;
4377         return $current_suspend;
4378 }
4379
4380 /**
4381  * Determine whether a site is the main site of the current network.
4382  *
4383  * @since 3.0.0
4384  *
4385  * @param int $site_id Optional. Site ID to test. Defaults to current site.
4386  * @return bool True if $site_id is the main site of the network, or if not
4387  *              running Multisite.
4388  */
4389 function is_main_site( $site_id = null ) {
4390         if ( ! is_multisite() )
4391                 return true;
4392
4393         if ( ! $site_id )
4394                 $site_id = get_current_blog_id();
4395
4396         return (int) $site_id === (int) get_network()->site_id;
4397 }
4398
4399 /**
4400  * Determine whether a network is the main network of the Multisite install.
4401  *
4402  * @since 3.7.0
4403  *
4404  * @param int $network_id Optional. Network ID to test. Defaults to current network.
4405  * @return bool True if $network_id is the main network, or if not running Multisite.
4406  */
4407 function is_main_network( $network_id = null ) {
4408         if ( ! is_multisite() ) {
4409                 return true;
4410         }
4411
4412         if ( null === $network_id ) {
4413                 $network_id = get_current_network_id();
4414         }
4415
4416         $network_id = (int) $network_id;
4417
4418         return ( $network_id === get_main_network_id() );
4419 }
4420
4421 /**
4422  * Get the main network ID.
4423  *
4424  * @since 4.3.0
4425  *
4426  * @return int The ID of the main network.
4427  */
4428 function get_main_network_id() {
4429         if ( ! is_multisite() ) {
4430                 return 1;
4431         }
4432
4433         $current_network = get_network();
4434
4435         if ( defined( 'PRIMARY_NETWORK_ID' ) ) {
4436                 $main_network_id = PRIMARY_NETWORK_ID;
4437         } elseif ( isset( $current_network->id ) && 1 === (int) $current_network->id ) {
4438                 // If the current network has an ID of 1, assume it is the main network.
4439                 $main_network_id = 1;
4440         } else {
4441                 $_networks = get_networks( array( 'fields' => 'ids', 'number' => 1 ) );
4442                 $main_network_id = array_shift( $_networks );
4443         }
4444
4445         /**
4446          * Filters the main network ID.
4447          *
4448          * @since 4.3.0
4449          *
4450          * @param int $main_network_id The ID of the main network.
4451          */
4452         return (int) apply_filters( 'get_main_network_id', $main_network_id );
4453 }
4454
4455 /**
4456  * Determine whether global terms are enabled.
4457  *
4458  * @since 3.0.0
4459  *
4460  * @staticvar bool $global_terms
4461  *
4462  * @return bool True if multisite and global terms enabled.
4463  */
4464 function global_terms_enabled() {
4465         if ( ! is_multisite() )
4466                 return false;
4467
4468         static $global_terms = null;
4469         if ( is_null( $global_terms ) ) {
4470
4471                 /**
4472                  * Filters whether global terms are enabled.
4473                  *
4474                  * Passing a non-null value to the filter will effectively short-circuit the function,
4475                  * returning the value of the 'global_terms_enabled' site option instead.
4476                  *
4477                  * @since 3.0.0
4478                  *
4479                  * @param null $enabled Whether global terms are enabled.
4480                  */
4481                 $filter = apply_filters( 'global_terms_enabled', null );
4482                 if ( ! is_null( $filter ) )
4483                         $global_terms = (bool) $filter;
4484                 else
4485                         $global_terms = (bool) get_site_option( 'global_terms_enabled', false );
4486         }
4487         return $global_terms;
4488 }
4489
4490 /**
4491  * gmt_offset modification for smart timezone handling.
4492  *
4493  * Overrides the gmt_offset option if we have a timezone_string available.
4494  *
4495  * @since 2.8.0
4496  *
4497  * @return float|false Timezone GMT offset, false otherwise.
4498  */
4499 function wp_timezone_override_offset() {
4500         if ( !$timezone_string = get_option( 'timezone_string' ) ) {
4501                 return false;
4502         }
4503
4504         $timezone_object = timezone_open( $timezone_string );
4505         $datetime_object = date_create();
4506         if ( false === $timezone_object || false === $datetime_object ) {
4507                 return false;
4508         }
4509         return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 );
4510 }
4511
4512 /**
4513  * Sort-helper for timezones.
4514  *
4515  * @since 2.9.0
4516  * @access private
4517  *
4518  * @param array $a
4519  * @param array $b
4520  * @return int
4521  */
4522 function _wp_timezone_choice_usort_callback( $a, $b ) {
4523         // Don't use translated versions of Etc
4524         if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
4525                 // Make the order of these more like the old dropdown
4526                 if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4527                         return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
4528                 }
4529                 if ( 'UTC' === $a['city'] ) {
4530                         if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
4531                                 return 1;
4532                         }
4533                         return -1;
4534                 }
4535                 if ( 'UTC' === $b['city'] ) {
4536                         if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
4537                                 return -1;
4538                         }
4539                         return 1;
4540                 }
4541                 return strnatcasecmp( $a['city'], $b['city'] );
4542         }
4543         if ( $a['t_continent'] == $b['t_continent'] ) {
4544                 if ( $a['t_city'] == $b['t_city'] ) {
4545                         return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
4546                 }
4547                 return strnatcasecmp( $a['t_city'], $b['t_city'] );
4548         } else {
4549                 // Force Etc to the bottom of the list
4550                 if ( 'Etc' === $a['continent'] ) {
4551                         return 1;
4552                 }
4553                 if ( 'Etc' === $b['continent'] ) {
4554                         return -1;
4555                 }
4556                 return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
4557         }
4558 }
4559
4560 /**
4561  * Gives a nicely-formatted list of timezone strings.
4562  *
4563  * @since 2.9.0
4564  * @since 4.7.0 Added the `$locale` parameter.
4565  *
4566  * @staticvar bool $mo_loaded
4567  * @staticvar string $locale_loaded
4568  *
4569  * @param string $selected_zone Selected timezone.
4570  * @param string $locale        Optional. Locale to load the timezones in. Default current site locale.
4571  * @return string
4572  */
4573 function wp_timezone_choice( $selected_zone, $locale = null ) {
4574         static $mo_loaded = false, $locale_loaded = null;
4575
4576         $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
4577
4578         // Load translations for continents and cities.
4579         if ( ! $mo_loaded || $locale !== $locale_loaded ) {
4580                 $locale_loaded = $locale ? $locale : get_locale();
4581                 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo';
4582                 unload_textdomain( 'continents-cities' );
4583                 load_textdomain( 'continents-cities', $mofile );
4584                 $mo_loaded = true;
4585         }
4586
4587         $zonen = array();
4588         foreach ( timezone_identifiers_list() as $zone ) {
4589                 $zone = explode( '/', $zone );
4590                 if ( !in_array( $zone[0], $continents ) ) {
4591                         continue;
4592                 }
4593
4594                 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
4595                 $exists = array(
4596                         0 => ( isset( $zone[0] ) && $zone[0] ),
4597                         1 => ( isset( $zone[1] ) && $zone[1] ),
4598                         2 => ( isset( $zone[2] ) && $zone[2] ),
4599                 );
4600                 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
4601                 $exists[4] = ( $exists[1] && $exists[3] );
4602                 $exists[5] = ( $exists[2] && $exists[3] );
4603
4604                 $zonen[] = array(
4605                         'continent'   => ( $exists[0] ? $zone[0] : '' ),
4606                         'city'        => ( $exists[1] ? $zone[1] : '' ),
4607                         'subcity'     => ( $exists[2] ? $zone[2] : '' ),
4608                         't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
4609                         't_city'      => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
4610                         't_subcity'   => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' )
4611                 );
4612         }
4613         usort( $zonen, '_wp_timezone_choice_usort_callback' );
4614
4615         $structure = array();
4616
4617         if ( empty( $selected_zone ) ) {
4618                 $structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>';
4619         }
4620
4621         foreach ( $zonen as $key => $zone ) {
4622                 // Build value in an array to join later
4623                 $value = array( $zone['continent'] );
4624
4625                 if ( empty( $zone['city'] ) ) {
4626                         // It's at the continent level (generally won't happen)
4627                         $display = $zone['t_continent'];
4628                 } else {
4629                         // It's inside a continent group
4630
4631                         // Continent optgroup
4632                         if ( !isset( $zonen[$key - 1] ) || $zonen[$key - 1]['continent'] !== $zone['continent'] ) {
4633                                 $label = $zone['t_continent'];
4634                                 $structure[] = '<optgroup label="'. esc_attr( $label ) .'">';
4635                         }
4636
4637                         // Add the city to the value
4638                         $value[] = $zone['city'];
4639
4640                         $display = $zone['t_city'];
4641                         if ( !empty( $zone['subcity'] ) ) {
4642                                 // Add the subcity to the value
4643                                 $value[] = $zone['subcity'];
4644                                 $display .= ' - ' . $zone['t_subcity'];
4645                         }
4646                 }
4647
4648                 // Build the value
4649                 $value = join( '/', $value );
4650                 $selected = '';
4651                 if ( $value === $selected_zone ) {
4652                         $selected = 'selected="selected" ';
4653                 }
4654                 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . "</option>";
4655
4656                 // Close continent optgroup
4657                 if ( !empty( $zone['city'] ) && ( !isset($zonen[$key + 1]) || (isset( $zonen[$key + 1] ) && $zonen[$key + 1]['continent'] !== $zone['continent']) ) ) {
4658                         $structure[] = '</optgroup>';
4659                 }
4660         }
4661
4662         // Do UTC
4663         $structure[] = '<optgroup label="'. esc_attr__( 'UTC' ) .'">';
4664         $selected = '';
4665         if ( 'UTC' === $selected_zone )
4666                 $selected = 'selected="selected" ';
4667         $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __('UTC') . '</option>';
4668         $structure[] = '</optgroup>';
4669
4670         // Do manual UTC offsets
4671         $structure[] = '<optgroup label="'. esc_attr__( 'Manual Offsets' ) .'">';
4672         $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,
4673                 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);
4674         foreach ( $offset_range as $offset ) {
4675                 if ( 0 <= $offset )
4676                         $offset_name = '+' . $offset;
4677                 else
4678                         $offset_name = (string) $offset;
4679
4680                 $offset_value = $offset_name;
4681                 $offset_name = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset_name);
4682                 $offset_name = 'UTC' . $offset_name;
4683                 $offset_value = 'UTC' . $offset_value;
4684                 $selected = '';
4685                 if ( $offset_value === $selected_zone )
4686                         $selected = 'selected="selected" ';
4687                 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . "</option>";
4688
4689         }
4690         $structure[] = '</optgroup>';
4691
4692         return join( "\n", $structure );
4693 }
4694
4695 /**
4696  * Strip close comment and close php tags from file headers used by WP.
4697  *
4698  * @since 2.8.0
4699  * @access private
4700  *
4701  * @see https://core.trac.wordpress.org/ticket/8497
4702  *
4703  * @param string $str Header comment to clean up.
4704  * @return string
4705  */
4706 function _cleanup_header_comment( $str ) {
4707         return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
4708 }
4709
4710 /**
4711  * Permanently delete comments or posts of any type that have held a status
4712  * of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
4713  *
4714  * The default value of `EMPTY_TRASH_DAYS` is 30 (days).
4715  *
4716  * @since 2.9.0
4717  *
4718  * @global wpdb $wpdb WordPress database abstraction object.
4719  */
4720 function wp_scheduled_delete() {
4721         global $wpdb;
4722
4723         $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
4724
4725         $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);
4726
4727         foreach ( (array) $posts_to_delete as $post ) {
4728                 $post_id = (int) $post['post_id'];
4729                 if ( !$post_id )
4730                         continue;
4731
4732                 $del_post = get_post($post_id);
4733
4734                 if ( !$del_post || 'trash' != $del_post->post_status ) {
4735                         delete_post_meta($post_id, '_wp_trash_meta_status');
4736                         delete_post_meta($post_id, '_wp_trash_meta_time');
4737                 } else {
4738                         wp_delete_post($post_id);
4739                 }
4740         }
4741
4742         $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);
4743
4744         foreach ( (array) $comments_to_delete as $comment ) {
4745                 $comment_id = (int) $comment['comment_id'];
4746                 if ( !$comment_id )
4747                         continue;
4748
4749                 $del_comment = get_comment($comment_id);
4750
4751                 if ( !$del_comment || 'trash' != $del_comment->comment_approved ) {
4752                         delete_comment_meta($comment_id, '_wp_trash_meta_time');
4753                         delete_comment_meta($comment_id, '_wp_trash_meta_status');
4754                 } else {
4755                         wp_delete_comment( $del_comment );
4756                 }
4757         }
4758 }
4759
4760 /**
4761  * Retrieve metadata from a file.
4762  *
4763  * Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
4764  * Each piece of metadata must be on its own line. Fields can not span multiple
4765  * lines, the value will get cut at the end of the first line.
4766  *
4767  * If the file data is not within that first 8kiB, then the author should correct
4768  * their plugin file and move the data headers to the top.
4769  *
4770  * @link https://codex.wordpress.org/File_Header
4771  *
4772  * @since 2.9.0
4773  *
4774  * @param string $file            Path to the file.
4775  * @param array  $default_headers List of headers, in the format array('HeaderKey' => 'Header Name').
4776  * @param string $context         Optional. If specified adds filter hook {@see 'extra_$context_headers'}.
4777  *                                Default empty.
4778  * @return array Array of file headers in `HeaderKey => Header Value` format.
4779  */
4780 function get_file_data( $file, $default_headers, $context = '' ) {
4781         // We don't need to write to the file, so just open for reading.
4782         $fp = fopen( $file, 'r' );
4783
4784         // Pull only the first 8kiB of the file in.
4785         $file_data = fread( $fp, 8192 );
4786
4787         // PHP will close file handle, but we are good citizens.
4788         fclose( $fp );
4789
4790         // Make sure we catch CR-only line endings.
4791         $file_data = str_replace( "\r", "\n", $file_data );
4792
4793         /**
4794          * Filters extra file headers by context.
4795          *
4796          * The dynamic portion of the hook name, `$context`, refers to
4797          * the context where extra headers might be loaded.
4798          *
4799          * @since 2.9.0
4800          *
4801          * @param array $extra_context_headers Empty array by default.
4802          */
4803         if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
4804                 $extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
4805                 $all_headers = array_merge( $extra_headers, (array) $default_headers );
4806         } else {
4807                 $all_headers = $default_headers;
4808         }
4809
4810         foreach ( $all_headers as $field => $regex ) {
4811                 if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
4812                         $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
4813                 else
4814                         $all_headers[ $field ] = '';
4815         }
4816
4817         return $all_headers;
4818 }
4819
4820 /**
4821  * Returns true.
4822  *
4823  * Useful for returning true to filters easily.
4824  *
4825  * @since 3.0.0
4826  *
4827  * @see __return_false()
4828  *
4829  * @return true True.
4830  */
4831 function __return_true() {
4832         return true;
4833 }
4834
4835 /**
4836  * Returns false.
4837  *
4838  * Useful for returning false to filters easily.
4839  *
4840  * @since 3.0.0
4841  *
4842  * @see __return_true()
4843  *
4844  * @return false False.
4845  */
4846 function __return_false() {
4847         return false;
4848 }
4849
4850 /**
4851  * Returns 0.
4852  *
4853  * Useful for returning 0 to filters easily.
4854  *
4855  * @since 3.0.0
4856  *
4857  * @return int 0.
4858  */
4859 function __return_zero() {
4860         return 0;
4861 }
4862
4863 /**
4864  * Returns an empty array.
4865  *
4866  * Useful for returning an empty array to filters easily.
4867  *
4868  * @since 3.0.0
4869  *
4870  * @return array Empty array.
4871  */
4872 function __return_empty_array() {
4873         return array();
4874 }
4875
4876 /**
4877  * Returns null.
4878  *
4879  * Useful for returning null to filters easily.
4880  *
4881  * @since 3.4.0
4882  *
4883  * @return null Null value.
4884  */
4885 function __return_null() {
4886         return null;
4887 }
4888
4889 /**
4890  * Returns an empty string.
4891  *
4892  * Useful for returning an empty string to filters easily.
4893  *
4894  * @since 3.7.0
4895  *
4896  * @see __return_null()
4897  *
4898  * @return string Empty string.
4899  */
4900 function __return_empty_string() {
4901         return '';
4902 }
4903
4904 /**
4905  * Send a HTTP header to disable content type sniffing in browsers which support it.
4906  *
4907  * @since 3.0.0
4908  *
4909  * @see https://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx
4910  * @see https://src.chromium.org/viewvc/chrome?view=rev&revision=6985
4911  */
4912 function send_nosniff_header() {
4913         @header( 'X-Content-Type-Options: nosniff' );
4914 }
4915
4916 /**
4917  * Return a MySQL expression for selecting the week number based on the start_of_week option.
4918  *
4919  * @ignore
4920  * @since 3.0.0
4921  *
4922  * @param string $column Database column.
4923  * @return string SQL clause.
4924  */
4925 function _wp_mysql_week( $column ) {
4926         switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) {
4927         case 1 :
4928                 return "WEEK( $column, 1 )";
4929         case 2 :
4930         case 3 :
4931         case 4 :
4932         case 5 :
4933         case 6 :
4934                 return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )";
4935         case 0 :
4936         default :
4937                 return "WEEK( $column, 0 )";
4938         }
4939 }
4940
4941 /**
4942  * Find hierarchy loops using a callback function that maps object IDs to parent IDs.
4943  *
4944  * @since 3.1.0
4945  * @access private
4946  *
4947  * @param callable $callback      Function that accepts ( ID, $callback_args ) and outputs parent_ID.
4948  * @param int      $start         The ID to start the loop check at.
4949  * @param int      $start_parent  The parent_ID of $start to use instead of calling $callback( $start ).
4950  *                                Use null to always use $callback
4951  * @param array    $callback_args Optional. Additional arguments to send to $callback.
4952  * @return array IDs of all members of loop.
4953  */
4954 function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
4955         $override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
4956
4957         if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
4958                 return array();
4959
4960         return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
4961 }
4962
4963 /**
4964  * Use the "The Tortoise and the Hare" algorithm to detect loops.
4965  *
4966  * For every step of the algorithm, the hare takes two steps and the tortoise one.
4967  * If the hare ever laps the tortoise, there must be a loop.
4968  *
4969  * @since 3.1.0
4970  * @access private
4971  *
4972  * @param callable $callback      Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
4973  * @param int      $start         The ID to start the loop check at.
4974  * @param array    $override      Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback.
4975  *                                Default empty array.
4976  * @param array    $callback_args Optional. Additional arguments to send to $callback. Default empty array.
4977  * @param bool     $_return_loop  Optional. Return loop members or just detect presence of loop? Only set
4978  *                                to true if you already know the given $start is part of a loop (otherwise
4979  *                                the returned array might include branches). Default false.
4980  * @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if
4981  *               $_return_loop
4982  */
4983 function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
4984         $tortoise = $hare = $evanescent_hare = $start;
4985         $return = array();
4986
4987         // Set evanescent_hare to one past hare
4988         // Increment hare two steps
4989         while (
4990                 $tortoise
4991         &&
4992                 ( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
4993         &&
4994                 ( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
4995         ) {
4996                 if ( $_return_loop )
4997                         $return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
4998
4999                 // tortoise got lapped - must be a loop
5000                 if ( $tortoise == $evanescent_hare || $tortoise == $hare )
5001                         return $_return_loop ? $return : $tortoise;
5002
5003                 // Increment tortoise by one step
5004                 $tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
5005         }
5006
5007         return false;
5008 }
5009
5010 /**
5011  * Send a HTTP header to limit rendering of pages to same origin iframes.
5012  *
5013  * @since 3.1.3
5014  *
5015  * @see https://developer.mozilla.org/en/the_x-frame-options_response_header
5016  */
5017 function send_frame_options_header() {
5018         @header( 'X-Frame-Options: SAMEORIGIN' );
5019 }
5020
5021 /**
5022  * Retrieve a list of protocols to allow in HTML attributes.
5023  *
5024  * @since 3.3.0
5025  * @since 4.3.0 Added 'webcal' to the protocols array.
5026  * @since 4.7.0 Added 'urn' to the protocols array.
5027  *
5028  * @see wp_kses()
5029  * @see esc_url()
5030  *
5031  * @staticvar array $protocols
5032  *
5033  * @return array Array of allowed protocols. Defaults to an array containing 'http', 'https',
5034  *               'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet',
5035  *               'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', and 'urn'.
5036  */
5037 function wp_allowed_protocols() {
5038         static $protocols = array();
5039
5040         if ( empty( $protocols ) ) {
5041                 $protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn' );
5042
5043                 /**
5044                  * Filters the list of protocols allowed in HTML attributes.
5045                  *
5046                  * @since 3.0.0
5047                  *
5048                  * @param array $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more.
5049                  */
5050                 $protocols = apply_filters( 'kses_allowed_protocols', $protocols );
5051         }
5052
5053         return $protocols;
5054 }
5055
5056 /**
5057  * Return a comma-separated string of functions that have been called to get
5058  * to the current point in code.
5059  *
5060  * @since 3.4.0
5061  *
5062  * @see https://core.trac.wordpress.org/ticket/19589
5063  *
5064  * @param string $ignore_class Optional. A class to ignore all function calls within - useful
5065  *                             when you want to just give info about the callee. Default null.
5066  * @param int    $skip_frames  Optional. A number of stack frames to skip - useful for unwinding
5067  *                             back to the source of the issue. Default 0.
5068  * @param bool   $pretty       Optional. Whether or not you want a comma separated string or raw
5069  *                             array returned. Default true.
5070  * @return string|array Either a string containing a reversed comma separated trace or an array
5071  *                      of individual calls.
5072  */
5073 function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
5074         if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) )
5075                 $trace = debug_backtrace( false );
5076         else
5077                 $trace = debug_backtrace();
5078
5079         $caller = array();
5080         $check_class = ! is_null( $ignore_class );
5081         $skip_frames++; // skip this function
5082
5083         foreach ( $trace as $call ) {
5084                 if ( $skip_frames > 0 ) {
5085                         $skip_frames--;
5086                 } elseif ( isset( $call['class'] ) ) {
5087                         if ( $check_class && $ignore_class == $call['class'] )
5088                                 continue; // Filter out calls
5089
5090                         $caller[] = "{$call['class']}{$call['type']}{$call['function']}";
5091                 } else {
5092                         if ( in_array( $call['function'], array( 'do_action', 'apply_filters' ) ) ) {
5093                                 $caller[] = "{$call['function']}('{$call['args'][0]}')";
5094                         } elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) {
5095                                 $caller[] = $call['function'] . "('" . str_replace( array( WP_CONTENT_DIR, ABSPATH ) , '', $call['args'][0] ) . "')";
5096                         } else {
5097                                 $caller[] = $call['function'];
5098                         }
5099                 }
5100         }
5101         if ( $pretty )
5102                 return join( ', ', array_reverse( $caller ) );
5103         else
5104                 return $caller;
5105 }
5106
5107 /**
5108  * Retrieve ids that are not already present in the cache.
5109  *
5110  * @since 3.4.0
5111  * @access private
5112  *
5113  * @param array  $object_ids ID list.
5114  * @param string $cache_key  The cache bucket to check against.
5115  *
5116  * @return array List of ids not present in the cache.
5117  */
5118 function _get_non_cached_ids( $object_ids, $cache_key ) {
5119         $clean = array();
5120         foreach ( $object_ids as $id ) {
5121                 $id = (int) $id;
5122                 if ( !wp_cache_get( $id, $cache_key ) ) {
5123                         $clean[] = $id;
5124                 }
5125         }
5126
5127         return $clean;
5128 }
5129
5130 /**
5131  * Test if the current device has the capability to upload files.
5132  *
5133  * @since 3.4.0
5134  * @access private
5135  *
5136  * @return bool Whether the device is able to upload files.
5137  */
5138 function _device_can_upload() {
5139         if ( ! wp_is_mobile() )
5140                 return true;
5141
5142         $ua = $_SERVER['HTTP_USER_AGENT'];
5143
5144         if ( strpos($ua, 'iPhone') !== false
5145                 || strpos($ua, 'iPad') !== false
5146                 || strpos($ua, 'iPod') !== false ) {
5147                         return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' );
5148         }
5149
5150         return true;
5151 }
5152
5153 /**
5154  * Test if a given path is a stream URL
5155  *
5156  * @param string $path The resource path or URL.
5157  * @return bool True if the path is a stream URL.
5158  */
5159 function wp_is_stream( $path ) {
5160         $wrappers = stream_get_wrappers();
5161         $wrappers_re = '(' . join('|', $wrappers) . ')';
5162
5163         return preg_match( "!^$wrappers_re://!", $path ) === 1;
5164 }
5165
5166 /**
5167  * Test if the supplied date is valid for the Gregorian calendar.
5168  *
5169  * @since 3.5.0
5170  *
5171  * @see checkdate()
5172  *
5173  * @param  int    $month       Month number.
5174  * @param  int    $day         Day number.
5175  * @param  int    $year        Year number.
5176  * @param  string $source_date The date to filter.
5177  * @return bool True if valid date, false if not valid date.
5178  */
5179 function wp_checkdate( $month, $day, $year, $source_date ) {
5180         /**
5181          * Filters whether the given date is valid for the Gregorian calendar.
5182          *
5183          * @since 3.5.0
5184          *
5185          * @param bool   $checkdate   Whether the given date is valid.
5186          * @param string $source_date Date to check.
5187          */
5188         return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
5189 }
5190
5191 /**
5192  * Load the auth check for monitoring whether the user is still logged in.
5193  *
5194  * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' );
5195  *
5196  * This is disabled for certain screens where a login screen could cause an
5197  * inconvenient interruption. A filter called {@see 'wp_auth_check_load'} can be used
5198  * for fine-grained control.
5199  *
5200  * @since 3.6.0
5201  */
5202 function wp_auth_check_load() {
5203         if ( ! is_admin() && ! is_user_logged_in() )
5204                 return;
5205
5206         if ( defined( 'IFRAME_REQUEST' ) )
5207                 return;
5208
5209         $screen = get_current_screen();
5210         $hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' );
5211         $show = ! in_array( $screen->id, $hidden );
5212
5213         /**
5214          * Filters whether to load the authentication check.
5215          *
5216          * Passing a falsey value to the filter will effectively short-circuit
5217          * loading the authentication check.
5218          *
5219          * @since 3.6.0
5220          *
5221          * @param bool      $show   Whether to load the authentication check.
5222          * @param WP_Screen $screen The current screen object.
5223          */
5224         if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) {
5225                 wp_enqueue_style( 'wp-auth-check' );
5226                 wp_enqueue_script( 'wp-auth-check' );
5227
5228                 add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 );
5229                 add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 );
5230         }
5231 }
5232
5233 /**
5234  * Output the HTML that shows the wp-login dialog when the user is no longer logged in.
5235  *
5236  * @since 3.6.0
5237  */
5238 function wp_auth_check_html() {
5239         $login_url = wp_login_url();
5240         $current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
5241         $same_domain = ( strpos( $login_url, $current_domain ) === 0 );
5242
5243         /**
5244          * Filters whether the authentication check originated at the same domain.
5245          *
5246          * @since 3.6.0
5247          *
5248          * @param bool $same_domain Whether the authentication check originated at the same domain.
5249          */
5250         $same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain );
5251         $wrap_class = $same_domain ? 'hidden' : 'hidden fallback';
5252
5253         ?>
5254         <div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>">
5255         <div id="wp-auth-check-bg"></div>
5256         <div id="wp-auth-check">
5257         <button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></button>
5258         <?php
5259
5260         if ( $same_domain ) {
5261                 ?>
5262                 <div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( add_query_arg( array( 'interim-login' => 1 ), $login_url ) ); ?>"></div>
5263                 <?php
5264         }
5265
5266         ?>
5267         <div class="wp-auth-fallback">
5268                 <p><b class="wp-auth-fallback-expired" tabindex="0"><?php _e('Session expired'); ?></b></p>
5269                 <p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php _e('Please log in again.'); ?></a>
5270                 <?php _e('The login page will open in a new window. After logging in you can close it and return to this page.'); ?></p>
5271         </div>
5272         </div>
5273         </div>
5274         <?php
5275 }
5276
5277 /**
5278  * Check whether a user is still logged in, for the heartbeat.
5279  *
5280  * Send a result that shows a log-in box if the user is no longer logged in,
5281  * or if their cookie is within the grace period.
5282  *
5283  * @since 3.6.0
5284  *
5285  * @global int $login_grace_period
5286  *
5287  * @param array $response  The Heartbeat response.
5288  * @return array $response The Heartbeat response with 'wp-auth-check' value set.
5289  */
5290 function wp_auth_check( $response ) {
5291         $response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] );
5292         return $response;
5293 }
5294
5295 /**
5296  * Return RegEx body to liberally match an opening HTML tag.
5297  *
5298  * Matches an opening HTML tag that:
5299  * 1. Is self-closing or
5300  * 2. Has no body but has a closing tag of the same name or
5301  * 3. Contains a body and a closing tag of the same name
5302  *
5303  * Note: this RegEx does not balance inner tags and does not attempt
5304  * to produce valid HTML
5305  *
5306  * @since 3.6.0
5307  *
5308  * @param string $tag An HTML tag name. Example: 'video'.
5309  * @return string Tag RegEx.
5310  */
5311 function get_tag_regex( $tag ) {
5312         if ( empty( $tag ) )
5313                 return;
5314         return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) );
5315 }
5316
5317 /**
5318  * Retrieve a canonical form of the provided charset appropriate for passing to PHP
5319  * functions such as htmlspecialchars() and charset html attributes.
5320  *
5321  * @since 3.6.0
5322  * @access private
5323  *
5324  * @see https://core.trac.wordpress.org/ticket/23688
5325  *
5326  * @param string $charset A charset name.
5327  * @return string The canonical form of the charset.
5328  */
5329 function _canonical_charset( $charset ) {
5330         if ( 'utf-8' === strtolower( $charset ) || 'utf8' === strtolower( $charset) ) {
5331
5332                 return 'UTF-8';
5333         }
5334
5335         if ( 'iso-8859-1' === strtolower( $charset ) || 'iso8859-1' === strtolower( $charset ) ) {
5336
5337                 return 'ISO-8859-1';
5338         }
5339
5340         return $charset;
5341 }
5342
5343 /**
5344  * Set the mbstring internal encoding to a binary safe encoding when func_overload
5345  * is enabled.
5346  *
5347  * When mbstring.func_overload is in use for multi-byte encodings, the results from
5348  * strlen() and similar functions respect the utf8 characters, causing binary data
5349  * to return incorrect lengths.
5350  *
5351  * This function overrides the mbstring encoding to a binary-safe encoding, and
5352  * resets it to the users expected encoding afterwards through the
5353  * `reset_mbstring_encoding` function.
5354  *
5355  * It is safe to recursively call this function, however each
5356  * `mbstring_binary_safe_encoding()` call must be followed up with an equal number
5357  * of `reset_mbstring_encoding()` calls.
5358  *
5359  * @since 3.7.0
5360  *
5361  * @see reset_mbstring_encoding()
5362  *
5363  * @staticvar array $encodings
5364  * @staticvar bool  $overloaded
5365  *
5366  * @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
5367  *                    Default false.
5368  */
5369 function mbstring_binary_safe_encoding( $reset = false ) {
5370         static $encodings = array();
5371         static $overloaded = null;
5372
5373         if ( is_null( $overloaded ) )
5374                 $overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 );
5375
5376         if ( false === $overloaded )
5377                 return;
5378
5379         if ( ! $reset ) {
5380                 $encoding = mb_internal_encoding();
5381                 array_push( $encodings, $encoding );
5382                 mb_internal_encoding( 'ISO-8859-1' );
5383         }
5384
5385         if ( $reset && $encodings ) {
5386                 $encoding = array_pop( $encodings );
5387                 mb_internal_encoding( $encoding );
5388         }
5389 }
5390
5391 /**
5392  * Reset the mbstring internal encoding to a users previously set encoding.
5393  *
5394  * @see mbstring_binary_safe_encoding()
5395  *
5396  * @since 3.7.0
5397  */
5398 function reset_mbstring_encoding() {
5399         mbstring_binary_safe_encoding( true );
5400 }
5401
5402 /**
5403  * Filter/validate a variable as a boolean.
5404  *
5405  * Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`.
5406  *
5407  * @since 4.0.0
5408  *
5409  * @param mixed $var Boolean value to validate.
5410  * @return bool Whether the value is validated.
5411  */
5412 function wp_validate_boolean( $var ) {
5413         if ( is_bool( $var ) ) {
5414                 return $var;
5415         }
5416
5417         if ( is_string( $var ) && 'false' === strtolower( $var ) ) {
5418                 return false;
5419         }
5420
5421         return (bool) $var;
5422 }
5423
5424 /**
5425  * Delete a file
5426  *
5427  * @since 4.2.0
5428  *
5429  * @param string $file The path to the file to delete.
5430  */
5431 function wp_delete_file( $file ) {
5432         /**
5433          * Filters the path of the file to delete.
5434          *
5435          * @since 2.1.0
5436          *
5437          * @param string $medium Path to the file to delete.
5438          */
5439         $delete = apply_filters( 'wp_delete_file', $file );
5440         if ( ! empty( $delete ) ) {
5441                 @unlink( $delete );
5442         }
5443 }
5444
5445 /**
5446  * Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload.
5447  *
5448  * This prevents reusing the same tab for a preview when the user has navigated away.
5449  *
5450  * @since 4.3.0
5451  */
5452 function wp_post_preview_js() {
5453         global $post;
5454
5455         if ( ! is_preview() || empty( $post ) ) {
5456                 return;
5457         }
5458
5459         // Has to match the window name used in post_submit_meta_box()
5460         $name = 'wp-preview-' . (int) $post->ID;
5461
5462         ?>
5463         <script>
5464         ( function() {
5465                 var query = document.location.search;
5466
5467                 if ( query && query.indexOf( 'preview=true' ) !== -1 ) {
5468                         window.name = '<?php echo $name; ?>';
5469                 }
5470
5471                 if ( window.addEventListener ) {
5472                         window.addEventListener( 'unload', function() { window.name = ''; }, false );
5473                 }
5474         }());
5475         </script>
5476         <?php
5477 }
5478
5479 /**
5480  * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339.
5481  *
5482  * Explicitly strips timezones, as datetimes are not saved with any timezone
5483  * information. Including any information on the offset could be misleading.
5484  *
5485  * @since 4.4.0
5486  *
5487  * @param string $date_string Date string to parse and format.
5488  * @return string Date formatted for ISO8601/RFC3339.
5489  */
5490 function mysql_to_rfc3339( $date_string ) {
5491         $formatted = mysql2date( 'c', $date_string, false );
5492
5493         // Strip timezone information
5494         return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
5495 }
5496
5497 /**
5498  * Attempts to raise the PHP memory limit for memory intensive processes.
5499  *
5500  * Only allows raising the existing limit and prevents lowering it.
5501  *
5502  * @since 4.6.0
5503  *
5504  * @param string $context Optional. Context in which the function is called. Accepts either 'admin',
5505  *                        'image', or an arbitrary other context. If an arbitrary context is passed,
5506  *                        the similarly arbitrary {@see '{$context}_memory_limit'} filter will be
5507  *                        invoked. Default 'admin'.
5508  * @return bool|int|string The limit that was set or false on failure.
5509  */
5510 function wp_raise_memory_limit( $context = 'admin' ) {
5511         // Exit early if the limit cannot be changed.
5512         if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
5513                 return false;
5514         }
5515
5516         $current_limit     = @ini_get( 'memory_limit' );
5517         $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
5518
5519         if ( -1 === $current_limit_int ) {
5520                 return false;
5521         }
5522
5523         $wp_max_limit     = WP_MAX_MEMORY_LIMIT;
5524         $wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
5525         $filtered_limit   = $wp_max_limit;
5526
5527         switch ( $context ) {
5528                 case 'admin':
5529                         /**
5530                          * Filters the maximum memory limit available for administration screens.
5531                          *
5532                          * This only applies to administrators, who may require more memory for tasks
5533                          * like updates. Memory limits when processing images (uploaded or edited by
5534                          * users of any role) are handled separately.
5535                          *
5536                          * The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory
5537                          * limit available when in the administration back end. The default is 256M
5538                          * (256 megabytes of memory) or the original `memory_limit` php.ini value if
5539                          * this is higher.
5540                          *
5541                          * @since 3.0.0
5542                          * @since 4.6.0 The default now takes the original `memory_limit` into account.
5543                          *
5544                          * @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer
5545                          *                                   (bytes), or a shorthand string notation, such as '256M'.
5546                          */
5547                         $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
5548                         break;
5549
5550                 case 'image':
5551                         /**
5552                          * Filters the memory limit allocated for image manipulation.
5553                          *
5554                          * @since 3.5.0
5555                          * @since 4.6.0 The default now takes the original `memory_limit` into account.
5556                          *
5557                          * @param int|string $filtered_limit Maximum memory limit to allocate for images.
5558                          *                                   Default `WP_MAX_MEMORY_LIMIT` or the original
5559                          *                                   php.ini `memory_limit`, whichever is higher.
5560                          *                                   Accepts an integer (bytes), or a shorthand string
5561                          *                                   notation, such as '256M'.
5562                          */
5563                         $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
5564                         break;
5565
5566                 default:
5567                         /**
5568                          * Filters the memory limit allocated for arbitrary contexts.
5569                          *
5570                          * The dynamic portion of the hook name, `$context`, refers to an arbitrary
5571                          * context passed on calling the function. This allows for plugins to define
5572                          * their own contexts for raising the memory limit.
5573                          *
5574                          * @since 4.6.0
5575                          *
5576                          * @param int|string $filtered_limit Maximum memory limit to allocate for images.
5577                          *                                   Default '256M' or the original php.ini `memory_limit`,
5578                          *                                   whichever is higher. Accepts an integer (bytes), or a
5579                          *                                   shorthand string notation, such as '256M'.
5580                          */
5581                         $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
5582                         break;
5583         }
5584
5585         $filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
5586
5587         if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
5588                 if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
5589                         return $filtered_limit;
5590                 } else {
5591                         return false;
5592                 }
5593         } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
5594                 if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
5595                         return $wp_max_limit;
5596                 } else {
5597                         return false;
5598                 }
5599         }
5600
5601         return false;
5602 }
5603
5604 /**
5605  * Generate a random UUID (version 4).
5606  *
5607  * @since 4.7.0
5608  *
5609  * @return string UUID.
5610  */
5611 function wp_generate_uuid4() {
5612         return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
5613                 mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
5614                 mt_rand( 0, 0xffff ),
5615                 mt_rand( 0, 0x0fff ) | 0x4000,
5616                 mt_rand( 0, 0x3fff ) | 0x8000,
5617                 mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
5618         );
5619 }
5620
5621 /**
5622  * Get last changed date for the specified cache group.
5623  *
5624  * @since 4.7.0
5625  *
5626  * @param $group Where the cache contents are grouped.
5627  *
5628  * @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed.
5629  */
5630 function wp_cache_get_last_changed( $group ) {
5631         $last_changed = wp_cache_get( 'last_changed', $group );
5632
5633         if ( ! $last_changed ) {
5634                 $last_changed = microtime();
5635                 wp_cache_set( 'last_changed', $last_changed, $group );
5636         }
5637
5638         return $last_changed;
5639 }