Skip to content

Commit 60037c8

Browse files
authored
Merge pull request #165 from 10up/feature/environment-indicator
Adds environment type indicator to admin bar
2 parents d0f06ee + 75e2eb3 commit 60037c8

File tree

8 files changed

+155
-4
lines changed

8 files changed

+155
-4
lines changed

.lintstagedrc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"*.css": [
3-
"10up-toolkit lint-style"
3+
"10up-toolkit lint-style --allow-empty-input"
44
],
55
"*.js": [
6-
"10up-toolkit lint-js"
6+
"10up-toolkit lint-js --allow-empty-input"
77
],
88
"*.php": [
99
"./vendor/bin/phpcs --extensions=php --warning-severity=8 -s"

.stylelintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/css/*.css
2+
vendor/*

10up-experience.php

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function ( $class_name ) {
7070
AdminCustomizations\Customizations::instance();
7171
}
7272

73+
AdminCustomizations\EnvironmentIndicator::instance();
7374
API\API::instance();
7475
Authentication\Usernames::instance();
7576
Authors\Authors::instance();

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ Filters how many log items to store. Items are stored in array saved to the opti
154154

155155
Define `TENUP_DISABLE_ACTIVITYLOG` as `true` to disable Activity Log.
156156

157+
158+
### Environment Indicator
159+
160+
To enhance user awareness and minimize the risk of making unintended changes, 10up Experience includes a visual indicator integrated into the admin bar. This feature clearly displays which environment (e.g., development, staging, production) the user is currently working in.
161+
162+
157163
### Comments
158164

159165
10up Experience includes a feature to disable comments across the site. This feature can be enabled or disabled in `Settings > General`. It is disabled by default.

assets/css/admin.css

+37
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,40 @@
1010
height: 32px;
1111
width: 20px;
1212
}
13+
14+
/* Environment Indicator Styling */
15+
.tenup-experience-environment-indicator {
16+
color: #fff;
17+
pointer-events: none;
18+
}
19+
20+
.tenup-experience-environment-indicator--production {
21+
background-color: #b92a2a !important;
22+
}
23+
24+
.tenup-experience-environment-indicator--staging {
25+
background-color: #d79d00 !important;
26+
}
27+
28+
.tenup-experience-environment-indicator--local,
29+
.tenup-experience-environment-indicator--development {
30+
background-color: #34863b !important;
31+
}
32+
33+
.tenup-experience-environment-indicator .ab-icon::before {
34+
content: "\f174";
35+
top: 3px;
36+
}
37+
38+
.tenup-experience-environment-indicator--production .ab-icon::before {
39+
content: "\f319";
40+
}
41+
42+
.tenup-experience-environment-indicator--staging .ab-icon::before {
43+
content: "\f111";
44+
}
45+
46+
.tenup-experience-environment-indicator--local .ab-icon::before,
47+
.tenup-experience-environment-indicator--development .ab-icon::before {
48+
content: "\f107";
49+
}

dist/css/admin.asset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array(), 'version' => '9c247eb8cd54451023c8');
1+
<?php return array('dependencies' => array(), 'version' => '715b54e10189c970d72e');

dist/css/admin.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Admin customizations
4+
*
5+
* @package 10up-experience
6+
*/
7+
8+
namespace TenUpExperience\AdminCustomizations;
9+
10+
use TenUpExperience\Singleton;
11+
12+
/**
13+
* Admin Customizations class
14+
*/
15+
class EnvironmentIndicator {
16+
17+
use Singleton;
18+
19+
/**
20+
* Setup module
21+
*
22+
* @since 1.7
23+
*/
24+
public function setup() {
25+
// Add an admin bar item if in wp-admin.
26+
add_action( 'admin_bar_menu', [ $this, 'add_toolbar_item' ], 7 );
27+
}
28+
29+
/**
30+
* Add environment indicator to admin bar
31+
*
32+
* @param WP_Admin_Bar $admin_bar Admin bar instance
33+
*/
34+
public function add_toolbar_item( $admin_bar ) {
35+
$environment = wp_get_environment_type();
36+
37+
// If the const isn't set, and we're on a local URL, assume we're in a development environment.
38+
if ( ! defined( 'WP_ENVIRONMENT_TYPE' ) && $this->is_local_url() ) {
39+
$environment = 'local';
40+
}
41+
42+
$admin_bar->add_menu(
43+
[
44+
'id' => 'tenup-experience-environment-indicator',
45+
'parent' => 'top-secondary',
46+
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label">' . esc_html( $this->get_environment_label( $environment ) ) . '</span>',
47+
'meta' => [
48+
'class' => esc_attr( "tenup-experience-environment-indicator tenup-experience-environment-indicator--$environment" ),
49+
],
50+
]
51+
);
52+
}
53+
54+
/**
55+
* Get human readable label for environment
56+
*
57+
* @param string $environment Environment type
58+
*
59+
* @return string
60+
*/
61+
public function get_environment_label( $environment ) {
62+
switch ( $environment ) {
63+
case 'development':
64+
case 'local':
65+
$label = __( 'Development', 'tenup' );
66+
break;
67+
case 'staging':
68+
$label = __( 'Staging', 'tenup' );
69+
break;
70+
default:
71+
$label = __( 'Production', 'tenup' );
72+
break;
73+
}
74+
75+
return $label;
76+
}
77+
78+
/**
79+
* Check if the current URL is a local URL
80+
*
81+
* @return bool
82+
*/
83+
protected function is_local_url() {
84+
$home_url = untrailingslashit( home_url() );
85+
86+
return $this->str_ends_with( $home_url, '.test' ) || $this->str_ends_with( $home_url, '.local' );
87+
}
88+
89+
/**
90+
* Check if a string ends with another string
91+
*
92+
* @param string $haystack Haystack string
93+
* @param string $needle Needle string
94+
*
95+
* @return bool
96+
*/
97+
protected function str_ends_with( $haystack, $needle ) {
98+
$length = strlen( $needle );
99+
if ( ! $length ) {
100+
return true;
101+
}
102+
103+
return substr( $haystack, - $length ) === $needle;
104+
}
105+
}

0 commit comments

Comments
 (0)