generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-environment-switcher.php
217 lines (191 loc) · 5.67 KB
/
wp-environment-switcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* 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.1.0
* Author: Sean Fisher
* Author URI: https://github.com/alleyinteractive/wp-environment-switcher
* Requires at least: 5.5.0
* Tested up to: 6.2
*
* Text Domain: wp-environment-switcher
*
* @package wp-environment-switcher
*/
namespace Alley\WP\WordPress_Environment_Switcher;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Instantiate the plugin.
*/
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();
/**
* Retrieve all the available environments for the switcher.
*
* @return array<string, string>
*/
function get_environments(): array {
return (array) apply_filters( 'wp_environment_switcher_environments', [] );
}
/**
* Retrieve the current environment name.
*
* Will attempt to infer the environment from the hosting provider and fallback
* to the WP_ENVIRONMENT_TYPE constant.
*
* @return string
*/
function get_current_environment(): string {
$default = match ( true ) {
! 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(),
};
/**
* Filter the current environment name.
*
* @param string $default The current environment.
*/
return (string) apply_filters( 'wp_environment_switcher_current_environment', $default );
}
/**
* Translate the current request path to a different host.
*
* Used to translate www.example.org/the/path to staging.example.org/the/path
* for switching environments with ease.
*
* @param string $environment_url The new base URL.
* @return string
*/
function get_translated_url( string $environment_url ): string {
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
return $environment_url;
}
return rtrim( $environment_url, '/' ) . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
/**
* 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 ) ) {
return;
}
$current = get_current_environment();
// Bail if we can't determine the current environment.
if ( empty( $current ) ) {
_doing_it_wrong(
__FUNCTION__,
esc_html__( 'The current environment could not be determined.', 'wp-environment-switcher' ),
'0.1.0'
);
return;
}
// Fire a warning if the current environment is not in the list of environments.
if ( ! isset( $environments[ $current ] ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s is the current environment */
esc_html__( 'The current environment (%s) is not in the list of environments.', 'wp-environment-switcher' ),
esc_html( $current )
),
'0.1.0'
);
}
global $wp_admin_bar;
$wp_admin_bar->add_menu(
[
'id' => 'wp-environment-switcher',
'title' => ucwords( $current ),
'href' => '#',
'parent' => 'top-secondary',
'meta' => [
'class' => 'wp-environment-switcher',
],
]
);
/**
* Filter the method used to translate the URL to the new environment.
*
* @param callable $callback The callback to use to translate the URL.
*/
$callback = apply_filters( 'wp_environment_switcher_url_translation', __NAMESPACE__ . '\\get_translated_url' );
// Fire a warning if the translation callback is not callable.
if ( ! is_callable( $callback ) ) {
_doing_it_wrong(
__FUNCTION__,
esc_html__( 'The URL translation callback is not callable.', 'wp-environment-switcher' ),
'0.1.0'
);
// Reverse the callback to the default.
$callback = __NAMESPACE__ . '\\get_translated_url';
}
foreach ( $environments as $environment => $url ) {
$wp_admin_bar->add_menu(
[
'id' => 'wp-environment-switcher-' . $environment,
'parent' => 'wp-environment-switcher',
'title' => ucwords( $environment ),
'href' => $callback( $url ),
'meta' => [
'class' => 'wp-environment-switcher__item ' . ( $environment === $current ? 'wp-environment-switcher__item--active' : '' ),
],
]
);
}
}
/**
* Add CSS to support the environment switcher.
*/
function add_switcher_css(): void {
if ( empty( get_environments() ) ) {
return;
}
?>
<style>
#wpadminbar #wp-admin-bar-wp-environment-switcher > .ab-item:before {
content: "\f177";
top: 2px;
}
<?php
/**
* Filter whether to warn the user when they are on production.
*
* @param bool $warn_production Whether to warn the user when they are on production. Defaults to true when on production.
*/
if ( apply_filters( 'wp_environment_switcher_warn_production', 'production' === wp_get_environment_type() ) ) {
?>
#wpadminbar #wp-admin-bar-wp-environment-switcher:not(.hover) > .ab-item {
background: #d63638;
}
<?php
}
?>
</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;
}