Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
greta-mik committed Aug 9, 2024
1 parent e1369eb commit a98dd13
Show file tree
Hide file tree
Showing 12 changed files with 517 additions and 29 deletions.
5 changes: 4 additions & 1 deletion omnisend/build/appMarket.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-element'), 'version' => 'fbe230065258aef97727');
<?php return array(
'dependencies' => array( 'react', 'wp-components', 'wp-element' ),
'version' => 'fbe230065258aef97727',
);
5 changes: 4 additions & 1 deletion omnisend/build/connected.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-element'), 'version' => '9365906682f82f0695ac');
<?php return array(
'dependencies' => array( 'react', 'wp-components', 'wp-element' ),
'version' => '9365906682f82f0695ac',
);
5 changes: 4 additions & 1 deletion omnisend/build/connection.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<?php return array('dependencies' => array('react', 'wp-components', 'wp-element'), 'version' => '259b909fd9e4ab80b67c');
<?php return array(
'dependencies' => array( 'react', 'wp-components', 'wp-element' ),
'version' => '259b909fd9e4ab80b67c',
);
5 changes: 4 additions & 1 deletion omnisend/build/notices.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<?php return array('dependencies' => array('react', 'wp-element'), 'version' => 'a0217d8e38bd6f0b7cfa');
<?php return array(
'dependencies' => array( 'react', 'wp-element' ),
'version' => 'a0217d8e38bd6f0b7cfa',
);
4 changes: 2 additions & 2 deletions omnisend/class-omnisend-core-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Plugin Name: Omnisend Email Marketing
* Description: Omnisend main plugin that enables integration with Omnisend.
* Version: 1.4.1
* Version: 1.4.2
* Author: Omnisend
* Author URI: https://www.omnisend.com
* Developer: Omnisend
Expand All @@ -23,7 +23,7 @@

defined( 'ABSPATH' ) || die( 'no direct access' );

const OMNISEND_CORE_PLUGIN_VERSION = '1.4.1';
const OMNISEND_CORE_PLUGIN_VERSION = '1.4.2';
const OMNISEND_CORE_SETTINGS_PAGE = 'omnisend';
const OMNISEND_CORE_PLUGIN_NAME = 'Email Marketing by Omnisend';
const OMNISEND_MENU_TITLE = 'Omnisend Email Marketing';
Expand Down
128 changes: 124 additions & 4 deletions omnisend/includes/Internal/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
use Omnisend\SDK\V1\CreateContactResponse;
use Omnisend\SDK\V1\Event;
use Omnisend\SDK\V1\SendCustomerEventResponse;
use Omnisend\SDK\V1\SaveContactResponse;
use Omnisend\SDK\V1\GetContactResponse;
use WP_Error;

defined( 'ABSPATH' ) || die( 'no direct access' );

