-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-network-search-replace.php
More file actions
69 lines (61 loc) · 1.89 KB
/
Copy pathwp-network-search-replace.php
File metadata and controls
69 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Run Search and Replace on WordPress Network sites.
*/
if (!empty($_ENV['PANTHEON_ENVIRONMENT'] && $_ENV['PANTHEON_ENVIRONMENT'] !== 'live') && !empty($_POST['wf_type'] && $_POST['wf_type'] == 'clone_database')) {
// Get environment contexts
$target = $_POST['to_environment'];
$source = $_POST['from_environment'];
// Get domains
$old_domain = get_domain($source);
$new_domain = get_domain($target);
// Run Search Replace on WPMS sites
$cmd = "wp search-replace '{$old_domain}' '{$new_domain}' --precise --recurse-objects --all-tables --verbose --network --skip-columns=guid";
passthru($cmd);
}
/**
* Fetch customer domains.
*
* @param [string] $env
* @return void
*/
function get_domain($env) {
$req = pantheon_curl("https://api.live.getpantheon.com/sites/self/environments/{$env}/hostnames", NULL, 8443);
$domains = json_decode($req['body'], true);
// Check if custom domains are available.
if (count($domains) > 1) {
// Check if there is a primary domain available. If so, use that.
return get_primary_domain($domains, $env);
} else {
return get_env_domain($env);
}
}
/**
* Return current environment platform domain
*
* @return string
*/
function get_env_domain($env = null) {
$sub = (!empty($env)) ? $env : $_ENV['PANTHEON_ENVIRONMENT'];
return "{$sub}-{$_ENV['PANTHEON_SITE_NAME']}.pantheonsite.io";
}
/**
* Get primary domain (if available)
*
* @param [array] $domains
* @param [string] $env
* @return void
*/
function get_primary_domain($domains, $env = null) {
// Loop through domains
foreach($domains as $domain) {
// Only look at custom domains
if (!empty($domain['type']) && $domain['type'] == 'custom') {
// If domain is marked as primary, use that.
if (!empty($domain['primary'])) {
return $domain['key'];
}
}
}
return get_env_domain($env);
}