home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / functions.php < prev    next >
Encoding:
PHP Script  |  2018-01-23  |  175.8 KB  |  5,819 lines

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