[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * These functions are needed to load WordPress.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Return the HTTP protocol sent by the server.
  10   *
  11   * @since 4.4.0
  12   *
  13   * @return string The HTTP protocol. Default: HTTP/1.0.
  14   */
  15  function wp_get_server_protocol() {
  16      $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : '';
  17      if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ), true ) ) {
  18          $protocol = 'HTTP/1.0';
  19      }
  20      return $protocol;
  21  }
  22  
  23  /**
  24   * Fix `$_SERVER` variables for various setups.
  25   *
  26   * @since 3.0.0
  27   * @access private
  28   *
  29   * @global string $PHP_SELF The filename of the currently executing script,
  30   *                          relative to the document root.
  31   */
  32  function wp_fix_server_vars() {
  33      global $PHP_SELF;
  34  
  35      $default_server_values = array(
  36          'SERVER_SOFTWARE' => '',
  37          'REQUEST_URI'     => '',
  38      );
  39  
  40      $_SERVER = array_merge( $default_server_values, $_SERVER );
  41  
  42      // Fix for IIS when running with PHP ISAPI.
  43      if ( empty( $_SERVER['REQUEST_URI'] ) || ( 'cgi-fcgi' !== PHP_SAPI && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) ) {
  44  
  45          if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
  46              // IIS Mod-Rewrite.
  47              $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
  48          } elseif ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) {
  49              // IIS Isapi_Rewrite.
  50              $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
  51          } else {
  52              // Use ORIG_PATH_INFO if there is no PATH_INFO.
  53              if ( ! isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) ) {
  54                  $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
  55              }
  56  
  57              // Some IIS + PHP configurations put the script-name in the path-info (no need to append it twice).
  58              if ( isset( $_SERVER['PATH_INFO'] ) ) {
  59                  if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] ) {
  60                      $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
  61                  } else {
  62                      $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
  63                  }
  64              }
  65  
  66              // Append the query string if it exists and isn't null.
  67              if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
  68                  $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
  69              }
  70          }
  71      }
  72  
  73      // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests.
  74      if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) == strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 ) ) {
  75          $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
  76      }
  77  
  78      // Fix for Dreamhost and other PHP as CGI hosts.
  79      if ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false ) {
  80          unset( $_SERVER['PATH_INFO'] );
  81      }
  82  
  83      // Fix empty PHP_SELF.
  84      $PHP_SELF = $_SERVER['PHP_SELF'];
  85      if ( empty( $PHP_SELF ) ) {
  86          $_SERVER['PHP_SELF'] = preg_replace( '/(\?.*)?$/', '', $_SERVER['REQUEST_URI'] );
  87          $PHP_SELF            = $_SERVER['PHP_SELF'];
  88      }
  89  }
  90  
  91  /**
  92   * Check for the required PHP version, and the MySQL extension or
  93   * a database drop-in.
  94   *
  95   * Dies if requirements are not met.
  96   *
  97   * @since 3.0.0
  98   * @access private
  99   *
 100   * @global string $required_php_version The required PHP version string.
 101   * @global string $wp_version           The WordPress version string.
 102   */
 103  function wp_check_php_mysql_versions() {
 104      global $required_php_version, $wp_version;
 105      $php_version = phpversion();
 106  
 107      if ( version_compare( $required_php_version, $php_version, '>' ) ) {
 108          $protocol = wp_get_server_protocol();
 109          header( sprintf( '%s 500 Internal Server Error', $protocol ), true, 500 );
 110          header( 'Content-Type: text/html; charset=utf-8' );
 111          printf( 'Your server is running PHP version %1$s but WordPress %2$s requires at least %3$s.', $php_version, $wp_version, $required_php_version );
 112          exit( 1 );
 113      }
 114  
 115      if ( ! extension_loaded( 'mysql' ) && ! extension_loaded( 'mysqli' ) && ! extension_loaded( 'mysqlnd' ) && ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) {
 116          require_once  ABSPATH . WPINC . '/functions.php';
 117          wp_load_translations_early();
 118          $args = array(
 119              'exit' => false,
 120              'code' => 'mysql_not_found',
 121          );
 122          wp_die(
 123              __( 'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.' ),
 124              __( 'Requirements Not Met' ),
 125              $args
 126          );
 127          exit( 1 );
 128      }
 129  }
 130  
 131  /**
 132   * Retrieves the current environment type.
 133   *
 134   * The type can be set via the `WP_ENVIRONMENT_TYPE` global system variable,
 135   * or a constant of the same name.
 136   *
 137   * Possible values are 'local', 'development', 'staging', and 'production'.
 138   * If not set, the type defaults to 'production'.
 139   *
 140   * @since 5.5.0
 141   * @since 5.5.1 Added the 'local' type.
 142   * @since 5.5.1 Removed the ability to alter the list of types.
 143   *
 144   * @return string The current environment type.
 145   */
 146  function wp_get_environment_type() {
 147      static $current_env = '';
 148  
 149      if ( $current_env ) {
 150          return $current_env;
 151      }
 152  
 153      $wp_environments = array(
 154          'local',
 155          'development',
 156          'staging',
 157          'production',
 158      );
 159  
 160      // Add a note about the deprecated WP_ENVIRONMENT_TYPES constant.
 161      if ( defined( 'WP_ENVIRONMENT_TYPES' ) && function_exists( '_deprecated_argument' ) ) {
 162          if ( function_exists( '__' ) ) {
 163              /* translators: %s: WP_ENVIRONMENT_TYPES */
 164              $message = sprintf( __( 'The %s constant is no longer supported.' ), 'WP_ENVIRONMENT_TYPES' );
 165          } else {
 166              $message = sprintf( 'The %s constant is no longer supported.', 'WP_ENVIRONMENT_TYPES' );
 167          }
 168  
 169          _deprecated_argument(
 170              'define()',
 171              '5.5.1',
 172              $message
 173          );
 174      }
 175  
 176      // Check if the environment variable has been set, if `getenv` is available on the system.
 177      if ( function_exists( 'getenv' ) ) {
 178          $has_env = getenv( 'WP_ENVIRONMENT_TYPE' );
 179          if ( false !== $has_env ) {
 180              $current_env = $has_env;
 181          }
 182      }
 183  
 184      // Fetch the environment from a constant, this overrides the global system variable.
 185      if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) {
 186          $current_env = WP_ENVIRONMENT_TYPE;
 187      }
 188  
 189      // Make sure the environment is an allowed one, and not accidentally set to an invalid value.
 190      if ( ! in_array( $current_env, $wp_environments, true ) ) {
 191          $current_env = 'production';
 192      }
 193  
 194      return $current_env;
 195  }
 196  
 197  /**
 198   * Don't load all of WordPress when handling a favicon.ico request.
 199   *
 200   * Instead, send the headers for a zero-length favicon and bail.
 201   *
 202   * @since 3.0.0
 203   * @deprecated 5.4.0 Deprecated in favor of do_favicon().
 204   */
 205  function wp_favicon_request() {
 206      if ( '/favicon.ico' === $_SERVER['REQUEST_URI'] ) {
 207          header( 'Content-Type: image/vnd.microsoft.icon' );
 208          exit;
 209      }
 210  }
 211  
 212  /**
 213   * Die with a maintenance message when conditions are met.
 214   *
 215   * The default message can be replaced by using a drop-in (maintenance.php in
 216   * the wp-content directory).
 217   *
 218   * @since 3.0.0
 219   * @access private
 220   */
 221  function wp_maintenance() {
 222      // Return if maintenance mode is disabled.
 223      if ( ! wp_is_maintenance_mode() ) {
 224          return;
 225      }
 226  
 227      if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
 228          require_once WP_CONTENT_DIR . '/maintenance.php';
 229          die();
 230      }
 231  
 232      require_once  ABSPATH . WPINC . '/functions.php';
 233      wp_load_translations_early();
 234  
 235      header( 'Retry-After: 600' );
 236  
 237      wp_die(
 238          __( 'Briefly unavailable for scheduled maintenance. Check back in a minute.' ),
 239          __( 'Maintenance' ),
 240          503
 241      );
 242  }
 243  
 244  /**
 245   * Check if maintenance mode is enabled.
 246   *
 247   * Checks for a file in the WordPress root directory named ".maintenance".
 248   * This file will contain the variable $upgrading, set to the time the file
 249   * was created. If the file was created less than 10 minutes ago, WordPress
 250   * is in maintenance mode.
 251   *
 252   * @since 5.5.0
 253   *
 254   * @global int $upgrading The Unix timestamp marking when upgrading WordPress began.
 255   *
 256   * @return bool True if maintenance mode is enabled, false otherwise.
 257   */
 258  function wp_is_maintenance_mode() {
 259      global $upgrading;
 260  
 261      if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() ) {
 262          return false;
 263      }
 264  
 265      require ABSPATH . '.maintenance';
 266      // If the $upgrading timestamp is older than 10 minutes, consider maintenance over.
 267      if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) {
 268          return false;
 269      }
 270  
 271      /**
 272       * Filters whether to enable maintenance mode.
 273       *
 274       * This filter runs before it can be used by plugins. It is designed for
 275       * non-web runtimes. If this filter returns true, maintenance mode will be
 276       * active and the request will end. If false, the request will be allowed to
 277       * continue processing even if maintenance mode should be active.
 278       *
 279       * @since 4.6.0
 280       *
 281       * @param bool $enable_checks Whether to enable maintenance mode. Default true.
 282       * @param int  $upgrading     The timestamp set in the .maintenance file.
 283       */
 284      if ( ! apply_filters( 'enable_maintenance_mode', true, $upgrading ) ) {
 285          return false;
 286      }
 287  
 288      return true;
 289  }
 290  
 291  /**
 292   * Start the WordPress micro-timer.
 293   *
 294   * @since 0.71
 295   * @access private
 296   *
 297   * @global float $timestart Unix timestamp set at the beginning of the page load.
 298   * @see timer_stop()
 299   *
 300   * @return bool Always returns true.
 301   */
 302  function timer_start() {
 303      global $timestart;
 304      $timestart = microtime( true );
 305      return true;
 306  }
 307  
 308  /**
 309   * Retrieve or display the time from the page start to when function is called.
 310   *
 311   * @since 0.71
 312   *
 313   * @global float   $timestart Seconds from when timer_start() is called.
 314   * @global float   $timeend   Seconds from when function is called.
 315   *
 316   * @param int|bool $display   Whether to echo or return the results. Accepts 0|false for return,
 317   *                            1|true for echo. Default 0|false.
 318   * @param int      $precision The number of digits from the right of the decimal to display.
 319   *                            Default 3.
 320   * @return string The "second.microsecond" finished time calculation. The number is formatted
 321   *                for human consumption, both localized and rounded.
 322   */
 323  function timer_stop( $display = 0, $precision = 3 ) {
 324      global $timestart, $timeend;
 325      $timeend   = microtime( true );
 326      $timetotal = $timeend - $timestart;
 327      $r         = ( function_exists( 'number_format_i18n' ) ) ? number_format_i18n( $timetotal, $precision ) : number_format( $timetotal, $precision );
 328      if ( $display ) {
 329          echo $r;
 330      }
 331      return $r;
 332  }
 333  
 334  /**
 335   * Set PHP error reporting based on WordPress debug settings.
 336   *
 337   * Uses three constants: `WP_DEBUG`, `WP_DEBUG_DISPLAY`, and `WP_DEBUG_LOG`.
 338   * All three can be defined in wp-config.php. By default, `WP_DEBUG` and
 339   * `WP_DEBUG_LOG` are set to false, and `WP_DEBUG_DISPLAY` is set to true.
 340   *
 341   * When `WP_DEBUG` is true, all PHP notices are reported. WordPress will also
 342   * display internal notices: when a deprecated WordPress function, function
 343   * argument, or file is used. Deprecated code may be removed from a later
 344   * version.
 345   *
 346   * It is strongly recommended that plugin and theme developers use `WP_DEBUG`
 347   * in their development environments.
 348   *
 349   * `WP_DEBUG_DISPLAY` and `WP_DEBUG_LOG` perform no function unless `WP_DEBUG`
 350   * is true.
 351   *
 352   * When `WP_DEBUG_DISPLAY` is true, WordPress will force errors to be displayed.
 353   * `WP_DEBUG_DISPLAY` defaults to true. Defining it as null prevents WordPress
 354   * from changing the global configuration setting. Defining `WP_DEBUG_DISPLAY`
 355   * as false will force errors to be hidden.
 356   *
 357   * When `WP_DEBUG_LOG` is true, errors will be logged to `wp-content/debug.log`.
 358   * When `WP_DEBUG_LOG` is a valid path, errors will be logged to the specified file.
 359   *
 360   * Errors are never displayed for XML-RPC, REST, and Ajax requests.
 361   *
 362   * @since 3.0.0
 363   * @since 5.1.0 `WP_DEBUG_LOG` can be a file path.
 364   * @access private
 365   */
 366  function wp_debug_mode() {
 367      /**
 368       * Filters whether to allow the debug mode check to occur.
 369       *
 370       * This filter runs before it can be used by plugins. It is designed for
 371       * non-web run-times. Returning false causes the `WP_DEBUG` and related
 372       * constants to not be checked and the default PHP values for errors
 373       * will be used unless you take care to update them yourself.
 374       *
 375       * @since 4.6.0
 376       *
 377       * @param bool $enable_debug_mode Whether to enable debug mode checks to occur. Default true.
 378       */
 379      if ( ! apply_filters( 'enable_wp_debug_mode_checks', true ) ) {
 380          return;
 381      }
 382  
 383      if ( WP_DEBUG ) {
 384          error_reporting( E_ALL );
 385  
 386          if ( WP_DEBUG_DISPLAY ) {
 387              ini_set( 'display_errors', 1 );
 388          } elseif ( null !== WP_DEBUG_DISPLAY ) {
 389              ini_set( 'display_errors', 0 );
 390          }
 391  
 392          if ( in_array( strtolower( (string) WP_DEBUG_LOG ), array( 'true', '1' ), true ) ) {
 393              $log_path = WP_CONTENT_DIR . '/debug.log';
 394          } elseif ( is_string( WP_DEBUG_LOG ) ) {
 395              $log_path = WP_DEBUG_LOG;
 396          } else {
 397              $log_path = false;
 398          }
 399  
 400          if ( $log_path ) {
 401              ini_set( 'log_errors', 1 );
 402              ini_set( 'error_log', $log_path );
 403          }
 404      } else {
 405          error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
 406      }
 407  
 408      if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() || wp_is_json_request() ) {
 409          ini_set( 'display_errors', 0 );
 410      }
 411  }
 412  
 413  /**
 414   * Set the location of the language directory.
 415   *
 416   * To set directory manually, define the `WP_LANG_DIR` constant
 417   * in wp-config.php.
 418   *
 419   * If the language directory exists within `WP_CONTENT_DIR`, it
 420   * is used. Otherwise the language directory is assumed to live
 421   * in `WPINC`.
 422   *
 423   * @since 3.0.0
 424   * @access private
 425   */
 426  function wp_set_lang_dir() {
 427      if ( ! defined( 'WP_LANG_DIR' ) ) {
 428          if ( file_exists( WP_CONTENT_DIR . '/languages' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) || ! @is_dir( ABSPATH . WPINC . '/languages' ) ) {
 429              /**
 430               * Server path of the language directory.
 431               *
 432               * No leading slash, no trailing slash, full path, not relative to ABSPATH
 433               *
 434               * @since 2.1.0
 435               */
 436              define( 'WP_LANG_DIR', WP_CONTENT_DIR . '/languages' );
 437              if ( ! defined( 'LANGDIR' ) ) {
 438                  // Old static relative path maintained for limited backward compatibility - won't work in some cases.
 439                  define( 'LANGDIR', 'wp-content/languages' );
 440              }
 441          } else {
 442              /**
 443               * Server path of the language directory.
 444               *
 445               * No leading slash, no trailing slash, full path, not relative to `ABSPATH`.
 446               *
 447               * @since 2.1.0
 448               */
 449              define( 'WP_LANG_DIR', ABSPATH . WPINC . '/languages' );
 450              if ( ! defined( 'LANGDIR' ) ) {
 451                  // Old relative path maintained for backward compatibility.
 452                  define( 'LANGDIR', WPINC . '/languages' );
 453              }
 454          }
 455      }
 456  }
 457  
 458  /**
 459   * Load the database class file and instantiate the `$wpdb` global.
 460   *
 461   * @since 2.5.0
 462   *
 463   * @global wpdb $wpdb WordPress database abstraction object.
 464   */
 465  function require_wp_db() {
 466      global $wpdb;
 467  
 468      require_once  ABSPATH . WPINC . '/wp-db.php';
 469      if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ) {
 470          require_once WP_CONTENT_DIR . '/db.php';
 471      }
 472  
 473      if ( isset( $wpdb ) ) {
 474          return;
 475      }
 476  
 477      $dbuser     = defined( 'DB_USER' ) ? DB_USER : '';
 478      $dbpassword = defined( 'DB_PASSWORD' ) ? DB_PASSWORD : '';
 479      $dbname     = defined( 'DB_NAME' ) ? DB_NAME : '';
 480      $dbhost     = defined( 'DB_HOST' ) ? DB_HOST : '';
 481  
 482      $wpdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
 483  }
 484  
 485  /**
 486   * Set the database table prefix and the format specifiers for database
 487   * table columns.
 488   *
 489   * Columns not listed here default to `%s`.
 490   *
 491   * @since 3.0.0
 492   * @access private
 493   *
 494   * @global wpdb   $wpdb         WordPress database abstraction object.
 495   * @global string $table_prefix The database table prefix.
 496   */
 497  function wp_set_wpdb_vars() {
 498      global $wpdb, $table_prefix;
 499      if ( ! empty( $wpdb->error ) ) {
 500          dead_db();
 501      }
 502  
 503      $wpdb->field_types = array(
 504          'post_author'      => '%d',
 505          'post_parent'      => '%d',
 506          'menu_order'       => '%d',
 507          'term_id'          => '%d',
 508          'term_group'       => '%d',
 509          'term_taxonomy_id' => '%d',
 510          'parent'           => '%d',
 511          'count'            => '%d',
 512          'object_id'        => '%d',
 513          'term_order'       => '%d',
 514          'ID'               => '%d',
 515          'comment_ID'       => '%d',
 516          'comment_post_ID'  => '%d',
 517          'comment_parent'   => '%d',
 518          'user_id'          => '%d',
 519          'link_id'          => '%d',
 520          'link_owner'       => '%d',
 521          'link_rating'      => '%d',
 522          'option_id'        => '%d',
 523          'blog_id'          => '%d',
 524          'meta_id'          => '%d',
 525          'post_id'          => '%d',
 526          'user_status'      => '%d',
 527          'umeta_id'         => '%d',
 528          'comment_karma'    => '%d',
 529          'comment_count'    => '%d',
 530          // Multisite:
 531          'active'           => '%d',
 532          'cat_id'           => '%d',
 533          'deleted'          => '%d',
 534          'lang_id'          => '%d',
 535          'mature'           => '%d',
 536          'public'           => '%d',
 537          'site_id'          => '%d',
 538          'spam'             => '%d',
 539      );
 540  
 541      $prefix = $wpdb->set_prefix( $table_prefix );
 542  
 543      if ( is_wp_error( $prefix ) ) {
 544          wp_load_translations_early();
 545          wp_die(
 546              sprintf(
 547                  /* translators: 1: $table_prefix, 2: wp-config.php */
 548                  __( '<strong>Error</strong>: %1$s in %2$s can only contain numbers, letters, and underscores.' ),
 549                  '<code>$table_prefix</code>',
 550                  '<code>wp-config.php</code>'
 551              )
 552          );
 553      }
 554  }
 555  
 556  /**
 557   * Toggle `$_wp_using_ext_object_cache` on and off without directly
 558   * touching global.
 559   *
 560   * @since 3.7.0
 561   *
 562   * @global bool $_wp_using_ext_object_cache
 563   *
 564   * @param bool $using Whether external object cache is being used.
 565   * @return bool The current 'using' setting.
 566   */
 567  function wp_using_ext_object_cache( $using = null ) {
 568      global $_wp_using_ext_object_cache;
 569      $current_using = $_wp_using_ext_object_cache;
 570      if ( null !== $using ) {
 571          $_wp_using_ext_object_cache = $using;
 572      }
 573      return $current_using;
 574  }
 575  
 576  /**
 577   * Start the WordPress object cache.
 578   *
 579   * If an object-cache.php file exists in the wp-content directory,
 580   * it uses that drop-in as an external object cache.
 581   *
 582   * @since 3.0.0
 583   * @access private
 584   *
 585   * @global array $wp_filter Stores all of the filters.
 586   */
 587  function wp_start_object_cache() {
 588      global $wp_filter;
 589      static $first_init = true;
 590  
 591      // Only perform the following checks once.
 592      if ( $first_init ) {
 593          if ( ! function_exists( 'wp_cache_init' ) ) {
 594              /*
 595               * This is the normal situation. First-run of this function. No
 596               * caching backend has been loaded.
 597               *
 598               * We try to load a custom caching backend, and then, if it
 599               * results in a wp_cache_init() function existing, we note
 600               * that an external object cache is being used.
 601               */
 602              if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
 603                  require_once WP_CONTENT_DIR . '/object-cache.php';
 604                  if ( function_exists( 'wp_cache_init' ) ) {
 605                      wp_using_ext_object_cache( true );
 606                  }
 607  
 608                  // Re-initialize any hooks added manually by object-cache.php.
 609                  if ( $wp_filter ) {
 610                      $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
 611                  }
 612              }
 613          } elseif ( ! wp_using_ext_object_cache() && file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
 614              /*
 615               * Sometimes advanced-cache.php can load object-cache.php before
 616               * this function is run. This breaks the function_exists() check
 617               * above and can result in wp_using_ext_object_cache() returning
 618               * false when actually an external cache is in use.
 619               */
 620              wp_using_ext_object_cache( true );
 621          }
 622      }
 623  
 624      if ( ! wp_using_ext_object_cache() ) {
 625          require_once  ABSPATH . WPINC . '/cache.php';
 626      }
 627  
 628      require_once  ABSPATH . WPINC . '/cache-compat.php';
 629  
 630      /*
 631       * If cache supports reset, reset instead of init if already
 632       * initialized. Reset signals to the cache that global IDs
 633       * have changed and it may need to update keys and cleanup caches.
 634       */
 635      if ( ! $first_init && function_exists( 'wp_cache_switch_to_blog' ) ) {
 636          wp_cache_switch_to_blog( get_current_blog_id() );
 637      } elseif ( function_exists( 'wp_cache_init' ) ) {
 638          wp_cache_init();
 639      }
 640  
 641      if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 642          wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'blog_meta' ) );
 643          wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 644      }
 645  
 646      $first_init = false;
 647  }
 648  
 649  /**
 650   * Redirect to the installer if WordPress is not installed.
 651   *
 652   * Dies with an error message when Multisite is enabled.
 653   *
 654   * @since 3.0.0
 655   * @access private
 656   */
 657  function wp_not_installed() {
 658      if ( is_multisite() ) {
 659          if ( ! is_blog_installed() && ! wp_installing() ) {
 660              nocache_headers();
 661  
 662              wp_die( __( 'The site you have requested is not installed properly. Please contact the system administrator.' ) );
 663          }
 664      } elseif ( ! is_blog_installed() && ! wp_installing() ) {
 665          nocache_headers();
 666  
 667          require  ABSPATH . WPINC . '/kses.php';
 668          require  ABSPATH . WPINC . '/pluggable.php';
 669  
 670          $link = wp_guess_url() . '/wp-admin/install.php';
 671  
 672          wp_redirect( $link );
 673          die();
 674      }
 675  }
 676  
 677  /**
 678   * Retrieve an array of must-use plugin files.
 679   *
 680   * The default directory is wp-content/mu-plugins. To change the default
 681   * directory manually, define `WPMU_PLUGIN_DIR` and `WPMU_PLUGIN_URL`
 682   * in wp-config.php.
 683   *
 684   * @since 3.0.0
 685   * @access private
 686   *
 687   * @return string[] Array of absolute paths of files to include.
 688   */
 689  function wp_get_mu_plugins() {
 690      $mu_plugins = array();
 691      if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
 692          return $mu_plugins;
 693      }
 694      $dh = opendir( WPMU_PLUGIN_DIR );
 695      if ( ! $dh ) {
 696          return $mu_plugins;
 697      }
 698      while ( ( $plugin = readdir( $dh ) ) !== false ) {
 699          if ( '.php' === substr( $plugin, -4 ) ) {
 700              $mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
 701          }
 702      }
 703      closedir( $dh );
 704      sort( $mu_plugins );
 705  
 706      return $mu_plugins;
 707  }
 708  
 709  /**
 710   * Retrieve an array of active and valid plugin files.
 711   *
 712   * While upgrading or installing WordPress, no plugins are returned.
 713   *
 714   * The default directory is `wp-content/plugins`. To change the default
 715   * directory manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL`
 716   * in `wp-config.php`.
 717   *
 718   * @since 3.0.0
 719   * @access private
 720   *
 721   * @return string[] Array of paths to plugin files relative to the plugins directory.
 722   */
 723  function wp_get_active_and_valid_plugins() {
 724      $plugins        = array();
 725      $active_plugins = (array) get_option( 'active_plugins', array() );
 726  
 727      // Check for hacks file if the option is enabled.
 728      if ( get_option( 'hack_file' ) && file_exists( ABSPATH . 'my-hacks.php' ) ) {
 729          _deprecated_file( 'my-hacks.php', '1.5.0' );
 730          array_unshift( $plugins, ABSPATH . 'my-hacks.php' );
 731      }
 732  
 733      if ( empty( $active_plugins ) || wp_installing() ) {
 734          return $plugins;
 735      }
 736  
 737      $network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;
 738  
 739      foreach ( $active_plugins as $plugin ) {
 740          if ( ! validate_file( $plugin )                     // $plugin must validate as file.
 741              && '.php' === substr( $plugin, -4 )             // $plugin must end with '.php'.
 742              && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
 743              // Not already included as a network plugin.
 744              && ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins, true ) )
 745              ) {
 746              $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
 747          }
 748      }
 749  
 750      /*
 751       * Remove plugins from the list of active plugins when we're on an endpoint
 752       * that should be protected against WSODs and the plugin is paused.
 753       */
 754      if ( wp_is_recovery_mode() ) {
 755          $plugins = wp_skip_paused_plugins( $plugins );
 756      }
 757  
 758      return $plugins;
 759  }
 760  
 761  /**
 762   * Filters a given list of plugins, removing any paused plugins from it.
 763   *
 764   * @since 5.2.0
 765   *
 766   * @param string[] $plugins Array of absolute plugin main file paths.
 767   * @return string[] Filtered array of plugins, without any paused plugins.
 768   */
 769  function wp_skip_paused_plugins( array $plugins ) {
 770      $paused_plugins = wp_paused_plugins()->get_all();
 771  
 772      if ( empty( $paused_plugins ) ) {
 773          return $plugins;
 774      }
 775  
 776      foreach ( $plugins as $index => $plugin ) {
 777          list( $plugin ) = explode( '/', plugin_basename( $plugin ) );
 778  
 779          if ( array_key_exists( $plugin, $paused_plugins ) ) {
 780              unset( $plugins[ $index ] );
 781  
 782              // Store list of paused plugins for displaying an admin notice.
 783              $GLOBALS['_paused_plugins'][ $plugin ] = $paused_plugins[ $plugin ];
 784          }
 785      }
 786  
 787      return $plugins;
 788  }
 789  
 790  /**
 791   * Retrieves an array of active and valid themes.
 792   *
 793   * While upgrading or installing WordPress, no themes are returned.
 794   *
 795   * @since 5.1.0
 796   * @access private
 797   *
 798   * @return string[] Array of absolute paths to theme directories.
 799   */
 800  function wp_get_active_and_valid_themes() {
 801      global $pagenow;
 802  
 803      $themes = array();
 804  
 805      if ( wp_installing() && 'wp-activate.php' !== $pagenow ) {
 806          return $themes;
 807      }
 808  
 809      if ( TEMPLATEPATH !== STYLESHEETPATH ) {
 810          $themes[] = STYLESHEETPATH;
 811      }
 812  
 813      $themes[] = TEMPLATEPATH;
 814  
 815      /*
 816       * Remove themes from the list of active themes when we're on an endpoint
 817       * that should be protected against WSODs and the theme is paused.
 818       */
 819      if ( wp_is_recovery_mode() ) {
 820          $themes = wp_skip_paused_themes( $themes );
 821  
 822          // If no active and valid themes exist, skip loading themes.
 823          if ( empty( $themes ) ) {
 824              add_filter( 'wp_using_themes', '__return_false' );
 825          }
 826      }
 827  
 828      return $themes;
 829  }
 830  
 831  /**
 832   * Filters a given list of themes, removing any paused themes from it.
 833   *
 834   * @since 5.2.0
 835   *
 836   * @param string[] $themes Array of absolute theme directory paths.
 837   * @return string[] Filtered array of absolute paths to themes, without any paused themes.
 838   */
 839  function wp_skip_paused_themes( array $themes ) {
 840      $paused_themes = wp_paused_themes()->get_all();
 841  
 842      if ( empty( $paused_themes ) ) {
 843          return $themes;
 844      }
 845  
 846      foreach ( $themes as $index => $theme ) {
 847          $theme = basename( $theme );
 848  
 849          if ( array_key_exists( $theme, $paused_themes ) ) {
 850              unset( $themes[ $index ] );
 851  
 852              // Store list of paused themes for displaying an admin notice.
 853              $GLOBALS['_paused_themes'][ $theme ] = $paused_themes[ $theme ];
 854          }
 855      }
 856  
 857      return $themes;
 858  }
 859  
 860  /**
 861   * Is WordPress in Recovery Mode.
 862   *
 863   * In this mode, plugins or themes that cause WSODs will be paused.
 864   *
 865   * @since 5.2.0
 866   *
 867   * @return bool
 868   */
 869  function wp_is_recovery_mode() {
 870      return wp_recovery_mode()->is_active();
 871  }
 872  
 873  /**
 874   * Determines whether we are currently on an endpoint that should be protected against WSODs.
 875   *
 876   * @since 5.2.0
 877   *
 878   * @return bool True if the current endpoint should be protected.
 879   */
 880  function is_protected_endpoint() {
 881      // Protect login pages.
 882      if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
 883          return true;
 884      }
 885  
 886      // Protect the admin backend.
 887      if ( is_admin() && ! wp_doing_ajax() ) {
 888          return true;
 889      }
 890  
 891      // Protect Ajax actions that could help resolve a fatal error should be available.
 892      if ( is_protected_ajax_action() ) {
 893          return true;
 894      }
 895  
 896      /**
 897       * Filters whether the current request is against a protected endpoint.
 898       *
 899       * This filter is only fired when an endpoint is requested which is not already protected by
 900       * WordPress core. As such, it exclusively allows providing further protected endpoints in
 901       * addition to the admin backend, login pages and protected Ajax actions.
 902       *
 903       * @since 5.2.0
 904       *
 905       * @param bool $is_protected_endpoint Whether the currently requested endpoint is protected.
 906       *                                    Default false.
 907       */
 908      return (bool) apply_filters( 'is_protected_endpoint', false );
 909  }
 910  
 911  /**
 912   * Determines whether we are currently handling an Ajax action that should be protected against WSODs.
 913   *
 914   * @since 5.2.0
 915   *
 916   * @return bool True if the current Ajax action should be protected.
 917   */
 918  function is_protected_ajax_action() {
 919      if ( ! wp_doing_ajax() ) {
 920          return false;
 921      }
 922  
 923      if ( ! isset( $_REQUEST['action'] ) ) {
 924          return false;
 925      }
 926  
 927      $actions_to_protect = array(
 928          'edit-theme-plugin-file', // Saving changes in the core code editor.
 929          'heartbeat',              // Keep the heart beating.
 930          'install-plugin',         // Installing a new plugin.
 931          'install-theme',          // Installing a new theme.
 932          'search-plugins',         // Searching in the list of plugins.
 933          'search-install-plugins', // Searching for a plugin in the plugin install screen.
 934          'update-plugin',          // Update an existing plugin.
 935          'update-theme',           // Update an existing theme.
 936      );
 937  
 938      /**
 939       * Filters the array of protected Ajax actions.
 940       *
 941       * This filter is only fired when doing Ajax and the Ajax request has an 'action' property.
 942       *
 943       * @since 5.2.0
 944       *
 945       * @param string[] $actions_to_protect Array of strings with Ajax actions to protect.
 946       */
 947      $actions_to_protect = (array) apply_filters( 'wp_protected_ajax_actions', $actions_to_protect );
 948  
 949      if ( ! in_array( $_REQUEST['action'], $actions_to_protect, true ) ) {
 950          return false;
 951      }
 952  
 953      return true;
 954  }
 955  
 956  /**
 957   * Set internal encoding.
 958   *
 959   * In most cases the default internal encoding is latin1, which is
 960   * of no use, since we want to use the `mb_` functions for `utf-8` strings.
 961   *
 962   * @since 3.0.0
 963   * @access private
 964   */
 965  function wp_set_internal_encoding() {
 966      if ( function_exists( 'mb_internal_encoding' ) ) {
 967          $charset = get_option( 'blog_charset' );
 968          // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
 969          if ( ! $charset || ! @mb_internal_encoding( $charset ) ) {
 970              mb_internal_encoding( 'UTF-8' );
 971          }
 972      }
 973  }
 974  
 975  /**
 976   * Add magic quotes to `$_GET`, `$_POST`, `$_COOKIE`, and `$_SERVER`.
 977   *
 978   * Also forces `$_REQUEST` to be `$_GET + $_POST`. If `$_SERVER`,
 979   * `$_COOKIE`, or `$_ENV` are needed, use those superglobals directly.
 980   *
 981   * @since 3.0.0
 982   * @access private
 983   */
 984  function wp_magic_quotes() {
 985      // Escape with wpdb.
 986      $_GET    = add_magic_quotes( $_GET );
 987      $_POST   = add_magic_quotes( $_POST );
 988      $_COOKIE = add_magic_quotes( $_COOKIE );
 989      $_SERVER = add_magic_quotes( $_SERVER );
 990  
 991      // Force REQUEST to be GET + POST.
 992      $_REQUEST = array_merge( $_GET, $_POST );
 993  }
 994  
 995  /**
 996   * Runs just before PHP shuts down execution.
 997   *
 998   * @since 1.2.0
 999   * @access private
1000   */
1001  function shutdown_action_hook() {
1002      /**
1003       * Fires just before PHP shuts down execution.
1004       *
1005       * @since 1.2.0
1006       */
1007      do_action( 'shutdown' );
1008  
1009      wp_cache_close();
1010  }
1011  
1012  /**
1013   * Copy an object.
1014   *
1015   * @since 2.7.0
1016   * @deprecated 3.2.0
1017   *
1018   * @param object $object The object to clone.
1019   * @return object The cloned object.
1020   */
1021  function wp_clone( $object ) {
1022      // Use parens for clone to accommodate PHP 4. See #17880.
1023      return clone( $object );
1024  }
1025  
1026  /**
1027   * Determines whether the current request is for an administrative interface page.
1028   *
1029   * Does not check if the user is an administrator; use current_user_can()
1030   * for checking roles and capabilities.
1031   *
1032   * For more information on this and similar theme functions, check out
1033   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1034   * Conditional Tags} article in the Theme Developer Handbook.
1035   *
1036   * @since 1.5.1
1037   *
1038   * @global WP_Screen $current_screen WordPress current screen object.
1039   *
1040   * @return bool True if inside WordPress administration interface, false otherwise.
1041   */
1042  function is_admin() {
1043      if ( isset( $GLOBALS['current_screen'] ) ) {
1044          return $GLOBALS['current_screen']->in_admin();
1045      } elseif ( defined( 'WP_ADMIN' ) ) {
1046          return WP_ADMIN;
1047      }
1048  
1049      return false;
1050  }
1051  
1052  /**
1053   * Whether the current request is for a site's administrative interface.
1054   *
1055   * e.g. `/wp-admin/`
1056   *
1057   * Does not check if the user is an administrator; use current_user_can()
1058   * for checking roles and capabilities.
1059   *
1060   * @since 3.1.0
1061   *
1062   * @global WP_Screen $current_screen WordPress current screen object.
1063   *
1064   * @return bool True if inside WordPress blog administration pages.
1065   */
1066  function is_blog_admin() {
1067      if ( isset( $GLOBALS['current_screen'] ) ) {
1068          return $GLOBALS['current_screen']->in_admin( 'site' );
1069      } elseif ( defined( 'WP_BLOG_ADMIN' ) ) {
1070          return WP_BLOG_ADMIN;
1071      }
1072  
1073      return false;
1074  }
1075  
1076  /**
1077   * Whether the current request is for the network administrative interface.
1078   *
1079   * e.g. `/wp-admin/network/`
1080   *
1081   * Does not check if the user is an administrator; use current_user_can()
1082   * for checking roles and capabilities.
1083   *
1084   * Does not check if the site is a Multisite network; use is_multisite()
1085   * for checking if Multisite is enabled.
1086   *
1087   * @since 3.1.0
1088   *
1089   * @global WP_Screen $current_screen WordPress current screen object.
1090   *
1091   * @return bool True if inside WordPress network administration pages.
1092   */
1093  function is_network_admin() {
1094      if ( isset( $GLOBALS['current_screen'] ) ) {
1095          return $GLOBALS['current_screen']->in_admin( 'network' );
1096      } elseif ( defined( 'WP_NETWORK_ADMIN' ) ) {
1097          return WP_NETWORK_ADMIN;
1098      }
1099  
1100      return false;
1101  }
1102  
1103  /**
1104   * Whether the current request is for a user admin screen.
1105   *
1106   * e.g. `/wp-admin/user/`
1107   *
1108   * Does not check if the user is an administrator; use current_user_can()
1109   * for checking roles and capabilities.
1110   *
1111   * @since 3.1.0
1112   *
1113   * @global WP_Screen $current_screen WordPress current screen object.
1114   *
1115   * @return bool True if inside WordPress user administration pages.
1116   */
1117  function is_user_admin() {
1118      if ( isset( $GLOBALS['current_screen'] ) ) {
1119          return $GLOBALS['current_screen']->in_admin( 'user' );
1120      } elseif ( defined( 'WP_USER_ADMIN' ) ) {
1121          return WP_USER_ADMIN;
1122      }
1123  
1124      return false;
1125  }
1126  
1127  /**
1128   * If Multisite is enabled.
1129   *
1130   * @since 3.0.0
1131   *
1132   * @return bool True if Multisite is enabled, false otherwise.
1133   */
1134  function is_multisite() {
1135      if ( defined( 'MULTISITE' ) ) {
1136          return MULTISITE;
1137      }
1138  
1139      if ( defined( 'SUBDOMAIN_INSTALL' ) || defined( 'VHOST' ) || defined( 'SUNRISE' ) ) {
1140          return true;
1141      }
1142  
1143      return false;
1144  }
1145  
1146  /**
1147   * Retrieve the current site ID.
1148   *
1149   * @since 3.1.0
1150   *
1151   * @global int $blog_id
1152   *
1153   * @return int Site ID.
1154   */
1155  function get_current_blog_id() {
1156      global $blog_id;
1157      return absint( $blog_id );
1158  }
1159  
1160  /**
1161   * Retrieves the current network ID.
1162   *
1163   * @since 4.6.0
1164   *
1165   * @return int The ID of the current network.
1166   */
1167  function get_current_network_id() {
1168      if ( ! is_multisite() ) {
1169          return 1;
1170      }
1171  
1172      $current_network = get_network();
1173  
1174      if ( ! isset( $current_network->id ) ) {
1175          return get_main_network_id();
1176      }
1177  
1178      return absint( $current_network->id );
1179  }
1180  
1181  /**
1182   * Attempt an early load of translations.
1183   *
1184   * Used for errors encountered during the initial loading process, before
1185   * the locale has been properly detected and loaded.
1186   *
1187   * Designed for unusual load sequences (like setup-config.php) or for when
1188   * the script will then terminate with an error, otherwise there is a risk
1189   * that a file can be double-included.
1190   *
1191   * @since 3.4.0
1192   * @access private
1193   *
1194   * @global WP_Locale $wp_locale WordPress date and time locale object.
1195   */
1196  function wp_load_translations_early() {
1197      global $wp_locale;
1198  
1199      static $loaded = false;
1200      if ( $loaded ) {
1201          return;
1202      }
1203      $loaded = true;
1204  
1205      if ( function_exists( 'did_action' ) && did_action( 'init' ) ) {
1206          return;
1207      }
1208  
1209      // We need $wp_local_package.
1210      require  ABSPATH . WPINC . '/version.php';
1211  
1212      // Translation and localization.
1213      require_once  ABSPATH . WPINC . '/pomo/mo.php';
1214      require_once  ABSPATH . WPINC . '/l10n.php';
1215      require_once  ABSPATH . WPINC . '/class-wp-locale.php';
1216      require_once  ABSPATH . WPINC . '/class-wp-locale-switcher.php';
1217  
1218      // General libraries.
1219      require_once  ABSPATH . WPINC . '/plugin.php';
1220  
1221      $locales   = array();
1222      $locations = array();
1223  
1224      while ( true ) {
1225          if ( defined( 'WPLANG' ) ) {
1226              if ( '' === WPLANG ) {
1227                  break;
1228              }
1229              $locales[] = WPLANG;
1230          }
1231  
1232          if ( isset( $wp_local_package ) ) {
1233              $locales[] = $wp_local_package;
1234          }
1235  
1236          if ( ! $locales ) {
1237              break;
1238          }
1239  
1240          if ( defined( 'WP_LANG_DIR' ) && @is_dir( WP_LANG_DIR ) ) {
1241              $locations[] = WP_LANG_DIR;
1242          }
1243  
1244          if ( defined( 'WP_CONTENT_DIR' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) ) {
1245              $locations[] = WP_CONTENT_DIR . '/languages';
1246          }
1247  
1248          if ( @is_dir( ABSPATH . 'wp-content/languages' ) ) {
1249              $locations[] = ABSPATH . 'wp-content/languages';
1250          }
1251  
1252          if ( @is_dir( ABSPATH . WPINC . '/languages' ) ) {
1253              $locations[] = ABSPATH . WPINC . '/languages';
1254          }
1255  
1256          if ( ! $locations ) {
1257              break;
1258          }
1259  
1260          $locations = array_unique( $locations );
1261  
1262          foreach ( $locales as $locale ) {
1263              foreach ( $locations as $location ) {
1264                  if ( file_exists( $location . '/' . $locale . '.mo' ) ) {
1265                      load_textdomain( 'default', $location . '/' . $locale . '.mo' );
1266                      if ( defined( 'WP_SETUP_CONFIG' ) && file_exists( $location . '/admin-' . $locale . '.mo' ) ) {
1267                          load_textdomain( 'default', $location . '/admin-' . $locale . '.mo' );
1268                      }
1269                      break 2;
1270                  }
1271              }
1272          }
1273  
1274          break;
1275      }
1276  
1277      $wp_locale = new WP_Locale();
1278  }
1279  
1280  /**
1281   * Check or set whether WordPress is in "installation" mode.
1282   *
1283   * If the `WP_INSTALLING` constant is defined during the bootstrap, `wp_installing()` will default to `true`.
1284   *
1285   * @since 4.4.0
1286   *
1287   * @param bool $is_installing Optional. True to set WP into Installing mode, false to turn Installing mode off.
1288   *                            Omit this parameter if you only want to fetch the current status.
1289   * @return bool True if WP is installing, otherwise false. When a `$is_installing` is passed, the function will
1290   *              report whether WP was in installing mode prior to the change to `$is_installing`.
1291   */
1292  function wp_installing( $is_installing = null ) {
1293      static $installing = null;
1294  
1295      // Support for the `WP_INSTALLING` constant, defined before WP is loaded.
1296      if ( is_null( $installing ) ) {
1297          $installing = defined( 'WP_INSTALLING' ) && WP_INSTALLING;
1298      }
1299  
1300      if ( ! is_null( $is_installing ) ) {
1301          $old_installing = $installing;
1302          $installing     = $is_installing;
1303          return (bool) $old_installing;
1304      }
1305  
1306      return (bool) $installing;
1307  }
1308  
1309  /**
1310   * Determines if SSL is used.
1311   *
1312   * @since 2.6.0
1313   * @since 4.6.0 Moved from functions.php to load.php.
1314   *
1315   * @return bool True if SSL, otherwise false.
1316   */
1317  function is_ssl() {
1318      if ( isset( $_SERVER['HTTPS'] ) ) {
1319          if ( 'on' === strtolower( $_SERVER['HTTPS'] ) ) {
1320              return true;
1321          }
1322  
1323          if ( '1' == $_SERVER['HTTPS'] ) {
1324              return true;
1325          }
1326      } elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
1327          return true;
1328      }
1329      return false;
1330  }
1331  
1332  /**
1333   * Converts a shorthand byte value to an integer byte value.
1334   *
1335   * @since 2.3.0
1336   * @since 4.6.0 Moved from media.php to load.php.
1337   *
1338   * @link https://www.php.net/manual/en/function.ini-get.php
1339   * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
1340   *
1341   * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
1342   * @return int An integer byte value.
1343   */
1344  function wp_convert_hr_to_bytes( $value ) {
1345      $value = strtolower( trim( $value ) );
1346      $bytes = (int) $value;
1347  
1348      if ( false !== strpos( $value, 'g' ) ) {
1349          $bytes *= GB_IN_BYTES;
1350      } elseif ( false !== strpos( $value, 'm' ) ) {
1351          $bytes *= MB_IN_BYTES;
1352      } elseif ( false !== strpos( $value, 'k' ) ) {
1353          $bytes *= KB_IN_BYTES;
1354      }
1355  
1356      // Deal with large (float) values which run into the maximum integer size.
1357      return min( $bytes, PHP_INT_MAX );
1358  }
1359  
1360  /**
1361   * Determines whether a PHP ini value is changeable at runtime.
1362   *
1363   * @since 4.6.0
1364   *
1365   * @link https://www.php.net/manual/en/function.ini-get-all.php
1366   *
1367   * @param string $setting The name of the ini setting to check.
1368   * @return bool True if the value is changeable at runtime. False otherwise.
1369   */
1370  function wp_is_ini_value_changeable( $setting ) {
1371      static $ini_all;
1372  
1373      if ( ! isset( $ini_all ) ) {
1374          $ini_all = false;
1375          // Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes".
1376          if ( function_exists( 'ini_get_all' ) ) {
1377              $ini_all = ini_get_all();
1378          }
1379      }
1380  
1381      // Bit operator to workaround https://bugs.php.net/bug.php?id=44936 which changes access level to 63 in PHP 5.2.6 - 5.2.17.
1382      if ( isset( $ini_all[ $setting ]['access'] ) && ( INI_ALL === ( $ini_all[ $setting ]['access'] & 7 ) || INI_USER === ( $ini_all[ $setting ]['access'] & 7 ) ) ) {
1383          return true;
1384      }
1385  
1386      // If we were unable to retrieve the details, fail gracefully to assume it's changeable.
1387      if ( ! is_array( $ini_all ) ) {
1388          return true;
1389      }
1390  
1391      return false;
1392  }
1393  
1394  /**
1395   * Determines whether the current request is a WordPress Ajax request.
1396   *
1397   * @since 4.7.0
1398   *
1399   * @return bool True if it's a WordPress Ajax request, false otherwise.
1400   */
1401  function wp_doing_ajax() {
1402      /**
1403       * Filters whether the current request is a WordPress Ajax request.
1404       *
1405       * @since 4.7.0
1406       *
1407       * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
1408       */
1409      return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
1410  }
1411  
1412  /**
1413   * Determines whether the current request should use themes.
1414   *
1415   * @since 5.1.0
1416   *
1417   * @return bool True if themes should be used, false otherwise.
1418   */
1419  function wp_using_themes() {
1420      /**
1421       * Filters whether the current request should use themes.
1422       *
1423       * @since 5.1.0
1424       *
1425       * @param bool $wp_using_themes Whether the current request should use themes.
1426       */
1427      return apply_filters( 'wp_using_themes', defined( 'WP_USE_THEMES' ) && WP_USE_THEMES );
1428  }
1429  
1430  /**
1431   * Determines whether the current request is a WordPress cron request.
1432   *
1433   * @since 4.8.0
1434   *
1435   * @return bool True if it's a WordPress cron request, false otherwise.
1436   */
1437  function wp_doing_cron() {
1438      /**
1439       * Filters whether the current request is a WordPress cron request.
1440       *
1441       * @since 4.8.0
1442       *
1443       * @param bool $wp_doing_cron Whether the current request is a WordPress cron request.
1444       */
1445      return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON );
1446  }
1447  
1448  /**
1449   * Check whether variable is a WordPress Error.
1450   *
1451   * Returns true if $thing is an object of the WP_Error class.
1452   *
1453   * @since 2.1.0
1454   *
1455   * @param mixed $thing Check if unknown variable is a WP_Error object.
1456   * @return bool True, if WP_Error. False, if not WP_Error.
1457   */
1458  function is_wp_error( $thing ) {
1459      return ( $thing instanceof WP_Error );
1460  }
1461  
1462  /**
1463   * Determines whether file modifications are allowed.
1464   *
1465   * @since 4.8.0
1466   *
1467   * @param string $context The usage context.
1468   * @return bool True if file modification is allowed, false otherwise.
1469   */
1470  function wp_is_file_mod_allowed( $context ) {
1471      /**
1472       * Filters whether file modifications are allowed.
1473       *
1474       * @since 4.8.0
1475       *
1476       * @param bool   $file_mod_allowed Whether file modifications are allowed.
1477       * @param string $context          The usage context.
1478       */
1479      return apply_filters( 'file_mod_allowed', ! defined( 'DISALLOW_FILE_MODS' ) || ! DISALLOW_FILE_MODS, $context );
1480  }
1481  
1482  /**
1483   * Start scraping edited file errors.
1484   *
1485   * @since 4.9.0
1486   */
1487  function wp_start_scraping_edited_file_errors() {
1488      if ( ! isset( $_REQUEST['wp_scrape_key'] ) || ! isset( $_REQUEST['wp_scrape_nonce'] ) ) {
1489          return;
1490      }
1491      $key   = substr( sanitize_key( wp_unslash( $_REQUEST['wp_scrape_key'] ) ), 0, 32 );
1492      $nonce = wp_unslash( $_REQUEST['wp_scrape_nonce'] );
1493  
1494      if ( get_transient( 'scrape_key_' . $key ) !== $nonce ) {
1495          echo "###### wp_scraping_result_start:$key ######";
1496          echo wp_json_encode(
1497              array(
1498                  'code'    => 'scrape_nonce_failure',
1499                  'message' => __( 'Scrape nonce check failed. Please try again.' ),
1500              )
1501          );
1502          echo "###### wp_scraping_result_end:$key ######";
1503          die();
1504      }
1505      if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
1506          define( 'WP_SANDBOX_SCRAPING', true );
1507      }
1508      register_shutdown_function( 'wp_finalize_scraping_edited_file_errors', $key );
1509  }
1510  
1511  /**
1512   * Finalize scraping for edited file errors.
1513   *
1514   * @since 4.9.0
1515   *
1516   * @param string $scrape_key Scrape key.
1517   */
1518  function wp_finalize_scraping_edited_file_errors( $scrape_key ) {
1519      $error = error_get_last();
1520      echo "\n###### wp_scraping_result_start:$scrape_key ######\n";
1521      if ( ! empty( $error ) && in_array( $error['type'], array( E_CORE_ERROR, E_COMPILE_ERROR, E_ERROR, E_PARSE, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
1522          $error = str_replace( ABSPATH, '', $error );
1523          echo wp_json_encode( $error );
1524      } else {
1525          echo wp_json_encode( true );
1526      }
1527      echo "\n###### wp_scraping_result_end:$scrape_key ######\n";
1528  }
1529  
1530  /**
1531   * Checks whether current request is a JSON request, or is expecting a JSON response.
1532   *
1533   * @since 5.0.0
1534   *
1535   * @return bool True if `Accepts` or `Content-Type` headers contain `application/json`.
1536   *              False otherwise.
1537   */
1538  function wp_is_json_request() {
1539  
1540      if ( isset( $_SERVER['HTTP_ACCEPT'] ) && false !== strpos( $_SERVER['HTTP_ACCEPT'], 'application/json' ) ) {
1541          return true;
1542      }
1543  
1544      if ( isset( $_SERVER['CONTENT_TYPE'] ) && 'application/json' === $_SERVER['CONTENT_TYPE'] ) {
1545          return true;
1546      }
1547  
1548      return false;
1549  
1550  }
1551  
1552  /**
1553   * Checks whether current request is a JSONP request, or is expecting a JSONP response.
1554   *
1555   * @since 5.2.0
1556   *
1557   * @return bool True if JSONP request, false otherwise.
1558   */
1559  function wp_is_jsonp_request() {
1560      if ( ! isset( $_GET['_jsonp'] ) ) {
1561          return false;
1562      }
1563  
1564      if ( ! function_exists( 'wp_check_jsonp_callback' ) ) {
1565          require_once  ABSPATH . WPINC . '/functions.php';
1566      }
1567  
1568      $jsonp_callback = $_GET['_jsonp'];
1569      if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) {
1570          return false;
1571      }
1572  
1573      /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
1574      $jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true );
1575  
1576      return $jsonp_enabled;
1577  
1578  }
1579  
1580  /**
1581   * Checks whether current request is an XML request, or is expecting an XML response.
1582   *
1583   * @since 5.2.0
1584   *
1585   * @return bool True if `Accepts` or `Content-Type` headers contain `text/xml`
1586   *              or one of the related MIME types. False otherwise.
1587   */
1588  function wp_is_xml_request() {
1589      $accepted = array(
1590          'text/xml',
1591          'application/rss+xml',
1592          'application/atom+xml',
1593          'application/rdf+xml',
1594          'text/xml+oembed',
1595          'application/xml+oembed',
1596      );
1597  
1598      if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
1599          foreach ( $accepted as $type ) {
1600              if ( false !== strpos( $_SERVER['HTTP_ACCEPT'], $type ) ) {
1601                  return true;
1602              }
1603          }
1604      }
1605  
1606      if ( isset( $_SERVER['CONTENT_TYPE'] ) && in_array( $_SERVER['CONTENT_TYPE'], $accepted, true ) ) {
1607          return true;
1608      }
1609  
1610      return false;
1611  }


Generated: Sat Sep 19 01:00:03 2020 Cross-referenced by PHPXref 0.7.1