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/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php new file mode 100644 index 0000000..5d45676 --- /dev/null +++ b/omnisend/includes/Internal/V1/class-client.php @@ -0,0 +1,111 @@ +api_key = $api_key; + $this->plugin_name = substr( $plugin_name, 0, 50 ); + $this->plugin_version = substr( $plugin_version, 0, 50 ); + } + + + public function create_contact( $contact ): mixed { + $error = new WP_Error(); + + 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() ); + + if ( $error->has_errors() ) { + return $error; + } + + $response = wp_remote_post( + OMNISEND_CORE_API_V3 . '/contacts', + array( + 'body' => wp_json_encode( $contact->to_array() ), + 'headers' => array( + 'Content-Type' => 'application/json', + 'X-API-Key' => $this->api_key, + 'X-INTEGRATION-NAME' => $this->plugin_name, + 'X-INTEGRATION-VERSION' => $this->plugin_version, + ), + 'timeout' => 10, + ) + ); + + if ( is_wp_error( $response ) ) { + error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore + return $response; + } + + $http_code = wp_remote_retrieve_response_code( $response ); + if ( $http_code >= 400 ) { + $body = wp_remote_retrieve_body( $response ); + $err_msg = "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}"; + $error->add( 'omnisend_api', $err_msg ); + return $error; + } + + $body = wp_remote_retrieve_body( $response ); + if ( ! $body ) { + $error->add( 'omnisend_api', 'empty response' ); + return $error; + } + + $arr = json_decode( $body, true ); + + if ( empty( $arr['contactID'] ) ) { + $error->add( 'omnisend_api', 'contactID not found in response' ); + return $error; + } + + 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/Client/V1/class-client.php b/omnisend/includes/Public/Client/V1/class-client.php deleted file mode 100644 index 5c2a604..0000000 --- a/omnisend/includes/Public/Client/V1/class-client.php +++ /dev/null @@ -1,98 +0,0 @@ -add( 'contact', 'Contact is not instance of Omnisend\Public\Client\V1\Contact' ); - return $error; - } - - if ( ! Options::get_api_key() ) { - $error->add( 'api_key', 'Omnisend plugin is not connected' ); - return $error; - } - - $error = $contact->validate(); - if ( $error->has_errors() ) { - return $error; - } - - $response = wp_remote_post( - 'https://api.omnisend.com/v3/contacts', - array( - 'body' => wp_json_encode( $contact->to_array() ), - 'headers' => array( - 'Content-Type' => 'application/json', - 'X-API-Key' => Options::get_api_key(), - ), - 'timeout' => 10, - ) - ); - - if ( is_wp_error( $response ) ) { - error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore - return $response; - } - - $http_code = wp_remote_retrieve_response_code( $response ); - 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; - } - - $body = wp_remote_retrieve_body( $response ); - if ( ! $body ) { - $error->add( 'omnisend_api', 'empty response' ); - return $error; - } - - $arr = json_decode( $body, true ); - - if ( empty( $arr['contactID'] ) ) { - $error->add( 'omnisend_api', 'contactID not found in response' ); - return $error; - } - - return (string) $arr['contactID']; - } -} 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 @@ +