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 7729d19
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ If you use [Bedrock](https://github.com/roots/bedrock), `WP_ENV` is already defi
If you're using Composer to manage WordPress, add wp-stage-switcher to your project's dependencies. Run:

```sh
composer require roots/wp-stage-switcher 1.0.3
composer require roots/wp-stage-switcher 2.0.0
```

Or manually add it to your `composer.json`:

```json
"require": {
"php": ">=5.3.0",
"wordpress": "3.9.2",
"roots/wp-stage-switcher": "1.0.3"
"php": ">=5.4.0",
"wordpress": "4.4.2",
"roots/wp-stage-switcher": "2.0.0"
}
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"forum": "http://discourse.roots.io/"
},
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"composer/installers": "~1.0",
"jwage/purl": "^0.0.6"
}
Expand Down
112 changes: 65 additions & 47 deletions wp-stage-switcher.php
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;

0 comments on commit 7729d19

Please sign in to comment.