load.php 42 KB

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