Passed
Push — master ( 33ec80...22a77e )
by Lucien
01:59
created

Filter::remove()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 3
1
<?php
2
3
namespace TwinDigital\WPTools;
4
5
/**
6
 * Class Filter
7
 */
8
class Filter {
9
10
  /**
11
   * Remove active filter from $wp_filter
12
   *
13
   * @param string $hook   Hook on which the original filter was applied.
14
   * @param string $class  Classname.
15
   * @param string $method Method.
16
   *
17
   * @SuppressWarnings(PHPMD.Superglobals) Don't worry, I know what I'm doing.
18
   * @return boolean
19
   */
20
  public static function remove(string $hook, string $class, string $method): bool {
21
    if (array_key_exists($hook, $GLOBALS['wp_filter']) === true) {
22
      $filters = $GLOBALS['wp_filter'][$hook];
23
    } else {
24
      return false;
25
    }
26
    $return = false;
27
    foreach ($filters as $priority => $filter) {
28
      foreach ($filter as $function) {
29
        if (self::isFilterFunctionClass($function, $class, $method) === true) {
30
          $return = remove_filter(
31
            $hook,
32
            [
33
              $function['function'][0],
34
              $method,
35
            ],
36
            $priority
37
          );
38
        }
39
      }
40
    }
41
42
    return $return;
43
  }
44
45
  /**
46
   * Checks whether a filter contains the supplied Class and Function
47
   *
48
   * @param array  $function The original filter from $wp_filter.
49
   * @param string $class    The class in which the method should be called.
50
   * @param string $method   The method to check for.
51
   *
52
   * @return boolean
53
   */
54
  private static function isFilterFunctionClass(array $function, string $class, string $method): bool {
55
    if (is_array($function) === true && is_a($function['function'][0], $class) === true && $method === $function['function'][1]) {
56
      return true;
57
    }
58
59
    return false;
60
  }
61
}
62