wp/wp-includes/class-wp-hook.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
--- a/wp/wp-includes/class-wp-hook.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/class-wp-hook.php	Fri Sep 05 18:40:08 2025 +0200
@@ -15,6 +15,7 @@
  * @see Iterator
  * @see ArrayAccess
  */
+#[AllowDynamicProperties]
 final class WP_Hook implements Iterator, ArrayAccess {
 
 	/**
@@ -26,6 +27,14 @@
 	public $callbacks = array();
 
 	/**
+	 * Priorities list.
+	 *
+	 * @since 6.4.0
+	 * @var array
+	 */
+	protected $priorities = array();
+
+	/**
 	 * The priority keys of actively running iterations of a hook.
 	 *
 	 * @since 4.7.0
@@ -77,7 +86,7 @@
 
 		$this->callbacks[ $priority ][ $idx ] = array(
 			'function'      => $callback,
-			'accepted_args' => $accepted_args,
+			'accepted_args' => (int) $accepted_args,
 		);
 
 		// If we're adding a new priority to the list, put them back in sorted order.
@@ -85,6 +94,8 @@
 			ksort( $this->callbacks, SORT_NUMERIC );
 		}
 
+		$this->priorities = array_keys( $this->callbacks );
+
 		if ( $this->nesting_level > 0 ) {
 			$this->resort_active_iterations( $priority, $priority_existed );
 		}
@@ -101,7 +112,7 @@
 	 *                                    filter was added. Default false.
 	 */
 	private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
-		$new_priorities = array_keys( $this->callbacks );
+		$new_priorities = $this->priorities;
 
 		// If there are no remaining hooks, clear out all running iterations.
 		if ( ! $new_priorities ) {
@@ -186,6 +197,8 @@
 			if ( ! $this->callbacks[ $priority ] ) {
 				unset( $this->callbacks[ $priority ] );
 
+				$this->priorities = array_keys( $this->callbacks );
+
 				if ( $this->nesting_level > 0 ) {
 					$this->resort_active_iterations();
 				}
@@ -261,9 +274,11 @@
 		}
 
 		if ( false === $priority ) {
-			$this->callbacks = array();
+			$this->callbacks  = array();
+			$this->priorities = array();
 		} elseif ( isset( $this->callbacks[ $priority ] ) ) {
 			unset( $this->callbacks[ $priority ] );
+			$this->priorities = array_keys( $this->callbacks );
 		}
 
 		if ( $this->nesting_level > 0 ) {
@@ -288,12 +303,14 @@
 
 		$nesting_level = $this->nesting_level++;
 
-		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
-		$num_args                           = count( $args );
+		$this->iterations[ $nesting_level ] = $this->priorities;
+
+		$num_args = count( $args );
 
 		do {
 			$this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
-			$priority                                 = $this->current_priority[ $nesting_level ];
+
+			$priority = $this->current_priority[ $nesting_level ];
 
 			foreach ( $this->callbacks[ $priority ] as $the_ ) {
 				if ( ! $this->doing_action ) {
@@ -301,12 +318,12 @@
 				}
 
 				// Avoid the array_slice() if possible.
-				if ( 0 == $the_['accepted_args'] ) {
+				if ( 0 === $the_['accepted_args'] ) {
 					$value = call_user_func( $the_['function'] );
 				} 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'] ) );
+					$value = call_user_func_array( $the_['function'], array_slice( $args, 0, $the_['accepted_args'] ) );
 				}
 			}
 		} while ( false !== next( $this->iterations[ $nesting_level ] ) );
@@ -314,7 +331,7 @@
 		unset( $this->iterations[ $nesting_level ] );
 		unset( $this->current_priority[ $nesting_level ] );
 
-		$this->nesting_level--;
+		--$this->nesting_level;
 
 		return $value;
 	}
@@ -345,7 +362,7 @@
 	 */
 	public function do_all_hook( &$args ) {
 		$nesting_level                      = $this->nesting_level++;
-		$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
+		$this->iterations[ $nesting_level ] = $this->priorities;
 
 		do {
 			$priority = current( $this->iterations[ $nesting_level ] );
@@ -356,7 +373,7 @@
 		} while ( false !== next( $this->iterations[ $nesting_level ] ) );
 
 		unset( $this->iterations[ $nesting_level ] );
-		$this->nesting_level--;
+		--$this->nesting_level;
 	}
 
 	/**
@@ -409,7 +426,7 @@
 		$normalized = array();
 
 		foreach ( $filters as $hook_name => $callback_groups ) {
-			if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
+			if ( $callback_groups instanceof WP_Hook ) {
 				$normalized[ $hook_name ] = $callback_groups;
 				continue;
 			}
@@ -478,6 +495,8 @@
 		} else {
 			$this->callbacks[ $offset ] = $value;
 		}
+
+		$this->priorities = array_keys( $this->callbacks );
 	}
 
 	/**
@@ -492,6 +511,7 @@
 	#[ReturnTypeWillChange]
 	public function offsetUnset( $offset ) {
 		unset( $this->callbacks[ $offset ] );
+		$this->priorities = array_keys( $this->callbacks );
 	}
 
 	/**
@@ -561,5 +581,4 @@
 	public function rewind() {
 		reset( $this->callbacks );
 	}
-
 }