From 459330bbb82dd86555bd279fc5974f8232f29dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Mon, 1 Jul 2024 18:32:51 +0300 Subject: [PATCH 01/29] add events API support --- omnisend/class-omnisend-core-bootstrap.php | 1 + .../includes/Internal/V1/class-client.php | 140 +++++++++++++----- omnisend/includes/SDK/V1/class-client.php | 19 ++- omnisend/includes/SDK/V1/class-event.php | 110 ++++++++++++++ .../SDK/V1/class-sendcustomeventresponse.php | 33 +++++ 5 files changed, 261 insertions(+), 42 deletions(-) create mode 100644 omnisend/includes/SDK/V1/class-event.php create mode 100644 omnisend/includes/SDK/V1/class-sendcustomeventresponse.php diff --git a/omnisend/class-omnisend-core-bootstrap.php b/omnisend/class-omnisend-core-bootstrap.php index 3aed023..02a8347 100644 --- a/omnisend/class-omnisend-core-bootstrap.php +++ b/omnisend/class-omnisend-core-bootstrap.php @@ -34,6 +34,7 @@ // Change for different environment. const OMNISEND_CORE_API_V3 = 'https://api.omnisend.com/v3'; +const OMNISEND_CORE_API_V5 = 'https://api.omnisend.com/v5'; const OMNISEND_CORE_SNIPPET_URL = 'https://omnisnippet1.com/inshop/launcher-v2.js'; // Omnisend for Woo plugin. diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index a3b9833..2dba8fa 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -1,4 +1,5 @@ api_key = $api_key; - $this->plugin_name = substr( $plugin_name, 0, 50 ); - $this->plugin_version = substr( $plugin_version, 0, 50 ); + $this->plugin_name = substr($plugin_name, 0, 50); + $this->plugin_version = substr($plugin_version, 0, 50); } - public function create_contact( $contact ): CreateContactResponse { + public function create_contact($contact): CreateContactResponse + { $error = new WP_Error(); - if ( $contact instanceof Contact ) { - $error->merge_from( $contact->validate() ); + if ($contact instanceof Contact) { + $error->merge_from($contact->validate()); } else { - $error->add( 'contact', 'Contact is not instance of Omnisend\SDK\V1\Contact.' ); + $error->add('contact', 'Contact is not instance of Omnisend\SDK\V1\Contact.'); } - $error->merge_from( $this->check_setup() ); + $error->merge_from($this->check_setup()); - if ( $error->has_errors() ) { - return new CreateContactResponse( '', $error ); + if ($error->has_errors()) { + return new CreateContactResponse('', $error); } $response = wp_remote_post( OMNISEND_CORE_API_V3 . '/contacts', array( - 'body' => wp_json_encode( $contact->to_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 new CreateContactResponse('', $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 new CreateContactResponse('', $error); + } + + $body = wp_remote_retrieve_body($response); + if (!$body) { + $error->add('omnisend_api', 'empty response'); + return new CreateContactResponse('', $error); + } + + $arr = json_decode($body, true); + + if (empty($arr['contactID'])) { + $error->add('omnisend_api', 'contactID not found in response.'); + return new CreateContactResponse('', $error); + } + + return new CreateContactResponse((string) $arr['contactID'], $error); + } + + public function send_customer_event($event): SendCustomEventResponse + { + $error = new WP_Error(); + + if ($event instanceof Event) { + $error->merge_from($event->validate()); + } else { + $error->add('event', 'Event is not instance of Omnisend\SDK\V1\Event.'); + } + + $error->merge_from($this->check_setup()); + + if ($error->has_errors()) { + return new SendCustomEventResponse($error); + } + + $response = wp_remote_post( + OMNISEND_CORE_API_V5 . '/events', + array( + 'body' => wp_json_encode($event->to_array()), 'headers' => array( 'Content-Type' => 'application/json', 'X-API-Key' => $this->api_key, @@ -60,51 +125,52 @@ public function create_contact( $contact ): CreateContactResponse { ) ); - if ( is_wp_error( $response ) ) { + if (is_wp_error($response)) { error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore - return new CreateContactResponse( '', $response ); + return new SendCustomEventResponse($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 new CreateContactResponse( '', $error ); + $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 new SendCustomEventResponse($error); } - $body = wp_remote_retrieve_body( $response ); - if ( ! $body ) { - $error->add( 'omnisend_api', 'empty response' ); - return new CreateContactResponse( '', $error ); + $body = wp_remote_retrieve_body($response); + if (!$body) { + $error->add('omnisend_api', 'empty response'); + return new CreateContactResponse('', $error); } - $arr = json_decode( $body, true ); + $arr = json_decode($body, true); - if ( empty( $arr['contactID'] ) ) { - $error->add( 'omnisend_api', 'contactID not found in response.' ); - return new CreateContactResponse( '', $error ); + if (empty($arr['contactID'])) { + $error->add('omnisend_api', 'contactID not found in response.'); + return new SendCustomEventResponse($error); } - return new CreateContactResponse( (string) $arr['contactID'], $error ); + return new SendCustomEventResponse($error); } /** * @return WP_Error */ - private function check_setup(): 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_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->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.' ); + if (!$this->api_key) { + $error->add('api_key', 'Omnisend plugin is not connected.'); } return $error; diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index 9a0cd88..d5f7d53 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -1,4 +1,5 @@ contact != null) { + $contactError = $this->contact->validate; + if (is_wp_error($contactError)) { + $error->add($contactError); + } + } + + return $error; + } + + /** + * Sets event name. + * + * @param $event_name + * + * @return void + */ + public function set_event_name($event_name): void + { + $this->event_name = $event_name; + } + + /** + * Sets event time. + * + * @param $event_time + * + * @return void + */ + public function set_event_time($event_time): void + { + $this->event_time = $event_time; + } + + + /** + * Sets event origin.". //todo event origin ar just origin? + * + * @param $origin + * + * @return void + */ + public function set_origin($origin): void + { + $this->origin = $origin; + } + + /** + * Sets event properties. //todo event properties ar just properties? + * + * @param $properties + * + * @return void + */ + public function set_properties($properties): void + { + $this->properties = $properties; + } + + /** + * Sets contact. + * + * @param $contact + * + * @return void + */ + public function set_contact($contact): void + { + $this->contact = $contact; + } +} diff --git a/omnisend/includes/SDK/V1/class-sendcustomeventresponse.php b/omnisend/includes/SDK/V1/class-sendcustomeventresponse.php new file mode 100644 index 0000000..dae7b7c --- /dev/null +++ b/omnisend/includes/SDK/V1/class-sendcustomeventresponse.php @@ -0,0 +1,33 @@ +wp_error = $wp_error; + } + + public function get_wp_error(): WP_Error + { + return $this->wp_error; + } +} From 40c8a05217de3fd9c47848052deaa2f1cfd097a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Mon, 1 Jul 2024 18:43:25 +0300 Subject: [PATCH 02/29] fix naming --- README.md | 1 + omnisend/includes/Internal/V1/class-client.php | 16 ++++++++-------- omnisend/includes/SDK/V1/class-client.php | 2 +- ...e.php => class-sendcustomereventresponse.php} | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) rename omnisend/includes/SDK/V1/{class-sendcustomeventresponse.php => class-sendcustomereventresponse.php} (92%) diff --git a/README.md b/README.md index 25b1787..76a963f 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Here is how you can create a basic client & submit contact. $response = $client->create_contact( $contact ); ``` +#### Customer events #### Error handling diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index 2dba8fa..8e6a9cc 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -11,7 +11,7 @@ use Omnisend\SDK\V1\Contact; use Omnisend\SDK\V1\CreateContactResponse; use Omnisend\SDK\V1\Event; -use Omnisend\SDK\V1\SendCustomEventResponse; +use Omnisend\SDK\V1\SendCustomerEventResponse; use WP_Error; defined('ABSPATH') || die('no direct access'); @@ -95,7 +95,7 @@ public function create_contact($contact): CreateContactResponse return new CreateContactResponse((string) $arr['contactID'], $error); } - public function send_customer_event($event): SendCustomEventResponse + public function send_customer_event($event): SendCustomerEventResponse { $error = new WP_Error(); @@ -108,7 +108,7 @@ public function send_customer_event($event): SendCustomEventResponse $error->merge_from($this->check_setup()); if ($error->has_errors()) { - return new SendCustomEventResponse($error); + return new SendCustomerEventResponse($error); } $response = wp_remote_post( @@ -127,7 +127,7 @@ public function send_customer_event($event): SendCustomEventResponse if (is_wp_error($response)) { error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore - return new SendCustomEventResponse($response); + return new SendCustomerEventResponse($response); } $http_code = wp_remote_retrieve_response_code($response); @@ -135,23 +135,23 @@ public function send_customer_event($event): SendCustomEventResponse $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 new SendCustomEventResponse($error); + return new SendCustomerEventResponse($error); } $body = wp_remote_retrieve_body($response); if (!$body) { $error->add('omnisend_api', 'empty response'); - return new CreateContactResponse('', $error); + return new SendCustomerEventResponse('', $error); } $arr = json_decode($body, true); if (empty($arr['contactID'])) { $error->add('omnisend_api', 'contactID not found in response.'); - return new SendCustomEventResponse($error); + return new SendCustomerEventResponse($error); } - return new SendCustomEventResponse($error); + return new SendCustomerEventResponse($error); } /** diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index d5f7d53..065c05e 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -33,5 +33,5 @@ public function create_contact($contact): CreateContactResponse; * * @return CreateContactResponse */ - public function send_customer_event($event): SendCustomEventResponse; + public function send_customer_event($event): SendCustomerEventResponse; } diff --git a/omnisend/includes/SDK/V1/class-sendcustomeventresponse.php b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php similarity index 92% rename from omnisend/includes/SDK/V1/class-sendcustomeventresponse.php rename to omnisend/includes/SDK/V1/class-sendcustomereventresponse.php index dae7b7c..73b0517 100644 --- a/omnisend/includes/SDK/V1/class-sendcustomeventresponse.php +++ b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php @@ -12,7 +12,7 @@ defined('ABSPATH') || die('no direct access'); -class SendCustomEventResponse +class SendCustomerEventResponse { private WP_Error $wp_error; From 108ee7b17a59957b376b93db0828240317723832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Mon, 1 Jul 2024 19:04:27 +0300 Subject: [PATCH 03/29] lint errors --- .../includes/Internal/V1/class-client.php | 132 +++++++++--------- omnisend/includes/SDK/V1/class-client.php | 12 +- omnisend/includes/SDK/V1/class-event.php | 43 +++--- .../V1/class-sendcustomereventresponse.php | 15 +- 4 files changed, 94 insertions(+), 108 deletions(-) diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index 8e6a9cc..bc9aea8 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -1,5 +1,4 @@ api_key = $api_key; - $this->plugin_name = substr($plugin_name, 0, 50); - $this->plugin_version = substr($plugin_version, 0, 50); + $this->plugin_name = substr( $plugin_name, 0, 50 ); + $this->plugin_version = substr( $plugin_version, 0, 50 ); } - public function create_contact($contact): CreateContactResponse - { + public function create_contact( $contact ): CreateContactResponse { $error = new WP_Error(); - if ($contact instanceof Contact) { - $error->merge_from($contact->validate()); + if ( $contact instanceof Contact ) { + $error->merge_from( $contact->validate() ); } else { - $error->add('contact', 'Contact is not instance of Omnisend\SDK\V1\Contact.'); + $error->add( 'contact', 'Contact is not instance of Omnisend\SDK\V1\Contact.' ); } - $error->merge_from($this->check_setup()); + $error->merge_from( $this->check_setup() ); - if ($error->has_errors()) { - return new CreateContactResponse('', $error); + if ( $error->has_errors() ) { + return new CreateContactResponse( '', $error ); } $response = wp_remote_post( OMNISEND_CORE_API_V3 . '/contacts', array( - 'body' => wp_json_encode($contact->to_array()), + 'body' => wp_json_encode( $contact->to_array() ), 'headers' => array( 'Content-Type' => 'application/json', 'X-API-Key' => $this->api_key, @@ -66,55 +64,54 @@ public function create_contact($contact): CreateContactResponse ) ); - if (is_wp_error($response)) { + if ( is_wp_error( $response ) ) { error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore - return new CreateContactResponse('', $response); + return new CreateContactResponse( '', $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 new CreateContactResponse('', $error); + $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 new CreateContactResponse( '', $error ); } - $body = wp_remote_retrieve_body($response); - if (!$body) { - $error->add('omnisend_api', 'empty response'); - return new CreateContactResponse('', $error); + $body = wp_remote_retrieve_body( $response ); + if ( ! $body ) { + $error->add( 'omnisend_api', 'empty response' ); + return new CreateContactResponse( '', $error ); } - $arr = json_decode($body, true); + $arr = json_decode( $body, true ); - if (empty($arr['contactID'])) { - $error->add('omnisend_api', 'contactID not found in response.'); - return new CreateContactResponse('', $error); + if ( empty( $arr['contactID'] ) ) { + $error->add( 'omnisend_api', 'contactID not found in response.' ); + return new CreateContactResponse( '', $error ); } - return new CreateContactResponse((string) $arr['contactID'], $error); + return new CreateContactResponse( (string) $arr['contactID'], $error ); } - public function send_customer_event($event): SendCustomerEventResponse - { + public function send_customer_event( $event ): SendCustomerEventResponse { $error = new WP_Error(); - if ($event instanceof Event) { - $error->merge_from($event->validate()); + if ( $event instanceof Event ) { + $error->merge_from( $event->validate() ); } else { - $error->add('event', 'Event is not instance of Omnisend\SDK\V1\Event.'); + $error->add( 'event', 'Event is not instance of Omnisend\SDK\V1\Event.' ); } - $error->merge_from($this->check_setup()); + $error->merge_from( $this->check_setup() ); - if ($error->has_errors()) { - return new SendCustomerEventResponse($error); + if ( $error->has_errors() ) { + return new SendCustomerEventResponse( $error ); } $response = wp_remote_post( OMNISEND_CORE_API_V5 . '/events', array( - 'body' => wp_json_encode($event->to_array()), + 'body' => wp_json_encode( $event->to_array() ), 'headers' => array( 'Content-Type' => 'application/json', 'X-API-Key' => $this->api_key, @@ -125,52 +122,51 @@ public function send_customer_event($event): SendCustomerEventResponse ) ); - if (is_wp_error($response)) { + if ( is_wp_error( $response ) ) { error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore - return new SendCustomerEventResponse($response); + return new SendCustomerEventResponse( $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 new SendCustomerEventResponse($error); + $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 new SendCustomerEventResponse( $error ); } - $body = wp_remote_retrieve_body($response); - if (!$body) { - $error->add('omnisend_api', 'empty response'); - return new SendCustomerEventResponse('', $error); + $body = wp_remote_retrieve_body( $response ); + if ( ! $body ) { + $error->add( 'omnisend_api', 'empty response' ); + return new SendCustomerEventResponse( '', $error ); } - $arr = json_decode($body, true); + $arr = json_decode( $body, true ); - if (empty($arr['contactID'])) { - $error->add('omnisend_api', 'contactID not found in response.'); - return new SendCustomerEventResponse($error); + if ( empty( $arr['contactID'] ) ) { + $error->add( 'omnisend_api', 'contactID not found in response.' ); + return new SendCustomerEventResponse( $error ); } - return new SendCustomerEventResponse($error); + return new SendCustomerEventResponse( $error ); } /** * @return WP_Error */ - private function check_setup(): 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_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->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.'); + if ( ! $this->api_key ) { + $error->add( 'api_key', 'Omnisend plugin is not connected.' ); } return $error; diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index 065c05e..bfb36db 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -1,5 +1,4 @@ contact != null) { - $contactError = $this->contact->validate; - if (is_wp_error($contactError)) { - $error->add($contactError); + if ( $this->contact != null ) { + $contact_error = $this->contact->validate; + if ( is_wp_error( $contact_error ) ) { + $error->add( $contact_error ); } } @@ -54,8 +52,7 @@ public function validate(): WP_Error * * @return void */ - public function set_event_name($event_name): void - { + public function set_event_name( $event_name ): void { $this->event_name = $event_name; } @@ -66,8 +63,7 @@ public function set_event_name($event_name): void * * @return void */ - public function set_event_time($event_time): void - { + public function set_event_time( $event_time ): void { $this->event_time = $event_time; } @@ -79,8 +75,7 @@ public function set_event_time($event_time): void * * @return void */ - public function set_origin($origin): void - { + public function set_origin( $origin ): void { $this->origin = $origin; } @@ -91,8 +86,7 @@ public function set_origin($origin): void * * @return void */ - public function set_properties($properties): void - { + public function set_properties( $properties ): void { $this->properties = $properties; } @@ -103,8 +97,7 @@ public function set_properties($properties): void * * @return void */ - public function set_contact($contact): void - { + public function set_contact( $contact ): void { $this->contact = $contact; } } diff --git a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php index 73b0517..f03df5b 100644 --- a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php +++ b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php @@ -1,5 +1,4 @@ wp_error = $wp_error; + public function __construct( WP_Error $wp_error ) { + $this->wp_error = $wp_error; } - public function get_wp_error(): WP_Error - { + public function get_wp_error(): WP_Error { return $this->wp_error; } } From 410d81acaa8d837012689da3de3781e2a3ec6fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulius=20Le=C5=A1=C4=8Dinskas?= Date: Tue, 2 Jul 2024 15:32:34 +0300 Subject: [PATCH 04/29] Fixes broken link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 76a963f..e4f74bf 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The plugin provides an SDK client to easily integrate with Omnisend API. > To use in your plugin you must check if wp-omnisend plugin is installed. > Provided client will send data to Omnisend if it is connected. -You can find function references in the [client folder](https://github.com/omnisend/wp-omnisend/tree/main/omnisend/includes/Public/V1). +You can find function references in the [client folder](https://github.com/omnisend/wp-omnisend/tree/events-support/omnisend/includes/SDK/V1). ### Examples From f1a281c1d281435fc525e80e9424fb125599345e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Tue, 2 Jul 2024 15:48:55 +0300 Subject: [PATCH 05/29] WIP events support --- .../includes/Internal/V1/class-client.php | 1 + omnisend/includes/SDK/V1/class-client.php | 2 +- omnisend/includes/SDK/V1/class-event.php | 78 ++++++++--- .../includes/SDK/V1/class-eventcontact.php | 122 ++++++++++++++++++ .../V1/class-sendcustomereventresponse.php | 1 - 5 files changed, 184 insertions(+), 20 deletions(-) create mode 100644 omnisend/includes/SDK/V1/class-eventcontact.php diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index bc9aea8..9a89055 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -19,6 +19,7 @@ class Client implements \Omnisend\SDK\V1\Client { + private string $api_key; private string $plugin_name; private string $plugin_version; diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index bfb36db..a60afa6 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -31,7 +31,7 @@ public function create_contact( $contact ): CreateContactResponse; * * @param Event $event * - * @return CreateContactResponse + * @return SendCustomerEventResponse */ public function send_customer_event( $event ): SendCustomerEventResponse; } diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 85da385..6cf57eb 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -7,6 +7,7 @@ namespace Omnisend\SDK\V1; +use Omnisend\SDK\V1\EventContact; use WP_Error; defined( 'ABSPATH' ) || die( 'no direct access' ); @@ -17,16 +18,17 @@ */ class Event { - private $contact = null; - private $event_name = null; - private $event_time = null; - private $origin = null; - private $properties = null; + + private $contact = null; + private $event_name = null; + private $event_time = null; + private $origin = null; + private array $properties = array(); /** * Validate event properties. * - * todo kas yra reuquired? ka reikia vailiduoti? + * TODO kas yra reuquired? ka reikia vailiduoti? * It ensures that phone or email is set and that they are valid. In addition other properties are validated if they are expected type and format. * * @return WP_Error @@ -34,12 +36,8 @@ class Event { public function validate(): WP_Error { $error = new WP_Error(); - - if ( $this->contact != null ) { - $contact_error = $this->contact->validate; - if ( is_wp_error( $contact_error ) ) { - $error->add( $contact_error ); - } + if ( $contact instanceof EventContact ) { + $error->merge_from( $contact->validate() ); } return $error; @@ -69,7 +67,7 @@ public function set_event_time( $event_time ): void { /** - * Sets event origin.". //todo event origin ar just origin? + * Sets event origin. * * @param $origin * @@ -80,14 +78,17 @@ public function set_origin( $origin ): void { } /** - * Sets event properties. //todo event properties ar just properties? - * - * @param $properties + * @param $key + * @param $value * * @return void */ - public function set_properties( $properties ): void { - $this->properties = $properties; + public function add_properties( $key, $value ): void { + if ( $key == '' ) { + return; + } + + $this->properties[ $key ] = $value; } /** @@ -100,4 +101,45 @@ public function set_properties( $properties ): void { public function set_contact( $contact ): void { $this->contact = $contact; } + + /** + * Convert event to array. + * + * If event is valid it will be transformed to array that can be sent to Omnisend. + * + * @return array + */ + public function to_array(): array { + if ( $this->validate()->has_errors() ) { + return array(); + } + + $time_now = gmdate( 'c' ); + + $arr = array(); + + if ( $this->contact ) { + $arr['contact'] = $this->contact->to_array(); + } + + if ( $this->event_name ) { + $arr['eventName'] = $this->event_name; + } + + if ( $this->event_time ) { + $arr['eventTime'] = $this->event_time; + } else { + $arr['eventTime'] = $this->$time_now; + } + + if ( $this->origin ) { + $arr['origin'] = $this->origin; + } + + if ( $this->properties ) { + $arr['properties'] = $this->properties; + } + + return $arr; + } } diff --git a/omnisend/includes/SDK/V1/class-eventcontact.php b/omnisend/includes/SDK/V1/class-eventcontact.php new file mode 100644 index 0000000..46dfd1e --- /dev/null +++ b/omnisend/includes/SDK/V1/class-eventcontact.php @@ -0,0 +1,122 @@ +email != null && ! is_email( $this->email ) ) { + $error->add( 'email', 'Not a email.' ); + } + + if ( $this->phone == null && $this->email == null && $this->id == null ) { + $error->add( 'identifier', 'Phone or email or ID must be set.' ); + } + + $string_properties = array( + 'id', + 'email', + 'phone', + ); + + foreach ( $string_properties as $property ) { + if ( $this->$property != null && ! is_string( $this->$property ) ) { + $error->add( $property, 'Not a string.' ); + } + } + + return $error; + } + + /** + * Sets event name. + * + * @param $id + * + * @return void + */ + public function set_id( $id ): void { + $this->id = $id; + } + + /** + * Sets event time. + * + * @param $email + * + * @return void + */ + public function set_email( $email ): void { + $this->email = $email; + } + + + /** + * Sets event phone. + * + * @param $phone + * + * @return void + */ + public function set_phone( $phone ): void { + $this->phone = $phone; + } + + /** + * Convert event to array. + * + * If event is valid it will be transformed to array that can be sent to Omnisend. + * + * @return array + */ + public function to_array(): array { + if ( $this->validate()->has_errors() ) { + return array(); + } + + $arr = array(); + + if ( $this->id ) { + $arr['id'] = $this->id; + } + + if ( $this->email ) { + $arr['email'] = $this->email; + } + + if ( $this->phone ) { + $arr['phone'] = $this->phone; + } + + return $arr; + } +} diff --git a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php index f03df5b..5f6418d 100644 --- a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php +++ b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php @@ -17,7 +17,6 @@ class SendCustomerEventResponse { private WP_Error $wp_error; /** - * @param string $contact_id * @param WP_Error $wp_error */ public function __construct( WP_Error $wp_error ) { From 0c408bd6cc5a49c418a6dd7677e5b7d3d6d56706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Tue, 2 Jul 2024 15:53:31 +0300 Subject: [PATCH 06/29] update comments --- omnisend/includes/SDK/V1/class-event.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 6cf57eb..e795374 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -18,7 +18,6 @@ */ class Event { - private $contact = null; private $event_name = null; private $event_time = null; @@ -29,7 +28,7 @@ class Event { * Validate event properties. * * TODO kas yra reuquired? ka reikia vailiduoti? - * It ensures that phone or email is set and that they are valid. In addition other properties are validated if they are expected type and format. + * It ensures that all required properties are set * * @return WP_Error */ From cbe0832176ac4527d38f625d2de095c71aa9ec64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Tue, 2 Jul 2024 16:16:00 +0300 Subject: [PATCH 07/29] add documentation --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e4f74bf..a5c6484 100644 --- a/README.md +++ b/README.md @@ -69,11 +69,30 @@ Here is how you can create a basic client & submit contact. $response = $client->create_contact( $contact ); ``` + #### Customer events +Here is how you can send customer events. + +```php + $contact = new EventContact(); + $contact->set_email( $email ); + + $event = new Event(); + $event->set_contact($contact); + $event->set_origin('wordpress'); + $event->set_event_name('something hapened'); + $event->add_properties('importantProperty1', $importantProperty1); + $event->add_properties('importantProperty2', $importantProperty2); + + $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); + + $response = $client->send_customer_event($event); +``` + #### Error handling -If data provided is invalid or creation fails, then +If data provided is invalid or contact creation fails, then ```php $response = $client->create_contact($contact) @@ -88,6 +107,21 @@ Will return `CreateContactResponse`. Depending on your integration logic you sho } ``` +If data provided is invalid or sending customer event fails, then + +```php +$response = $client->send_customer_event($event); +``` + +Will return `SendCustomerEventResponse`. Depending on your integration logic you should handle the error i.e + +```php + if ( $response->get_wp_error()->has_errors() ) { + error_log( 'Error in after_submission: ' . $response->get_wp_error()->get_error_message()); + return; + } +``` + ## PHP Linting WordPress.org team mandates our plugin to be linted From 8280f96da69d2ce3db97ce5dbe7ecfe027cc2904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Tue, 2 Jul 2024 16:18:04 +0300 Subject: [PATCH 08/29] alignment --- README.md | 63 +++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a5c6484..7e93537 100644 --- a/README.md +++ b/README.md @@ -46,28 +46,27 @@ This is done by getting an actual client Here is how you can create a basic client & submit contact. ```php - $contact = new Contact(); - - $contact->set_email( $email ); - if ( $phone_number != '' ) { - $contact->set_phone( $phone_number ); - } - $contact->set_first_name( $first_name ); - $contact->set_last_name( $last_name ); - $contact->set_birthday( $birthday ); - $contact->set_postal_code( $postal_code ); - $contact->set_address( $address ); - $contact->set_state( $state ); - $contact->set_country( $country ); - $contact->set_city( $city ); - if ( $email_consent ) { - $contact->set_email_consent( 'actual_email_consent_for_gdrp' ); - $contact->set_email_opt_in( 'where user opted to become subscriber' ); - } - - $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); - - $response = $client->create_contact( $contact ); + $contact = new Contact(); + + $contact->set_email( $email ); + if ( $phone_number != '' ) { + $contact->set_phone( $phone_number ); + } + $contact->set_first_name( $first_name ); + $contact->set_last_name( $last_name ); + $contact->set_birthday( $birthday ); + $contact->set_postal_code( $postal_code ); + $contact->set_address( $address ); + $contact->set_state( $state ); + $contact->set_country( $country ); + $contact->set_city( $city ); + if ( $email_consent ) { + $contact->set_email_consent( 'actual_email_consent_for_gdrp' ); + $contact->set_email_opt_in( 'where user opted to become subscriber' ); + } + $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); + + $response = $client->create_contact( $contact ); ``` #### Customer events @@ -75,19 +74,19 @@ Here is how you can create a basic client & submit contact. Here is how you can send customer events. ```php - $contact = new EventContact(); - $contact->set_email( $email ); + $contact = new EventContact(); + $contact->set_email( $email ); - $event = new Event(); - $event->set_contact($contact); - $event->set_origin('wordpress'); - $event->set_event_name('something hapened'); - $event->add_properties('importantProperty1', $importantProperty1); - $event->add_properties('importantProperty2', $importantProperty2); + $event = new Event(); + $event->set_contact($contact); + $event->set_origin('wordpress'); + $event->set_event_name('something hapened'); + $event->add_properties('importantProperty1', $importantProperty1); + $event->add_properties('importantProperty2', $importantProperty2); - $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); + $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); - $response = $client->send_customer_event($event); + $response = $client->send_customer_event($event); ``` #### Error handling From 93f172235a5a40d0e4dc6b8ffb579e09e004525b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Tue, 2 Jul 2024 16:56:11 +0300 Subject: [PATCH 09/29] fix client --- omnisend/includes/Internal/V1/class-client.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index 9a89055..f630cdc 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -136,19 +136,6 @@ public function send_customer_event( $event ): SendCustomerEventResponse { return new SendCustomerEventResponse( $error ); } - $body = wp_remote_retrieve_body( $response ); - if ( ! $body ) { - $error->add( 'omnisend_api', 'empty response' ); - return new SendCustomerEventResponse( '', $error ); - } - - $arr = json_decode( $body, true ); - - if ( empty( $arr['contactID'] ) ) { - $error->add( 'omnisend_api', 'contactID not found in response.' ); - return new SendCustomerEventResponse( $error ); - } - return new SendCustomerEventResponse( $error ); } From 97b3141e8a97721e78482828cf981bf2dc7229bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 10:52:19 +0300 Subject: [PATCH 10/29] use existing contact contract --- omnisend/includes/SDK/V1/class-contact.php | 146 ++++++++++++++++-- omnisend/includes/SDK/V1/class-event.php | 7 +- .../includes/SDK/V1/class-eventcontact.php | 122 --------------- 3 files changed, 139 insertions(+), 136 deletions(-) delete mode 100644 omnisend/includes/SDK/V1/class-eventcontact.php diff --git a/omnisend/includes/SDK/V1/class-contact.php b/omnisend/includes/SDK/V1/class-contact.php index 01c037d..3aad778 100644 --- a/omnisend/includes/SDK/V1/class-contact.php +++ b/omnisend/includes/SDK/V1/class-contact.php @@ -17,6 +17,8 @@ * */ class Contact { + + private $id = null; private $first_name = null; private $last_name = null; private $email = null; @@ -81,8 +83,8 @@ public function validate(): WP_Error { $error->add( 'send_welcome_email', 'Not a valid boolean.' ); } - if ( $this->phone == null && $this->email == null ) { - $error->add( 'identifier', 'Phone or email must be set.' ); + if ( $this->phone == null && $this->email == null && $this->id == null ) { + $error->add( 'identifier', 'Phone, email or ID must be set.' ); } if ( $this->gender != null && ! in_array( $this->gender, array( 'm', 'f' ) ) ) { @@ -175,6 +177,10 @@ public function to_array(): array { $arr['identifiers'][] = $phone_identifier; } + if ( $this->id ) { + $arr['contactID'] = $this->id; + } + if ( $this->first_name ) { $arr['firstName'] = $this->first_name; } @@ -219,6 +225,111 @@ public function to_array(): array { } + /** + * Convert contact to array for events. + * + * If contact is valid it will be transformed to array that can be sent to Omnisend while creating event. + * + * @return array + */ + public function to_array_for_event(): array { + if ( $this->validate()->has_errors() ) { + return array(); + } + + $time_now = gmdate( 'c' ); + + $user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? 'user agent not found' ) ); + $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? 'ip not found' ) ); + + $arr = array( + 'consents' => array(), + 'tags' => array_values( array_unique( $this->tags ) ), + ); + + if ( $this->email ) { + if ( $this->email_consent ) { + $email_consent['consent'] = array( + 'channel' => 'email', + 'source' => $this->email_consent, + 'createdAt' => $time_now, + 'ip' => $ip, + 'userAgent' => $user_agent, + ); + } + + $arr['consents'][] = $email_consent; + } + + if ( $this->custom_properties ) { + $arr['customProperties'] = $this->custom_properties; + } + + if ( $this->phone ) { + if ( $this->phone_consent ) { + $phone_consent['consent'] = array( + 'channel' => 'phone', + 'source' => $this->phone_consent, + 'createdAt' => $time_now, + 'ip' => $ip, + 'userAgent' => $user_agent, + ); + } + $arr['consents'][] = $phone_consent; + } + + if ( $this->email ) { + $arr['email'] = $this->email; + } + + if ( $this->phone ) { + $arr['phone'] = $this->phone; + } + + if ( $this->id ) { + $arr['id'] = $this->id; + } + + if ( $this->first_name ) { + $arr['firstName'] = $this->first_name; + } + + if ( $this->last_name ) { + $arr['lastName'] = $this->last_name; + } + + if ( $this->address ) { + $arr['address'] = $this->address; + } + + if ( $this->city ) { + $arr['city'] = $this->city; + } + + if ( $this->state ) { + $arr['state'] = $this->state; + } + + if ( $this->country ) { + $arr['country'] = $this->country; + } + + if ( $this->postal_code ) { + $arr['postalCode'] = $this->postal_code; + } + + if ( $this->birthday ) { + $arr['birthdate'] = $this->birthday; + } + + if ( $this->gender ) { + $arr['gender'] = $this->gender; + } + + return $arr; + } + + /** * Sets contact email. * @@ -232,6 +343,19 @@ public function set_email( $email ): void { } } + /** + * Sets contact id. + * + * @param $id + * + * @return void + */ + public function set_id( $id ): void { + if ( $id && is_string( $id ) ) { + $this->id = $id; + } + } + /** * Sets contact gender. It can be "m" or "f". * @@ -400,15 +524,15 @@ public function set_email_consent( $consent_text ): void { $this->email_consent = $consent_text; } - /** - * Sets email concent status. It's needed for GDPR compliance. - * - * Common format is `form:form_name` or `popup:popup_name`. - * - * @param $consent_text - * - * @return void - */ + /** + * Sets email concent status. It's needed for GDPR compliance. + * + * Common format is `form:form_name` or `popup:popup_name`. + * + * @param $consent_text + * + * @return void + */ public function set_phone_consent( $consent_text ): void { $this->phone_consent = $consent_text; } diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index e795374..4455945 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -7,7 +7,7 @@ namespace Omnisend\SDK\V1; -use Omnisend\SDK\V1\EventContact; +use Omnisend\SDK\V1\Contact; use WP_Error; defined( 'ABSPATH' ) || die( 'no direct access' ); @@ -18,6 +18,7 @@ */ class Event { + private $contact = null; private $event_name = null; private $event_time = null; @@ -35,7 +36,7 @@ class Event { public function validate(): WP_Error { $error = new WP_Error(); - if ( $contact instanceof EventContact ) { + if ( $contact instanceof Contact ) { $error->merge_from( $contact->validate() ); } @@ -118,7 +119,7 @@ public function to_array(): array { $arr = array(); if ( $this->contact ) { - $arr['contact'] = $this->contact->to_array(); + $arr['contact'] = $this->contact->to_array_for_event(); } if ( $this->event_name ) { diff --git a/omnisend/includes/SDK/V1/class-eventcontact.php b/omnisend/includes/SDK/V1/class-eventcontact.php deleted file mode 100644 index 46dfd1e..0000000 --- a/omnisend/includes/SDK/V1/class-eventcontact.php +++ /dev/null @@ -1,122 +0,0 @@ -email != null && ! is_email( $this->email ) ) { - $error->add( 'email', 'Not a email.' ); - } - - if ( $this->phone == null && $this->email == null && $this->id == null ) { - $error->add( 'identifier', 'Phone or email or ID must be set.' ); - } - - $string_properties = array( - 'id', - 'email', - 'phone', - ); - - foreach ( $string_properties as $property ) { - if ( $this->$property != null && ! is_string( $this->$property ) ) { - $error->add( $property, 'Not a string.' ); - } - } - - return $error; - } - - /** - * Sets event name. - * - * @param $id - * - * @return void - */ - public function set_id( $id ): void { - $this->id = $id; - } - - /** - * Sets event time. - * - * @param $email - * - * @return void - */ - public function set_email( $email ): void { - $this->email = $email; - } - - - /** - * Sets event phone. - * - * @param $phone - * - * @return void - */ - public function set_phone( $phone ): void { - $this->phone = $phone; - } - - /** - * Convert event to array. - * - * If event is valid it will be transformed to array that can be sent to Omnisend. - * - * @return array - */ - public function to_array(): array { - if ( $this->validate()->has_errors() ) { - return array(); - } - - $arr = array(); - - if ( $this->id ) { - $arr['id'] = $this->id; - } - - if ( $this->email ) { - $arr['email'] = $this->email; - } - - if ( $this->phone ) { - $arr['phone'] = $this->phone; - } - - return $arr; - } -} From b1ca2a14f58ef29531ff1fbfa5125b5a58817c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 10:55:00 +0300 Subject: [PATCH 11/29] update readme --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7e93537..3ff5d00 100644 --- a/README.md +++ b/README.md @@ -74,21 +74,23 @@ Here is how you can create a basic client & submit contact. Here is how you can send customer events. ```php - $contact = new EventContact(); + $contact = new Contact(); $contact->set_email( $email ); $event = new Event(); - $event->set_contact($contact); - $event->set_origin('wordpress'); - $event->set_event_name('something hapened'); - $event->add_properties('importantProperty1', $importantProperty1); - $event->add_properties('importantProperty2', $importantProperty2); + $event->set_contact( $contact ); + $event->set_origin( 'appName' ); + $event->set_event_name( 'something hapened' ); + $event->add_properties( 'importantProperty1', $importantProperty1 ); + $event->add_properties( 'importantProperty2', $importantProperty2 ); $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); $response = $client->send_customer_event($event); ``` +You can send contact identifiers and if contact exists, then event will be attributed for this contact, if not - new contact will be created and event will be attributed to this new contact + #### Error handling If data provided is invalid or contact creation fails, then From c48d9f4cf35bebdacb384b8a50ad7e3cddfd7c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 11:05:21 +0300 Subject: [PATCH 12/29] fix setting channel --- omnisend/includes/SDK/V1/class-contact.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-contact.php b/omnisend/includes/SDK/V1/class-contact.php index 3aad778..37c159c 100644 --- a/omnisend/includes/SDK/V1/class-contact.php +++ b/omnisend/includes/SDK/V1/class-contact.php @@ -18,6 +18,7 @@ */ class Contact { + private $id = null; private $first_name = null; private $last_name = null; @@ -249,16 +250,16 @@ public function to_array_for_event(): array { if ( $this->email ) { if ( $this->email_consent ) { - $email_consent['consent'] = array( + $email_cahnnel_consent = array( 'channel' => 'email', 'source' => $this->email_consent, 'createdAt' => $time_now, 'ip' => $ip, 'userAgent' => $user_agent, ); - } - $arr['consents'][] = $email_consent; + $arr['consents'][] = $email_cahnnel_consent; + } } if ( $this->custom_properties ) { @@ -267,15 +268,15 @@ public function to_array_for_event(): array { if ( $this->phone ) { if ( $this->phone_consent ) { - $phone_consent['consent'] = array( + $phone_channel_consent = array( 'channel' => 'phone', 'source' => $this->phone_consent, 'createdAt' => $time_now, 'ip' => $ip, 'userAgent' => $user_agent, ); + $arr['consents'][] = $phone_channel_consent; } - $arr['consents'][] = $phone_consent; } if ( $this->email ) { From 90dbcf2ed939765ba5d12c6ab20268c63129ba8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 11:18:15 +0300 Subject: [PATCH 13/29] pass optin source --- omnisend/includes/SDK/V1/class-contact.php | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-contact.php b/omnisend/includes/SDK/V1/class-contact.php index 37c159c..95d5463 100644 --- a/omnisend/includes/SDK/V1/class-contact.php +++ b/omnisend/includes/SDK/V1/class-contact.php @@ -19,6 +19,7 @@ class Contact { + private $id = null; private $first_name = null; private $last_name = null; @@ -245,10 +246,13 @@ public function to_array_for_event(): array { $arr = array( 'consents' => array(), + 'optIns' => array(), 'tags' => array_values( array_unique( $this->tags ) ), ); if ( $this->email ) { + $arr['email'] = $this->email; + if ( $this->email_consent ) { $email_cahnnel_consent = array( 'channel' => 'email', @@ -260,6 +264,16 @@ public function to_array_for_event(): array { $arr['consents'][] = $email_cahnnel_consent; } + + if ( $this->email_opt_in_source ) { + $email_cahnnel_opt_in = array( + 'channel' => 'email', + 'createdAt' => $time_now, + 'source' => $this->email_opt_in_source, + ); + + $arr['optIns'][] = $email_cahnnel_opt_in; + } } if ( $this->custom_properties ) { @@ -267,6 +281,8 @@ public function to_array_for_event(): array { } if ( $this->phone ) { + $arr['phone'] = $this->phone; + if ( $this->phone_consent ) { $phone_channel_consent = array( 'channel' => 'phone', @@ -275,16 +291,18 @@ public function to_array_for_event(): array { 'ip' => $ip, 'userAgent' => $user_agent, ); - $arr['consents'][] = $phone_channel_consent; + $arr['consents'][] = $phone_channel_consent; } - } - if ( $this->email ) { - $arr['email'] = $this->email; - } + if ( $this->phone_opt_in_source ) { + $phone_cahnnel_opt_in = array( + 'channel' => 'phone', + 'createdAt' => $time_now, + 'source' => $this->phone_opt_in_source, + ); - if ( $this->phone ) { - $arr['phone'] = $this->phone; + $arr['optIns'][] = $phone_cahnnel_opt_in; + } } if ( $this->id ) { From cfc83b0799d242c3c865b6c42247ae4194c0c27d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 11:21:26 +0300 Subject: [PATCH 14/29] use contacts v5 --- omnisend/includes/Internal/V1/class-client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index f630cdc..d17b690 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -52,7 +52,7 @@ public function create_contact( $contact ): CreateContactResponse { } $response = wp_remote_post( - OMNISEND_CORE_API_V3 . '/contacts', + OMNISEND_CORE_API_V5 . '/contacts', array( 'body' => wp_json_encode( $contact->to_array() ), 'headers' => array( From 99618b5fddbc7e9091bc2a54c338eca9cb0534dc Mon Sep 17 00:00:00 2001 From: Greta Mikneviciute <100136409+greta-mik@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:26:54 +0300 Subject: [PATCH 15/29] Update README.md Co-authored-by: nerijuszaniauskas <98449470+nerijuszaniauskas@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3ff5d00..4c1c918 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The plugin provides an SDK client to easily integrate with Omnisend API. > To use in your plugin you must check if wp-omnisend plugin is installed. > Provided client will send data to Omnisend if it is connected. -You can find function references in the [client folder](https://github.com/omnisend/wp-omnisend/tree/events-support/omnisend/includes/SDK/V1). +You can find function references in the [client folder](https://github.com/omnisend/wp-omnisend/tree/main/omnisend/includes/SDK/V1). ### Examples From 2a9fc83b17d00a11c885b16401c87d4e35c165d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 11:30:33 +0300 Subject: [PATCH 16/29] add more validation --- omnisend/includes/SDK/V1/class-client.php | 3 ++- omnisend/includes/SDK/V1/class-event.php | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index a60afa6..7488edb 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -17,6 +17,7 @@ interface Client { + /** * Create a contact in Omnisend. For it to succeed ensure that provided contact at least have email or phone number. * @@ -27,7 +28,7 @@ interface Client { public function create_contact( $contact ): CreateContactResponse; /** - * Send customer event in Omnisend. Customer events are used to track customer behavior and trigger automations based on that behavior. + * Send customer event to Omnisend. Customer events are used to track customer behavior and trigger automations based on that behavior. * * @param Event $event * diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 4455945..7f337d6 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -28,7 +28,6 @@ class Event { /** * Validate event properties. * - * TODO kas yra reuquired? ka reikia vailiduoti? * It ensures that all required properties are set * * @return WP_Error @@ -40,6 +39,27 @@ public function validate(): WP_Error { $error->merge_from( $contact->validate() ); } + if ( $this->$event_name == null ) { + $error->add( 'event_name', 'Is required.' ); + } + + if ( $this->$event_name != null && ! is_string( $this->$event_name ) ) { + $error->add( $event_name, 'Not a string.' ); + } + + if ( $this->$origin != null && ! is_string( $this->$origin ) ) { + $error->add( $origin, 'Not a string.' ); + } + + foreach ( $properties as $name => $value ) { + if ( ! is_string( $name ) ) { + $error->add( $name, 'Not a string.' ); + } + if ( ! is_string( $value ) ) { + $error->add( $value, 'Not a string.' ); + } + } + return $error; } From 07ff18ad419f13777bd55df8f2a3b1554a49b397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 13:25:28 +0300 Subject: [PATCH 17/29] add event version --- omnisend/includes/SDK/V1/class-event.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 7f337d6..9ef8e6a 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -22,6 +22,7 @@ class Event { private $contact = null; private $event_name = null; private $event_time = null; + private $event_version = null; private $origin = null; private array $properties = array(); @@ -47,6 +48,10 @@ public function validate(): WP_Error { $error->add( $event_name, 'Not a string.' ); } + if ( $this->$event_version != null && ! is_string( $this->$event_version ) ) { + $error->add( $event_version, 'Not a string.' ); + } + if ( $this->$origin != null && ! is_string( $this->$origin ) ) { $error->add( $origin, 'Not a string.' ); } @@ -85,6 +90,17 @@ public function set_event_time( $event_time ): void { $this->event_time = $event_time; } + /** + * Sets event version. + * + * @param $event_version + * + * @return void + */ + public function set_event_version( $event_version ): void { + $this->event_version = $event_version; + } + /** * Sets event origin. @@ -156,6 +172,10 @@ public function to_array(): array { $arr['origin'] = $this->origin; } + if ( $this->event_version ) { + $arr['eventVersion'] = $this->event_version; + } + if ( $this->properties ) { $arr['properties'] = $this->properties; } From 2db3e575a368b8f9da327f0a602c46a4f025846f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 13:28:22 +0300 Subject: [PATCH 18/29] update readme --- README.md | 60 +++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 4c1c918..dc09eeb 100644 --- a/README.md +++ b/README.md @@ -46,27 +46,27 @@ This is done by getting an actual client Here is how you can create a basic client & submit contact. ```php - $contact = new Contact(); - - $contact->set_email( $email ); - if ( $phone_number != '' ) { - $contact->set_phone( $phone_number ); - } - $contact->set_first_name( $first_name ); - $contact->set_last_name( $last_name ); - $contact->set_birthday( $birthday ); - $contact->set_postal_code( $postal_code ); - $contact->set_address( $address ); - $contact->set_state( $state ); - $contact->set_country( $country ); - $contact->set_city( $city ); - if ( $email_consent ) { - $contact->set_email_consent( 'actual_email_consent_for_gdrp' ); - $contact->set_email_opt_in( 'where user opted to become subscriber' ); - } - $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); +$contact = new Contact(); + +$contact->set_email( $email ); +if ( $phone_number != '' ) { + $contact->set_phone( $phone_number ); +} +$contact->set_first_name( $first_name ); +$contact->set_last_name( $last_name ); +$contact->set_birthday( $birthday ); +$contact->set_postal_code( $postal_code ); +$contact->set_address( $address ); +$contact->set_state( $state ); +$contact->set_country( $country ); +$contact->set_city( $city ); +if ( $email_consent ) { + $contact->set_email_consent( 'actual_email_consent_for_gdrp' ); + $contact->set_email_opt_in( 'where user opted to become subscriber' ); +} +$client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); - $response = $client->create_contact( $contact ); +$response = $client->create_contact( $contact ); ``` #### Customer events @@ -74,19 +74,19 @@ Here is how you can create a basic client & submit contact. Here is how you can send customer events. ```php - $contact = new Contact(); - $contact->set_email( $email ); +$contact = new Contact(); +$contact->set_email( $email ); - $event = new Event(); - $event->set_contact( $contact ); - $event->set_origin( 'appName' ); - $event->set_event_name( 'something hapened' ); - $event->add_properties( 'importantProperty1', $importantProperty1 ); - $event->add_properties( 'importantProperty2', $importantProperty2 ); +$event = new Event(); +$event->set_contact( $contact ); +$event->set_origin( 'appName' ); +$event->set_event_name( 'something hapened' ); +$event->add_properties( 'importantProperty1', $importantProperty1 ); +$event->add_properties( 'importantProperty2', $importantProperty2 ); - $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); +$client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); - $response = $client->send_customer_event($event); +$response = $client->send_customer_event($event); ``` You can send contact identifiers and if contact exists, then event will be attributed for this contact, if not - new contact will be created and event will be attributed to this new contact From 8ae2dcd91c7beda9ae5c48e907ee71323da84c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 13:30:21 +0300 Subject: [PATCH 19/29] review fixes --- omnisend/includes/SDK/V1/class-client.php | 3 --- omnisend/includes/SDK/V1/class-event.php | 4 ---- 2 files changed, 7 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index 7488edb..a3c3e6a 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -15,9 +15,6 @@ */ interface Client { - - - /** * Create a contact in Omnisend. For it to succeed ensure that provided contact at least have email or phone number. * diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 9ef8e6a..987e8a9 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -150,8 +150,6 @@ public function to_array(): array { return array(); } - $time_now = gmdate( 'c' ); - $arr = array(); if ( $this->contact ) { @@ -164,8 +162,6 @@ public function to_array(): array { if ( $this->event_time ) { $arr['eventTime'] = $this->event_time; - } else { - $arr['eventTime'] = $this->$time_now; } if ( $this->origin ) { From defa97c78baee997c43c70f57dd87f443ee74b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:00:15 +0300 Subject: [PATCH 20/29] validate event time --- omnisend/includes/SDK/V1/class-event.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 987e8a9..c324463 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -56,6 +56,13 @@ public function validate(): WP_Error { $error->add( $origin, 'Not a string.' ); } + if ( $this->$event_time != null ) { + $dt = DateTime::createFromFormat( 'Y-m-d', $event_time ); + if ( ! $dt ) { + $error->add( $event_time, 'Not valid date format.' ); + } + } + foreach ( $properties as $name => $value ) { if ( ! is_string( $name ) ) { $error->add( $name, 'Not a string.' ); @@ -80,7 +87,7 @@ public function set_event_name( $event_name ): void { } /** - * Sets event time. + * Sets event time. Time should be in RFC3339 (example 2011-01-01T00:00:00Z) * * @param $event_time * From dbc362d66bae4d2de768ab7c5991fb890f32b846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:02:22 +0300 Subject: [PATCH 21/29] review fixes --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dc09eeb..83863d4 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,9 @@ $contact->set_email( $email ); $event = new Event(); $event->set_contact( $contact ); $event->set_origin( 'appName' ); -$event->set_event_name( 'something hapened' ); -$event->add_properties( 'importantProperty1', $importantProperty1 ); -$event->add_properties( 'importantProperty2', $importantProperty2 ); +$event->set_event_name( 'site viewed' ); +$event->add_properties( 'pageUrl', $pageUrl ); +$event->add_properties( 'pageTitle', $pageTitle ); $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' ); From d56c0d35335637b52610968cf6721fc66b0206ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:07:50 +0300 Subject: [PATCH 22/29] fixes --- omnisend/includes/SDK/V1/class-event.php | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index c324463..18202b7 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -1,4 +1,5 @@ merge_from( $contact->validate() ); + if ( $this->contact instanceof Contact ) { + $error->merge_from( $this->contact->validate() ); } - if ( $this->$event_name == null ) { + if ( $this->event_name == null ) { $error->add( 'event_name', 'Is required.' ); } - if ( $this->$event_name != null && ! is_string( $this->$event_name ) ) { - $error->add( $event_name, 'Not a string.' ); + if ( $this->event_name != null && ! is_string( $this->event_name ) ) { + $error->add( 'event_name', 'Not a string.' ); } - if ( $this->$event_version != null && ! is_string( $this->$event_version ) ) { - $error->add( $event_version, 'Not a string.' ); + if ( $this->event_version != null && ! is_string( $this->event_version ) ) { + $error->add( 'event_version', 'Not a string.' ); } - if ( $this->$origin != null && ! is_string( $this->$origin ) ) { - $error->add( $origin, 'Not a string.' ); + if ( $this->origin != null && ! is_string( $this->origin ) ) { + $error->add( 'origin', 'Not a string.' ); } - if ( $this->$event_time != null ) { - $dt = DateTime::createFromFormat( 'Y-m-d', $event_time ); + if ( $this->event_time != null ) { + $dt = DateTime::createFromFormat( 'Y-m-d', $this->event_time ); if ( ! $dt ) { - $error->add( $event_time, 'Not valid date format.' ); + $error->add( 'event_time', 'Not valid date format.' ); } } - foreach ( $properties as $name => $value ) { + foreach ( $this->properties as $name => $value ) { if ( ! is_string( $name ) ) { $error->add( $name, 'Not a string.' ); } From 03603a491f03e0abf1a5de4cd8ecf4544732ed63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:18:58 +0300 Subject: [PATCH 23/29] origin is required --- omnisend/includes/SDK/V1/class-event.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 18202b7..fbc9b2d 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -54,6 +54,10 @@ public function validate(): WP_Error { $error->add( 'event_version', 'Not a string.' ); } + if ( $this->origin == null ) { + $error->add( 'origin', 'Is required.' ); + } + if ( $this->origin != null && ! is_string( $this->origin ) ) { $error->add( 'origin', 'Not a string.' ); } From 9e82bf19336f4740e2951c5b71c9103f498c34a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:20:44 +0300 Subject: [PATCH 24/29] lint --- omnisend/includes/SDK/V1/class-event.php | 1 - 1 file changed, 1 deletion(-) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index fbc9b2d..d93a464 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -1,5 +1,4 @@ Date: Wed, 3 Jul 2024 14:23:04 +0300 Subject: [PATCH 25/29] pass default value for origin --- omnisend/includes/SDK/V1/class-event.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index d93a464..d2a8bf9 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -53,10 +53,6 @@ public function validate(): WP_Error { $error->add( 'event_version', 'Not a string.' ); } - if ( $this->origin == null ) { - $error->add( 'origin', 'Is required.' ); - } - if ( $this->origin != null && ! is_string( $this->origin ) ) { $error->add( 'origin', 'Not a string.' ); } @@ -115,13 +111,16 @@ public function set_event_version( $event_version ): void { /** - * Sets event origin. + * Sets event origin. Default value is api * * @param $origin * * @return void */ public function set_origin( $origin ): void { + if ( $origin == null ) { + $this->origin = 'api'; + } $this->origin = $origin; } From 894ac35f5f3cff087804263303647e5fdd439463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:24:08 +0300 Subject: [PATCH 26/29] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 83863d4..924257f 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ $contact->set_email( $email ); $event = new Event(); $event->set_contact( $contact ); -$event->set_origin( 'appName' ); +$event->set_origin( 'api' ); $event->set_event_name( 'site viewed' ); $event->add_properties( 'pageUrl', $pageUrl ); $event->add_properties( 'pageTitle', $pageTitle ); From 723ef5d3400f3a4f3175aa762075806f79d78618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 14:38:33 +0300 Subject: [PATCH 27/29] origin default value fix --- omnisend/includes/SDK/V1/class-event.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index d2a8bf9..bbb6e42 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -20,6 +20,7 @@ class Event { + private $contact = null; private $event_name = null; private $event_time = null; @@ -118,9 +119,6 @@ public function set_event_version( $event_version ): void { * @return void */ public function set_origin( $origin ): void { - if ( $origin == null ) { - $this->origin = 'api'; - } $this->origin = $origin; } @@ -176,6 +174,9 @@ public function to_array(): array { } if ( $this->origin ) { + if ( $this->origin == null ) { + $this->origin = 'api'; + } $arr['origin'] = $this->origin; } From 9e6ab40f7ed5ea1819448cae633a0b262c69e6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 15:14:15 +0300 Subject: [PATCH 28/29] clean up --- omnisend/includes/SDK/V1/class-contact.php | 2 -- omnisend/includes/SDK/V1/class-event.php | 3 --- omnisend/includes/SDK/V1/class-sendcustomereventresponse.php | 1 - 3 files changed, 6 deletions(-) diff --git a/omnisend/includes/SDK/V1/class-contact.php b/omnisend/includes/SDK/V1/class-contact.php index 95d5463..488f859 100644 --- a/omnisend/includes/SDK/V1/class-contact.php +++ b/omnisend/includes/SDK/V1/class-contact.php @@ -18,8 +18,6 @@ */ class Contact { - - private $id = null; private $first_name = null; private $last_name = null; diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index bbb6e42..3831332 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -18,9 +18,6 @@ */ class Event { - - - private $contact = null; private $event_name = null; private $event_time = null; diff --git a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php index 5f6418d..d70ff68 100644 --- a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php +++ b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php @@ -13,7 +13,6 @@ class SendCustomerEventResponse { - private WP_Error $wp_error; /** From dd9b094dcc04a6b2d7128930fb7af9e7cf0dbda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greta=20Miknevic=CC=8Ciu=CC=84te=CC=87?= Date: Wed, 3 Jul 2024 15:35:14 +0300 Subject: [PATCH 29/29] review fixes --- README.md | 2 +- .../includes/Internal/V1/class-client.php | 1 - omnisend/includes/SDK/V1/class-event.php | 32 ++----------------- 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 924257f..f9fee6e 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ $contact->set_email( $email ); $event = new Event(); $event->set_contact( $contact ); $event->set_origin( 'api' ); -$event->set_event_name( 'site viewed' ); +$event->set_event_name( 'something hapened' ); $event->add_properties( 'pageUrl', $pageUrl ); $event->add_properties( 'pageTitle', $pageTitle ); diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index d17b690..1937251 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -133,7 +133,6 @@ public function send_customer_event( $event ): SendCustomerEventResponse { $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 new SendCustomerEventResponse( $error ); } return new SendCustomerEventResponse( $error ); diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 3831332..ece56bf 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -20,7 +20,6 @@ class Event { private $contact = null; private $event_name = null; - private $event_time = null; private $event_version = null; private $origin = null; private array $properties = array(); @@ -55,20 +54,10 @@ public function validate(): WP_Error { $error->add( 'origin', 'Not a string.' ); } - if ( $this->event_time != null ) { - $dt = DateTime::createFromFormat( 'Y-m-d', $this->event_time ); - if ( ! $dt ) { - $error->add( 'event_time', 'Not valid date format.' ); - } - } - - foreach ( $this->properties as $name => $value ) { + foreach ( $this->properties as $name ) { if ( ! is_string( $name ) ) { $error->add( $name, 'Not a string.' ); } - if ( ! is_string( $value ) ) { - $error->add( $value, 'Not a string.' ); - } } return $error; @@ -85,17 +74,6 @@ public function set_event_name( $event_name ): void { $this->event_name = $event_name; } - /** - * Sets event time. Time should be in RFC3339 (example 2011-01-01T00:00:00Z) - * - * @param $event_time - * - * @return void - */ - public function set_event_time( $event_time ): void { - $this->event_time = $event_time; - } - /** * Sets event version. * @@ -166,14 +144,8 @@ public function to_array(): array { $arr['eventName'] = $this->event_name; } - if ( $this->event_time ) { - $arr['eventTime'] = $this->event_time; - } - + $arr['origin'] = 'api'; if ( $this->origin ) { - if ( $this->origin == null ) { - $this->origin = 'api'; - } $arr['origin'] = $this->origin; }