diff --git a/omnisend/class-omnisend-core-bootstrap.php b/omnisend/class-omnisend-core-bootstrap.php
index f45976d..f83a64b 100644
--- a/omnisend/class-omnisend-core-bootstrap.php
+++ b/omnisend/class-omnisend-core-bootstrap.php
@@ -20,10 +20,14 @@
defined( 'ABSPATH' ) || exit;
+define( 'OMNISEND_CORE_PLUGIN_VERSION', '1.0.0' );
define( 'OMNISEND_CORE_SETTINGS_PAGE', 'omnisend' );
define( 'OMNISEND_CORE_PLUGIN_NAME', 'Omnisend Core' );
define( 'OMNISEND_CORE_WOOCOMMERCE_PLUGIN_NAME', 'Email Marketing for WooCommerce by Omnisend' );
-define( 'OMNISEND_CORE_API_V3', 'https://api.omnisend.com/v3' );
+
+// Change for different environment.
+define( 'OMNISEND_CORE_API_V3', 'https://api.omnisend.com/v3' ); // todo do not commit.
+define( 'OMNISEND_CORE_SNIPPET_URL', 'https://omnisnippet1.com/inshop/launcher-v2.js' );
require_once 'module/class-omnisend-core-connection.php';
require_once 'module/class-omnisend-core-options.php';
diff --git a/omnisend/module/class-omnisend-core-connection.php b/omnisend/module/class-omnisend-core-connection.php
index 30bb772..2dbbb17 100644
--- a/omnisend/module/class-omnisend-core-connection.php
+++ b/omnisend/module/class-omnisend-core-connection.php
@@ -10,19 +10,30 @@
class Omnisend_Core_Connection {
public static function display() {
- if ( ! empty( $_POST['action'] ) && 'connect' == $_POST['action'] && ! empty( $_POST['api_key'] ) && ! Omnisend_Core_Options::get_api_key() ) {
+ $connected = Omnisend_Core_Options::is_store_connected();
+
+ if ( ! $connected && ! empty( $_POST['action'] ) && 'connect' == $_POST['action'] && ! empty( $_POST['api_key'] ) ) {
check_admin_referer( 'connect' );
$api_key = sanitize_text_field( wp_unslash( $_POST['api_key'] ) );
$brand_id = self::get_brand_id( $api_key );
if ( $brand_id ) {
+ // Set credentials so snippet can be added for snippet verification.
Omnisend_Core_Options::set_api_key( $api_key );
Omnisend_Core_Options::set_brand_id( $brand_id );
- } else {
+
+ $connected = self::connect_store( $api_key );
+ if ( $connected ) {
+ Omnisend_Core_Options::set_store_connected();
+ }
+ }
+
+ if ( ! $connected ) {
+ Omnisend_Core_Options::disconnect(); // Store was not connected, clean up.
echo '
';
}
}
- if ( Omnisend_Core_Options::get_api_key() ) {
+ if ( $connected ) {
echo 'You are connected to Omnisend!';
return;
}
@@ -51,4 +62,45 @@ private static function get_brand_id( $api_key ): string {
return is_array( $arr ) && ! empty( $arr['brandID'] ) && is_string( $arr['brandID'] ) ? $arr['brandID'] : '';
}
+
+ private static function connect_store( $api_key ): bool {
+ $data = array(
+ 'website' => site_url(),
+ 'platform' => 'api',
+ 'version' => OMNISEND_CORE_PLUGIN_VERSION,
+ 'webserver' => 'Wordpress: Omnisend Core Plugin',
+ 'phpVersion' => phpversion(),
+ 'platformVersion' => get_bloginfo( 'version' ),
+ );
+
+ $response = wp_remote_post(
+ OMNISEND_CORE_API_V3 . '/accounts',
+ array(
+ 'body' => wp_json_encode( $data ),
+ 'headers' => array(
+ 'Content-Type' => 'application/json',
+ 'X-API-Key' => $api_key,
+ ),
+ 'timeout' => 10,
+ )
+ );
+
+ if ( is_wp_error( $response ) ) {
+ return false;
+ }
+
+ $http_code = wp_remote_retrieve_response_code( $response );
+ if ( $http_code >= 400 ) {
+ return false;
+ }
+
+ $body = wp_remote_retrieve_body( $response );
+ if ( ! $body ) {
+ return false;
+ }
+
+ $arr = json_decode( $body, true );
+
+ return ! empty( $arr['verified'] );
+ }
}
diff --git a/omnisend/module/class-omnisend-core-options.php b/omnisend/module/class-omnisend-core-options.php
index 793e290..3c1f7a8 100644
--- a/omnisend/module/class-omnisend-core-options.php
+++ b/omnisend/module/class-omnisend-core-options.php
@@ -9,8 +9,9 @@
class Omnisend_Core_Options {
- private const OPTION_API_KEY = 'omnisend_core_api_key';
- private const OPTION_BRAND_ID = 'omnisend_core_brand_id';
+ private const OPTION_API_KEY = 'omnisend_core_api_key';
+ private const OPTION_BRAND_ID = 'omnisend_core_brand_id';
+ private const OPTION_STORE_CONNECTED = 'omnisend_core_store_connected';
public static function get_api_key(): string {
$api_key = get_option( self::OPTION_API_KEY );
@@ -39,4 +40,18 @@ public static function set_brand_id( $brand_id ): bool {
return update_option( self::OPTION_BRAND_ID, $brand_id );
}
+
+ public static function set_store_connected(): bool {
+ return update_option( self::OPTION_STORE_CONNECTED, true );
+ }
+
+ public static function is_store_connected(): bool {
+ return boolval( get_option( self::OPTION_STORE_CONNECTED ) );
+ }
+
+ public static function disconnect() {
+ delete_option( self::OPTION_API_KEY );
+ delete_option( self::OPTION_BRAND_ID );
+ delete_option( self::OPTION_STORE_CONNECTED );
+ }
}
diff --git a/omnisend/module/view/snippet.html b/omnisend/module/view/snippet.html
index c7f7b7e..41bbaad 100644
--- a/omnisend/module/view/snippet.html
+++ b/omnisend/module/view/snippet.html
@@ -4,7 +4,7 @@
omnisend.push(["track", "$pageViewed"]);
!function(){var e=document.createElement("script");
e.type="text/javascript",e.async=!0,
- e.src="https://omnisnippet1.com/inshop/launcher-v2.js";
+ e.src="";
var t=document.getElementsByTagName("script")[0];
t.parentNode.insertBefore(e,t)}();