Filter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
eloc 21
c 2
b 0
f 1
dl 0
loc 59
rs 10

2 Methods

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