PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/class-wp-hook.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 510 lines | 212 code | 59 blank | 239 comment | 36 complexity | 4e709a7d66e0bc2482b80813df086cb2 MD5 | raw file
  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 particular action
  61. * are executed. Lower numbers correspond with earlier execution,
  62. * and functions with the same priority are executed in the order
  63. * 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.
  147. * @param callable $function_to_remove The callback to be removed from running when the filter is applied.
  148. * @param int $priority The exact priority used when adding the original filter callback.
  149. * @return bool Whether the callback existed before it was removed.
  150. */
  151. public function remove_filter( $tag, $function_to_remove, $priority ) {
  152. $function_key = _wp_filter_build_unique_id( $tag, $function_to_remove, $priority );
  153. $exists = isset( $this->callbacks[ $priority ][ $function_key ] );
  154. if ( $exists ) {
  155. unset( $this->callbacks[ $priority ][ $function_key ] );
  156. if ( ! $this->callbacks[ $priority ] ) {
  157. unset( $this->callbacks[ $priority ] );
  158. if ( $this->nesting_level > 0 ) {
  159. $this->resort_active_iterations();
  160. }
  161. }
  162. }
  163. return $exists;
  164. }
  165. /**
  166. * Checks if a specific action has been registered for this hook.
  167. *
  168. * @since 4.7.0
  169. *
  170. * @param string $tag Optional. The name of the filter hook. Default empty.
  171. * @param callable|bool $function_to_check Optional. The callback to check for. Default false.
  172. * @return bool|int The priority of that hook is returned, or false if the function is not attached.
  173. */
  174. public function has_filter( $tag = '', $function_to_check = false ) {
  175. if ( false === $function_to_check ) {
  176. return $this->has_filters();
  177. }
  178. $function_key = _wp_filter_build_unique_id( $tag, $function_to_check, false );
  179. if ( ! $function_key ) {
  180. return false;
  181. }
  182. foreach ( $this->callbacks as $priority => $callbacks ) {
  183. if ( isset( $callbacks[ $function_key ] ) ) {
  184. return $priority;
  185. }
  186. }
  187. return false;
  188. }
  189. /**
  190. * Checks if any callbacks have been registered for this hook.
  191. *
  192. * @since 4.7.0
  193. *
  194. * @return bool True if callbacks have been registered for the current hook, otherwise false.
  195. */
  196. public function has_filters() {
  197. foreach ( $this->callbacks as $callbacks ) {
  198. if ( $callbacks ) {
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. /**
  205. * Removes all callbacks from the current filter.
  206. *
  207. * @since 4.7.0
  208. *
  209. * @param int|bool $priority Optional. The priority number to remove. Default false.
  210. */
  211. public function remove_all_filters( $priority = false ) {
  212. if ( ! $this->callbacks ) {
  213. return;
  214. }
  215. if ( false === $priority ) {
  216. $this->callbacks = array();
  217. } elseif ( isset( $this->callbacks[ $priority ] ) ) {
  218. unset( $this->callbacks[ $priority ] );
  219. }
  220. if ( $this->nesting_level > 0 ) {
  221. $this->resort_active_iterations();
  222. }
  223. }
  224. /**
  225. * Calls the callback functions that have been added to a filter hook.
  226. *
  227. * @since 4.7.0
  228. *
  229. * @param mixed $value The value to filter.
  230. * @param array $args Additional parameters to pass to the callback functions.
  231. * This array is expected to include $value at index 0.
  232. * @return mixed The filtered value after all hooked functions are applied to it.
  233. */
  234. public function apply_filters( $value, $args ) {
  235. if ( ! $this->callbacks ) {
  236. return $value;
  237. }
  238. $nesting_level = $this->nesting_level++;
  239. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  240. $num_args = count( $args );
  241. do {
  242. $this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
  243. $priority = $this->current_priority[ $nesting_level ];
  244. foreach ( $this->callbacks[ $priority ] as $the_ ) {
  245. if ( ! $this->doing_action ) {
  246. $args[0] = $value;
  247. }
  248. // Avoid the array_slice() if possible.
  249. if ( 0 == $the_['accepted_args'] ) {
  250. $value = call_user_func( $the_['function'] );
  251. } elseif ( $the_['accepted_args'] >= $num_args ) {
  252. $value = call_user_func_array( $the_['function'], $args );
  253. } else {
  254. $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
  255. }
  256. }
  257. } while ( false !== next( $this->iterations[ $nesting_level ] ) );
  258. unset( $this->iterations[ $nesting_level ] );
  259. unset( $this->current_priority[ $nesting_level ] );
  260. $this->nesting_level--;
  261. return $value;
  262. }
  263. /**
  264. * Calls the callback functions that have been added to an action hook.
  265. *
  266. * @since 4.7.0
  267. *
  268. * @param array $args Parameters to pass to the callback functions.
  269. */
  270. public function do_action( $args ) {
  271. $this->doing_action = true;
  272. $this->apply_filters( '', $args );
  273. // If there are recursive calls to the current action, we haven't finished it until we get to the last one.
  274. if ( ! $this->nesting_level ) {
  275. $this->doing_action = false;
  276. }
  277. }
  278. /**
  279. * Processes the functions hooked into the 'all' hook.
  280. *
  281. * @since 4.7.0
  282. *
  283. * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
  284. */
  285. public function do_all_hook( &$args ) {
  286. $nesting_level = $this->nesting_level++;
  287. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  288. do {
  289. $priority = current( $this->iterations[ $nesting_level ] );
  290. foreach ( $this->callbacks[ $priority ] as $the_ ) {
  291. call_user_func_array( $the_['function'], $args );
  292. }
  293. } while ( false !== next( $this->iterations[ $nesting_level ] ) );
  294. unset( $this->iterations[ $nesting_level ] );
  295. $this->nesting_level--;
  296. }
  297. /**
  298. * Return the current priority level of the currently running iteration of the hook.
  299. *
  300. * @since 4.7.0
  301. *
  302. * @return int|false If the hook is running, return the current priority level. If it isn't running, return false.
  303. */
  304. public function current_priority() {
  305. if ( false === current( $this->iterations ) ) {
  306. return false;
  307. }
  308. return current( current( $this->iterations ) );
  309. }
  310. /**
  311. * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
  312. *
  313. * @since 4.7.0
  314. *
  315. * @param array $filters Filters to normalize.
  316. * @return WP_Hook[] Array of normalized filters.
  317. */
  318. public static function build_preinitialized_hooks( $filters ) {
  319. /** @var WP_Hook[] $normalized */
  320. $normalized = array();
  321. foreach ( $filters as $tag => $callback_groups ) {
  322. if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
  323. $normalized[ $tag ] = $callback_groups;
  324. continue;
  325. }
  326. $hook = new WP_Hook();
  327. // Loop through callback groups.
  328. foreach ( $callback_groups as $priority => $callbacks ) {
  329. // Loop through callbacks.
  330. foreach ( $callbacks as $cb ) {
  331. $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
  332. }
  333. }
  334. $normalized[ $tag ] = $hook;
  335. }
  336. return $normalized;
  337. }
  338. /**
  339. * Determines whether an offset value exists.
  340. *
  341. * @since 4.7.0
  342. *
  343. * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
  344. *
  345. * @param mixed $offset An offset to check for.
  346. * @return bool True if the offset exists, false otherwise.
  347. */
  348. public function offsetExists( $offset ) {
  349. return isset( $this->callbacks[ $offset ] );
  350. }
  351. /**
  352. * Retrieves a value at a specified offset.
  353. *
  354. * @since 4.7.0
  355. *
  356. * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
  357. *
  358. * @param mixed $offset The offset to retrieve.
  359. * @return mixed If set, the value at the specified offset, null otherwise.
  360. */
  361. public function offsetGet( $offset ) {
  362. return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
  363. }
  364. /**
  365. * Sets a value at a specified offset.
  366. *
  367. * @since 4.7.0
  368. *
  369. * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
  370. *
  371. * @param mixed $offset The offset to assign the value to.
  372. * @param mixed $value The value to set.
  373. */
  374. public function offsetSet( $offset, $value ) {
  375. if ( is_null( $offset ) ) {
  376. $this->callbacks[] = $value;
  377. } else {
  378. $this->callbacks[ $offset ] = $value;
  379. }
  380. }
  381. /**
  382. * Unsets a specified offset.
  383. *
  384. * @since 4.7.0
  385. *
  386. * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
  387. *
  388. * @param mixed $offset The offset to unset.
  389. */
  390. public function offsetUnset( $offset ) {
  391. unset( $this->callbacks[ $offset ] );
  392. }
  393. /**
  394. * Returns the current element.
  395. *
  396. * @since 4.7.0
  397. *
  398. * @link https://www.php.net/manual/en/iterator.current.php
  399. *
  400. * @return array Of callbacks at current priority.
  401. */
  402. public function current() {
  403. return current( $this->callbacks );
  404. }
  405. /**
  406. * Moves forward to the next element.
  407. *
  408. * @since 4.7.0
  409. *
  410. * @link https://www.php.net/manual/en/iterator.next.php
  411. *
  412. * @return array Of callbacks at next priority.
  413. */
  414. public function next() {
  415. return next( $this->callbacks );
  416. }
  417. /**
  418. * Returns the key of the current element.
  419. *
  420. * @since 4.7.0
  421. *
  422. * @link https://www.php.net/manual/en/iterator.key.php
  423. *
  424. * @return mixed Returns current priority on success, or NULL on failure
  425. */
  426. public function key() {
  427. return key( $this->callbacks );
  428. }
  429. /**
  430. * Checks if current position is valid.
  431. *
  432. * @since 4.7.0
  433. *
  434. * @link https://www.php.net/manual/en/iterator.valid.php
  435. *
  436. * @return boolean
  437. */
  438. public function valid() {
  439. return key( $this->callbacks ) !== null;
  440. }
  441. /**
  442. * Rewinds the Iterator to the first element.
  443. *
  444. * @since 4.7.0
  445. *
  446. * @link https://www.php.net/manual/en/iterator.rewind.php
  447. */
  448. public function rewind() {
  449. reset( $this->callbacks );
  450. }
  451. }