Skip to content

Commit

Permalink
Remove wishlist event
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Sep 6, 2024
1 parent f4b959d commit a596df0
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 30 deletions.
2 changes: 1 addition & 1 deletion assets/integration/woocommerce.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/Admin/PluginAvailability.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class PluginAvailability {
public function register() {
$this->seo_plugins();
$this->conflicting_plugins();
$this->wishlist_plugins();
$this->register_plugins();
}

Expand Down Expand Up @@ -76,6 +77,27 @@ protected function conflicting_plugins() {
];
}

/**
* Wishlist plugins.
*
* @return void
*/
protected function wishlist_plugins() {
$this->plugins['wishlist_plugins'] = [
'yith-woocommerce-wishlist' => [
'name' => 'YITH WooCommerce Wishlist',
'slug' => 'yith-woocommerce-wishlist/init.php',
'gf' => true,
],

'gtm-ecommerce-woo-pro' => [
'name' => 'TI WooCommerce Wishlist',
'slug' => 'ti-woocommerce-wishlist/ti-woocommerce-wishlist.php',
'gf' => true,
],
];
}

/**
* Registers the plugins status.
*
Expand Down Expand Up @@ -133,4 +155,15 @@ public function get_plugins( string $category = '' ): array {
public function is_active( array $plugin ): bool {
return isset( $plugin['active'] ) && $plugin['active'] === true;
}

/**
* Determines whether a grandfathered polyfill is available.
*
* @param array<string, mixed> $plugin The plugin to check.
*
* @return bool Whether the plugin is active.
*/
public function gf_polyfill_available( array $plugin ): bool {
return isset( $plugin['gf'] ) && $plugin['gf'] === true;
}
}
172 changes: 170 additions & 2 deletions src/Admin/PluginSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace TLA_Media\GTM_Kit\Admin;

use TLA_Media\GTM_Kit\Common\Conditionals\EasyDigitalDownloadsConditional;
use TLA_Media\GTM_Kit\Common\Conditionals\PremiumConditional;
use TLA_Media\GTM_Kit\Common\Conditionals\WooCommerceConditional;
use TLA_Media\GTM_Kit\Options;

/**
* Suggested plugins
Expand All @@ -20,14 +22,14 @@ final class PluginSuggestions {
*
* @var PluginAvailability
*/
protected $plugin_availability;
protected PluginAvailability $plugin_availability;

/**
* An instance of NotificationsHandler.
*
* @var NotificationsHandler
*/
private $notifications_handler;
private NotificationsHandler $notifications_handler;

/**
* Constructor.
Expand All @@ -52,8 +54,57 @@ public static function register( NotificationsHandler $notifications_handler, Pl
$page = new self( $notifications_handler, $plugin_availability );

add_action( 'admin_init', [ $page->plugin_availability, 'register' ] );
add_action( 'admin_init', [ $page, 'suggest_premium' ] );
add_action( 'admin_init', [ $page, 'suggest_seo_plugin' ] );
add_action( 'admin_init', [ $page, 'detect_conflicting_plugins' ] );
add_action( 'admin_init', [ $page, 'suggest_grandfathered_wishlist' ] );
}

/**
* Suggest GTM Kit Woo Add-On.
*
* @return void
*/
public function suggest_premium(): void {

$notification_id = 'gtmkit-premium-woo';

if ( ! (
( new WooCommerceConditional() )->is_met() &&
! ( new PremiumConditional() )->is_met() &&
! Options::init()->get( 'misc', 'gf_wishlist' ) === true )
) {
$this->notifications_handler->remove_notification_by_id( $notification_id );
return;
}

$add_notification = false;

$plugins = $this->plugin_availability->get_plugins( 'wishlist_plugins' );
$extensions = [
'plugin' => '',
'theme' => '',
];

foreach ( $plugins as $plugin ) {
if ( $this->plugin_availability->is_active( $plugin ) ) {
$extensions['plugin'] = $plugin['name'];
$add_notification = true;

break;
}
}

if ( \wp_get_theme()->get( 'Name' ) === 'Woodmart' || wp_get_theme()->get( 'Template' ) === 'woodmart' ) {
$extensions['theme'] = 'woodmart';
}

if ( $add_notification ) {
$notification = $this->get_premium_notification( $notification_id, $extensions );
$this->notifications_handler->add_notification( $notification );
} else {
$this->notifications_handler->remove_notification_by_id( $notification_id );
}
}

/**
Expand Down Expand Up @@ -109,6 +160,94 @@ public function detect_conflicting_plugins(): void {
}
}

/**
* Suggest a supported SEO plugin.
*
* @return void
*/
public function suggest_grandfathered_wishlist(): void {
$notification_id = 'gtmkit-gf_wishlist';

if ( ! (
( new WooCommerceConditional() )->is_met() &&
! ( new PremiumConditional() )->is_met() &&
Options::init()->get( 'misc', 'gf_wishlist' ) === true )
) {
$this->notifications_handler->remove_notification_by_id( $notification_id );
return;
}

$add_notification = false;

if ( ! defined( 'GTMKIT_GF_WISHLIST_VERSION' ) ) {
$plugins = $this->plugin_availability->get_plugins( 'wishlist_plugins' );

foreach ( $plugins as $plugin ) {
if ( $this->plugin_availability->is_active( $plugin ) && $this->plugin_availability->gf_polyfill_available( $plugin ) ) {
$add_notification = true;

break;
}
}
}

if ( $add_notification ) {
$notification = $this->get_gf_wishlist_plugin_notification( $notification_id );
$this->notifications_handler->add_notification( $notification );
} else {
$this->notifications_handler->remove_notification_by_id( $notification_id );
}
}

