wp/wp-includes/class-wp-hook.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    69 	 *                                  earlier execution, and functions with the same priority are executed
    69 	 *                                  earlier execution, and functions with the same priority are executed
    70 	 *                                  in the order in which they were added to the action.
    70 	 *                                  in the order in which they were added to the action.
    71 	 * @param int      $accepted_args   The number of arguments the function accepts.
    71 	 * @param int      $accepted_args   The number of arguments the function accepts.
    72 	 */
    72 	 */
    73 	public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
    73 	public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
    74 		$idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
    74 		$idx              = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
    75 		$priority_existed = isset( $this->callbacks[ $priority ] );
    75 		$priority_existed = isset( $this->callbacks[ $priority ] );
    76 
    76 
    77 		$this->callbacks[ $priority ][ $idx ] = array(
    77 		$this->callbacks[ $priority ][ $idx ] = array(
    78 			'function' => $function_to_add,
    78 			'function'      => $function_to_add,
    79 			'accepted_args' => $accepted_args
    79 			'accepted_args' => $accepted_args,
    80 		);
    80 		);
    81 
    81 
    82 		// if we're adding a new priority to the list, put them back in sorted order
    82 		// if we're adding a new priority to the list, put them back in sorted order
    83 		if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
    83 		if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
    84 			ksort( $this->callbacks, SORT_NUMERIC );
    84 			ksort( $this->callbacks, SORT_NUMERIC );
   241 			return;
   241 			return;
   242 		}
   242 		}
   243 
   243 
   244 		if ( false === $priority ) {
   244 		if ( false === $priority ) {
   245 			$this->callbacks = array();
   245 			$this->callbacks = array();
   246 		} else if ( isset( $this->callbacks[ $priority ] ) ) {
   246 		} elseif ( isset( $this->callbacks[ $priority ] ) ) {
   247 			unset( $this->callbacks[ $priority ] );
   247 			unset( $this->callbacks[ $priority ] );
   248 		}
   248 		}
   249 
   249 
   250 		if ( $this->nesting_level > 0 ) {
   250 		if ( $this->nesting_level > 0 ) {
   251 			$this->resort_active_iterations();
   251 			$this->resort_active_iterations();
   267 		}
   267 		}
   268 
   268 
   269 		$nesting_level = $this->nesting_level++;
   269 		$nesting_level = $this->nesting_level++;
   270 
   270 
   271 		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
   271 		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
   272 		$num_args = count( $args );
   272 		$num_args                           = count( $args );
   273 
   273 
   274 		do {
   274 		do {
   275 			$this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $nesting_level ] );
   275 			$this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $nesting_level ] );
   276 
   276 
   277 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
   277 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
   278 				if( ! $this->doing_action ) {
   278 				if ( ! $this->doing_action ) {
   279 					$args[ 0 ] = $value;
   279 					$args[0] = $value;
   280 				}
   280 				}
   281 
   281 
   282 				// Avoid the array_slice if possible.
   282 				// Avoid the array_slice if possible.
   283 				if ( $the_['accepted_args'] == 0 ) {
   283 				if ( $the_['accepted_args'] == 0 ) {
   284 					$value = call_user_func_array( $the_['function'], array() );
   284 					$value = call_user_func_array( $the_['function'], array() );
   285 				} elseif ( $the_['accepted_args'] >= $num_args ) {
   285 				} elseif ( $the_['accepted_args'] >= $num_args ) {
   286 					$value = call_user_func_array( $the_['function'], $args );
   286 					$value = call_user_func_array( $the_['function'], $args );
   287 				} else {
   287 				} else {
   288 					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) );
   288 					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
   289 				}
   289 				}
   290 			}
   290 			}
   291 		} while ( false !== next( $this->iterations[ $nesting_level ] ) );
   291 		} while ( false !== next( $this->iterations[ $nesting_level ] ) );
   292 
   292 
   293 		unset( $this->iterations[ $nesting_level ] );
   293 		unset( $this->iterations[ $nesting_level ] );
   321 	 * @since 4.7.0
   321 	 * @since 4.7.0
   322 	 *
   322 	 *
   323 	 * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
   323 	 * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
   324 	 */
   324 	 */
   325 	public function do_all_hook( &$args ) {
   325 	public function do_all_hook( &$args ) {
   326 		$nesting_level = $this->nesting_level++;
   326 		$nesting_level                      = $this->nesting_level++;
   327 		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
   327 		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
   328 
   328 
   329 		do {
   329 		do {
   330 			$priority = current( $this->iterations[ $nesting_level ] );
   330 			$priority = current( $this->iterations[ $nesting_level ] );
   331 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
   331 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
   354 
   354 
   355 	/**
   355 	/**
   356 	 * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
   356 	 * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
   357 	 *
   357 	 *
   358 	 * @since 4.7.0
   358 	 * @since 4.7.0
   359 	 * @static
       
   360 	 *
   359 	 *
   361 	 * @param array $filters Filters to normalize.
   360 	 * @param array $filters Filters to normalize.
   362 	 * @return WP_Hook[] Array of normalized filters.
   361 	 * @return WP_Hook[] Array of normalized filters.
   363 	 */
   362 	 */
   364 	public static function build_preinitialized_hooks( $filters ) {
   363 	public static function build_preinitialized_hooks( $filters ) {