Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Every variable in this README works either way. The environment is checked first

3. Run `wp sync` from the project root.

To sync only part of the site, pass a flag: `wp sync --database` (database only) or `wp sync --media` (uploads folder only). With no flags, everything is synced.

## First Sync

You may find yourself working on a bedrock project that already exists on a production server and you don't have the database setup locally yet. Running `wp sync` in the project will fail in this case as it requires an active WordPress installation to run.
Expand Down
4 changes: 0 additions & 4 deletions core/variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,3 @@

// Move to project root
chdir($loc_proj_loc);

// Activate Maintenance Mode
$command = $local_wp_cli . ' maintenance-mode activate';
exec($command);
52 changes: 42 additions & 10 deletions wp-cli-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,50 @@

// Define Sync Command
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$sync = function() {
$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');
require_once(__DIR__.'/tasks/database_sync.php');
require_once(__DIR__.'/tasks/url_replace.php');
require_once(__DIR__.'/tasks/uploads_sync.php');
require_once(__DIR__.'/tasks/plugins_sync.php');
require_once(__DIR__.'/tasks/plugins_management.php');

// Deactivate Maintenance Mode
$command = $local_wp_cli . ' maintenance-mode deactivate';
exec($command);
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) {
Expand All @@ -74,5 +101,10 @@

};

WP_CLI::add_command('sync', $sync);
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'),
),
));
}