Skip to content

Commit

Permalink
Merge pull request #9 from alleyinteractive/feature/cap
Browse files Browse the repository at this point in the history
Add a capability to control who can view the switcher
  • Loading branch information
srtfisher authored Jul 3, 2024
2 parents bf231d9 + adce36a commit e52e6a9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/coding-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ name: Code Quality
on:
push:
branches:
- main
- develop
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
code-quality:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches:
- develop
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
coding-standards:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

## 1.1.0 - 2024-07-02

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

## 1.0.1 - 2023-07-20

- Infer the environment from the hosting provider, allow it to be filtered via `wp_environment_switcher_current_environment`.
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ add_filter(
```

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

## Testing

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
}
],
"require": {
"php": "^8.0"
"php": "^8.1"
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^1.0",
"alleyinteractive/alley-coding-standards": "^2.0",
"szepeviktor/phpstan-wordpress": "^1.1"
},
"config": {
Expand Down
25 changes: 23 additions & 2 deletions wp-environment-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WordPress Environment Switcher
* Plugin URI: https://github.com/alleyinteractive/wp-environment-switcher
* Description: Easily switch between different site environments from the WordPress admin bar.
* Version: 1.0.0
* Version: 1.1.0
* Author: Sean Fisher
* Author URI: https://github.com/alleyinteractive/wp-environment-switcher
* Requires at least: 5.5.0
Expand All @@ -26,6 +26,7 @@
function main(): void {
add_action( 'admin_bar_menu', __NAMESPACE__ . '\\register_admin_bar', 300 );
add_action( 'wp_before_admin_bar_render', __NAMESPACE__ . '\\add_switcher_css' );
add_filter( 'map_meta_cap', __NAMESPACE__ . '\\map_meta_cap', 10, 2 );
}
main();

Expand All @@ -48,7 +49,7 @@ function get_environments(): array {
*/
function get_current_environment(): string {
$default = match ( true ) {
! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) => (string) $_ENV['PANTHEON_ENVIRONMENT'],
! empty( $_ENV['PANTHEON_ENVIRONMENT'] ) => (string) $_ENV['PANTHEON_ENVIRONMENT'], // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
defined( 'VIP_GO_APP_ENVIRONMENT' ) => (string) VIP_GO_APP_ENVIRONMENT,
default => (string) wp_get_environment_type(),
};
Expand Down Expand Up @@ -82,6 +83,11 @@ function get_translated_url( string $environment_url ): string {
* Register the admin environment switcher in the admin bar.
*/
function register_admin_bar(): void {
// Check if the user has permission to view the switcher.
if ( ! current_user_can( 'view_environment_switcher' ) ) {
return;
}

$environments = get_environments();

if ( empty( $environments ) ) {
Expand Down Expand Up @@ -194,3 +200,18 @@ function add_switcher_css(): void {
</style>
<?php
}

/**
* Map the meta capability for viewing the environment switcher.
*
* @param array<string> $caps An array of the user's capabilities.
* @param string $cap The capability being checked.
* @return array<string>
*/
function map_meta_cap( $caps, $cap ): array {
if ( 'view_environment_switcher' === $cap ) {
$caps = [ 'manage_options' ];
}

return $caps;
}

0 comments on commit e52e6a9

Please sign in to comment.