class-wp-hook.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. * Plugin API: WP_Hook class
  4. *
  5. * @package WordPress
  6. * @subpackage Plugin
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core class used to implement action and filter hook functionality.
  11. *
  12. * @since 4.7.0
  13. *
  14. * @see Iterator
  15. * @see ArrayAccess
  16. */
  17. final class WP_Hook implements Iterator, ArrayAccess {
  18. /**
  19. * Hook callbacks.
  20. *
  21. * @since 4.7.0
  22. * @var array
  23. */
  24. public $callbacks = array();
  25. /**
  26. * The priority keys of actively running iterations of a hook.
  27. *
  28. * @since 4.7.0
  29. * @var array
  30. */
  31. private $iterations = array();
  32. /**
  33. * The current priority of actively running iterations of a hook.
  34. *
  35. * @since 4.7.0
  36. * @var array
  37. */
  38. private $current_priority = array();
  39. /**
  40. * Number of levels this hook can be recursively called.
  41. *
  42. * @since 4.7.0
  43. * @var int
  44. */
  45. private $nesting_level = 0;
  46. /**
  47. * Flag for if we're current doing an action, rather than a filter.
  48. *
  49. * @since 4.7.0
  50. * @var bool
  51. */
  52. private $doing_action = false;
  53. /**
  54. * Hooks a function or method to a specific filter action.
  55. *
  56. * @since 4.7.0
  57. *
  58. * @param string $tag The name of the filter to hook the $function_to_add callback to.
  59. * @param callable $function_to_add The callback to be run when the filter is applied.
  60. * @param int $priority The order in which the functions associated with a
  61. * particular action are executed. Lower numbers correspond with
  62. * earlier execution, and functions with the same priority are executed
  63. * in the order in which they were added to the action.
  64. * @param int $accepted_args The number of arguments the function accepts.
  65. */
  66. public function add_filter( $tag, $function_to_add, $priority, $accepted_args ) {
  67. $idx = _wp_filter_build_unique_id( $tag, $function_to_add, $priority );
  68. $priority_existed = isset( $this->callbacks[ $priority ] );
  69. $this->callbacks[ $priority ][ $idx ] = array(
  70. 'function' => $function_to_add,
  71. 'accepted_args' => $accepted_args,
  72. );
  73. // if we're adding a new priority to the list, put them back in sorted order
  74. if ( ! $priority_existed && count( $this->callbacks ) > 1 ) {
  75. ksort( $this->callbacks, SORT_NUMERIC );
  76. }
  77. if ( $this->nesting_level > 0 ) {
  78. $this->resort_active_iterations( $priority, $priority_existed );
  79. }
  80. }
  81. /**
  82. * Handles resetting callback priority keys mid-iteration.
  83. *
  84. * @since 4.7.0
  85. *
  86. * @param bool|int $new_priority Optional. The priority of the new filter being added. Default false,
  87. * for no priority being added.
  88. * @param bool $priority_existed Optional. Flag for whether the priority already existed before the new
  89. * filter was added. Default false.
  90. */
  91. private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
  92. $new_priorities = array_keys( $this->callbacks );
  93. // If there are no remaining hooks, clear out all running iterations.
  94. if ( ! $new_priorities ) {
  95. foreach ( $this->iterations as $index => $iteration ) {
  96. $this->iterations[ $index ] = $new_priorities;
  97. }
  98. return;
  99. }
  100. $min = min( $new_priorities );
  101. foreach ( $this->iterations as $index => &$iteration ) {
  102. $current = current( $iteration );
  103. // If we're already at the end of this iteration, just leave the array pointer where it is.
  104. if ( false === $current ) {
  105. continue;
  106. }
  107. $iteration = $new_priorities;
  108. if ( $current < $min ) {
  109. array_unshift( $iteration, $current );
  110. continue;
  111. }
  112. while ( current( $iteration ) < $current ) {
  113. if ( false === next( $iteration ) ) {
  114. break;
  115. }
  116. }
  117. // If we have a new priority that didn't exist, but ::apply_filters() or ::do_action() thinks it's the current priority...
  118. if ( $new_priority === $this->current_priority[ $index ] && ! $priority_existed ) {
  119. /*
  120. * ... and the new priority is the same as what $this->iterations thinks is the previous
  121. * priority, we need to move back to it.
  122. */
  123. if ( false === current( $iteration ) ) {
  124. // If we've already moved off the end of the array, go back to the last element.
  125. $prev = end( $iteration );
  126. } else {
  127. // Otherwise, just go back to the previous element.
  128. $prev = prev( $iteration );
  129. }
  130. if ( false === $prev ) {
  131. // Start of the array. Reset, and go about our day.
  132. reset( $iteration );
  133. } elseif ( $new_priority !== $prev ) {
  134. // Previous wasn't the same. Move forward again.
  135. next( $iteration );
  136. }
  137. }
  138. }
  139. unset( $iteration );
  140. }
  141. /**
  142. * Unhooks a function or method from a specific filter action.
  143. *
  144. * @since 4.7.0
  145. *
  146. * @param string $tag The filter hook to which the function to be removed is hooked. Used
  147. * for building the callback ID when SPL is not available.
  148. * @param callable $function_to_remove The callback to be removed from running when the filter is applied.
  149. * @param int $priority The exact priority used when adding the original filter callback.
  150. * @return bool Whether the callback existed before it was removed.
  151. */
  152. public function remove_filter( $tag, $function_to_remove, $priority ) {
  153. $function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
  154. $exists = isset( $this->callbacks[ $priority ][ $function_key ] );
  155. if ( $exists ) {
  156. unset( $this->callbacks[ $priority ][ $function_key ] );
  157. if ( ! $this->callbacks[ $priority ] ) {
  158. unset( $this->callbacks[ $priority ] );
  159. if ( $this->nesting_level > 0 ) {
  160. $this->resort_active_iterations();
  161. }
  162. }
  163. }
  164. return $exists;
  165. }
  166. /**
  167. * Checks if a specific action has been registered for this hook.
  168. *
  169. * @since 4.7.0
  170. *
  171. * @param string $tag Optional. The name of the filter hook. Used for building
  172. * the callback ID when SPL is not available. Default empty.
  173. * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
  174. * @return bool|int The priority of that hook is returned, or false if the function is not attached.
  175. */
  176. public function has_filter( $tag = '', $function_to_check = false ) {
  177. if ( false === $function_to_check ) {
  178. return $this->has_filters();
  179. }
  180. $function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false );
  181. if ( ! $function_key ) {
  182. return false;
  183. }
  184. foreach ( $this->callbacks as $priority => $callbacks ) {
  185. if ( isset( $callbacks[ $function_key ] ) ) {
  186. return $priority;
  187. }
  188. }
  189. return false;
  190. }
  191. /**
  192. * Checks if any callbacks have been registered for this hook.
  193. *
  194. * @since 4.7.0
  195. *
  196. * @return bool True if callbacks have been registered for the current hook, otherwise false.
  197. */
  198. public function has_filters() {
  199. foreach ( $this->callbacks as $callbacks ) {
  200. if ( $callbacks ) {
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. /**
  207. * Removes all callbacks from the current filter.
  208. *
  209. * @since 4.7.0
  210. *
  211. * @param int|bool $priority Optional. The priority number to remove. Default false.
  212. */
  213. public function remove_all_filters( $priority = false ) {
  214. if ( ! $this->callbacks ) {
  215. return;
  216. }
  217. if ( false === $priority ) {
  218. $this->callbacks = array();
  219. } elseif ( isset( $this->callbacks[ $priority ] ) ) {
  220. unset( $this->callbacks[ $priority ] );
  221. }
  222. if ( $this->nesting_level > 0 ) {
  223. $this->resort_active_iterations();
  224. }
  225. }
  226. /**
  227. * Calls the callback functions that have been added to a filter hook.
  228. *
  229. * @since 4.7.0
  230. *
  231. * @param mixed $value The value to filter.
  232. * @param array $args Additional parameters to pass to the callback functions.
  233. * This array is expected to include $value at index 0.
  234. * @return mixed The filtered value after all hooked functions are applied to it.
  235. */
  236. public function apply_filters( $value, $args ) {
  237. if ( ! $this->callbacks ) {
  238. return $value;
  239. }
  240. $nesting_level = $this->nesting_level++;
  241. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  242. $num_args = count( $args );
  243. do {
  244. $this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
  245. $priority = $this->current_priority[ $nesting_level ];
  246. foreach ( $this->callbacks[ $priority ] as $the_ ) {
  247. if ( ! $this->doing_action ) {
  248. $args[0] = $value;
  249. }
  250. // Avoid the array_slice if possible.
  251. if ( $the_['accepted_args'] == 0 ) {
  252. $value = call_user_func( $the_['function'] );
  253. } elseif ( $the_['accepted_args'] >= $num_args ) {
  254. $value = call_user_func_array( $the_['function'], $args );
  255. } else {
  256. $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
  257. }
  258. }
  259. } while ( false !== next( $this->iterations[ $nesting_level ] ) );
  260. unset( $this->iterations[ $nesting_level ] );
  261. unset( $this->current_priority[ $nesting_level ] );
  262. $this->nesting_level--;
  263. return $value;
  264. }
  265. /**
  266. * Calls the callback functions that have been added to an action hook.
  267. *
  268. * @since 4.7.0
  269. *
  270. * @param array $args Parameters to pass to the callback functions.
  271. */
  272. public function do_action( $args ) {
  273. $this->doing_action = true;
  274. $this->apply_filters( '', $args );
  275. // If there are recursive calls to the current action, we haven't finished it until we get to the last one.
  276. if ( ! $this->nesting_level ) {
  277. $this->doing_action = false;
  278. }
  279. }
  280. /**
  281. * Processes the functions hooked into the 'all' hook.
  282. *
  283. * @since 4.7.0
  284. *
  285. * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
  286. */
  287. public function do_all_hook( &$args ) {
  288. $nesting_level = $this->nesting_level++;
  289. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  290. do {
  291. $priority = current( $this->iterations[ $nesting_level ] );
  292. foreach ( $this->callbacks[ $priority ] as $the_ ) {
  293. call_user_func_array( $the_['function'], $args );
  294. }
  295. } while ( false !== next( $this->iterations[ $nesting_level ] ) );
  296. unset( $this->iterations[ $nesting_level ] );
  297. $this->nesting_level--;
  298. }
  299. /**
  300. * Return the current priority level of the currently running iteration of the hook.
  301. *
  302. * @since 4.7.0
  303. *
  304. * @return int|false If the hook is running, return the current priority level. If it isn't running, return false.
  305. */
  306. public function current_priority() {
  307. if ( false === current( $this->iterations ) ) {
  308. return false;
  309. }
  310. return current( current( $this->iterations ) );
  311. }
  312. /**
  313. * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
  314. *
  315. * @since 4.7.0
  316. *
  317. * @param array $filters Filters to normalize.
  318. * @return WP_Hook[] Array of normalized filters.
  319. */
  320. public static function build_preinitialized_hooks( $filters ) {
  321. /** @var WP_Hook[] $normalized */
  322. $normalized = array();
  323. foreach ( $filters as $tag => $callback_groups ) {
  324. if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
  325. $normalized[ $tag ] = $callback_groups;
  326. continue;
  327. }
  328. $hook = new WP_Hook();
  329. // Loop through callback groups.
  330. foreach ( $callback_groups as $priority => $callbacks ) {
  331. // Loop through callbacks.
  332. foreach ( $callbacks as $cb ) {
  333. $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
  334. }
  335. }
  336. $normalized[ $tag ] = $hook;
  337. }
  338. return $normalized;
  339. }
  340. /**
  341. * Determines whether an offset value exists.
  342. *
  343. * @since 4.7.0
  344. *
  345. * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
  346. *
  347. * @param mixed $offset An offset to check for.
  348. * @return bool True if the offset exists, false otherwise.
  349. */
  350. public function offsetExists( $offset ) {
  351. return isset( $this->callbacks[ $offset ] );
  352. }
  353. /**
  354. * Retrieves a value at a specified offset.
  355. *
  356. * @since 4.7.0
  357. *
  358. * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
  359. *
  360. * @param mixed $offset The offset to retrieve.
  361. * @return mixed If set, the value at the specified offset, null otherwise.
  362. */
  363. public function offsetGet( $offset ) {
  364. return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
  365. }
  366. /**
  367. * Sets a value at a specified offset.
  368. *
  369. * @since 4.7.0
  370. *
  371. * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
  372. *
  373. * @param mixed $offset The offset to assign the value to.
  374. * @param mixed $value The value to set.
  375. */
  376. public function offsetSet( $offset, $value ) {
  377. if ( is_null( $offset ) ) {
  378. $this->callbacks[] = $value;
  379. } else {
  380. $this->callbacks[ $offset ] = $value;
  381. }
  382. }
  383. /**
  384. * Unsets a specified offset.
  385. *
  386. * @since 4.7.0
  387. *
  388. * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
  389. *
  390. * @param mixed $offset The offset to unset.
  391. */
  392. public function offsetUnset( $offset ) {
  393. unset( $this->callbacks[ $offset ] );
  394. }
  395. /**
  396. * Returns the current element.
  397. *
  398. * @since 4.7.0
  399. *
  400. * @link https://secure.php.net/manual/en/iterator.current.php
  401. *
  402. * @return array Of callbacks at current priority.
  403. */
  404. public function current() {
  405. return current( $this->callbacks );
  406. }
  407. /**
  408. * Moves forward to the next element.
  409. *
  410. * @since 4.7.0
  411. *
  412. * @link https://secure.php.net/manual/en/iterator.next.php
  413. *
  414. * @return array Of callbacks at next priority.
  415. */
  416. public function next() {
  417. return next( $this->callbacks );
  418. }
  419. /**
  420. * Returns the key of the current element.
  421. *
  422. * @since 4.7.0
  423. *
  424. * @link https://secure.php.net/manual/en/iterator.key.php
  425. *
  426. * @return mixed Returns current priority on success, or NULL on failure
  427. */
  428. public function key() {
  429. return key( $this->callbacks );
  430. }
  431. /**
  432. * Checks if current position is valid.
  433. *
  434. * @since 4.7.0
  435. *
  436. * @link https://secure.php.net/manual/en/iterator.valid.php
  437. *
  438. * @return boolean
  439. */
  440. public function valid() {
  441. return key( $this->callbacks ) !== null;
  442. }
  443. /**
  444. * Rewinds the Iterator to the first element.
  445. *
  446. * @since 4.7.0
  447. *
  448. * @link https://secure.php.net/manual/en/iterator.rewind.php
  449. */
  450. public function rewind() {
  451. reset( $this->callbacks );
  452. }
  453. }