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 */
3838function 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