Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_get_environment_type ›

Seit5.5.0
Veraltetn/v
wp_get_environment_type ( Keine Parameter )
Gibt zurück:
  • (string) The current environment type.
Definiert in:
Codex:

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 Array filter. Possible values include 'development', 'staging', 'production'. If not set, the type defaults to 'production'.


Quellcode

function wp_get_environment_type() {
	static $current_env = '';

	if ( $current_env ) {
		return $current_env;
	}

	$wp_environments = array(
		'development',
		'staging',
		'production',
	);

	// Check if the environment variable has been set, if `getenv` is available on the system.
	if ( function_exists( 'getenv' ) ) {
		$has_env = getenv( 'WP_ENVIRONMENT_TYPES' );
		if ( false !== $has_env ) {
			$wp_environments = explode( ',', $has_env );
		}
	}

	// Fetch the environment types from a constant, this overrides the global system variable.
	if ( defined( 'WP_ENVIRONMENT_TYPES' ) ) {
		$wp_environments = WP_ENVIRONMENT_TYPES;
	}

	/**
	 * Filters the list of supported environment types.
	 *
	 * This filter runs before it can be used by plugins. It is designed for non-web runtimes.
	 *
	 * @since 5.5.0
	 *
	 * @param array $wp_environments The list of environment types. Possible values
	 *                               include 'development', 'staging', 'production'.
	 */
	$wp_environments = apply_filters( 'wp_environment_types', $wp_environments );

	// Check if the environment variable has been set, 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', 'staging', '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, $wp_environments, true ) ) {
		$current_env = 'production';
	}

	return $current_env;
}