Skip to content

Commit

Permalink
Fixed: API validation didn't show error if get_goals failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan0sz committed Feb 20, 2024
1 parent 6366313 commit 70c6296
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Plausible\Analytics\WP;

use Exception;
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;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function __construct( $token = '' ) {
/**
* Validates the API token (password) set in the current instance.
* @return bool
* @throws ApiException
*/
public function validate_api_token() {
$password = $this->api_instance->getConfig()->getPassword();
Expand All @@ -49,16 +51,39 @@ public function validate_api_token() {
}

/**
* @return Client\Model\GoalListResponse|UnauthorizedError|void
* We intentionally ignore the Exception, because this method is (currently) only used for API token validation.
* @return Client\Model\GoalListResponse|UnauthorizedError|false
* @throws ApiException
*/
public function get_goals() {
try {
return $this->api_instance->plausibleWebPluginsAPIControllersGoalsIndex( 10 );
} catch ( Exception $e ) {
$this->send_json_error(
$e,
__( 'Couldn\'t retrieve goals: %s', 'plausible-analytics' )
} catch ( \Exception $e ) {
return false;
}
}

/**
* Create Shared Link in Plausible Dashboard.
* @return void
*/
public function create_shared_link() {
$shared_link = (object) [];

try {
$result = $this->api_instance->plausibleWebPluginsAPIControllersSharedLinksCreate(
[ 'shared_link' => [ 'name' => 'WordPress - Shared Dashboard', 'password_protected' => false ] ]
);
} catch ( Exception $e ) {
$this->send_json_error( $e, __( 'Something went wrong while creating Shared Link: %s', 'plausible-analytics' ) );
}

if ( $result instanceof SharedLink ) {
$shared_link = $result->getSharedLink();
}

if ( ! empty( $shared_link->getHref() ) ) {
Helpers::update_setting( 'shared_link', $shared_link->getHref() );
}
}

Expand Down Expand Up @@ -106,30 +131,6 @@ private function send_json_error( $e, $error_message ) {
wp_send_json_error( sprintf( $error_message, $message ) );
}

/**
* Create Shared Link in Plausible Dashboard.
* @return void
*/
public function create_shared_link() {
$shared_link = (object) [];

try {
$result = $this->api_instance->plausibleWebPluginsAPIControllersSharedLinksCreate(
[ 'shared_link' => [ 'name' => 'WordPress - Shared Dashboard', 'password_protected' => false ] ]
);
} catch ( Exception $e ) {
$this->send_json_error( $e, __( 'Something went wrong while creating Shared Link: %s', 'plausible-analytics' ) );
}

if ( $result instanceof SharedLink ) {
$shared_link = $result->getSharedLink();
}

if ( ! empty( $shared_link->getHref() ) ) {
Helpers::update_setting( 'shared_link', $shared_link->getHref() );
}
}

/**
* Allows creating Custom Event Goals in bulk.
*
Expand Down

0 comments on commit 70c6296

Please sign in to comment.