Make WordPress Core

Changeset 47919


Ignore:
Timestamp:
06/06/2020 11:05:35 AM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/load.php

    r47871 r47919  
    127127        exit( 1 );
    128128    }
     129}
     130
     131/**
     132 * Retrieves the current environment type.
     133 *
     134 * The type can be set via the `WP_ENVIRONMENT_TYPE` global system variable,
     135 * a constant of the same name, or the {@see 'wp_get_environment_type'} filter.
     136 *
     137 * Possible values include 'development', 'stage', 'production'. If not set,
     138 * the type defaults to 'production'.
     139 *
     140 * @since 5.5.0
     141 *
     142 * @return string The current environment type.
     143 */
     144function wp_get_environment_type() {
     145    $approved_environments = array(
     146        'development',
     147        'stage',
     148        'production',
     149    );
     150
     151    /**
     152     * Filters the list of approved environment types.
     153     *
     154     * This filter runs before it can be used by plugins. It is designed for non-web runtimes.
     155     *
     156     * @since 5.5.0
     157     *
     158     * @param string $approved_environments The list of approved environment types. Possible values
     159     *                                      include 'development', 'stage', 'production'.
     160     */
     161    $approved_environments = apply_filters( 'wp_approved_environment_types', $approved_environments );
     162
     163    $current_env = '';
     164
     165    // Check if a environment variable has been set for max flexibility, if `getenv` is available on the system.
     166    if ( function_exists( 'getenv' ) ) {
     167        $has_env = getenv( 'WP_ENVIRONMENT_TYPE' );
     168        if ( false !== $has_env ) {
     169            $current_env = $has_env;
     170        }
     171    }
     172
     173    // Fetch the environment from a constant, this overrides the global system variable.
     174    if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) {
     175        $current_env = WP_ENVIRONMENT_TYPE;
     176    }
     177
     178    /**
     179     * Filters the current environment type.
     180     *
     181     * This filter runs before it can be used by plugins. It is designed for
     182     * non-web runtimes. The value returned by this filter has a priority over both
     183     * the `WP_ENVIRONMENT_TYPE` system variable and a constant of the same name.
     184     *
     185     * @since 5.5.0
     186     *
     187     * @param string $current_env The current environment type. Possible values
     188     *                            include 'development', 'stage', 'production'.
     189     */
     190    $current_env = apply_filters( 'wp_get_environment_type', $current_env );
     191
     192    // Make sure the environment is an allowed one, and not accidentally set to an invalid value.
     193    if ( ! in_array( $current_env, $approved_environments, true ) ) {
     194        $current_env = 'production';
     195    }
     196
     197    return $current_env;
    129198}
    130199
Note: See TracChangeset for help on using the changeset viewer.