FormsRoutes.php
8 months ago
HostingRoutes.php
3 months ago
IntegrationsRoutes.php
1 week ago
ReachRoutes.php
1 month ago
Routes.php
5 months ago
FormsRoutes.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Api\Routes; |
| 4 | |
| 5 | use Hostinger\Reach\Api\ApiKeyManager; |
| 6 | use Hostinger\Reach\Api\Handlers\FormsApiHandler; |
| 7 | use Hostinger\Reach\Api\Handlers\IntegrationsApiHandler; |
| 8 | use WP_REST_Request; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class FormsRoutes extends Routes { |
| 15 | private FormsApiHandler $handler; |
| 16 | private ApiKeyManager $api_key_manager; |
| 17 | |
| 18 | public function __construct( FormsApiHandler $handler, ApiKeyManager $api_key_manager ) { |
| 19 | $this->handler = $handler; |
| 20 | $this->api_key_manager = $api_key_manager; |
| 21 | } |
| 22 | |
| 23 | public function init(): void { |
| 24 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 25 | add_action( 'rest_api_init', array( $this, 'add_block_check_field_to_pages' ) ); |
| 26 | } |
| 27 | |
| 28 | public function register_routes(): void { |
| 29 | register_rest_route( |
| 30 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 31 | 'contact-lists', |
| 32 | array( |
| 33 | 'methods' => 'GET', |
| 34 | 'callback' => array( $this->handler, 'get_contact_lists_handler' ), |
| 35 | 'permission_callback' => array( $this, 'permission_check' ), |
| 36 | ) |
| 37 | ); |
| 38 | |
| 39 | register_rest_route( |
| 40 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 41 | 'forms', |
| 42 | array( |
| 43 | 'methods' => 'GET', |
| 44 | 'callback' => array( $this->handler, 'get_forms_handler' ), |
| 45 | 'permission_callback' => array( $this, 'permission_check' ), |
| 46 | 'args' => array( |
| 47 | 'contact_list_id' => array( |
| 48 | 'required' => false, |
| 49 | 'type' => 'int', |
| 50 | 'validate_callback' => function ( $param ) { |
| 51 | return is_numeric( $param ); |
| 52 | }, |
| 53 | 'sanitize_callback' => function ( $param ) { |
| 54 | return absint( $param ); |
| 55 | }, |
| 56 | ), |
| 57 | 'type' => array( |
| 58 | 'required' => false, |
| 59 | 'type' => 'string', |
| 60 | 'validate_callback' => function ( $param ) { |
| 61 | return array_key_exists( $param, IntegrationsApiHandler::get_integrations() ); |
| 62 | }, |
| 63 | ), |
| 64 | ), |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | register_rest_route( |
| 69 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 70 | 'forms', |
| 71 | array( |
| 72 | 'methods' => 'POST', |
| 73 | 'callback' => array( $this->handler, 'post_forms_handler' ), |
| 74 | 'permission_callback' => array( $this, 'permission_check' ), |
| 75 | 'args' => array( |
| 76 | 'form_id' => array( |
| 77 | 'required' => true, |
| 78 | 'type' => 'string', |
| 79 | ), |
| 80 | 'type' => array( |
| 81 | 'required' => true, |
| 82 | 'type' => 'string', |
| 83 | ), |
| 84 | 'is_active' => array( |
| 85 | 'required' => false, |
| 86 | 'type' => 'boolean', |
| 87 | 'sanitize_callback' => function ( $param ) { |
| 88 | return (int) $param; |
| 89 | }, |
| 90 | ), |
| 91 | ), |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | public function add_block_check_field_to_pages(): void { |
| 97 | register_rest_field( |
| 98 | 'page', |
| 99 | '_hostinger_reach_plugin_has_subscription_block', |
| 100 | array( |
| 101 | 'get_callback' => array( $this, 'has_subscription_block' ), |
| 102 | 'schema' => array( |
| 103 | 'type' => 'boolean', |
| 104 | ), |
| 105 | ) |
| 106 | ); |
| 107 | register_rest_field( |
| 108 | 'page', |
| 109 | '_hostinger_reach_plugin_is_elementor', |
| 110 | array( |
| 111 | 'get_callback' => array( $this, 'is_elementor' ), |
| 112 | 'schema' => array( |
| 113 | 'type' => 'boolean', |
| 114 | ), |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | public function is_elementor( array $post, string $field_name, WP_REST_Request $request ): bool { |
| 120 | if ( ! $request->has_param( 'hostinger_reach_page_query' ) ) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | return $this->handler->get_functions()->is_elementor( $post['id'] ); |
| 125 | } |
| 126 | |
| 127 | public function has_subscription_block( array $post, string $field_name, WP_REST_Request $request ): bool { |
| 128 | if ( ! $request->has_param( 'hostinger_reach_page_query' ) ) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | return $this->handler->get_functions()->has_reach_form( $post['id'] ); |
| 133 | } |
| 134 | } |
| 135 |