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


Generated: Thu Apr 2 01:00:03 2020 Cross-referenced by PHPXref 0.7.1