| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Save migration |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace WPSynchro\API; |
| 8 |
|
| 9 |
use WPSynchro\Migration\Migration; |
| 10 |
use WPSynchro\Migration\MigrationFactory; |
| 11 |
use WPSynchro\Schedule\ScheduleFactory; |
| 12 |
use WPSynchro\Utilities\CommonFunctions; |
| 13 |
use WPSynchro\Files\Location; |
| 14 |
|
| 15 |
class SaveMigration extends WPSynchroService |
| 16 |
{ |
| 17 |
public function service() |
| 18 |
{ |
| 19 |
$nonce = $_GET['nonce'] ?? ''; |
| 20 |
if (!wp_verify_nonce($nonce, 'wpsynchro-addedit')) { |
| 21 |
\http_response_code(401); |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// Extract parameters |
| 26 |
$body = $this->getRequestBody(); |
| 27 |
$parameters = json_decode($body); |
| 28 |
|
| 29 |
// If json is invalid for some reason |
| 30 |
if (is_null($parameters)) { |
| 31 |
\http_response_code(400); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
// We can receive migration data from the wizard or from the advanced add/edit page, so we need to map it to a Migration object |
| 36 |
if (isset($parameters->type) && $parameters->type === "simple") { |
| 37 |
// Simple migration from the wizard |
| 38 |
$migration = $this->mapFromWizard($parameters); |
| 39 |
} else { |
| 40 |
// Advanced migration from the add/edit page |
| 41 |
$migration = Migration::map($parameters); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
if (strlen($migration->id) == 0) { |
| 46 |
$migration->id = uniqid(); |
| 47 |
} |
| 48 |
|
| 49 |
// Sanitize |
| 50 |
$migration->sanitize(); |
| 51 |
|
| 52 |
// Save |
| 53 |
$migration_factory = MigrationFactory::getInstance(); |
| 54 |
$migration_factory->addMigration($migration); |
| 55 |
|
| 56 |
|
| 57 |
// Update all the scheduled migrations according to our migrations |
| 58 |
if (CommonFunctions::isPremiumVersion()) { |
| 59 |
$all_migrations = $migration_factory->getAllMigrations(); |
| 60 |
$schedule_factory = ScheduleFactory::getInstance(); |
| 61 |
$schedule_factory->synchronizeWithMigrations($all_migrations); |
| 62 |
} |
| 63 |
|
| 64 |
// Return the sanitized version |
| 65 |
echo json_encode($migration); |
| 66 |
} |
| 67 |
|
| 68 |
private function mapFromWizard(object $parameters): Migration |
| 69 |
{ |
| 70 |
$migration = new Migration(); |
| 71 |
$migration->sync_preset = "none"; |
| 72 |
|
| 73 |
$generated_name = __("Migrate from %s", "wpsynchro"); |
| 74 |
if ($parameters->migrate_type === "push") { |
| 75 |
$generated_name = __("Migrate to %s", "wpsynchro"); |
| 76 |
} |
| 77 |
$generated_name = sprintf($generated_name, $parameters->remote_server_url); |
| 78 |
|
| 79 |
$migration->name = $generated_name; |
| 80 |
$migration->site_url = $parameters->remote_server_url; |
| 81 |
$migration->access_key = $parameters->remote_server_key; |
| 82 |
$migration->sync_database = $parameters->migrate_db; |
| 83 |
$migration->searchreplaces_regenerate = true; |
| 84 |
$migration->sync_files = $parameters->migrate_files; |
| 85 |
$migration->files_ask_user_for_confirm = true; |
| 86 |
$migration->type = $parameters->migrate_type; |
| 87 |
$migration->db_make_backup = $parameters->backup_db; |
| 88 |
|
| 89 |
// If migrate files is done, we add the web root as a location |
| 90 |
if ($migration->sync_files) { |
| 91 |
$loc = new Location(); |
| 92 |
$loc->base = "webroot"; |
| 93 |
$loc->path = "/"; |
| 94 |
$migration->file_locations[] = $loc; |
| 95 |
} |
| 96 |
|
| 97 |
return $migration; |
| 98 |
} |
| 99 |
} |
| 100 |
|