Skip to content

Commit 1140b07

Browse files
committed
Improved: store token caps in DB during validation.
1 parent 086b4ab commit 1140b07

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

src/Client.php

+45-41
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Plausible\Analytics\WP\Client\Model\Capabilities;
1212
use Plausible\Analytics\WP\Client\Model\CapabilitiesFeatures;
1313
use Plausible\Analytics\WP\Client\Model\CustomPropEnableRequestBulkEnable;
14+
use Plausible\Analytics\WP\Client\Model\FunnelCreateRequest;
1415
use Plausible\Analytics\WP\Client\Model\GoalCreateRequestBulkGetOrCreate;
16+
use Plausible\Analytics\WP\Client\Model\GoalListResponse;
1517
use Plausible\Analytics\WP\Client\Model\PaymentRequiredError;
1618
use Plausible\Analytics\WP\Client\Model\SharedLink;
1719
use Plausible\Analytics\WP\Client\Model\UnauthorizedError;
@@ -58,13 +60,15 @@ public function validate_api_token() {
5860

5961
$data_domain = $this->get_data_domain();
6062
$token = $this->api_instance->getConfig()->getPassword();
61-
$is_valid = strpos( $token, 'plausible-plugin' ) !== false && ! empty( $features->getGoals() ) && $data_domain === Helpers::get_domain();
63+
$is_valid = str_contains( $token, 'plausible-plugin' ) && ! empty( $features->getGoals() ) && $data_domain === Helpers::get_domain();
6264

6365
/**
6466
* Don't cache invalid API tokens.
6567
*/
6668
if ( $is_valid ) {
6769
set_transient( 'plausible_analytics_valid_token', [ $token => true ], 86400 ); // @codeCoverageIgnore
70+
71+
$this->update_capabilities( $token );
6872
}
6973

7074
return $is_valid;
@@ -129,6 +133,44 @@ private function get_data_domain() {
129133
return false;
130134
}
131135

136+
/**
137+
* Stores the capabilities for the currently entered API token in the DB for later use.
138+
*
139+
* @param $token
140+
*
141+
* @return false|array
142+
*
143+
* @codeCoverageIgnore
144+
*/
145+
private function update_capabilities( $token = '' ) {
146+
$client_factory = new ClientFactory( $token );
147+
/** @var Client $client */
148+
$client = $client_factory->build();
149+
150+
if ( ! $client instanceof Client ) {
151+
return false;
152+
}
153+
154+
/** @var Client\Model\CapabilitiesFeatures $features */
155+
$features = $client->get_features();
156+
157+
if ( ! $features ) {
158+
return false;
159+
}
160+
161+
$caps = [
162+
'funnels' => $features->getFunnels(),
163+
'goals' => $features->getGoals(),
164+
'props' => $features->getProps(),
165+
'revenue' => $features->getRevenueGoals(),
166+
'stats' => $features->getStatsApi(),
167+
];
168+
169+
update_option( 'plausible_analytics_api_token_caps', $caps );
170+
171+
return $caps;
172+
}
173+
132174
/**
133175
* Create Shared Link in Plausible Dashboard.
134176
*
@@ -217,50 +259,12 @@ private function send_json_error( $e, $error_message ) {
217259
wp_send_json_error( [ 'capabilities' => $caps ], $code );
218260
}
219261

220-
/**
221-
* Stores the capabilities for the currently entered API token in the DB for later use.
222-
*
223-
* @param $token
224-
*
225-
* @return false|array
226-
*
227-
* @codeCoverageIgnore
228-
*/
229-
private function update_capabilities( $token = '' ) {
230-
$client_factory = new ClientFactory( $token );
231-
/** @var Client $client */
232-
$client = $client_factory->build();
233-
234-
if ( ! $client instanceof Client ) {
235-
return false;
236-
}
237-
238-
/** @var Client\Model\CapabilitiesFeatures $features */
239-
$features = $client->get_features();
240-
241-
if ( ! $features ) {
242-
return false;
243-
}
244-
245-
$caps = [
246-
'funnels' => $features->getFunnels(),
247-
'goals' => $features->getGoals(),
248-
'props' => $features->getProps(),
249-
'revenue' => $features->getRevenueGoals(),
250-
'stats' => $features->getStatsApi(),
251-
];
252-
253-
update_option( 'plausible_analytics_api_token_caps', $caps );
254-
255-
return $caps;
256-
}
257-
258262
/**
259263
* Allows creating Custom Event Goals in bulk.
260264
*
261265
* @param GoalCreateRequestBulkGetOrCreate $goals
262266
*
263-
* @return Client\Model\PaymentRequiredError|Client\Model\PlausibleWebPluginsAPIControllersGoalsCreate201Response|Client\Model\UnauthorizedError|Client\Model\UnprocessableEntityError|null
267+
* @return GoalListResponse|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void
264268
*
265269
* @codeCoverageIgnore
266270
*/
@@ -275,7 +279,7 @@ public function create_goals( $goals ) {
275279
/**
276280
* Allows creating Funnels in bulk.
277281
*
278-
* @param \Plausible\Analytics\WP\Client\Model\FunnelCreateRequest $funnel
282+
* @param FunnelCreateRequest $funnel
279283
*
280284
* @return Client\Model\Funnel|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void
281285
*

0 commit comments

Comments
 (0)