| 1 |
<?php |
| 2 |
/** |
| 3 |
* Hook Registry class. |
| 4 |
* |
| 5 |
* @package WebberZone\Top_Ten\Util |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WebberZone\Top_Ten\Util; |
| 9 |
|
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Hook Registry class for managing WordPress actions and filters. |
| 16 |
* |
| 17 |
* @since 4.0.0 |
| 18 |
*/ |
| 19 |
class Hook_Registry { |
| 20 |
|
| 21 |
/** |
| 22 |
* Registered hooks. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
public static $hooks = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Register a hook (action or filter). |
| 30 |
* |
| 31 |
* @param string $hook_type Either 'action' or 'filter'. |
| 32 |
* @param string $hook_name The hook name. |
| 33 |
* @param callable $callback The callback function. |
| 34 |
* @param int $priority Priority of the hook. |
| 35 |
* @param int $args Number of arguments. |
| 36 |
* |
| 37 |
* @return bool True if registered, false if duplicate or invalid hook. |
| 38 |
*/ |
| 39 |
public static function register( string $hook_type, string $hook_name, callable $callback, int $priority = 10, int $args = 1 ): bool { |
| 40 |
if ( ! in_array( $hook_type, array( 'action', 'filter' ), true ) ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
if ( empty( trim( $hook_name ) ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
if ( $priority < 0 ) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
if ( $args < 1 ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$closure_id = ''; |
| 54 |
if ( $callback instanceof \Closure ) { |
| 55 |
$closure_id = uniqid( 'closure_', true ); |
| 56 |
} |
| 57 |
|
| 58 |
$key = self::create_hook_key( $hook_name, $callback, $priority, $closure_id ); |
| 59 |
|
| 60 |
if ( isset( self::$hooks[ $key ] ) ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
// Store the hook details. |
| 65 |
self::$hooks[ $key ] = array( |
| 66 |
'type' => $hook_type, |
| 67 |
'name' => $hook_name, |
| 68 |
'callback' => $callback, |
| 69 |
'priority' => $priority, |
| 70 |
'args' => $args, |
| 71 |
'closure_id' => $closure_id, |
| 72 |
); |
| 73 |
|
| 74 |
// Register with WordPress. |
| 75 |
if ( 'action' === $hook_type ) { |
| 76 |
add_action( $hook_name, $callback, $priority, $args ); |
| 77 |
} else { |
| 78 |
add_filter( $hook_name, $callback, $priority, $args ); |
| 79 |
} |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Register an action. |
| 86 |
* |
| 87 |
* @param string $hook_name The hook name. |
| 88 |
* @param callable $callback The callback function. |
| 89 |
* @param int $priority Priority of the hook. |
| 90 |
* @param int $args Number of arguments. |
| 91 |
* |
| 92 |
* @return bool True if registered, false if duplicate or invalid hook. |
| 93 |
*/ |
| 94 |
public static function add_action( $hook_name, $callback, $priority = 10, $args = 1 ) { |
| 95 |
return self::register( 'action', $hook_name, $callback, $priority, $args ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Register a filter. |
| 100 |
* |
| 101 |
* @param string $hook_name The hook name. |
| 102 |
* @param callable $callback The callback function. |
| 103 |
* @param int $priority Priority of the hook. |
| 104 |
* @param int $args Number of arguments. |
| 105 |
* |
| 106 |
* @return bool True if registered, false if duplicate or invalid hook. |
| 107 |
*/ |
| 108 |
public static function add_filter( $hook_name, $callback, $priority = 10, $args = 1 ) { |
| 109 |
return self::register( 'filter', $hook_name, $callback, $priority, $args ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Remove a hook (action or filter). |
| 114 |
* |
| 115 |
* Note: Closures are removable only if registered via this class. |
| 116 |
* |
| 117 |
* @param string $hook_type Either 'action' or 'filter'. |
| 118 |
* @param string $hook_name The hook name. |
| 119 |
* @param callable $callback The callback function. |
| 120 |
* @param int $priority Priority of the hook. |
| 121 |
* |
| 122 |
* @return bool True if removed, false if not found or removal failed. |
| 123 |
*/ |
| 124 |
public static function remove( $hook_type, $hook_name, $callback, $priority = 10 ) { |
| 125 |
$closure_id = ''; |
| 126 |
if ( $callback instanceof \Closure ) { |
| 127 |
// Find the closure_id for this callback. |
| 128 |
foreach ( self::$hooks as $hook ) { |
| 129 |
if ( $hook['name'] === $hook_name && |
| 130 |
$hook['priority'] === $priority && |
| 131 |
$hook['type'] === $hook_type && |
| 132 |
$hook['callback'] === $callback ) { |
| 133 |
$closure_id = $hook['closure_id']; |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
$key = self::create_hook_key( $hook_name, $callback, $priority, $closure_id ); |
| 140 |
|
| 141 |
if ( ! isset( self::$hooks[ $key ] ) || self::$hooks[ $key ]['type'] !== $hook_type ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
$removed = false; |
| 146 |
if ( 'action' === $hook_type ) { |
| 147 |
$removed = remove_action( $hook_name, $callback, $priority ); |
| 148 |
} else { |
| 149 |
$removed = remove_filter( $hook_name, $callback, $priority ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( $removed ) { |
| 153 |
unset( self::$hooks[ $key ] ); |
| 154 |
} |
| 155 |
|
| 156 |
return $removed; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Remove an action. |
| 161 |
* |
| 162 |
* @param string $hook_name The hook name. |
| 163 |
* @param callable $callback The callback function. |
| 164 |
* @param int $priority Priority of the hook. |
| 165 |
* |
| 166 |
* @return bool True if removed, false if not found or removal failed. |
| 167 |
*/ |
| 168 |
public static function remove_action( $hook_name, $callback, $priority = 10 ) { |
| 169 |
return self::remove( 'action', $hook_name, $callback, $priority ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Remove a filter. |
| 174 |
* |
| 175 |
* @param string $hook_name The hook name. |
| 176 |
* @param callable $callback The callback function. |
| 177 |
* @param int $priority Priority of the hook. |
| 178 |
* |
| 179 |
* @return bool True if removed, false if not found or removal failed. |
| 180 |
*/ |
| 181 |
public static function remove_filter( $hook_name, $callback, $priority = 10 ) { |
| 182 |
return self::remove( 'filter', $hook_name, $callback, $priority ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get all registered hooks. |
| 187 |
* |
| 188 |
* @return array Array of registered hooks. |
| 189 |
*/ |
| 190 |
public static function get_hooks() { |
| 191 |
return self::$hooks; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Remove all registered hooks. |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public static function remove_all_hooks() { |
| 200 |
foreach ( self::$hooks as $key => $hook ) { |
| 201 |
if ( 'action' === $hook['type'] ) { |
| 202 |
remove_action( $hook['name'], $hook['callback'], $hook['priority'] ); |
| 203 |
} else { |
| 204 |
remove_filter( $hook['name'], $hook['callback'], $hook['priority'] ); |
| 205 |
} |
| 206 |
unset( self::$hooks[ $key ] ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Create a unique key for a hook registration. |
| 212 |
* |
| 213 |
* @param string $hook_name The hook name. |
| 214 |
* @param callable $callback The callback function. |
| 215 |
* @param int $priority Priority of the hook. |
| 216 |
* @param string $closure_id Unique ID for closures. |
| 217 |
* |
| 218 |
* @return string Unique key. |
| 219 |
*/ |
| 220 |
public static function create_hook_key( $hook_name, $callback, $priority, $closure_id = '' ) { |
| 221 |
return md5( $hook_name . self::callback_to_string( $callback ) . $priority . $closure_id ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Convert a callback to a string representation. |
| 226 |
* |
| 227 |
* @param callable $callback The callback to convert. |
| 228 |
* |
| 229 |
* @return string String representation of the callback. |
| 230 |
*/ |
| 231 |
public static function callback_to_string( $callback ) { |
| 232 |
if ( is_string( $callback ) ) { |
| 233 |
return $callback; |
| 234 |
} |
| 235 |
|
| 236 |
if ( is_array( $callback ) ) { |
| 237 |
if ( is_object( $callback[0] ) ) { |
| 238 |
$object_hash = spl_object_hash( $callback[0] ); |
| 239 |
return get_class( $callback[0] ) . '#' . $object_hash . '::' . $callback[1]; |
| 240 |
} |
| 241 |
return $callback[0] . '::' . $callback[1]; |
| 242 |
} |
| 243 |
|
| 244 |
if ( $callback instanceof \Closure ) { |
| 245 |
return 'closure'; |
| 246 |
} |
| 247 |
|
| 248 |
return 'unknown'; |
| 249 |
} |
| 250 |
} |
| 251 |
|