diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 0b417f1..787741f 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -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 ] ] ); } } diff --git a/src/Admin/Settings/Page.php b/src/Admin/Settings/Page.php index f87c60a..416d336 100644 --- a/src/Admin/Settings/Page.php +++ b/src/Admin/Settings/Page.php @@ -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' ), diff --git a/src/Client.php b/src/Client.php index 8a70453..4b1f341 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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; @@ -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. * @@ -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 */ @@ -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 *