Skip to content

Commit

Permalink
Add ability to connect platform
Browse files Browse the repository at this point in the history
  • Loading branch information
nerijuszaniauskas committed Dec 2, 2024
1 parent fb9696d commit bb402c4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
63 changes: 63 additions & 0 deletions omnisend/includes/Internal/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Omnisend\Internal\V1;

use Omnisend\SDK\V1\ConnectStoreResponse;
use Omnisend\SDK\V1\Contact;
use Omnisend\SDK\V1\CreateContactResponse;
use Omnisend\SDK\V1\Event;
Expand Down Expand Up @@ -242,6 +243,59 @@ public function send_customer_event( $event ): SendCustomerEventResponse {
return new SendCustomerEventResponse( $error );
}

public function connect_store( $platform ): ConnectStoreResponse {
$error = new WP_Error();
$error->merge_from( $this->check_setup() );

if ( ! is_string( $platform ) ) {
$error->add( 'platform', 'Platform must be string' );
}

$brand_id = $this->get_brand_id();
if ( ! $brand_id ) {
$error->add( 'brand_id', 'Unable to get brand_id. Please reinstall Omnisend plugin.' );
}

if ( $error->has_errors() ) {
return new ConnectStoreResponse( $error );
}

$data = array(
'website' => site_url(),
'platform' => $platform,
'version' => $this->plugin_version,
'phpVersion' => phpversion(),
'platformVersion' => get_bloginfo( 'version' ),
);

$response = wp_remote_post(
OMNISEND_CORE_API_V3 . '/accounts/' . $brand_id,
array(
'body' => wp_json_encode( $data ),
'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 ) ) {
return new ConnectStoreResponse( $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 ConnectStoreResponse( $error );
}

/**
* @return WP_Error
*/
Expand All @@ -262,4 +316,13 @@ private function check_setup(): WP_Error {

return $error;
}

private function get_brand_id(): string {
$list = explode( '-', $this->api_key );
if ( count( $list ) != 2 ) {
return '';
}

return $list[0];
}
}
10 changes: 10 additions & 0 deletions omnisend/includes/SDK/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public function save_contact( Contact $contact ): SaveContactResponse;
* @return GetContactResponse
*/
public function get_contact_by_email( string $email ): GetContactResponse;

/**
* Connect PHP based ecommerce platform/store to Omnisend.
*
* @param string $platform must be whitelisted (for additional added value) in Omnisend.
* If you're integrating new platform please contact [email protected]
*
* @return ConnectStoreResponse
*/
public function connect_store( string $platform ): ConnectStoreResponse;
}
28 changes: 28 additions & 0 deletions omnisend/includes/SDK/V1/class-connectstoreresponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\SDK\V1;

use WP_Error;

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

class ConnectStoreResponse {

private WP_Error $wp_error;

/**
* @param WP_Error $wp_error
*/
public function __construct( WP_Error $wp_error ) {
$this->wp_error = $wp_error;
}

public function get_wp_error(): WP_Error {
return $this->wp_error;
}
}

0 comments on commit bb402c4

Please sign in to comment.