Skip to content

Commit

Permalink
Merge pull request #14 from msilinskas/using_omnisend_sdk
Browse files Browse the repository at this point in the history
Using omnisend SDK
  • Loading branch information
nerijuszaniauskas authored Feb 19, 2024
2 parents 4c72d10 + 6c35cdb commit d8d4c5f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Omnisend for Ninja Forms Add-On
* Description: A ninja forms add-on to sync contacts with Omnisend. In collaboration with Omnisend for WooCommerce plugin it enables better customer tracking
* Version: 1.0.4
* Version: 1.1.0
* Author: Omnisend
* Author URI: https://omnisend.com
* Developer: Omnisend
Expand All @@ -21,7 +21,9 @@
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'OMNISEND_NINJA_ADDON_VERSION', '1.0.4' );

const OMNISEND_NINJA_ADDON_NAME = 'Omnisend for NINJA Forms Add-On';
const OMNISEND_NINJA_ADDON_VERSION = '1.1.0';

add_action( 'ninja_forms_register_actions', array( 'Omnisend_NinjaFormsAddOn_Bootstrap', 'register_actions' ), 10 );
spl_autoload_register( array( 'Omnisend_NinjaFormsAddOn_Bootstrap', 'autoloader' ) );
Expand All @@ -40,6 +42,10 @@ class Omnisend_NinjaFormsAddOn_Bootstrap {
* @return array The modified array of actions.
*/
public static function register_actions( $actions ) {
if ( ! class_exists( 'Omnisend\SDK\V1\Omnisend' ) ) {
return array();
}

$path_to_snippet = plugins_url( '/js/snippet.js', __FILE__ );
$actions['omnisend'] = new OmnisendAddOnAction( $path_to_snippet );

Expand Down Expand Up @@ -75,26 +81,32 @@ public static function autoloader( $class_name ) {
*/
public static function check_plugin_requirements() {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
$omnisend_plugin = 'omnisend-connect/omnisend-woocommerce.php';
$omnisend_plugin = 'omnisend/class-omnisend-core-bootstrap.php';

if ( ! file_exists( WP_PLUGIN_DIR . '/' . $omnisend_plugin ) || ! is_plugin_active( $omnisend_plugin ) ) {
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninja-forms-addon-bootstrap.php' );
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninjaformsaddon-bootstrap.php' );
add_action( 'admin_notices', array( 'Omnisend_NinjaFormsAddOn_Bootstrap', 'omnisend_woocommerce_notice' ) );

return;
}

$api_key = get_option( 'omnisend_api_key', null );
if ( is_null( $api_key ) ) {
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninja-forms-addon-bootstrap.php' );
if ( ! class_exists( 'Omnisend\SDK\V1\Omnisend' ) ) {
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninjaformsaddon-bootstrap.php' );
add_action( 'admin_notices', array( 'Omnisend_NinjaFormsAddOn_Bootstrap', 'omnisend_woocommerce_not_up_to_date' ) );

return;
}

if ( ! Omnisend\SDK\V1\Omnisend::is_connected() ) {
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninjaformsaddon-bootstrap.php' );
add_action( 'admin_notices', array( 'Omnisend_NinjaFormsAddOn_Bootstrap', 'omnisend_woocommerce_api_key_notice' ) );

return;
}

$ninja_forms_plugin = 'ninja-forms/ninja-forms.php';
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $ninja_forms_plugin ) || ! is_plugin_active( 'ninja-forms/ninja-forms.php' ) ) {
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninja-forms-addon-bootstrap.php' );
deactivate_plugins( 'omnisend-for-ninja-forms-add-on/class-omnisend-ninjaformsaddon-bootstrap.php' );
add_action( 'admin_notices', array( 'Omnisend_NinjaFormsAddOn_Bootstrap', 'ninja_forms_notice' ) );
}
}
Expand All @@ -110,7 +122,14 @@ public static function omnisend_woocommerce_api_key_notice() {
* Display a notice for the missing Omnisend WooCommerce plugin.
*/
public static function omnisend_woocommerce_notice() {
echo '<div class="error"><p>' . esc_html__( 'Plugin Omnisend for Ninja Forms Add-On is deactivated. Please install and activate ', 'ninja-forms' ) . '<a href="https://wordpress.org/plugins/omnisend-connect/">' . esc_html__( 'Omnisend for Woocommerce plugin.', 'ninja-forms' ) . '</a></p></div>';
echo '<div class="error"><p>' . esc_html__( 'Plugin Omnisend for Ninja Forms Add-On is deactivated. Please install and activate ', 'ninja-forms' ) . '<a href="https://wordpress.org/plugins/omnisend/">' . esc_html__( 'Email Marketing by Omnisend.', 'ninja-forms' ) . '</a></p></div>';
}

/**
* Display a notice for not up to date omnisend plugin.
*/
public static function omnisend_woocommerce_not_up_to_date() {
echo '<div class="error"><p>' . esc_html__( 'Your Email Marketing by Omnisend is not up to date. Please update plugins ', 'ninja-forms' ) . '<a href="https://wordpress.org/plugins/omnisend/">' . esc_html__( 'Omnisend for Woocommerce plugin.', 'ninja-forms' ) . '</a></p></div>';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Omnisend\NinjaFormsAddon\Builder;

use Omnisend\NinjaFormsAddon\Actions\OmnisendAddOnAction;
use Omnisend\SDK\V1\Contact;

/**
* Class RequestBodyBuilder
Expand All @@ -22,26 +23,20 @@ class RequestBodyBuilder {
*
* @param array $mapped_fields The mapped fields.
* @param string $form_name The form name.
* @return array The request body.
* @return Contact|null The request body.
*/
public function get_body( array $mapped_fields, string $form_name ): array {
public function get_body( array $mapped_fields, string $form_name ): ?Contact {
$form_name = preg_replace( self::FORM_NAME_REGEXP, '', $form_name );
$email_consent = 'nonSubscribed';
$phone_consent = 'nonSubscribed';
$contact = new Contact();

if ( ! isset( $_SERVER['REMOTE_ADDR'] ) && ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
error_log( 'Unable to fetch REMOTE_ADDR and HTTP_USER_AGENT.' );

return array();
return null;
}

$consent_object = array(
'source' => 'ninja-forms-' . $form_name,
'createdAt' => gmdate( 'c' ),
'ip' => sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ),
'userAgent' => sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ),
);

$fields_to_process = array(
OmnisendAddOnAction::EMAIL,
OmnisendAddOnAction::ADDRESS,
Expand Down Expand Up @@ -82,44 +77,24 @@ public function get_body( array $mapped_fields, string $form_name ): array {
return array();
}

$identifiers = array();
if ( '' !== $email ) {
$email_identifier = array(
'type' => 'email',
'channels' => array(
'email' => array(
'status' => $email_consent,
'statusDate' => gmdate( 'c' ),
),
),
'id' => $email,
);

if ( 'subscribed' === $email_consent ) {
$email_identifier['consent'] = $consent_object;
}
array_push( $identifiers, $email_identifier );
if ( '' === $email ) {
return null;
}

$contact->set_email( $email );
if ( 'subscribed' === $email_consent ) {
$contact->set_email_consent( 'ninja-forms-' . $form_name );
$contact->set_email_opt_in( 'ninja-forms-' . $form_name );
}

if ( '' !== $phone_number ) {
$phone_identifier = array(
'type' => 'phone',
'channels' => array(
'sms' => array(
'status' => $phone_consent,
'statusDate' => gmdate( 'c' ),
),
),
'id' => $phone_number,
);
$contact->set_phone( $phone_number );
if ( 'subscribed' === $phone_consent ) {
$phone_identifier['consent'] = $consent_object;
$contact->set_phone_consent( 'ninja-forms-' . $form_name );
$contact->set_phone_opt_in( 'ninja-forms-' . $form_name );
}

array_push( $identifiers, $phone_identifier );
}

$data = array( 'identifiers' => $identifiers );
$fields_to_data_keys = array(
'first_name' => 'firstName',
'last_name' => 'lastName',
Expand All @@ -133,20 +108,22 @@ public function get_body( array $mapped_fields, string $form_name ): array {

foreach ( $fields_to_data_keys as $variable => $data_key ) {
if ( ! empty( $$variable ) ) {
$data[ $data_key ] = $$variable;
$method = 'set_' . $variable;
$contact->$method( $$variable );
}
}

if ( isset( $mapped_fields['sendWelcomeEmail'] ) ) {
$data['sendWelcomeEmail'] = $mapped_fields['sendWelcomeEmail'];
$contact->set_welcome_email( $mapped_fields['sendWelcomeEmail'] );
}

$data['tags'] = array( 'ninja_forms', 'ninja_forms ' . $form_name );
$contact->add_tag( 'ninja_forms' );
$contact->add_tag( 'ninja_forms ' . $form_name );

if ( ! empty( $mapped_fields['customFields'] ) ) {
$data['customProperties'] = (object) $mapped_fields['customFields'];
foreach ( $mapped_fields['customFields'] as $field_name => $field_value ) {
$contact->add_custom_property( $field_name, $field_value );
}

return $data;
return $contact;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

namespace Omnisend\NinjaFormsAddon\Service;

use Omnisend\Internal\V1\Client;
use Omnisend\NinjaFormsAddon\Actions\OmnisendAddOnAction;
use Omnisend\NinjaFormsAddon\Builder\RequestBodyBuilder;
use Omnisend\NinjaFormsAddon\Client\OmnisendApiClient;
use Omnisend\NinjaFormsAddon\Factory\OmnisendResponseFactory;
use Omnisend\NinjaFormsAddon\Mapper\FormFieldsMapper;
use Omnisend\NinjaFormsAddon\OmnisendResponse;
use Omnisend\NinjaFormsAddon\Validator\ResponseValidator;
use Omnisend\SDK\V1\Omnisend;

/**
* Omnisend API Service.
Expand All @@ -25,7 +26,7 @@ class OmnisendApiService {
/**
* Omnisend API client.
*
* @var OmnisendApiClient
* @var Client
*/
private $client;

Expand Down Expand Up @@ -62,10 +63,13 @@ class OmnisendApiService {
*/
public function __construct() {
$this->fields_mapper = new FormFieldsMapper();
$this->client = new OmnisendApiClient();
$this->body_builder = new RequestBodyBuilder();
$this->response_factory = new OmnisendResponseFactory();
$this->response_validator = new ResponseValidator();
$this->client = Omnisend::get_client(
OMNISEND_NINJA_ADDON_NAME,
OMNISEND_NINJA_ADDON_VERSION
);
}

/**
Expand All @@ -78,13 +82,12 @@ public function __construct() {
*/
public function create_omnisend_contact( string $form_name, array $action_settings, array $form_data ): OmnisendResponse {
$mapped_fields_values = $this->fields_mapper->get_field_mappings( $action_settings, $form_data );
$body = $this->body_builder->get_body( $mapped_fields_values, $form_name );
$contact = $this->body_builder->get_body( $mapped_fields_values, $form_name );

if ( empty( $body ) ) {
if ( null === $contact ) {
return $this->response_factory->create( false );
}

$response = $this->client->create_omnisend_contact( $body );
$response = $this->client->create_contact( $contact );

if ( ! $this->response_validator->validate_response( $response ) ) {
return $this->response_factory->create( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Omnisend\NinjaFormsAddon\Validator;

use Omnisend\SDK\V1\CreateContactResponse;

/**
* Class ResponseValidator
*
Expand All @@ -21,25 +23,19 @@ class ResponseValidator {
/**
* Validates the API response.
*
* @param mixed $response The API response.
* @param CreateContactResponse $response The API response.
* @return bool True if the response is valid, false otherwise.
*/
public function validate_response( $response ): bool {
if ( is_wp_error( $response ) ) {
public function validate_response( CreateContactResponse $response ): bool {
if ( $response->get_wp_error()->has_errors() ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'wp_remote_post error: ' . $response->get_error_message() );
}

return false;
}

$http_code = wp_remote_retrieve_response_code( $response );
if ( $http_code >= 400 ) {
$body = wp_remote_retrieve_body( $response );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}" );
}

if ( empty( $response->get_contact_id() ) ) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion omnisend-for-ninja-forms/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: Ninja forms, form, email marketing, web tracking, subscriber collection
Requires at least: 4.7.0
Tested up to: 6.4
Requires PHP: 7.1
Stable tag: 1.0.4
Stable tag: 1.1.0
License: GPLv3 or later
URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand Down

0 comments on commit d8d4c5f

Please sign in to comment.