From f88908cba72da107a8b6b18caad67abd46e06c99 Mon Sep 17 00:00:00 2001 From: Danny Painter Date: Sat, 19 Sep 2026 17:23:38 +0100 Subject: [PATCH] Add --database and --media flags to limit the sync wp sync --database runs the database side only (export/import, URL replace, plugin management), wp sync --media only the file transfers (uploads, and the plugins folder on vanilla projects). The --no- variants skip a part instead, and no flags syncs everything as before. Media-only syncs skip maintenance mode, as the database is never touched. --- README.md | 2 ++ core/variables.php | 4 ---- wp-cli-sync.php | 52 +++++++++++++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4a9549b..db68941 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/core/variables.php b/core/variables.php index f219758..5678b60 100644 --- a/core/variables.php +++ b/core/variables.php @@ -55,7 +55,3 @@ // Move to project root chdir($loc_proj_loc); - -// Activate Maintenance Mode -$command = $local_wp_cli . ' maintenance-mode activate'; -exec($command); diff --git a/wp-cli-sync.php b/wp-cli-sync.php index d7ef200..f62b2d7 100644 --- a/wp-cli-sync.php +++ b/wp-cli-sync.php @@ -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) { @@ -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'), + ), + )); }