Skip to content

Commit 370f5d3

Browse files
Brian ThompsonBrian Thompson
Brian Thompson
authored and
Brian Thompson
committed
Initial commit with functionality.
0 parents  commit 370f5d3

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Terminus Stooges Plugin
2+
[![Terminus v1.x Compatible](https://img.shields.io/badge/terminus-v1.x-green.svg)](https://github.com/terminus-plugin-project/terminus-stooges-plugin/tree/1.x)
3+
4+
*Note that this plugin only supports WordPress*
5+
6+
Terminus plugin to search and destroy Platform/Vanity domains from an environment.
7+
This is an automated way of using WP CLI's search and replace functionality to ensure copies
8+
of a Pantheon URL (pantheonsite.io) are removed from content in the database.
9+
10+
## Use Case
11+
When taking your WordPress site live, you are often left with links pointing to live-sitename.pantheonsite.io,
12+
or other links from dev, test, multidev, or vanity domain environments. While each can be cleaned up individually,
13+
this is a time consuming process requiring multiple commands to be run. This plugin aims to simplify this process.
14+
15+
## Examples
16+
### Replace platform and vanity domains from the dev environment
17+
`$ terminus site:stooges companysite-33.dev`
18+
19+
### Replace all platform and vanity domains from the live environment, regardless of source environment
20+
`$ terminus site:stooges:sparky companysite-33`
21+
22+
## Installation
23+
To install this plugin place it in `~/.terminus/plugins/`.
24+
25+
On Mac OS/Linux:
26+
```
27+
mkdir -p ~/.terminus/plugins
28+
composer create-project -d ~/.terminus/plugins terminus-plugin-project/terminus-stooges-plugin:~1
29+
```
30+
31+
## Help
32+
Run `terminus help site:stooges` for help.

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "terminus-plugin-project/terminus-stooges-plugin",
3+
"description": "Stooges - A Terminus plugin for searching & destroying (replacing) all instances of pantheonsite.io URLs in a database.",
4+
"homepage": "https://github.com/terminus-plugin-project/terminus-stooges",
5+
"license": "MIT",
6+
"type": "terminus-plugin",
7+
"require": {
8+
"php": ">=5.4"
9+
},
10+
"autoload": {
11+
"psr-4": { "Pantheon\\TerminusFiler\\": "src" }
12+
},
13+
"extra": {
14+
"terminus": {
15+
"compatible-version": "^1"
16+
}
17+
}
18+
}

src/Commands/StoogesCommand.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace Pantheon\TerminusFiler\Commands;
4+
5+
use Pantheon\Terminus\Collections\Domains;
6+
use Pantheon\Terminus\Collections\Environments;
7+
use Pantheon\Terminus\Commands\TerminusCommand;
8+
use Pantheon\Terminus\Models\Environment;
9+
use Pantheon\Terminus\Models\Site;
10+
use Pantheon\Terminus\Site\SiteAwareInterface;
11+
use Pantheon\Terminus\Site\SiteAwareTrait;
12+
use Pantheon\Terminus\Exceptions\TerminusProcessException;
13+
14+
/**
15+
* Class StoogesCommand
16+
* Searches for and destroys (replaces) all instances of a pantheonsite.io domain.
17+
*/
18+
class StoogesCommand extends TerminusCommand implements SiteAwareInterface
19+
{
20+
21+
use SiteAwareTrait;
22+
23+
/**
24+
* Replaces platform and vanity domains with the chosen custom domain.
25+
*
26+
* @authorize
27+
*
28+
* @command site:stooges
29+
* @aliases sar
30+
* @aliases search-and-replace
31+
* @aliases search-replace
32+
* @aliases search-and-destroy
33+
* @aliases stooges
34+
*
35+
* @param string $site_env Site & environment in the format `site-name.env`
36+
*
37+
* @usage terminus site:stooges <site>.<env>
38+
*/
39+
public function stooges($site_env) {
40+
41+
/** @var Environment $env */
42+
/** @var Site $site */
43+
list($site, $env) = $this->getSiteEnv($site_env);
44+
if ($site->get('framework') != 'wordpress') {
45+
$this->logger->notice('Refusing to act on site as it is not WordPress.');
46+
return;
47+
}
48+
/** @var Domains $domains */
49+
$domains = $env->getDomains()->serialize();
50+
51+
$replaceable_domains = [];
52+
$target_domains = [];
53+
foreach ($domains as $domain => $data) {
54+
if ('custom' == $data['type']) {
55+
$target_domains[] = $domain;
56+
}
57+
else {
58+
$replaceable_domains[] = $domain;
59+
}
60+
}
61+
62+
if (count($target_domains) < 1) {
63+
$this->logger->notice('Refusing to act on site as it has no custom domains available.');
64+
return;
65+
}
66+
67+
$target_domain = $this->io()->choice('What domain would you like to use as the replacement domain?', $target_domains);
68+
69+
$echoOutputFn = function ($type, $buffer) {
70+
};
71+
72+
foreach ($replaceable_domains as $replaceable_domain) {
73+
$command_line = 'wp search-replace ' . $replaceable_domain . ' ' . $target_domain;
74+
$this->io()->text('Replacing domain ' . $replaceable_domain . ' with ' . $target_domain);
75+
$result = $env->sendCommandViaSsh($command_line, $echoOutputFn, false);
76+
$output = $result['output'];
77+
$exit = $result['exit_code'];
78+
79+
if ($exit != 0) {
80+
throw new TerminusProcessException($output);
81+
}
82+
}
83+
84+
}
85+
86+
/**
87+
* Replaces platform and vanity domains from all environments with the chosen
88+
* custom domain.
89+
*
90+
* @authorize
91+
*
92+
* @command site:stooges:sparky
93+
* @aliases sparky
94+
*
95+
* @param string $site_id Site in the format `site-name`
96+
*
97+
* @usage terminus site:stooges:sparky <site>
98+
*/
99+
public function sparky($site_id) {
100+
/** @var Site $site */
101+
$site = $this->getSite($site_id);
102+
103+
if ($site->get('framework') != 'wordpress') {
104+
$this->logger->notice('Refusing to act on site as it is not WordPress.');
105+
return;
106+
}
107+
108+
/** @var Environments $environments */
109+
$environments = $site->getEnvironments()->fetch();
110+
$environment_ids = $environments->ids();
111+
/** @var Environment $live_environment */
112+
$live_environment = $environments->get('live');
113+
$replaceable_domains = [];
114+
$target_domains = [];
115+
foreach ($environment_ids as $environment_id) {
116+
/** @var Environment $environment */
117+
$environment = $environments->get($environment_id);
118+
/** @var Domains $domains */
119+
$domains = $environment->getDomains()->serialize();
120+
foreach ($domains as $domain => $data) {
121+
if ('custom' == $data['type']) {
122+
$target_domains[] = $domain;
123+
}
124+
else {
125+
$replaceable_domains[] = $domain;
126+
}
127+
}
128+
}
129+
130+
if (count($target_domains) == 1) {
131+
$target_domain = array_pop($target_domains);
132+
}
133+
elseif (count($target_domains) == 0) {
134+
$this->logger->notice('Refusing to act on site as their are no custom domains.');
135+
return;
136+
}
137+
else {
138+
$target_domain = $this->io()->choice('What domain would you like to use as the replacement domain?', $target_domains);
139+
}
140+
141+
$echoOutputFn = function ($type, $buffer) {
142+
};
143+
144+
foreach ($replaceable_domains as $replaceable_domain) {
145+
$command_line = 'wp search-replace ' . $replaceable_domain . ' ' . $target_domain;
146+
$this->io()->text('Replacing domain ' . $replaceable_domain . ' with ' . $target_domain);
147+
$result = $live_environment->sendCommandViaSsh($command_line, $echoOutputFn, false);
148+
$output = $result['output'];
149+
$exit = $result['exit_code'];
150+
151+
if ($exit != 0) {
152+
throw new TerminusProcessException($output);
153+
}
154+
}
155+
156+
}
157+
158+
}

0 commit comments

Comments
 (0)