├── .gitignore ├── LICENSE.md ├── README.md ├── assets ├── css │ ├── styles.css │ └── styles.min.css └── images │ ├── banner-772x250.png │ ├── icon-128x128.png │ ├── screenshot-1.png │ ├── screenshot-2.png │ ├── screenshot-3.png │ ├── screenshot-4.png │ └── screenshot-5.png ├── plugin.php └── readme.txt /.gitignore: -------------------------------------------------------------------------------- 1 | assets/screenshots.psd 2 | *.scss 3 | .sass-cache 4 | assets/css/scss 5 | .buildpath 6 | .project 7 | .settings/ 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Show Environment in Admin Bar 2 | Add an indication to the Admin Bar of the environment WordPress is running in (e.g., Prod, Staging, QA, Dev, etc). 3 | 4 | ## Description 5 | 6 | If you're like me, you often have multiple versions of the same WordPress site open in different browser windows, e.g., production in one window and development in another window. 7 | 8 | And if you're like me, you have also unwittingly edited content in the production environment thinking you were doing so in the development environment or vice versa. 9 | 10 | If so, then this plugin is for you! 11 | 12 | It adds an indication of the current environment to the Admin Bar that is easier to see than examining the URL in your browser's address bar. 13 | 14 | ![Production](assets/images/screenshot-1.png?raw=true "Production") 15 | ![Staging](assets/images/screenshot-2.png?raw=true "Staging") 16 | ![QA](assets/images/screenshot-3.png?raw=true "QA") 17 | ![Development](assets/images/screenshot-4.png?raw=true "Development") 18 | ![Custom — QA](assets/images/screenshot-5.png?raw=true "Custom — Preview") 19 | 20 | ## Installation 21 | 22 | Installation of this plugin works like any other plugin out there: 23 | 24 | 1. Upload the contents of the zip file to the '/wp-content/plugins/' directory 25 | 2. Activate the plugin through the 'Plugins' menu in WordPress 26 | 27 | ### Minimum Requirements 28 | 29 | * WordPress 4.6 or greater (3.1 if you don't need a localized version) 30 | 31 | ## Out-of-the-box behavior 32 | 33 | Out-of-the-box, 2 classes of environment are recongized: 34 | 35 | * Production 36 | * The node added to the Admin Bar has a red background (i.e., Stop/be carefull with any changes you make). 37 | * If neither a staging, QA, nor development environment is detected, then 'Prod' is displayed in the Admin Bar. 38 | * Development 39 | * The node added to the Admin Bar has a green background (i.e., Go ahead, it is safe to make changes). 40 | * If WP is running on localhost (either a loopback IP address or LAN IP address), then 'Local' displays in the Admin Bar. 41 | * If WP_DEBUG is defined & true, then 'Dev' displays in the Admin Bar. 42 | 43 | ## Customizing the out-of-the-box behavior 44 | 45 | Two additional enviroments are supported, but cannot be automatically detected: 46 | 47 | * Staging 48 | * The node added to the Admin Bar has a yellow-ish background (i.e., Slow down, changes _might_ make it into the production site). 49 | * It is not possible to automatically detect a staging environment, so you will need to use the `SHC_SHOW_ENV_STAGING` environment variable, `SHC_SHOW_ENV_STAGING` PHP constant, or `shc_show_env_id_env` filter, see below. 50 | * QA 51 | * The node added to the Admin Bar has a blue background (i.e., "It's cool", changes you make won't affect the production site). 52 | * It is not possible to automatically detect a QA environment, so you will need to use the `SHC_SHOW_ENV_QA` environment variable, `SHC_SHOW_ENV_QA` PHP constant, or `shc_show_env_id_env` filter, see below. 53 | 54 | ### Setting web server environment variables 55 | 56 | You can change the out-of-the-box behavior by defining 1 of 5 different environment variables (e.g., in the web server configuration) as follows: 57 | 58 | 1. `SHC_SHOW_ENV_PROD` 59 | 1. `SHC_SHOW_ENV_STAGING` 60 | 1. `SHC_SHOW_ENV_QA` 61 | 1. `SHC_SHOW_ENV_DEV` 62 | 1. `SHC_SHOW_ENV_CUSTOM` 63 | 64 | If one of these environment variables is set, then it's value is used as the "name" of the environment in the Admin Bar. If more than one of these web server environment variables is set (BTW, _you shouldn't do that!_), `SHC_SHOW_ENV_PROD` takes precedence, followed by `SHC_SHOW_ENV_STAGING`, followed by `SHC_SHOW_ENV_QA`, followed by `SHC_SHOW_ENV_DEV`, and finally `SHC_SHOW_ENV_CUSTOM`. 65 | 66 | If one of these environment variables is set, then the PHP constants discussed below are not examined nor is the `shc_show_env_id_env` filter applied! 67 | 68 | If `SHC_SHOW_ENV_CUSTOM` is set, then it is your responsibility to enqueue CSS rules for how that custom class should be formatted. See below. 69 | 70 | ### Defining constants in wp-config.php 71 | 72 | You can also change the out-of-the-box behavior by defining 1 of 5 different PHP constants (e.g., in wp-config.php) as follows: 73 | 74 | ```PHP 75 | // you can define one of these 5 PHP constants 76 | define( 'SHC_SHOW_ENV_PROD', 'Production' ); 77 | define( 'SHC_SHOW_ENV_STAGING', 'Staging' ); 78 | define( 'SHC_SHOW_ENV_QA', 'QA' ); 79 | define( 'SHC_SHOW_ENV_DEV', 'Development' ); 80 | define( 'SHC_SHOW_ENV_CUSTOM', 'Preview' ); 81 | 82 | ``` 83 | 84 | If one of these PHP constants is defined, then it's value is used as the "name" of the environment in the Admin Bar. If more than one of these constants is defined (BTW, _you shouldn't do that!_), `SHC_SHOW_ENV_PROD` takes precedence, followed by `SHC_SHOW_ENV_STAGING`, followed by `SHC_SHOW_ENV_QA`, followed by `SHC_SHOW_ENV_DEV`, and finally `SHC_SHOW_ENV_CUSTOM`. 85 | 86 | If one of these PHP constants is defined, then the `shc_show_env_id_env` filter is **not** applied! 87 | 88 | ### Hooking into the shc_show_env_id_env filter 89 | 90 | You can also change the out-of-the-box behavior by hooking into the `shc_show_env_id_env` filter. 91 | 92 | The function you hook into this filter should return an (indexed) array of strings. The value in index 0 is the "name" of the environment to display in the Admin Bar. The value in index 1 is the CSS class of the environment (e.g., 'prod', 'staging', 'qa', 'dev', or a custom class). 93 | 94 | If you return a custom CSS class, then it is your responsibility to enqueue CSS rules for how that custom class should be formatted. See below. 95 | 96 | For example, 97 | 98 | ```PHP 99 | add_filter( 'shc_show_env_id_env', 'my_show_env_id_env' ); 100 | function my_show_env_id_env( $env ) { 101 | // match staging.example.com 102 | if ( preg_match( '/\bstaging\b/i', $_SERVER['HTTP_HOST'] ) ) { 103 | return array( 'Staging', 'staging' ); 104 | } 105 | 106 | // match qa.example.com 107 | if ( preg_match( '/\bqa\b/i', $_SERVER['HTTP_HOST'] ) ) { 108 | return array( 'QA', 'qa' ); 109 | } 110 | 111 | return $env; 112 | } 113 | ``` 114 | 115 | ### Enqueueing CSS rules for a custom class 116 | 117 | If you return a custom CSS class from the function you hook to `shc_show_env_id_env`, then it is your responsibility to enqueue CSS to style that custom class. If you use either the `SHC_SHOW_ENV_CUSTOM` environment variable or the `SHC_SHOW_ENV_CUSTOM` PHP constant, the CSS you enqueue should use the `custom` class. 118 | 119 | For example, to style the 'preview' custom class in the example above, you could do: 120 | 121 | ```PHP 122 | add_action( 'init', 'my_plugin_init' ); 123 | function my_plugin_init() { 124 | if ( is_admin_bar_showing() ) { 125 | add_action( 'wp_enqueue_scripts', 'my_show_env_enqueue' ); 126 | add_action( 'admin_enqueue_scripts', 'my_show_env_enqueue' ); 127 | } 128 | } 129 | 130 | function my_show_env_enqueue() { 131 | wp_enqueue_style( 'my_show_env', plugins_url( 'css/my_show_env_styles.css', __FILE__ ), array( 'shc-show-env' ) ); 132 | } 133 | 134 | ``` 135 | 136 | Notice that the call to `wp_enqueue_style()` above includes `array( 'shc-show-env' )` as the 3rd parameter. This ensures that your custom CSS rules are enqueued **after** the styles from this plugin. For more info, see [wp_enqueue_style](https://developer.wordpress.org/reference/functions/wp_enqueue_style/). 137 | 138 | `css/my_show_env_styles.css` should contain something like: 139 | 140 | ```CSS 141 | #wpadminbar .ab-top-menu .shc-show-env.preview .ab-item, 142 | #wpadminbar .ab-top-menu .shc-show-env.preview:hover .ab-item { 143 | background-color: #dd823b ; 144 | } 145 | ``` 146 | 147 | The background-color you choose for your custom class should be relatively distinct from the background-colors used for all of the admin color schemes shipped with WP (after all, the whole point of this plugin is to make the indicator added to the Admin Bar stand out so you can easily see it and know which environment you are logged into). If the background-color you choose is not sufficiently distinct, you should add additional styling to help it stand out. For example, the stylesheet included with this plugin contains the following rule: 148 | 149 | ```CSS 150 | /* 151 | * background-color for 'prod' is REAL close the admin bar background-color in the sunrise 152 | * Admin Color Scheme, so put a black border around it to help it stand out 153 | */ 154 | .admin-color-sunrise #wpadminbar .ab-top-menu .shc-show-env.prod .ab-item { 155 | border: 2px solid black ; 156 | box-sizing: border-box ; 157 | /* compensate for the border, so the text stays vertically centered */ 158 | line-height: 28px; 159 | } 160 | 161 | ``` 162 | 163 | ### Conditionally hiding the environment in the Admin Bar 164 | 165 | You can also conditionally hide the indication of the environment in the Admin Bar by hooking into the `shc_show_env_hide` filter. This filter should return a boolean, with `true` meaning "hide the environment in the admin bar". For example, 166 | 167 | ```PHP 168 | add_filter( 'shc_show_env_hide', 'my_env_conditionally_hide' ); 169 | function my_env_conditionally_hide( $hide ) { 170 | return ! current_user_can( 'manage_options' ); 171 | } 172 | 173 | ``` 174 | 175 | ## Changelog 176 | 177 | ### 1.2 (2024-07-15) 178 | 179 | * Updated the priority at which `admin_bar_menu` is hooked into. See [Trac 60685, comment 18](https://core.trac.wordpress.org/ticket/60685#comment:18) for why this change was made. 180 | 181 | 182 | ### 1.1 183 | 184 | * General code reorg 185 | * Added support for the QA environment 186 | * minor CSS fixes 187 | * changed the Text Domain (for localization) to 'show-environment-in-admin-bar', so that the [translate.wordpress.org](https://translate.wordpress.org/projects/wp-plugins/show-environment-in-admin-bar) service can be used. 188 | 189 | ### 1.0.1 190 | 191 | * Correct formatting in readme.txt 192 | * Removed GitHub Plugin URL reader 193 | 194 | ### 1.0 195 | 196 | * Initial release on .org 197 | 198 | ### 0.9 199 | 200 | * PHP Coding Standards compliance 201 | 202 | ### 0.5.2 203 | 204 | * Added ! defined( 'ABSPATH' ) security check 205 | 206 | ### 0.5.1 207 | 208 | * Enqueue minified styles if `SCRIPT_DEBUG` is not defined 209 | 210 | ### 0.5 211 | 212 | * Added `SHC_SHOW_ENV_(PROD|STAGING|DEV)` environment variables to env ID 213 | 214 | ### 0.1.2 215 | 216 | * Added `shc_show_env_hide` filter, to allow conditional hiding of the admin-bar node 217 | 218 | ### 0.1.1 219 | 220 | * Fixed typo in wp-config.php constants 221 | * Made CSS for .admin-color-sunrise .shc-show-env.staging more distinct 222 | * Added detailed Developer Notes 223 | 224 | ### 0.1 225 | 226 | * Initial commit 227 | 228 | ## Ideas? 229 | Please let me know by creating a new [issue](https://github.com/pbiron/shc-show-env/issues/new) and describe your idea. 230 | Pull Requests are welcome! 231 | 232 | ## Other Notes 233 | 234 | I was inspired to write this plugin when I saw the [Blue Admin Bar](https://wordpress.org/plugins/blue-admin-bar/) plugin. I thought that was a great idea, but having the background-color of the entire Admin Bar be different was a bit jarring. 235 | 236 | ## Buy me a beer 237 | 238 | If you like this plugin, please support it's continued development by [buying me a beer](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Z6D97FA595WSU). 239 | -------------------------------------------------------------------------------- /assets/css/styles.css: -------------------------------------------------------------------------------- 1 | /* =general purpose rules */ 2 | /* 3 | * prevent change of color on :hover 4 | */ 5 | #wpadminbar:not(.mobile) .ab-top-menu .shc-show-env:hover .ab-item { 6 | color: #eee; 7 | } 8 | 9 | /* =rules for each env */ 10 | /* 11 | * rules for the 'prod' environment 12 | * 13 | * The backgroud-color for this is same as the border-left-color of #message.notice-error. 14 | */ 15 | #wpadminbar .ab-top-menu .shc-show-env.prod .ab-item, 16 | #wpadminbar .ab-top-menu .shc-show-env.prod:hover .ab-item { 17 | background-color: #dc3232; 18 | } 19 | 20 | /* 21 | * rules for the 'staging' environment 22 | * 23 | * The backgroud-color for this is same as the border-left-color of #message.notice-warning. 24 | */ 25 | #wpadminbar .ab-top-menu .shc-show-env.staging .ab-item, 26 | #wpadminbar .ab-top-menu .shc-show-env.staging:hover .ab-item { 27 | background-color: #ffba00; 28 | color: #333; 29 | font-weight: 600; 30 | } 31 | 32 | /* 33 | * rules for the 'qa' environment 34 | * 35 | * The backgroud-color for this is same as the border-left-color of #message.notice-info. 36 | */ 37 | #wpadminbar .ab-top-menu .shc-show-env.qa .ab-item, 38 | #wpadminbar .ab-top-menu .shc-show-env.qa:hover .ab-item { 39 | background-color: #00a0d2; 40 | } 41 | 42 | /* 43 | * rules for the 'dev' environment 44 | * 45 | * The backgroud-color for this is same as the border-left-color of #message.notice-success. 46 | */ 47 | #wpadminbar .ab-top-menu .shc-show-env.dev .ab-item, 48 | #wpadminbar .ab-top-menu .shc-show-env.dev:hover .ab-item { 49 | background-color: #46b450; 50 | } 51 | 52 | /* =Admin Color Scheme specific rules */ 53 | /* 54 | * @todo add description for this rule 55 | */ 56 | .admin-color-light #wpadminbar .ab-top-menu .shc-show-env:not(.staging) .ab-item { 57 | color: #eee; 58 | } 59 | 60 | /* 61 | * background-color for 'prod' is REAL close the admin bar background-color in the sunrise 62 | * Admin Color Scheme, so put a black border around it to help it stand out 63 | */ 64 | .admin-color-sunrise #wpadminbar .ab-top-menu .shc-show-env.prod .ab-item { 65 | border: 2px solid #000; 66 | box-sizing: border-box; 67 | /* compensate for the border, so the text stays vertically centered */ 68 | line-height: 28px; 69 | } 70 | 71 | /* 72 | * background-color for 'qa' is REAL close the admin bar background-color in the blue 73 | * Admin Color Scheme, so put a black border around it to help it stand out 74 | */ 75 | .admin-color-blue #wpadminbar .ab-top-menu .shc-show-env.qa .ab-item { 76 | border: 2px solid #000; 77 | box-sizing: border-box; 78 | /* compensate for the border, so the text stays vertically centered */ 79 | line-height: 28px; 80 | } 81 | 82 | /* =rules for smaller screens */ 83 | @media screen and (max-width: 782px) { 84 | /* make sure our admin bar node shows on small screens */ 85 | #wpadminbar li#wp-admin-bar-shc-show-env { 86 | display: block; 87 | } 88 | 89 | /* add some padding, equivlent to what is used by default for nodes in larger screens */ 90 | #wpadminbar li#wp-admin-bar-shc-show-env .ab-item { 91 | padding-left: 10px; 92 | padding-right: 10px; 93 | } 94 | 95 | /* compensate for the border, so the text stays vertically centered */ 96 | .admin-color-sunrise #wpadminbar .ab-top-menu .shc-show-env.prod .ab-item, 97 | .admin-color-blue #wpadminbar .ab-top-menu .shc-show-env.qa .ab-item { 98 | line-height: 42px; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /assets/css/styles.min.css: -------------------------------------------------------------------------------- 1 | #wpadminbar:not(.mobile) .ab-top-menu .shc-show-env:hover .ab-item{color:#eee}#wpadminbar .ab-top-menu .shc-show-env.prod .ab-item,#wpadminbar .ab-top-menu .shc-show-env.prod:hover .ab-item{background-color:#dc3232}#wpadminbar .ab-top-menu .shc-show-env.staging .ab-item,#wpadminbar .ab-top-menu .shc-show-env.staging:hover .ab-item{background-color:#ffba00;color:#333;font-weight:600}#wpadminbar .ab-top-menu .shc-show-env.qa .ab-item,#wpadminbar .ab-top-menu .shc-show-env.qa:hover .ab-item{background-color:#00a0d2}#wpadminbar .ab-top-menu .shc-show-env.dev .ab-item,#wpadminbar .ab-top-menu .shc-show-env.dev:hover .ab-item{background-color:#46b450}.admin-color-light #wpadminbar .ab-top-menu .shc-show-env:not(.staging) .ab-item{color:#eee}.admin-color-sunrise #wpadminbar .ab-top-menu .shc-show-env.prod .ab-item{border:2px solid #000;box-sizing:border-box;line-height:28px}.admin-color-blue #wpadminbar .ab-top-menu .shc-show-env.qa .ab-item{border:2px solid #000;box-sizing:border-box;line-height:28px}@media screen and (max-width: 782px){#wpadminbar li#wp-admin-bar-shc-show-env{display:block}#wpadminbar li#wp-admin-bar-shc-show-env .ab-item{padding-left:10px;padding-right:10px}.admin-color-sunrise #wpadminbar .ab-top-menu .shc-show-env.prod .ab-item,.admin-color-blue #wpadminbar .ab-top-menu .shc-show-env.qa .ab-item{line-height:42px}} 2 | -------------------------------------------------------------------------------- /assets/images/banner-772x250.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/banner-772x250.png -------------------------------------------------------------------------------- /assets/images/icon-128x128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/icon-128x128.png -------------------------------------------------------------------------------- /assets/images/screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/screenshot-1.png -------------------------------------------------------------------------------- /assets/images/screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/screenshot-2.png -------------------------------------------------------------------------------- /assets/images/screenshot-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/screenshot-3.png -------------------------------------------------------------------------------- /assets/images/screenshot-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/screenshot-4.png -------------------------------------------------------------------------------- /assets/images/screenshot-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pbiron/shc-show-env/c000f767e5df572180e37852d8f259e4d5761341/assets/images/screenshot-5.png -------------------------------------------------------------------------------- /plugin.php: -------------------------------------------------------------------------------- 1 | =' ) ? PHP_INT_MAX : PHP_INT_MIN ); 92 | 93 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) ); 94 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) ); 95 | 96 | return; 97 | } 98 | 99 | /** 100 | * Add a node to the admin bar that identifies the environment. 101 | * 102 | * @since 1.1 103 | * 104 | * @return void 105 | * 106 | * @action admin_bar_menu 107 | */ 108 | static function add_admin_bar_node() { 109 | /** 110 | * @global WP_Admin_Bar $wp_admin_bar Global admin bar object. 111 | */ 112 | global $wp_admin_bar; 113 | 114 | list( $name, $class ) = self::id_env(); 115 | 116 | // ensure $class is a single token 117 | $class = str_replace( ' ', '-', $class ); 118 | $args = array( 119 | 'id' => 'shc-show-env', 120 | 'parent' => 'top-secondary', 121 | 'title' => $name, 122 | 'meta' => array( 'class' => "shc-show-env $class" ), 123 | ); 124 | $wp_admin_bar->add_node( $args ); 125 | 126 | return; 127 | } 128 | 129 | /** 130 | * Identify the environment we're running in. 131 | * 132 | * If the environment is represented by a custom class (via any of 133 | * the `SHC_SHOW_ENV_CUSTOM` environment variable, the `SHC_SHOW_ENV_CUSTOM` 134 | * PHP constant, or the `shc_show_env_id_env` filter), it is the responsibility 135 | * of the entity that defined that custom class to ensure that CSS styles 136 | * are enqueued that define how that custom class is to be rendered in the 137 | * Admin Bar. 138 | * 139 | * @since 1.1 140 | * 141 | * @return array $env { 142 | * @type string $0 The environment name to show in the Admin Bar. 143 | * @type string $1 The class of the env. One of 'prod', 'staging', 'qa', 'dev', 144 | * or a custom class. 145 | * } 146 | * 147 | * @see shc_show_env_id_env 148 | */ 149 | static function id_env() { 150 | // if one of our environment variables is defined (e.g., in the web server configuration), 151 | // return that...without filtering it 152 | if ( getenv( 'SHC_SHOW_ENV_PROD' ) ) { 153 | return array( getenv( 'SHC_SHOW_ENV_PROD' ), 'prod' ); 154 | } 155 | elseif ( getenv( 'SHC_SHOW_ENV_STAGING' ) ) { 156 | return array( getenv( 'SHC_SHOW_ENV_STAGING' ), 'staging' ); 157 | } 158 | elseif ( getenv( 'SHC_SHOW_ENV_QA' ) ) { 159 | return array( getenv( 'SHC_SHOW_ENV_QA' ), 'qa' ); 160 | } 161 | elseif ( getenv( 'SHC_SHOW_ENV_DEV' ) ) { 162 | return array( getenv( 'SHC_SHOW_ENV_DEV' ), 'dev'); 163 | } 164 | elseif ( getenv( 'SHC_SHOW_ENV_CUSTOM' ) ) { 165 | return array( getenv( 'SHC_SHOW_ENV_CUSTOM' ), 'custom'); 166 | } 167 | // if one of our constants is defined (e.g., in wp-config.php), 168 | // return that...without filtering it 169 | elseif ( defined( 'SHC_SHOW_ENV_PROD' ) ) { 170 | return array( SHC_SHOW_ENV_PROD, 'prod' ); 171 | } 172 | elseif ( defined( 'SHC_SHOW_ENV_STAGING' ) ) { 173 | return array( SHC_SHOW_ENV_STAGING, 'staging' ); 174 | } 175 | elseif ( defined( 'SHC_SHOW_ENV_QA' ) ) { 176 | return array( SHC_SHOW_ENV_QA, 'qa' ); 177 | } 178 | elseif ( defined( 'SHC_SHOW_ENV_DEV' ) ) { 179 | return array( SHC_SHOW_ENV_DEV, 'dev' ); 180 | } 181 | elseif ( defined( 'SHC_SHOW_ENV_CUSTOM' ) ) { 182 | return array( SHC_SHOW_ENV_CUSTOM, 'custom' ); 183 | } 184 | 185 | /* translators: abbreviation for 'Production' */ 186 | $env = array( __( 'Prod', 'show-environment-in-admin-bar' ), 'prod' ); 187 | 188 | if ( preg_match('/^(127|192\.168|169\.254)\./', $_SERVER['SERVER_ADDR'] ) || 189 | 'localhost' === $_SERVER['SERVER_NAME'] ) { 190 | /* translators: abbreviation for 'Localhost' */ 191 | $env = array( __( 'Local', 'show-environment-in-admin-bar' ), 'dev' ); 192 | } 193 | elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 194 | /* translators: abbreviation for 'Development' */ 195 | $env = array( __( 'Dev', 'show-environment-in-admin-bar' ), 'dev' ); 196 | } 197 | 198 | /** 199 | * Filters the identified environment. 200 | * 201 | * @since 1.0 202 | * 203 | * @param array $env { 204 | * @type string $0 The environment name to show in the Admin Bar. 205 | * @type string $1 The class of the env. One of 'prod', 'staging', 'qa', 'dev', 206 | * or a custom class. 207 | * } 208 | */ 209 | return apply_filters( 'shc_show_env_id_env', $env ); 210 | } 211 | 212 | /** 213 | * Register our styles. 214 | * 215 | * plugins/themes that define a custom environment class should use our 216 | * styles handle as a dependency in their call to `wp_register_style()` 217 | * or `wp_enqueue_style()` to guarantee that our styles are enqueued 218 | * before theirs. 219 | * 220 | * @since 1.1 221 | * 222 | * @return void 223 | */ 224 | function register_styles() { 225 | $suffix = SCRIPT_DEBUG ? '' : '.min'; 226 | 227 | wp_register_style( 'shc-show-env', plugins_url( "assets/css/styles{$suffix}.css", __FILE__ ), array(), self::version ) ; 228 | 229 | return; 230 | } 231 | 232 | /** 233 | * Enqueue our styles. 234 | * 235 | * @since 1.1 236 | * 237 | * @return void 238 | * 239 | * @action wp_enqueue_scripts 240 | * @action admin_enqueue_scripts 241 | */ 242 | static function enqueue_styles() { 243 | wp_enqueue_style( 'shc-show-env' ) ; 244 | 245 | return; 246 | } 247 | } 248 | 249 | // instantiate our plugin class 250 | new SHC_Show_Env(); 251 | 252 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Show Environment in Admin Bar === 2 | 3 | Contributors: pbiron 4 | Tags: admin, admin-bar 5 | Requires at least: 4.6 6 | Tested up to: 6.6 7 | Stable tag: 1.2.1 8 | License: GPLv2 or later 9 | License URL: http://www.gnu.org/licenses/gpl-2.0.html 10 | Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Z6D97FA595WSU 11 | 12 | Add an indication to the Admin Bar of the environment WordPress is running in (e.g., Prod, Staging, QA, Dev, etc). 13 | 14 | == Description == 15 | 16 | If you're like me, you often have multiple versions of the same WordPress site open in different browser windows, e.g., production in one window and development in another window. 17 | 18 | And if you're like me, you have also unwittingly edited content in the production environment thinking you were doing so in the development environment or vice versa. 19 | 20 | If so, then this plugin is for you! 21 | 22 | It adds an indication of the current environment to the Admin Bar that is easier to see than examining the URL in your browser's address bar. 23 | 24 | == Out-of-the-box behavior == 25 | 26 | Out-of-the-box, 2 different environments are recognized: 27 | 28 | * Production 29 | * The node added to the Admin Bar has a red background (i.e., Stop/be careful with any changes you make). 30 | * If neither a staging, QA, nor development environment is detected, then `Prod` is displayed in the Admin Bar. 31 | 32 | * Development 33 | * The node added to the Admin Bar has a green background (i.e., Go ahead, it is safe to make changes). 34 | * If WP is running on localhost (either a loopback IP address or LAN IP address), then `Local` displays in the Admin Bar. 35 | * If WP_DEBUG is defined & true, then `Dev` displays in the Admin Bar. 36 | 37 | == Customizing the out-of-the-box behavior == 38 | 39 | Two additional environments are supported, but cannot be automatically detected: 40 | 41 | * Staging 42 | * The node added to the Admin Bar has a yellow-ish background (i.e., Slow down, changes _might_ make it into the production site). 43 | * For information on how to enable this environment, see the [documentation](https://github.com/pbiron/shc-show-env/). 44 | * QA 45 | * The node added to the Admin Bar has a blue background (i.e., "It's cool", changes you make won't affect the production site). 46 | * For information on how to enable this environment, see the [documentation](https://github.com/pbiron/shc-show-env/). 47 | 48 | == Screenshots == 49 | 50 | 1. Production 51 | 2. Staging 52 | 3. QA 53 | 4. Development 54 | 5. Custom — Preview 55 | 56 | == Changelog == 57 | 58 | = 1.2.1 (2024-07-15) = 59 | 60 | * Bug fix: forgot to declare `$wp_version` as a global in `SHC_Show_Env:init()`. D'oh! 61 | 62 | = 1.2 (2024-07-15) = 63 | 64 | * Updated the priority at which `admin_bar_menu` is hooked into. See [Trac 60685, comment 18](https://core.trac.wordpress.org/ticket/60685#comment:18) for why this change was made. 65 | 66 | = 1.1 = 67 | 68 | * General code reorg 69 | * Added support for QA environment 70 | * minor CSS fixes 71 | * changed the Text Domain (for localization) to 'show-environment-in-admin-bar', so that the [translate.wordpress.org](https://translate.wordpress.org/projects/wp-plugins/show-environment-in-admin-bar) service can be used. 72 | 73 | = 1.0.1 = 74 | 75 | * Correct formatting in readme.txt 76 | * Removed GitHub Plugin URL reader 77 | 78 | = 1.0 = 79 | 80 | * Initial release on .org 81 | --------------------------------------------------------------------------------