diff --git a/js/instance_switcher.js b/js/instance_switcher.js new file mode 100644 index 00000000..f9f50abe --- /dev/null +++ b/js/instance_switcher.js @@ -0,0 +1,17 @@ +(function(){ + var links = document.querySelectorAll('#wp-admin-bar-wpis li > a'); + var listener = function(e){ + e.preventDefault(); + var instance = e.target.getAttribute('href').substr(1); + if (instance === 'exit') { + document.cookie = "wpp_shadow=;path=/"; + } else { + document.cookie = "wpp_shadow=" + instance + ";path=/"; + } + location.reload(); + }; + + for(var i = 0; i < links.length; i++){ + links[i].addEventListener('click', listener); + } +})(); diff --git a/modules/instance_switcher.php b/modules/instance_switcher.php new file mode 100644 index 00000000..9ad739e1 --- /dev/null +++ b/modules/instance_switcher.php @@ -0,0 +1,172 @@ + $constant ) { + if( ! preg_match( '#WPIS-#', $key ) ) { + unset( $instances[$key] ); + } + } + + + if( empty( $instances ) ) { + return; + } + + $id = 'wpis'; + $current_instance = getenv('CONTAINER'); + + $instance_index = strpos($current_instance,'_') + 1; + for( $x = 0 ; $x < $instance_index; ++$x ) { + $current_instance = substr($current_instance, 1); + } + + // define the name of the current instance to be shown in the bar + foreach( $instances as $key => $instance ) { + if($current_instance == $instance){ + $current_instance = substr($key, 5); + } + } + + $domain = ""; //$this->get_domain( $_SERVER['HTTP_HOST'] ); + + if ( getenv('WP_ENV') && getenv('WP_ENV') != 'production' ) { + $menuclass = 'wpis-warning'; + } + + // create the parent menu here + $wp_admin_bar->add_menu([ + 'id' => $id, + 'title' => $current_instance, + 'href' => '#', + 'meta' => [ + 'class' => $menuclass, + ], + ]); + + // add menu entries for each shadow + foreach($instances as $key => $instance) { + $wp_admin_bar->add_menu([ + 'parent' => $id, + 'title' => substr($key, 5), + 'id' => $instance, + 'href' => "#$instance", + ]); + } + + // Last item is always to exit shadow + $wp_admin_bar->add_menu(array( + 'parent' => $id, + 'title' => __('Exit Shadow', 'seravo-plugin'), + 'id' => 'exit-shadow', + 'href' => "#exit", + )); + } + + public static function render_shadow_indicator() { +?> + +
+ a { + background-color: #cc0000; +} +