Skip to content

Commit

Permalink
Connect store
Browse files Browse the repository at this point in the history
  • Loading branch information
nerijuszaniauskas committed Jan 4, 2024
1 parent 57ec810 commit 92f8a4a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
6 changes: 5 additions & 1 deletion omnisend/class-omnisend-core-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
58 changes: 55 additions & 3 deletions omnisend/module/class-omnisend-core-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div class="notice notice-error"><p>API key is not valid</p></div>';
}
}

if ( Omnisend_Core_Options::get_api_key() ) {
if ( $connected ) {
echo 'You are connected to Omnisend!';
return;
}
Expand Down Expand Up @@ -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'] );
}
}
19 changes: 17 additions & 2 deletions omnisend/module/class-omnisend-core-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}
}
2 changes: 1 addition & 1 deletion omnisend/module/view/snippet.html
Original file line number Diff line number Diff line change
Expand Up @@ -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="<?php echo OMNISEND_CORE_SNIPPET_URL; ?>";
var t=document.getElementsByTagName("script")[0];
t.parentNode.insertBefore(e,t)}();
</script>

0 comments on commit 92f8a4a

Please sign in to comment.