Skip to content

Commit

Permalink
Merge pull request #24 from omnisend/sdk-improvements
Browse files Browse the repository at this point in the history
Sdk improvements
  • Loading branch information
nerijuszaniauskas authored Feb 13, 2024
2 parents 5b95efd + 7175858 commit 8df7e0e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 31 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ You can find function references in the [client folder](https://github.com/omnis

Before using Omnisend Client you need to ensure the following conditions:
* Omnisend Plugin is installed `is_plugin_active( 'omnisend/class-omnisend-core-bootstrap.php' )`
* Omnisend Plugin is up to date `class_exists( 'Omnisend\Public\V1\Omnisend' )`
* Omnisend is connected to the WordPress `Omnisend\Public\V1\Omnisend::is_connected()`
* Omnisend Plugin is up to date `class_exists( 'Omnisend\SDK\V1\Omnisend' )`
* Omnisend is connected to account `Omnisend\SDK\V1\Omnisend::is_connected()`

If any of these conditions are false you should ask to resolve them.

Expand All @@ -36,7 +36,7 @@ To send contact to the Omnisend you need to provide your integration name & vers

This is done by getting an actual client

` $client = \Omnisend\Public\V1\Omnisend::get_client( 'integration name', 'integration version' );`
` $client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' );`

'integration name' - should be your integration name
'integration version' - should be your integration version
Expand Down Expand Up @@ -64,7 +64,7 @@ Here is how you can create a basic client & submit contact.
$contact->set_email_opt_in( 'where user opted to become subscriber' );
}

$client = \Omnisend\Public\V1\Omnisend::get_client( 'integration name', 'integration version' );
$client = \Omnisend\SDK\V1\Omnisend::get_client( 'integration name', 'integration version' );

$response = $client->create_contact( $contact );
```
Expand All @@ -74,15 +74,15 @@ Here is how you can create a basic client & submit contact.
If data provided is invalid or creation fails, then

```php
$client->create_contact($contact)
$response = $client->create_contact($contact)
```

Will return `errors` in `WP_Error`. Depending on your integration logic you should handle the error i.e
Will return `CreateContactResponse`. Depending on your integration logic you should handle the error i.e

```php
if ( is_wp_error( $response ) ) {
error_log( 'Error in after_submission: ' . $response->get_error_message());
return;
if ( $response->get_wp_error()->has_errors() ) {
error_log( 'Error in after_submission: ' . $response->get_wp_error()->get_error_message());
return;
}
```

Expand Down
8 changes: 4 additions & 4 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
* Description: Omnisend main plugin that enables integration with Omnisend.
* Version: 1.1.4
* Version: 1.2.0
* Author: Omnisend
* Author URI: https://www.omnisend.com
* Developer: Omnisend
Expand All @@ -22,7 +22,7 @@

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

const OMNISEND_CORE_PLUGIN_VERSION = '1.1.4';
const OMNISEND_CORE_PLUGIN_VERSION = '1.2.0';
const OMNISEND_CORE_SETTINGS_PAGE = 'omnisend';
const OMNISEND_CORE_PLUGIN_NAME = 'Email Marketing by Omnisend';

Expand Down Expand Up @@ -71,13 +71,13 @@ public static function load_omnisend_admin_styles(): void {
'roboto.css',
plugin_dir_url( __FILE__ ) . 'assets/fonts/roboto/roboto.css?' . time(),
array(),
'1.1.4',
OMNISEND_CORE_PLUGIN_VERSION,
);
wp_enqueue_style(
'styles.css',
plugin_dir_url( __FILE__ ) . 'styles/styles.css?' . time(),
array(),
'1.1.4',
OMNISEND_CORE_PLUGIN_VERSION,
);
}
}
Expand Down
21 changes: 11 additions & 10 deletions omnisend/includes/Internal/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

namespace Omnisend\Internal\V1;

use Omnisend\Public\V1\Contact;
use Omnisend\SDK\V1\Contact;
use Omnisend\SDK\V1\CreateContactResponse;
use WP_Error;

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

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

private string $api_key;
private string $plugin_name;
Expand All @@ -30,19 +31,19 @@ public function __construct( string $api_key, string $plugin_name, string $plugi
}


public function create_contact( $contact ): mixed {
public function create_contact( $contact ): CreateContactResponse {
$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->add( 'contact', 'Contact is not instance of Omnisend\SDK\V1\Contact.' );
}

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

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

$response = wp_remote_post(
Expand All @@ -61,31 +62,31 @@ public function create_contact( $contact ): mixed {

if ( is_wp_error( $response ) ) {
error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore
return $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 $error;
return new CreateContactResponse( '', $error );
}

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

$arr = json_decode( $body, true );

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

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

/**
Expand Down
4 changes: 2 additions & 2 deletions omnisend/includes/Internal/class-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public static function display(): void {
return;
}

if ( $response['verified'] === true && $response['platform'] !== wordpress_platform ) {
if ( $response['verified'] === true && $response['platform'] !== $wordpress_platform ) {
echo '<div class="notice notice-error"><p>This Omnisend account is already connected to non-WordPress site. Log in to access it.
<a target="_blank" href="https://www.omnisend.com/customer-support/">Contact our support</a> if you have other issues.</p></div>';
require_once __DIR__ . '/../../view/connection-form.html';
return;
}

$connected = false;
if ( $response['platform'] === wordpress_platform ) {
if ( $response['platform'] === $wordpress_platform ) {
$connected = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package OmnisendClient
*/

namespace Omnisend\Public\V1;
namespace Omnisend\SDK\V1;

use WP_Error;

Expand All @@ -22,7 +22,7 @@ interface Client {
*
* @param Contact $contact
*
* @return string|WP_Error Created/updated contact identifier (ID) or WP_Error
* @return CreateContactResponse
*/
public function create_contact( $contact ): mixed;
public function create_contact( $contact ): CreateContactResponse;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package OmnisendClient
*/

namespace Omnisend\Public\V1;
namespace Omnisend\SDK\V1;

use Omnisend\Internal\Utils;
use WP_Error;
Expand Down
36 changes: 36 additions & 0 deletions omnisend/includes/SDK/V1/class-createcontactresponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\SDK\V1;

use WP_Error;

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

class CreateContactResponse {

private string $contact_id;

private WP_Error $wp_error;

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

public function get_contact_id(): string {
return $this->contact_id;
}

public function get_wp_error(): WP_Error {
return $this->wp_error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package OmnisendClient
*/

namespace Omnisend\Public\V1;
namespace Omnisend\SDK\V1;

use Omnisend\Internal\Options;

Expand Down
6 changes: 5 additions & 1 deletion omnisend/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: email marketing, marketing, newsletter, sms, form
Requires at least: 4.7.0
Tested up to: 6.4
Requires PHP: 7.1
Stable tag: 1.1.4
Stable tag: 1.2.0
License: GPLv3 or later
URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -54,6 +54,10 @@ Read Omnisend [Terms of Use](https://www.omnisend.com/terms)

== Changelog ==

= 1.2.0 =

* Improve SDK client

= 1.1.4 =

* Improve connection messages
Expand Down

0 comments on commit 8df7e0e

Please sign in to comment.