PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.7.2
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.7.2
1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / vendor / hostinger / hostinger-wp-amplitude / src / Rest.php
hostinger-reach / vendor / hostinger / hostinger-wp-amplitude / src Last commit date
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