Skip to content

Commit e52e6a9

Browse files
authored
Merge pull request #9 from alleyinteractive/feature/cap
Add a capability to control who can view the switcher
2 parents bf231d9 + adce36a commit e52e6a9

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

.github/workflows/coding-quality.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ name: Code Quality
33
on:
44
push:
55
branches:
6-
- main
6+
- develop
77
pull_request:
8-
schedule:
9-
- cron: '0 0 * * *'
108

119
jobs:
1210
code-quality:

.github/workflows/coding-standards.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ on:
55
branches:
66
- develop
77
pull_request:
8-
schedule:
9-
- cron: '0 0 * * *'
108

119
jobs:
1210
coding-standards:

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

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

5+
## 1.1.0 - 2024-07-02
6+
7+
- Upgrade to PHP 8.1.
8+
- Change the environment switcher to show if the user has the
9+
`view_environment_switcher` capability (which is mapped to `manage_options`).
10+
This allows for more fine-grained control over who can see the environment
11+
switcher.
12+
513
## 1.0.1 - 2023-07-20
614

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

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ add_filter(
4848
```
4949

5050
The plugin will automatically detect the current environment and highlight it in
51-
the switcher.
51+
the switcher. By default, the plugin will show the switcher to anybody with the
52+
`manage_options` capability. You can change this by modifying the capability
53+
mapped to the `view_environment_switcher` capability with `map_meta_cap`.
5254

5355
## Testing
5456

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^8.0"
18+
"php": "^8.1"
1919
},
2020
"require-dev": {
21-
"alleyinteractive/alley-coding-standards": "^1.0",
21+
"alleyinteractive/alley-coding-standards": "^2.0",
2222
"szepeviktor/phpstan-wordpress": "^1.1"
2323
},
2424
"config": {

wp-environment-switcher.php

Lines changed: 23 additions & 2 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.0.0
6+
* Version: 1.1.0
77
* Author: Sean Fisher
88
* Author URI: https://github.com/alleyinteractive/wp-environment-switcher
99
* Requires at least: 5.5.0
@@ -26,6 +26,7 @@
2626
function main(): void {
2727
add_action( 'admin_bar_menu', __NAMESPACE__ . '\\register_admin_bar', 300 );
2828
add_action( 'wp_before_admin_bar_render', __NAMESPACE__ . '\\add_switcher_css' );
29+
add_filter( 'map_meta_cap', __NAMESPACE__ . '\\map_meta_cap', 10, 2 );
2930
}
3031
main();
3132

@@ -48,7 +49,7 @@ function get_environments(): array {
4849
*/
4950
function get_current_environment(): string {
5051
$default = match ( true ) {
51-
! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) => (string) $_ENV['PANTHEON_ENVIRONMENT'],
52+
! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) => (string) $_ENV['PANTHEON_ENVIRONMENT'], // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
5253
defined( 'VIP_GO_APP_ENVIRONMENT' ) => (string) VIP_GO_APP_ENVIRONMENT,
5354
default => (string) wp_get_environment_type(),
5455
};
@@ -82,6 +83,11 @@ function get_translated_url( string $environment_url ): string {
8283
* Register the admin environment switcher in the admin bar.
8384
*/
8485
function register_admin_bar(): void {
86+
// Check if the user has permission to view the switcher.
87+
if ( ! current_user_can( 'view_environment_switcher' ) ) {
88+
return;
89+
}
90+
8591
$environments = get_environments();
8692

8793
if ( empty( $environments ) ) {
@@ -194,3 +200,18 @@ function add_switcher_css(): void {
194200
</style>
195201
<?php
196202
}
203+
204+
/**
205+
* Map the meta capability for viewing the environment switcher.
206+
*
207+
* @param array<string> $caps An array of the user's capabilities.
208+
* @param string $cap The capability being checked.
209+
* @return array<string>
210+
*/
211+
function map_meta_cap( $caps, $cap ): array {
212+
if ( 'view_environment_switcher' === $cap ) {
213+
$caps = [ 'manage_options' ];
214+
}
215+
216+
return $caps;
217+
}

0 commit comments

Comments
 (0)