Passed
Push — main ( e91c36...af3979 )
by Paul
05:45
created

Compatibility   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 9
eloc 19
dl 0
loc 32
ccs 0
cts 20
cp 0
rs 10
c 2
b 1
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B findCallback() 0 20 7
A removeHook() 0 8 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
7
class Compatibility
8
{
9
    public function findCallback(string $hook, string $fn, string $className, int $priority = 10): array
10
    {
11
        global $wp_filter;
12
        if (!isset($wp_filter[$hook])) {
13
            return [];
14
        }
15
        if (!isset($wp_filter[$hook]->callbacks[$priority])) {
16
            return [];
17
        }
18
        foreach ($wp_filter[$hook]->callbacks[$priority] as $callback) {
19
            if (!is_array($callback['function'])) {
20
                continue;
21
            }
22
            $object = Arr::get($callback['function'], 0);
23
            $method = Arr::get($callback['function'], 1);
24
            if (is_a($object, $className) && $method === $fn) {
25
                return $callback;
26
            }
27
        }
28
        return [];
29
    }
30
31
    public function removeHook(string $hook, string $fn, string $className, int $priority = 10): bool
32
    {
33
        $callback = $this->findCallback($hook, $fn, $className, $priority);
34
        if (!empty($callback['function'])) {
35
            remove_filter($hook, $callback['function'], $priority);
36
            return true;
37
        }
38
        return false;
39
    }
40
}
41