File: /var/www/kevin-demo/wp-content/plugins/wpforms-sendinblue/src/Provider/Account.php
<?php
namespace WPFormsSendinblue\Provider;
use Exception;
use WP_Error;
// phpcs:ignore WPForms.PHP.UseStatement.UnusedUseStatement
use WPFormsSendinblue\Api\Connection;
use WPFormsSendinblue\Plugin;
use WPFormsSendinblue\Api\Http\Response;
use WPFormsSendinblue\Api\Exceptions\ResponseException;
use WPFormsSendinblue\Api\Exceptions\AccountAlreadyExistsException;
use WPFormsSendinblue\Api\Exceptions\RequiredArgumentMissingException;
/**
* Class Account.
*
* @since 1.0.0
*/
class Account {
/**
* Get form template for a new account.
*
* @since 1.0.0
*
* @return string
*/
public function get_form() {
return wpforms_sendinblue()
->get( 'template' )
->get_settings_template(
'new-account-form',
[
'core_name' => esc_html__( 'Brevo', 'wpforms-sendinblue' ),
]
);
}
/**
* Check if account with this API key already exists.
*
* @since 1.0.0
*
* @param string $api_key Api key.
*
* @throws AccountAlreadyExistsException The account already exists.
*/
private function is_already_exist( $api_key ) {
$options = wpforms_get_providers_options( Plugin::SLUG );
if (
empty( $options ) ||
! array_key_exists( 'api_key', $options )
) {
return;
}
$keys = wp_list_pluck( $options, 'api_key' );
if ( ! empty( array_search( $api_key, $keys, true ) ) ) {
throw new AccountAlreadyExistsException();
}
}
/**
* Validate an API response.
*
* @since 1.0.0
*
* @param Response $account Account API response.
*
* @throws ResponseException Response has an error message.
* @throws RequiredArgumentMissingException Required argument is missing.
*/
private function validate_account( $account ) {
$body = $account->get_body();
$status = $account->get_response_code();
// Request error.
if ( $status !== 200 ) {
throw new ResponseException( esc_html( $account->get_response_message() ) );
}
// Response data.
if ( empty( $body['email'] ) ) {
throw new RequiredArgumentMissingException( 'email' );
}
}
/**
* Get list of available accounts.
*
* @since 1.0.0
*
* @return array
*/
public function get_accounts() {
$accounts = wpforms_get_providers_options( Plugin::SLUG );
if ( empty( $accounts ) ) {
return [];
}
foreach ( $accounts as $account_id => $account ) {
if ( empty( $account['api_key'] ) || empty( $account['label'] ) ) {
$this->remove_account( $account_id );
continue;
}
$accounts[ $account_id ] = $account['label'];
}
return $accounts;
}
/**
* Get connection.
*
* @since 1.0.0
*
* @param string $account_id Account ID.
*
* @return null|Connection
*/
public function get_connection( $account_id ) {
if ( ! $this->account_exists( $account_id ) ) {
return null;
}
$accounts = wpforms_get_providers_options( Plugin::SLUG );
if ( empty( $accounts[ $account_id ]['api_key'] ) ) {
return null;
}
return wpforms_sendinblue()
->get( 'client' )
->new_connection( $accounts[ $account_id ]['api_key'] );
}
/**
* Get events tracker connection.
*
* @since 1.0.0
*
* @param string $account_id Account ID.
*
* @return null|Connection
*/
public function get_tracker_connection( $account_id ) {
if ( ! $this->account_exists( $account_id ) ) {
return null;
}
$accounts = wpforms_get_providers_options( Plugin::SLUG );
if ( empty( $accounts[ $account_id ]['ma_key'] ) ) {
return null;
}
return wpforms_sendinblue()
->get( 'client' )
->new_tracker_connection( $accounts[ $account_id ]['ma_key'] );
}
/**
* Remove an account.
*
* @since 1.0.0
*
* @param string $account_id Account ID.
*/
public function remove_account( $account_id ) {
$providers = wpforms_get_providers_options();
if ( empty( $providers[ Plugin::SLUG ][ $account_id ] ) ) {
return;
}
unset( $providers[ Plugin::SLUG ][ $account_id ] );
update_option( 'wpforms_providers', $providers );
}
/**
* Get an account by an API key.
*
* @since 1.0.0
*
* @param string $api_key Api key.
*
* @return Response
*
* @throws ResponseException Response has an error message.
* @throws RequiredArgumentMissingException Required argument is missing.
*/
public function get_connection_by_api_key( $api_key ) {
$account = wpforms_sendinblue()
->get( 'client' )
->new_connection( sanitize_text_field( $api_key ) )
->get_account();
$this->validate_account( $account );
return $account;
}
/**
* Account exists.
*
* @since 1.0.0
*
* @param string $account_id Account ID.
*
* @return bool
*/
public function account_exists( $account_id ) {
$accounts = wpforms_get_providers_options( Plugin::SLUG );
return ! empty( $accounts[ $account_id ] );
}
/**
* Add a new account.
*
* @since 1.0.0
*
* @param string $api_key API key.
* @param string $account_name Account name.
*
* @return array
*
* @throws ResponseException Response has an error message.
* @throws RequiredArgumentMissingException Required argument is missing.
* @throws AccountAlreadyExistsException Account already exists.
*/
public function add( $api_key, $account_name ) {
$account = $this->get_connection_by_api_key( $api_key );
$this->is_already_exist( $api_key );
return $this->save( $api_key, $account_name, $account );
}
/**
* Save a new account.
*
* @since 1.0.0
*
* @param string $api_key API key.
* @param string $account_name Account name.
* @param Response $account Account API response.
*
* @return array
*/
private function save( $api_key, $account_name, $account ) {
$body = $account->get_body();
if ( empty( $account_name ) ) {
$account_name = sanitize_email( $body['email'] );
}
$key = uniqid( '', true );
$options = [
'api_key' => $api_key,
'label' => $account_name,
'date' => time(),
'ma_key' => ! empty( $body['marketingAutomation']['key'] ) ? $body['marketingAutomation']['key'] : '',
];
// Save this connection for the provider.
wpforms_update_providers_options( Plugin::SLUG, $options, $key );
$options['key'] = $key;
return $options;
}
/**
* Get and save the MA key of an account.
*
* @since 1.3.0
*
* @param string $account_id Account ID.
*
* @return true|WP_Error Returns `true` if no error occurred. Otherwise, returns `WP_Error`.
* Please note that `true` doesn't necessarily mean that the new MA key
* was saved successfully in the DB.
*/
public function get_and_save_ma_key( $account_id ) {
$account = $this->get_account_by_id( $account_id );
if ( empty( $account ) || empty( $account['api_key'] ) || ! empty( $account['ma_key'] ) ) {
return new WP_Error(
'wpforms_sendinblue_unable_to_fetch_ma_key',
esc_html__( 'Unable to fetch MA key. Either MA key already exists or no API key present.', 'wpforms-sendinblue' )
);
}
try {
$ma_key = $this->get_ma_key_by_api_key( $account['api_key'] );
if ( empty( $ma_key ) ) {
return new WP_Error(
'wpforms_sendinblue_no_refetched_ma_key',
esc_html__( 'No MA key returned.', 'wpforms-sendinblue' )
);
}
$account['ma_key'] = $ma_key;
wpforms_update_providers_options( Plugin::SLUG, $account, $account_id );
} catch ( Exception $e ) {
return new WP_Error( 'wpforms_sendinblue_fetch_ma_key_error', $e->getMessage() );
}
return true;
}
/**
* Get an account by ID.
*
* @since 1.3.0
*
* @param string $account_id Account ID.
*
* @return array
*/
private function get_account_by_id( $account_id ) {
$accounts = wpforms_get_providers_options( Plugin::SLUG );
if ( empty( $accounts ) ) {
return [];
}
foreach ( $accounts as $acc_id => $account ) {
if ( empty( $account['api_key'] ) || empty( $account['label'] ) ) {
$this->remove_account( $account_id );
continue;
}
if ( $acc_id === $account_id ) {
return $account;
}
}
return [];
}
/**
* Get MA key by API key.
*
* @since 1.3.0
*
* @param string $api_key API key.
*
* @return string
*
* @throws RequiredArgumentMissingException Required argument is missing.
* @throws ResponseException Response has an error message.
*/
private function get_ma_key_by_api_key( $api_key ) {
$account_data = $this->get_connection_by_api_key( $api_key );
$body = $account_data->get_body();
return empty( $body['marketingAutomation']['key'] ) ? '' : $body['marketingAutomation']['key'];
}
}