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

Compatibility::findCallback()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
ccs 0
cts 14
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 4
crap 56
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