Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MigrateController | |
0.00% |
0 / 45 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
| connect_instawp | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| send_event | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| rest_is_authorized_admin | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Migration\RestApi; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Migration\Services\EventService; |
| 6 | use NewfoldLabs\WP\Module\Migration\Services\InstaMigrateService; |
| 7 | |
| 8 | /** |
| 9 | * Class MigrateController |
| 10 | */ |
| 11 | class MigrateController { |
| 12 | |
| 13 | /** |
| 14 | * REST namespace |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $namespace = 'newfold-migration/v1'; |
| 19 | |
| 20 | /** |
| 21 | * REST base |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $rest_base = '/migrate'; |
| 26 | |
| 27 | /** |
| 28 | * Registers rest routes for MigrateController. |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function register_routes() { |
| 33 | register_rest_route( |
| 34 | $this->namespace, |
| 35 | $this->rest_base . '/connect', |
| 36 | array( |
| 37 | array( |
| 38 | 'methods' => \WP_REST_Server::READABLE, |
| 39 | 'callback' => array( $this, 'connect_instawp' ), |
| 40 | 'permission_callback' => array( $this, 'rest_is_authorized_admin' ), |
| 41 | ), |
| 42 | ) |
| 43 | ); |
| 44 | |
| 45 | register_rest_route( |
| 46 | $this->namespace, |
| 47 | $this->rest_base . '/events', |
| 48 | array( |
| 49 | array( |
| 50 | 'methods' => \WP_REST_Server::CREATABLE, |
| 51 | 'callback' => array( $this, 'send_event' ), |
| 52 | 'permission_callback' => array( $this, 'rest_is_authorized_admin' ), |
| 53 | 'args' => array( |
| 54 | 'key' => array( |
| 55 | 'required' => true, |
| 56 | 'type' => 'string', |
| 57 | ), |
| 58 | 'data' => array( |
| 59 | 'required' => true, |
| 60 | 'type' => 'object', |
| 61 | ), |
| 62 | ), |
| 63 | ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Initiates the connnection with instawp plugin |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | public function connect_instawp() { |
| 74 | $insta_service = new InstaMigrateService(); |
| 75 | $response = $insta_service->run(); |
| 76 | |
| 77 | if ( is_wp_error( $response ) ) { |
| 78 | return $response; |
| 79 | } |
| 80 | return wp_send_json_success( $response ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sends an application-level event to the Hiive endpoint. |
| 85 | * |
| 86 | * @param \WP_REST_Request $request The REST request containing 'key' (string) and 'data' (array). |
| 87 | * |
| 88 | * @return \WP_REST_Response|\WP_Error Success response on success, WP_Error on failure. |
| 89 | */ |
| 90 | public function send_event( \WP_REST_Request $request ) { |
| 91 | $key = $request->get_param( 'key' ); |
| 92 | $data = $request->get_param( 'data' ); |
| 93 | |
| 94 | $result = EventService::send_application_event( $key, $data ); |
| 95 | |
| 96 | if ( is_wp_error( $result ) ) { |
| 97 | return $result; |
| 98 | } |
| 99 | return new \WP_REST_Response( array( 'success' => true ), 200 ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Confirm REST API caller has ADMIN user capabilities. |
| 104 | * |
| 105 | * @return boolean |
| 106 | */ |
| 107 | public static function rest_is_authorized_admin() { |
| 108 | $admin = 'manage_options'; |
| 109 | return \is_user_logged_in() && \current_user_can( $admin ); |
| 110 | } |
| 111 | } |