| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPSynchro\API; |
| 4 |
|
| 5 |
use WPSynchro\Migration\MigrationController; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class for handling service "migrate" |
| 9 |
* Call should already be verified by permissions callback |
| 10 |
* |
| 11 |
*/ |
| 12 |
class Migrate extends WPSynchroService |
| 13 |
{ |
| 14 |
public function service() |
| 15 |
{ |
| 16 |
// Extract parameters |
| 17 |
$body = $this->getRequestBody(); |
| 18 |
$parameters = json_decode($body); |
| 19 |
|
| 20 |
if (isset($parameters->migration_id)) { |
| 21 |
$migration_id = $parameters->migration_id; |
| 22 |
} else { |
| 23 |
$migration_id = ''; |
| 24 |
} |
| 25 |
if (isset($parameters->job_id)) { |
| 26 |
$job_id = $parameters->job_id; |
| 27 |
} else { |
| 28 |
$job_id = ''; |
| 29 |
} |
| 30 |
if (isset($parameters->migration_restart)) { |
| 31 |
$migration_restart = filter_var($parameters->migration_restart, FILTER_VALIDATE_BOOLEAN); |
| 32 |
} else { |
| 33 |
$migration_restart = false; |
| 34 |
} |
| 35 |
|
| 36 |
$migrate = MigrationController::getInstance(); |
| 37 |
$migrate->setup($migration_id, $job_id); |
| 38 |
|
| 39 |
// If we should restart a failed migration and try to continue |
| 40 |
if ($migration_restart) { |
| 41 |
$migrate->attemptMigrationResume(); |
| 42 |
} |
| 43 |
|
| 44 |
$sync_response = $migrate->runMigration(); |
| 45 |
|
| 46 |
if (isset($sync_response->errors) && count($sync_response->errors) > 0) { |
| 47 |
// Set that we should not continue migration from frontend JS |
| 48 |
$sync_response->should_continue = false; |
| 49 |
} else { |
| 50 |
// Set to frontend to continue migration |
| 51 |
$sync_response->should_continue = true; |
| 52 |
} |
| 53 |
|
| 54 |
if (isset($sync_response->is_completed) && $sync_response->is_completed === true) { |
| 55 |
$sync_response->should_continue = false; |
| 56 |
} |
| 57 |
|
| 58 |
echo json_encode($sync_response); |
| 59 |
} |
| 60 |
} |
| 61 |
|