FormsRoutes.php
9 months ago
HostingRoutes.php
3 months ago
IntegrationsRoutes.php
2 weeks ago
ReachRoutes.php
1 month ago
Routes.php
5 months ago
ReachRoutes.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Api\Routes; |
| 4 | |
| 5 | use Hostinger\Reach\Api\ApiKeyManager; |
| 6 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 7 | use Hostinger\Reach\Repositories\ContactListRepository; |
| 8 | use WP_REST_Request; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class ReachRoutes extends Routes { |
| 15 | private ReachApiHandler $handler; |
| 16 | private ApiKeyManager $api_key_manager; |
| 17 | |
| 18 | private ContactListRepository $contact_list_repository; |
| 19 | |
| 20 | public function __construct( ReachApiHandler $handler, ApiKeyManager $api_key_manager, ContactListRepository $contact_list_repository ) { |
| 21 | $this->handler = $handler; |
| 22 | $this->api_key_manager = $api_key_manager; |
| 23 | $this->contact_list_repository = $contact_list_repository; |
| 24 | } |
| 25 | |
| 26 | public function register_routes(): void { |
| 27 | register_rest_route( |
| 28 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 29 | 'contact', |
| 30 | array( |
| 31 | 'methods' => 'POST', |
| 32 | 'callback' => array( $this->handler, 'post_contact_handler' ), |
| 33 | 'permission_callback' => '__return_true', |
| 34 | 'args' => array( |
| 35 | 'id' => array( |
| 36 | 'required' => true, |
| 37 | 'type' => 'string', |
| 38 | ), |
| 39 | 'group' => array( |
| 40 | 'required' => false, |
| 41 | 'default' => HOSTINGER_REACH_DEFAULT_CONTACT_LIST, |
| 42 | 'type' => 'string', |
| 43 | 'validate_callback' => array( $this, 'is_valid_contact_list' ), |
| 44 | ), |
| 45 | 'tags' => array( |
| 46 | 'required' => false, |
| 47 | 'default' => HOSTINGER_REACH_DEFAULT_CONTACT_LIST, |
| 48 | 'type' => 'string', |
| 49 | ), |
| 50 | 'email' => array( |
| 51 | 'required' => true, |
| 52 | 'type' => 'string', |
| 53 | 'validate_callback' => function ( $param ) { |
| 54 | return filter_var( $param, FILTER_VALIDATE_EMAIL ); |
| 55 | }, |
| 56 | ), |
| 57 | 'name' => array( |
| 58 | 'required' => false, |
| 59 | 'default' => '', |
| 60 | 'type' => 'string', |
| 61 | ), |
| 62 | 'surname' => array( |
| 63 | 'required' => false, |
| 64 | 'default' => '', |
| 65 | 'type' => 'string', |
| 66 | ), |
| 67 | 'metadata' => array( |
| 68 | 'required' => false, |
| 69 | 'type' => 'object', |
| 70 | 'properties' => array( |
| 71 | 'plugin' => array( |
| 72 | 'type' => 'string', |
| 73 | ), |
| 74 | ), |
| 75 | ), |
| 76 | ), |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | register_rest_route( |
| 81 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 82 | 'token', |
| 83 | array( |
| 84 | 'methods' => 'POST', |
| 85 | 'callback' => array( $this->handler, 'post_token_handler' ), |
| 86 | 'permission_callback' => function ( WP_REST_Request $request ) { |
| 87 | $csrf = $request->get_param( 'csrf_field' ); |
| 88 | |
| 89 | return $csrf && $this->api_key_manager->validate_csrf( $csrf ); |
| 90 | }, |
| 91 | 'args' => array( |
| 92 | 'csrf_field' => array( |
| 93 | 'required' => true, |
| 94 | 'type' => 'string', |
| 95 | ), |
| 96 | 'token' => array( |
| 97 | 'required' => true, |
| 98 | 'type' => 'string', |
| 99 | ), |
| 100 | ), |
| 101 | ) |
| 102 | ); |
| 103 | |
| 104 | register_rest_route( |
| 105 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 106 | 'connect', |
| 107 | array( |
| 108 | 'methods' => 'POST', |
| 109 | 'callback' => array( $this->handler, 'post_connect_handler' ), |
| 110 | 'permission_callback' => array( $this, 'permission_check' ), |
| 111 | ) |
| 112 | ); |
| 113 | |
| 114 | register_rest_route( |
| 115 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 116 | 'generate-auth-url', |
| 117 | array( |
| 118 | 'methods' => 'POST', |
| 119 | 'callback' => array( $this->handler, 'post_generate_auth_url' ), |
| 120 | 'permission_callback' => array( $this, 'permission_check' ), |
| 121 | ) |
| 122 | ); |
| 123 | |
| 124 | register_rest_route( |
| 125 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 126 | 'overview', |
| 127 | array( |
| 128 | 'methods' => 'GET', |
| 129 | 'callback' => array( $this->handler, 'get_overview_handler' ), |
| 130 | 'permission_callback' => array( $this, 'permission_check' ), |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | register_rest_route( |
| 135 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 136 | 'tags', |
| 137 | array( |
| 138 | array( |
| 139 | 'methods' => 'GET', |
| 140 | 'callback' => array( $this->handler, 'get_tags_handler' ), |
| 141 | 'permission_callback' => array( $this, 'permission_check' ), |
| 142 | ), |
| 143 | array( |
| 144 | 'methods' => 'POST', |
| 145 | 'callback' => array( $this->handler, 'post_tags_handler' ), |
| 146 | 'permission_callback' => array( $this, 'permission_check' ), |
| 147 | 'args' => array( |
| 148 | 'names' => array( |
| 149 | 'type' => 'array', |
| 150 | 'required' => true, |
| 151 | 'default' => array( HOSTINGER_REACH_DEFAULT_CONTACT_LIST ), |
| 152 | 'items' => array( |
| 153 | 'type' => 'string', |
| 154 | ), |
| 155 | ), |
| 156 | ), |
| 157 | ), |
| 158 | |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | public function is_valid_contact_list( string $param ): bool { |
| 164 | return $param === '' || $this->contact_list_repository->exists( $param ); |
| 165 | } |
| 166 | } |
| 167 |