Cache
7 months ago
DBPermissions.php
9 months ago
DataEncoder.php
10 months ago
DatabaseOptions.php
2 weeks ago
Escape.php
9 months ago
Glob.php
2 years ago
Hooks.php
2 months ago
Math.php
9 months ago
PluginInfo.php
5 months ago
Sanitize.php
2 months ago
ServerVars.php
2 years ago
SlashMode.php
5 years ago
Strings.php
7 months ago
Times.php
5 months ago
Urls.php
2 weeks ago
Version.php
1 year ago
WpDefaultDirectories.php
2 years ago
Hooks.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | /** |
| 6 | * WP Staging wrapper for WordPress hooks functions. |
| 7 | */ |
| 8 | class Hooks |
| 9 | { |
| 10 | /** |
| 11 | * @var array<string, callable> |
| 12 | */ |
| 13 | private $internalHooks; |
| 14 | |
| 15 | /** |
| 16 | * @param string $hookName |
| 17 | * @param callable $callback |
| 18 | * @return void |
| 19 | */ |
| 20 | public function registerInternalHook(string $hookName, $callback) |
| 21 | { |
| 22 | if (!is_callable($callback)) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if (isset($this->internalHooks[$hookName])) { |
| 27 | $this->unregisterInternalHook($hookName); |
| 28 | } |
| 29 | |
| 30 | $this->internalHooks[$hookName] = $callback; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param string $hookName |
| 35 | * @return void |
| 36 | */ |
| 37 | public function unregisterInternalHook(string $hookName) |
| 38 | { |
| 39 | if (isset($this->internalHooks[$hookName])) { |
| 40 | unset($this->internalHooks[$hookName]); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param string $hookName |
| 46 | * @param array $args [] |
| 47 | * @param mixed $defaultValue null |
| 48 | * @return mixed |
| 49 | */ |
| 50 | public function callInternalHook(string $hookName, array $args = [], $defaultValue = null) |
| 51 | { |
| 52 | if (isset($this->internalHooks[$hookName]) && is_callable($this->internalHooks[$hookName])) { |
| 53 | return call_user_func_array($this->internalHooks[$hookName], $args); |
| 54 | } |
| 55 | |
| 56 | return $defaultValue; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param string $hookName |
| 61 | * @param mixed ...$args |
| 62 | * @return void |
| 63 | */ |
| 64 | public function doAction(string $hookName, ...$args) |
| 65 | { |
| 66 | if (!function_exists('do_action') || !$this->isHookAllowed($hookName)) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | do_action($hookName, ...$args); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param string $hookName |
| 75 | * @param mixed $value |
| 76 | * @param mixed ...$args |
| 77 | * @return mixed |
| 78 | */ |
| 79 | public function applyFilters(string $hookName, $value, ...$args) |
| 80 | { |
| 81 | if (!function_exists('apply_filters') || !$this->isHookAllowed($hookName)) { |
| 82 | return $value; |
| 83 | } |
| 84 | |
| 85 | return apply_filters($hookName, $value, ...$args); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $hookName |
| 90 | * @return bool |
| 91 | */ |
| 92 | private function isHookAllowed(string $hookName): bool |
| 93 | { |
| 94 | if (!$this->isWpstgHook($hookName)) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | // Block test hooks outside of test context |
| 99 | if (strpos($hookName, 'wpstg.tests.') === 0 && !$this->isTest()) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param string $hookName |
| 108 | * @return bool |
| 109 | */ |
| 110 | private function isWpstgHook(string $hookName): bool |
| 111 | { |
| 112 | return strpos($hookName, 'wpstg.') === 0 || strpos($hookName, 'wpstg_') === 0; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return bool |
| 117 | */ |
| 118 | protected function isTest(): bool |
| 119 | { |
| 120 | return defined('WPSTG_TEST') && constant('WPSTG_TEST') === true; |
| 121 | } |
| 122 | } |
| 123 |