From 869a284bae731478ae2d276b29653fe26180493b Mon Sep 17 00:00:00 2001 From: Mika Haulo Date: Wed, 3 May 2017 14:35:03 +0300 Subject: [PATCH 1/2] Merge WP-palvelu Instance Switcher plugin to this plugin --- js/instance_switcher.js | 17 ++++ modules/instance_switcher.php | 172 ++++++++++++++++++++++++++++++++++ seravo-plugin.php | 7 ++ style/instance_switcher.css | 4 + 4 files changed, 200 insertions(+) create mode 100644 js/instance_switcher.js create mode 100644 modules/instance_switcher.php create mode 100644 style/instance_switcher.css 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; +} + From 48413cd269087bd27961a65c73e69fbdd96678f1 Mon Sep 17 00:00:00 2001 From: Mika Haulo Date: Fri, 5 May 2017 01:34:18 +0300 Subject: [PATCH 2/2] Get shadow list from SWD API --- modules/instance_switcher.php | 63 +++++++++++++++++++---------------- seravo-plugin.php | 1 + 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/modules/instance_switcher.php b/modules/instance_switcher.php index 9ad739e1..01cefbe2 100644 --- a/modules/instance_switcher.php +++ b/modules/instance_switcher.php @@ -62,6 +62,28 @@ public static function assets(){ wp_enqueue_script( 'wpisjs', plugins_url( '../js/instance_switcher.js' , __FILE__), null, null, true ); wp_enqueue_style( 'wpisjs', plugins_url( '../style/instance_switcher.css' , __FILE__), null, null, 'all' ); } + + public static function load_shadow_list(){ + if ( ( $shadow_list = get_transient( 'shadow_list' ) ) === false ) { + $site = getenv('USER'); + $ch = curl_init('http://localhost:8888/v1/site/' . $site . '/shadows'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Api-Key: ' . getenv('SERAVO_API_KEY'))); + $response = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (curl_error($ch) || $httpcode != 200) { + error_log('SWD API error '. $httpcode .': '. curl_error($ch)); + die('API call failed: ' . curl_error($ch)); + } + + curl_close($ch); + $shadow_list = json_decode($response, true); + set_transient( 'shadow_list', $shadow_list, 10 * MINUTE_IN_SECONDS ); + } + + return $shadow_list; + } /** * Create the menu itself @@ -82,46 +104,25 @@ public static function add_switcher( $wp_admin_bar ){ return; } - - $instances = get_defined_constants(); - - // get the wpis specific constants - foreach( $instances as $key => $constant ) { - if( ! preg_match( '#WPIS-#', $key ) ) { - unset( $instances[$key] ); - } - } - + $instances = InstanceSwitcher::load_shadow_list(); 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'; } + + $current_title = getenv('WP_ENV'); // create the parent menu here $wp_admin_bar->add_menu([ 'id' => $id, - 'title' => $current_instance, + 'title' => $current_title, 'href' => '#', 'meta' => [ 'class' => $menuclass, @@ -130,11 +131,17 @@ public static function add_switcher( $wp_admin_bar ){ // add menu entries for each shadow foreach($instances as $key => $instance) { + $title = $instance["env"]; + + if ( strlen( $instance["info"] ) > 0 ) { + $title .= " (" . $instance["info"] . ")"; + } + $wp_admin_bar->add_menu([ 'parent' => $id, - 'title' => substr($key, 5), - 'id' => $instance, - 'href' => "#$instance", + 'title' => $title, + 'id' => $instance["name"], + 'href' => "#" . substr($instance["name"], 6), ]); } diff --git a/seravo-plugin.php b/seravo-plugin.php index b516affc..ef61a4a4 100644 --- a/seravo-plugin.php +++ b/seravo-plugin.php @@ -167,6 +167,7 @@ public static function loadAllModules() { */ if (apply_filters('seravo_show_domains_page',true)) { require_once(dirname( __FILE__ ) . '/modules/domains.php'); + } /* * Instance switcher