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


Generated: Thu Sep 19 01:00:03 2019 Cross-referenced by PHPXref 0.7.1