Skip to content

Commit

Permalink
Fix #217: first stab at ClientFactory to make sure PHP instances with…
Browse files Browse the repository at this point in the history
…out curl and/or allow_url_fopen disabled fail softly.
  • Loading branch information
Dan0sz committed Jul 16, 2024
1 parent bc99154 commit 856f64e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 31 deletions.
36 changes: 9 additions & 27 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
use Plausible\Analytics\WP\Client\Model\GoalCreateRequestCustomEvent;
use Plausible\Analytics\WP\Client\Model\GoalCreateRequestPageview;
use Plausible\Analytics\WP\Client\Model\GoalCreateRequestRevenue;
use Plausible\Analytics\WP\ClientFactory;
use Plausible\Analytics\WP\Helpers;
use Plausible\Analytics\WP\Integrations;
use Plausible\Analytics\WP\Integrations\WooCommerce;

class Provisioning {
/**
* @var ClientFactory
*/
private $client_factory;

/**
* @var Client $client
*/
Expand Down Expand Up @@ -54,19 +60,11 @@ class Provisioning {
* @codeCoverageIgnore
*/
public function __construct( $client = null ) {
/**
* cURL or allow_url_fopen ini setting is required for GuzzleHttp to function properly.
*/
if ( ! extension_loaded( 'curl' ) && ! ini_get( 'allow_url_fopen' ) ) {
add_action( 'init', [ $this, 'add_curl_error' ] );

return;
}

$this->client = $client;

if ( ! $this->client ) {
$this->client = new Client();
$this->client_factory = new ClientFactory();
$this->client = $this->client_factory->build();
}

$this->custom_event_goals = [
Expand All @@ -88,7 +86,7 @@ public function __construct( $client = null ) {
* @codeCoverageIgnore
*/
private function init() {
if ( ! $this->client->validate_api_token() ) {
if ( ! $this->client instanceof Client || ! $this->client->validate_api_token() ) {
return; // @codeCoverageIgnore
}

Expand All @@ -100,22 +98,6 @@ private function init() {
add_action( 'update_option_plausible_analytics_settings', [ $this, 'maybe_create_custom_properties' ], 11, 2 );
}

/**
* Show an error on the settings screen if cURL isn't enabled on this machine.
*
* @return void
*
* @codeCoverageIgnore
*/
public function add_curl_error() {
Messages::set_error(
__(
'cURL is not enabled on this server, which means API provisioning will not work. Please contact your hosting provider to enable the cURL module or <code>allow_url_fopen</code>.',
'plausible-analytics'
)
);
}

/**
* Create shared link when Enable Analytics Dashboard option is enabled.
*
Expand Down
12 changes: 10 additions & 2 deletions src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Exception;
use Plausible\Analytics\WP\Client;
use Plausible\Analytics\WP\ClientFactory;
use Plausible\Analytics\WP\Helpers;

class Page extends API {
Expand Down Expand Up @@ -59,6 +60,11 @@ class Page extends API {
*/
public $fields = [];

/**
* @var ClientFactory $client_factory
*/
private $client_factory;

/**
* @var Client $client
*/
Expand All @@ -77,8 +83,9 @@ public function __construct() {

$settings = Helpers::get_settings();

$this->client = new Client();
$this->fields = [
$this->client_factory = new ClientFactory();
$this->client = $this->client_factory->build();
$this->fields = [
'general' => [
[
'label' => esc_html__( 'Connect your website with Plausible Analytics', 'plausible-analytics' ),
Expand Down Expand Up @@ -118,6 +125,7 @@ public function __construct() {
'type' => 'button',
'disabled' => empty( $settings[ 'domain_name' ] ) ||
empty( $settings[ 'api_token' ] ) ||
$this->client instanceof Client ||
$this->client->is_api_token_valid(),
],
],
Expand Down
5 changes: 3 additions & 2 deletions src/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,10 @@ public function save_options() {
* @throws ApiException
*/
private function validate_api_token( $token = '' ) {
$client = new Client( $token );
$client_factory = new ClientFactory( $token );
$client = $client_factory->build();

if ( ! $client->validate_api_token() ) {
if ( $client instanceof Client && ! $client->validate_api_token() ) {
$hosted_domain = Helpers::get_hosted_domain_url();
$domain = Helpers::get_domain();

Expand Down
55 changes: 55 additions & 0 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Plausible\Analytics\WP;

use Plausible\Analytics\WP\Admin\Messages;

class ClientFactory {
/**
* @var string $token
*/
private $token;

/**
* Setup basic authorization.
*
* @param string $token Allows to specify the token, e.g. when it's not stored in the DB yet.
*/
public function __construct( $token = '' ) {
$this->token = $token;
}

/**
* Loads the Client class if all conditions are met.
*
* @return false|Client
*/
public function build() {
/**
* cURL or allow_url_fopen ini setting is required for GuzzleHttp to function properly.
*/
if ( ! extension_loaded( 'curl' ) && ! ini_get( 'allow_url_fopen' ) ) {
add_action( 'init', [ $this, 'add_curl_error' ] );

return false;
}

return new Client( $this->token );
}

/**
* Show an error on the settings screen if cURL isn't enabled on this machine.
*
* @return void
*
* @codeCoverageIgnore
*/
public function add_curl_error() {
Messages::set_error(
__(
'cURL is not enabled on this server, which means API provisioning will not work. Please contact your hosting provider to enable the cURL module or <code>allow_url_fopen</code>.',
'plausible-analytics'
)
);
}
}

0 comments on commit 856f64e

Please sign in to comment.