Passed
Push — master ( 07fba6...d9a690 )
by Lucien
01:57
created

PluginTools::getPluginByTitleCaseInsensitive()   A

Complexity

Conditions 3
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.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace TwinDigital\WPTools;
4
5
/**
6
 * Class PluginTools
7
 * Collection of tools
8
 * @package TwinDigital\WPTools
9
 * @version 1.0.0
10
 */
11
class PluginTools {
12
13
  /**
14
   * Keeps track of the loaded plugins
15
   *
16
   * @var array $loadedPlugins
17
   * @since 1.0.0
18
   */
19
  public static $loadedPlugins = [];
20
21
  /**
22
   * Loads the list of plugins.
23
   *
24
   * @since 1.0.0
25
   * @return void
26
   */
27
  public static function loadPluginList(): void {
28
    if (empty(self::$loadedPlugins) === true) {
29
      include_once \ABSPATH . '/wp-admin/includes/plugin.php';
30
      $allPlugins = get_plugins();
31
      $activePlugins = (array)get_option('active_plugins', []);
32
      foreach ($allPlugins as $k => $plugin) {
33
        self::$loadedPlugins[] = array_merge(
34
          $plugin,
35
          [
36
            'Path'   => $k,
37
            'Active' => (bool)(in_array($k, $activePlugins)),
38
          ]
39
        );
40
      }
41
    }
42
  }
43
44
  /**
45
   * Refreshes the loaded plugins.
46
   *
47
   * @see   \TwinDigital\WPTools\PluginTools::loadPluginList()
48
   * @since 1.0.0
49
   * @return void
50
   */
51
  public static function refreshLoadedPlugins(): void {
52
    wp_cache_flush();
53
    self::$loadedPlugins = [];
54
    self::loadPluginList();
55
  }
56
57
  /**
58
   * Returns plugin by name (case-sensitive)
59
   *
60
   * @param string $title Title of the plugin.
61
   *
62
   * @since 1.0.0
63
   * @return array|boolean False if not found, array otherwise.
64
   */
65
  public static function getPluginByTitle(string $title) {
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|boolean False if not found, array otherwise.
83
   */
84
  public static function getPluginByTitleCaseInsensitive(string $title) {
85
    self::loadPluginList();
86
    foreach (self::$loadedPlugins as $v) {
87
      $v['Name'] = strtolower($v['Name']);
88
      $title = strtolower($title);
89
      if ($v['Name'] === $title) {
90
        return $v;
91
      }
92
    }
93
94
    return false;
95
  }
96
}
97