-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #217: first stab at ClientFactory to make sure PHP instances with…
…out curl and/or allow_url_fopen disabled fail softly.
- Loading branch information
Showing
4 changed files
with
77 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) | ||
); | ||
} | ||
} |