Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
48 / 48 |
|
100.00% |
44 / 44 |
|
7.60% |
13 / 171 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Query | |
100.00% |
48 / 48 |
|
100.00% |
44 / 44 |
|
7.60% |
13 / 171 |
|
100.00% |
4 / 4 |
646.44 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
6 | |||
| find_callbacks | |
100.00% |
27 / 27 |
|
100.00% |
35 / 35 |
|
4.88% |
8 / 164 |
|
100.00% |
1 / 1 |
296.86 | |||
| add_to_wp_filter_structure | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
|
50.00% |
2 / 4 |
|
100.00% |
1 / 1 |
4.12 | |||
| 1 | <?php namespace GorillaClaw; |
| 2 | |
| 3 | class Query { |
| 4 | private $wp_filter; |
| 5 | |
| 6 | function __construct(array &$wp_filter) { |
| 7 | $this->wp_filter = &$wp_filter; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * find |
| 12 | * |
| 13 | * @param mixed $hook_names |
| 14 | * @param mixed $target_callback |
| 15 | * @return HookCollection |
| 16 | */ |
| 17 | |
| 18 | public function find(string | array $hook_names, string | \Closure | array | false $target_callback = false): HookCollection { |
| 19 | $results = []; |
| 20 | |
| 21 | if(is_string($hook_names)) { |
| 22 | $hook_names = explode(" ", $hook_names); |
| 23 | } |
| 24 | |
| 25 | array_walk($this->wp_filter, function($entry, $hook_name) use ($hook_names, $target_callback, &$results) { |
| 26 | if(in_array($hook_name, $hook_names)) { |
| 27 | // We have found a matching hook name |
| 28 | |
| 29 | foreach($entry->callbacks as $priority => $callbacks) { |
| 30 | |
| 31 | if($target_callback === false) { |
| 32 | $this->add_to_wp_filter_structure($results, $hook_name, $callbacks, $priority); |
| 33 | continue; |
| 34 | } |
| 35 | |
| 36 | $found = $this->find_callbacks($target_callback, $callbacks); |
| 37 | |
| 38 | if(count($found) > 0) { |
| 39 | $this->add_to_wp_filter_structure($results, $hook_name, $found, $priority); |
| 40 | } |
| 41 | |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return; |
| 46 | }); |
| 47 | |
| 48 | return new HookCollection($results); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * find_callbacks |
| 53 | * |
| 54 | * @param mixed $needle |
| 55 | * @param mixed $haystack |
| 56 | * @return array |
| 57 | */ |
| 58 | |
| 59 | private function find_callbacks(string | \Closure | array $needle, $haystack): array { |
| 60 | |
| 61 | $ret = []; |
| 62 | $is_static = false; |
| 63 | |
| 64 | if(is_string($needle) && strpos($needle, "::")) { |
| 65 | $is_static = true; |
| 66 | } |
| 67 | |
| 68 | foreach($haystack as $function_key => $callback_definition) { |
| 69 | |
| 70 | if(is_array($needle)) { |
| 71 | // We are looking for an object only. |
| 72 | |
| 73 | if($callback_definition['function'] instanceof \Closure) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | $obj = $callback_definition['function'][0]; |
| 78 | $method = $callback_definition['function'][1]; |
| 79 | |
| 80 | if(is_string($obj) ? $obj === $needle[0] : get_class($obj) === $needle[0]) { |
| 81 | if($method === $needle[1] || $needle[1] === false) { |
| 82 | $ret[$function_key] = $callback_definition; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } elseif(is_string($needle) && $is_static) { |
| 87 | // We are looking for a static method |
| 88 | |
| 89 | if(is_string($callback_definition['function'])) { |
| 90 | $callback_definition['function'] = explode("::", $callback_definition['function']); |
| 91 | } |
| 92 | |
| 93 | if($callback_definition['function'] instanceof \Closure) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | $obj = $callback_definition['function'][0]; |
| 98 | $method = $callback_definition['function'][1]; |
| 99 | list($find_obj, $find_method) = explode("::", $needle); |
| 100 | |
| 101 | if($obj === $find_obj) { |
| 102 | if($method === $find_method || empty($find_method)) { |
| 103 | $ret[$function_key] = $callback_definition; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } else { |
| 108 | // We are looking for a function name or a closure |
| 109 | |
| 110 | if($callback_definition['function'] === $needle) { |
| 111 | $ret[$function_key] = $callback_definition; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | return $ret; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * add_to_wp_filter_structure |
| 122 | * |
| 123 | * @param mixed $wp_filter |
| 124 | * @param mixed $hook_name |
| 125 | * @param mixed $callbacks |
| 126 | * @param mixed $priority |
| 127 | * @return void |
| 128 | */ |
| 129 | |
| 130 | private function add_to_wp_filter_structure(array &$wp_filter, string $hook_name, array $callbacks, int $priority) { |
| 131 | if(!isset($wp_filter[$hook_name])) { |
| 132 | $wp_filter[$hook_name] = [$priority => []]; |
| 133 | } |
| 134 | |
| 135 | if(!isset($wp_filter[$hook_name][$priority])) { |
| 136 | $wp_filter[$hook_name][$priority] = []; |
| 137 | } |
| 138 | |
| 139 | $wp_filter[$hook_name][$priority] += $callbacks; |
| 140 | } |
| 141 | } |