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

PluginRequirements::removePluginRequirement()   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 PluginRequirements
7
 */
8
class PluginRequirements {
9
10
  /**
11
   * The minimum required PHP-version.
12
   * @var string $phpMinVersion
13
   */
14
  public static $phpMinVersion;
15
16
  /**
17
   * The maximum required PHP-version.
18
   * @var string $phpMaxVersion
19
   */
20
  public static $phpMaxVersion;
21
22
  /**
23
   * Internal list of the required plugins that are added by `addPluginRequirement()`
24
   * @var array $requiredPlugins
25
   */
26
  protected static $requiredPlugins;
27
28
  /**
29
   * The minimum required WordPress-version.
30
   * @var string $wpMinVersion
31
   */
32
  public static $wpMinVersion;
33
34
  /**
35
   * The maximum required WordPress-version.
36
   * @var string $wpMaxVersion
37
   */
38
  public static $wpMaxVersion;
39
40
  /**
41
   * Add requirement for a plugin.
42
   * @param string  $pluginName       The name of the plugin.
43
   * @param string  $pluginMinVersion The minimum version (SemVer) of the plugin.
44
   * @param string  $pluginMaxVersion The maximum version (SemVer) of the plugin.
45
   * @param boolean $activated        Wether the plugin should be activated or not.
46
   *
47
   * @return boolean Wether the plugin was added to the requirements or not.
48
   */
49
  public static function addPluginRequirement(string $pluginName, string $pluginMinVersion, string $pluginMaxVersion, bool $activated = true) {
50
    if (is_array(self::$requiredPlugins) === true) {
51
      foreach (self::$requiredPlugins as $plugin) {
52
        if ($plugin['Name'] === $pluginName) {
53
          return false;
54
        }
55
      }
56
    }
57
    self::$requiredPlugins[] = [
58
      'Name'       => $pluginName,
59
      'MinVersion' => $pluginMinVersion,
60
      'MaxVersion' => $pluginMaxVersion,
61
      'Active'     => $activated,
62
    ];
63
64
    return true;
65
  }
66
67
  /**
68
   * Removes a plugin from the requirements-list.
69
   * @param string $pluginName The name of the plugin.
70
   *
71
   * @return boolean
72
   */
73
  public static function removePluginRequirement(string $pluginName): bool {
74
    $return = false;
75
    foreach (self::$requiredPlugins as $key => $plugin) {
76
      if ($plugin['Name'] === $pluginName) {
77
        unset(self::$requiredPlugins[$key]);
78
        $return = true;
79
      }
80
    }
81
    self::$requiredPlugins = array_values(self::$requiredPlugins);
82
83
    return $return;
84
  }
85
86
  /**
87
   * Checks the required plugins.
88
   * @return boolean Wether all checks are successful.
89
   */
90
  public static function checkRequiredPlugins(): bool {
91
    $return = true;
92
    foreach (self::$requiredPlugins as $plugin) {
93
      $installed = self::checkPluginInstalled($plugin['Name']);
94
      if ($installed === false) {
95
        $return = false;
96
      } else if ($installed === true) {
97
        if ($plugin['Active'] === true) {
98
          $return = (bool)($plugin['Active'] === $installed['Active']);
99
        }
100
      }
101
    }
102
103
    return $return;
104
  }
105
106
  /**
107
   * Checks wether a plugin is installed.
108
   * @param string $pluginName The name of the plugin.
109
   *
110
   * @return boolean
111
   */
112
  public static function checkPluginInstalled(string $pluginName): bool {
113
    $return = false;
114
    if (is_array(PluginTools::getPluginByTitleCaseInsensitive($pluginName)) === true) {
115
      $return = true;
116
    }
117
118
    return $return;
119
  }
120
}
121