Ticket #17817: 17817.12.diff
File 17817.12.diff, 57.9 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-hook.php
1 <?php 2 /** 3 * Plugin API: WP_Hook class 4 * 5 * @package WordPress 6 * @subpackage Plugin 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used to implement action and filter hook functionality. 12 * 13 * @since 4.7.0 14 * 15 * @see Iterator 16 * @see ArrayAccess 17 */ 18 final class WP_Hook implements Iterator, ArrayAccess { 19 20 /** 21 * Hook callbacks. 22 * 23 * @since 4.7.0 24 * @access public 25 * @var array 26 */ 27 public $callbacks = array(); 28 29 /** 30 * The priority keys of actively running iterations of a hook. 31 * 32 * @since 4.7.0 33 * @access private 34 * @var array 35 */ 36 private $iterations = array(); 37 38 /** 39 * Number of levels this hook can be recursively called. 40 * 41 * @since 4.7.0 42 * @access private 43 * @var int 44 */ 45 private $nesting_level = 0; 46 47 /** 48 * Hooks a function or method to a specific filter action. 49 * 50 * @since 4.7.0 51 * @access public 52 * 53 * @param string $tag The name of the filter to hook the $function_to_add callback to. 54 * @param callable $function_to_add The callback to be run when the filter is applied. 55 * @param int $priority The order in which the functions associated with a 56 * particular action are executed. Lower numbers correspond with 57 * earlier execution, and functions with the same priority are executed 58 * in the order in which they were added to the action. 59 * @param int $accepted_args The number of arguments the function accepts. 60 */ 61 public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) { 62 $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority ); 63 $priority_existed = isset( $this->callbacks[ $priority ] ); 64 65 $this->callbacks[ $priority ][ $idx ] = array( 66 'function' => $function_to_add, 67 'accepted_args' => $accepted_args 68 ); 69 70 // if we're adding a new priority to the list, put them back in sorted order 71 if ( ! $priority_existed && count( $this->callbacks ) > 1 ) { 72 ksort( $this->callbacks, SORT_NUMERIC ); 73 } 74 75 if ( $this->nesting_level > 0 ) { 76 $this->resort_active_iterations(); 77 } 78 } 79 80 /** 81 * Handles reseting callback priority keys mid-iteration. 82 * 83 * @since 4.7.0 84 * @access private 85 */ 86 private function resort_active_iterations() { 87 $new_priorities = array_keys( $this->callbacks ); 88 89 // If there are no remaining hooks, clear out all running iterations. 90 if ( ! $new_priorities ) { 91 foreach ( $this->iterations as $index => $iteration ) { 92 $this->iterations[ $index ] = $new_priorities; 93 } 94 return; 95 } 96 97 $min = min( $new_priorities ); 98 foreach ( $this->iterations as $index => $iteration ) { 99 $current = current( $iteration ); 100 $this->iterations[ $index ] = $new_priorities; 101 102 if ( $current < $min ) { 103 array_unshift( $this->iterations[ $index ], $current ); 104 continue; 105 } 106 107 while ( current( $this->iterations[ $index ] ) < $current ) { 108 if ( false === next( $this->iterations[ $index ] ) ) { 109 break; 110 } 111 } 112 } 113 } 114 115 /** 116 * Unhooks a function or method from a specific filter action. 117 * 118 * @since 4.7.0 119 * @access public 120 * 121 * @param string $tag The filter hook to which the function to be removed is hooked. Used 122 * for building the callback ID when SPL is not available. 123 * @param callable $function_to_remove The callback to be removed from running when the filter is applied. 124 * @param int $priority The exact priority used when adding the original filter callback. 125 * @return bool Whether the callback existed before it was removed. 126 */ 127 public function remove_filter( $tag, $function_to_remove, $priority ) { 128 $function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority ); 129 130 $exists = isset( $this->callbacks[ $priority ][ $function_key ] ); 131 if ( $exists ) { 132 unset( $this->callbacks[ $priority ][ $function_key ] ); 133 if ( ! $this->callbacks[ $priority ] ) { 134 unset( $this->callbacks[ $priority ] ); 135 if ( $this->nesting_level > 0 ) { 136 $this->resort_active_iterations(); 137 } 138 } 139 } 140 return $exists; 141 } 142 143 /** 144 * Checks if a specific action has been registered for this hook. 145 * 146 * @since 4.7.0 147 * @access public 148 * 149 * @param callable|bool $function_to_check Optional. The callback to check for. Default false. 150 * @param string $tag Optional. The name of the filter hook. Default empty. 151 * Used for building the callback ID when SPL is not available. 152 * @return bool|int The priority of that hook is returned, or false if the function is not attached. 153 */ 154 public function has_filter( $tag = '', $function_to_check = false ) { 155 if ( false === $function_to_check ) { 156 return $this->has_filters(); 157 } 158 159 $function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false ); 160 if ( ! $function_key ) { 161 return false; 162 } 163 164 foreach ( $this->callbacks as $priority => $callbacks ) { 165 if ( isset( $callbacks[ $function_key ] ) ) { 166 return $priority; 167 } 168 } 169 170 return false; 171 } 172 173 /** 174 * Checks if any callbacks have been registered for this hook. 175 * 176 * @since 4.7.0 177 * @access public 178 * 179 * @return bool True if callbacks have been registered for the current hook, false otherwise. 180 */ 181 public function has_filters() { 182 foreach ( $this->callbacks as $callbacks ) { 183 if ( $callbacks ) { 184 return true; 185 } 186 } 187 return false; 188 } 189 190 /** 191 * Removes all callbacks from the current filter. 192 * 193 * @since 4.7.0 194 * @access public 195 * 196 * @param int|bool $priority Optional. The priority number to remove. Default false. 197 */ 198 public function remove_all_filters( $priority = false ) { 199 if ( ! $this->callbacks ) { 200 return; 201 } 202 203 if ( false === $priority ) { 204 $this->callbacks = array(); 205 } else if ( isset( $this->callbacks[ $priority ] ) ) { 206 unset( $this->callbacks[ $priority ] ); 207 } 208 209 if ( $this->nesting_level > 0 ) { 210 $this->resort_active_iterations(); 211 } 212 } 213 214 /** 215 * Calls the callback functions added to a filter hook. 216 * 217 * @since 4.7.0 218 * @access public 219 * 220 * @param mixed $value The value to filter. 221 * @param array $args Arguments to pass to callbacks. 222 * @return mixed The filtered value after all hooked functions are applied to it. 223 */ 224 public function apply_filters( $value, $args ) { 225 if ( ! $this->callbacks ) { 226 return $value; 227 } 228 $nesting_level = $this->nesting_level++; 229 230 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks ); 231 $num_args = count( $args ); 232 233 do { 234 $priority = current( $this->iterations[ $nesting_level ] ); 235 236 foreach ( $this->callbacks[ $priority ] as $the_ ) { 237 $args[ 0 ] = $value; 238 239 // Avoid the array_slice if possible. 240 if ( $the_['accepted_args'] == 0 ) { 241 $value = call_user_func_array( $the_['function'], array() ); 242 } elseif ( $the_['accepted_args'] >= $num_args ) { 243 $value = call_user_func_array( $the_['function'], $args ); 244 } else { 245 $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) ); 246 } 247 } 248 } while ( false !== next( $this->iterations[ $nesting_level ] ) ); 249 250 unset( $this->iterations[ $nesting_level ] ); 251 252 $this->nesting_level--; 253 254 return $value; 255 } 256 257 /** 258 * Executes the callback functions hooked on a specific action hook. 259 * 260 * @since 4.7.0 261 * @access public 262 * 263 * @param mixed $args Arguments to pass to the hook callbacks. 264 */ 265 public function do_action( $args ) { 266 if ( ! $this->callbacks ) { 267 return; 268 } 269 $nesting_level = $this->nesting_level++; 270 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks ); 271 $num_args = count( $args ); 272 273 do { 274 $priority = current( $this->iterations[ $nesting_level ] ); 275 276 foreach ( $this->callbacks[ $priority ] as $the_ ) { 277 278 // Avoid the array_slice if possible. 279 if ( $the_['accepted_args'] == 0 ) { 280 call_user_func_array( $the_['function'], array() ); 281 } elseif ( $the_['accepted_args'] >= $num_args ) { 282 call_user_func_array( $the_['function'], $args ); 283 } else { 284 call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) ); 285 } 286 } 287 } while ( next( $this->iterations[ $nesting_level ] ) !== false ); 288 289 unset( $this->iterations[ $nesting_level ] ); 290 $this->nesting_level--; 291 } 292 293 /** 294 * Processes the functions hooked into the 'all' hook. 295 * 296 * @since 4.7.0 297 * @access public 298 * 299 * @param array $args Arguments to pass to the hook callbacks. Passed by reference. 300 */ 301 public function do_all_hook( &$args ) { 302 $nesting_level = $this->nesting_level++; 303 $this->iterations[ $nesting_level ] = array_keys( $this->callbacks ); 304 305 do { 306 $priority = current( $this->iterations[ $nesting_level ] ); 307 foreach ( $this->callbacks[ $priority ] as $the_ ) { 308 call_user_func_array( $the_['function'], $args ); 309 } 310 } while ( false !== next( $this->iterations[ $nesting_level ] ) ); 311 312 unset( $this->iterations[ $nesting_level ] ); 313 $this->nesting_level--; 314 } 315 316 /** 317 * Normalizes filters setup before WordPress has initialized to WP_Hook objects. 318 * 319 * @since 4.7.0 320 * @access public 321 * @static 322 * 323 * @param array $filters Filters to normalize. 324 * @return WP_Hook[] Array of normalized filters. 325 */ 326 public static function build_preinitialized_hooks( $filters ) { 327 /** @var WP_Hook[] $normalized */ 328 $normalized = array(); 329 330 foreach ( $filters as $tag => $callback_groups ) { 331 if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) { 332 $normalized[ $tag ] = $callback_groups; 333 continue; 334 } 335 $hook = new WP_Hook(); 336 337 // Loop through callback groups. 338 foreach ( $callback_groups as $priority => $callbacks ) { 339 340 // Loop through callbacks. 341 foreach ( $callbacks as $cb ) { 342 $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] ); 343 } 344 } 345 $normalized[ $tag ] = $hook; 346 } 347 return $normalized; 348 } 349 350 /** 351 * Determines whether an offset value exists. 352 * 353 * @since 4.7.0 354 * @access public 355 * 356 * @link http://php.net/manual/en/arrayaccess.offsetexists.php 357 * 358 * @param mixed $offset An offset to check for. 359 * @return bool True if the offset exists, false otherwise. 360 */ 361 public function offsetExists( $offset ) { 362 return isset( $this->callbacks[ $offset ] ); 363 } 364 365 /** 366 * Retrieves a value at a specified offset. 367 * 368 * @since 4.7.0 369 * @access public 370 * 371 * @link http://php.net/manual/en/arrayaccess.offsetget.php 372 * 373 * @param mixed $offset The offset to retrieve. 374 * @return mixed If set, the value at the specified offset, null otherwise. 375 */ 376 public function offsetGet( $offset ) { 377 return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null; 378 } 379 380 /** 381 * Sets a value at a specified offset. 382 * 383 * @since 4.7.0 384 * @access public 385 * 386 * @link http://php.net/manual/en/arrayaccess.offsetset.php 387 * 388 * @param mixed $offset The offset to assign the value to. 389 * @param mixed $value The value to set. 390 */ 391 public function offsetSet( $offset, $value ) { 392 if ( is_null( $offset ) ) { 393 $this->callbacks[] = $value; 394 } else { 395 $this->callbacks[ $offset ] = $value; 396 } 397 } 398 399 /** 400 * Unsets a specified offset. 401 * 402 * @since 4.7.0 403 * @access public 404 * 405 * @link http://php.net/manual/en/arrayaccess.offsetunset.php 406 * 407 * @param mixed $offset The offset to unset. 408 */ 409 public function offsetUnset( $offset ) { 410 unset( $this->callbacks[ $offset ] ); 411 } 412 413 /** 414 * Return the current element 415 * 416 * @since 4.7.0 417 * @access public 418 * 419 * @link http://php.net/manual/en/iterator.current.php 420 * 421 * @return mixed 422 */ 423 public function current() { 424 return current( $this->callbacks ); 425 } 426 427 /** 428 * Move forward to the next element 429 * 430 * @since 4.7.0 431 * @access public 432 * 433 * @link http://php.net/manual/en/iterator.next.php 434 */ 435 public function next() { 436 return next( $this->callbacks ); 437 } 438 439 /** 440 * Return the key of the current element 441 * 442 * @since 4.7.0 443 * @access public 444 * 445 * @link http://php.net/manual/en/iterator.key.php 446 * 447 * @return mixed Returns scalar on success, or NULL on failure 448 */ 449 public function key() { 450 return key( $this->callbacks ); 451 } 452 453 /** 454 * Checks if current position is valid 455 * 456 * @since 4.7.0 457 * @access public 458 * 459 * @link http://php.net/manual/en/iterator.valid.php 460 * 461 * @return boolean 462 */ 463 public function valid() { 464 return key( $this->callbacks ) !== null; 465 } 466 467 /** 468 * Rewind the Iterator to the first element 469 * 470 * @since 4.7.0 471 * @access public 472 * 473 * @link http://php.net/manual/en/iterator.rewind.php 474 */ 475 public function rewind() { 476 return reset( $this->callbacks ); 477 } 478 479 480 } -
src/wp-includes/plugin.php
Property changes on: src/wp-includes/class-wp-hook.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
22 22 */ 23 23 24 24 // Initialize the filter globals. 25 global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;25 require( ABSPATH . WPINC . '/class-wp-hook.php' ); 26 26 27 if ( ! isset( $wp_filter ) ) 27 /** @var WP_Hook[] $wp_filter */ 28 global $wp_filter, $wp_actions, $wp_current_filter; 29 30 if ( $wp_filter ) { 31 $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter ); 32 } else { 28 33 $wp_filter = array(); 34 } 29 35 30 36 if ( ! isset( $wp_actions ) ) 31 37 $wp_actions = array(); 32 38 33 if ( ! isset( $merged_filters ) )34 $merged_filters = array();35 36 39 if ( ! isset( $wp_current_filter ) ) 37 40 $wp_current_filter = array(); 38 41 … … 89 92 * @since 0.71 90 93 * 91 94 * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. 92 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added,93 * it doesn't need to run through that process.94 95 * 95 96 * @param string $tag The name of the filter to hook the $function_to_add callback to. 96 97 * @param callable $function_to_add The callback to be run when the filter is applied. … … 103 104 * @return true 104 105 */ 105 106 function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { 106 global $wp_filter , $merged_filters;107 108 $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);109 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);110 unset( $merged_filters[ $tag ]);107 global $wp_filter; 108 if ( ! isset( $wp_filter[ $tag ] ) ) { 109 $wp_filter[ $tag ] = new WP_Hook(); 110 } 111 $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args ); 111 112 return true; 112 113 } 113 114 … … 128 129 * return value. 129 130 */ 130 131 function has_filter($tag, $function_to_check = false) { 131 // Don't reset the internal array pointer 132 $wp_filter = $GLOBALS['wp_filter']; 133 134 $has = ! empty( $wp_filter[ $tag ] ); 135 136 // Make sure at least one priority has a filter callback 137 if ( $has ) { 138 $exists = false; 139 foreach ( $wp_filter[ $tag ] as $callbacks ) { 140 if ( ! empty( $callbacks ) ) { 141 $exists = true; 142 break; 143 } 144 } 145 146 if ( ! $exists ) { 147 $has = false; 148 } 149 } 150 151 if ( false === $function_to_check || false === $has ) 152 return $has; 132 global $wp_filter; 153 133 154 if ( ! $idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )134 if ( ! isset( $wp_filter[ $tag ] ) ) { 155 135 return false; 156 157 foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {158 if ( isset($wp_filter[$tag][$priority][$idx]) )159 return $priority;160 136 } 161 137 162 return false;138 return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check ); 163 139 } 164 140 165 141 /** … … 190 166 * @since 0.71 191 167 * 192 168 * @global array $wp_filter Stores all of the filters. 193 * @global array $merged_filters Merges the filter hooks using this function.194 169 * @global array $wp_current_filter Stores the list of current filters with the current one last. 195 170 * 196 171 * @param string $tag The name of the filter hook. … … 199 174 * @return mixed The filtered value after all hooked functions are applied to it. 200 175 */ 201 176 function apply_filters( $tag, $value ) { 202 global $wp_filter, $ merged_filters, $wp_current_filter;177 global $wp_filter, $wp_current_filter; 203 178 204 179 $args = array(); 205 180 … … 219 194 if ( !isset($wp_filter['all']) ) 220 195 $wp_current_filter[] = $tag; 221 196 222 // Sort.223 if ( !isset( $merged_filters[ $tag ] ) ) {224 ksort($wp_filter[$tag]);225 $merged_filters[ $tag ] = true;226 }227 228 reset( $wp_filter[ $tag ] );229 230 197 if ( empty($args) ) 231 198 $args = func_get_args(); 232 199 233 do { 234 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 235 if ( !is_null($the_['function']) ){ 236 $args[1] = $value; 237 $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 238 } 200 // don't pass the tag name to WP_Hook 201 array_shift( $args ); 239 202 240 } while ( next($wp_filter[$tag]) !== false);203 $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args ); 241 204 242 205 array_pop( $wp_current_filter ); 243 206 244 return $ value;207 return $filtered; 245 208 } 246 209 247 210 /** … … 253 216 * functions hooked to `$tag` are supplied using an array. 254 217 * 255 218 * @global array $wp_filter Stores all of the filters 256 * @global array $merged_filters Merges the filter hooks using this function.257 219 * @global array $wp_current_filter Stores the list of current filters with the current one last 258 220 * 259 221 * @param string $tag The name of the filter hook. … … 261 223 * @return mixed The filtered value after all hooked functions are applied to it. 262 224 */ 263 225 function apply_filters_ref_array($tag, $args) { 264 global $wp_filter, $ merged_filters, $wp_current_filter;226 global $wp_filter, $wp_current_filter; 265 227 266 228 // Do 'all' actions first 267 229 if ( isset($wp_filter['all']) ) { … … 279 241 if ( !isset($wp_filter['all']) ) 280 242 $wp_current_filter[] = $tag; 281 243 282 // Sort 283 if ( !isset( $merged_filters[ $tag ] ) ) { 284 ksort($wp_filter[$tag]); 285 $merged_filters[ $tag ] = true; 286 } 287 288 reset( $wp_filter[ $tag ] ); 289 290 do { 291 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 292 if ( !is_null($the_['function']) ) 293 $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 294 295 } while ( next($wp_filter[$tag]) !== false ); 244 $filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args ); 296 245 297 246 array_pop( $wp_current_filter ); 298 247 299 return $ args[0];248 return $filtered; 300 249 } 301 250 302 251 /** … … 313 262 * @since 1.2.0 314 263 * 315 264 * @global array $wp_filter Stores all of the filters 316 * @global array $merged_filters Merges the filter hooks using this function.317 265 * 318 266 * @param string $tag The filter hook to which the function to be removed is hooked. 319 267 * @param callable $function_to_remove The name of the function which should be removed. … … 321 269 * @return bool Whether the function existed before it was removed. 322 270 */ 323 271 function remove_filter( $tag, $function_to_remove, $priority = 10 ) { 324 $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority ); 325 326 $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); 272 global $wp_filter; 327 273 328 if ( true === $r ) { 329 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] ); 330 if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) { 331 unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] ); 274 $r = false; 275 if ( isset( $wp_filter[ $tag ] ) ) { 276 $r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority ); 277 if ( ! $wp_filter[ $tag ]->callbacks ) { 278 unset( $wp_filter[ $tag ] ); 332 279 } 333 if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) {334 $GLOBALS['wp_filter'][ $tag ] = array();335 }336 unset( $GLOBALS['merged_filters'][ $tag ] );337 280 } 338 281 339 282 return $r; … … 344 287 * 345 288 * @since 2.7.0 346 289 * 347 * @global array $wp_filter Stores all of the filters 348 * @global array $merged_filters Merges the filter hooks using this function. 290 * @global array $wp_filter Stores all of the filters 349 291 * 350 292 * @param string $tag The filter to remove hooks from. 351 293 * @param int|bool $priority Optional. The priority number to remove. Default false. 352 294 * @return true True when finished. 353 295 */ 354 296 function remove_all_filters( $tag, $priority = false ) { 355 global $wp_filter , $merged_filters;297 global $wp_filter; 356 298 357 299 if ( isset( $wp_filter[ $tag ]) ) { 358 if ( false === $priority ) { 359 $wp_filter[ $tag ] = array(); 360 } elseif ( isset( $wp_filter[ $tag ][ $priority ] ) ) { 361 $wp_filter[ $tag ][ $priority ] = array(); 300 $wp_filter[ $tag ]->remove_all_filters( $priority ); 301 if ( ! $wp_filter[ $tag ]->has_filters() ) { 302 unset( $wp_filter[ $tag ] ); 362 303 } 363 304 } 364 305 365 unset( $merged_filters[ $tag ] );366 367 306 return true; 368 307 } 369 308 … … 473 412 * 474 413 * @global array $wp_filter Stores all of the filters 475 414 * @global array $wp_actions Increments the amount of times action was triggered. 476 * @global array $merged_filters Merges the filter hooks using this function.477 415 * @global array $wp_current_filter Stores the list of current filters with the current one last 478 416 * 479 417 * @param string $tag The name of the action to be executed. … … 481 419 * functions hooked to the action. Default empty. 482 420 */ 483 421 function do_action($tag, $arg = '') { 484 global $wp_filter, $wp_actions, $ merged_filters, $wp_current_filter;422 global $wp_filter, $wp_actions, $wp_current_filter; 485 423 486 424 if ( ! isset($wp_actions[$tag]) ) 487 425 $wp_actions[$tag] = 1; … … 512 450 for ( $a = 2, $num = func_num_args(); $a < $num; $a++ ) 513 451 $args[] = func_get_arg($a); 514 452 515 // Sort 516 if ( !isset( $merged_filters[ $tag ] ) ) { 517 ksort($wp_filter[$tag]); 518 $merged_filters[ $tag ] = true; 519 } 520 521 reset( $wp_filter[ $tag ] ); 522 523 do { 524 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 525 if ( !is_null($the_['function']) ) 526 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 527 528 } while ( next($wp_filter[$tag]) !== false ); 453 $wp_filter[ $tag ]->do_action( $args ); 529 454 530 455 array_pop($wp_current_filter); 531 456 } … … 558 483 * functions hooked to $tag< are supplied using an array. 559 484 * @global array $wp_filter Stores all of the filters 560 485 * @global array $wp_actions Increments the amount of times action was triggered. 561 * @global array $merged_filters Merges the filter hooks using this function.562 486 * @global array $wp_current_filter Stores the list of current filters with the current one last 563 487 * 564 488 * @param string $tag The name of the action to be executed. 565 489 * @param array $args The arguments supplied to the functions hooked to `$tag`. 566 490 */ 567 491 function do_action_ref_array($tag, $args) { 568 global $wp_filter, $wp_actions, $ merged_filters, $wp_current_filter;492 global $wp_filter, $wp_actions, $wp_current_filter; 569 493 570 494 if ( ! isset($wp_actions[$tag]) ) 571 495 $wp_actions[$tag] = 1; … … 588 512 if ( !isset($wp_filter['all']) ) 589 513 $wp_current_filter[] = $tag; 590 514 591 // Sort 592 if ( !isset( $merged_filters[ $tag ] ) ) { 593 ksort($wp_filter[$tag]); 594 $merged_filters[ $tag ] = true; 595 } 596 597 reset( $wp_filter[ $tag ] ); 598 599 do { 600 foreach ( (array) current($wp_filter[$tag]) as $the_ ) 601 if ( !is_null($the_['function']) ) 602 call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); 603 604 } while ( next($wp_filter[$tag]) !== false ); 515 $wp_filter[ $tag ]->do_action( $args ); 605 516 606 517 array_pop($wp_current_filter); 607 518 } … … 923 834 function _wp_call_all_hook($args) { 924 835 global $wp_filter; 925 836 926 reset( $wp_filter['all'] ); 927 do { 928 foreach ( (array) current($wp_filter['all']) as $the_ ) 929 if ( !is_null($the_['function']) ) 930 call_user_func_array($the_['function'], $args); 931 932 } while ( next($wp_filter['all']) !== false ); 837 $wp_filter['all']->do_all_hook( $args ); 933 838 } 934 839 935 840 /** -
tests/phpunit/includes/functions.php
21 21 22 22 // For adding hooks before loading WP 23 23 function tests_add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 24 global $wp_filter , $merged_filters;24 global $wp_filter; 25 25 26 26 $idx = _test_filter_build_unique_id($tag, $function_to_add, $priority); 27 27 $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 28 unset( $merged_filters[ $tag ] );29 28 return true; 30 29 } 31 30 32 31 function _test_filter_build_unique_id($tag, $function, $priority) { 33 global $wp_filter;34 static $filter_id_count = 0;35 36 32 if ( is_string($function) ) 37 33 return $function; 38 34 -
tests/phpunit/includes/testcase.php
223 223 * @return void 224 224 */ 225 225 protected function _backup_hooks() { 226 $globals = array( ' merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );226 $globals = array( 'wp_actions', 'wp_current_filter' ); 227 227 foreach ( $globals as $key ) { 228 228 self::$hooks_saved[ $key ] = $GLOBALS[ $key ]; 229 229 } 230 self::$hooks_saved['wp_filter'] = array(); 231 foreach ( $GLOBALS['wp_filter'] as $hook_name => $hook_object ) { 232 self::$hooks_saved['wp_filter'][ $hook_name ] = clone $hook_object; 233 } 230 234 } 231 235 232 236 /** … … 240 244 * @return void 241 245 */ 242 246 protected function _restore_hooks() { 243 $globals = array( ' merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );247 $globals = array( 'wp_actions', 'wp_current_filter' ); 244 248 foreach ( $globals as $key ) { 245 249 if ( isset( self::$hooks_saved[ $key ] ) ) { 246 250 $GLOBALS[ $key ] = self::$hooks_saved[ $key ]; 247 251 } 248 252 } 253 if ( isset( self::$hooks_saved['wp_filter'] ) ) { 254 $GLOBALS['wp_filter'] = array(); 255 foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) { 256 $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object; 257 } 258 } 249 259 } 250 260 251 261 static function flush_cache() { -
tests/phpunit/tests/actions.php
114 114 $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) ); 115 115 } 116 116 117 /** 118 * Test that multiple callbacks receive the correct number of args even when the number 119 * is less than, or greater than previous hooks. 120 * 121 * @see https://core.trac.wordpress.org/ticket/17817#comment:72 122 * @ticket 17817 123 */ 124 function test_action_args_3() { 125 $a1 = new MockAction(); 126 $a2 = new MockAction(); 127 $a3 = new MockAction(); 128 $tag = rand_str(); 129 $val1 = rand_str(); 130 $val2 = rand_str(); 131 132 // a1 accepts two arguments, a2 doesn't, a3 accepts two arguments 133 add_action( $tag, array( &$a1, 'action' ), 10, 2 ); 134 add_action( $tag, array( &$a2, 'action' ) ); 135 add_action( $tag, array( &$a3, 'action' ), 10, 2 ); 136 // call the action with two arguments 137 do_action( $tag, $val1, $val2 ); 138 139 $call_count = $a1->get_call_count(); 140 // a1 should be called with both args 141 $this->assertEquals( 1, $call_count ); 142 $argsvar1 = $a1->get_args(); 143 $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) ); 144 145 // a2 should be called with one only 146 $this->assertEquals( 1, $a2->get_call_count() ); 147 $argsvar2 = $a2->get_args(); 148 $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) ); 149 150 // a3 should be called with both args 151 $this->assertEquals( 1, $a3->get_call_count() ); 152 $argsvar3 = $a3->get_args(); 153 $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar3 ) ); 154 } 155 117 156 function test_action_priority() { 118 157 $a = new MockAction(); 119 158 $tag = rand_str(); … … 258 297 } 259 298 260 299 /** 300 * @ticket 17817 301 */ 302 function test_action_recursion() { 303 $tag = rand_str(); 304 $a = new MockAction(); 305 $b = new MockAction(); 306 307 add_action( $tag, array( $a, 'action' ), 11, 1 ); 308 add_action( $tag, array( $b, 'action' ), 13, 1 ); 309 add_action( $tag, array( $this, 'action_that_causes_recursion' ), 12, 1 ); 310 do_action( $tag, $tag ); 311 312 $this->assertEquals( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' ); 313 $this->assertEquals( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' ); 314 } 315 316 function action_that_causes_recursion( $tag ) { 317 static $recursing = false; 318 if ( ! $recursing ) { 319 $recursing = true; 320 do_action( $tag, $tag ); 321 } 322 $recursing = false; 323 } 324 325 /** 326 * @ticket 9968 327 * @ticket 17817 328 */ 329 function test_action_callback_manipulation_while_running() { 330 $tag = rand_str(); 331 $a = new MockAction(); 332 $b = new MockAction(); 333 $c = new MockAction(); 334 $d = new MockAction(); 335 $e = new MockAction(); 336 337 add_action( $tag, array( $a, 'action' ), 11, 2 ); 338 add_action( $tag, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 ); 339 add_action( $tag, array( $b, 'action' ), 12, 2 ); 340 341 do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) ); 342 do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) ); 343 344 $this->assertEquals( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' ); 345 $this->assertEquals( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' ); 346 $this->assertEquals( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' ); 347 $this->assertEquals( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' ); 348 $this->assertEquals( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' ); 349 } 350 351 function action_that_manipulates_a_running_hook( $tag, $mocks ) { 352 remove_action( $tag, array( $mocks[ 1 ], 'action' ), 12, 2 ); 353 add_action( $tag, array( $mocks[ 2 ], 'action' ), 12, 2 ); 354 add_action( $tag, array( $mocks[ 3 ], 'action' ), 13, 2 ); 355 add_action( $tag, array( $mocks[ 4 ], 'action' ), 10, 2 ); 356 } 357 358 /** 359 * @ticket 17817 360 * 361 * This specificaly addresses the concern raised at 362 * https://core.trac.wordpress.org/ticket/17817#comment:52 363 */ 364 function test_remove_anonymous_callback() { 365 $tag = rand_str(); 366 $a = new MockAction(); 367 add_action( $tag, array( $a, 'action' ), 12, 1 ); 368 $this->assertTrue( has_action( $tag ) ); 369 370 $hook = $GLOBALS['wp_filter'][ $tag ]; 371 372 // From http://wordpress.stackexchange.com/a/57088/6445 373 foreach ( $hook as $priority => $filter ) { 374 foreach ( $filter as $identifier => $function ) { 375 if ( is_array( $function ) 376 && is_a( $function['function'][ 0 ], 'MockAction' ) 377 && 'action' === $function['function'][ 1 ] 378 ) { 379 remove_filter( 380 $tag, 381 array( $function['function'][ 0 ], 'action' ), 382 $priority 383 ); 384 } 385 } 386 } 387 388 $this->assertFalse( has_action( $tag ) ); 389 } 390 391 392 /** 393 * Test the ArrayAccess methods of WP_Hook 394 * 395 * @ticket 17817 396 */ 397 function test_array_access_of_wp_filter_global() { 398 global $wp_filter; 399 $tag = rand_str(); 400 401 add_action( $tag, '__return_null', 11, 1 ); 402 403 $this->assertTrue( isset( $wp_filter[ $tag ][ 11 ] ) ); 404 $this->assertArrayHasKey( '__return_null', $wp_filter[ $tag ][ 11 ] ); 405 406 unset( $wp_filter[ $tag ][ 11 ] ); 407 $this->assertFalse( has_action( $tag, '__return_null' ) ); 408 409 $wp_filter[ $tag ][ 11 ] = array( '__return_null' => array( 'function' => '__return_null', 'accepted_args' => 1 ) ); 410 $this->assertEquals( 11, has_action( $tag, '__return_null' ) ); 411 } 412 413 /** 261 414 * Make sure current_action() behaves as current_filter() 262 415 * 263 416 * @ticket 14994 -
tests/phpunit/tests/filters.php
296 296 } 297 297 298 298 /** 299 * @ticket 29070300 */301 function test_has_filter_doesnt_reset_wp_filter() {302 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 1 );303 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 2 );304 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 3 );305 add_action( 'action_test_has_filter_doesnt_reset_wp_filter', array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ), 4 );306 307 do_action( 'action_test_has_filter_doesnt_reset_wp_filter' );308 }309 function _action_test_has_filter_doesnt_reset_wp_filter() {310 global $wp_filter;311 312 has_action( 'action_test_has_filter_doesnt_reset_wp_filter', '_function_that_doesnt_exist' );313 314 $filters = current( $wp_filter['action_test_has_filter_doesnt_reset_wp_filter'] );315 $the_ = current( $filters );316 $this->assertEquals( $the_['function'], array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ) );317 }318 319 /**320 299 * @ticket 10441 321 300 * @expectedDeprecated tests_apply_filters_deprecated 322 301 */ -
tests/phpunit/tests/hooks/add_filter.php
1 <?php 2 3 4 /** 5 * Test the add_filter method of WP_Hook 6 * 7 * @group hooks 8 */ 9 class Tests_WP_Hook_Add_Filter extends WP_UnitTestCase { 10 11 public function test_add_filter_with_function() { 12 $callback = '__return_null'; 13 $hook = new WP_Hook(); 14 $tag = rand_str(); 15 $priority = rand( 1, 100 ); 16 $accepted_args = rand( 1, 100 ); 17 18 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 19 20 $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority ); 21 $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] ); 22 $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] ); 23 } 24 25 public function test_add_filter_with_object() { 26 $a = new MockAction(); 27 $callback = array( $a, 'action' ); 28 $hook = new WP_Hook(); 29 $tag = rand_str(); 30 $priority = rand( 1, 100 ); 31 $accepted_args = rand( 1, 100 ); 32 33 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 34 35 $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority ); 36 $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] ); 37 $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] ); 38 } 39 40 public function test_add_filter_with_static_method() { 41 $callback = array( 'MockAction', 'action' ); 42 $hook = new WP_Hook(); 43 $tag = rand_str(); 44 $priority = rand( 1, 100 ); 45 $accepted_args = rand( 1, 100 ); 46 47 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 48 49 $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority ); 50 $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] ); 51 $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] ); 52 } 53 54 public function test_add_two_filters_with_same_priority() { 55 $callback_one = '__return_null'; 56 $callback_two = '__return_false'; 57 $hook = new WP_Hook(); 58 $tag = rand_str(); 59 $priority = rand( 1, 100 ); 60 $accepted_args = rand( 1, 100 ); 61 62 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 63 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 64 65 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args ); 66 $this->assertCount( 2, $hook->callbacks[ $priority ] ); 67 } 68 69 public function test_add_two_filters_with_different_priority() { 70 $callback_one = '__return_null'; 71 $callback_two = '__return_false'; 72 $hook = new WP_Hook(); 73 $tag = rand_str(); 74 $priority = rand( 1, 100 ); 75 $accepted_args = rand( 1, 100 ); 76 77 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 78 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 79 80 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args ); 81 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 82 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); 83 } 84 85 public function test_readd_filter() { 86 $callback = '__return_null'; 87 $hook = new WP_Hook(); 88 $tag = rand_str(); 89 $priority = rand( 1, 100 ); 90 $accepted_args = rand( 1, 100 ); 91 92 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 93 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 94 95 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 96 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 97 } 98 99 public function test_readd_filter_with_different_priority() { 100 $callback = '__return_null'; 101 $hook = new WP_Hook(); 102 $tag = rand_str(); 103 $priority = rand( 1, 100 ); 104 $accepted_args = rand( 1, 100 ); 105 106 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 107 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 108 109 $hook->add_filter( $tag, $callback, $priority + 1, $accepted_args ); 110 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 111 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); 112 } 113 114 public function test_sort_after_add_filter() { 115 $a = new MockAction(); 116 $b = new MockAction(); 117 $c = new MockAction(); 118 $hook = new WP_Hook(); 119 $tag = rand_str(); 120 121 $hook->add_filter( $tag, array( $a, 'action' ), 10, 1 ); 122 $hook->add_filter( $tag, array( $b, 'action' ), 5, 1 ); 123 $hook->add_filter( $tag, array( $c, 'action' ), 8, 1 ); 124 125 $this->assertEquals( array( 5, 8, 10 ), array_keys( $hook->callbacks ) ); 126 } 127 } -
tests/phpunit/tests/hooks/apply_filters.php
Property changes on: tests/phpunit/tests/hooks/add_filter.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the apply_filters method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Apply_Filters extends WP_UnitTestCase { 9 10 public function test_apply_filters_with_callback() { 11 $a = new MockAction(); 12 $callback = array( $a, 'filter' ); 13 $hook = new WP_Hook(); 14 $tag = rand_str(); 15 $priority = rand( 1, 100 ); 16 $accepted_args = rand( 1, 100 ); 17 $arg = rand_str(); 18 19 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 20 21 $returned = $hook->apply_filters( $arg, array( $arg ) ); 22 23 $this->assertEquals( $returned, $arg ); 24 $this->assertEquals( 1, $a->get_call_count() ); 25 } 26 27 public function test_apply_filters_with_multiple_calls() { 28 $a = new MockAction(); 29 $callback = array( $a, 'filter' ); 30 $hook = new WP_Hook(); 31 $tag = rand_str(); 32 $priority = rand( 1, 100 ); 33 $accepted_args = rand( 1, 100 ); 34 $arg = rand_str(); 35 36 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 37 38 $returned_one = $hook->apply_filters( $arg, array( $arg ) ); 39 $returned_two = $hook->apply_filters( $returned_one, array( $returned_one ) ); 40 41 $this->assertEquals( $returned_two, $arg ); 42 $this->assertEquals( 2, $a->get_call_count() ); 43 } 44 45 } -
tests/phpunit/tests/hooks/do_action.php
Property changes on: tests/phpunit/tests/hooks/apply_filters.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the do_action method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Do_Action extends WP_UnitTestCase { 9 private $events = array(); 10 11 public function setUp() { 12 parent::setUp(); 13 $this->events = array(); 14 } 15 16 public function test_do_action_with_callback() { 17 $a = new MockAction(); 18 $callback = array( $a, 'action' ); 19 $hook = new WP_Hook(); 20 $tag = rand_str(); 21 $priority = rand( 1, 100 ); 22 $accepted_args = rand( 1, 100 ); 23 $arg = rand_str(); 24 25 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 26 $hook->do_action( array( $arg ) ); 27 28 $this->assertEquals( 1, $a->get_call_count() ); 29 } 30 31 public function test_do_action_with_multiple_calls() { 32 $a = new MockAction(); 33 $callback = array( $a, 'filter' ); 34 $hook = new WP_Hook(); 35 $tag = rand_str(); 36 $priority = rand( 1, 100 ); 37 $accepted_args = rand( 1, 100 ); 38 $arg = rand_str(); 39 40 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 41 $hook->do_action( array( $arg ) ); 42 $hook->do_action( array( $arg ) ); 43 44 $this->assertEquals( 2, $a->get_call_count() ); 45 } 46 47 public function test_do_action_with_multiple_callbacks_on_same_priority() { 48 $a = new MockAction(); 49 $b = new MockAction(); 50 $callback_one = array( $a, 'filter' ); 51 $callback_two = array( $b, 'filter' ); 52 $hook = new WP_Hook(); 53 $tag = rand_str(); 54 $priority = rand( 1, 100 ); 55 $accepted_args = rand( 1, 100 ); 56 $arg = rand_str(); 57 58 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 59 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args ); 60 $hook->do_action( array( $arg ) ); 61 62 $this->assertEquals( 1, $a->get_call_count() ); 63 $this->assertEquals( 1, $a->get_call_count() ); 64 } 65 66 public function test_do_action_with_multiple_callbacks_on_different_priorities() { 67 $a = new MockAction(); 68 $b = new MockAction(); 69 $callback_one = array( $a, 'filter' ); 70 $callback_two = array( $b, 'filter' ); 71 $hook = new WP_Hook(); 72 $tag = rand_str(); 73 $priority = rand( 1, 100 ); 74 $accepted_args = rand( 1, 100 ); 75 $arg = rand_str(); 76 77 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 78 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args ); 79 $hook->do_action( array( $arg ) ); 80 81 $this->assertEquals( 1, $a->get_call_count() ); 82 $this->assertEquals( 1, $a->get_call_count() ); 83 } 84 85 public function test_do_action_with_no_accepted_args() { 86 $callback = array( $this, '_action_callback' ); 87 $hook = new WP_Hook(); 88 $tag = rand_str(); 89 $priority = rand( 1, 100 ); 90 $accepted_args = 0; 91 $arg = rand_str(); 92 93 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 94 $hook->do_action( array( $arg ) ); 95 96 $this->assertEmpty( $this->events[0]['args'] ); 97 } 98 99 public function test_do_action_with_one_accepted_arg() { 100 $callback = array( $this, '_action_callback' ); 101 $hook = new WP_Hook(); 102 $tag = rand_str(); 103 $priority = rand( 1, 100 ); 104 $accepted_args = 1; 105 $arg = rand_str(); 106 107 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 108 $hook->do_action( array( $arg ) ); 109 110 $this->assertCount( 1, $this->events[0]['args'] ); 111 } 112 113 public function test_do_action_with_more_accepted_args() { 114 $callback = array( $this, '_action_callback' ); 115 $hook = new WP_Hook(); 116 $tag = rand_str(); 117 $priority = rand( 1, 100 ); 118 $accepted_args = 1000; 119 $arg = rand_str(); 120 121 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 122 $hook->do_action( array( $arg ) ); 123 124 $this->assertCount( 1, $this->events[0]['args'] ); 125 } 126 127 /** 128 * Use this rather than MockAction so we can test callbacks with no args 129 */ 130 public function _action_callback() { 131 $args = func_get_args(); 132 $this->events[] = array('action' => __FUNCTION__, 'args'=>$args); 133 } 134 } -
tests/phpunit/tests/hooks/do_all_hook.php
Property changes on: tests/phpunit/tests/hooks/do_action.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the do_all_hook method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Do_All_Hook extends WP_UnitTestCase { 9 10 public function test_do_all_hook_with_multiple_calls() { 11 $a = new MockAction(); 12 $callback = array( $a, 'action' ); 13 $hook = new WP_Hook(); 14 $tag = 'all'; 15 $priority = rand( 1, 100 ); 16 $accepted_args = rand( 1, 100 ); 17 $arg = rand_str(); 18 19 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 20 $args = array( $arg ); 21 $hook->do_all_hook( $args ); 22 $hook->do_all_hook( $args ); 23 24 $this->assertEquals( 2, $a->get_call_count() ); 25 } 26 } -
tests/phpunit/tests/hooks/has_filter.php
Property changes on: tests/phpunit/tests/hooks/do_all_hook.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the has_filter method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Has_Filter extends WP_UnitTestCase { 9 10 public function test_has_filter_with_function() { 11 $callback = '__return_null'; 12 $hook = new WP_Hook(); 13 $tag = rand_str(); 14 $priority = rand( 1, 100 ); 15 $accepted_args = rand( 1, 100 ); 16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 18 19 $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) ); 20 } 21 22 public function test_has_filter_with_object() { 23 $a = new MockAction(); 24 $callback = array( $a, 'action' ); 25 $hook = new WP_Hook(); 26 $tag = rand_str(); 27 $priority = rand( 1, 100 ); 28 $accepted_args = rand( 1, 100 ); 29 30 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 31 32 $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) ); 33 } 34 35 public function test_has_filter_with_static_method() { 36 $callback = array( 'MockAction', 'action' ); 37 $hook = new WP_Hook(); 38 $tag = rand_str(); 39 $priority = rand( 1, 100 ); 40 $accepted_args = rand( 1, 100 ); 41 42 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 43 44 $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) ); 45 } 46 47 public function test_has_filter_without_callback() { 48 $callback = '__return_null'; 49 $hook = new WP_Hook(); 50 $tag = rand_str(); 51 $priority = rand( 1, 100 ); 52 $accepted_args = rand( 1, 100 ); 53 54 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 55 56 $this->assertTrue( $hook->has_filter() ); 57 } 58 59 public function test_not_has_filter_without_callback() { 60 $hook = new WP_Hook(); 61 $this->assertFalse( $hook->has_filter() ); 62 } 63 64 public function test_not_has_filter_with_callback() { 65 $callback = '__return_null'; 66 $hook = new WP_Hook(); 67 $tag = rand_str(); 68 69 $this->assertFalse( $hook->has_filter( $tag, $callback ) ); 70 } 71 72 public function test_has_filter_with_wrong_callback() { 73 $callback = '__return_null'; 74 $hook = new WP_Hook(); 75 $tag = rand_str(); 76 $priority = rand( 1, 100 ); 77 $accepted_args = rand( 1, 100 ); 78 79 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 80 81 $this->assertFalse( $hook->has_filter( $tag, '__return_false' ) ); 82 } 83 } -
tests/phpunit/tests/hooks/has_filters.php
Property changes on: tests/phpunit/tests/hooks/has_filter.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the has_filters method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Has_Filters extends WP_UnitTestCase { 9 10 public function test_has_filters_with_callback() { 11 $callback = '__return_null'; 12 $hook = new WP_Hook(); 13 $tag = rand_str(); 14 $priority = rand( 1, 100 ); 15 $accepted_args = rand( 1, 100 ); 16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 18 19 $this->assertTrue( $hook->has_filters() ); 20 } 21 22 public function test_has_filters_without_callback() { 23 $hook = new WP_Hook(); 24 $this->assertFalse( $hook->has_filters() ); 25 } 26 27 public function test_not_has_filters_with_removed_callback() { 28 $callback = '__return_null'; 29 $hook = new WP_Hook(); 30 $tag = rand_str(); 31 $priority = rand( 1, 100 ); 32 $accepted_args = rand( 1, 100 ); 33 34 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 35 $hook->remove_filter( $tag, $callback, $priority ); 36 $this->assertFalse( $hook->has_filters() ); 37 } 38 39 public function test_not_has_filter_with_directly_removed_callback() { 40 $callback = '__return_null'; 41 $hook = new WP_Hook(); 42 $tag = rand_str(); 43 $priority = rand( 1, 100 ); 44 $accepted_args = rand( 1, 100 ); 45 46 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 47 $function_key = _wp_filter_build_unique_id( $tag, $callback, $priority ); 48 unset( $hook->callbacks[ $priority ][ $function_key ] ); 49 50 $this->assertFalse( $hook->has_filters() ); 51 } 52 } -
tests/phpunit/tests/hooks/iterator.php
Property changes on: tests/phpunit/tests/hooks/has_filters.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the Iterator implementation of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Iterator extends WP_UnitTestCase { 9 public function test_foreach() { 10 $callback_one = '__return_null'; 11 $callback_two = '__return_false'; 12 $hook = new WP_Hook(); 13 $tag = rand_str(); 14 $priority = rand( 1, 100 ); 15 $accepted_args = rand( 1, 100 ); 16 17 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 18 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args ); 19 20 $functions = array(); 21 $priorities = array(); 22 foreach ( $hook as $key => $callbacks ) { 23 $priorities[] = $key; 24 foreach ( $callbacks as $function_index => $the_ ) { 25 $functions[] = $the_['function']; 26 } 27 } 28 $this->assertEqualSets( array( $priority, $priority + 1 ), $priorities ); 29 $this->assertEqualSets( array( $callback_one, $callback_two ), $functions ); 30 } 31 } -
tests/phpunit/tests/hooks/preinit_hooks.php
Property changes on: tests/phpunit/tests/hooks/iterator.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the IteratorAggregate implementation of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Preinit_Hooks extends WP_UnitTestCase { 9 10 public function test_array_to_hooks() { 11 $tag1 = rand_str(); 12 $priority1 = rand( 1, 100 ); 13 $tag2 = rand_str(); 14 $priority2 = rand( 1, 100 ); 15 $filters = array( 16 $tag1 => array( 17 $priority1 => array( 18 'test1' => array( 19 'function' => '__return_false', 20 'accepted_args' => 2, 21 ), 22 ), 23 ), 24 $tag2 => array( 25 $priority2 => array( 26 'test1' => array( 27 'function' => '__return_null', 28 'accepted_args' => 1, 29 ), 30 ), 31 ), 32 ); 33 34 $hooks = WP_Hook::build_preinitialized_hooks( $filters ); 35 36 $this->assertEquals( $priority1, $hooks[ $tag1 ]->has_filter( $tag1, '__return_false' ) ); 37 $this->assertEquals( $priority2, $hooks[ $tag2 ]->has_filter( $tag2, '__return_null' ) ); 38 } 39 } -
tests/phpunit/tests/hooks/remove_all_filters.php
Property changes on: tests/phpunit/tests/hooks/preinit_hooks.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the remove_all_filters method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Remove_All_Filters extends WP_UnitTestCase { 9 10 public function test_remove_all_filters() { 11 $callback = '__return_null'; 12 $hook = new WP_Hook(); 13 $tag = rand_str(); 14 $priority = rand( 1, 100 ); 15 $accepted_args = rand( 1, 100 ); 16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 18 19 $hook->remove_all_filters(); 20 21 $this->assertFalse( $hook->has_filters() ); 22 } 23 24 public function test_remove_all_filters_with_priority() { 25 $callback_one = '__return_null'; 26 $callback_two = '__return_false'; 27 $hook = new WP_Hook(); 28 $tag = rand_str(); 29 $priority = rand( 1, 100 ); 30 $accepted_args = rand( 1, 100 ); 31 32 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 33 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args ); 34 35 $hook->remove_all_filters( $priority ); 36 37 $this->assertFalse( $hook->has_filter( $tag, $callback_one ) ); 38 $this->assertTrue( $hook->has_filters() ); 39 $this->assertEquals( $priority + 1, $hook->has_filter( $tag, $callback_two ) ); 40 } 41 } -
tests/phpunit/tests/hooks/remove_filter.php
Property changes on: tests/phpunit/tests/hooks/remove_all_filters.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <?php 2 3 /** 4 * Test the remove_filter method of WP_Hook 5 * 6 * @group hooks 7 */ 8 class Tests_WP_Hook_Remove_Filter extends WP_UnitTestCase { 9 10 public function test_remove_filter_with_function() { 11 $callback = '__return_null'; 12 $hook = new WP_Hook(); 13 $tag = rand_str(); 14 $priority = rand( 1, 100 ); 15 $accepted_args = rand( 1, 100 ); 16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 18 $hook->remove_filter( $tag, $callback, $priority ); 19 20 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) ); 21 } 22 23 public function test_remove_filter_with_object() { 24 $a = new MockAction(); 25 $callback = array( $a, 'action' ); 26 $hook = new WP_Hook(); 27 $tag = rand_str(); 28 $priority = rand( 1, 100 ); 29 $accepted_args = rand( 1, 100 ); 30 31 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 32 $hook->remove_filter( $tag, $callback, $priority ); 33 34 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) ); 35 } 36 37 public function test_remove_filter_with_static_method() { 38 $callback = array( 'MockAction', 'action' ); 39 $hook = new WP_Hook(); 40 $tag = rand_str(); 41 $priority = rand( 1, 100 ); 42 $accepted_args = rand( 1, 100 ); 43 44 $hook->add_filter( $tag, $callback, $priority, $accepted_args ); 45 $hook->remove_filter( $tag, $callback, $priority ); 46 47 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) ); 48 } 49 50 public function test_remove_filters_with_another_at_same_priority() { 51 $callback_one = '__return_null'; 52 $callback_two = '__return_false'; 53 $hook = new WP_Hook(); 54 $tag = rand_str(); 55 $priority = rand( 1, 100 ); 56 $accepted_args = rand( 1, 100 ); 57 58 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 59 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args ); 60 61 $hook->remove_filter( $tag, $callback_one, $priority ); 62 63 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 64 } 65 66 public function test_remove_filter_with_another_at_different_priority() { 67 $callback_one = '__return_null'; 68 $callback_two = '__return_false'; 69 $hook = new WP_Hook(); 70 $tag = rand_str(); 71 $priority = rand( 1, 100 ); 72 $accepted_args = rand( 1, 100 ); 73 74 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args ); 75 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args ); 76 77 $hook->remove_filter( $tag, $callback_one, $priority ); 78 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) ); 79 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); 80 } 81 } -
tests/phpunit/tests/post/types.php
Property changes on: tests/phpunit/tests/hooks/remove_filter.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
446 446 447 447 $this->assertSame( 1, count( $wp_filter['future_foo'] ) ); 448 448 $this->assertTrue( unregister_post_type( 'foo' ) ); 449 $this->assert Same( array(), $wp_filter['future_foo']);449 $this->assertArrayNotHasKey( 'future_foo', $wp_filter ); 450 450 } 451 451 452 452 /** … … 462 462 463 463 $this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) ); 464 464 $this->assertTrue( unregister_post_type( 'foo' ) ); 465 $this->assert Same( array(), $wp_filter['add_meta_boxes_foo']);465 $this->assertArrayNotHasKey( 'add_meta_boxes_foo', $wp_filter ); 466 466 } 467 467 468 468 /** -
tests/phpunit/tests/taxonomy.php
697 697 698 698 $this->assertSame( 1, count( $wp_filter['wp_ajax_add-foo'] ) ); 699 699 $this->assertTrue( unregister_taxonomy( 'foo' ) ); 700 $this->assert Same( array(), $wp_filter['wp_ajax_add-foo']);700 $this->assertArrayNotHasKey( 'wp_ajax_add-foo', $wp_filter ); 701 701 } 702 702 703 703 /**