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


Generated: Wed Dec 2 01:00:04 2020 Cross-referenced by PHPXref 0.7.1