|
| 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