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