From 33af89aa0b0badb3c92aa1047367cccfedc9594a Mon Sep 17 00:00:00 2001 From: Nerijus Zaniauskas Date: Tue, 30 Jan 2024 16:26:52 +0200 Subject: [PATCH 1/4] Add Client factory and client with tracking --- .../Client => Internal}/V1/class-client.php | 77 +++++++++++-------- .../Client/V1 => Internal}/class-utils.php | 2 +- omnisend/includes/Public/V1/class-client.php | 28 +++++++ .../Public/{Client => }/V1/class-contact.php | 3 +- .../includes/Public/V1/class-omnisend.php | 34 ++++++++ 5 files changed, 110 insertions(+), 34 deletions(-) rename omnisend/includes/{Public/Client => Internal}/V1/class-client.php (50%) rename omnisend/includes/{Public/Client/V1 => Internal}/class-utils.php (96%) create mode 100644 omnisend/includes/Public/V1/class-client.php rename omnisend/includes/Public/{Client => }/V1/class-contact.php (99%) create mode 100644 omnisend/includes/Public/V1/class-omnisend.php diff --git a/omnisend/includes/Public/Client/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php similarity index 50% rename from omnisend/includes/Public/Client/V1/class-client.php rename to omnisend/includes/Internal/V1/class-client.php index 5c2a604..fea0723 100644 --- a/omnisend/includes/Public/Client/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -5,62 +5,55 @@ * @package OmnisendClient */ -namespace Omnisend\Public\Client\V1; +namespace Omnisend\Internal\V1; -use Omnisend\Internal\Options; +use Omnisend\Public\V1\Contact; use WP_Error; defined( 'ABSPATH' ) || die( 'no direct access' ); +class Client implements \Omnisend\Public\V1\Client { -/** - * Client class to interact with Omnisend. - * - */ -class Client { + private string $api_key; + private string $plugin_name; + private string $plugin_version; /** - * Check and return if plugin connected to Omnisend account. If connection does not exist, it will not be possible - * to send data to Omnisend. - * - * @return bool + * @param string $plugin_name + * @param string $plugin_version + * @param string $api_key */ - public static function is_connected(): bool { - return Options::get_api_key() != ''; + public function __construct( string $api_key, string $plugin_name, string $plugin_version ) { + $this->api_key = $api_key; + $this->plugin_name = substr( $plugin_name, 0, 50 ); + $this->plugin_version = substr( $plugin_version, 0, 50 ); } - /** - * Create a contact in Omnisend. For it to succeed ensure that provided contact at least have email or phone number. - * - * @param Contact $contact - * - * @return string|WP_Error - */ - public static function create_contact( $contact ): mixed { + + public function create_contact( $contact ): mixed { $error = new WP_Error(); if ( ! $contact instanceof Contact ) { - $error->add( 'contact', 'Contact is not instance of Omnisend\Public\Client\V1\Contact' ); - return $error; + $error->add( 'contact', 'Contact is not instance of Omnisend\Public\V1\Contact' ); + } else { + $error->merge_from( $contact->validate() ); } - if ( ! Options::get_api_key() ) { - $error->add( 'api_key', 'Omnisend plugin is not connected' ); - return $error; - } + $error->merge_from( $this->check_setup() ); - $error = $contact->validate(); if ( $error->has_errors() ) { return $error; } $response = wp_remote_post( - 'https://api.omnisend.com/v3/contacts', + OMNISEND_CORE_API_V3 . '/contacts', array( 'body' => wp_json_encode( $contact->to_array() ), 'headers' => array( - 'Content-Type' => 'application/json', - 'X-API-Key' => Options::get_api_key(), + 'Content-Type' => 'application/json', + 'X-API-Key' => $this->api_key, + 'X-INTEGRATION-NAME' => $this->plugin_name, + 'X-INTEGRATION-VERSION' => $this->plugin_version, ), 'timeout' => 10, ) @@ -75,7 +68,6 @@ public static function create_contact( $contact ): mixed { if ( $http_code >= 400 ) { $body = wp_remote_retrieve_body( $response ); $err_msg = "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}"; - error_log($err_msg); // phpcs:ignore $error->add( 'omnisend_api', $err_msg ); return $error; } @@ -95,4 +87,25 @@ public static function create_contact( $contact ): mixed { return (string) $arr['contactID']; } + + /** + * @return WP_Error + */ + private function check_setup(): WP_Error { + $error = new WP_Error(); + + if ( ! $this->plugin_name ) { + $error->add( 'initialisation', 'Client is created with empty plugin name' ); + } + + if ( ! $this->plugin_version ) { + $error->add( 'initialisation', 'Client is created with empty plugin version' ); + } + + if ( ! $this->api_key ) { + $error->add( 'api_key', 'Omnisend plugin is not connected' ); + } + + return $error; + } } diff --git a/omnisend/includes/Public/Client/V1/class-utils.php b/omnisend/includes/Internal/class-utils.php similarity index 96% rename from omnisend/includes/Public/Client/V1/class-utils.php rename to omnisend/includes/Internal/class-utils.php index 744c14f..591c883 100644 --- a/omnisend/includes/Public/Client/V1/class-utils.php +++ b/omnisend/includes/Internal/class-utils.php @@ -5,7 +5,7 @@ * @package OmnisendClient */ -namespace Omnisend\Public\Client\V1; +namespace Omnisend\Internal; ! defined( 'ABSPATH' ) && die( 'no direct access' ); diff --git a/omnisend/includes/Public/V1/class-client.php b/omnisend/includes/Public/V1/class-client.php new file mode 100644 index 0000000..411d841 --- /dev/null +++ b/omnisend/includes/Public/V1/class-client.php @@ -0,0 +1,28 @@ + Date: Tue, 30 Jan 2024 16:28:46 +0200 Subject: [PATCH 2/4] Refactor --- omnisend/includes/Internal/V1/class-client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index fea0723..5d45676 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -33,10 +33,10 @@ public function __construct( string $api_key, string $plugin_name, string $plugi public function create_contact( $contact ): mixed { $error = new WP_Error(); - if ( ! $contact instanceof Contact ) { - $error->add( 'contact', 'Contact is not instance of Omnisend\Public\V1\Contact' ); - } else { + if ( $contact instanceof Contact ) { $error->merge_from( $contact->validate() ); + } else { + $error->add( 'contact', 'Contact is not instance of Omnisend\Public\V1\Contact' ); } $error->merge_from( $this->check_setup() ); From e00ef5c6e28ae879d4b3b8425c874c674f8d5517 Mon Sep 17 00:00:00 2001 From: Nerijus Zaniauskas Date: Wed, 31 Jan 2024 15:21:42 +0200 Subject: [PATCH 3/4] Add comment --- omnisend/includes/Public/V1/class-omnisend.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/omnisend/includes/Public/V1/class-omnisend.php b/omnisend/includes/Public/V1/class-omnisend.php index 9080c18..148e070 100644 --- a/omnisend/includes/Public/V1/class-omnisend.php +++ b/omnisend/includes/Public/V1/class-omnisend.php @@ -14,6 +14,8 @@ class Omnisend { /** + * Factory to create Omnisend client. + * * @param $plugin string plugin using client name * @param $version string plugin using client version * @return Client From 64060d615e1b3ab4ab60466fec767851f67c9522 Mon Sep 17 00:00:00 2001 From: Nerijus Zaniauskas Date: Wed, 31 Jan 2024 16:06:27 +0200 Subject: [PATCH 4/4] Change plugin name --- omnisend/class-omnisend-core-bootstrap.php | 4 ++-- omnisend/readme.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/omnisend/class-omnisend-core-bootstrap.php b/omnisend/class-omnisend-core-bootstrap.php index 3461a1c..457d983 100644 --- a/omnisend/class-omnisend-core-bootstrap.php +++ b/omnisend/class-omnisend-core-bootstrap.php @@ -24,7 +24,7 @@ const OMNISEND_CORE_PLUGIN_VERSION = '1.0.0'; const OMNISEND_CORE_SETTINGS_PAGE = 'omnisend'; -const OMNISEND_CORE_PLUGIN_NAME = 'Omnisend'; +const OMNISEND_CORE_PLUGIN_NAME = 'Email Marketing by Omnisend'; // Change for different environment. const OMNISEND_CORE_API_V3 = 'https://api.omnisend.com/v3'; @@ -53,7 +53,7 @@ public static function load(): void { public static function add_admin_menu() { $page_title = OMNISEND_CORE_PLUGIN_NAME; - $menu_title = OMNISEND_CORE_PLUGIN_NAME; + $menu_title = 'Omnisend'; $capability = 'manage_options'; $menu_slug = OMNISEND_CORE_SETTINGS_PAGE; $function = 'Omnisend\Internal\Connection::display'; diff --git a/omnisend/readme.txt b/omnisend/readme.txt index 5d94eaf..fc60d53 100644 --- a/omnisend/readme.txt +++ b/omnisend/readme.txt @@ -1,5 +1,5 @@ === Omnisend === -Plugin Name: Omnisend +Plugin Name: Email Marketing by Omnisend Contributors: Omnisend Tags: form, email marketing, web tracking, subscriber collection Requires at least: 4.7.0