Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
kalenjohnson committed Feb 10, 2016
1 parent 21e4c34 commit b47a9a9
Showing 1 changed file with 61 additions and 45 deletions.
106 changes: 61 additions & 45 deletions wp-stage-switcher.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php
/*
Plugin Name: Stage Switcher
Plugin URI: http://roots.io/plugins/stage-switcher/
Plugin URI: https://roots.io/plugins/stage-switcher/
Description: A WordPress plugin that allows you to switch between different environments from the admin bar.
Version: 1.0.3
Author: Ben Word
Author URI: http://roots.io/
Version: 2.0.0
Author: Roots
Author URI: https://roots.io/
License: MIT License
*/

namespace Roots\Bedrock;
namespace Roots\StageSwitcher;

use Purl\Url;

/**
* Require Composer autoloader
Expand All @@ -22,60 +24,74 @@
*
* ENVIRONMENTS constant must be a serialized array of 'environment' => 'url' elements:
*
* $envs = array(
* $envs = [
* 'development' => 'http://example.dev',
* 'staging' => 'http://staging.example.com',
* 'staging' => 'http://example-staging.com',
* 'production' => 'http://example.com'
* );
* ];
*
* define('ENVIRONMENTS', serialize($envs));
*
* WP_ENV must be defined as the current environment
*/
function admin_bar_stage_switcher($admin_bar) {
if (defined('ENVIRONMENTS') && defined('WP_ENV') && apply_filters('bedrock_stage_switcher_visibility', is_super_admin())) {
class StageSwitcher {
public function __construct() {
add_action('admin_bar_menu', [$this, 'admin_bar_stage_switcher']);
add_action('wp_before_admin_bar_render', [$this, 'admin_css']);
}

public function admin_bar_stage_switcher($admin_bar) {
if (!defined('ENVIRONMENTS') && !defined('WP_ENV') && !apply_filters('bedrock/stage_switcher_visibility', is_super_admin())) {
return;
}

$stages = unserialize(ENVIRONMENTS);
$current_stage = WP_ENV;
} else {
return;
}

$admin_bar->add_menu(array(
'id' => 'environment',
'parent' => 'top-secondary',
'title' => ucwords($current_stage),
'href' => '#'
));
foreach($stages as $stage => $url) {
if ($stage === $current_stage) {
continue;
}

foreach($stages as $stage => $url) {
if ($stage === $current_stage) {
continue;
}
if (is_multisite() && defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL && !is_main_site()) {
$url = $this->multisite_url($url) . $_SERVER['REQUEST_URI'];
} else {
$url .= $_SERVER['REQUEST_URI'];
}

if (is_multisite() && defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL && !is_main_site()) {
$current_site = new \Purl\Url(get_home_url(get_current_blog_id()));
$subdomain = $current_site->subdomain;
// now take $subdomain and add it to $url
} else {
$url .= $_SERVER['REQUEST_URI'];
$admin_bar->add_menu([
'id' => 'environment',
'parent' => 'top-secondary',
'title' => ucwords($current_stage),
'href' => '#'
]);

$admin_bar->add_menu([
'id' => "stage_$stage",
'parent' => 'environment',
'title' => ucwords($stage),
'href' => $url
]);
}
}

public function admin_css() { ?>
<style>
#wp-admin-bar-environment > a:before {
content: "\f177";
top: 2px;
}
</style>
<?php
}

private function multisite_url($url) {
$stage_url = new Url($url);
$current_site = new Url(get_home_url(get_current_blog_id()));
$current_site->host = str_replace($current_site->registerableDomain, $stage_url->registerableDomain, $current_site->host);

$admin_bar->add_menu(array(
'id' => "stage_$stage",
'parent' => 'environment',
'title' => ucwords($stage),
'href' => $url
));
return rtrim($current_site->getUrl(), '/') . $_SERVER['REQUEST_URI'];
}
}
add_action('admin_bar_menu', 'Roots\\Bedrock\\admin_bar_stage_switcher');

function admin_css() { ?>
<style>
#wp-admin-bar-environment > a:before {
content: "\f177";
top: 2px;
}
</style>
<?php }
add_action('wp_before_admin_bar_render', 'Roots\\Bedrock\\admin_css');
new StageSwitcher;

0 comments on commit b47a9a9

Please sign in to comment.