Completed
Push — master ( abbf77...28136f )
by Lucien
01:41
created

Filter::remove()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
cc 8
eloc 15
nc 10
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
   * @param string $hook   Hook on which the original filter was applied.
13
   * @param string $class  Classname.
14
   * @param string $method Method.
15
   *
16
   * @todo Create tests.
17
   *
18
   * @SuppressWarnings(PHPMD.Superglobals) Don't worry, I know what I'm doing.
19
   * @return void
20
   */
21
  public static function remove(string $hook, string $class, string $method) {
22
    $filters = [];
23
    if (array_key_exists($hook, $GLOBALS['wp_filter']) === true) {
24
      $filters = $GLOBALS['wp_filter'][$hook];
25
    }
26
    if (empty($filters) === true) {
27
      return;
28
    }
29
30
    foreach ($filters as $priority => $filter) {
31
      foreach ($filter as $function) {
32
        if (is_array($function) === true
33
            && is_a($function['function'][0], $class) === true
34
            && $method === $function['function'][1]
35
        ) {
36
          remove_filter(
37
            $hook,
38
            [
39
              $function['function'][0],
40
              $method,
41
            ],
42
            $priority
43
          );
44
        }
45
      }
46
    }
47
  }
48
}
49