| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Rest_Api\Endpoints; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Db_Queries\Execution_Builder; |
| 7 |
use Jet_Form_Builder\Migrations\Migration_Exception; |
| 8 |
use Jet_Form_Builder\Migrations\Migration_Incomplete_Exception; |
| 9 |
use Jet_Form_Builder\Migrations\Migrator; |
| 10 |
use JFB_Components\Rest_Api\Rest_Api_Endpoint_Base; |
| 11 |
|
| 12 |
// If this file is called directly, abort. |
| 13 |
if ( ! defined( 'WPINC' ) ) { |
| 14 |
die; |
| 15 |
} |
| 16 |
|
| 17 |
class Install_Migrations_Endpoint extends Rest_Api_Endpoint_Base { |
| 18 |
|
| 19 |
/** |
| 20 |
* @return mixed |
| 21 |
*/ |
| 22 |
public static function get_rest_base() { |
| 23 |
return 'migrations/install'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @return mixed |
| 28 |
*/ |
| 29 |
public static function get_methods() { |
| 30 |
return \WP_REST_Server::CREATABLE; |
| 31 |
} |
| 32 |
|
| 33 |
public function check_permission(): bool { |
| 34 |
return current_user_can( 'manage_options' ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param \WP_REST_Request $request |
| 39 |
* |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
public function run_callback( \WP_REST_Request $request ) { |
| 43 |
try { |
| 44 |
Execution_Builder::instance()->transaction_start(); |
| 45 |
|
| 46 |
Migrator::instance()->install(); |
| 47 |
|
| 48 |
Execution_Builder::instance()->transaction_commit(); |
| 49 |
|
| 50 |
} catch ( Migration_Incomplete_Exception $exception ) { |
| 51 |
// Not a failure: a time-boxed migration (e.g. the SSR registry import) already |
| 52 |
// committed its own partial progress and a resume cursor before throwing this — |
| 53 |
// see its own docblock. There is nothing left to roll back here; calling this |
| 54 |
// endpoint again resumes from that cursor. |
| 55 |
return new \WP_REST_Response( |
| 56 |
array( |
| 57 |
'message' => __( 'Migration is still in progress. Call this endpoint again to continue.', 'jet-form-builder' ), |
| 58 |
'complete' => false, |
| 59 |
) |
| 60 |
); |
| 61 |
} catch ( Migration_Exception $exception ) { |
| 62 |
Execution_Builder::instance()->transaction_rollback(); |
| 63 |
|
| 64 |
return new \WP_REST_Response( |
| 65 |
array( |
| 66 |
'message' => $exception->getMessage(), |
| 67 |
'version' => $exception->get_version(), |
| 68 |
), |
| 69 |
400 |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
return new \WP_REST_Response( |
| 74 |
array( |
| 75 |
'message' => __( 'Successfully installed migrations.', 'jet-form-builder' ), |
| 76 |
'complete' => true, |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|