<!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>[56609] trunk: Plugins: Store result of call to array_keys, to save repeated calls in WP_Hook class.</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 { white-space: pre-line; 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/56609">56609</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/56609","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-09-18 12:39:18 +0000 (Mon, 18 Sep 2023)</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'>Plugins: Store result of call to array_keys, to save repeated calls in WP_Hook class.
In the `WP_Hook` class the function `array_keys` was called every time an array of hook priorities was needed. For sites with lots of filters or actions, this would result in thousands of calls to the `array_keys` function, which uses server resources. Instead of recomputing this array every time it is needed, only compute it when filters are added and removed, then store the result as a class property. Improve unit tests to ensure this behaviour is tested.
Props spacedmonkey, bor0, flixos90, hellofromTonya, mukesh27.
Fixes <a href="https://core.trac.wordpress.org/ticket/58458">#58458</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswphookphp">trunk/src/wp-includes/class-wp-hook.php</a></li>
<li><a href="#trunktestsphpunittestshooksaddFilterphp">trunk/tests/phpunit/tests/hooks/addFilter.php</a></li>
<li><a href="#trunktestsphpunittestshooksremoveAllFiltersphp">trunk/tests/phpunit/tests/hooks/removeAllFilters.php</a></li>
<li><a href="#trunktestsphpunittestshooksremoveFilterphp">trunk/tests/phpunit/tests/hooks/removeFilter.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswphookphp"></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/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 2023-09-18 10:55:35 UTC (rev 56608)
+++ trunk/src/wp-includes/class-wp-hook.php 2023-09-18 12:39:18 UTC (rev 56609)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,6 +27,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $callbacks = array();
</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">+ * Priorities list.
+ *
+ * @since 6.4.0
+ * @var array
+ */
+ protected $priorities = array();
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * The priority keys of actively running iterations of a hook.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -86,6 +94,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ksort( $this->callbacks, SORT_NUMERIC );
</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">+ $this->priorities = array_keys( $this->callbacks );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $this->nesting_level > 0 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->resort_active_iterations( $priority, $priority_existed );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -102,7 +112,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * filter was added. Default false.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $new_priorities = array_keys( $this->callbacks );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $new_priorities = $this->priorities;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If there are no remaining hooks, clear out all running iterations.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $new_priorities ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -187,6 +197,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $this->callbacks[ $priority ] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->priorities = array_keys( $this->callbacks );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $this->nesting_level > 0 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->resort_active_iterations();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -262,9 +274,11 @@
</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"> if ( false === $priority ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->callbacks = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->callbacks = array();
+ $this->priorities = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( isset( $this->callbacks[ $priority ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->callbacks[ $priority ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->priorities = array_keys( $this->callbacks );
</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"> if ( $this->nesting_level > 0 ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -289,7 +303,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $nesting_level = $this->nesting_level++;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->iterations[ $nesting_level ] = $this->priorities;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $num_args = count( $args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -348,7 +362,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function do_all_hook( &$args ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $nesting_level = $this->nesting_level++;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->iterations[ $nesting_level ] = $this->priorities;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> do {
</span><span class="cx" style="display: block; padding: 0 10px"> $priority = current( $this->iterations[ $nesting_level ] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,6 +495,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->callbacks[ $offset ] = $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $this->priorities = array_keys( $this->callbacks );
</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">@@ -495,6 +511,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> #[ReturnTypeWillChange]
</span><span class="cx" style="display: block; padding: 0 10px"> public function offsetUnset( $offset ) {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->callbacks[ $offset ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->priorities = array_keys( $this->callbacks );
</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="trunktestsphpunittestshooksaddFilterphp"></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/hooks/addFilter.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/addFilter.php 2023-09-18 10:55:35 UTC (rev 56608)
+++ trunk/tests/phpunit/tests/hooks/addFilter.php 2023-09-18 12:39:18 UTC (rev 56609)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,6 +35,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -50,6 +51,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -64,6 +66,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,6 +82,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback_two, $priority, $accepted_args );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -94,9 +98,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority + 1 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -109,6 +115,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -123,9 +130,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $accepted_args = 2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority + 1, $accepted_args );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority + 1 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -141,7 +150,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, array( $b, 'action' ), 5, 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, array( $c, 'action' ), 8, 1 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array( 5, 8, 10 ), array_keys( $hook->callbacks ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( array( 5, 8, 10 ), $this->get_priorities( $hook ) );
</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"> public function test_remove_and_add() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -148,13 +157,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook = new WP_Hook();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 10 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 11 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 12 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $value = $this->hook->apply_filters( '', array() );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( '24', $value );
</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">@@ -162,13 +173,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook = new WP_Hook();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 10 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 11 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 12, 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 12 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $value = $this->hook->apply_filters( '', array() );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( '12', $value );
</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">@@ -183,6 +196,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSameSets( array( 10, 11, 12 ), $this->get_priorities( $this->hook ), 'The priorities should match this array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $value = $this->hook->apply_filters( '', array() );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( '1-134-234', $value );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,7 +210,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function _filter_remove_and_add2( $value ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11 );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 11 );
</ins><span class="cx" style="display: block; padding: 0 10px"> return $value . '2';
</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">@@ -205,7 +220,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $value .= '-' . $this->hook->apply_filters( '', array() ) . '-';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $this->hook, 11 );
</ins><span class="cx" style="display: block; padding: 0 10px"> return $value . '2';
</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">@@ -291,4 +306,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function _action_remove_and_add4() {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->action_output .= '4';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ protected function check_priority_exists( $hook, $priority ) {
+ $priorities = $this->get_priorities( $hook );
+
+ $this->assertContains( $priority, $priorities );
+ }
+
+ protected function get_priorities( $hook ) {
+ $reflection = new ReflectionClass( $hook );
+ $reflection_property = $reflection->getProperty( 'priorities' );
+ $reflection_property->setAccessible( true );
+
+ return $reflection_property->getValue( $hook );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestshooksremoveAllFiltersphp"></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/hooks/removeAllFilters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/removeAllFilters.php 2023-09-18 10:55:35 UTC (rev 56608)
+++ trunk/tests/phpunit/tests/hooks/removeAllFilters.php 2023-09-18 12:39:18 UTC (rev 56609)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,6 +18,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_all_filters();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_non_existent( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $hook->has_filters() );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,9 +35,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_all_filters( $priority );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_non_existent( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $hook->has_filter( $hook_name, $callback_one ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( $hook->has_filters() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $priority + 1, $hook->has_filter( $hook_name, $callback_two ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority + 1 );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ protected function check_priority_non_existent( $hook, $priority ) {
+ $priorities = $this->get_priorities( $hook );
+
+ $this->assertNotContains( $priority, $priorities );
+ }
+
+ protected function check_priority_exists( $hook, $priority ) {
+ $priorities = $this->get_priorities( $hook );
+
+ $this->assertContains( $priority, $priorities );
+ }
+ protected function get_priorities( $hook ) {
+ $reflection = new ReflectionClass( $hook );
+ $reflection_property = $reflection->getProperty( 'priorities' );
+ $reflection_property->setAccessible( true );
+
+ return $reflection_property->getValue( $hook );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestshooksremoveFilterphp"></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/hooks/removeFilter.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/removeFilter.php 2023-09-18 10:55:35 UTC (rev 56608)
+++ trunk/tests/phpunit/tests/hooks/removeFilter.php 2023-09-18 12:39:18 UTC (rev 56609)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +17,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_filter( $hook_name, $callback, $priority );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_non_existent( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayNotHasKey( $priority, $hook->callbacks );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,6 +32,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_filter( $hook_name, $callback, $priority );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_non_existent( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayNotHasKey( $priority, $hook->callbacks );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -44,6 +46,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_filter( $hook_name, $callback, $priority );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_non_existent( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayNotHasKey( $priority, $hook->callbacks );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,6 +65,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_filter( $hook_name, $callback_one, $priority );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority, 'Has priority of 2' );
</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"> public function test_remove_filter_with_another_at_different_priority() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -76,7 +80,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $hook->remove_filter( $hook_name, $callback_one, $priority );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_non_existent( $hook, $priority );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayNotHasKey( $priority, $hook->callbacks );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->check_priority_exists( $hook, $priority + 1, 'Should priority of 3' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ protected function check_priority_non_existent( $hook, $priority ) {
+ $priorities = $this->get_priorities( $hook );
+
+ $this->assertNotContains( $priority, $priorities );
+ }
+
+ protected function check_priority_exists( $hook, $priority ) {
+ $priorities = $this->get_priorities( $hook );
+
+ $this->assertContains( $priority, $priorities );
+ }
+
+ protected function get_priorities( $hook ) {
+ $reflection = new ReflectionClass( $hook );
+ $reflection_property = $reflection->getProperty( 'priorities' );
+ $reflection_property->setAccessible( true );
+
+ return $reflection_property->getValue( $hook );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>