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

Make core and woo plugin working together #8

Merged
merged 8 commits into from
Jan 25, 2024
Merged
22 changes: 11 additions & 11 deletions omnisend/class-omnisend-core-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
class Omnisend_Core_Bootstrap {

public static function load() {
if ( self::is_omnisend_woocommerce_plugin_active() || self::is_woocommerce_plugin_activate() ) {
return; // Do not load if "Omnisend for WooCommerce" or "WooCommerce" plugins are active.
}

// phpcs:disable WordPress.Security.NonceVerification
if ( isset( $_GET['page'] ) ) {
if ( in_array( $_GET['page'], array( 'omnisend' ), true ) ) {
Expand All @@ -62,7 +58,11 @@ public static function load() {
}

add_action( 'admin_menu', 'Omnisend_Core_Bootstrap::add_admin_menu' );
add_action( 'wp_footer', 'Omnisend_Core_Snippet::add' );
add_action( 'admin_init', 'Omnisend_Core_Connection::connect_with_omnisend_for_woo_plugin' );

if ( ! self::is_omnisend_woocommerce_plugin_active() || ! self::is_omnisend_woocommerce_plugin_connected() ) {
add_action( 'wp_footer', 'Omnisend_Core_Snippet::add' );
}
}

public static function add_admin_menu() {
Expand All @@ -77,19 +77,19 @@ public static function add_admin_menu() {
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $omnisend_icon, $position );
}



public static function admin_notices() {
if ( self::is_omnisend_woocommerce_plugin_active() ) {
echo '<div class="notice notice-error"><strong>' . esc_html( OMNISEND_CORE_PLUGIN_NAME ) . '</strong> plugin is not compatible with <strong>' . esc_html( OMNISEND_CORE_WOOCOMMERCE_PLUGIN_NAME ) . '</strong> plugin. Please use <strong>' . esc_html( OMNISEND_CORE_WOOCOMMERCE_PLUGIN_NAME ) . '</strong> plugin to connect to Omnisend.</p></div>';
} elseif ( self::is_woocommerce_plugin_activate() ) {
echo '<div class="notice notice-error"><strong>WooCommerce</strong> plugin is active. Please use <strong>' . esc_html( OMNISEND_CORE_WOOCOMMERCE_PLUGIN_NAME ) . '</strong> plugin instead of <strong>' . esc_html( OMNISEND_CORE_PLUGIN_NAME ) . '</strong> plugin.</p></div>';
if ( Omnisend_Core_Options::is_connected() && self::is_omnisend_woocommerce_plugin_active() && ! get_option( 'omnisend_api_key' ) ) {
nerijuszaniauskas marked this conversation as resolved.
Show resolved Hide resolved
echo '<div class="notice notice-error">If you want to use <strong>Omnisend for Woo</strong> plugin please contact customer support.</p></div>';
nerijuszaniauskas marked this conversation as resolved.
Show resolved Hide resolved
}
}

public static function is_omnisend_woocommerce_plugin_active(): bool {
return in_array( 'omnisend-connect/omnisend-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
}

public static function is_woocommerce_plugin_activate() {
return in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
public static function is_omnisend_woocommerce_plugin_connected(): bool {
return self::is_omnisend_woocommerce_plugin_active() && get_option( 'omnisend_account_id', null ) !== null;
}
}
24 changes: 24 additions & 0 deletions omnisend/module/class-omnisend-core-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,28 @@ private static function connect_store( $api_key ): bool {

return ! empty( $arr['verified'] );
}

public static function connect_with_omnisend_for_woo_plugin() {
if ( Omnisend_Core_Options::is_connected() ) {
return; // Already connected.
}

if ( ! Omnisend_Core_Bootstrap::is_omnisend_woocommerce_plugin_active() ) {
return;
}

$api_key = get_option( 'omnisend_api_key' );
if ( ! $api_key ) {
return;
}

$brand_id = self::get_brand_id( $api_key );
if ( ! $brand_id ) {
return;
}

Omnisend_Core_Options::set_api_key( $api_key );
Omnisend_Core_Options::set_brand_id( $brand_id );
Omnisend_Core_Options::set_store_connected();
}
}
4 changes: 4 additions & 0 deletions omnisend/module/class-omnisend-core-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public static function is_store_connected(): bool {
return boolval( get_option( self::OPTION_STORE_CONNECTED ) );
}

public static function is_connected(): bool {
return self::is_store_connected() && self::get_api_key();
}

public static function disconnect() {
delete_option( self::OPTION_API_KEY );
delete_option( self::OPTION_BRAND_ID );
Expand Down