Passed
Push — master ( 8ec7ae...f60e59 )
by Lucien
01:27
created

PluginTools::refreshLoadedPlugins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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
   * @param boolean $force_reload Forces reload of the loaded plugins. Also flushes cache.
25
   *
26
   * @since 1.0.0
27
   * @return void
28
   */
29
  public static function loadPluginList(bool $force_reload = false): void {
30
    if ($force_reload === true) {
31
      wp_cache_flush();
32
      self::$loadedPlugins = [];
33
    }
34
    if (empty(self::$loadedPlugins) === true) {
35
      include_once \ABSPATH . '/wp-admin/includes/plugin.php';
36
      $all_plugins = get_plugins();
37
      $active_plugins = (array)get_option('active_plugins', []);
38
      foreach ($all_plugins as $k => $plugin) {
39
        self::$loadedPlugins[] = array_merge(
40
          $plugin,
41
          [
42
            'Path'   => $k,
43
            'Active' => (bool)(in_array($k, $active_plugins)),
44
          ]
45
        );
46
      }
47
    }
48
  }
49
50
  /**
51
   * Refreshes the loaded plugins.
52
   *
53
   * @see   \TwinDigital\WPTools\PluginTools::loadPluginList()
54
   * @since 1.0.0
55
   * @return void
56
   */
57
  public static function refreshLoadedPlugins(): void {
58
    self::loadPluginList(true);
59
  }
60
61
  /**
62
   * Returns plugin by name (case-sensitive)
63
   *
64
   * @param string  $title          Title of the plugin.
65
   * @param boolean $case_sensitive Wether the title-search should be case-sensitive, true by default.
66
   *
67
   * @since 1.0.0
68
   * @return array|boolean False if not found, array otherwise.
69
   */
70
  public static function getPluginByTitle(string $title, bool $case_sensitive = true) {
71
    self::loadPluginList();
72
    foreach (self::$loadedPlugins as $k => $v) {
73
      if ($case_sensitive === false) {
74
        $v['Name'] = strtolower($v['Name']);
75
        $title = strtolower($title);
76
      }
77
      if ($v['Name'] === $title) {
78
        return $v;
79
      }
80
    }
81
82
    return false;
83
  }
84
}
85