forked from jonbp/wp-cli-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-cli-sync.php
151 lines (124 loc) · 4.04 KB
/
wp-cli-sync.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/*
Plugin Name: WP-CLI Sync
Description: A WP-CLI command to sync the database and uploads from a live site to a development environment
Version: 1.1.3
Author: Jon Beaumont-Pike
Author URI: https://jonbp.co.uk/
License: MIT License
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$sync = function() {
// Task Message
function task_message($message, $title='Task', $color = 34, $firstBreak = true) {
if($firstBreak == true) {
echo "\n";
}
echo "\033[".$color."m".$title.": ".$message."\n\033[0m";
}
// Debug Message
function debug_message($message, $title='Debug', $color = 33, $firstBreak = false) {
if (!env('DEV_TASK_DEBUG')) {
return;
}
if ($firstBreak == true) {
echo "\n";
}
echo "\033[".$color."m".$title.": ".$message."\n\033[0m";
}
// Line Break + Color Reset
function lb_cr() {
echo "\n\033[0m";
}
// Fail Count Var
$fail_count = 0;
// Sync vars
$ssh_hostname = env('LIVE_SSH_HOSTNAME');
$ssh_username = env('LIVE_SSH_USERNAME');
$rem_proj_loc = env('REMOTE_PROJECT_LOCATION');
// Exit if some vars missing
if (empty($ssh_hostname) || empty($ssh_username) || empty($rem_proj_loc)) {
// Exit Messages
task_message('some/all dev sync vars are not set in .env file', 'Error', 31);
// Line Break + Color Reset + Exit
lb_cr();
exit();
}
// Plugin Vars
$dev_activated_plugins = env('DEV_ACTIVATED_PLUGINS');
$dev_deactivated_plugins = env('DEV_DEACTIVATED_PLUGINS');
// Move to project root
chdir(ABSPATH.'../../');
/**
* TASK: Database Sync
*/
$task_name = 'Sync Database';
task_message($task_name);
// pv check
if (`which pv`) {
$pipe = '| pv |';
} else {
task_message('Install the \'pv\' command to monitor import progress', 'Notice', 33, false);
$pipe = '|';
}
$command = 'ssh '.$ssh_username.'@'.$ssh_hostname.' "bash -c \"cd '.$rem_proj_loc.' && '.$rem_proj_loc.'/vendor/bin/wp db export --single-transaction -\"" '.$pipe.' wp db import -';
debug_message($command);
system($command);
/**
* TASK: Post sync queries
*/
if ($queries = env('DEV_POST_SYNC_QUERIES')) {
$command = 'wp db query "' . preg_replace('/(`|")/i', '\\\\${1}', $queries) . '"';
debug_message($command);
system($command);
}
/**
* TASK: Sync Uploads Folder
*/
$task_name = 'Sync Uploads Folder';
$excludes = '';
if ($exclude_dirs = env('DEV_SYNC_DIR_EXCLUDES')) {
$exclude_dirs = explode(',', $exclude_dirs);
foreach ($exclude_dirs as $dir) {
$excludes .= ' --exclude=' . $dir;
}
}
if (`which rsync`) {
task_message($task_name);
$command = 'rsync -avhP '.$ssh_username.'@'.$ssh_hostname.':'.$rem_proj_loc.'/web/app/uploads/ ./web/app/uploads/' . $excludes;
debug_message($command);
system($command);
} else {
task_message($task_name.' task not ran, please install \'rsync\'', 'Error', 31);
$fail_count++;
}
/**
* TASK: Activate / Deactivate Plugins
*/
// Activate Plugins
if (!empty($dev_activated_plugins)) {
task_message('Activate Plugins');
$cleaned_arr_list = preg_replace('/[ ,]+/', ' ', trim($dev_activated_plugins));
$command = 'wp plugin activate '.$cleaned_arr_list;
debug_message($command);
system($command);
}
// Deactivate Plugins
if (!empty($dev_deactivated_plugins)) {
task_message('Deactivate Plugins');
$cleaned_arr_list = preg_replace('/[ ,]+/', ' ', trim($dev_deactivated_plugins));
$command = 'wp plugin deactivate '.$cleaned_arr_list;
debug_message($command);
system($command);
}
// Completion Message
if ($fail_count > 0) {
task_message('Finished with '.$fail_count. ' errors', 'Warning', 33);
} else {
task_message('All Tasks Finished', 'Success', 32);
}
// Final Line Break + Color Reset
lb_cr();
};
WP_CLI::add_command( 'sync', $sync);
}