| 1 |
<?php |
| 2 |
namespace YayMail\Controllers; |
| 3 |
|
| 4 |
use YayMail\Abstracts\BaseController; |
| 5 |
use YayMail\Migrations\AbstractMigration; |
| 6 |
use YayMail\Models\MigrationModel; |
| 7 |
use YayMail\Utils\SingletonTrait; |
| 8 |
/** |
| 9 |
* Migration Controller |
| 10 |
* |
| 11 |
* @method static MigrationController get_instance() |
| 12 |
*/ |
| 13 |
class MigrationController extends BaseController { |
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
/** @var MigrationModel */ |
| 17 |
private $model; |
| 18 |
|
| 19 |
private function __construct() { |
| 20 |
$this->model = MigrationModel::get_instance(); |
| 21 |
$this->init_hooks(); |
| 22 |
} |
| 23 |
|
| 24 |
private function init_hooks() { |
| 25 |
register_rest_route( |
| 26 |
YAYMAIL_REST_NAMESPACE, |
| 27 |
'/migrations/get-onload-data', |
| 28 |
[ |
| 29 |
[ |
| 30 |
'methods' => \WP_REST_Server::READABLE, |
| 31 |
'callback' => [ $this, 'exec_get_onload_data' ], |
| 32 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 33 |
], |
| 34 |
] |
| 35 |
); |
| 36 |
register_rest_route( |
| 37 |
YAYMAIL_REST_NAMESPACE, |
| 38 |
'/migrations/migrate', |
| 39 |
[ |
| 40 |
[ |
| 41 |
'methods' => \WP_REST_Server::EDITABLE, |
| 42 |
'callback' => [ $this, 'exec_migrate' ], |
| 43 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 44 |
], |
| 45 |
] |
| 46 |
); |
| 47 |
register_rest_route( |
| 48 |
YAYMAIL_REST_NAMESPACE, |
| 49 |
'/migrations/reset/(?P<backup_name>[a-zA-Z0-9_-]+)', |
| 50 |
[ |
| 51 |
[ |
| 52 |
'methods' => \WP_REST_Server::EDITABLE, |
| 53 |
'callback' => [ $this, 'exec_reset' ], |
| 54 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 55 |
'args' => [ |
| 56 |
'backup_name' => [ |
| 57 |
'type' => 'string', |
| 58 |
'required' => true, |
| 59 |
], |
| 60 |
], |
| 61 |
], |
| 62 |
] |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public function exec_get_onload_data( \WP_REST_Request $request ) { |
| 67 |
return $this->exec( [ $this, 'get_onload_data' ], $request ); |
| 68 |
} |
| 69 |
public function get_onload_data( \WP_REST_Request $request ) { |
| 70 |
return $this->model->get_onload_data( $request ); |
| 71 |
} |
| 72 |
|
| 73 |
public function exec_migrate( \WP_REST_Request $request ) { |
| 74 |
return $this->exec( [ $this, 'migrate' ], $request ); |
| 75 |
} |
| 76 |
public function migrate( \WP_REST_Request $request ) { |
| 77 |
$response = $this->model->migrate(); |
| 78 |
return array_merge( [ 'success' => true ], $response ); |
| 79 |
} |
| 80 |
|
| 81 |
public function exec_reset( \WP_REST_Request $request ) { |
| 82 |
return $this->exec( [ $this, 'reset' ], $request ); |
| 83 |
} |
| 84 |
public function reset( \WP_REST_Request $request ) { |
| 85 |
$backup_name = sanitize_text_field( $request->get_param( 'backup_name' ) ); |
| 86 |
$backup_data = get_option( $backup_name, null ); |
| 87 |
|
| 88 |
if ( ! $backup_data ) { |
| 89 |
return [ |
| 90 |
'success' => false, |
| 91 |
'message' => esc_html__( 'Backup not found', 'yaymail' ), |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
$version = str_replace( AbstractMigration::BACKUP_PREFIX, '', $backup_name ); |
| 96 |
$version = str_replace( '_', '.', $version ); |
| 97 |
$backup_data['name'] = $backup_name; |
| 98 |
$backup_data['version'] = $version; |
| 99 |
$response = $this->model->reset( $backup_data ); |
| 100 |
return array_merge( [ 'success' => true ], $response ); |
| 101 |
} |
| 102 |
} |
| 103 |
|