ActionDispatcher.php
1 month ago
AmplitudeLoader.php
1 month ago
AmplitudeManager.php
1 month ago
Rest.php
1 month ago
Rest.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Rest API Routes |
| 5 | * |
| 6 | * @package HostingerAffiliatePlugin |
| 7 | */ |
| 8 | |
| 9 | namespace Hostinger\Amplitude; |
| 10 | |
| 11 | use Hostinger\WpHelper\Utils as Helper; |
| 12 | use Hostinger\WpHelper\Requests\Client; |
| 13 | use Hostinger\WpHelper\Config; |
| 14 | use Hostinger\WpHelper\Constants; |
| 15 | |
| 16 | /** |
| 17 | * Avoid possibility to get file accessed directly |
| 18 | */ |
| 19 | if (! defined('ABSPATH')) { |
| 20 | die; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Class for handling Rest Api Routes |
| 25 | */ |
| 26 | class Rest |
| 27 | { |
| 28 | public const REST_NAMESPACE = 'hostinger-amplitude/v1'; |
| 29 | |
| 30 | /** |
| 31 | * @var Helper |
| 32 | */ |
| 33 | private Helper $helper; |
| 34 | |
| 35 | /** |
| 36 | * @var Config |
| 37 | */ |
| 38 | private Config $configHandler; |
| 39 | |
| 40 | /** |
| 41 | * @var Client |
| 42 | */ |
| 43 | private Client $client; |
| 44 | |
| 45 | public function __construct() |
| 46 | { |
| 47 | $this->helper = new Helper(); |
| 48 | $this->configHandler = new Config(); |
| 49 | $this->client = new Client( |
| 50 | $this->configHandler->getConfigValue('base_rest_uri', Constants::HOSTINGER_REST_URI), |
| 51 | array( |
| 52 | Config::TOKEN_HEADER => $this->helper::getApiToken(), |
| 53 | Config::DOMAIN_HEADER => $this->helper->getHostInfo() |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Init rest routes |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function init(): void |
| 64 | { |
| 65 | add_action('rest_api_init', [ $this, 'registerRoutes' ]); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return void |
| 70 | */ |
| 71 | public function registerRoutes(): void |
| 72 | { |
| 73 | $this->registerAmplitudeRoute(); |
| 74 | $this->registerExperimentsRoute(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return void |
| 79 | */ |
| 80 | public function registerAmplitudeRoute(): void |
| 81 | { |
| 82 | register_rest_route( |
| 83 | self::REST_NAMESPACE, |
| 84 | 'hostinger-amplitude-event', |
| 85 | array( |
| 86 | 'methods' => 'POST', |
| 87 | 'callback' => array( $this, 'sendAmplitudeEvent' ), |
| 88 | 'permission_callback' => array( $this, 'permissionCheck' ), |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | public function registerExperimentsRoute(): void |
| 94 | { |
| 95 | register_rest_route( |
| 96 | self::REST_NAMESPACE, |
| 97 | 'hostinger-amplitude-experiments', |
| 98 | array( |
| 99 | 'methods' => 'GET', |
| 100 | 'callback' => array( $this, 'getExperiments' ), |
| 101 | 'permission_callback' => array( $this, 'permissionCheck' ), |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return \WP_REST_Response |
| 108 | */ |
| 109 | public function getExperiments(): \WP_REST_Response |
| 110 | { |
| 111 | $data = array(); |
| 112 | |
| 113 | $response = new \WP_REST_Response(); |
| 114 | |
| 115 | try { |
| 116 | $response->set_status(\WP_Http::OK); |
| 117 | |
| 118 | $request = $this->client->get('/v3/wordpress/amplitude/experiments', array( 'domain' => $this->helper->getHostInfo() )); |
| 119 | |
| 120 | $data = $request; |
| 121 | |
| 122 | if (!empty($request['body'])) { |
| 123 | $json = json_decode($request['body'], true); |
| 124 | |
| 125 | if (!empty($json['data'])) { |
| 126 | $data = array( |
| 127 | 'status' => 'success', |
| 128 | 'data' => $json['data'] |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 | } catch (\Exception $exception) { |
| 133 | $response->set_status(\WP_Http::BAD_REQUEST); |
| 134 | |
| 135 | $this->helper->errorLog('Error sending request: ' . $exception->getMessage()); |
| 136 | |
| 137 | $data = array( |
| 138 | 'status' => 'error', |
| 139 | 'message' => $exception->getMessage() |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | $response->set_data($data); |
| 144 | |
| 145 | $response->set_headers(array( 'Cache-Control' => 'no-cache' )); |
| 146 | |
| 147 | return $response; |
| 148 | } |
| 149 | |
| 150 | public function sendAmplitudeEvent($request) |
| 151 | { |
| 152 | $params = $request->get_param('params') ?: []; |
| 153 | $headers = $request->get_headers() ?: []; |
| 154 | |
| 155 | $amplitudeManager = new AmplitudeManager($this->helper, $this->configHandler, $this->client); |
| 156 | $status = $amplitudeManager->sendRequest($amplitudeManager::AMPLITUDE_ENDPOINT, $params, $headers); |
| 157 | |
| 158 | $response = new \WP_REST_Response([ 'status' => $status ]); |
| 159 | $response->set_headers([ 'Cache-Control' => 'no-cache' ]); |
| 160 | $response->set_status(\WP_Http::OK); |
| 161 | |
| 162 | return $response; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @param WP_REST_Request $request WordPress rest request. |
| 167 | * |
| 168 | * @return bool |
| 169 | */ |
| 170 | public function permissionCheck($request): bool |
| 171 | { |
| 172 | if (empty(is_user_logged_in())) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | // Implement custom capabilities when needed. |
| 177 | return current_user_can('manage_options'); |
| 178 | } |
| 179 | } |
| 180 |