/**
* Build premium plugin notification.
*
* @param string $notification_id The id of the notification to be created.
* @param array<string, string> $extensions The plugin data.
*
* @return Notification The notification containing the suggested plugin.
*/
protected function get_premium_notification( string $notification_id, array $extensions ): Notification {

$message = '';

if ( ! empty( $extensions['plugin'] ) && ! empty( $extensions['theme'] ) ) {
$message = sprintf(
/* translators: %1$s is the name of the plugin and %2$s is the name of the theme. */
__( 'It seems that you have installed the %1$s plugin and %1$s theme.', 'gtm-kit' ),
$extensions['plugin'],
$extensions['theme']
) . ' ';
} elseif ( ! empty( $extensions['plugin'] ) ) {
$message = sprintf(
/* translators: %1$s is the name of the plugin. */
__( 'It seems that you have installed the %1$s plugin.', 'gtm-kit' ),
$extensions['plugin'],
$extensions['theme']
) . ' ';
} elseif ( ! empty( $extensions['theme'] ) ) {
$message = sprintf(
/* translators: %1$s is the name of the theme. */
__( 'It seems that you have installed the %1$s theme.', 'gtm-kit' ),
$extensions['plugin'],
$extensions['theme']
) . ' ';
}

$link = '<a href="https://jump.gtmkit.com/link/2-30DDC" class="gtmkit-text-color-primary gtmkit hover:gtmkit-underline gtmkit-font-bold">GTM Kit Woo Add-On</a>';
$message .= sprintf(
/* translators: %1$s is a link with the text 'GTM Kit Woo Add-On'. */
__( 'With the %1$s, you can track the add_to_wishlist event and leverage server-side tracking for enhanced accuracy and deeper insights into customer behavior.', 'gtm-kit' ),
$link,
);

return $this->new_notification(
$notification_id,
$message,
__( 'Upgrade:', 'gtm-kit' )
);
}

/**
* Build suggestion of SEO plugin notification.
*
Expand Down Expand Up @@ -154,6 +293,35 @@ protected function get_conflicting_plugin_notification( string $notification_id,
);
}


/**
* Build GF wishlist notification.
*
* @param string $notification_id The id of the notification to be created.
*
* @return Notification The notification containing the suggested plugin.
*/
protected function get_gf_wishlist_plugin_notification( string $notification_id ): Notification {

$link_1 = '<a href="https://jump.gtmkit.com/link/2-30DDC" class="gtmkit-text-color-primary gtmkit hover:gtmkit-underline gtmkit-font-bold">GTM Kit Woo Add-On</a>';
$link_2 = '<a href="https://jump.gtmkit.com/link/3-63585" class="gtmkit-text-color-primary gtmkit hover:gtmkit-underline gtmkit-font-bold">Grandfathered Wishlist Functionality</a>';

$message = sprintf(
/* translators: %1$s and %2$s are links with the text 'GTM Kit Woo Add-On' and 'Grandfathered Wishlist Functionality' respectively. */
__( 'Starting with GTM Kit version 2.0, the add_to_wishlist event is no longer supported in the free version of GTM Kit. To continue tracking the add_to_wishlist event, you must either purchase the %1$s or download the free %2$s plugin.', 'gtm-kit' ),
$link_1,
$link_2
);

return $this->new_notification(
$notification_id,
$message,
__( 'Breaking change:', 'gtm-kit' ),
Notification::PROBLEM
);
}


/**
* New notification.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Installation/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace TLA_Media\GTM_Kit\Installation;

use TLA_Media\GTM_Kit\Common\Conditionals\WooCommerceConditional;
use TLA_Media\GTM_Kit\Options;

/**
Expand Down Expand Up @@ -44,6 +45,7 @@ protected function get_upgrades(): array {
'1.15' => 'v115_upgrade',
'1.20' => 'v120_upgrade',
'1.22' => 'v122_upgrade',
'2.0' => 'v2_upgrade',
];

$current_version = \get_option( 'gtmkit_version' );
Expand Down Expand Up @@ -153,4 +155,23 @@ protected function v122_upgrade(): void {

Options::init()->set( $values, false, false );
}

/**
* Upgrade routine for v2.0
*/
protected function v2_upgrade(): void {

if ( (
new WooCommerceConditional() )->is_met() &&
( is_plugin_active( 'yith-woocommerce-wishlist/init.php' ) || is_plugin_active( 'ti-woocommerce-wishlist/ti-woocommerce-wishlist.php' )
) ) {
$values = [
'misc' => [
'gf_wishlist' => true,
],
];

Options::init()->set( $values, false, false );
}
}
}
Loading

0 comments on commit a596df0

Please sign in to comment.