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
2626function 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}
3031main ();
3132
@@ -48,7 +49,7 @@ function get_environments(): array {
4849 */
4950function 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 */
8485function 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