/** * Perform CLI migration. * * @param mixed $profile Profile key or array * * @return void */ protected function _perform_cli_migration($profile) { $wpmdb_cli = null; //load correct cli class if (function_exists('wpmdb_pro_cli')) { $wpmdb_cli = wpmdb_pro_cli(); } else { $wpmdb_cli = wpmdb_cli(); } if (empty($wpmdb_cli)) { WP_CLI::error(__('WP Migrate DB CLI class not available.', 'wp-migrate-db-cli')); return; } $result = $wpmdb_cli->cli_migration($profile); if (!is_wp_error($result)) { $success_msg = sprintf(__('Export saved to: %s', 'wp-migrate-db-cli'), $result); if ('find_replace' === $profile['action']) { $success_msg = __('Find & Replace complete', 'wp-migrate-db-cli'); } WP_CLI::success($success_msg); } elseif (is_wp_error($result)) { WP_CLI::error(WPMDB_CLI::cleanup_message($result->get_error_message())); } }
/** * Get profile data from CLI args. * * @param array $args * @param array $assoc_args * * @return array|WP_Error */ public function get_profile_data_from_args($args, $assoc_args) { //load correct cli class if (function_exists('wp_migrate_db_pro_cli_addon')) { $wpmdb_cli = wp_migrate_db_pro_cli_addon(); } elseif (function_exists('wpmdb_pro_cli')) { $wpmdb_cli = wpmdb_pro_cli(); } else { $wpmdb_cli = wpmdb_cli(); } $unknown_args = $this->get_unknown_args($assoc_args); if (!empty($unknown_args)) { $message = __('Parameter errors: ', 'wp-migrate-db-cli'); foreach ($unknown_args as $unknown_arg) { $message .= "\n " . sprintf(__('unknown %s parameter', 'wp-migrate-db-cli'), '--' . $unknown_arg); } if (is_a($wpmdb_cli, 'WPMDBPro_CLI')) { $message .= "\n" . __('Please make sure that you have activated the appropriate addons for WP Migrate DB Pro.', 'wp-migrate-db-cli'); } return $wpmdb_cli->cli_error($message); } if (empty($assoc_args['action'])) { return $wpmdb_cli->cli_error(__('Missing action parameter', 'wp-migrate-db-cli')); } if ('savefile' === $assoc_args['action'] && !empty($assoc_args['export_dest'])) { $export_dest = $assoc_args['export_dest']; } $action = $assoc_args['action']; // --find=<old> and --replace=<new> $replace_old = array(); $replace_new = array(); if (!empty($assoc_args['find'])) { $replace_old = str_getcsv($assoc_args['find']); } else { if ('find_replace' === $assoc_args['action']) { return $wpmdb_cli->cli_error(__('Missing find and replace values.', 'wp-migrate-db-cli')); } } if (!empty($assoc_args['replace'])) { $replace_new = str_getcsv($assoc_args['replace']); } if (count($replace_old) !== count($replace_new)) { return $wpmdb_cli->cli_error(sprintf(__('%1$s and %2$s must contain the same number of values', 'wp-migrate-db-cli'), '--find', '--replace')); } array_unshift($replace_old, ''); array_unshift($replace_new, ''); // --exclude-spam $exclude_spam = intval(isset($assoc_args['exclude-spam'])); // --gzip-file $gzip_file = intval(isset($assoc_args['gzip-file'])); $select_post_types = array(); // --exclude-post-revisions if (!empty($assoc_args['exclude-post-revisions'])) { $select_post_types[] = 'revision'; } $exclude_post_types = count($select_post_types) > 0 ? 1 : 0; // --skip-replace-guids $replace_guids = 1; if (isset($assoc_args['skip-replace-guids'])) { $replace_guids = 0; } $select_tables = array(); $table_migrate_option = 'migrate_only_with_prefix'; // --include-transients. $exclude_transients = intval(!isset($assoc_args['include-transients'])); //cleanup filename for exports if (!empty($export_dest)) { if ($gzip_file) { if ('gz' !== pathinfo($export_dest, PATHINFO_EXTENSION)) { if ('sql' === pathinfo($export_dest, PATHINFO_EXTENSION)) { $export_dest .= '.gz'; } else { $export_dest .= '.sql.gz'; } } } elseif ('sql' !== pathinfo($export_dest, PATHINFO_EXTENSION)) { $export_dest = preg_replace('/(\\.sql)?(\\.gz)?$/i', '', $export_dest) . '.sql'; } // ensure export destination is writable if (!@touch($export_dest)) { return $wpmdb_cli->cli_error(sprintf(__('Cannot write to file "%1$s". Please ensure that the specified directory exists and is writable.', 'wp-migrate-db-cli'), $export_dest)); } } $profile = compact('action', 'replace_old', 'table_migrate_option', 'replace_new', 'select_tables', 'exclude_post_types', 'select_post_types', 'replace_guids', 'exclude_spam', 'gzip_file', 'exclude_transients', 'export_dest'); $profile = apply_filters('wpmdb_cli_filter_get_profile_data_from_args', $profile, $args, $assoc_args); return $profile; }