Passed
Push — master ( 75f1be...abbf77 )
by Lucien
01:58
created

PluginTools::isPluginActive()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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