[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> functions.php (source)

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