PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/WPonEB/WPonEB
PHP | 510 lines | 211 code | 58 blank | 241 comment | 38 complexity | ab7c1eac9f4a1faf66083ecc7e91562d 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
  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 reseting 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. } else if ( 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 added to a filter hook.
  228. *
  229. * @since 4.7.0
  230. *
  231. * @param mixed $value The value to filter.
  232. * @param array $args Arguments to pass to callbacks.
  233. * @return mixed The filtered value after all hooked functions are applied to it.
  234. */
  235. public function apply_filters( $value, $args ) {
  236. if ( ! $this->callbacks ) {
  237. return $value;
  238. }
  239. $nesting_level = $this->nesting_level++;
  240. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  241. $num_args = count( $args );
  242. do {
  243. $this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $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 ( $the_['accepted_args'] == 0 ) {
  250. $value = call_user_func_array( $the_['function'], array() );
  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. * Executes the callback functions hooked on a specific action hook.
  265. *
  266. * @since 4.7.0
  267. *
  268. * @param mixed $args Arguments to pass to the hook callbacks.
  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. * @static
  315. *
  316. * @param array $filters Filters to normalize.
  317. * @return WP_Hook[] Array of normalized filters.
  318. */
  319. public static function build_preinitialized_hooks( $filters ) {
  320. /** @var WP_Hook[] $normalized */
  321. $normalized = array();
  322. foreach ( $filters as $tag => $callback_groups ) {
  323. if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
  324. $normalized[ $tag ] = $callback_groups;
  325. continue;
  326. }
  327. $hook = new WP_Hook();
  328. // Loop through callback groups.
  329. foreach ( $callback_groups as $priority => $callbacks ) {
  330. // Loop through callbacks.
  331. foreach ( $callbacks as $cb ) {
  332. $hook->add_filter( $tag, $cb['function'], $priority, $cb['accepted_args'] );
  333. }
  334. }
  335. $normalized[ $tag ] = $hook;
  336. }
  337. return $normalized;
  338. }
  339. /**
  340. * Determines whether an offset value exists.
  341. *
  342. * @since 4.7.0
  343. *
  344. * @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
  345. *
  346. * @param mixed $offset An offset to check for.
  347. * @return bool True if the offset exists, false otherwise.
  348. */
  349. public function offsetExists( $offset ) {
  350. return isset( $this->callbacks[ $offset ] );
  351. }
  352. /**
  353. * Retrieves a value at a specified offset.
  354. *
  355. * @since 4.7.0
  356. *
  357. * @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
  358. *
  359. * @param mixed $offset The offset to retrieve.
  360. * @return mixed If set, the value at the specified offset, null otherwise.
  361. */
  362. public function offsetGet( $offset ) {
  363. return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
  364. }
  365. /**
  366. * Sets a value at a specified offset.
  367. *
  368. * @since 4.7.0
  369. *
  370. * @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
  371. *
  372. * @param mixed $offset The offset to assign the value to.
  373. * @param mixed $value The value to set.
  374. */
  375. public function offsetSet( $offset, $value ) {
  376. if ( is_null( $offset ) ) {
  377. $this->callbacks[] = $value;
  378. } else {
  379. $this->callbacks[ $offset ] = $value;
  380. }
  381. }
  382. /**
  383. * Unsets a specified offset.
  384. *
  385. * @since 4.7.0
  386. *
  387. * @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
  388. *
  389. * @param mixed $offset The offset to unset.
  390. */
  391. public function offsetUnset( $offset ) {
  392. unset( $this->callbacks[ $offset ] );
  393. }
  394. /**
  395. * Returns the current element.
  396. *
  397. * @since 4.7.0
  398. *
  399. * @link https://secure.php.net/manual/en/iterator.current.php
  400. *
  401. * @return array Of callbacks at current priority.
  402. */
  403. public function current() {
  404. return current( $this->callbacks );
  405. }
  406. /**
  407. * Moves forward to the next element.
  408. *
  409. * @since 4.7.0
  410. *
  411. * @link https://secure.php.net/manual/en/iterator.next.php
  412. *
  413. * @return array Of callbacks at next priority.
  414. */
  415. public function next() {
  416. return next( $this->callbacks );
  417. }
  418. /**
  419. * Returns the key of the current element.
  420. *
  421. * @since 4.7.0
  422. *
  423. * @link https://secure.php.net/manual/en/iterator.key.php
  424. *
  425. * @return mixed Returns current priority on success, or NULL on failure
  426. */
  427. public function key() {
  428. return key( $this->callbacks );
  429. }
  430. /**
  431. * Checks if current position is valid.
  432. *
  433. * @since 4.7.0
  434. *
  435. * @link https://secure.php.net/manual/en/iterator.valid.php
  436. *
  437. * @return boolean
  438. */
  439. public function valid() {
  440. return key( $this->callbacks ) !== null;
  441. }
  442. /**
  443. * Rewinds the Iterator to the first element.
  444. *
  445. * @since 4.7.0
  446. *
  447. * @link https://secure.php.net/manual/en/iterator.rewind.php
  448. */
  449. public function rewind() {
  450. reset( $this->callbacks );
  451. }
  452. }