Make WordPress Core

Ticket #47288: load.php

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