Changeset 50807
- Timestamp:
- 05/04/2021 10:46:26 AM (4 years ago)
- Location:
- trunk/src/wp-includes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-hook.php
r49929 r50807 59 59 60 60 /** 61 * Hooks a function or method to a specific filter action.62 * 63 * @since 4.7.0 64 * 65 * @param string $ tag The name of the filter to hook the $function_to_addcallback to.66 * @param callable $ function_to_addThe callback to be run when the filter is applied.67 * @param int $priority 68 * 69 * 70 * 71 * @param int $accepted_args 72 */ 73 public function add_filter( $ tag, $function_to_add, $priority, $accepted_args ) {74 $idx = _wp_filter_build_unique_id( $ tag, $function_to_add, $priority );61 * Adds a callback function to the specified filter hook. 62 * 63 * @since 4.7.0 64 * 65 * @param string $hook_name The name of the filter to add the callback to. 66 * @param callable $callback The callback to be run when the filter is applied. 67 * @param int $priority The order in which the functions associated with a particular action 68 * are executed. Lower numbers correspond with earlier execution, 69 * and functions with the same priority are executed in the order 70 * in which they were added to the action. 71 * @param int $accepted_args The number of arguments the function accepts. 72 */ 73 public function add_filter( $hook_name, $callback, $priority, $accepted_args ) { 74 $idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 75 75 76 76 $priority_existed = isset( $this->callbacks[ $priority ] ); 77 77 78 78 $this->callbacks[ $priority ][ $idx ] = array( 79 'function' => $ function_to_add,79 'function' => $callback, 80 80 'accepted_args' => $accepted_args, 81 81 ); … … 109 109 $this->iterations[ $index ] = $new_priorities; 110 110 } 111 111 112 return; 112 113 } 113 114 114 115 $min = min( $new_priorities ); 116 115 117 foreach ( $this->iterations as $index => &$iteration ) { 116 118 $current = current( $iteration ); 119 117 120 // If we're already at the end of this iteration, just leave the array pointer where it is. 118 121 if ( false === $current ) { … … 147 150 $prev = prev( $iteration ); 148 151 } 152 149 153 if ( false === $prev ) { 150 154 // Start of the array. Reset, and go about our day. … … 156 160 } 157 161 } 162 158 163 unset( $iteration ); 159 164 } 160 165 161 166 /** 162 * Unhooks a function or method from a specific filter action.163 * 164 * @since 4.7.0 165 * 166 * @param string $ tagThe filter hook to which the function to be removed is hooked.167 * @param callable $ function_to_removeThe callback to be removed from running when the filter is applied.168 * @param int $priority 167 * Removes a callback function from the specified filter hook. 168 * 169 * @since 4.7.0 170 * 171 * @param string $hook_name The filter hook to which the function to be removed is hooked. 172 * @param callable $callback The callback to be removed from running when the filter is applied. 173 * @param int $priority The exact priority used when adding the original filter callback. 169 174 * @return bool Whether the callback existed before it was removed. 170 175 */ 171 public function remove_filter( $ tag, $function_to_remove, $priority ) {172 $function_key = _wp_filter_build_unique_id( $ tag, $function_to_remove, $priority );176 public function remove_filter( $hook_name, $callback, $priority ) { 177 $function_key = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 173 178 174 179 $exists = isset( $this->callbacks[ $priority ][ $function_key ] ); 180 175 181 if ( $exists ) { 176 182 unset( $this->callbacks[ $priority ][ $function_key ] ); 183 177 184 if ( ! $this->callbacks[ $priority ] ) { 178 185 unset( $this->callbacks[ $priority ] ); 186 179 187 if ( $this->nesting_level > 0 ) { 180 188 $this->resort_active_iterations(); … … 182 190 } 183 191 } 192 184 193 return $exists; 185 194 } 186 195 187 196 /** 188 * Checks if a specific actionhas been registered for this hook.189 * 190 * When using the `$ function_to_check` argument, this function may return a non-boolean value197 * Checks if a specific callback has been registered for this hook. 198 * 199 * When using the `$callback` argument, this function may return a non-boolean value 191 200 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. 192 201 * 193 202 * @since 4.7.0 194 203 * 195 * @param string $ tagOptional. The name of the filter hook. Default empty.196 * @param callable|false $ function_to_checkOptional. The callback to check for. Default false.197 * @return bool|int If `$ function_to_check` is omitted, returns boolean for whether the hook has198 * anything registered. When checking a specific function, the priority of that199 * hook is returned, or false if the function is not attached.200 */ 201 public function has_filter( $ tag = '', $function_to_check = false ) {202 if ( false === $ function_to_check ) {204 * @param string $hook_name Optional. The name of the filter hook. Default empty. 205 * @param callable|false $callback Optional. The callback to check for. Default false. 206 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 207 * anything registered. When checking a specific function, the priority 208 * of that hook is returned, or false if the function is not attached. 209 */ 210 public function has_filter( $hook_name = '', $callback = false ) { 211 if ( false === $callback ) { 203 212 return $this->has_filters(); 204 213 } 205 214 206 $function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false ); 215 $function_key = _wp_filter_build_unique_id( $hook_name, $callback, false ); 216 207 217 if ( ! $function_key ) { 208 218 return false; … … 231 241 } 232 242 } 243 233 244 return false; 234 245 } … … 335 346 do { 336 347 $priority = current( $this->iterations[ $nesting_level ] ); 348 337 349 foreach ( $this->callbacks[ $priority ] as $the_ ) { 338 350 call_user_func_array( $the_['function'], $args ); … … 349 361 * @since 4.7.0 350 362 * 351 * @return int|false If the hook is running, return the current priority level. If it isn't running, return false. 363 * @return int|false If the hook is running, return the current priority level. 364 * If it isn't running, return false. 352 365 */ 353 366 public function current_priority() { … … 392 405 $normalized = array(); 393 406 394 foreach ( $filters as $ tag=> $callback_groups ) {407 foreach ( $filters as $hook_name => $callback_groups ) { 395 408 if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) { 396 $normalized[ $ tag] = $callback_groups;409 $normalized[ $hook_name ] = $callback_groups; 397 410 continue; 398 411 } 412 399 413 $hook = new WP_Hook(); 400 414 … … 404 418 // Loop through callbacks. 405 419 foreach ( $callbacks as $cb ) { 406 $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] ); 407 } 408 } 409 $normalized[ $tag ] = $hook; 410 } 420 $hook->add_filter( $hook_name, $cb['function'], $priority, $cb['accepted_args'] ); 421 } 422 } 423 424 $normalized[ $hook_name ] = $hook; 425 } 426 411 427 return $normalized; 412 428 } -
trunk/src/wp-includes/plugin.php
r49929 r50807 49 49 50 50 /** 51 * Hook a function or method to a specific filter action.51 * Adds a callback function to a filter hook. 52 52 * 53 53 * WordPress offers filter hooks to allow plugins to modify … … 102 102 * @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. 103 103 * 104 * @param string $ tag The name of the filter to hook the $function_to_addcallback to.105 * @param callable $ function_to_addThe callback to be run when the filter is applied.106 * @param int $priority 107 * associated with a particular actionare executed.108 * 109 * 110 * in the order in which they were added to the action. Default 10.111 * @param int $accepted_args 112 * @return true 113 */ 114 function add_filter( $ tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {104 * @param string $hook_name The name of the filter to add the callback to. 105 * @param callable $callback The callback to be run when the filter is applied. 106 * @param int $priority Optional. Used to specify the order in which the functions 107 * associated with a particular filter are executed. 108 * Lower numbers correspond with earlier execution, 109 * and functions with the same priority are executed 110 * in the order in which they were added to the filter. Default 10. 111 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. 112 * @return true Always returns true. 113 */ 114 function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { 115 115 global $wp_filter; 116 if ( ! isset( $wp_filter[ $tag ] ) ) { 117 $wp_filter[ $tag ] = new WP_Hook(); 118 } 119 $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args ); 116 117 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 118 $wp_filter[ $hook_name ] = new WP_Hook(); 119 } 120 121 $wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args ); 122 120 123 return true; 121 124 } 122 125 123 126 /** 124 * Checks if any filter has been registered for a hook.125 *126 * When using the `$function_to_check` argument, this function may return a non-boolean value127 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.128 *129 * @since 2.5.0130 *131 * @global WP_Hook[] $wp_filter Stores all of the filters and actions.132 *133 * @param string $tag The name of the filter hook.134 * @param callable|false $function_to_check Optional. The callback to check for. Default false.135 * @return bool|int If `$function_to_check` is omitted, returns boolean for whether the hook has136 * anything registered. When checking a specific function, the priority of that137 * hook is returned, or false if the function is not attached.138 */139 function has_filter( $tag, $function_to_check = false ) {140 global $wp_filter;141 142 if ( ! isset( $wp_filter[ $tag ] ) ) {143 return false;144 }145 146 return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check );147 }148 149 /**150 127 * Calls the callback functions that have been added to a filter hook. 151 128 * 152 * The callback functions attached to the filter hook are invoked by calling 153 * this function. This function can be used to create a new filter hook by 154 * simply calling this function with the name of the new hook specified using 155 * the `$tag` parameter. 129 * This function invokes all functions attached to filter hook `$hook_name`. 130 * It is possible to create new filter hooks by simply calling this function, 131 * specifying the name of the new hook using the `$hook_name` parameter. 156 132 * 157 133 * The function also allows for multiple additional arguments to be passed to hooks. … … 180 156 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 181 157 * 182 * @param string $ tagThe name of the filter hook.183 * @param mixed $value The value to filter.184 * @param mixed ...$args Additional parameters to pass to the callback functions.158 * @param string $hook_name The name of the filter hook. 159 * @param mixed $value The value to filter. 160 * @param mixed ...$args Additional parameters to pass to the callback functions. 185 161 * @return mixed The filtered value after all hooked functions are applied to it. 186 162 */ 187 function apply_filters( $ tag, $value ) {163 function apply_filters( $hook_name, $value ) { 188 164 global $wp_filter, $wp_current_filter; 189 165 … … 192 168 // Do 'all' actions first. 193 169 if ( isset( $wp_filter['all'] ) ) { 194 $wp_current_filter[] = $ tag;170 $wp_current_filter[] = $hook_name; 195 171 _wp_call_all_hook( $args ); 196 172 } 197 173 198 if ( ! isset( $wp_filter[ $ tag] ) ) {174 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 199 175 if ( isset( $wp_filter['all'] ) ) { 200 176 array_pop( $wp_current_filter ); 201 177 } 178 202 179 return $value; 203 180 } 204 181 205 182 if ( ! isset( $wp_filter['all'] ) ) { 206 $wp_current_filter[] = $ tag;183 $wp_current_filter[] = $hook_name; 207 184 } 208 185 … … 210 187 array_shift( $args ); 211 188 212 $filtered = $wp_filter[ $ tag]->apply_filters( $value, $args );189 $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args ); 213 190 214 191 array_pop( $wp_current_filter ); … … 223 200 * 224 201 * @see apply_filters() This function is identical, but the arguments passed to the 225 * functions hooked to `$tag` are supplied using an array.202 * functions hooked to `$hook_name` are supplied using an array. 226 203 * 227 204 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 228 205 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 229 206 * 230 * @param string $ tagThe name of the filter hook.231 * @param array $args The arguments supplied to the functions hooked to $tag.207 * @param string $hook_name The name of the filter hook. 208 * @param array $args The arguments supplied to the functions hooked to `$hook_name`. 232 209 * @return mixed The filtered value after all hooked functions are applied to it. 233 210 */ 234 function apply_filters_ref_array( $ tag, $args ) {211 function apply_filters_ref_array( $hook_name, $args ) { 235 212 global $wp_filter, $wp_current_filter; 236 213 237 214 // Do 'all' actions first. 238 215 if ( isset( $wp_filter['all'] ) ) { 239 $wp_current_filter[] = $ tag;216 $wp_current_filter[] = $hook_name; 240 217 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 241 218 _wp_call_all_hook( $all_args ); 242 219 } 243 220 244 if ( ! isset( $wp_filter[ $ tag] ) ) {221 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 245 222 if ( isset( $wp_filter['all'] ) ) { 246 223 array_pop( $wp_current_filter ); 247 224 } 225 248 226 return $args[0]; 249 227 } 250 228 251 229 if ( ! isset( $wp_filter['all'] ) ) { 252 $wp_current_filter[] = $ tag;253 } 254 255 $filtered = $wp_filter[ $ tag]->apply_filters( $args[0], $args );230 $wp_current_filter[] = $hook_name; 231 } 232 233 $filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args ); 256 234 257 235 array_pop( $wp_current_filter ); … … 261 239 262 240 /** 263 * Removes a function from a specified filter hook. 264 * 265 * This function removes a function attached to a specified filter hook. This 266 * method can be used to remove default functions attached to a specific filter 241 * Checks if any filter has been registered for a hook. 242 * 243 * When using the `$callback` argument, this function may return a non-boolean value 244 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. 245 * 246 * @since 2.5.0 247 * 248 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 249 * 250 * @param string $hook_name The name of the filter hook. 251 * @param callable|false $callback Optional. The callback to check for. Default false. 252 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 253 * anything registered. When checking a specific function, the priority 254 * of that hook is returned, or false if the function is not attached. 255 */ 256 function has_filter( $hook_name, $callback = false ) { 257 global $wp_filter; 258 259 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 260 return false; 261 } 262 263 return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback ); 264 } 265 266 /** 267 * Removes a callback function from a filter hook. 268 * 269 * This can be used to remove default functions attached to a specific filter 267 270 * hook and possibly replace them with a substitute. 268 271 * 269 * To remove a hook, the $function_to_remove and $priorityarguments must match272 * To remove a hook, the `$callback` and `$priority` arguments must match 270 273 * when the hook was added. This goes for both filters and actions. No warning 271 274 * will be given on removal failure. … … 275 278 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 276 279 * 277 * @param string $ tagThe filter hook to which the function to be removed is hooked.278 * @param callable $ function_to_removeThe name of the function which should be removed.279 * @param int $priority 280 * @return bool 281 */ 282 function remove_filter( $ tag, $function_to_remove, $priority = 10 ) {280 * @param string $hook_name The filter hook to which the function to be removed is hooked. 281 * @param callable $callback The name of the function which should be removed. 282 * @param int $priority Optional. The priority of the function. Default 10. 283 * @return bool Whether the function existed before it was removed. 284 */ 285 function remove_filter( $hook_name, $callback, $priority = 10 ) { 283 286 global $wp_filter; 284 287 285 288 $r = false; 286 if ( isset( $wp_filter[ $tag ] ) ) { 287 $r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority ); 288 if ( ! $wp_filter[ $tag ]->callbacks ) { 289 unset( $wp_filter[ $tag ] ); 289 290 if ( isset( $wp_filter[ $hook_name ] ) ) { 291 $r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority ); 292 293 if ( ! $wp_filter[ $hook_name ]->callbacks ) { 294 unset( $wp_filter[ $hook_name ] ); 290 295 } 291 296 } … … 295 300 296 301 /** 297 * Remove all of the hooks from a filter.302 * Removes all of the callback functions from a filter hook. 298 303 * 299 304 * @since 2.7.0 … … 301 306 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. 302 307 * 303 * @param string $tag The filter to remove hooks from. 304 * @param int|false $priority Optional. The priority number to remove. Default false. 305 * @return true True when finished. 306 */ 307 function remove_all_filters( $tag, $priority = false ) { 308 * @param string $hook_name The filter to remove callbacks from. 309 * @param int|false $priority Optional. The priority number to remove them from. 310 * Default false. 311 * @return true Always returns true. 312 */ 313 function remove_all_filters( $hook_name, $priority = false ) { 308 314 global $wp_filter; 309 315 310 if ( isset( $wp_filter[ $tag ] ) ) { 311 $wp_filter[ $tag ]->remove_all_filters( $priority ); 312 if ( ! $wp_filter[ $tag ]->has_filters() ) { 313 unset( $wp_filter[ $tag ] ); 316 if ( isset( $wp_filter[ $hook_name ] ) ) { 317 $wp_filter[ $hook_name ]->remove_all_filters( $priority ); 318 319 if ( ! $wp_filter[ $hook_name ]->has_filters() ) { 320 unset( $wp_filter[ $hook_name ] ); 314 321 } 315 322 } … … 319 326 320 327 /** 321 * Retrieve the name of the current filter or action.328 * Retrieves the name of the current filter hook. 322 329 * 323 330 * @since 2.5.0 … … 325 332 * @global string[] $wp_current_filter Stores the list of current filters with the current one last 326 333 * 327 * @return string Hook name of the current filter or action.334 * @return string Hook name of the current filter. 328 335 */ 329 336 function current_filter() { 330 337 global $wp_current_filter; 338 331 339 return end( $wp_current_filter ); 332 340 } 333 341 334 342 /** 335 * Retrieve the name of the current action. 336 * 337 * @since 3.9.0 338 * 339 * @return string Hook name of the current action. 340 */ 341 function current_action() { 342 return current_filter(); 343 } 344 345 /** 346 * Retrieve the name of a filter currently being processed. 343 * Returns whether or not a filter hook is currently being processed. 347 344 * 348 345 * The function current_filter() only returns the most recent filter or action … … 350 347 * processed. 351 348 * 352 * This function allows detection for any filter currently being 353 * executed (despite not beingthe most recent filter to fire, in the case of349 * This function allows detection for any filter currently being executed 350 * (regardless of whether it's the most recent filter to fire, in the case of 354 351 * hooks called from hook callbacks) to be verified. 355 352 * … … 360 357 * @global string[] $wp_current_filter Current filter. 361 358 * 362 * @param null|string $ filter Optional. Filter to check. Defaults to null, which363 * checks if any filter is currently being run.359 * @param null|string $hook_name Optional. Filter hook to check. Defaults to null, 360 * which checks if any filter is currently being run. 364 361 * @return bool Whether the filter is currently in the stack. 365 362 */ 366 function doing_filter( $ filter= null ) {363 function doing_filter( $hook_name = null ) { 367 364 global $wp_current_filter; 368 365 369 if ( null === $ filter) {366 if ( null === $hook_name ) { 370 367 return ! empty( $wp_current_filter ); 371 368 } 372 369 373 return in_array( $filter, $wp_current_filter, true ); 374 } 375 376 /** 377 * Retrieve the name of an action currently being processed. 378 * 379 * @since 3.9.0 380 * 381 * @param string|null $action Optional. Action to check. Defaults to null, which checks 382 * if any action is currently being run. 383 * @return bool Whether the action is currently in the stack. 384 */ 385 function doing_action( $action = null ) { 386 return doing_filter( $action ); 387 } 388 389 /** 390 * Hooks a function on to a specific action. 370 return in_array( $hook_name, $wp_current_filter, true ); 371 } 372 373 /** 374 * Adds a callback function to an action hook. 391 375 * 392 376 * Actions are the hooks that the WordPress core launches at specific points … … 397 381 * @since 1.2.0 398 382 * 399 * @param string $ tag The name of the action to which the $function_to_add is hooked.400 * @param callable $ function_to_add The name of the function you wish to becalled.383 * @param string $hook_name The name of the action to add the callback to. 384 * @param callable $callback The callback to be run when the action is called. 401 385 * @param int $priority Optional. Used to specify the order in which the functions 402 * associated with a particular action are executed. Default 10.386 * associated with a particular action are executed. 403 387 * Lower numbers correspond with earlier execution, 404 388 * and functions with the same priority are executed 405 * in the order in which they were added to the action. 389 * in the order in which they were added to the action. Default 10. 406 390 * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. 407 * @return true Will always returntrue.408 */ 409 function add_action( $ tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {410 return add_filter( $ tag, $function_to_add, $priority, $accepted_args );411 } 412 413 /** 414 * Execute functions hooked on a specificaction hook.415 * 416 * This function invokes all functions attached to action hook `$ tag`. It is417 * possible to create new action hooks by simply calling this function,418 * specifying the name of the new hook using the `$ tag` parameter.391 * @return true Always returns true. 392 */ 393 function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) { 394 return add_filter( $hook_name, $callback, $priority, $accepted_args ); 395 } 396 397 /** 398 * Calls the callback functions that have been added to an action hook. 399 * 400 * This function invokes all functions attached to action hook `$hook_name`. 401 * It is possible to create new action hooks by simply calling this function, 402 * specifying the name of the new hook using the `$hook_name` parameter. 419 403 * 420 404 * You can pass extra arguments to the hooks, much like you can with `apply_filters()`. … … 444 428 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 445 429 * 446 * @param string $ tagThe name of the action to be executed.447 * @param mixed ...$arg Optional. Additional arguments which are passed on to the448 * functions hooked to the action. Default empty.449 */ 450 function do_action( $ tag, ...$arg ) {430 * @param string $hook_name The name of the action to be executed. 431 * @param mixed ...$arg Optional. Additional arguments which are passed on to the 432 * functions hooked to the action. Default empty. 433 */ 434 function do_action( $hook_name, ...$arg ) { 451 435 global $wp_filter, $wp_actions, $wp_current_filter; 452 436 453 if ( ! isset( $wp_actions[ $ tag] ) ) {454 $wp_actions[ $ tag] = 1;437 if ( ! isset( $wp_actions[ $hook_name ] ) ) { 438 $wp_actions[ $hook_name ] = 1; 455 439 } else { 456 ++$wp_actions[ $ tag];440 ++$wp_actions[ $hook_name ]; 457 441 } 458 442 459 443 // Do 'all' actions first. 460 444 if ( isset( $wp_filter['all'] ) ) { 461 $wp_current_filter[] = $ tag;445 $wp_current_filter[] = $hook_name; 462 446 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 463 447 _wp_call_all_hook( $all_args ); 464 448 } 465 449 466 if ( ! isset( $wp_filter[ $ tag] ) ) {450 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 467 451 if ( isset( $wp_filter['all'] ) ) { 468 452 array_pop( $wp_current_filter ); 469 453 } 454 470 455 return; 471 456 } 472 457 473 458 if ( ! isset( $wp_filter['all'] ) ) { 474 $wp_current_filter[] = $ tag;459 $wp_current_filter[] = $hook_name; 475 460 } 476 461 … … 482 467 } 483 468 484 $wp_filter[ $ tag]->do_action( $arg );469 $wp_filter[ $hook_name ]->do_action( $arg ); 485 470 486 471 array_pop( $wp_current_filter ); … … 488 473 489 474 /** 490 * Retrieve the number of times an action is fired.475 * Calls the callback functions that have been added to an action hook, specifying arguments in an array. 491 476 * 492 477 * @since 2.1.0 493 478 * 494 * @global int[] $wp_actions Stores the number of times each action was triggered.495 *496 * @param string $tag The name of the action hook.497 * @return int The number of times action hook $tag is fired.498 */499 function did_action( $tag ) {500 global $wp_actions;501 502 if ( ! isset( $wp_actions[ $tag ] ) ) {503 return 0;504 }505 506 return $wp_actions[ $tag ];507 }508 509 /**510 * Calls the callback functions that have been added to an action hook, specifying arguments in an array.511 *512 * @since 2.1.0513 *514 479 * @see do_action() This function is identical, but the arguments passed to the 515 * functions hooked to `$ tag` are supplied using an array.480 * functions hooked to `$hook_name` are supplied using an array. 516 481 * 517 482 * @global WP_Hook[] $wp_filter Stores all of the filters and actions. … … 519 484 * @global string[] $wp_current_filter Stores the list of current filters with the current one last. 520 485 * 521 * @param string $ tagThe name of the action to be executed.522 * @param array $args The arguments supplied to the functions hooked to `$tag`.523 */ 524 function do_action_ref_array( $ tag, $args ) {486 * @param string $hook_name The name of the action to be executed. 487 * @param array $args The arguments supplied to the functions hooked to `$hook_name`. 488 */ 489 function do_action_ref_array( $hook_name, $args ) { 525 490 global $wp_filter, $wp_actions, $wp_current_filter; 526 491 527 if ( ! isset( $wp_actions[ $ tag] ) ) {528 $wp_actions[ $ tag] = 1;492 if ( ! isset( $wp_actions[ $hook_name ] ) ) { 493 $wp_actions[ $hook_name ] = 1; 529 494 } else { 530 ++$wp_actions[ $ tag];495 ++$wp_actions[ $hook_name ]; 531 496 } 532 497 533 498 // Do 'all' actions first. 534 499 if ( isset( $wp_filter['all'] ) ) { 535 $wp_current_filter[] = $ tag;500 $wp_current_filter[] = $hook_name; 536 501 $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection 537 502 _wp_call_all_hook( $all_args ); 538 503 } 539 504 540 if ( ! isset( $wp_filter[ $ tag] ) ) {505 if ( ! isset( $wp_filter[ $hook_name ] ) ) { 541 506 if ( isset( $wp_filter['all'] ) ) { 542 507 array_pop( $wp_current_filter ); 543 508 } 509 544 510 return; 545 511 } 546 512 547 513 if ( ! isset( $wp_filter['all'] ) ) { 548 $wp_current_filter[] = $ tag;549 } 550 551 $wp_filter[ $ tag]->do_action( $args );514 $wp_current_filter[] = $hook_name; 515 } 516 517 $wp_filter[ $hook_name ]->do_action( $args ); 552 518 553 519 array_pop( $wp_current_filter ); … … 557 523 * Checks if any action has been registered for a hook. 558 524 * 559 * When using the `$ function_to_check` argument, this function may return a non-boolean value525 * When using the `$callback` argument, this function may return a non-boolean value 560 526 * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value. 561 527 * … … 564 530 * @see has_filter() has_action() is an alias of has_filter(). 565 531 * 566 * @param string $tag The name of the action hook. 567 * @param callable|false $function_to_check Optional. The callback to check for. Default false. 568 * @return bool|int If `$function_to_check` is omitted, returns boolean for whether the hook has 569 * anything registered. When checking a specific function, the priority of that 570 * hook is returned, or false if the function is not attached. 571 */ 572 function has_action( $tag, $function_to_check = false ) { 573 return has_filter( $tag, $function_to_check ); 574 } 575 576 /** 577 * Removes a function from a specified action hook. 578 * 579 * This function removes a function attached to a specified action hook. This 580 * method can be used to remove default functions attached to a specific filter 532 * @param string $hook_name The name of the action hook. 533 * @param callable|false $callback Optional. The callback to check for. Default false. 534 * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has 535 * anything registered. When checking a specific function, the priority 536 * of that hook is returned, or false if the function is not attached. 537 */ 538 function has_action( $hook_name, $callback = false ) { 539 return has_filter( $hook_name, $callback ); 540 } 541 542 /** 543 * Removes a callback function from an action hook. 544 * 545 * This can be used to remove default functions attached to a specific action 581 546 * hook and possibly replace them with a substitute. 582 547 * 548 * To remove a hook, the `$callback` and `$priority` arguments must match 549 * when the hook was added. This goes for both filters and actions. No warning 550 * will be given on removal failure. 551 * 583 552 * @since 1.2.0 584 553 * 585 * @param string $ tagThe action hook to which the function to be removed is hooked.586 * @param callable $ function_to_removeThe name of the function which should be removed.587 * @param int $priority 554 * @param string $hook_name The action hook to which the function to be removed is hooked. 555 * @param callable $callback The name of the function which should be removed. 556 * @param int $priority Optional. The priority of the function. Default 10. 588 557 * @return bool Whether the function is removed. 589 558 */ 590 function remove_action( $ tag, $function_to_remove, $priority = 10 ) {591 return remove_filter( $ tag, $function_to_remove, $priority );592 } 593 594 /** 595 * Remove all of the hooks from an action.559 function remove_action( $hook_name, $callback, $priority = 10 ) { 560 return remove_filter( $hook_name, $callback, $priority ); 561 } 562 563 /** 564 * Removes all of the callback functions from an action hook. 596 565 * 597 566 * @since 2.7.0 598 567 * 599 * @param string $tag The action to remove hooks from. 600 * @param int|false $priority The priority number to remove them from. Default false. 601 * @return true True when finished. 602 */ 603 function remove_all_actions( $tag, $priority = false ) { 604 return remove_all_filters( $tag, $priority ); 568 * @param string $hook_name The action to remove callbacks from. 569 * @param int|false $priority Optional. The priority number to remove them from. 570 * Default false. 571 * @return true Always returns true. 572 */ 573 function remove_all_actions( $hook_name, $priority = false ) { 574 return remove_all_filters( $hook_name, $priority ); 575 } 576 577 /** 578 * Retrieves the name of the current action hook. 579 * 580 * @since 3.9.0 581 * 582 * @return string Hook name of the current action. 583 */ 584 function current_action() { 585 return current_filter(); 586 } 587 588 /** 589 * Returns whether or not an action hook is currently being processed. 590 * 591 * @since 3.9.0 592 * 593 * @param string|null $hook_name Optional. Action hook to check. Defaults to null, 594 * which checks if any action is currently being run. 595 * @return bool Whether the action is currently in the stack. 596 */ 597 function doing_action( $hook_name = null ) { 598 return doing_filter( $hook_name ); 599 } 600 601 /** 602 * Retrieves the number of times an action has been fired during the current request. 603 * 604 * @since 2.1.0 605 * 606 * @global int[] $wp_actions Stores the number of times each action was triggered. 607 * 608 * @param string $hook_name The name of the action hook. 609 * @return int The number of times the action hook has been fired. 610 */ 611 function did_action( $hook_name ) { 612 global $wp_actions; 613 614 if ( ! isset( $wp_actions[ $hook_name ] ) ) { 615 return 0; 616 } 617 618 return $wp_actions[ $hook_name ]; 605 619 } 606 620 … … 625 639 * @see _deprecated_hook() 626 640 * 627 * @param string $ tagThe name of the filter hook.641 * @param string $hook_name The name of the filter hook. 628 642 * @param array $args Array of additional function arguments to be passed to apply_filters(). 629 643 * @param string $version The version of WordPress that deprecated the hook. … … 631 645 * @param string $message Optional. A message regarding the change. Default empty. 632 646 */ 633 function apply_filters_deprecated( $ tag, $args, $version, $replacement = '', $message = '' ) {634 if ( ! has_filter( $ tag) ) {647 function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { 648 if ( ! has_filter( $hook_name ) ) { 635 649 return $args[0]; 636 650 } 637 651 638 _deprecated_hook( $ tag, $version, $replacement, $message );639 640 return apply_filters_ref_array( $ tag, $args );652 _deprecated_hook( $hook_name, $version, $replacement, $message ); 653 654 return apply_filters_ref_array( $hook_name, $args ); 641 655 } 642 656 … … 652 666 * @see _deprecated_hook() 653 667 * 654 * @param string $ tagThe name of the action hook.668 * @param string $hook_name The name of the action hook. 655 669 * @param array $args Array of additional function arguments to be passed to do_action(). 656 670 * @param string $version The version of WordPress that deprecated the hook. … … 658 672 * @param string $message Optional. A message regarding the change. Default empty. 659 673 */ 660 function do_action_deprecated( $ tag, $args, $version, $replacement = '', $message = '' ) {661 if ( ! has_action( $ tag) ) {674 function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) { 675 if ( ! has_action( $hook_name ) ) { 662 676 return; 663 677 } 664 678 665 _deprecated_hook( $ tag, $version, $replacement, $message );666 667 do_action_ref_array( $ tag, $args );679 _deprecated_hook( $hook_name, $version, $replacement, $message ); 680 681 do_action_ref_array( $hook_name, $args ); 668 682 } 669 683 … … 691 705 692 706 arsort( $wp_plugin_paths ); 707 693 708 foreach ( $wp_plugin_paths as $dir => $realdir ) { 694 709 if ( strpos( $file, $realdir ) === 0 ) { … … 725 740 // Normalize, but store as static to avoid recalculation of a constant value. 726 741 static $wp_plugin_path = null, $wpmu_plugin_path = null; 742 727 743 if ( ! isset( $wp_plugin_path ) ) { 728 744 $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR ); … … 784 800 * 785 801 * @param string $file The filename of the plugin including the path. 786 * @param callable $ functionThe function hooked to the 'activate_PLUGIN' action.787 */ 788 function register_activation_hook( $file, $ function) {802 * @param callable $callback The function hooked to the 'activate_PLUGIN' action. 803 */ 804 function register_activation_hook( $file, $callback ) { 789 805 $file = plugin_basename( $file ); 790 add_action( 'activate_' . $file, $ function);791 } 792 793 /** 794 * Set the deactivation hook for a plugin.806 add_action( 'activate_' . $file, $callback ); 807 } 808 809 /** 810 * Sets the deactivation hook for a plugin. 795 811 * 796 812 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is … … 807 823 * 808 824 * @param string $file The filename of the plugin including the path. 809 * @param callable $ functionThe function hooked to the 'deactivate_PLUGIN' action.810 */ 811 function register_deactivation_hook( $file, $ function) {825 * @param callable $callback The function hooked to the 'deactivate_PLUGIN' action. 826 */ 827 function register_deactivation_hook( $file, $callback ) { 812 828 $file = plugin_basename( $file ); 813 add_action( 'deactivate_' . $file, $ function);814 } 815 816 /** 817 * Set the uninstallation hook for a plugin.829 add_action( 'deactivate_' . $file, $callback ); 830 } 831 832 /** 833 * Sets the uninstallation hook for a plugin. 818 834 * 819 835 * Registers the uninstall hook that will be called when the user clicks on the … … 853 869 $uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); 854 870 $plugin_basename = plugin_basename( $file ); 871 855 872 if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) { 856 873 $uninstallable_plugins[ $plugin_basename ] = $callback; … … 860 877 861 878 /** 862 * Call the 'all' hook, which will process the functions hooked into it.879 * Calls the 'all' hook, which will process the functions hooked into it. 863 880 * 864 881 * The 'all' hook passes all of the arguments or parameters that were used for … … 884 901 885 902 /** 886 * Build Unique ID for storage and retrieval.903 * Builds Unique ID for storage and retrieval. 887 904 * 888 905 * The old way to serialize the callback caused issues and this function is the … … 904 921 * @since 2.2.3 905 922 * @since 5.3.0 Removed workarounds for spl_object_hash(). 906 * `$ tag` and `$priority` are no longer used,923 * `$hook_name` and `$priority` are no longer used, 907 924 * and the function always returns a string. 908 925 * @access private 909 926 * 910 * @param string $ tagUnused. The name of the filter to build ID for.911 * @param callable $ functionThe function to generate ID for.912 * @param int $priority Unused. The order in which the functions913 * associated with a particular action are executed.927 * @param string $hook_name Unused. The name of the filter to build ID for. 928 * @param callable $callback The function to generate ID for. 929 * @param int $priority Unused. The order in which the functions 930 * associated with a particular action are executed. 914 931 * @return string Unique function ID for usage as array key. 915 932 */ 916 function _wp_filter_build_unique_id( $ tag, $function, $priority ) {917 if ( is_string( $ function) ) {918 return $ function;919 } 920 921 if ( is_object( $ function) ) {933 function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) { 934 if ( is_string( $callback ) ) { 935 return $callback; 936 } 937 938 if ( is_object( $callback ) ) { 922 939 // Closures are currently implemented as objects. 923 $ function = array( $function, '' );940 $callback = array( $callback, '' ); 924 941 } else { 925 $ function = (array) $function;926 } 927 928 if ( is_object( $ function[0] ) ) {942 $callback = (array) $callback; 943 } 944 945 if ( is_object( $callback[0] ) ) { 929 946 // Object class calling. 930 return spl_object_hash( $ function[0] ) . $function[1];931 } elseif ( is_string( $ function[0] ) ) {947 return spl_object_hash( $callback[0] ) . $callback[1]; 948 } elseif ( is_string( $callback[0] ) ) { 932 949 // Static calling. 933 return $ function[0] . '::' . $function[1];934 } 935 } 950 return $callback[0] . '::' . $callback[1]; 951 } 952 }
Note: See TracChangeset
for help on using the changeset viewer.