62 * |
62 * |
63 * @since 4.7.0 |
63 * @since 4.7.0 |
64 * |
64 * |
65 * @param string $tag The name of the filter to hook the $function_to_add callback to. |
65 * @param string $tag The name of the filter to hook the $function_to_add callback to. |
66 * @param callable $function_to_add The callback to be run when the filter is applied. |
66 * @param callable $function_to_add The callback to be run when the filter is applied. |
67 * @param int $priority The order in which the functions associated with a |
67 * @param int $priority The order in which the functions associated with a particular action |
68 * particular action are executed. Lower numbers correspond with |
68 * are executed. Lower numbers correspond with earlier execution, |
69 * earlier execution, and functions with the same priority are executed |
69 * and functions with the same priority are executed in the order |
70 * in the order in which they were added to the action. |
70 * 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 |
75 $priority_existed = isset( $this->callbacks[ $priority ] ); |
76 $priority_existed = isset( $this->callbacks[ $priority ] ); |
76 |
77 |
77 $this->callbacks[ $priority ][ $idx ] = array( |
78 $this->callbacks[ $priority ][ $idx ] = array( |
78 'function' => $function_to_add, |
79 'function' => $function_to_add, |
79 'accepted_args' => $accepted_args, |
80 'accepted_args' => $accepted_args, |
80 ); |
81 ); |
81 |
82 |
82 // if we're adding a new priority to the list, put them back in sorted order |
83 // If we're adding a new priority to the list, put them back in sorted order. |
83 if ( ! $priority_existed && count( $this->callbacks ) > 1 ) { |
84 if ( ! $priority_existed && count( $this->callbacks ) > 1 ) { |
84 ksort( $this->callbacks, SORT_NUMERIC ); |
85 ksort( $this->callbacks, SORT_NUMERIC ); |
85 } |
86 } |
86 |
87 |
87 if ( $this->nesting_level > 0 ) { |
88 if ( $this->nesting_level > 0 ) { |
88 $this->resort_active_iterations( $priority, $priority_existed ); |
89 $this->resort_active_iterations( $priority, $priority_existed ); |
89 } |
90 } |
90 } |
91 } |
91 |
92 |
92 /** |
93 /** |
93 * Handles reseting callback priority keys mid-iteration. |
94 * Handles resetting callback priority keys mid-iteration. |
94 * |
95 * |
95 * @since 4.7.0 |
96 * @since 4.7.0 |
96 * |
97 * |
97 * @param bool|int $new_priority Optional. The priority of the new filter being added. Default false, |
98 * @param bool|int $new_priority Optional. The priority of the new filter being added. Default false, |
98 * for no priority being added. |
99 * for no priority being added. |
132 } |
133 } |
133 |
134 |
134 // If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority... |
135 // If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority... |
135 if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) { |
136 if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) { |
136 /* |
137 /* |
137 * ... and the new priority is the same as what $this->iterations thinks is the previous |
138 * ...and the new priority is the same as what $this->iterations thinks is the previous |
138 * priority, we need to move back to it. |
139 * priority, we need to move back to it. |
139 */ |
140 */ |
140 |
141 |
141 if ( false === current( $iteration ) ) { |
142 if ( false === current( $iteration ) ) { |
142 // If we've already moved off the end of the array, go back to the last element. |
143 // If we've already moved off the end of the array, go back to the last element. |
160 /** |
161 /** |
161 * Unhooks a function or method from a specific filter action. |
162 * Unhooks a function or method from a specific filter action. |
162 * |
163 * |
163 * @since 4.7.0 |
164 * @since 4.7.0 |
164 * |
165 * |
165 * @param string $tag The filter hook to which the function to be removed is hooked. Used |
166 * @param string $tag The filter hook to which the function to be removed is hooked. |
166 * for building the callback ID when SPL is not available. |
|
167 * @param callable $function_to_remove The callback to be removed from running when the filter is applied. |
167 * @param callable $function_to_remove The callback to be removed from running when the filter is applied. |
168 * @param int $priority The exact priority used when adding the original filter callback. |
168 * @param int $priority The exact priority used when adding the original filter callback. |
169 * @return bool Whether the callback existed before it was removed. |
169 * @return bool Whether the callback existed before it was removed. |
170 */ |
170 */ |
171 public function remove_filter( $tag, $function_to_remove, $priority ) { |
171 public function remove_filter( $tag, $function_to_remove, $priority ) { |
187 /** |
187 /** |
188 * Checks if a specific action has been registered for this hook. |
188 * Checks if a specific action has been registered for this hook. |
189 * |
189 * |
190 * @since 4.7.0 |
190 * @since 4.7.0 |
191 * |
191 * |
192 * @param string $tag Optional. The name of the filter hook. Used for building |
192 * @param string $tag Optional. The name of the filter hook. Default empty. |
193 * the callback ID when SPL is not available. Default empty. |
|
194 * @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
193 * @param callable|bool $function_to_check Optional. The callback to check for. Default false. |
195 * @return bool|int The priority of that hook is returned, or false if the function is not attached. |
194 * @return bool|int The priority of that hook is returned, or false if the function is not attached. |
196 */ |
195 */ |
197 public function has_filter( $tag = '', $function_to_check = false ) { |
196 public function has_filter( $tag = '', $function_to_check = false ) { |
198 if ( false === $function_to_check ) { |
197 if ( false === $function_to_check ) { |
251 $this->resort_active_iterations(); |
250 $this->resort_active_iterations(); |
252 } |
251 } |
253 } |
252 } |
254 |
253 |
255 /** |
254 /** |
256 * Calls the callback functions added to a filter hook. |
255 * Calls the callback functions that have been added to a filter hook. |
257 * |
256 * |
258 * @since 4.7.0 |
257 * @since 4.7.0 |
259 * |
258 * |
260 * @param mixed $value The value to filter. |
259 * @param mixed $value The value to filter. |
261 * @param array $args Arguments to pass to callbacks. |
260 * @param array $args Additional parameters to pass to the callback functions. |
|
261 * This array is expected to include $value at index 0. |
262 * @return mixed The filtered value after all hooked functions are applied to it. |
262 * @return mixed The filtered value after all hooked functions are applied to it. |
263 */ |
263 */ |
264 public function apply_filters( $value, $args ) { |
264 public function apply_filters( $value, $args ) { |
265 if ( ! $this->callbacks ) { |
265 if ( ! $this->callbacks ) { |
266 return $value; |
266 return $value; |
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 ] = current( $this->iterations[ $nesting_level ] ); |
|
276 $priority = $this->current_priority[ $nesting_level ]; |
276 |
277 |
277 foreach ( $this->callbacks[ $priority ] as $the_ ) { |
278 foreach ( $this->callbacks[ $priority ] as $the_ ) { |
278 if ( ! $this->doing_action ) { |
279 if ( ! $this->doing_action ) { |
279 $args[0] = $value; |
280 $args[0] = $value; |
280 } |
281 } |
281 |
282 |
282 // Avoid the array_slice if possible. |
283 // Avoid the array_slice() if possible. |
283 if ( $the_['accepted_args'] == 0 ) { |
284 if ( 0 == $the_['accepted_args'] ) { |
284 $value = call_user_func_array( $the_['function'], array() ); |
285 $value = call_user_func( $the_['function'] ); |
285 } elseif ( $the_['accepted_args'] >= $num_args ) { |
286 } elseif ( $the_['accepted_args'] >= $num_args ) { |
286 $value = call_user_func_array( $the_['function'], $args ); |
287 $value = call_user_func_array( $the_['function'], $args ); |
287 } else { |
288 } else { |
288 $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); |
289 $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); |
289 } |
290 } |
401 /** |
402 /** |
402 * Retrieves a value at a specified offset. |
403 * Retrieves a value at a specified offset. |
403 * |
404 * |
404 * @since 4.7.0 |
405 * @since 4.7.0 |
405 * |
406 * |
406 * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php |
407 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php |
407 * |
408 * |
408 * @param mixed $offset The offset to retrieve. |
409 * @param mixed $offset The offset to retrieve. |
409 * @return mixed If set, the value at the specified offset, null otherwise. |
410 * @return mixed If set, the value at the specified offset, null otherwise. |
410 */ |
411 */ |
411 public function offsetGet( $offset ) { |
412 public function offsetGet( $offset ) { |