From 09aaa764521197be5dd3f40e3913de7bce1823b6 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 6 Jun 2020 11:05:35 +0000 Subject: [PATCH] Bootstrap/Load: Introduce `wp_get_environment_type()` to retrieve the current environment type. The type can be set via the `WP_ENVIRONMENT_TYPE` global system variable, a constant of the same name, or the `wp_get_environment_type` filter. Possible values include `development`, `stage`, `production'. If not set, the type defaults to `production`. Props Clorith, krogsgard, joostdevalk, frank-klein, Maelacuna, nathanrice, grierson, jchristopher, davidvee, jackfungi, johnbillion, tabrisrp, knutsp, ev3rywh3re, Rastaban, danielbachhuber, pfefferle, audrasjb, SergeyBiryukov. Fixes #33161. git-svn-id: https://develop.svn.wordpress.org/trunk@47919 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/load.php | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 70b2188c4b..2ce982af2c 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -128,6 +128,75 @@ function wp_check_php_mysql_versions() { } } +/** + * Retrieves the current environment type. + * + * The type can be set via the `WP_ENVIRONMENT_TYPE` global system variable, + * a constant of the same name, or the {@see 'wp_get_environment_type'} filter. + * + * Possible values include 'development', 'stage', 'production'. If not set, + * the type defaults to 'production'. + * + * @since 5.5.0 + * + * @return string The current environment type. + */ +function wp_get_environment_type() { + $approved_environments = array( + 'development', + 'stage', + 'production', + ); + + /** + * Filters the list of approved environment types. + * + * This filter runs before it can be used by plugins. It is designed for non-web runtimes. + * + * @since 5.5.0 + * + * @param string $approved_environments The list of approved environment types. Possible values + * include 'development', 'stage', 'production'. + */ + $approved_environments = apply_filters( 'wp_approved_environment_types', $approved_environments ); + + $current_env = ''; + + // Check if a environment variable has been set for max flexibility, if `getenv` is available on the system. + if ( function_exists( 'getenv' ) ) { + $has_env = getenv( 'WP_ENVIRONMENT_TYPE' ); + if ( false !== $has_env ) { + $current_env = $has_env; + } + } + + // Fetch the environment from a constant, this overrides the global system variable. + if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) { + $current_env = WP_ENVIRONMENT_TYPE; + } + + /** + * Filters the current environment type. + * + * This filter runs before it can be used by plugins. It is designed for + * non-web runtimes. The value returned by this filter has a priority over both + * the `WP_ENVIRONMENT_TYPE` system variable and a constant of the same name. + * + * @since 5.5.0 + * + * @param string $current_env The current environment type. Possible values + * include 'development', 'stage', 'production'. + */ + $current_env = apply_filters( 'wp_get_environment_type', $current_env ); + + // Make sure the environment is an allowed one, and not accidentally set to an invalid value. + if ( ! in_array( $current_env, $approved_environments, true ) ) { + $current_env = 'production'; + } + + return $current_env; +} + /** * Don't load all of WordPress when handling a favicon.ico request. *