PluginTools::getPluginByTitleCaseInsensitive()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace TwinDigital\WPTools;
4
5
/**
6
 * Class PluginTools
7
 * Collection of tools
8
 */
9
class PluginTools
10
{
11
12
    /**
13
     * Keeps track of the loaded plugins
14
     * @var array $loadedPlugins
15
     */
16
    public static $loadedPlugins = [];
17
18
    /**
19
     * Loads the list of plugins.
20
     *
21
     * @since 1.0.0
22
     * @return void
23
     */
24
    public static function loadPluginList(): void
25
    {
26
        if (empty(self::$loadedPlugins) === true || count(self::$loadedPlugins) === 0) {
27
            include_once \ABSPATH . '/wp-admin/includes/plugin.php';
28
            $allPlugins    = get_plugins();
29
            $activePlugins = (array)get_option('active_plugins', []);
30
            foreach ($allPlugins as $k => $plugin) {
31
                self::$loadedPlugins[] = array_merge(
32
                    $plugin,
33
                    [
34
                        'Path'   => $k,
35
                        'Active' => (bool)(in_array($k, $activePlugins)),
36
                    ]
37
                );
38
            }
39
        }
40
    }
41
42
    /**
43
     * Refreshes the loaded plugins.
44
     *
45
     * @see   \TwinDigital\WPTools\PluginTools::loadPluginList()
46
     * @since 1.0.0
47
     * @return void
48
     */
49
    public static function refreshLoadedPlugins(): void
50
    {
51
        wp_cache_flush();
52
        self::$loadedPlugins = [];
53
        self::loadPluginList();
54
    }
55
56
    /**
57
     * Returns plugin by name (case-sensitive)
58
     *
59
     * @param string $title Title of the plugin.
60
     *
61
     * @since 1.0.0
62
     * @return array|bool False if not found, array otherwise.
63
     */
64
    public static function getPluginByTitle(string $title)
65
    {
66
        self::loadPluginList();
67
        foreach (self::$loadedPlugins as $v) {
68
            if ($v['Name'] === $title) {
69
                return $v;
70
            }
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * Returns plugin by name (case-sensitive)
78
     *
79
     * @param string $title Title of the plugin.
80
     *
81
     * @since 1.0.0
82
     * @return array|bool False if not found, array otherwise.
83
     */
84
    public static function getPluginByTitleCaseInsensitive(string $title)
85
    {
86
        self::loadPluginList();
87
        foreach (self::$loadedPlugins as $v) {
88
            $v['Name'] = strtolower($v['Name']);
89
            $title     = strtolower($title);
90
            if ($v['Name'] === $title) {
91
                return $v;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Checks if the plugin is installed and activated.
100
     * @param string $pluginName The Name of the plugin.
101
     *
102
     * @return bool True if the plugin is active, false otherwise.
103
     */
104
    public static function isPluginActive(string $pluginName): bool
105
    {
106
        $return = false;
107
        $plugin = self::getPluginByTitle($pluginName);
108
        if ($plugin === false) {
109
            return false;
110
        }
111
        if (is_array($plugin) === true && array_key_exists('Name', $plugin) === true) {
112
            $return = $plugin['Active'];
113
        }
114
115
        return $return;
116
    }
117
}
118