wp-cli.php
225 lines
| 1 | <?php |
| 2 | |
| 3 | namespace BMI\Plugin\CLI; |
| 4 | |
| 5 | use BMI\Plugin\Backup_Migration_Plugin as BMP; |
| 6 | use WP_CLI; |
| 7 | |
| 8 | if (!defined('ABSPATH')) exit; |
| 9 | |
| 10 | /** |
| 11 | * Manage backups, restores, and migrations via WP-CLI for Backup Migration plugin. |
| 12 | */ |
| 13 | class BMI_WP_CLI_Command { |
| 14 | |
| 15 | /** |
| 16 | * Run a backup via WP-CLI. |
| 17 | * |
| 18 | * ## OPTIONS |
| 19 | * |
| 20 | * [<name>] |
| 21 | * : Optional custom backup file name. |
| 22 | * |
| 23 | * [--name=<name>] |
| 24 | * : Optional custom backup file name. |
| 25 | * |
| 26 | * ## EXAMPLES |
| 27 | * |
| 28 | * wp bmi backup my_custom_backup_2026-10-31_16-41-24.zip |
| 29 | * wp bmi backup --name=my_custom_backup.zip |
| 30 | */ |
| 31 | public function backup($args = [], $assoc_args = []) { |
| 32 | if (!defined('BMI_USING_CLI_FUNCTIONALITY')) { |
| 33 | define('BMI_USING_CLI_FUNCTIONALITY', true); |
| 34 | } |
| 35 | if (!defined('BMI_CLI_FUNCTION')) { |
| 36 | define('BMI_CLI_FUNCTION', 'bmi_backup'); |
| 37 | } |
| 38 | |
| 39 | $name = null; |
| 40 | if (isset($args[0]) && !empty($args[0])) { |
| 41 | $name = $args[0]; |
| 42 | } elseif (isset($assoc_args['name']) && !empty($assoc_args['name'])) { |
| 43 | $name = $assoc_args['name']; |
| 44 | } |
| 45 | |
| 46 | if ($name && !defined('BMI_CLI_ARGUMENT')) { |
| 47 | define('BMI_CLI_ARGUMENT', $name); |
| 48 | } |
| 49 | |
| 50 | $_SERVER['REQUEST_METHOD'] = 'CLI'; |
| 51 | $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; |
| 52 | $_POST['f'] = 'create-backup'; |
| 53 | |
| 54 | $bmi = new \BMI\Plugin\Backup_Migration_Plugin(); |
| 55 | $bmi->ajax(true); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Run a scheduled cron backup via WP-CLI. |
| 60 | * |
| 61 | * ## OPTIONS |
| 62 | * |
| 63 | * [<name>] |
| 64 | * : Optional custom backup file name. |
| 65 | * |
| 66 | * [--name=<name>] |
| 67 | * : Optional custom backup file name. |
| 68 | * |
| 69 | * ## EXAMPLES |
| 70 | * |
| 71 | * wp bmi backup_cron my_custom_backup_2026-10-31_16-41-24.zip |
| 72 | * wp bmi backup_cron --name=my_custom_backup.zip |
| 73 | */ |
| 74 | public function backup_cron($args = [], $assoc_args = []) { |
| 75 | if (!defined('BMI_DOING_SCHEDULED_BACKUP')) { |
| 76 | define('BMI_DOING_SCHEDULED_BACKUP', true); |
| 77 | } |
| 78 | if (!defined('BMI_DOING_SCHEDULED_BACKUP_VIA_CLI')) { |
| 79 | define('BMI_DOING_SCHEDULED_BACKUP_VIA_CLI', true); |
| 80 | } |
| 81 | if (!defined('BMI_USING_CLI_FUNCTIONALITY')) { |
| 82 | define('BMI_USING_CLI_FUNCTIONALITY', true); |
| 83 | } |
| 84 | if (!defined('BMI_CLI_FUNCTION')) { |
| 85 | define('BMI_CLI_FUNCTION', 'bmi_backup_cron'); |
| 86 | } |
| 87 | |
| 88 | $name = null; |
| 89 | if (isset($args[0]) && !empty($args[0])) { |
| 90 | $name = $args[0]; |
| 91 | } elseif (isset($assoc_args['name']) && !empty($assoc_args['name'])) { |
| 92 | $name = $assoc_args['name']; |
| 93 | } |
| 94 | |
| 95 | if ($name && !defined('BMI_CLI_ARGUMENT')) { |
| 96 | define('BMI_CLI_ARGUMENT', $name); |
| 97 | } |
| 98 | |
| 99 | $_SERVER['REQUEST_METHOD'] = 'CLI'; |
| 100 | $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; |
| 101 | $_POST['f'] = 'create-backup'; |
| 102 | |
| 103 | $bmi = new \BMI\Plugin\Backup_Migration_Plugin(); |
| 104 | $bmi->ajax(true); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Restore a backup via WP-CLI. |
| 109 | * |
| 110 | * ## OPTIONS |
| 111 | * |
| 112 | * <file> |
| 113 | * : The backup zip file name located in the backups directory. |
| 114 | * |
| 115 | * [<remote>] |
| 116 | * : Whether the backup is remote ('true' or 'false'). |
| 117 | * |
| 118 | * [--file=<file>] |
| 119 | * : The backup zip file name. |
| 120 | * |
| 121 | * [--remote=<remote>] |
| 122 | * : Whether the backup is remote ('true' or 'false'). |
| 123 | * |
| 124 | * ## EXAMPLES |
| 125 | * |
| 126 | * wp bmi restore 2026-10-31_16-41-24.zip --remote=false |
| 127 | * wp bmi restore --file=backup_file_name.zip --remote=false |
| 128 | */ |
| 129 | public function restore($args = [], $assoc_args = []) { |
| 130 | $file = null; |
| 131 | if (isset($args[0]) && !empty($args[0])) { |
| 132 | $file = $args[0]; |
| 133 | } elseif (isset($assoc_args['file']) && !empty($assoc_args['file'])) { |
| 134 | $file = $assoc_args['file']; |
| 135 | } |
| 136 | |
| 137 | if (empty($file)) { |
| 138 | if (class_exists('WP_CLI')) { |
| 139 | \WP_CLI::error('Please specify backup file name to restore. Example: wp bmi_restore <backup_name.zip>'); |
| 140 | } |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $remote = false; |
| 145 | if (isset($args[1])) { |
| 146 | $remote = $args[1]; |
| 147 | } elseif (isset($assoc_args['remote'])) { |
| 148 | $remote = $assoc_args['remote']; |
| 149 | } |
| 150 | |
| 151 | if (!defined('BMI_USING_CLI_FUNCTIONALITY')) { |
| 152 | define('BMI_USING_CLI_FUNCTIONALITY', true); |
| 153 | } |
| 154 | if (!defined('BMI_CLI_FUNCTION')) { |
| 155 | define('BMI_CLI_FUNCTION', 'bmi_restore'); |
| 156 | } |
| 157 | if (!defined('BMI_CLI_ARGUMENT')) { |
| 158 | define('BMI_CLI_ARGUMENT', $file); |
| 159 | } |
| 160 | if (!defined('BMI_CLI_ARGUMENT_2')) { |
| 161 | define('BMI_CLI_ARGUMENT_2', $remote); |
| 162 | } |
| 163 | |
| 164 | $_SERVER['REQUEST_METHOD'] = 'CLI'; |
| 165 | $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; |
| 166 | $_POST['f'] = 'restore-backup'; |
| 167 | $_POST['file'] = $file; |
| 168 | $_POST['remote'] = $remote; |
| 169 | |
| 170 | $bmi = new \BMI\Plugin\Backup_Migration_Plugin(); |
| 171 | $bmi->ajax(true); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Perform quick migration from a backup URL via WP-CLI. |
| 176 | * |
| 177 | * ## OPTIONS |
| 178 | * |
| 179 | * <url> |
| 180 | * : The URL of the backup zip file to download and migrate. |
| 181 | * |
| 182 | * ## EXAMPLES |
| 183 | * |
| 184 | * wp bmi quick_migration https://example.com/backup.zip |
| 185 | */ |
| 186 | public function quick_migration($args = [], $assoc_args = []) { |
| 187 | $url = null; |
| 188 | if (isset($args[0]) && !empty($args[0])) { |
| 189 | $url = $args[0]; |
| 190 | } elseif (isset($assoc_args['url']) && !empty($assoc_args['url'])) { |
| 191 | $url = $assoc_args['url']; |
| 192 | } |
| 193 | |
| 194 | if (empty($url)) { |
| 195 | if (class_exists('WP_CLI')) { |
| 196 | \WP_CLI::error('Please specify backup URL. Example: wp bmi_quick_migration <backup_url>'); |
| 197 | } |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (!defined('BMI_USING_CLI_FUNCTIONALITY')) { |
| 202 | define('BMI_USING_CLI_FUNCTIONALITY', true); |
| 203 | } |
| 204 | if (!defined('BMI_CLI_FUNCTION')) { |
| 205 | define('BMI_CLI_FUNCTION', 'bmi_quick_migration'); |
| 206 | } |
| 207 | if (!defined('BMI_CLI_ARGUMENT')) { |
| 208 | define('BMI_CLI_ARGUMENT', $url); |
| 209 | } |
| 210 | |
| 211 | $_SERVER['REQUEST_METHOD'] = 'CLI'; |
| 212 | $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; |
| 213 | $_POST['f'] = 'download-backup'; |
| 214 | $_POST['url'] = $url; |
| 215 | |
| 216 | $bmi = new \BMI\Plugin\Backup_Migration_Plugin(); |
| 217 | $bmi->ajax(true); |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | if (class_exists('WP_CLI')) { |
| 223 | \WP_CLI::add_command('bmi', BMI_WP_CLI_Command::class); |
| 224 | } |
| 225 |