Skip to content

Commit 47a835a

Browse files
authored
Merge pull request #253 from plausible/fix_search_queries
Added: Search Queries can now be enabled on Growth plans
2 parents f91546f + fbc934c commit 47a835a

File tree

3 files changed

+52
-42
lines changed

3 files changed

+52
-42
lines changed

src/Admin/Provisioning.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
360360
* Create Custom Properties for Search Queries option.
361361
*/
362362
if ( Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) {
363+
$caps = get_option( 'plausible_analytics_api_token_caps', [] );
364+
363365
foreach ( $this->custom_search_properties as $property ) {
366+
if ( empty( $caps[ 'props' ] ) && $property === 'result_count' ) {
367+
continue;
368+
}
369+
364370
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
365371
}
366372
}

src/Admin/Settings/Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function __construct() {
173173
'slug' => 'enhanced_measurements',
174174
'type' => 'checkbox',
175175
'value' => 'search',
176-
'caps' => [ self::CAP_GOALS, self::CAP_PROPS ],
176+
'caps' => [ self::CAP_GOALS ],
177177
],
178178
'tagged-events' => [
179179
'label' => esc_html__( 'Custom events', 'plausible-analytics' ),

src/Client.php

Lines changed: 45 additions & 41 deletions
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 ); // @codeCoverageIgnore
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)