FormsRoutes.php
8 months ago
HostingRoutes.php
3 months ago
IntegrationsRoutes.php
2 weeks ago
ReachRoutes.php
1 month ago
Routes.php
5 months ago
IntegrationsRoutes.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Api\Routes; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\IntegrationsApiHandler; |
| 6 | use Hostinger\Reach\Integrations\Integration; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | class IntegrationsRoutes extends Routes { |
| 13 | |
| 14 | private IntegrationsApiHandler $handler; |
| 15 | |
| 16 | public function __construct( IntegrationsApiHandler $handler ) { |
| 17 | $this->handler = $handler; |
| 18 | } |
| 19 | |
| 20 | public function register_routes(): void { |
| 21 | register_rest_route( |
| 22 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 23 | 'integrations', |
| 24 | array( |
| 25 | 'methods' => 'GET', |
| 26 | 'callback' => array( $this->handler, 'get_integrations_handler' ), |
| 27 | 'permission_callback' => array( $this, 'permission_check' ), |
| 28 | ) |
| 29 | ); |
| 30 | |
| 31 | register_rest_route( |
| 32 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 33 | 'status', |
| 34 | array( |
| 35 | 'methods' => 'GET', |
| 36 | 'callback' => array( $this->handler, 'get_status_handler' ), |
| 37 | 'permission_callback' => '__return_true', |
| 38 | ) |
| 39 | ); |
| 40 | |
| 41 | register_rest_route( |
| 42 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 43 | 'integrations', |
| 44 | array( |
| 45 | 'methods' => 'POST', |
| 46 | 'callback' => array( $this->handler, 'post_integrations_handler' ), |
| 47 | 'permission_callback' => array( $this, 'permission_check' ), |
| 48 | 'args' => array( |
| 49 | 'integration' => array( |
| 50 | 'required' => true, |
| 51 | 'type' => 'string', |
| 52 | 'validate_callback' => function ( $param ) { |
| 53 | return array_key_exists( $param, IntegrationsApiHandler::get_integrations() ); |
| 54 | }, |
| 55 | ), |
| 56 | Integration::INTEGRATION_IS_ACTIVE => array( |
| 57 | 'required' => true, |
| 58 | 'type' => 'boolean', |
| 59 | ), |
| 60 | ), |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | register_rest_route( |
| 65 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 66 | 'import/(?P<integration>[a-z0-9-]+)', |
| 67 | array( |
| 68 | 'methods' => 'GET', |
| 69 | 'callback' => array( $this->handler, 'get_import_handler' ), |
| 70 | 'permission_callback' => array( $this, 'permission_check' ), |
| 71 | 'args' => array( |
| 72 | 'integration' => array( |
| 73 | 'required' => true, |
| 74 | 'type' => 'string', |
| 75 | 'validate_callback' => function ( $param ) { |
| 76 | return array_key_exists( $param, IntegrationsApiHandler::get_integrations() ); |
| 77 | }, |
| 78 | ), |
| 79 | ), |
| 80 | ) |
| 81 | ); |
| 82 | |
| 83 | register_rest_route( |
| 84 | HOSTINGER_REACH_PLUGIN_REST_API_BASE, |
| 85 | 'import', |
| 86 | array( |
| 87 | 'methods' => 'POST', |
| 88 | 'callback' => array( $this->handler, 'post_import_handler' ), |
| 89 | 'permission_callback' => array( $this, 'permission_check' ), |
| 90 | 'args' => array( |
| 91 | 'integrations' => array( |
| 92 | 'required' => true, |
| 93 | 'type' => 'object', |
| 94 | 'validate_callback' => function ( $param ) { |
| 95 | foreach ( array_keys( $param ) as $integration ) { |
| 96 | if ( ! array_key_exists( $integration, IntegrationsApiHandler::get_integrations() ) ) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Validate if the value structure is an array. |
| 102 | foreach ( $param as $ids ) { |
| 103 | if ( ! is_array( $ids ) ) { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | }, |
| 110 | ), |
| 111 | ), |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 |