-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommand.php
More file actions
126 lines (96 loc) · 3.95 KB
/
command.php
File metadata and controls
126 lines (96 loc) · 3.95 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
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
<?php
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
/**
* Syncs a local WP install with a remote installation.
*
* @when after_wp_load
*/
$do_sync = function($args, $assoc_args) {
try {
$env = WP_CLI\Utils\get_flag_value($assoc_args, 'env');
$sync_uploads = WP_CLI\Utils\get_flag_value($assoc_args, 'uploads', true);
$sync_users = WP_CLI\Utils\get_flag_value($assoc_args, 'users', false);
$cwd = getcwd();
$sqlfile = $cwd . '/sync.sql';
$force = WP_CLI\Utils\get_flag_value($assoc_args, 'force', false);
if (empty($env)) {
WP_CLI::error( 'Please provide an environment. Like --env=staging or --env=production.' );
}
$env = '@'.$env;
$aliasses = WP_CLI::get_configurator()->get_aliases();
if (!isset($aliasses[$env])) {
WP_CLI::error( "Environment $env doesn't seem present in your WP-CLI config." );
}
if (!isset($aliasses[$env]['url'])) {
WP_CLI::error( "The $env environment doesn't have a 'url' setting in your WP-CLI config." );
}
if (!isset($aliasses[$env]['ssh']) && !isset($aliasses[$env]['path'])) {
WP_CLI::error( "The $env environment doesn't have a 'ssh' of 'path' setting in your WP-CLI config." );
}
$localdomain = WP_CLI::runcommand("option get home", ['return' => true]);
$remotedomain = WP_CLI::runcommand("$env option get home", ['return' => true, 'exit_error' => false]);
if (empty($remotedomain)) {
WP_CLI::error('Could not get remote domain.');
}
// Ask for confirmation.
if (!$force) {
WP_CLI::confirm("Local domain is $localdomain. Remote domain is $remotedomain. Continue?");
}
// Generate table names with the correct prefix.
// @NOTE: Remote and local prefix should match!!!
$dbprefix = WP_CLI::runcommand('db prefix', ['return' => true]);
// $table_users = $dbprefix . 'users';
// $table_usermeta = $dbprefix . 'usermeta';
$table_posts = $dbprefix . 'posts';
$table_postmeta = $dbprefix . 'postmeta';
// Export DB on remote server.
WP_CLI::log('- Deleting transients.');
WP_CLI::runcommand("$env transient delete --all");
$exclude_tables = [];
$exclude_string = '';
if (!$sync_users) {
$exclude_tables[] = 'users';
$exclude_tables[] = 'usermeta';
}
if (!empty($exclude_tables)) {
$exclude_string = '--exclude_tables=' . implode(',', array_map(function($t) use ($dbprefix) {
return $dbprefix . $t;
}, $exclude_tables));
}
// Export DB on remote server.
WP_CLI::log('- Exporting database.');
WP_CLI::runcommand("$env db export $exclude_string - > \"$sqlfile\"");
// Import into local DB
WP_CLI::log('- Importing database.');
WP_CLI::runcommand("db import \"$sqlfile\"");
// Search and replace domains
if ($remotedomain && $localdomain) {
WP_CLI::log('- Replacing domains.');
WP_CLI::runcommand("search-replace $remotedomain $localdomain --no-report");
}
// Remove Ninja Forms personal data
WP_CLI::log('- Removing Ninja Forms submissions.');
WP_CLI::runcommand("db query 'delete from {$table_postmeta} where post_id in (select id from {$table_posts} where post_type = \"nf_sub\");delete from {$table_posts} where post_type = \"nf_sub\";'");
// Remove dump file
unlink($sqlfile);
if ($sync_uploads) {
$ssh = rtrim($aliasses[$env]['ssh'], '/');
$uploads = wp_upload_dir();
$upload_full_path = $uploads['basedir'];
$upload_rel_path = trim($uploads['relative'], '/');
// rsync needs a fully qualified remote path with a colon before the path.
$ssh = preg_replace('/\//', ':/', $ssh, 1);
$cmd = "rsync -av --delete $ssh/$upload_rel_path/ \"$upload_full_path\"";
// Transfer all uploaded files
WP_CLI::log('- Transfering uploads folder.');
passthru($cmd);
}
WP_CLI::success( "Sync complete." );
}
catch(Exception $error) {
WP_CLI::error($error->getMessage());
}
};
WP_CLI::add_command( 'sync', $do_sync );