Açıklama Yok

class-wp-hook.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 currently doing an action, rather than a filter.
  48. *
  49. * @since 4.7.0
  50. * @var bool
  51. */
  52. private $doing_action = false;
  53. /**
  54. * Adds a callback function to a filter hook.
  55. *
  56. * @since 4.7.0
  57. *
  58. * @param string $hook_name The name of the filter to add the callback to.
  59. * @param callable $callback The callback to be run when the filter is applied.
  60. * @param int $priority The order in which the functions associated with a particular filter
  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 filter.
  64. * @param int $accepted_args The number of arguments the function accepts.
  65. */
  66. public function add_filter( $hook_name, $callback, $priority, $accepted_args ) {
  67. $idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
  68. $priority_existed = isset( $this->callbacks[ $priority ] );
  69. $this->callbacks[ $priority ][ $idx ] = array(
  70. 'function' => $callback,
  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 false|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. * Removes a callback function from a filter hook.
  143. *
  144. * @since 4.7.0
  145. *
  146. * @param string $hook_name The filter hook to which the function to be removed is hooked.
  147. * @param callable $callback 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( $hook_name, $callback, $priority ) {
  152. $function_key = _wp_filter_build_unique_id( $hook_name, $callback, $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 callback has been registered for this hook.
  167. *
  168. * When using the `$callback` argument, this function may return a non-boolean value
  169. * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
  170. *
  171. * @since 4.7.0
  172. *
  173. * @param string $hook_name Optional. The name of the filter hook. Default empty.
  174. * @param callable|false $callback Optional. The callback to check for. Default false.
  175. * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
  176. * anything registered. When checking a specific function, the priority
  177. * of that hook is returned, or false if the function is not attached.
  178. */
  179. public function has_filter( $hook_name = '', $callback = false ) {
  180. if ( false === $callback ) {
  181. return $this->has_filters();
  182. }
  183. $function_key = _wp_filter_build_unique_id( $hook_name, $callback, false );
  184. if ( ! $function_key ) {
  185. return false;
  186. }
  187. foreach ( $this->callbacks as $priority => $callbacks ) {
  188. if ( isset( $callbacks[ $function_key ] ) ) {
  189. return $priority;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Checks if any callbacks have been registered for this hook.
  196. *
  197. * @since 4.7.0
  198. *
  199. * @return bool True if callbacks have been registered for the current hook, otherwise false.
  200. */
  201. public function has_filters() {
  202. foreach ( $this->callbacks as $callbacks ) {
  203. if ( $callbacks ) {
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. /**
  210. * Removes all callbacks from the current filter.
  211. *
  212. * @since 4.7.0
  213. *
  214. * @param int|false $priority Optional. The priority number to remove. Default false.
  215. */
  216. public function remove_all_filters( $priority = false ) {
  217. if ( ! $this->callbacks ) {
  218. return;
  219. }
  220. if ( false === $priority ) {
  221. $this->callbacks = array();
  222. } elseif ( isset( $this->callbacks[ $priority ] ) ) {
  223. unset( $this->callbacks[ $priority ] );
  224. }
  225. if ( $this->nesting_level > 0 ) {
  226. $this->resort_active_iterations();
  227. }
  228. }
  229. /**
  230. * Calls the callback functions that have been added to a filter hook.
  231. *
  232. * @since 4.7.0
  233. *
  234. * @param mixed $value The value to filter.
  235. * @param array $args Additional parameters to pass to the callback functions.
  236. * This array is expected to include $value at index 0.
  237. * @return mixed The filtered value after all hooked functions are applied to it.
  238. */
  239. public function apply_filters( $value, $args ) {
  240. if ( ! $this->callbacks ) {
  241. return $value;
  242. }
  243. $nesting_level = $this->nesting_level++;
  244. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  245. $num_args = count( $args );
  246. do {
  247. $this->current_priority[ $nesting_level ] = current( $this->iterations[ $nesting_level ] );
  248. $priority = $this->current_priority[ $nesting_level ];
  249. foreach ( $this->callbacks[ $priority ] as $the_ ) {
  250. if ( ! $this->doing_action ) {
  251. $args[0] = $value;
  252. }
  253. // Avoid the array_slice() if possible.
  254. if ( 0 == $the_['accepted_args'] ) {
  255. $value = call_user_func( $the_['function'] );
  256. } elseif ( $the_['accepted_args'] >= $num_args ) {
  257. $value = call_user_func_array( $the_['function'], $args );
  258. } else {
  259. $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
  260. }
  261. }
  262. } while ( false !== next( $this->iterations[ $nesting_level ] ) );
  263. unset( $this->iterations[ $nesting_level ] );
  264. unset( $this->current_priority[ $nesting_level ] );
  265. $this->nesting_level--;
  266. return $value;
  267. }
  268. /**
  269. * Calls the callback functions that have been added to an action hook.
  270. *
  271. * @since 4.7.0
  272. *
  273. * @param array $args Parameters to pass to the callback functions.
  274. */
  275. public function do_action( $args ) {
  276. $this->doing_action = true;
  277. $this->apply_filters( '', $args );
  278. // If there are recursive calls to the current action, we haven't finished it until we get to the last one.
  279. if ( ! $this->nesting_level ) {
  280. $this->doing_action = false;
  281. }
  282. }
  283. /**
  284. * Processes the functions hooked into the 'all' hook.
  285. *
  286. * @since 4.7.0
  287. *
  288. * @param array $args Arguments to pass to the hook callbacks. Passed by reference.
  289. */
  290. public function do_all_hook( &$args ) {
  291. $nesting_level = $this->nesting_level++;
  292. $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
  293. do {
  294. $priority = current( $this->iterations[ $nesting_level ] );
  295. foreach ( $this->callbacks[ $priority ] as $the_ ) {
  296. call_user_func_array( $the_['function'], $args );
  297. }
  298. } while ( false !== next( $this->iterations[ $nesting_level ] ) );
  299. unset( $this->iterations[ $nesting_level ] );
  300. $this->nesting_level--;
  301. }
  302. /**
  303. * Return the current priority level of the currently running iteration of the hook.
  304. *
  305. * @since 4.7.0
  306. *
  307. * @return int|false If the hook is running, return the current priority level.
  308. * If it isn't running, return false.
  309. */
  310. public function current_priority() {
  311. if ( false === current( $this->iterations ) ) {
  312. return false;
  313. }
  314. return current( current( $this->iterations ) );
  315. }
  316. /**
  317. * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
  318. *
  319. * The `$filters` parameter should be an array keyed by hook name, with values
  320. * containing either:
  321. *
  322. * - A `WP_Hook` instance
  323. * - An array of callbacks keyed by their priorities
  324. *
  325. * Examples:
  326. *
  327. * $filters = array(
  328. * 'wp_fatal_error_handler_enabled' => array(
  329. * 10 => array(
  330. * array(
  331. * 'accepted_args' => 0,
  332. * 'function' => function() {
  333. * return false;
  334. * },
  335. * ),
  336. * ),
  337. * ),
  338. * );
  339. *
  340. * @since 4.7.0
  341. *
  342. * @param array $filters Filters to normalize. See documentation above for details.
  343. * @return WP_Hook[] Array of normalized filters.
  344. */
  345. public static function build_preinitialized_hooks( $filters ) {
  346. /** @var WP_Hook[] $normalized */
  347. $normalized = array();
  348. foreach ( $filters as $hook_name => $callback_groups ) {
  349. if ( is_object( $callback_groups ) && $callback_groups instanceof WP_Hook ) {
  350. $normalized[ $hook_name ] = $callback_groups;
  351. continue;
  352. }
  353. $hook = new WP_Hook();
  354. // Loop through callback groups.
  355. foreach ( $callback_groups as $priority => $callbacks ) {
  356. // Loop through callbacks.
  357. foreach ( $callbacks as $cb ) {
  358. $hook->add_filter( $hook_name, $cb['function'], $priority, $cb['accepted_args'] );
  359. }
  360. }
  361. $normalized[ $hook_name ] = $hook;
  362. }
  363. return $normalized;
  364. }
  365. /**
  366. * Determines whether an offset value exists.
  367. *
  368. * @since 4.7.0
  369. *
  370. * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
  371. *
  372. * @param mixed $offset An offset to check for.
  373. * @return bool True if the offset exists, false otherwise.
  374. */
  375. public function offsetExists( $offset ) {
  376. return isset( $this->callbacks[ $offset ] );
  377. }
  378. /**
  379. * Retrieves a value at a specified offset.
  380. *
  381. * @since 4.7.0
  382. *
  383. * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
  384. *
  385. * @param mixed $offset The offset to retrieve.
  386. * @return mixed If set, the value at the specified offset, null otherwise.
  387. */
  388. public function offsetGet( $offset ) {
  389. return isset( $this->callbacks[ $offset ] ) ? $this->callbacks[ $offset ] : null;
  390. }
  391. /**
  392. * Sets a value at a specified offset.
  393. *
  394. * @since 4.7.0
  395. *
  396. * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
  397. *
  398. * @param mixed $offset The offset to assign the value to.
  399. * @param mixed $value The value to set.
  400. */
  401. public function offsetSet( $offset, $value ) {
  402. if ( is_null( $offset ) ) {
  403. $this->callbacks[] = $value;
  404. } else {
  405. $this->callbacks[ $offset ] = $value;
  406. }
  407. }
  408. /**
  409. * Unsets a specified offset.
  410. *
  411. * @since 4.7.0
  412. *
  413. * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
  414. *
  415. * @param mixed $offset The offset to unset.
  416. */
  417. public function offsetUnset( $offset ) {
  418. unset( $this->callbacks[ $offset ] );
  419. }
  420. /**
  421. * Returns the current element.
  422. *
  423. * @since 4.7.0
  424. *
  425. * @link https://www.php.net/manual/en/iterator.current.php
  426. *
  427. * @return array Of callbacks at current priority.
  428. */
  429. public function current() {
  430. return current( $this->callbacks );
  431. }
  432. /**
  433. * Moves forward to the next element.
  434. *
  435. * @since 4.7.0
  436. *
  437. * @link https://www.php.net/manual/en/iterator.next.php
  438. *
  439. * @return array Of callbacks at next priority.
  440. */
  441. public function next() {
  442. return next( $this->callbacks );
  443. }
  444. /**
  445. * Returns the key of the current element.
  446. *
  447. * @since 4.7.0
  448. *
  449. * @link https://www.php.net/manual/en/iterator.key.php
  450. *
  451. * @return mixed Returns current priority on success, or NULL on failure
  452. */
  453. public function key() {
  454. return key( $this->callbacks );
  455. }
  456. /**
  457. * Checks if current position is valid.
  458. *
  459. * @since 4.7.0
  460. *
  461. * @link https://www.php.net/manual/en/iterator.valid.php
  462. *
  463. * @return bool Whether the current position is valid.
  464. */
  465. public function valid() {
  466. return key( $this->callbacks ) !== null;
  467. }
  468. /**
  469. * Rewinds the Iterator to the first element.
  470. *
  471. * @since 4.7.0
  472. *
  473. * @link https://www.php.net/manual/en/iterator.rewind.php
  474. */
  475. public function rewind() {
  476. reset( $this->callbacks );
  477. }
  478. }