<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38571] trunk: Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38571">38571</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38571","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>pento</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-08 03:54:13 +0000 (Thu, 08 Sep 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.
Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.
And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.
Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes <a href="https://core.trac.wordpress.org/ticket/17817">#17817</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespluginphp">trunk/src/wp-includes/plugin.php</a></li>
<li><a href="#trunktestsphpunitincludesfunctionsphp">trunk/tests/phpunit/includes/functions.php</a></li>
<li><a href="#trunktestsphpunitincludestestcasephp">trunk/tests/phpunit/includes/testcase.php</a></li>
<li><a href="#trunktestsphpunittestsactionsphp">trunk/tests/phpunit/tests/actions.php</a></li>
<li><a href="#trunktestsphpunittestsfiltersphp">trunk/tests/phpunit/tests/filters.php</a></li>
<li><a href="#trunktestsphpunittestsposttypesphp">trunk/tests/phpunit/tests/post/types.php</a></li>
<li><a href="#trunktestsphpunitteststaxonomyphp">trunk/tests/phpunit/tests/taxonomy.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswphookphp">trunk/src/wp-includes/class-wp-hook.php</a></li>
<li>trunk/tests/phpunit/tests/hooks/</li>
<li><a href="#trunktestsphpunittestshooksadd_filterphp">trunk/tests/phpunit/tests/hooks/add_filter.php</a></li>
<li><a href="#trunktestsphpunittestshooksapply_filtersphp">trunk/tests/phpunit/tests/hooks/apply_filters.php</a></li>
<li><a href="#trunktestsphpunittestshooksdo_actionphp">trunk/tests/phpunit/tests/hooks/do_action.php</a></li>
<li><a href="#trunktestsphpunittestshooksdo_all_hookphp">trunk/tests/phpunit/tests/hooks/do_all_hook.php</a></li>
<li><a href="#trunktestsphpunittestshookshas_filterphp">trunk/tests/phpunit/tests/hooks/has_filter.php</a></li>
<li><a href="#trunktestsphpunittestshookshas_filtersphp">trunk/tests/phpunit/tests/hooks/has_filters.php</a></li>
<li><a href="#trunktestsphpunittestshooksiteratorphp">trunk/tests/phpunit/tests/hooks/iterator.php</a></li>
<li><a href="#trunktestsphpunittestshookspreinit_hooksphp">trunk/tests/phpunit/tests/hooks/preinit_hooks.php</a></li>
<li><a href="#trunktestsphpunittestshooksremove_all_filtersphp">trunk/tests/phpunit/tests/hooks/remove_all_filters.php</a></li>
<li><a href="#trunktestsphpunittestshooksremove_filterphp">trunk/tests/phpunit/tests/hooks/remove_filter.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswphookphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/class-wp-hook.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-hook.php (rev 0)
+++ trunk/src/wp-includes/class-wp-hook.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,516 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Plugin API: WP_Hook class
+ *
+ * @package WordPress
+ * @subpackage Plugin
+ * @since 4.7.0
+ */
+
+/**
+ * Core class used to implement action and filter hook functionality.
+ *
+ * @since 4.7.0
+ *
+ * @see Iterator
+ * @see ArrayAccess
+ */
+final class WP_Hook implements Iterator, ArrayAccess {
+
+ /**
+ * Hook callbacks.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var array
+ */
+ public $callbacks = array();
+
+ /**
+ * The priority keys of actively running iterations of a hook.
+ *
+ * @since 4.7.0
+ * @access private
+ * @var array
+ */
+ private $iterations = array();
+
+ /**
+ * The current priority of actively running iterations of a hook.
+ *
+ * @since 4.7.0
+ * @access private
+ * @var array
+ */
+ private $current_priority = array();
+
+ /**
+ * Number of levels this hook can be recursively called.
+ *
+ * @since 4.7.0
+ * @access private
+ * @var int
+ */
+ private $nesting_level = 0;
+
+ /**
+ * Flag for if we're current doing an action, rather than a filter.
+ *
+ * @since 4.7.0
+ * @access private
+ * @var boolean
+ */
+ private $doing_action = false;
+
+ /**
+ * Hooks a function or method to a specific filter action.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param string $tag The name of the filter to hook the $function_to_add callback to.
+ * @param callable $function_to_add The callback to be run when the filter is applied.
+ * @param int $priority The order in which the functions associated with a
+ * particular action are executed. Lower numbers correspond with
+ * earlier execution, and functions with the same priority are executed
+ * in the order in which they were added to the action.
+ * @param int $accepted_args The number of arguments the function accepts.
+ */
+ public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
+ $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
+ $priority_existed = isset( $this->callbacks[ $priority ] );
+
+ $this->callbacks[ $priority ][ $idx ] = array(
+ 'function' => $function_to_add,
+ 'accepted_args' => $accepted_args
+ );
+
+ // if we're adding a new priority to the list, put them back in sorted order
+ if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
+ ksort( $this->callbacks, SORT_NUMERIC );
+ }
+
+ if ( $this->nesting_level > 0 ) {
+ $this->resort_active_iterations( $priority, $priority_existed );
+ }
+ }
+
+ /**
+ * Handles reseting callback priority keys mid-iteration.
+ *
+ * @since 4.7.0
+ * @access private
+ *
+ * @param bool|int $new_priority The priority of the new filter being added. Default false, for no priority being added.
+ * @param bool $priority_existed Flag for whether the priority already existed before the new filter was added.
+ */
+ private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
+ $new_priorities = array_keys( $this->callbacks );
+
+ // If there are no remaining hooks, clear out all running iterations.
+ if ( ! $new_priorities ) {
+ foreach ( $this->iterations as $index => $iteration ) {
+ $this->iterations[ $index ] = $new_priorities;
+ }
+ return;
+ }
+
+ $min = min( $new_priorities );
+ foreach ( $this->iterations as $index => &$iteration ) {
+ $current = current( $iteration );
+ // If we're already at the end of this iteration, just leave the array pointer where it is.
+ if ( false === $current ) {
+ continue;
+ }
+
+ $iteration = $new_priorities;
+
+ if ( $current < $min ) {
+ array_unshift( $iteration, $current );
+ continue;
+ }
+
+ while ( current( $iteration ) < $current ) {
+ if ( false === next( $iteration ) ) {
+ break;
+ }
+ }
+
+ // If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority...
+ if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) {
+ // ... and the new priority is the same as what $this->iterations thinks is the previous priority,
+ // We need to move back to it.
+
+ if ( false === current( $iteration ) ) {
+ // If we've already moved off the end of the array, go back to the last element.
+ $prev = end( $iteration );
+ } else {
+ // Otherwise, just go back to the previous element.
+ $prev = prev( $iteration );
+ }
+ if ( false === $prev ) {
+ // Start of the array. Reset, and go about our day.
+ reset( $iteration );
+ } elseif ( $new_priority !== $prev ) {
+ // Previous wasn't the same. Move forward again.
+ next( $iteration );
+ }
+ }
+ }
+ unset( $iteration );
+ }
+
+ /**
+ * Unhooks a function or method from a specific filter action.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param string $tag The filter hook to which the function to be removed is hooked. Used
+ * for building the callback ID when SPL is not available.
+ * @param callable $function_to_remove The callback to be removed from running when the filter is applied.
+ * @param int $priority The exact priority used when adding the original filter callback.
+ * @return bool Whether the callback existed before it was removed.
+ */
+ public function remove_filter( $tag, $function_to_remove, $priority ) {
+ $function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
+
+ $exists = isset( $this->callbacks[ $priority ][ $function_key ] );
+ if ( $exists ) {
+ unset( $this->callbacks[ $priority ][ $function_key ] );
+ if ( ! $this->callbacks[ $priority ] ) {
+ unset( $this->callbacks[ $priority ] );
+ if ( $this->nesting_level > 0 ) {
+ $this->resort_active_iterations();
+ }
+ }
+ }
+ return $exists;
+ }
+
+ /**
+ * Checks if a specific action has been registered for this hook.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
+ * @param string $tag Optional. The name of the filter hook. Default empty.
+ * Used for building the callback ID when SPL is not available.
+ * @return bool|int The priority of that hook is returned, or false if the function is not attached.
+ */
+ public function has_filter( $tag = '', $function_to_check = false ) {
+ if ( false === $function_to_check ) {
+ return $this->has_filters();
+ }
+
+ $function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false );
+ if ( ! $function_key ) {
+ return false;
+ }
+
+ foreach ( $this->callbacks as $priority => $callbacks ) {
+ if ( isset( $callbacks[ $function_key ] ) ) {
+ return $priority;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Checks if any callbacks have been registered for this hook.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @return bool True if callbacks have been registered for the current hook, false otherwise.
+ */
+ public function has_filters() {
+ foreach ( $this->callbacks as $callbacks ) {
+ if ( $callbacks ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Removes all callbacks from the current filter.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param int|bool $priority Optional. The priority number to remove. Default false.
+ */
+ public function remove_all_filters( $priority = false ) {
+ if ( ! $this->callbacks ) {
+ return;
+ }
+
+ if ( false === $priority ) {
+ $this->callbacks = array();
+ } else if ( isset( $this->callbacks[ $priority ] ) ) {
+ unset( $this->callbacks[ $priority ] );
+ }
+
+ if ( $this->nesting_level > 0 ) {
+ $this->resort_active_iterations();
+ }
+ }
+
+ /**
+ * Calls the callback functions added to a filter hook.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param mixed $value The value to filter.
+ * @param array $args Arguments to pass to callbacks.
+ * @return mixed The filtered value after all hooked functions are applied to it.
+ */
+ public function apply_filters( $value, $args ) {
+ if ( ! $this->callbacks ) {
+ return $value;
+ }
+
+ $nesting_level = $this->nesting_level++;
+
+ $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
+ $num_args = count( $args );
+
+ do {
+ $this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $nesting_level ] );
+
+ foreach ( $this->callbacks[ $priority ] as $the_ ) {
+ if( ! $this->doing_action ) {
+ $args[ 0 ] = $value;
+ }
+
+ // Avoid the array_slice if possible.
+ if ( $the_['accepted_args'] == 0 ) {
+ $value = call_user_func_array( $the_['function'], array() );
+ } elseif ( $the_['accepted_args'] >= $num_args ) {
+ $value = call_user_func_array( $the_['function'], $args );
+ } else {
+ $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) );
+ }
+ }
+ } while ( false !== next( $this->iterations[ $nesting_level ] ) );
+
+ unset( $this->iterations[ $nesting_level ] );
+ unset( $this->current_priority[ $nesting_level ] );
+
+ $this->nesting_level--;
+
+ return $value;
+ }
+
+ /**
+ * Executes the callback functions hooked on a specific action hook.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param mixed $args Arguments to pass to the hook callbacks.
+ */
+ public function do_action( $args ) {
+ $this->doing_action = true;
+ $this->apply_filters( '', $args );
+
+ // If there are recursive calls to the current action, we haven't finished it until we get to the last one.
+ if ( ! $this->nesting_level ) {
+ $this->doing_action = false;
+ }
+ }
+
+ /**
+ * Processes the functions hooked into the 'all' hook.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
+ */
+ public function do_all_hook( &$args ) {
+ $nesting_level = $this->nesting_level++;
+ $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
+
+ do {
+ $priority = current( $this->iterations[ $nesting_level ] );
+ foreach ( $this->callbacks[ $priority ] as $the_ ) {
+ call_user_func_array( $the_['function'], $args );
+ }
+ } while ( false !== next( $this->iterations[ $nesting_level ] ) );
+
+ unset( $this->iterations[ $nesting_level ] );
+ $this->nesting_level--;
+ }
+
+ /**
+ * Normalizes filters setup before WordPress has initialized to WP_Hook objects.
+ *
+ * @since 4.7.0
+ * @access public
+ * @static
+ *
+ * @param array $filters Filters to normalize.
+ * @return WP_Hook[] Array of normalized filters.
+ */
+ public static function build_preinitialized_hooks( $filters ) {
+ /** @var WP_Hook[] $normalized */
+ $normalized = array();
+
+ foreach ( $filters as $tag => $callback_groups ) {
+ if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
+ $normalized[ $tag ] = $callback_groups;
+ continue;
+ }
+ $hook = new WP_Hook();
+
+ // Loop through callback groups.
+ foreach ( $callback_groups as $priority => $callbacks ) {
+
+ // Loop through callbacks.
+ foreach ( $callbacks as $cb ) {
+ $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
+ }
+ }
+ $normalized[ $tag ] = $hook;
+ }
+ return $normalized;
+ }
+
+ /**
+ * Determines whether an offset value exists.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/arrayaccess.offsetexists.php
+ *
+ * @param mixed $offset An offset to check for.
+ * @return bool True if the offset exists, false otherwise.
+ */
+ public function offsetExists( $offset ) {
+ return isset( $this->callbacks[ $offset ] );
+ }
+
+ /**
+ * Retrieves a value at a specified offset.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/arrayaccess.offsetget.php
+ *
+ * @param mixed $offset The offset to retrieve.
+ * @return mixed If set, the value at the specified offset, null otherwise.
+ */
+ public function offsetGet( $offset ) {
+ return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
+ }
+
+ /**
+ * Sets a value at a specified offset.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/arrayaccess.offsetset.php
+ *
+ * @param mixed $offset The offset to assign the value to.
+ * @param mixed $value The value to set.
+ */
+ public function offsetSet( $offset, $value ) {
+ if ( is_null( $offset ) ) {
+ $this->callbacks[] = $value;
+ } else {
+ $this->callbacks[ $offset ] = $value;
+ }
+ }
+
+ /**
+ * Unsets a specified offset.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/arrayaccess.offsetunset.php
+ *
+ * @param mixed $offset The offset to unset.
+ */
+ public function offsetUnset( $offset ) {
+ unset( $this->callbacks[ $offset ] );
+ }
+
+ /**
+ * Return the current element
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/iterator.current.php
+ *
+ * @return array Of callbacks at current priority.
+ */
+ public function current() {
+ return current( $this->callbacks );
+ }
+
+ /**
+ * Move forward to the next element
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/iterator.next.php
+ *
+ * @return array Of callbacks at next priority.
+ */
+ public function next() {
+ return next( $this->callbacks );
+ }
+
+ /**
+ * Return the key of the current element
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/iterator.key.php
+ *
+ * @return mixed Returns current priority on success, or NULL on failure
+ */
+ public function key() {
+ return key( $this->callbacks );
+ }
+
+ /**
+ * Checks if current position is valid
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/iterator.valid.php
+ *
+ * @return boolean
+ */
+ public function valid() {
+ return key( $this->callbacks ) !== null;
+ }
+
+ /**
+ * Rewind the Iterator to the first element
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @link http://php.net/manual/en/iterator.rewind.php
+ */
+ public function rewind() {
+ reset( $this->callbacks );
+ }
+
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/class-wp-hook.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludespluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/plugin.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/src/wp-includes/plugin.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,17 +22,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Initialize the filter globals.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/class-wp-hook.php' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( ! isset( $wp_filter ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/** @var WP_Hook[] $wp_filter */
+global $wp_filter, $wp_actions, $wp_current_filter;
+
+if ( $wp_filter ) {
+ $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
+} else {
</ins><span class="cx" style="display: block; padding: 0 10px"> $wp_filter = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $wp_actions ) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_actions = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( ! isset( $merged_filters ) )
- $merged_filters = array();
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $wp_current_filter ) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_current_filter = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -89,8 +92,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 0.71
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added,
- * it doesn't need to run through that process.
</del><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The name of the filter to hook the $function_to_add callback to.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function_to_add The callback to be run when the filter is applied.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -103,11 +104,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return true
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $merged_filters;
-
- $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
- $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
- unset( $merged_filters[ $tag ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter;
+ if ( ! isset( $wp_filter[ $tag ] ) ) {
+ $wp_filter[ $tag ] = new WP_Hook();
+ }
+ $wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
</ins><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,38 +129,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * return value.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function has_filter($tag, $function_to_check = false) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Don't reset the internal array pointer
- $wp_filter = $GLOBALS['wp_filter'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $has = ! empty( $wp_filter[ $tag ] );
-
- // Make sure at least one priority has a filter callback
- if ( $has ) {
- $exists = false;
- foreach ( $wp_filter[ $tag ] as $callbacks ) {
- if ( ! empty( $callbacks ) ) {
- $exists = true;
- break;
- }
- }
-
- if ( ! $exists ) {
- $has = false;
- }
- }
-
- if ( false === $function_to_check || false === $has )
- return $has;
-
- if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $wp_filter[ $tag ] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
- if ( isset($wp_filter[$tag][$priority][$idx]) )
- return $priority;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,7 +166,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 0.71
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter Stores all of the filters.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $merged_filters Merges the filter hooks using this function.
</del><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_current_filter Stores the list of current filters with the current one last.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The name of the filter hook.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -199,7 +174,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed The filtered value after all hooked functions are applied to it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function apply_filters( $tag, $value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $merged_filters, $wp_current_filter;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter, $wp_current_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $args = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -219,29 +194,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !isset($wp_filter['all']) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_current_filter[] = $tag;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Sort.
- if ( !isset( $merged_filters[ $tag ] ) ) {
- ksort($wp_filter[$tag]);
- $merged_filters[ $tag ] = true;
- }
-
- reset( $wp_filter[ $tag ] );
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( empty($args) )
</span><span class="cx" style="display: block; padding: 0 10px"> $args = func_get_args();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- do {
- foreach ( (array) current($wp_filter[$tag]) as $the_ )
- if ( !is_null($the_['function']) ){
- $args[1] = $value;
- $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // don't pass the tag name to WP_Hook
+ array_shift( $args );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } while ( next($wp_filter[$tag]) !== false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> array_pop( $wp_current_filter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $value;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $filtered;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -253,7 +216,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * functions hooked to `$tag` are supplied using an array.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter Stores all of the filters
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $merged_filters Merges the filter hooks using this function.
</del><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_current_filter Stores the list of current filters with the current one last
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The name of the filter hook.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -261,7 +223,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed The filtered value after all hooked functions are applied to it.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function apply_filters_ref_array($tag, $args) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $merged_filters, $wp_current_filter;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter, $wp_current_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Do 'all' actions first
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset($wp_filter['all']) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -279,24 +241,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !isset($wp_filter['all']) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_current_filter[] = $tag;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Sort
- if ( !isset( $merged_filters[ $tag ] ) ) {
- ksort($wp_filter[$tag]);
- $merged_filters[ $tag ] = true;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $filtered = $wp_filter[ $tag ]->apply_filters( $args[0], $args );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- reset( $wp_filter[ $tag ] );
-
- do {
- foreach ( (array) current($wp_filter[$tag]) as $the_ )
- if ( !is_null($the_['function']) )
- $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
-
- } while ( next($wp_filter[$tag]) !== false );
-
</del><span class="cx" style="display: block; padding: 0 10px"> array_pop( $wp_current_filter );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $args[0];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $filtered;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -313,7 +262,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.2.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter Stores all of the filters
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $merged_filters Merges the filter hooks using this function.
</del><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The filter hook to which the function to be removed is hooked.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $function_to_remove The name of the function which should be removed.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -321,19 +269,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool Whether the function existed before it was removed.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $function_to_remove = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $r = isset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
-
- if ( true === $r ) {
- unset( $GLOBALS['wp_filter'][ $tag ][ $priority ][ $function_to_remove ] );
- if ( empty( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) {
- unset( $GLOBALS['wp_filter'][ $tag ][ $priority ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $r = false;
+ if ( isset( $wp_filter[ $tag ] ) ) {
+ $r = $wp_filter[ $tag ]->remove_filter( $tag, $function_to_remove, $priority );
+ if ( ! $wp_filter[ $tag ]->callbacks ) {
+ unset( $wp_filter[ $tag ] );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $GLOBALS['wp_filter'][ $tag ] ) ) {
- $GLOBALS['wp_filter'][ $tag ] = array();
- }
- unset( $GLOBALS['merged_filters'][ $tag ] );
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $r;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -344,26 +287,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $wp_filter Stores all of the filters
- * @global array $merged_filters Merges the filter hooks using this function.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @global array $wp_filter Stores all of the filters
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The filter to remove hooks from.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int|bool $priority Optional. The priority number to remove. Default false.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return true True when finished.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function remove_all_filters( $tag, $priority = false ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $merged_filters;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $wp_filter[ $tag ]) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( false === $priority ) {
- $wp_filter[ $tag ] = array();
- } elseif ( isset( $wp_filter[ $tag ][ $priority ] ) ) {
- $wp_filter[ $tag ][ $priority ] = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_filter[ $tag ]->remove_all_filters( $priority );
+ if ( ! $wp_filter[ $tag ]->has_filters() ) {
+ unset( $wp_filter[ $tag ] );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- unset( $merged_filters[ $tag ] );
-
</del><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -473,7 +412,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter Stores all of the filters
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_actions Increments the amount of times action was triggered.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $merged_filters Merges the filter hooks using this function.
</del><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_current_filter Stores the list of current filters with the current one last
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The name of the action to be executed.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,7 +419,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * functions hooked to the action. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function do_action($tag, $arg = '') {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter, $wp_actions, $wp_current_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset($wp_actions[$tag]) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_actions[$tag] = 1;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -512,21 +450,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
</span><span class="cx" style="display: block; padding: 0 10px"> $args[] = func_get_arg($a);
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Sort
- if ( !isset( $merged_filters[ $tag ] ) ) {
- ksort($wp_filter[$tag]);
- $merged_filters[ $tag ] = true;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_filter[ $tag ]->do_action( $args );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- reset( $wp_filter[ $tag ] );
-
- do {
- foreach ( (array) current($wp_filter[$tag]) as $the_ )
- if ( !is_null($the_['function']) )
- call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
-
- } while ( next($wp_filter[$tag]) !== false );
-
</del><span class="cx" style="display: block; padding: 0 10px"> array_pop($wp_current_filter);
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -558,14 +483,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * functions hooked to $tag< are supplied using an array.
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter Stores all of the filters
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_actions Increments the amount of times action was triggered.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global array $merged_filters Merges the filter hooks using this function.
</del><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_current_filter Stores the list of current filters with the current one last
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag The name of the action to be executed.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $args The arguments supplied to the functions hooked to `$tag`.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function do_action_ref_array($tag, $args) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter, $wp_actions, $wp_current_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset($wp_actions[$tag]) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_actions[$tag] = 1;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -588,21 +512,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !isset($wp_filter['all']) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_current_filter[] = $tag;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Sort
- if ( !isset( $merged_filters[ $tag ] ) ) {
- ksort($wp_filter[$tag]);
- $merged_filters[ $tag ] = true;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_filter[ $tag ]->do_action( $args );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- reset( $wp_filter[ $tag ] );
-
- do {
- foreach ( (array) current($wp_filter[$tag]) as $the_ )
- if ( !is_null($the_['function']) )
- call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
-
- } while ( next($wp_filter[$tag]) !== false );
-
</del><span class="cx" style="display: block; padding: 0 10px"> array_pop($wp_current_filter);
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -923,13 +834,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function _wp_call_all_hook($args) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_filter;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- reset( $wp_filter['all'] );
- do {
- foreach ( (array) current($wp_filter['all']) as $the_ )
- if ( !is_null($the_['function']) )
- call_user_func_array($the_['function'], $args);
-
- } while ( next($wp_filter['all']) !== false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_filter['all']->do_all_hook( $args );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunitincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/functions.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/tests/phpunit/includes/functions.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,18 +21,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // For adding hooks before loading WP
</span><span class="cx" style="display: block; padding: 0 10px"> function tests_add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter, $merged_filters;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_filter;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $idx = _test_filter_build_unique_id($tag, $function_to_add, $priority);
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- unset( $merged_filters[ $tag ] );
</del><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> function _test_filter_build_unique_id($tag, $function, $priority) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_filter;
- static $filter_id_count = 0;
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( is_string($function) )
</span><span class="cx" style="display: block; padding: 0 10px"> return $function;
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunitincludestestcasephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/testcase.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/testcase.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/tests/phpunit/includes/testcase.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,10 +223,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return void
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function _backup_hooks() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $globals = array( 'wp_actions', 'wp_current_filter' );
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $globals as $key ) {
</span><span class="cx" style="display: block; padding: 0 10px"> self::$hooks_saved[ $key ] = $GLOBALS[ $key ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$hooks_saved['wp_filter'] = array();
+ foreach ( $GLOBALS['wp_filter'] as $hook_name => $hook_object ) {
+ self::$hooks_saved['wp_filter'][ $hook_name ] = clone $hook_object;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -240,12 +244,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return void
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function _restore_hooks() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $globals = array( 'wp_actions', 'wp_current_filter' );
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $globals as $key ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( self::$hooks_saved[ $key ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $GLOBALS[ $key ] = self::$hooks_saved[ $key ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( self::$hooks_saved['wp_filter'] ) ) {
+ $GLOBALS['wp_filter'] = array();
+ foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) {
+ $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object;
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> static function flush_cache() {
</span></span></pre></div>
<a id="trunktestsphpunittestsactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/actions.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/tests/phpunit/tests/actions.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -114,6 +114,45 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Test that multiple callbacks receive the correct number of args even when the number
+ * is less than, or greater than previous hooks.
+ *
+ * @see https://core.trac.wordpress.org/ticket/17817#comment:72
+ * @ticket 17817
+ */
+ function test_action_args_3() {
+ $a1 = new MockAction();
+ $a2 = new MockAction();
+ $a3 = new MockAction();
+ $tag = rand_str();
+ $val1 = rand_str();
+ $val2 = rand_str();
+
+ // a1 accepts two arguments, a2 doesn't, a3 accepts two arguments
+ add_action( $tag, array( &$a1, 'action' ), 10, 2 );
+ add_action( $tag, array( &$a2, 'action' ) );
+ add_action( $tag, array( &$a3, 'action' ), 10, 2 );
+ // call the action with two arguments
+ do_action( $tag, $val1, $val2 );
+
+ $call_count = $a1->get_call_count();
+ // a1 should be called with both args
+ $this->assertEquals( 1, $call_count );
+ $argsvar1 = $a1->get_args();
+ $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) );
+
+ // a2 should be called with one only
+ $this->assertEquals( 1, $a2->get_call_count() );
+ $argsvar2 = $a2->get_args();
+ $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
+
+ // a3 should be called with both args
+ $this->assertEquals( 1, $a3->get_call_count() );
+ $argsvar3 = $a3->get_args();
+ $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar3 ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> function test_action_priority() {
</span><span class="cx" style="display: block; padding: 0 10px"> $a = new MockAction();
</span><span class="cx" style="display: block; padding: 0 10px"> $tag = rand_str();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -258,6 +297,120 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 17817
+ */
+ function test_action_recursion() {
+ $tag = rand_str();
+ $a = new MockAction();
+ $b = new MockAction();
+
+ add_action( $tag, array( $a, 'action' ), 11, 1 );
+ add_action( $tag, array( $b, 'action' ), 13, 1 );
+ add_action( $tag, array( $this, 'action_that_causes_recursion' ), 12, 1 );
+ do_action( $tag, $tag );
+
+ $this->assertEquals( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
+ $this->assertEquals( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
+ }
+
+ function action_that_causes_recursion( $tag ) {
+ static $recursing = false;
+ if ( ! $recursing ) {
+ $recursing = true;
+ do_action( $tag, $tag );
+ }
+ $recursing = false;
+ }
+
+ /**
+ * @ticket 9968
+ * @ticket 17817
+ */
+ function test_action_callback_manipulation_while_running() {
+ $tag = rand_str();
+ $a = new MockAction();
+ $b = new MockAction();
+ $c = new MockAction();
+ $d = new MockAction();
+ $e = new MockAction();
+
+ add_action( $tag, array( $a, 'action' ), 11, 2 );
+ add_action( $tag, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 );
+ add_action( $tag, array( $b, 'action' ), 12, 2 );
+
+ do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
+ do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
+
+ $this->assertEquals( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
+ $this->assertEquals( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
+ $this->assertEquals( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' );
+ $this->assertEquals( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' );
+ $this->assertEquals( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
+ }
+
+ function action_that_manipulates_a_running_hook( $tag, $mocks ) {
+ remove_action( $tag, array( $mocks[ 1 ], 'action' ), 12, 2 );
+ add_action( $tag, array( $mocks[ 2 ], 'action' ), 12, 2 );
+ add_action( $tag, array( $mocks[ 3 ], 'action' ), 13, 2 );
+ add_action( $tag, array( $mocks[ 4 ], 'action' ), 10, 2 );
+ }
+
+ /**
+ * @ticket 17817
+ *
+ * This specificaly addresses the concern raised at
+ * https://core.trac.wordpress.org/ticket/17817#comment:52
+ */
+ function test_remove_anonymous_callback() {
+ $tag = rand_str();
+ $a = new MockAction();
+ add_action( $tag, array( $a, 'action' ), 12, 1 );
+ $this->assertTrue( has_action( $tag ) );
+
+ $hook = $GLOBALS['wp_filter'][ $tag ];
+
+ // From http://wordpress.stackexchange.com/a/57088/6445
+ foreach ( $hook as $priority => $filter ) {
+ foreach ( $filter as $identifier => $function ) {
+ if ( is_array( $function )
+ && is_a( $function['function'][ 0 ], 'MockAction' )
+ && 'action' === $function['function'][ 1 ]
+ ) {
+ remove_filter(
+ $tag,
+ array( $function['function'][ 0 ], 'action' ),
+ $priority
+ );
+ }
+ }
+ }
+
+ $this->assertFalse( has_action( $tag ) );
+ }
+
+
+ /**
+ * Test the ArrayAccess methods of WP_Hook
+ *
+ * @ticket 17817
+ */
+ function test_array_access_of_wp_filter_global() {
+ global $wp_filter;
+ $tag = rand_str();
+
+ add_action( $tag, '__return_null', 11, 1 );
+
+ $this->assertTrue( isset( $wp_filter[ $tag ][ 11 ] ) );
+ $this->assertArrayHasKey( '__return_null', $wp_filter[ $tag ][ 11 ] );
+
+ unset( $wp_filter[ $tag ][ 11 ] );
+ $this->assertFalse( has_action( $tag, '__return_null' ) );
+
+ $wp_filter[ $tag ][ 11 ] = array( '__return_null' => array( 'function' => '__return_null', 'accepted_args' => 1 ) );
+ $this->assertEquals( 11, has_action( $tag, '__return_null' ) );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Make sure current_action() behaves as current_filter()
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 14994
</span></span></pre></div>
<a id="trunktestsphpunittestsfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/filters.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/tests/phpunit/tests/filters.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -296,27 +296,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @ticket 29070
- */
- function test_has_filter_doesnt_reset_wp_filter() {
- add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 1 );
- add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 2 );
- add_action( 'action_test_has_filter_doesnt_reset_wp_filter', '__return_null', 3 );
- add_action( 'action_test_has_filter_doesnt_reset_wp_filter', array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ), 4 );
-
- do_action( 'action_test_has_filter_doesnt_reset_wp_filter' );
- }
- function _action_test_has_filter_doesnt_reset_wp_filter() {
- global $wp_filter;
-
- has_action( 'action_test_has_filter_doesnt_reset_wp_filter', '_function_that_doesnt_exist' );
-
- $filters = current( $wp_filter['action_test_has_filter_doesnt_reset_wp_filter'] );
- $the_ = current( $filters );
- $this->assertEquals( $the_['function'], array( $this, '_action_test_has_filter_doesnt_reset_wp_filter' ) );
- }
-
- /**
</del><span class="cx" style="display: block; padding: 0 10px"> * @ticket 10441
</span><span class="cx" style="display: block; padding: 0 10px"> * @expectedDeprecated tests_apply_filters_deprecated
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre></div>
<a id="trunktestsphpunittestshooksadd_filterphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/add_filter.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/add_filter.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/add_filter.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,280 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+
+/**
+ * Test the add_filter method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Add_Filter extends WP_UnitTestCase {
+
+ public $hook;
+
+ public function test_add_filter_with_function() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );
+ $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
+ $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );
+ }
+
+ public function test_add_filter_with_object() {
+ $a = new MockAction();
+ $callback = array( $a, 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );
+ $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
+ $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );
+ }
+
+ public function test_add_filter_with_static_method() {
+ $callback = array( 'MockAction', 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );
+ $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
+ $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );
+ }
+
+ public function test_add_two_filters_with_same_priority() {
+ $callback_one = '__return_null';
+ $callback_two = '__return_false';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+
+ $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );
+ $this->assertCount( 2, $hook->callbacks[ $priority ] );
+ }
+
+ public function test_add_two_filters_with_different_priority() {
+ $callback_one = '__return_null';
+ $callback_two = '__return_false';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+
+ $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+ $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
+ }
+
+ public function test_readd_filter() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+ }
+
+ public function test_readd_filter_with_different_priority() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+
+ $hook->add_filter( $tag, $callback, $priority + 1, $accepted_args );
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+ $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
+ }
+
+ public function test_sort_after_add_filter() {
+ $a = new MockAction();
+ $b = new MockAction();
+ $c = new MockAction();
+ $hook = new WP_Hook();
+ $tag = rand_str();
+
+ $hook->add_filter( $tag, array( $a, 'action' ), 10, 1 );
+ $hook->add_filter( $tag, array( $b, 'action' ), 5, 1 );
+ $hook->add_filter( $tag, array( $c, 'action' ), 8, 1 );
+
+ $this->assertEquals( array( 5, 8, 10 ), array_keys( $hook->callbacks ) );
+ }
+
+ public function test_remove_and_add() {
+ $this->hook = new Wp_Hook();
+
+ $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
+
+ $value = $this->hook->apply_filters( '', array() );
+
+ $this->assertSame( '24', $value );
+ }
+
+ public function test_remove_and_add_last_filter() {
+ $this->hook = new Wp_Hook();
+
+ $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 12, 1 );
+
+ $value = $this->hook->apply_filters( '', array() );
+
+ $this->assertSame( '12', $value );
+ }
+
+ public function test_remove_and_recurse_and_add() {
+ $this->hook = new Wp_Hook();
+
+ $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add3' ), 11, 1 );
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
+
+ $value = $this->hook->apply_filters( '', array() );
+
+ $this->assertSame( '1-134-234', $value );
+ }
+
+ public function _filter_remove_and_add1( $string ) {
+ return $string . '1';
+ }
+
+ public function _filter_remove_and_add2( $string ) {
+ $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11 );
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
+
+ return $string . '2';
+ }
+
+ public function _filter_remove_and_recurse_and_add2( $string ) {
+ $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11 );
+
+ $string .= '-' . $this->hook->apply_filters( '', array() ) . '-';
+
+ $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );
+
+ return $string . '2';
+ }
+
+ public function _filter_remove_and_add3( $string ) {
+ return $string . '3';
+ }
+
+ public function _filter_remove_and_add4( $string ) {
+ return $string . '4';
+ }
+
+ public function test_remove_and_add_action() {
+ $this->hook = new Wp_Hook();
+ $this->action_output = '';
+
+ $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11, 0 );
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add4' ), 12, 0 );
+
+ $this->hook->do_action( array() );
+
+ $this->assertSame( '24', $this->action_output );
+ }
+
+ public function test_remove_and_add_last_action() {
+ $this->hook = new Wp_Hook();
+ $this->action_output = '';
+
+ $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add1' ), 11, 0 );
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 12, 0 );
+
+ $this->hook->do_action( array() );
+
+ $this->assertSame( '12', $this->action_output );
+ }
+
+ public function test_remove_and_recurse_and_add_action() {
+ $this->hook = new Wp_Hook();
+ $this->action_output = '';
+
+ $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add1' ), 11, 0 );
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11, 0 );
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add3' ), 11, 0 );
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add4' ), 12, 0 );
+
+ $this->hook->do_action( array() );
+
+ $this->assertSame( '1-134-234', $this->action_output );
+ }
+
+ public function _action_remove_and_add1() {
+ $this->action_output .= 1;
+ }
+
+ public function _action_remove_and_add2() {
+ $this->hook->remove_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11 );
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11, 0 );
+
+ $this->action_output .= '2';
+ }
+
+ public function _action_remove_and_recurse_and_add2() {
+ $this->hook->remove_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11 );
+
+ $this->action_output .= '-';
+ $this->hook->do_action( array() );
+ $this->action_output .= '-';
+
+ $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11, 0 );
+
+ $this->action_output .= '2';
+ }
+
+ public function _action_remove_and_add3() {
+ $this->action_output .= '3';
+ }
+
+ public function _action_remove_and_add4() {
+ $this->action_output .= '4';
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/add_filter.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshooksapply_filtersphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/apply_filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/apply_filters.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/apply_filters.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,45 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the apply_filters method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Apply_Filters extends WP_UnitTestCase {
+
+ public function test_apply_filters_with_callback() {
+ $a = new MockAction();
+ $callback = array( $a, 'filter' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $returned = $hook->apply_filters( $arg, array( $arg ) );
+
+ $this->assertEquals( $returned, $arg );
+ $this->assertEquals( 1, $a->get_call_count() );
+ }
+
+ public function test_apply_filters_with_multiple_calls() {
+ $a = new MockAction();
+ $callback = array( $a, 'filter' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $returned_one = $hook->apply_filters( $arg, array( $arg ) );
+ $returned_two = $hook->apply_filters( $returned_one, array( $returned_one ) );
+
+ $this->assertEquals( $returned_two, $arg );
+ $this->assertEquals( 2, $a->get_call_count() );
+ }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/apply_filters.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshooksdo_actionphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/do_action.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/do_action.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/do_action.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,172 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the do_action method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Do_Action extends WP_UnitTestCase {
+ private $events = array();
+ private $action_output = '';
+ private $hook;
+
+ public function setUp() {
+ parent::setUp();
+ $this->events = array();
+ }
+
+ public function test_do_action_with_callback() {
+ $a = new MockAction();
+ $callback = array( $a, 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertEquals( 1, $a->get_call_count() );
+ }
+
+ public function test_do_action_with_multiple_calls() {
+ $a = new MockAction();
+ $callback = array( $a, 'filter' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertEquals( 2, $a->get_call_count() );
+ }
+
+ public function test_do_action_with_multiple_callbacks_on_same_priority() {
+ $a = new MockAction();
+ $b = new MockAction();
+ $callback_one = array( $a, 'filter' );
+ $callback_two = array( $b, 'filter' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertEquals( 1, $a->get_call_count() );
+ $this->assertEquals( 1, $a->get_call_count() );
+ }
+
+ public function test_do_action_with_multiple_callbacks_on_different_priorities() {
+ $a = new MockAction();
+ $b = new MockAction();
+ $callback_one = array( $a, 'filter' );
+ $callback_two = array( $b, 'filter' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertEquals( 1, $a->get_call_count() );
+ $this->assertEquals( 1, $a->get_call_count() );
+ }
+
+ public function test_do_action_with_no_accepted_args() {
+ $callback = array( $this, '_action_callback' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = 0;
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertEmpty( $this->events[0]['args'] );
+ }
+
+ public function test_do_action_with_one_accepted_arg() {
+ $callback = array( $this, '_action_callback' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = 1;
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertCount( 1, $this->events[0]['args'] );
+ }
+
+ public function test_do_action_with_more_accepted_args() {
+ $callback = array( $this, '_action_callback' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = 1000;
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->do_action( array( $arg ) );
+
+ $this->assertCount( 1, $this->events[0]['args'] );
+ }
+
+ public function test_do_action_doesnt_change_value() {
+ $this->hook = new WP_Hook();
+ $this->action_output = '';
+
+ $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value1' ), 10, 1 );
+ $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );
+ $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value3' ), 11, 1 );
+
+ $this->hook->do_action( array( 'a' ) );
+
+ $this->assertSame( 'a1-b1b3-a2a3', $this->action_output );
+ }
+
+ public function _filter_do_action_doesnt_change_value1( $value ) {
+ $this->action_output .= $value . 1;
+ return 'x1';
+ }
+ public function _filter_do_action_doesnt_change_value2( $value ) {
+ $this->hook->remove_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10 );
+
+ $this->action_output .= '-';
+ $this->hook->do_action( array( 'b' ) );
+ $this->action_output .= '-';
+
+ $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );
+
+ $this->action_output .= $value . 2;
+
+ return 'x2';
+ }
+
+ public function _filter_do_action_doesnt_change_value3( $value ) {
+ $this->action_output .= $value . 3;
+ return 'x3';
+ }
+
+ /**
+ * Use this rather than MockAction so we can test callbacks with no args
+ */
+ public function _action_callback() {
+ $args = func_get_args();
+ $this->events[] = array('action' => __FUNCTION__, 'args'=>$args);
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/do_action.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshooksdo_all_hookphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/do_all_hook.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/do_all_hook.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/do_all_hook.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,26 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the do_all_hook method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Do_All_Hook extends WP_UnitTestCase {
+
+ public function test_do_all_hook_with_multiple_calls() {
+ $a = new MockAction();
+ $callback = array( $a, 'action' );
+ $hook = new WP_Hook();
+ $tag = 'all';
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+ $arg = rand_str();
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $args = array( $arg );
+ $hook->do_all_hook( $args );
+ $hook->do_all_hook( $args );
+
+ $this->assertEquals( 2, $a->get_call_count() );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/do_all_hook.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshookshas_filterphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/has_filter.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/has_filter.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/has_filter.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,83 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the has_filter method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Has_Filter extends WP_UnitTestCase {
+
+ public function test_has_filter_with_function() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) );
+ }
+
+ public function test_has_filter_with_object() {
+ $a = new MockAction();
+ $callback = array( $a, 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) );
+ }
+
+ public function test_has_filter_with_static_method() {
+ $callback = array( 'MockAction', 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) );
+ }
+
+ public function test_has_filter_without_callback() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $this->assertTrue( $hook->has_filter() );
+ }
+
+ public function test_not_has_filter_without_callback() {
+ $hook = new WP_Hook();
+ $this->assertFalse( $hook->has_filter() );
+ }
+
+ public function test_not_has_filter_with_callback() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+
+ $this->assertFalse( $hook->has_filter( $tag, $callback ) );
+ }
+
+ public function test_has_filter_with_wrong_callback() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $this->assertFalse( $hook->has_filter( $tag, '__return_false' ) );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/has_filter.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshookshas_filtersphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/has_filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/has_filters.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/has_filters.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,52 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the has_filters method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Has_Filters extends WP_UnitTestCase {
+
+ public function test_has_filters_with_callback() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $this->assertTrue( $hook->has_filters() );
+ }
+
+ public function test_has_filters_without_callback() {
+ $hook = new WP_Hook();
+ $this->assertFalse( $hook->has_filters() );
+ }
+
+ public function test_not_has_filters_with_removed_callback() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->remove_filter( $tag, $callback, $priority );
+ $this->assertFalse( $hook->has_filters() );
+ }
+
+ public function test_not_has_filter_with_directly_removed_callback() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $function_key = _wp_filter_build_unique_id( $tag, $callback, $priority );
+ unset( $hook->callbacks[ $priority ][ $function_key ] );
+
+ $this->assertFalse( $hook->has_filters() );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/has_filters.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshooksiteratorphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/iterator.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/iterator.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/iterator.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,32 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the Iterator implementation of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Iterator extends WP_UnitTestCase {
+
+ public function test_foreach() {
+ $callback_one = '__return_null';
+ $callback_two = '__return_false';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
+
+ $functions = array();
+ $priorities = array();
+ foreach ( $hook as $key => $callbacks ) {
+ $priorities[] = $key;
+ foreach ( $callbacks as $function_index => $the_ ) {
+ $functions[] = $the_['function'];
+ }
+ }
+ $this->assertEqualSets( array( $priority, $priority + 1 ), $priorities );
+ $this->assertEqualSets( array( $callback_one, $callback_two ), $functions );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/iterator.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshookspreinit_hooksphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/preinit_hooks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/preinit_hooks.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/preinit_hooks.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,39 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the IteratorAggregate implementation of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Preinit_Hooks extends WP_UnitTestCase {
+
+ public function test_array_to_hooks() {
+ $tag1 = rand_str();
+ $priority1 = rand( 1, 100 );
+ $tag2 = rand_str();
+ $priority2 = rand( 1, 100 );
+ $filters = array(
+ $tag1 => array(
+ $priority1 => array(
+ 'test1' => array(
+ 'function' => '__return_false',
+ 'accepted_args' => 2,
+ ),
+ ),
+ ),
+ $tag2 => array(
+ $priority2 => array(
+ 'test1' => array(
+ 'function' => '__return_null',
+ 'accepted_args' => 1,
+ ),
+ ),
+ ),
+ );
+
+ $hooks = WP_Hook::build_preinitialized_hooks( $filters );
+
+ $this->assertEquals( $priority1, $hooks[ $tag1 ]->has_filter( $tag1, '__return_false' ) );
+ $this->assertEquals( $priority2, $hooks[ $tag2 ]->has_filter( $tag2, '__return_null' ) );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/preinit_hooks.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshooksremove_all_filtersphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/remove_all_filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/remove_all_filters.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/remove_all_filters.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,41 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the remove_all_filters method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Remove_All_Filters extends WP_UnitTestCase {
+
+ public function test_remove_all_filters() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+
+ $hook->remove_all_filters();
+
+ $this->assertFalse( $hook->has_filters() );
+ }
+
+ public function test_remove_all_filters_with_priority() {
+ $callback_one = '__return_null';
+ $callback_two = '__return_false';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
+
+ $hook->remove_all_filters( $priority );
+
+ $this->assertFalse( $hook->has_filter( $tag, $callback_one ) );
+ $this->assertTrue( $hook->has_filters() );
+ $this->assertEquals( $priority + 1, $hook->has_filter( $tag, $callback_two ) );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/remove_all_filters.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestshooksremove_filterphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/hooks/remove_filter.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/remove_filter.php (rev 0)
+++ trunk/tests/phpunit/tests/hooks/remove_filter.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,81 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Test the remove_filter method of WP_Hook
+ *
+ * @group hooks
+ */
+class Tests_WP_Hook_Remove_Filter extends WP_UnitTestCase {
+
+ public function test_remove_filter_with_function() {
+ $callback = '__return_null';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->remove_filter( $tag, $callback, $priority );
+
+ $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
+ }
+
+ public function test_remove_filter_with_object() {
+ $a = new MockAction();
+ $callback = array( $a, 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->remove_filter( $tag, $callback, $priority );
+
+ $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
+ }
+
+ public function test_remove_filter_with_static_method() {
+ $callback = array( 'MockAction', 'action' );
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback, $priority, $accepted_args );
+ $hook->remove_filter( $tag, $callback, $priority );
+
+ $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
+ }
+
+ public function test_remove_filters_with_another_at_same_priority() {
+ $callback_one = '__return_null';
+ $callback_two = '__return_false';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );
+
+ $hook->remove_filter( $tag, $callback_one, $priority );
+
+ $this->assertCount( 1, $hook->callbacks[ $priority ] );
+ }
+
+ public function test_remove_filter_with_another_at_different_priority() {
+ $callback_one = '__return_null';
+ $callback_two = '__return_false';
+ $hook = new WP_Hook();
+ $tag = rand_str();
+ $priority = rand( 1, 100 );
+ $accepted_args = rand( 1, 100 );
+
+ $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
+ $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
+
+ $hook->remove_filter( $tag, $callback_one, $priority );
+ $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );
+ $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/hooks/remove_filter.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:executable</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+*
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsposttypesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/types.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/types.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/tests/phpunit/tests/post/types.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -446,7 +446,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 1, count( $wp_filter['future_foo'] ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( unregister_post_type( 'foo' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array(), $wp_filter['future_foo'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayNotHasKey( 'future_foo', $wp_filter );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -462,7 +462,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( unregister_post_type( 'foo' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array(), $wp_filter['add_meta_boxes_foo'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayNotHasKey( 'add_meta_boxes_foo', $wp_filter );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunitteststaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/taxonomy.php 2016-09-07 17:17:41 UTC (rev 38570)
+++ trunk/tests/phpunit/tests/taxonomy.php 2016-09-08 03:54:13 UTC (rev 38571)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -697,7 +697,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 1, count( $wp_filter['wp_ajax_add-foo'] ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( unregister_taxonomy( 'foo' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array(), $wp_filter['wp_ajax_add-foo'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayNotHasKey( 'wp_ajax_add-foo', $wp_filter );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre>
</div>
</div>
</body>
</html>