Skip to content

Commit 606c335

Browse files
authored
Merge pull request #14 from alleyinteractive/feature/12-structure-improve
Add support for environments of the same name
2 parents 8caca2d + fc35751 commit 606c335

File tree

6 files changed

+126
-27
lines changed

6 files changed

+126
-27
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
All notable changes to `WordPress Environment Switcher` will be documented in this file.
44

5-
## 1.1.0 - 2024-07-02
5+
## 1.2.0
6+
7+
- Added support for more flexible environment configurations.
8+
9+
## 1.1.0
610

711
- Upgrade to PHP 8.1.
812
- Change the environment switcher to show if the user has the
913
`view_environment_switcher` capability (which is mapped to `manage_options`).
1014
This allows for more fine-grained control over who can see the environment
1115
switcher.
1216

13-
## 1.0.1 - 2023-07-20
17+
## 1.0.1
1418

1519
- Infer the environment from the hosting provider, allow it to be filtered via `wp_environment_switcher_current_environment`.
1620

17-
## 1.0.0 - 2023-07-10
21+
## 1.0.0
1822

1923
- Initial release

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ add_filter(
4747
);
4848
```
4949

50+
The environments can also be defined as an array of associative arrays, which allows for more flexibility:
51+
52+
```php
53+
add_filter(
54+
'wp_environment_switcher_environments',
55+
fn () => [
56+
[
57+
'type' => 'production',
58+
'url' => 'https://example.org',
59+
'label' => 'Production Site',
60+
],
61+
[
62+
'type' => 'staging',
63+
'url' => 'https://staging.example.org',
64+
'label' => 'Staging Site',
65+
],
66+
[
67+
'type' => 'local',
68+
'url' => 'https://example.test',
69+
'label' => 'Local Development',
70+
],
71+
]
72+
);
73+
```
74+
75+
The `type`, `url`, and `label` keys are required for each environment.
76+
5077
The plugin will automatically detect the current environment and highlight it in
5178
the switcher. By default, the plugin will show the switcher to anybody with the
5279
`manage_options` capability. You can change this by modifying the capability

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"test": "tests"
77
},
88
"scripts": {
9+
"release": "npx @alleyinteractive/create-release@latest",
910
"start": "wp-env start",
1011
"test": "playwright test"
1112
},

tests/test.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Page } from '@playwright/test';
12
import { test, expect } from '@wordpress/e2e-test-utils-playwright';
23

34
test.beforeEach(async ({ page }) => {
@@ -12,9 +13,7 @@ test.beforeEach(async ({ page }) => {
1213
).toBeVisible();
1314
});
1415

15-
test('switcher is in the admin bar', async ({ page }) => {
16-
await page.goto('/wp-admin/index.php');
17-
16+
const testPage = async (page: Page) => {
1817
const switcher = page.locator('#wp-admin-bar-wp-environment-switcher');
1918
await expect(switcher).toBeVisible();
2019
await expect(switcher.locator('> a')).toHaveText('Staging');
@@ -28,4 +27,17 @@ test('switcher is in the admin bar', async ({ page }) => {
2827
await expect(dropdown.getByText('Production')).toBeVisible();
2928
await expect(dropdown.getByText('Staging')).toBeVisible();
3029
await expect(dropdown.getByText('Local')).toBeVisible();
30+
};
31+
32+
test('switcher is in the admin bar', async ({ page }) => {
33+
await page.goto('/wp-admin/');
34+
35+
await testPage(page);
36+
});
37+
38+
test('switcher is in the admin bar (key-value pairs)', async ({ page }) => {
39+
// Simulate a key-value pair environment configuration.
40+
await page.goto('/wp-admin/?keyvalue=true');
41+
42+
await testPage(page);
3143
});

tests/wp-env.php.stub

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
add_filter( 'wp_environment_switcher_current_environment', fn () => 'staging' );
44
add_filter(
55
'wp_environment_switcher_environments',
6-
fn () => [
7-
'production' => 'https://example.org',
8-
'staging' => 'https://localhost:8000',
9-
'local' => 'https://example.test',
10-
],
6+
function () {
7+
if ( ! empty( $_GET['keyvalue'] ) ) {
8+
return [
9+
'production' => 'https://example.org',
10+
'staging' => 'https://localhost:8000',
11+
'local' => 'https://example.test',
12+
];
13+
}
14+
15+
return [
16+
[
17+
'type' => 'production',
18+
'url' => 'https://example.org',
19+
'label' => 'Production',
20+
],
21+
[
22+
'type' => 'staging',
23+
'url' => 'https://localhost:8000',
24+
'label' => 'Staging',
25+
],
26+
[
27+
'type' => 'local',
28+
'url' => 'https://example.test',
29+
'label' => 'Local',
30+
],
31+
];
32+
},
1133
);

wp-environment-switcher.php

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: WordPress Environment Switcher
44
* Plugin URI: https://github.com/alleyinteractive/wp-environment-switcher
55
* Description: Easily switch between different site environments from the WordPress admin bar.
6-
* Version: 1.1.0
6+
* Version: 1.2.0
77
* Author: Sean Fisher
88
* Author URI: https://github.com/alleyinteractive/wp-environment-switcher
99
* Requires at least: 5.5.0
@@ -33,7 +33,7 @@ function main(): void {
3333
/**
3434
* Retrieve all the available environments for the switcher.
3535
*
36-
* @return array<string, string>
36+
* @return array<string, string>|array<array{type?: string, url?: string, label?: string}>
3737
*/
3838
function get_environments(): array {
3939
return (array) apply_filters( 'wp_environment_switcher_environments', [] ); // @phpstan-ignore-line return.type
@@ -109,8 +109,17 @@ function register_admin_bar(): void {
109109
return;
110110
}
111111

112+
// Determine if the environments are a key-value pair or an array of
113+
// associative arrays. Key-value pairs are of the form of 'environment' => 'url',
114+
// while associative arrays are of the form of
115+
// [ 'type' => 'environment', 'url' => 'url', 'label' => 'Label' ].
116+
$is_key_value = ! array_is_list( $environments );
117+
112118
// Fire a warning if the current environment is not in the list of environments.
113-
if ( ! isset( $environments[ $current ] ) ) {
119+
if (
120+
( $is_key_value && ! isset( $environments[ $current ] ) )
121+
|| ( ! $is_key_value && ! in_array( $current, array_column( $environments, 'type' ), true ) )
122+
) {
114123
_doing_it_wrong(
115124
__FUNCTION__,
116125
sprintf(
@@ -159,18 +168,42 @@ function register_admin_bar(): void {
159168
$callback = __NAMESPACE__ . '\\get_translated_url';
160169
}
161170

162-
foreach ( $environments as $environment => $url ) {
163-
$wp_admin_bar->add_menu(
164-
[
165-
'id' => 'wp-environment-switcher-' . $environment,
166-
'parent' => 'wp-environment-switcher',
167-
'title' => ucwords( $environment ),
168-
'href' => $callback( $url ),
169-
'meta' => [
170-
'class' => 'wp-environment-switcher__item ' . ( $environment === $current ? 'wp-environment-switcher__item--active' : '' ),
171-
],
172-
]
173-
);
171+
if ( $is_key_value ) {
172+
foreach ( $environments as $environment => $url ) {
173+
if ( ! is_string( $url ) ) {
174+
continue;
175+
}
176+
177+
$wp_admin_bar->add_menu(
178+
[
179+
'id' => 'wp-environment-switcher-' . $environment,
180+
'parent' => 'wp-environment-switcher',
181+
'title' => ucwords( $environment ),
182+
'href' => $callback( $url ),
183+
'meta' => [
184+
'class' => 'wp-environment-switcher__item ' . ( $environment === $current ? 'wp-environment-switcher__item--active' : '' ),
185+
],
186+
]
187+
);
188+
}
189+
} else {
190+
foreach ( $environments as $environment ) {
191+
if ( ! is_array( $environment ) || ! isset( $environment['type'], $environment['url'], $environment['label'] ) ) {
192+
continue;
193+
}
194+
195+
$wp_admin_bar->add_menu(
196+
[
197+
'id' => 'wp-environment-switcher-' . esc_attr( "{$environment['type']}-{$environment['label']}" ),
198+
'parent' => 'wp-environment-switcher',
199+
'title' => $environment['label'],
200+
'href' => $callback( $environment['url'] ),
201+
'meta' => [
202+
'class' => 'wp-environment-switcher__item ' . ( $environment['type'] === $current ? 'wp-environment-switcher__item--active' : '' ),
203+
],
204+
]
205+
);
206+
}
174207
}
175208
}
176209

@@ -195,7 +228,7 @@ function add_switcher_css(): void {
195228
*
196229
* @param bool $warn_production Whether to warn the user when they are on production. Defaults to true when on production.
197230
*/
198-
if ( apply_filters( 'wp_environment_switcher_warn_production', 'production' === wp_get_environment_type() ) ) {
231+
if ( apply_filters( 'wp_environment_switcher_warn_production', 'production' === get_current_environment() ) ) {
199232
?>
200233
#wpadminbar #wp-admin-bar-wp-environment-switcher:not(.hover) > .ab-item {
201234
background: #d63638;

0 commit comments

Comments
 (0)