editor-fetch-endpoint.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Active_Campaign\Rest_Api; |
| 5 | |
| 6 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 7 | use JFB_Modules\Active_Campaign\Api\Retrieve_Custom_Fields_Action; |
| 8 | use JFB_Modules\Active_Campaign\Api\Retrieve_Lists_Action; |
| 9 | use JFB_Components\Rest_Api\Rest_Api_Endpoint_Base; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class Editor_Fetch_Endpoint extends Rest_Api_Endpoint_Base { |
| 17 | |
| 18 | public static function get_rest_base() { |
| 19 | return 'activecampaign/editor'; |
| 20 | } |
| 21 | |
| 22 | public static function get_methods() { |
| 23 | return \WP_REST_Server::READABLE; |
| 24 | } |
| 25 | |
| 26 | public function check_permission(): bool { |
| 27 | return current_user_can( 'manage_options' ); |
| 28 | } |
| 29 | |
| 30 | public function run_callback( \WP_REST_Request $request ) { |
| 31 | $token = $request->get_header( 'API-TOKEN' ); |
| 32 | $url = $request->get_header( 'API-URL' ); |
| 33 | |
| 34 | try { |
| 35 | /** @var Retrieve_Custom_Fields_Action $fields */ |
| 36 | $fields = ( new Retrieve_Custom_Fields_Action() ) |
| 37 | ->set_base( $url ) |
| 38 | ->set_token( $token ) |
| 39 | ->request() |
| 40 | ->check_response_code() |
| 41 | ->response_body_as_array(); |
| 42 | |
| 43 | } catch ( Gateway_Exception $exception ) { |
| 44 | return new \WP_REST_Response( |
| 45 | array( |
| 46 | 'action' => Retrieve_Custom_Fields_Action::class, |
| 47 | 'message' => $exception->getMessage(), |
| 48 | 'data' => $exception->get_additional(), |
| 49 | ), |
| 50 | 400 |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | /** @var Retrieve_Lists_Action $lists */ |
| 56 | $lists = ( new Retrieve_Lists_Action() ) |
| 57 | ->set_base( $url ) |
| 58 | ->set_token( $token ) |
| 59 | ->request() |
| 60 | ->check_response_code() |
| 61 | ->response_body_as_array(); |
| 62 | |
| 63 | } catch ( Gateway_Exception $exception ) { |
| 64 | return new \WP_REST_Response( |
| 65 | array( |
| 66 | 'action' => Retrieve_Lists_Action::class, |
| 67 | 'message' => $exception->getMessage(), |
| 68 | 'data' => $exception->get_additional(), |
| 69 | ), |
| 70 | 400 |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | return new \WP_REST_Response( |
| 75 | array( |
| 76 | 'fields' => array_merge( |
| 77 | array( |
| 78 | array( |
| 79 | 'value' => 'email', |
| 80 | 'label' => __( 'Email', 'jet-form-builder' ), |
| 81 | 'required' => true, |
| 82 | ), |
| 83 | array( |
| 84 | 'value' => 'firstName', |
| 85 | 'label' => __( 'First Name', 'jet-form-builder' ), |
| 86 | ), |
| 87 | array( |
| 88 | 'value' => 'lastName', |
| 89 | 'label' => __( 'Last Name', 'jet-form-builder' ), |
| 90 | ), |
| 91 | array( |
| 92 | 'value' => 'phone', |
| 93 | 'label' => __( 'Phone', 'jet-form-builder' ), |
| 94 | ), |
| 95 | ), |
| 96 | $fields->to_array() |
| 97 | ), |
| 98 | 'lists' => $lists->to_array(), |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 |