PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/load.php

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