-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21e4c34
commit 7729d19
Showing
4 changed files
with
71 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,99 @@ | ||
<?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 | ||
* Require Composer autoloader if installed on it's own | ||
*/ | ||
require_once __DIR__ . '/vendor/autoload.php'; | ||
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) { | ||
require_once $composer; | ||
} | ||
|
||
/** | ||
* Add stage/environment switcher to admin bar | ||
* Inspired by http://37signals.com/svn/posts/3535-beyond-the-default-rails-environments | ||
* | ||
* 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']; | ||
} | ||
|
||
$admin_bar->add_menu([ | ||
'id' => 'environment', | ||
'parent' => 'top-secondary', | ||
'title' => ucwords($current_stage), | ||
'href' => '#' | ||
]); | ||
|
||
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' => "stage_$stage", | ||
'parent' => 'environment', | ||
'title' => ucwords($stage), | ||
'href' => $url | ||
]); | ||
} | ||
} | ||
|
||
$admin_bar->add_menu(array( | ||
'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); | ||
|
||
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; |