-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwp-cli-sync.php
More file actions
110 lines (90 loc) · 3.42 KB
/
Copy pathwp-cli-sync.php
File metadata and controls
110 lines (90 loc) · 3.42 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
<?php
/*
Plugin Name: WP-CLI Sync
Description: A WP-CLI command for syncing a live site to a development environment
Version: 1.5.0
Author: Jon Beaumont-Pike
Author URI: https://jonbp.co.uk/
License: MIT License
*/
// Set Default Vars
$env_variables = array(
'LIVE_SSH_USERNAME' => '',
'LIVE_SSH_HOSTNAME' => '',
'LIVE_DOMAIN' => '',
'REMOTE_PROJECT_LOCATION' => '',
'DEV_DOMAIN' => '',
'DEV_ACTIVATED_PLUGINS' => '',
'DEV_DEACTIVATED_PLUGINS' => '',
'DEV_POST_SYNC_QUERIES' => '',
'DEV_SYNC_DIR_EXCLUDES' => '',
'DEV_TASK_DEBUG' => '',
// Left empty to be auto-detected in core/variables.php
'UPLOAD_DIR' => '',
'PLUGIN_DIR' => '',
'LOCAL_PROJECT_LOCATION' => '',
'LOCAL_WP_CLI' => '',
'REMOTE_WP_CLI' => ''
);
// Values come from the environment, where bedrock's .env lands them, or from
// constants defined in wp-config.php on a vanilla project
foreach($env_variables as $env_variable => $env_variable_default) {
$env_variable_value = getenv($env_variable);
if (empty($env_variable_value) && defined($env_variable)) {
$env_variable_value = constant($env_variable);
}
$_ENV[$env_variable] = $env_variable_value ?: $env_variable_default;
}
// Define Sync Command
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$sync = function($args, $assoc_args) {
// Flags: --database / --media limit the sync; neither = sync everything.
// filter_var so --database=true/1 and --database=false/0 behave too.
$database_flag = isset($assoc_args['database']) ? filter_var($assoc_args['database'], FILTER_VALIDATE_BOOLEAN) : null;
$media_flag = isset($assoc_args['media']) ? filter_var($assoc_args['media'], FILTER_VALIDATE_BOOLEAN) : null;
$only_requested = ($database_flag === true) || ($media_flag === true);
$sync_database = $database_flag ?? !$only_requested;
$sync_media = $media_flag ?? !$only_requested;
if (!$sync_database && !$sync_media) {
WP_CLI::error('Nothing to sync: both --no-database and --no-media given.');
}
// Include base functions
require_once(__DIR__.'/core/functions.php');
require_once(__DIR__.'/core/variables.php');
// Activate Maintenance Mode (media-only syncs leave the database alone)
if ($sync_database) {
$command = $local_wp_cli . ' maintenance-mode activate';
exec($command);
}
// Include tasks
require_once(__DIR__.'/tasks/connection_check.php');
if ($sync_database) {
require_once(__DIR__.'/tasks/database_sync.php');
require_once(__DIR__.'/tasks/url_replace.php');
}
if ($sync_media) {
require_once(__DIR__.'/tasks/uploads_sync.php');
require_once(__DIR__.'/tasks/plugins_sync.php');
}
if ($sync_database) {
require_once(__DIR__.'/tasks/plugins_management.php');
// Deactivate Maintenance Mode
$command = $local_wp_cli . ' maintenance-mode deactivate';
exec($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, array(
'synopsis' => array(
array('type' => 'flag', 'name' => 'database', 'optional' => true, 'description' => 'Only sync the database'),
array('type' => 'flag', 'name' => 'media', 'optional' => true, 'description' => 'Only sync the uploads folder'),
),
));
}