Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,13 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
* Create Custom Properties for Search Queries option.
*/
if ( Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) {
$caps = get_option( 'plausible_analytics_api_token_caps', [] );

foreach ( $this->custom_search_properties as $property ) {
if ( empty( $caps[ 'props' ] ) && $property === 'result_count' ) {
continue;
}

$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function __construct() {
'slug' => 'enhanced_measurements',
'type' => 'checkbox',
'value' => 'search',
'caps' => [ self::CAP_GOALS, self::CAP_PROPS ],
'caps' => [ self::CAP_GOALS ],
],
'tagged-events' => [
'label' => esc_html__( 'Custom events', 'plausible-analytics' ),
Expand Down
86 changes: 45 additions & 41 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
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;
Expand Down Expand Up @@ -58,13 +60,15 @@ public function validate_api_token() {

$data_domain = $this->get_data_domain();
$token = $this->api_instance->getConfig()->getPassword();
$is_valid = strpos( $token, 'plausible-plugin' ) !== false && ! empty( $features->getGoals() ) && $data_domain === Helpers::get_domain();
$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;
Expand Down Expand Up @@ -129,6 +133,44 @@ private function get_data_domain() {
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 = [
'funnels' => $features->getFunnels(),
'goals' => $features->getGoals(),
'props' => $features->getProps(),
'revenue' => $features->getRevenueGoals(),
'stats' => $features->getStatsApi(),
];

update_option( 'plausible_analytics_api_token_caps', $caps );

return $caps;
}

/**
* Create Shared Link in Plausible Dashboard.
*
Expand Down Expand Up @@ -217,50 +259,12 @@ private function send_json_error( $e, $error_message ) {
wp_send_json_error( [ 'capabilities' => $caps ], $code );
}

/**
* 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 = [
'funnels' => $features->getFunnels(),
'goals' => $features->getGoals(),
'props' => $features->getProps(),
'revenue' => $features->getRevenueGoals(),
'stats' => $features->getStatsApi(),
];

update_option( 'plausible_analytics_api_token_caps', $caps );

return $caps;
}

/**
* Allows creating Custom Event Goals in bulk.
*
* @param GoalCreateRequestBulkGetOrCreate $goals
*
* @return Client\Model\PaymentRequiredError|Client\Model\PlausibleWebPluginsAPIControllersGoalsCreate201Response|Client\Model\UnauthorizedError|Client\Model\UnprocessableEntityError|null
* @return GoalListResponse|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void
*
* @codeCoverageIgnore
*/
Expand All @@ -275,7 +279,7 @@ public function create_goals( $goals ) {
/**
* Allows creating Funnels in bulk.
*
* @param \Plausible\Analytics\WP\Client\Model\FunnelCreateRequest $funnel
* @param FunnelCreateRequest $funnel
*
* @return Client\Model\Funnel|PaymentRequiredError|UnauthorizedError|UnprocessableEntityError|void
*
Expand Down