class Client implements \Omnisend\SDK\V1\Client {




private string $api_key;
private string $plugin_name;
private string $plugin_version;
Expand Down Expand Up @@ -94,6 +93,126 @@ public function create_contact( $contact ): CreateContactResponse {
return new CreateContactResponse( (string) $arr['contactID'], $error );
}

public function save_contact( Contact $contact ): SaveContactResponse {
$error = new WP_Error();

if ( $contact instanceof Contact ) {
$error->merge_from( $contact->validate() );
} else {
$error->add( 'contact', 'Contact is not instance of Omnisend\SDK\V1\Contact.' );
}

$error->merge_from( $this->check_setup() );

if ( $error->has_errors() ) {
return new SaveContactResponse( '', $error );
}

$contract_array = $contact->to_array();

$response = wp_remote_post(
OMNISEND_CORE_API_V5 . '/contacts',
array(
'body' => wp_json_encode( $contract_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() );
return new SaveContactResponse( '', $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 SaveContactResponse( '', $error );
}

$body = wp_remote_retrieve_body( $response );
if ( ! $body ) {
$error->add( 'omnisend_api', 'empty response' );
return new SaveContactResponse( '', $error );
}

$arr = json_decode( $body, true );

if ( empty( $arr['contactID'] ) ) {
$error->add( 'omnisend_api', 'contactID not found in response.' );
return new SaveContactResponse( '', $error );
}

return new SaveContactResponse( (string) $arr['contactID'], $error );
}

public function get_contact_by_email( string $email ): GetContactResponse {
$error = new WP_Error();

$response = wp_remote_get(
OMNISEND_CORE_API_V5 . '/contacts?email=' . $email,
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());

return new GetContactResponse( null, $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 GetContactResponse( null, $error );
}

$body = wp_remote_retrieve_body( $response );
if ( ! $body ) {
$error->add( 'omnisend_api', 'empty response' );

return new GetContactResponse( null, $error );
}

$contact_data = json_decode( $body, true );

if ( empty( $contact_data['contacts'][0]['contactID'] ) ) {
$error->add( 'omnisend_api', 'contactID not found in response.' );

return new GetContactResponse( null, $error );
}

$contact = ContactFactory::create_contact( $contact_data['contacts'][0] );

if ( ! isset( $contact_data['contacts'][0] ) ) {
$error->add( 'omnisend_api', 'empty contacts array' );

return new GetContactResponse( null, $error );
}
$contact_data = reset( $contact_data['contacts'] );
$contact = ( new ContactFactory() )->create_contact( $contact_data['contacts'][0] );

return new GetContactResponse( $contact, $error );
}

public function send_customer_event( $event ): SendCustomerEventResponse {
$error = new WP_Error();

Expand Down Expand Up @@ -124,7 +243,8 @@ public function send_customer_event( $event ): SendCustomerEventResponse {
);

if ( is_wp_error( $response ) ) {
error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore
error_log('wp_remote_post error: ' . $response->get_error_message());

return new SendCustomerEventResponse( $response );
}

Expand Down
99 changes: 99 additions & 0 deletions omnisend/includes/Internal/class-contactfactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\Internal\V1;

use Omnisend\SDK\V1\Contact;

class ContactFactory {

/**
* Create a Contact object from an array of contact data.
*
* @param array $contact_data
* @return Contact
*/
public static function create_contact( array $contact_data ): Contact {
$contact = new Contact();

if ( isset( $contact_data['contactID'] ) ) {
$contact->set_id( $contact_data['contactID'] );
}

if ( isset( $contact_data['firstName'] ) ) {
$contact->set_first_name( $contact_data['firstName'] );
}

if ( isset( $contact_data['email'] ) ) {
$contact->set_email( $contact_data['email'] );
$contact->set_email_opt_in( $contact_data['email'] );
}

if ( isset( $contact_data['lastName'] ) ) {
$contact->set_last_name( $contact_data['lastName'] );
}

if ( isset( $contact_data['country'] ) ) {
$contact->set_country( $contact_data['country'] );
}

if ( isset( $contact_data['address'] ) ) {
$contact->set_address( $contact_data['address'] );
}

if ( isset( $contact_data['city'] ) ) {
$contact->set_city( $contact_data['city'] );
}

if ( isset( $contact_data['state'] ) ) {
$contact->set_state( $contact_data['state'] );
}

if ( isset( $contact_data['postalCode'] ) ) {
$contact->set_postal_code( $contact_data['postalCode'] );
}

if ( isset( $contact_data['phone'] ) ) {
$contact->set_phone( $contact_data['phone'][0] );
$contact->set_phone_opt_in( $contact_data['phone'][0] );
}

if ( isset( $contact_data['birthdate'] ) ) {
$contact->set_birthday( $contact_data['birthdate'] );
}

if ( isset( $contact_data['gender'] ) ) {
$contact->set_gender( $contact_data['gender'] );
}

if ( isset( $contact_data['tags'] ) ) {
foreach ( $contact_data['tags'] as $tag ) {
$contact->add_tag( $tag );
}
}

if ( isset( $contact_data['customProperties'] ) ) {
foreach ( $contact_data['customProperties'] as $key => $value ) {
$contact->add_custom_property( $key, $value, false );
}
}

if ( isset( $contact_data['identifiers'] ) ) {
foreach ( $contact_data['identifiers'] as $single_consent ) {
if ( isset( $single_consent['channels']['sms']['status'] ) ) {
$contact->set_phone_status( $single_consent['channels']['sms']['status'] );
}

if ( isset( $single_consent['channels']['email']['status'] ) ) {
$contact->set_email_status( $single_consent['channels']['email']['status'] );
}
}
}

return $contact;
}
}
17 changes: 17 additions & 0 deletions omnisend/includes/SDK/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ public function create_contact( $contact ): CreateContactResponse;
* @return SendCustomerEventResponse
*/
public function send_customer_event( $event ): SendCustomerEventResponse;

/**
* Patch a contact in Omnisend.
* @param Contact $contact
*
* @return SaveContactResponse
*/
public function save_contact( Contact $contact ): SaveContactResponse;

/**
* Get contact in Omnisend by Email.
*
* @param string $email
*
* @return GetContactResponse
*/
public function get_contact_by_email( string $email ): GetContactResponse;
}
Loading

0 comments on commit a98dd13

Please sign in to comment.