| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class for handling to get file changes for current sync |
| 5 |
* Call should already be verified by permissions callback |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPSynchro\API; |
| 9 |
|
| 10 |
use WPSynchro\Files\SyncList; |
| 11 |
use WPSynchro\Migration\Job; |
| 12 |
|
| 13 |
class StatusFileChanges extends WPSynchroService |
| 14 |
{ |
| 15 |
public function service() |
| 16 |
{ |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Get the file changes |
| 21 |
*/ |
| 22 |
private function getJobFromRequest() |
| 23 |
{ |
| 24 |
if (!isset($_REQUEST['job_id']) || strlen($_REQUEST['job_id']) == 0) { |
| 25 |
http_response_code(400); |
| 26 |
return; |
| 27 |
} |
| 28 |
if (!isset($_REQUEST['migration_id']) || strlen($_REQUEST['migration_id']) == 0) { |
| 29 |
http_response_code(400); |
| 30 |
return; |
| 31 |
} |
| 32 |
$migration_id = $_REQUEST['migration_id']; |
| 33 |
$job_id = $_REQUEST['job_id']; |
| 34 |
|
| 35 |
$job = new Job(); |
| 36 |
$job_loaded = $job->load($migration_id, $job_id); |
| 37 |
if (!$job_loaded) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
return $job; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Get the file changes |
| 46 |
*/ |
| 47 |
public function getFileChanges() |
| 48 |
{ |
| 49 |
$job = $this->getJobFromRequest(); |
| 50 |
|
| 51 |
$sync_list = new SyncList(); |
| 52 |
$sync_list->job = $job; |
| 53 |
|
| 54 |
$need_transfer = $sync_list->getFileChangesByType("add"); |
| 55 |
$files_for_delete = $sync_list->getFileChangesByType("delete"); |
| 56 |
|
| 57 |
$files_changed = [ |
| 58 |
'will_be_deleted' => $files_for_delete, |
| 59 |
'will_be_added_changed' => $need_transfer, |
| 60 |
'basepath' => $job->to_files_above_webroot_dir, |
| 61 |
]; |
| 62 |
|
| 63 |
echo json_encode($files_changed); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Accept from user of file changes |
| 68 |
*/ |
| 69 |
public function acceptFileChanges() |
| 70 |
{ |
| 71 |
$job = $this->getJobFromRequest(); |
| 72 |
|
| 73 |
// Set it as confirmed |
| 74 |
$job->files_user_confirmed_actions = true; |
| 75 |
// As the worker is paused, we just update the run lock timer, giving the JS worker thread 20 seconds to start again |
| 76 |
$job->run_lock_problem_time = time() + 20; |
| 77 |
// Boyaa! |
| 78 |
$job->save(); |
| 79 |
} |
| 80 |
} |
| 81 |
|