Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WP_Rocket\Addon\Cloudflare\Auth;
|
||||
|
||||
use WP_Error;
|
||||
|
||||
class APIKey implements AuthInterface {
|
||||
/**
|
||||
* Cloudflare email
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $email = '';
|
||||
|
||||
/**
|
||||
* Cloudflare API Key
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $api_key = '';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $email Cloudflare email.
|
||||
* @param string $api_key Cloudflare API key.
|
||||
*/
|
||||
public function __construct( string $email = '', string $api_key = '' ) {
|
||||
$this->email = $email;
|
||||
$this->api_key = $api_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets headers for Cloudflare API request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_headers(): array {
|
||||
return [
|
||||
'X-Auth-Email' => $this->email,
|
||||
'X-Auth-Key' => $this->api_key,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the credentials are set.
|
||||
*
|
||||
* @return bool|WP_Error true if authorized, false if not, WP_Error if either credential is empty.
|
||||
*/
|
||||
public function is_valid_credentials() {
|
||||
if (
|
||||
empty( $this->email )
|
||||
||
|
||||
empty( $this->api_key )
|
||||
) {
|
||||
return new WP_Error(
|
||||
'cloudflare_credentials_empty',
|
||||
sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Cloudflare email and/or API key are not set. Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return false !== filter_var( $this->email, FILTER_VALIDATE_EMAIL );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare\Auth;
|
||||
|
||||
use WP_Rocket\Addon\Cloudflare\Auth\APIKey;
|
||||
use WP_Rocket\Addon\Cloudflare\Auth\AuthInterface;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
|
||||
class APIKeyFactory implements AuthFactoryInterface {
|
||||
|
||||
|
||||
/**
|
||||
* Options Data instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Instantiate the class.
|
||||
*
|
||||
* @param Options_Data $options Options Data instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new authentication instance.
|
||||
*
|
||||
* @param array $data Data to inject into the client.
|
||||
* @return AuthInterface
|
||||
*/
|
||||
public function create( array $data = [] ): AuthInterface {
|
||||
|
||||
$cf_api_key = defined( 'WP_ROCKET_CF_API_KEY' ) ? rocket_get_constant( 'WP_ROCKET_CF_API_KEY', '' ) : $this->options->get( 'cloudflare_api_key', '' );
|
||||
|
||||
$email = key_exists( 'cloudflare_email', $data ) ? $data['cloudflare_email'] : $this->options->get( 'cloudflare_email', '' );
|
||||
$api_key = key_exists( 'cloudflare_api_key', $data ) ? $data['cloudflare_api_key'] : $cf_api_key;
|
||||
|
||||
return new APIKey( $email, $api_key );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare\Auth;
|
||||
|
||||
use WP_Rocket\Addon\Cloudflare\Auth\AuthInterface;
|
||||
|
||||
interface AuthFactoryInterface {
|
||||
|
||||
/**
|
||||
* Create a new authentication instance.
|
||||
*
|
||||
* @param array $data Data to inject into the client.
|
||||
* @return AuthInterface
|
||||
*/
|
||||
public function create( array $data = [] ): AuthInterface;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Addon\Cloudflare\Auth;
|
||||
|
||||
use WP_Error;
|
||||
|
||||
interface AuthInterface {
|
||||
/**
|
||||
* Gets headers for Cloudflare API request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_headers(): array;
|
||||
|
||||
/**
|
||||
* Checks if the credentials are set.
|
||||
*
|
||||
* @return bool|WP_Error true if authorized, false otherwise, WP_Error if missing credentials.
|
||||
*/
|
||||
public function is_valid_credentials();
|
||||
}
|
||||
Reference in New Issue
Block a user