1616
1717namespace Alley \WP \WordPress_Environment_Switcher ;
1818
19+ use WP_Admin_Bar ;
20+
1921if ( ! defined ( 'ABSPATH ' ) ) {
2022 exit ;
2123}
@@ -33,10 +35,30 @@ function main(): void {
3335/**
3436 * Retrieve all the available environments for the switcher.
3537 *
36- * @return array<string, string>|array <array{type?: string, url?: string, label?: string}>
38+ * @return list <array{type?: string, url?: string, label?: string}>
3739 */
3840function get_environments (): array {
39- return (array ) apply_filters ( 'wp_environment_switcher_environments ' , [] ); // @phpstan-ignore-line return.type
41+ $ environments = (array ) apply_filters ( 'wp_environment_switcher_environments ' , [] );
42+
43+ // Determine if the environments are a key-value pair or an array of
44+ // associative arrays. Key-value pairs are of the form of 'environment' => 'url',
45+ // while associative arrays are of the form of
46+ // [ 'type' => 'environment', 'url' => 'url', 'label' => 'Label' ].
47+ $ is_key_value = ! array_is_list ( $ environments );
48+
49+ // Shape key-value pairs into associative arrays for easier handling afterwards.
50+ if ( $ is_key_value ) {
51+ // @phpstan-ignore argument.type
52+ $ environments = array_map ( static function ( string $ type , string $ url ): array {
53+ return [
54+ 'type ' => $ type ,
55+ 'url ' => $ url ,
56+ 'label ' => ucwords ( $ type ),
57+ ];
58+ }, array_keys ( $ environments ), $ environments );
59+ }
60+
61+ return $ environments ; // @phpstan-ignore return.type
4062}
4163
4264/**
@@ -83,8 +105,10 @@ function get_translated_url( string $environment_url ): string {
83105
84106/**
85107 * Register the admin environment switcher in the admin bar.
108+ *
109+ * @param WP_Admin_Bar $wp_admin_bar The admin bar instance.
86110 */
87- function register_admin_bar (): void {
111+ function register_admin_bar ( WP_Admin_Bar $ wp_admin_bar ): void {
88112 // Check if the user has permission to view the switcher.
89113 if ( ! current_user_can ( 'view_environment_switcher ' ) ) {
90114 return ;
@@ -96,10 +120,10 @@ function register_admin_bar(): void {
96120 return ;
97121 }
98122
99- $ current = get_current_environment ();
123+ $ current_type = get_current_environment ();
100124
101125 // Bail if we can't determine the current environment.
102- if ( empty ( $ current ) ) {
126+ if ( empty ( $ current_type ) ) {
103127 _doing_it_wrong (
104128 __FUNCTION__ ,
105129 esc_html__ ( 'The current environment could not be determined. ' , 'wp-environment-switcher ' ),
@@ -109,38 +133,30 @@ function register_admin_bar(): void {
109133 return ;
110134 }
111135
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-
118136 // Fire a warning if the current environment is not in the list of environments.
119- if (
120- ( $ is_key_value && ! isset ( $ environments [ $ current ] ) )
121- || ( ! $ is_key_value && ! in_array ( $ current , array_column ( $ environments , 'type ' ), true ) )
122- ) {
137+ if ( ! in_array ( $ current_type , array_column ( $ environments , 'type ' ), true ) ) {
123138 _doing_it_wrong (
124139 __FUNCTION__ ,
125140 sprintf (
126141 /* translators: %s is the current environment */
127142 esc_html__ ( 'The current environment (%s) is not in the list of environments. ' , 'wp-environment-switcher ' ),
128- esc_html ( $ current )
143+ esc_html ( $ current_type )
129144 ),
130145 '0.1.0 '
131146 );
132147 }
133148
134- global $ wp_admin_bar ;
135-
136- if ( ! $ wp_admin_bar instanceof \WP_Admin_Bar ) {
137- return ;
138- }
149+ $ current_environment = array_values ( array_filter (
150+ $ environments ,
151+ static function ( array $ environment ) use ( $ current_type ): bool {
152+ return ( $ environment ['type ' ] ?? null ) === $ current_type ;
153+ }
154+ ) ) [0 ];
139155
140156 $ wp_admin_bar ->add_menu (
141157 [
142158 'id ' => 'wp-environment-switcher ' ,
143- 'title ' => ucwords ( $ current ),
159+ 'title ' => $ current_environment [ ' label ' ] ?? ucwords ( $ current_type ),
144160 'href ' => '# ' ,
145161 'parent ' => 'top-secondary ' ,
146162 'meta ' => [
@@ -168,42 +184,30 @@ function register_admin_bar(): void {
168184 $ callback = __NAMESPACE__ . '\\get_translated_url ' ;
169185 }
170186
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- );
187+ foreach ( $ environments as $ environment ) {
188+ if ( ! isset ( $ environment ['type ' ], $ environment ['url ' ], $ environment ['label ' ] ) ) {
189+ continue ;
206190 }
191+
192+ $ environment_slug = sanitize_title_with_dashes ( $ environment ['label ' ] );
193+
194+ $ wp_admin_bar ->add_menu (
195+ [
196+ 'id ' => 'wp-environment-switcher- ' . esc_attr ( "{$ environment ['type ' ]}- {$ environment_slug }" ),
197+ 'parent ' => 'wp-environment-switcher ' ,
198+ 'title ' => $ environment ['label ' ],
199+ 'href ' => $ callback ( $ environment ['url ' ] ),
200+ 'meta ' => [
201+ 'class ' => implode ( ' ' , array_unique ( array_filter ( [
202+ 'wp-environment-switcher__item ' ,
203+ 'wp-environment-switcher__item-- ' . esc_attr ( $ environment_slug ),
204+ 'wp-environment-switcher__item-- ' . esc_attr ( $ environment ['type ' ] ),
205+ $ environment ['type ' ] === $ current_type ? 'wp-environment-switcher__item--active ' : null ,
206+ ] ) ) ),
207+ 'target ' => '_blank ' ,
208+ ],
209+ ]
210+ );
207211 }
208212}
209213
0 commit comments