Completed
Branch new-addon-api (b7bde0)
by
unknown
18:29 queued 08:41
created

deactivateIncompatibleAddons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\addon\api;
4
5
use EE_Error;
6
7
class IncompatibleAddonHandler
8
{
9
    /**
10
     * @return void
11
     */
12
    public function deactivateIncompatibleAddons()
13
    {
14
        $this->deactivateIncompatibleAddon(
15
            'Wait Lists',
16
            'EE_WAIT_LISTS_VERSION',
17
            '1.0.0.beta.074',
18
            'load_espresso_wait_lists',
19
            'EE_WAIT_LISTS_PLUGIN_FILE'
20
        );
21
        $this->deactivateIncompatibleAddon(
22
            'Automated Upcoming Event Notifications',
23
            'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_VERSION',
24
            '1.0.0.beta.091',
25
            'load_espresso_automated_upcoming_event_notification',
26
            'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_PLUGIN_FILE'
27
        );
28
    }
29
30
31
    /**
32
     * @param string $addon_name
33
     * @param string $version_constant
34
     * @param string $min_version_required
35
     * @param string $load_callback
36
     * @param string $plugin_file_constant
37
     * @return void
38
     */
39
    private function deactivateIncompatibleAddon(
40
        string $addon_name,
41
        string $version_constant,
42
        string $min_version_required,
43
        string $load_callback,
44
        string $plugin_file_constant
45
    ) {
46
        if (! defined($version_constant)) {
47
            return;
48
        }
49
        $addon_version = constant($version_constant);
50
        if ($addon_version && version_compare($addon_version, $min_version_required, '<')) {
51
            remove_action('AHEE__EE_System__load_espresso_addons', $load_callback);
52
            if (! function_exists('deactivate_plugins')) {
53
                require_once ABSPATH . 'wp-admin/includes/plugin.php';
54
            }
55
            deactivate_plugins(plugin_basename(constant($plugin_file_constant)));
56
            unset($_GET['activate'], $_REQUEST['activate'], $_GET['activate-multi'], $_REQUEST['activate-multi']);
57
            EE_Error::add_error(
58
                sprintf(
59
                    esc_html__(
60
                        'We\'re sorry, but the Event Espresso %1$s addon was deactivated because version %2$s or higher is required with this version of Event Espresso core.',
61
                        'event_espresso'
62
                    ),
63
                    $addon_name,
64
                    $min_version_required
65
                ),
66
                __FILE__,
67
                __FUNCTION__ . "({$addon_name})",
68
                __LINE__
69
            );
70
            EE_Error::get_notices(false, true);
71
        }
72
    }
73
}
74