Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client usage tracking #15

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions omnisend/class-omnisend-core-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

// Change for different environment.
const OMNISEND_CORE_API_V3 = 'https://api.omnisend.com/v3';
Expand Down Expand Up @@ -53,7 +53,7 @@ public static function load(): void {

public static function add_admin_menu() {
$page_title = OMNISEND_CORE_PLUGIN_NAME;
$menu_title = OMNISEND_CORE_PLUGIN_NAME;
$menu_title = 'Omnisend';
$capability = 'manage_options';
$menu_slug = OMNISEND_CORE_SETTINGS_PAGE;
$function = 'Omnisend\Internal\Connection::display';
Expand Down
111 changes: 111 additions & 0 deletions omnisend/includes/Internal/V1/class-client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\Internal\V1;

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

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

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

private string $api_key;
private string $plugin_name;
private string $plugin_version;

/**
* @param string $plugin_name
* @param string $plugin_version
* @param string $api_key
*/
public function __construct( string $api_key, string $plugin_name, string $plugin_version ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe throw error here? User shouldn't encounter this error as dev will be forced to provide value

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try not to break client website. Throwing error could take all website down, I think we should avoid this. This class will not be created directly. Only trough Factory, where we ask to provide values, I think this should be enough to inform about this requirement.

$this->api_key = $api_key;
$this->plugin_name = substr( $plugin_name, 0, 50 );
$this->plugin_version = substr( $plugin_version, 0, 50 );
}


public function create_contact( $contact ): mixed {
$error = new WP_Error();

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

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

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

$response = wp_remote_post(
OMNISEND_CORE_API_V3 . '/contacts',
array(
'body' => wp_json_encode( $contact->to_array() ),
'headers' => array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->api_key,
'X-INTEGRATION-NAME' => $this->plugin_name,
'X-INTEGRATION-VERSION' => $this->plugin_version,
),
'timeout' => 10,
)
);

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

$http_code = wp_remote_retrieve_response_code( $response );
if ( $http_code >= 400 ) {
$body = wp_remote_retrieve_body( $response );
$err_msg = "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}";
$error->add( 'omnisend_api', $err_msg );
return $error;
}

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

$arr = json_decode( $body, true );

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

return (string) $arr['contactID'];
}

/**
* @return WP_Error
*/
private function check_setup(): WP_Error {
$error = new WP_Error();

if ( ! $this->plugin_name ) {
$error->add( 'initialisation', 'Client is created with empty plugin name' );
}

if ( ! $this->plugin_version ) {
$error->add( 'initialisation', 'Client is created with empty plugin version' );
}

if ( ! $this->api_key ) {
$error->add( 'api_key', 'Omnisend plugin is not connected' );
}

return $error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package OmnisendClient
*/

namespace Omnisend\Public\Client\V1;
namespace Omnisend\Internal;

! defined( 'ABSPATH' ) && die( 'no direct access' );

Expand Down
98 changes: 0 additions & 98 deletions omnisend/includes/Public/Client/V1/class-client.php

This file was deleted.

28 changes: 28 additions & 0 deletions omnisend/includes/Public/V1/class-client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\Public\V1;

use WP_Error;

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

/**
* Client to interact with Omnisend.
*
*/
interface Client {

/**
* Create a contact in Omnisend. For it to succeed ensure that provided contact at least have email or phone number.
*
* @param Contact $contact
*
* @return string|WP_Error Created/updated contact identifier (ID) or WP_Error
*/
public function create_contact( $contact ): mixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* @package OmnisendClient
*/

namespace Omnisend\Public\Client\V1;
namespace Omnisend\Public\V1;

use Omnisend\Internal\Utils;
use WP_Error;

defined( 'ABSPATH' ) || die( 'no direct access' );
Expand Down
36 changes: 36 additions & 0 deletions omnisend/includes/Public/V1/class-omnisend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\Public\V1;

use Omnisend\Internal\Options;

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

class Omnisend {

/**
* Factory to create Omnisend client.
*
* @param $plugin string plugin using client name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment what this function does

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

* @param $version string plugin using client version
* @return Client
*/
public static function get_client( $plugin, $version ): Client {
return new \Omnisend\Internal\V1\Client( Options::get_api_key(), (string) $plugin, (string) $version );
}

/**
* Check and return if plugin connected to Omnisend account. If connection does not exist, it will not be possible
* to send data to Omnisend.
*
* @return bool
*/
public static function is_connected(): bool {
return Options::get_api_key() != '';
}
}
2 changes: 1 addition & 1 deletion omnisend/readme.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=== Omnisend ===
Plugin Name: Omnisend
Plugin Name: Email Marketing by Omnisend
Contributors: Omnisend
Tags: form, email marketing, web tracking, subscriber collection
Requires at least: 4.7.0
Expand Down
Loading