-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathClient.php
More file actions
405 lines (348 loc) · 10.7 KB
/
Client.php
File metadata and controls
405 lines (348 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
namespace Plausible\Analytics\WP;
use Exception;
use Plausible\Analytics\WP\Capabilities as WPCapabilities;
use Plausible\Analytics\WP\Admin\Messages;
use Plausible\Analytics\WP\Client\ApiException;
use Plausible\Analytics\WP\Client\Lib\GuzzleHttp\Client as GuzzleClient;
use Plausible\Analytics\WP\Client\Api\DefaultApi;
use Plausible\Analytics\WP\Client\Configuration;
use Plausible\Analytics\WP\Client\Model\Capabilities;
use Plausible\Analytics\WP\Client\Model\CapabilitiesFeatures;
use Plausible\Analytics\WP\Client\Model\CustomPropEnableRequestBulkEnable;
use Plausible\Analytics\WP\Client\Model\FunnelCreateRequest;
use Plausible\Analytics\WP\Client\Model\GoalCreateRequestBulkGetOrCreate;
use Plausible\Analytics\WP\Client\Model\GoalListResponse;
use Plausible\Analytics\WP\Client\Model\PaymentRequiredError;
use Plausible\Analytics\WP\Client\Model\SharedLink;
use Plausible\Analytics\WP\Client\Model\UnauthorizedError;
use Plausible\Analytics\WP\Client\Model\UnprocessableEntityError;
/**
* This class acts as middleware between our OpenAPI generated API client and our WP plugin, and takes care of setting
* the required configuration, so we can use the Client in a unified manner.
*/
class Client {
/**
* @var DefaultApi $api_instance
*/
private $api_instance;
/**
* Setup basic authorization, basic_auth.
*
* @param string $token Allows to specify the token, e.g. when it's not stored in the DB yet.
*/
public function __construct( $token = '' ) {
$config = Configuration::getDefaultConfiguration()->setUsername( 'WordPress' )->setPassword(
$token
)->setHost( Helpers::get_hosted_domain_url() );
$this->api_instance = new DefaultApi( new GuzzleClient(), $config );
}
/**
* Validates the Plugin Token (password) set in the current instance and caches the state to a transient valid for 1 day.
*
* @return bool
* @throws ApiException
*/
public function validate_api_token() {
if ( $this->is_api_token_valid() ) {
return true; // @codeCoverageIgnore
}
$features = $this->get_features();
if ( ! $features instanceof CapabilitiesFeatures ) {
return false; // @codeCoverageIgnore
}
$data_domain = $this->get_data_domain();
$token = $this->api_instance->getConfig()->getPassword();
$is_valid = str_contains( $token, 'plausible-plugin' ) && ! empty( $features->getGoals() ) && $data_domain === Helpers::get_domain();
/**
* Don't cache invalid API tokens.
*/
if ( $is_valid ) {
set_transient( 'plausible_analytics_valid_token', [ $token => true ], 86400 ); // @codeCoverageIgnore
$this->update_capabilities( $token ); // @codeCoverageIgnore
}
return $is_valid;
}
/**
* Is currently stored token valid?
*
* @return bool
*/
public function is_api_token_valid() {
$token = $this->api_instance->getConfig()->getPassword();
$valid_tokens = get_transient( 'plausible_analytics_valid_token' );
return isset( $valid_tokens[ $token ] ) && $valid_tokens[ $token ] === true;
}
/**
* Retrieve Features from Capabilities object.
*
* @return false|Client\Model\CapabilitiesFeatures
*/
public function get_features() {
$capabilities = $this->get_capabilities();
if ( $capabilities instanceof Capabilities ) {
return $capabilities->getFeatures();
}
return false; // @codeCoverageIgnore
}
/**
* Retrieve all capabilities assigned to configured Plugin Token.
*
* @return bool|Client\Model\Capabilities
*
* @codeCoverageIgnore
*/
private function get_capabilities() {
try {
return $this->api_instance->plausibleWebPluginsAPIControllersCapabilitiesIndex();
} catch ( \Exception $e ) {
return false;
}
}
/**
* Retrieve Data Domain property from Capabilities object.
*
* @return false|string
*
* @codeCoverageIgnore
*/
private function get_data_domain() {
$capabilities = $this->get_capabilities();
if ( $capabilities instanceof Capabilities ) {
return $capabilities->getDataDomain();
}
return false;
}
/**
* Stores the capabilities for the currently entered API token in the DB for later use.
*
* @param $token
*
* @return false|array
*
* @codeCoverageIgnore
*/
private function update_capabilities( $token = '' ) {
$client_factory = new ClientFactory( $token );
/** @var Client $client */
$client = $client_factory->build();
if ( ! $client instanceof Client ) {
return false;
}
/** @var Client\Model\CapabilitiesFeatures $features */
$features = $client->get_features();
if ( ! $features ) {
return false;
}
$caps = [
WPCapabilities::FUNNELS => $features->getFunnels(),
WPCapabilities::GOALS => $features->getGoals(),
WPCapabilities::PROPS => $features->getProps(),
WPCapabilities::REVENUE => $features->getRevenueGoals(),
WPCapabilities::STATS => $features->getStatsApi(),
];
update_option( 'plausible_analytics_api_token_caps', $caps );
return $caps;
}
/**
* Retrieve the configured Tracker ID and stores it in the options table.
*
* @return string
*
* @codeCoverageIgnore Because we don't want to test WordPress core functionality.
*/
public function get_tracker_id() {
$id = get_option( 'plausible_analytics_tracker_id' );
if ( ! $id ) {
$tracker_configuration = $this->get_configuration();
if ( ! $tracker_configuration instanceof Client\Model\TrackerScriptConfigurationTrackerScriptConfiguration ) {
return '';
}
$id = $tracker_configuration->getId();
update_option( 'plausible_analytics_tracker_id', $id );
}
return $id;
}
/**
* Retrieve the configured Tracker Script Configuration.
*
* @return false|Client\Model\TrackerScriptConfigurationTrackerScriptConfiguration
*
* @codeCoverageIgnore Because we don't want to test the API's response.
*/
private function get_configuration() {
try {
$configuration = $this->api_instance->plausibleWebPluginsAPIControllersTrackerScriptConfigurationGet();
return $configuration->getTrackerScriptConfiguration();
} catch ( \Exception $e ) {
return false;
}
}
/**
* Update the configured Tracker Script Configuration.
*
* @param \Plausible\Analytics\WP\Client\Model\TrackerScriptConfigurationUpdateRequest $tracker_script_config_update_request
*
* @codeCoverageIgnore
*/
public function update_tracker_script_configuration( $tracker_script_config_update_request ) {
try {
$this->api_instance->plausibleWebPluginsAPIControllersTrackerScriptConfigurationUpdate(
$tracker_script_config_update_request
);
} catch ( Exception $e ) {
$this->send_json_error(
$e,
__(
'Something went wrong while updating tracker script configuration: %s',
'plausible-analytics'
)
);
}
}
/**
* @param Exception $e
* @param string $error_message The human-readable part of the error message, requires a %s at the end!
*
* @return void
*
* @codeCoverageIgnore
*/
private function send_json_error( $e, $error_message ) {
if ( ! wp_doing_ajax() ) {
return;
}
$code = $e->getCode();
// Any error codes outside the 4xx range should show a generic error.
if ( $code <= 399 || $code >= 500 ) {
Messages::set_error( __( 'Something went wrong, try again later.', 'plausible-analytics' ) );
wp_send_json_error( null, $code );
}
$message = $e->getMessage();
$response_body = $e->getResponseBody();
if ( $response_body !== null ) {
$response_json = json_decode( $response_body );
if ( ! empty( $response_json->errors ) ) {
$message = '';
foreach ( $response_json->errors as $error_no => $error ) {
$message .= $error->detail;
if ( $error_no + 1 === count( $response_json->errors ) ) {
$message .= '.';
} elseif ( count( $response_json->errors ) > 1 ) {
$message .= ', ';
}
}
}
}
Messages::set_error( sprintf( $error_message, $message ) );
$caps = $this->update_capabilities();
wp_send_json_error( [ 'capabilities' => $caps ], $code );
}
/**
* Create Shared Link in Plausible Dashboard.
*
* @return void
*/
public function create_shared_link() {
$shared_link = (object) [];
$result = (object) [];
try {
$result = $this->bulk_create_shared_links();
// @codeCoverageIgnoreStart
} catch ( Exception $e ) {
$this->send_json_error( $e, __( 'Something went wrong while creating Shared Link: %s', 'plausible-analytics' ) );
// @codeCoverageIgnoreEnd
}
if ( $result instanceof SharedLink ) {
$shared_link = $result->getSharedLink();
}
if ( ! empty( $shared_link->getHref() ) ) {
Helpers::update_setting( 'shared_link', $shared_link->getHref() );
}
}
/**
* @return SharedLink|UnauthorizedError|UnprocessableEntityError
* @throws ApiException
*
* @codeCoverageIgnore
*/
public function bulk_create_shared_links() {
return $this->api_instance->plausibleWebPluginsAPIControllersSharedLinksCreate(
[ 'shared_link' => [ 'name' => 'WordPress - Shared Dashboard', 'password_protected' => false ] ]
);
}
/**
* Allows creating Custom Event Goals in bulk.
*
* @param GoalCreateRequestBulkGetOrCreate $goals
*
* @return GoalListResponse|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void
*
* @codeCoverageIgnore
*/
public function create_goals( $goals ) {
try {
return $this->api_instance->goalGetOrCreate( $goals );
} catch ( Exception $e ) {
$this->send_json_error( $e, __( 'Something went wrong while creating Custom Event Goal: %s', 'plausible-analytics' ) );
}
}
/**
* Allows creating Funnels in bulk.
*
* @param FunnelCreateRequest $funnel
*
* @return Client\Model\Funnel|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void
*
* @codeCoverageIgnore
*/
public function create_funnel( $funnel ) {
try {
return $this->api_instance->funnelGetOrCreate( $funnel );
} catch ( Exception $e ) {
$this->send_json_error( $e, __( 'Something went wrong while creating Funnel: %s', 'plausible-analytics' ) );
}
}
/**
* Delete a Custom Event Goal by ID.
*
* @param int $id
*
* @codeCoverageIgnore
*/
public function delete_goal( $id ) {
try {
$this->api_instance->plausibleWebPluginsAPIControllersGoalsDelete( $id );
} catch ( Exception $e ) {
$this->send_json_error(
$e,
__(
'Something went wrong while deleting a Custom Event Goal: %s',
'plausible-analytics'
)
);
}
}
/**
* Enable (or get) a custom property.
*
* @param CustomPropEnableRequestBulkEnable $enable_request
*
* @throws PaymentRequiredError|UnauthorizedError|UnprocessableEntityError
*
* @codeCoverageIgnore
*/
public function enable_custom_property( $enable_request ) {
try {
$this->api_instance->customPropGetOrEnable( $enable_request );
} catch ( Exception $e ) {
$this->send_json_error(
$e,
__(
'Something went wrong while enabling Pageview Properties: %s',
'plausible-analytics'
)
);
}
}
}