Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit d67e499

Browse files
authored
Added filter to turn plugin off after wp init hook (#5)
* Added filter to turn plugin off after wp init hook * Moved callback functions outside init function + bumped version number
1 parent b8e1850 commit d67e499

File tree

1 file changed

+75
-67
lines changed

1 file changed

+75
-67
lines changed

plugin.php

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Description: Adds a banner to the frontend and an item to the admin bar informing about the server environment
66
* Author: Dekode
77
* Author URI: https://dekode.no
8-
* Version: 1.0.1
8+
* Version: 1.0.2
99
*
1010
* @package Dekode-Label-Environment
1111
*/
@@ -14,76 +14,84 @@
1414

1515
namespace Dekode\Label_Environment;
1616

17-
// Shows on all environment except from on production. Can be turned off by filter.
18-
if ( \apply_filters( 'dekode_label_environment_enabled', true ) ) {
17+
\add_action( 'init', __NAMESPACE__ . '\\on_init' );
1918

20-
\add_action( 'admin_bar_menu', __NAMESPACE__ . '\\add_environment_to_admin_bar', 999 );
21-
\add_action( 'admin_head', __NAMESPACE__ . '\\add_admin_bar_styles', 999 );
19+
/**
20+
* Fires after WordPress has finished loading but before any headers are sent.
21+
*
22+
* @return void
23+
*/
24+
function on_init() {
25+
// Add hooks to display the admin bar item and possibly the label.
26+
if ( \apply_filters( 'dekode_label_environment_enabled', true ) ) {
27+
\add_action( 'admin_bar_menu', __NAMESPACE__ . '\\add_environment_to_admin_bar', 999 );
28+
\add_action( 'admin_head', __NAMESPACE__ . '\\add_admin_bar_styles', 999 );
2229

23-
/**
24-
* Add information about the environment in the admin bar
25-
*
26-
* @param \WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance, passed by reference.
27-
*
28-
* @return void
29-
*/
30-
function add_environment_to_admin_bar( \WP_Admin_Bar $wp_admin_bar ) {
31-
$args = [
32-
'id' => 'dekode-label-environment',
33-
'title' => \sprintf( 'Environment: %s', \esc_html( \wp_get_environment_type() ) ),
34-
];
35-
$wp_admin_bar->add_node( $args );
30+
// Display the banner if enabled and on all environments except production.
31+
if ( \apply_filters( 'dekode_label_environment_banner_enabled', ( 'production' !== \wp_get_environment_type() ) ) ) {
32+
\add_action( 'wp_head', __NAMESPACE__ . '\\add_banner_styles' );
33+
}
3634
}
35+
}
3736

38-
// Display the banner on all environments except production.
39-
if ( \apply_filters( 'dekode_label_environment_banner_enabled', ( 'production' !== \wp_get_environment_type() ) ) ) {
40-
\add_action( 'wp_head', __NAMESPACE__ . '\\add_banner_styles' );
41-
}
37+
/**
38+
* Add information about the environment in the admin bar
39+
*
40+
* @param \WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance, passed by reference.
41+
*
42+
* @return void
43+
*/
44+
function add_environment_to_admin_bar( \WP_Admin_Bar $wp_admin_bar ) {
45+
$args = [
46+
'id' => 'dekode-label-environment',
47+
'title' => \sprintf( 'Environment: %s', \esc_html( \wp_get_environment_type() ) ),
48+
];
49+
$wp_admin_bar->add_node( $args );
50+
}
4251

43-
/**
44-
* Add inline style to head for displaying admin bar environment information in admin
45-
* Remove pointer events since the text is not linked
46-
*
47-
* @return void
48-
*/
49-
function add_admin_bar_styles() {
50-
echo '<style>#wp-admin-bar-dekode-label-environment{pointer-events:none;}</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
51-
}
52+
/**
53+
* Add inline style to head for displaying admin bar environment information in admin
54+
* Remove pointer events since the text is not linked
55+
*
56+
* @return void
57+
*/
58+
function add_admin_bar_styles() {
59+
echo '<style>#wp-admin-bar-dekode-label-environment{pointer-events:none;}</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
60+
}
5261

53-
/**
54-
* Add inline style to head for displaying the banner on frontend
55-
*
56-
* @return void
57-
*/
58-
function add_banner_styles() {
59-
$css = 'body:after{'
60-
. 'align-items:center;'
61-
. 'background-color:#1d2327;'
62-
. 'color:#f0f0f1;'
63-
. 'content:"' . \wp_get_environment_type() . '";'
64-
. 'display:flex;'
65-
. 'font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;'
66-
. 'font-size:12px;'
67-
. 'font-weight:400;'
68-
. 'left:0;'
69-
. 'line-height:24px;'
70-
. 'justify-content:center;'
71-
. 'position:fixed;'
72-
. 'top:56px;'
73-
. 'transform:rotate(-45deg);'
74-
. 'transform-origin:bottom left;'
75-
. 'width:112px;'
76-
. 'z-index:50;'
77-
. '-webkit-font-smoothing:auto;'
78-
. '}'
79-
. 'body.admin-bar:after{' // Admin bar has 32px height - bump label down.
80-
. 'top:88px;'
81-
. '}'
82-
. '@media screen and (max-width: 782px) {'
83-
. 'body.admin-bar:after{' // Admin bar has 46px height on screen resolution 782px and less - bump label down.
84-
. 'top:102px;'
85-
. '}'
86-
. '}';
87-
echo '<style>' . $css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
88-
}
62+
/**
63+
* Add inline style to head for displaying the banner on frontend
64+
*
65+
* @return void
66+
*/
67+
function add_banner_styles() {
68+
$css = 'body:after{'
69+
. 'align-items:center;'
70+
. 'background-color:#1d2327;'
71+
. 'color:#f0f0f1;'
72+
. 'content:"' . \wp_get_environment_type() . '";'
73+
. 'display:flex;'
74+
. 'font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;'
75+
. 'font-size:12px;'
76+
. 'font-weight:400;'
77+
. 'left:0;'
78+
. 'line-height:24px;'
79+
. 'justify-content:center;'
80+
. 'position:fixed;'
81+
. 'top:56px;'
82+
. 'transform:rotate(-45deg);'
83+
. 'transform-origin:bottom left;'
84+
. 'width:112px;'
85+
. 'z-index:50;'
86+
. '-webkit-font-smoothing:auto;'
87+
. '}'
88+
. 'body.admin-bar:after{' // Admin bar has 32px height - bump label down.
89+
. 'top:88px;'
90+
. '}'
91+
. '@media screen and (max-width: 782px) {'
92+
. 'body.admin-bar:after{' // Admin bar has 46px height on screen resolution 782px and less - bump label down.
93+
. 'top:102px;'
94+
. '}'
95+
. '}';
96+
echo '<style>' . $css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
8997
}

0 commit comments

Comments
 (0)