install-migrations-endpoint.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Migrations\Rest_Api; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Db_Queries\Execution_Builder; |
| 8 | use Jet_Form_Builder\Migrations\Migration_Exception; |
| 9 | use Jet_Form_Builder\Migrations\Migrator; |
| 10 | use Jet_Form_Builder\Rest_Api\Rest_Api_Endpoint_Base; |
| 11 | |
| 12 | class Install_Migrations_Endpoint extends Rest_Api_Endpoint_Base { |
| 13 | |
| 14 | /** |
| 15 | * @return mixed |
| 16 | */ |
| 17 | public static function get_rest_base() { |
| 18 | return 'migrations/install'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @return mixed |
| 23 | */ |
| 24 | public static function get_methods() { |
| 25 | return \WP_REST_Server::CREATABLE; |
| 26 | } |
| 27 | |
| 28 | public function check_permission(): bool { |
| 29 | return current_user_can( 'manage_options' ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param \WP_REST_Request $request |
| 34 | * |
| 35 | * @return mixed |
| 36 | */ |
| 37 | public function run_callback( \WP_REST_Request $request ) { |
| 38 | try { |
| 39 | Execution_Builder::instance()->transaction_start(); |
| 40 | |
| 41 | Migrator::instance()->install(); |
| 42 | |
| 43 | Execution_Builder::instance()->transaction_commit(); |
| 44 | |
| 45 | } catch ( Migration_Exception $exception ) { |
| 46 | Execution_Builder::instance()->transaction_rollback(); |
| 47 | |
| 48 | return new \WP_REST_Response( |
| 49 | array( |
| 50 | 'message' => $exception->getMessage(), |
| 51 | 'version' => $exception->get_version(), |
| 52 | ), |
| 53 | 400 |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | return new \WP_REST_Response( |
| 58 | array( |
| 59 | 'message' => __( 'Successfully installed migrations.', 'jet-form-builder' ), |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | } |