ApiHandler.php
2 weeks ago
FormsApiHandler.php
4 months ago
HostingApiHandler.php
2 months ago
IntegrationsApiHandler.php
2 months ago
ReachApiHandler.php
2 weeks ago
FormsApiHandler.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Api\Handlers; |
| 4 | |
| 5 | use Hostinger\Reach\Functions; |
| 6 | use Hostinger\Reach\Repositories\ContactListRepository; |
| 7 | use Hostinger\Reach\Repositories\FormRepository; |
| 8 | use WP_REST_Request; |
| 9 | use WP_REST_Response; |
| 10 | use Exception; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class FormsApiHandler extends ApiHandler { |
| 17 | |
| 18 | public const FORMS_API_HANDLER_LAST_UPDATE = 'hostinger_reach_forms_api_handler_last_update'; |
| 19 | |
| 20 | private ContactListRepository $contact_list_repository; |
| 21 | private FormRepository $form_repository; |
| 22 | |
| 23 | public function __construct( Functions $functions, ContactListRepository $contact_list_repository, FormRepository $form_repository ) { |
| 24 | parent::__construct( $functions ); |
| 25 | $this->contact_list_repository = $contact_list_repository; |
| 26 | $this->form_repository = $form_repository; |
| 27 | } |
| 28 | |
| 29 | public function get_contact_lists_handler(): WP_REST_Response { |
| 30 | $contact_lists = $this->contact_list_repository->all(); |
| 31 | |
| 32 | return $this->handle_response( |
| 33 | array( |
| 34 | 'response' => array( |
| 35 | 'code' => 200, |
| 36 | ), |
| 37 | 'body' => wp_json_encode( $contact_lists ), |
| 38 | ) |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | public function get_forms_handler( WP_REST_Request $request ): WP_REST_Response { |
| 43 | $contact_list_id = $request->get_param( 'contact_list_id' ); |
| 44 | $type = $request->get_param( 'type' ); |
| 45 | $args = array( 'type' => $type ); |
| 46 | if ( $contact_list_id ) { |
| 47 | $args['contact_list_id'] = $contact_list_id; |
| 48 | } |
| 49 | $forms = apply_filters( 'hostinger_reach_forms', array(), $args ); |
| 50 | |
| 51 | if ( ! empty( $forms ) ) { |
| 52 | update_option( Functions::HOSTINGER_REACH_HAS_USER_ACTION, true ); |
| 53 | update_option( Functions::HOSTINGER_REACH_HAS_FORMS, true ); |
| 54 | } else { |
| 55 | update_option( Functions::HOSTINGER_REACH_HAS_FORMS, false ); |
| 56 | } |
| 57 | |
| 58 | return $this->handle_response( |
| 59 | array( |
| 60 | 'response' => array( |
| 61 | 'code' => 200, |
| 62 | ), |
| 63 | 'body' => wp_json_encode( $forms ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | public function post_forms_handler( WP_REST_Request $request ): WP_REST_Response { |
| 69 | $form_id = $request->get_param( 'form_id' ); |
| 70 | $type = $request->get_param( 'type' ); |
| 71 | $is_active = (int) $request->get_param( 'is_active' ); |
| 72 | |
| 73 | try { |
| 74 | $is_updated = apply_filters( |
| 75 | 'hostinger_reach_after_form_state_is_set', |
| 76 | $this->form_repository->update( |
| 77 | array( |
| 78 | 'form_id' => $form_id, |
| 79 | 'is_active' => $is_active, |
| 80 | ) |
| 81 | ), |
| 82 | $form_id, |
| 83 | $is_active, |
| 84 | $type |
| 85 | ); |
| 86 | } catch ( Exception $e ) { |
| 87 | $message = $e->getMessage(); |
| 88 | |
| 89 | return $this->handle_response( |
| 90 | array( |
| 91 | 'body' => wp_json_encode( array( 'error' => sanitize_text_field( $message ) ) ), |
| 92 | 'response' => array( |
| 93 | 'code' => 400, |
| 94 | ), |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | return $this->handle_response( |
| 100 | array( |
| 101 | 'response' => array( |
| 102 | 'code' => $is_updated ? 201 : 400, |
| 103 | ), |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 |