Hook.php
2 days ago
HookCollection.php
2 days ago
HookCollectionFactory.php
2 days ago
Hooks.php
2 days ago
Hook.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Deprecated; |
| 6 | |
| 7 | use Closure; |
| 8 | use LogicException; |
| 9 | use ReflectionFunction; |
| 10 | |
| 11 | class Hook |
| 12 | { |
| 13 | private string $name; |
| 14 | |
| 15 | private string $version; |
| 16 | |
| 17 | private ?string $replacement; |
| 18 | |
| 19 | public function __construct(string $name, string $version, ?string $replacement = null) |
| 20 | { |
| 21 | $this->name = $name; |
| 22 | $this->version = $version; |
| 23 | $this->replacement = $replacement; |
| 24 | } |
| 25 | |
| 26 | public function get_name(): string |
| 27 | { |
| 28 | return $this->name; |
| 29 | } |
| 30 | |
| 31 | public function get_version(): string |
| 32 | { |
| 33 | return $this->version; |
| 34 | } |
| 35 | |
| 36 | public function get_replacement(): string |
| 37 | { |
| 38 | if (null === $this->replacement) { |
| 39 | throw new LogicException('Empty replacement'); |
| 40 | } |
| 41 | |
| 42 | return $this->replacement; |
| 43 | } |
| 44 | |
| 45 | public function has_replacement(): bool |
| 46 | { |
| 47 | return null !== $this->replacement; |
| 48 | } |
| 49 | |
| 50 | public function has_hook(): bool |
| 51 | { |
| 52 | return has_filter($this->name); |
| 53 | } |
| 54 | |
| 55 | public function usage_count(): int |
| 56 | { |
| 57 | return count($this->get_callbacks() ?? []); |
| 58 | } |
| 59 | |
| 60 | private function get_filter_callbacks(): array |
| 61 | { |
| 62 | global $wp_filter; |
| 63 | |
| 64 | return $wp_filter[$this->name]->callbacks ?? []; |
| 65 | } |
| 66 | |
| 67 | public function get_callbacks(): ?array |
| 68 | { |
| 69 | $messages = []; |
| 70 | |
| 71 | foreach ($this->get_filter_callbacks() as $callback) { |
| 72 | foreach ($callback as $cb) { |
| 73 | $function = $cb['function']; |
| 74 | |
| 75 | // Function |
| 76 | if (is_scalar($function)) { |
| 77 | $messages[] = sprintf('%s is called %s()', __('Function', 'codepress-admin-columns'), $function); |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | // Method |
| 82 | if (is_array($function)) { |
| 83 | $class_name = is_object($function[0]) ? get_class($function[0]) : $function[0]; |
| 84 | $messages[] = sprintf('%s::%s()', $class_name, $function[1]); |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Anonymous function |
| 89 | if ($function instanceof Closure) { |
| 90 | $reflection = new ReflectionFunction($function); |
| 91 | $closure = $reflection->getClosureScopeClass(); |
| 92 | |
| 93 | $class_name = $closure |
| 94 | ? $closure->getName() |
| 95 | : null; |
| 96 | |
| 97 | if ($class_name) { |
| 98 | $messages[] = sprintf( |
| 99 | '%s is called from %s', |
| 100 | __('Anonymous function', 'codepress-admin-columns'), |
| 101 | $class_name, |
| 102 | ); |
| 103 | |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | $messages[] = sprintf( |
| 108 | '%s is called from %s', |
| 109 | __('Anonymous Function', 'codepress-admin-columns'), |
| 110 | $reflection->getFileName(), |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (! $messages) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | natcasesort($messages); |
| 121 | |
| 122 | return $messages; |
| 123 | } |
| 124 | |
| 125 | } |
| 126 |