Feature
1 year ago
Integration
1 year ago
Interfaces
2 years ago
Util
2 years ago
WordPress
1 year ago
CLI.php
1 year ago
ModuleHandler.php
1 year ago
PluginStateHandler.php
1 year ago
PluginStateHandler.php
71 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack; |
| 4 | |
| 5 | use NitroPack\Integration\Plugin\AeliaCurrencySwitcher; |
| 6 | use NitroPack\Integration\Plugin\GeoTargetingWP; |
| 7 | |
| 8 | class PluginStateHandler { |
| 9 | const eventHandlersMap = [ |
| 10 | 'woocommerce-aelia-currencyswitcher/woocommerce-aelia-currencyswitcher.php' => [ |
| 11 | 'activateCallback' => 'HandleAeliaCurrencyActivation', |
| 12 | 'deactivateCallback' => 'HandleAeliaCurrencyDeactivation', |
| 13 | ], |
| 14 | ]; |
| 15 | private static $instance; |
| 16 | |
| 17 | public static function getInstance() { |
| 18 | if (!self::$instance) { |
| 19 | self::$instance = new PluginStateHandler(); |
| 20 | } |
| 21 | return self::$instance; |
| 22 | } |
| 23 | |
| 24 | public static function init() { |
| 25 | add_action('activated_plugin', [self::getInstance(), 'handleActivation'], 10, 1); |
| 26 | add_action('deactivated_plugin', [self::getInstance(), 'handleDeactivation'], 10, 1); |
| 27 | add_action('update_option_active_plugins', [self::getInstance(), 'onUpdatedActivePluginsList'], 10, 3); |
| 28 | } |
| 29 | |
| 30 | public function handleActivation($plugin) { |
| 31 | if (array_key_exists($plugin, self::eventHandlersMap) && !empty(self::eventHandlersMap[$plugin]['activateCallback'])) { |
| 32 | self::{self::eventHandlersMap[$plugin]['activateCallback']}(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public function handleDeactivation($plugin) { |
| 37 | if (array_key_exists($plugin, self::eventHandlersMap) && !empty(self::eventHandlersMap[$plugin]['deactivateCallback'])) { |
| 38 | self::{self::eventHandlersMap[$plugin]['deactivateCallback']}(); |
| 39 | } |
| 40 | } |
| 41 | public function onUpdatedActivePluginsList($old_value, $value, $option) { |
| 42 | if ($old_value === $value) return; |
| 43 | |
| 44 | $activated_plugins = array_diff($value, $old_value); |
| 45 | $deactivated_plugins = array_diff($old_value, $value); |
| 46 | |
| 47 | if (in_array('woocommerce/woocommerce.php', $activated_plugins)) { |
| 48 | nitropack_event("platform_change", null, array("platform" => 'WooCommerce')); |
| 49 | } else if (in_array('woocommerce/woocommerce.php', $deactivated_plugins)) { |
| 50 | nitropack_event("platform_change", null, array("platform" => 'WordPress')); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // maybe have these handlers be part of each plugin compatibility class (maybe even have a class PluginCompatibility that they extend). |
| 55 | public static function HandleAeliaCurrencyActivation() { |
| 56 | initVariationCookies(AeliaCurrencySwitcher::customVariationCookies); |
| 57 | } |
| 58 | |
| 59 | public static function HandleAeliaCurrencyDeactivation() { |
| 60 | removeVariationCookies(AeliaCurrencySwitcher::customVariationCookies); |
| 61 | } |
| 62 | |
| 63 | public static function HandleGeowpActivation() { |
| 64 | initVariationCookies(GeoTargetingWP::getCustomVariationCookies()); |
| 65 | } |
| 66 | |
| 67 | public static function HandleGeowpDeactivation() { |
| 68 | removeVariationCookies(GeoTargetingWP::allGeoWpCookies); |
| 69 | } |
| 70 | } |
| 71 |