Skip to content

Commit

Permalink
Allow the environment to be filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jul 19, 2023
1 parent da0e8ed commit 335fdaa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ Activate the plugin in WordPress and you will see the switcher appear in the top

![Screenshot of plugin](https://github.com/alleyinteractive/wp-environment-switcher/assets/346399/83684c99-4f74-4969-b302-a0c617c17190)

The plugin reads the current WordPress environment from `wp_get_environment_type()` which can be set by defining `WP_ENVIRONMENT_TYPE` in your `wp-config.php` file. You can define the available environments by using the `wp_environment_switcher_environments` filter:
The plugin reads the current WordPress environment from the current hosting
provider (Pantheon and WordPress VIP supported) and falls back to
`wp_get_environment_type()` which can be set by defining `WP_ENVIRONMENT_TYPE`
in your `wp-config.php` file. You can override the current environment by
using the `wp_environment_switcher_current_environment` filter:

```php
add_filter(
'wp_environment_switcher_current_environment',
fn () => 'my-custom-environment'
);
```

You can define the available environments by using the
`wp_environment_switcher_environments` filter:

```php
add_filter(
Expand Down
25 changes: 24 additions & 1 deletion wp-environment-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ function get_environments(): array {
return (array) apply_filters( 'wp_environment_switcher_environments', [] );
}

/**
* Retrieve the current environment name.
*
* Will attempt to infer the environment from the hosting provider and fallback
* to the WP_ENVIRONMENT_TYPE constant.
*
* @return string
*/
function get_current_environment(): string {
$default = match ( true ) {
! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) => (string) $_ENV['PANTHEON_ENVIRONMENT'],
defined( 'VIP_GO_APP_ENVIRONMENT' ) => (string) VIP_GO_APP_ENVIRONMENT,
default => (string) wp_get_environment_type(),
};

/**
* Filter the current environment name.
*
* @param string $default The current environment.
*/
return (string) apply_filters( 'wp_environment_switcher_current_environment', $default );
}

/**
* Translate the current request path to a different host.
*
Expand Down Expand Up @@ -65,7 +88,7 @@ function register_admin_bar(): void {
return;
}

$current = wp_get_environment_type();
$current = get_current_environment();

// Bail if we can't determine the current environment.
if ( empty( $current ) ) {
Expand Down

0 comments on commit 335fdaa

Please sign in to comment.