暫無描述

load.php 49KB

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