plugin.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. <?php
  2. /**
  3. * The plugin API is located in this file, which allows for creating actions
  4. * and filters and hooking functions, and methods. The functions or methods will
  5. * then be run when the action or filter is called.
  6. *
  7. * The API callback examples reference functions, but can be methods of classes.
  8. * To hook methods, you'll need to pass an array one of two ways.
  9. *
  10. * Any of the syntaxes explained in the PHP documentation for the
  11. * {@link https://www.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'}
  12. * type are valid.
  13. *
  14. * Also see the {@link https://developer.wordpress.org/plugins/ Plugin API} for
  15. * more information and examples on how to use a lot of these functions.
  16. *
  17. * This file should have no external dependencies.
  18. *
  19. * @package WordPress
  20. * @subpackage Plugin
  21. * @since 1.5.0
  22. */
  23. // Initialize the filter globals.
  24. require __DIR__ . '/class-wp-hook.php';
  25. /** @var WP_Hook[] $wp_filter */
  26. global $wp_filter;
  27. /** @var int[] $wp_actions */
  28. global $wp_actions;
  29. /** @var int[] $wp_filters */
  30. global $wp_filters;
  31. /** @var string[] $wp_current_filter */
  32. global $wp_current_filter;
  33. if ( $wp_filter ) {
  34. $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
  35. } else {
  36. $wp_filter = array();
  37. }
  38. if ( ! isset( $wp_actions ) ) {
  39. $wp_actions = array();
  40. }
  41. if ( ! isset( $wp_filters ) ) {
  42. $wp_filters = array();
  43. }
  44. if ( ! isset( $wp_current_filter ) ) {
  45. $wp_current_filter = array();
  46. }
  47. /**
  48. * Adds a callback function to a filter hook.
  49. *
  50. * WordPress offers filter hooks to allow plugins to modify
  51. * various types of internal data at runtime.
  52. *
  53. * A plugin can modify data by binding a callback to a filter hook. When the filter
  54. * is later applied, each bound callback is run in order of priority, and given
  55. * the opportunity to modify a value by returning a new value.
  56. *
  57. * The following example shows how a callback function is bound to a filter hook.
  58. *
  59. * Note that `$example` is passed to the callback, (maybe) modified, then returned:
  60. *
  61. * function example_callback( $example ) {
  62. * // Maybe modify $example in some way.
  63. * return $example;
  64. * }
  65. * add_filter( 'example_filter', 'example_callback' );
  66. *
  67. * Bound callbacks can accept from none to the total number of arguments passed as parameters
  68. * in the corresponding apply_filters() call.
  69. *
  70. * In other words, if an apply_filters() call passes four total arguments, callbacks bound to
  71. * it can accept none (the same as 1) of the arguments or up to four. The important part is that
  72. * the `$accepted_args` value must reflect the number of arguments the bound callback *actually*
  73. * opted to accept. If no arguments were accepted by the callback that is considered to be the
  74. * same as accepting 1 argument. For example:
  75. *
  76. * // Filter call.
  77. * $value = apply_filters( 'hook', $value, $arg2, $arg3 );
  78. *
  79. * // Accepting zero/one arguments.
  80. * function example_callback() {
  81. * ...
  82. * return 'some value';
  83. * }
  84. * add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.
  85. *
  86. * // Accepting two arguments (three possible).
  87. * function example_callback( $value, $arg2 ) {
  88. * ...
  89. * return $maybe_modified_value;
  90. * }
  91. * add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.
  92. *
  93. * *Note:* The function will return true whether or not the callback is valid.
  94. * It is up to you to take care. This is done for optimization purposes, so
  95. * everything is as quick as possible.
  96. *
  97. * @since 0.71
  98. *
  99. * @global WP_Hook[] $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
  100. *
  101. * @param string $hook_name The name of the filter to add the callback to.
  102. * @param callable $callback The callback to be run when the filter is applied.
  103. * @param int $priority Optional. Used to specify the order in which the functions
  104. * associated with a particular filter are executed.
  105. * Lower numbers correspond with earlier execution,
  106. * and functions with the same priority are executed
  107. * in the order in which they were added to the filter. Default 10.
  108. * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
  109. * @return true Always returns true.
  110. */
  111. function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
  112. global $wp_filter;
  113. if ( ! isset( $wp_filter[ $hook_name ] ) ) {
  114. $wp_filter[ $hook_name ] = new WP_Hook();
  115. }
  116. $wp_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args );
  117. return true;
  118. }
  119. /**
  120. * Calls the callback functions that have been added to a filter hook.
  121. *
  122. * This function invokes all functions attached to filter hook `$hook_name`.
  123. * It is possible to create new filter hooks by simply calling this function,
  124. * specifying the name of the new hook using the `$hook_name` parameter.
  125. *
  126. * The function also allows for multiple additional arguments to be passed to hooks.
  127. *
  128. * Example usage:
  129. *
  130. * // The filter callback function.
  131. * function example_callback( $string, $arg1, $arg2 ) {
  132. * // (maybe) modify $string.
  133. * return $string;
  134. * }
  135. * add_filter( 'example_filter', 'example_callback', 10, 3 );
  136. *
  137. * /*
  138. * * Apply the filters by calling the 'example_callback()' function
  139. * * that's hooked onto `example_filter` above.
  140. * *
  141. * * - 'example_filter' is the filter hook.
  142. * * - 'filter me' is the value being filtered.
  143. * * - $arg1 and $arg2 are the additional arguments passed to the callback.
  144. * $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
  145. *
  146. * @since 0.71
  147. * @since 6.0.0 Formalized the existing and already documented `...$args` parameter
  148. * by adding it to the function signature.
  149. *
  150. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  151. * @global int[] $wp_filters Stores the number of times each filter was triggered.
  152. * @global string[] $wp_current_filter Stores the list of current filters with the current one last.
  153. *
  154. * @param string $hook_name The name of the filter hook.
  155. * @param mixed $value The value to filter.
  156. * @param mixed ...$args Additional parameters to pass to the callback functions.
  157. * @return mixed The filtered value after all hooked functions are applied to it.
  158. */
  159. function apply_filters( $hook_name, $value, ...$args ) {
  160. global $wp_filter, $wp_filters, $wp_current_filter;
  161. if ( ! isset( $wp_filters[ $hook_name ] ) ) {
  162. $wp_filters[ $hook_name ] = 1;
  163. } else {
  164. ++$wp_filters[ $hook_name ];
  165. }
  166. // Do 'all' actions first.
  167. if ( isset( $wp_filter['all'] ) ) {
  168. $wp_current_filter[] = $hook_name;
  169. $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
  170. _wp_call_all_hook( $all_args );
  171. }
  172. if ( ! isset( $wp_filter[ $hook_name ] ) ) {
  173. if ( isset( $wp_filter['all'] ) ) {
  174. array_pop( $wp_current_filter );
  175. }
  176. return $value;
  177. }
  178. if ( ! isset( $wp_filter['all'] ) ) {
  179. $wp_current_filter[] = $hook_name;
  180. }
  181. // Pass the value to WP_Hook.
  182. array_unshift( $args, $value );
  183. $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
  184. array_pop( $wp_current_filter );
  185. return $filtered;
  186. }
  187. /**
  188. * Calls the callback functions that have been added to a filter hook, specifying arguments in an array.
  189. *
  190. * @since 3.0.0
  191. *
  192. * @see apply_filters() This function is identical, but the arguments passed to the
  193. * functions hooked to `$hook_name` are supplied using an array.
  194. *
  195. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  196. * @global int[] $wp_filters Stores the number of times each filter was triggered.
  197. * @global string[] $wp_current_filter Stores the list of current filters with the current one last.
  198. *
  199. * @param string $hook_name The name of the filter hook.
  200. * @param array $args The arguments supplied to the functions hooked to `$hook_name`.
  201. * @return mixed The filtered value after all hooked functions are applied to it.
  202. */
  203. function apply_filters_ref_array( $hook_name, $args ) {
  204. global $wp_filter, $wp_filters, $wp_current_filter;
  205. if ( ! isset( $wp_filters[ $hook_name ] ) ) {
  206. $wp_filters[ $hook_name ] = 1;
  207. } else {
  208. ++$wp_filters[ $hook_name ];
  209. }
  210. // Do 'all' actions first.
  211. if ( isset( $wp_filter['all'] ) ) {
  212. $wp_current_filter[] = $hook_name;
  213. $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
  214. _wp_call_all_hook( $all_args );
  215. }
  216. if ( ! isset( $wp_filter[ $hook_name ] ) ) {
  217. if ( isset( $wp_filter['all'] ) ) {
  218. array_pop( $wp_current_filter );
  219. }
  220. return $args[0];
  221. }
  222. if ( ! isset( $wp_filter['all'] ) ) {
  223. $wp_current_filter[] = $hook_name;
  224. }
  225. $filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args );
  226. array_pop( $wp_current_filter );
  227. return $filtered;
  228. }
  229. /**
  230. * Checks if any filter has been registered for a hook.
  231. *
  232. * When using the `$callback` argument, this function may return a non-boolean value
  233. * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
  234. *
  235. * @since 2.5.0
  236. *
  237. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  238. *
  239. * @param string $hook_name The name of the filter hook.
  240. * @param callable|string|array|false $callback Optional. The callback to check for.
  241. * This function can be called unconditionally to speculatively check
  242. * a callback that may or may not exist. Default false.
  243. * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
  244. * anything registered. When checking a specific function, the priority
  245. * of that hook is returned, or false if the function is not attached.
  246. */
  247. function has_filter( $hook_name, $callback = false ) {
  248. global $wp_filter;
  249. if ( ! isset( $wp_filter[ $hook_name ] ) ) {
  250. return false;
  251. }
  252. return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback );
  253. }
  254. /**
  255. * Removes a callback function from a filter hook.
  256. *
  257. * This can be used to remove default functions attached to a specific filter
  258. * hook and possibly replace them with a substitute.
  259. *
  260. * To remove a hook, the `$callback` and `$priority` arguments must match
  261. * when the hook was added. This goes for both filters and actions. No warning
  262. * will be given on removal failure.
  263. *
  264. * @since 1.2.0
  265. *
  266. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  267. *
  268. * @param string $hook_name The filter hook to which the function to be removed is hooked.
  269. * @param callable|string|array $callback The callback to be removed from running when the filter is applied.
  270. * This function can be called unconditionally to speculatively remove
  271. * a callback that may or may not exist.
  272. * @param int $priority Optional. The exact priority used when adding the original
  273. * filter callback. Default 10.
  274. * @return bool Whether the function existed before it was removed.
  275. */
  276. function remove_filter( $hook_name, $callback, $priority = 10 ) {
  277. global $wp_filter;
  278. $r = false;
  279. if ( isset( $wp_filter[ $hook_name ] ) ) {
  280. $r = $wp_filter[ $hook_name ]->remove_filter( $hook_name, $callback, $priority );
  281. if ( ! $wp_filter[ $hook_name ]->callbacks ) {
  282. unset( $wp_filter[ $hook_name ] );
  283. }
  284. }
  285. return $r;
  286. }
  287. /**
  288. * Removes all of the callback functions from a filter hook.
  289. *
  290. * @since 2.7.0
  291. *
  292. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  293. *
  294. * @param string $hook_name The filter to remove callbacks from.
  295. * @param int|false $priority Optional. The priority number to remove them from.
  296. * Default false.
  297. * @return true Always returns true.
  298. */
  299. function remove_all_filters( $hook_name, $priority = false ) {
  300. global $wp_filter;
  301. if ( isset( $wp_filter[ $hook_name ] ) ) {
  302. $wp_filter[ $hook_name ]->remove_all_filters( $priority );
  303. if ( ! $wp_filter[ $hook_name ]->has_filters() ) {
  304. unset( $wp_filter[ $hook_name ] );
  305. }
  306. }
  307. return true;
  308. }
  309. /**
  310. * Retrieves the name of the current filter hook.
  311. *
  312. * @since 2.5.0
  313. *
  314. * @global string[] $wp_current_filter Stores the list of current filters with the current one last
  315. *
  316. * @return string Hook name of the current filter.
  317. */
  318. function current_filter() {
  319. global $wp_current_filter;
  320. return end( $wp_current_filter );
  321. }
  322. /**
  323. * Returns whether or not a filter hook is currently being processed.
  324. *
  325. * The function current_filter() only returns the most recent filter being executed.
  326. * did_filter() returns the number of times a filter has been applied during
  327. * the current request.
  328. *
  329. * This function allows detection for any filter currently being executed
  330. * (regardless of whether it's the most recent filter to fire, in the case of
  331. * hooks called from hook callbacks) to be verified.
  332. *
  333. * @since 3.9.0
  334. *
  335. * @see current_filter()
  336. * @see did_filter()
  337. * @global string[] $wp_current_filter Current filter.
  338. *
  339. * @param string|null $hook_name Optional. Filter hook to check. Defaults to null,
  340. * which checks if any filter is currently being run.
  341. * @return bool Whether the filter is currently in the stack.
  342. */
  343. function doing_filter( $hook_name = null ) {
  344. global $wp_current_filter;
  345. if ( null === $hook_name ) {
  346. return ! empty( $wp_current_filter );
  347. }
  348. return in_array( $hook_name, $wp_current_filter, true );
  349. }
  350. /**
  351. * Retrieves the number of times a filter has been applied during the current request.
  352. *
  353. * @since 6.1.0
  354. *
  355. * @global int[] $wp_filters Stores the number of times each filter was triggered.
  356. *
  357. * @param string $hook_name The name of the filter hook.
  358. * @return int The number of times the filter hook has been applied.
  359. */
  360. function did_filter( $hook_name ) {
  361. global $wp_filters;
  362. if ( ! isset( $wp_filters[ $hook_name ] ) ) {
  363. return 0;
  364. }
  365. return $wp_filters[ $hook_name ];
  366. }
  367. /**
  368. * Adds a callback function to an action hook.
  369. *
  370. * Actions are the hooks that the WordPress core launches at specific points
  371. * during execution, or when specific events occur. Plugins can specify that
  372. * one or more of its PHP functions are executed at these points, using the
  373. * Action API.
  374. *
  375. * @since 1.2.0
  376. *
  377. * @param string $hook_name The name of the action to add the callback to.
  378. * @param callable $callback The callback to be run when the action is called.
  379. * @param int $priority Optional. Used to specify the order in which the functions
  380. * associated with a particular action are executed.
  381. * Lower numbers correspond with earlier execution,
  382. * and functions with the same priority are executed
  383. * in the order in which they were added to the action. Default 10.
  384. * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
  385. * @return true Always returns true.
  386. */
  387. function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
  388. return add_filter( $hook_name, $callback, $priority, $accepted_args );
  389. }
  390. /**
  391. * Calls the callback functions that have been added to an action hook.
  392. *
  393. * This function invokes all functions attached to action hook `$hook_name`.
  394. * It is possible to create new action hooks by simply calling this function,
  395. * specifying the name of the new hook using the `$hook_name` parameter.
  396. *
  397. * You can pass extra arguments to the hooks, much like you can with `apply_filters()`.
  398. *
  399. * Example usage:
  400. *
  401. * // The action callback function.
  402. * function example_callback( $arg1, $arg2 ) {
  403. * // (maybe) do something with the args.
  404. * }
  405. * add_action( 'example_action', 'example_callback', 10, 2 );
  406. *
  407. * /*
  408. * * Trigger the actions by calling the 'example_callback()' function
  409. * * that's hooked onto `example_action` above.
  410. * *
  411. * * - 'example_action' is the action hook.
  412. * * - $arg1 and $arg2 are the additional arguments passed to the callback.
  413. * do_action( 'example_action', $arg1, $arg2 );
  414. *
  415. * @since 1.2.0
  416. * @since 5.3.0 Formalized the existing and already documented `...$arg` parameter
  417. * by adding it to the function signature.
  418. *
  419. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  420. * @global int[] $wp_actions Stores the number of times each action was triggered.
  421. * @global string[] $wp_current_filter Stores the list of current filters with the current one last.
  422. *
  423. * @param string $hook_name The name of the action to be executed.
  424. * @param mixed ...$arg Optional. Additional arguments which are passed on to the
  425. * functions hooked to the action. Default empty.
  426. */
  427. function do_action( $hook_name, ...$arg ) {
  428. global $wp_filter, $wp_actions, $wp_current_filter;
  429. if ( ! isset( $wp_actions[ $hook_name ] ) ) {
  430. $wp_actions[ $hook_name ] = 1;
  431. } else {
  432. ++$wp_actions[ $hook_name ];
  433. }
  434. // Do 'all' actions first.
  435. if ( isset( $wp_filter['all'] ) ) {
  436. $wp_current_filter[] = $hook_name;
  437. $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
  438. _wp_call_all_hook( $all_args );
  439. }
  440. if ( ! isset( $wp_filter[ $hook_name ] ) ) {
  441. if ( isset( $wp_filter['all'] ) ) {
  442. array_pop( $wp_current_filter );
  443. }
  444. return;
  445. }
  446. if ( ! isset( $wp_filter['all'] ) ) {
  447. $wp_current_filter[] = $hook_name;
  448. }
  449. if ( empty( $arg ) ) {
  450. $arg[] = '';
  451. } elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) {
  452. // Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`.
  453. $arg[0] = $arg[0][0];
  454. }
  455. $wp_filter[ $hook_name ]->do_action( $arg );
  456. array_pop( $wp_current_filter );
  457. }
  458. /**
  459. * Calls the callback functions that have been added to an action hook, specifying arguments in an array.
  460. *
  461. * @since 2.1.0
  462. *
  463. * @see do_action() This function is identical, but the arguments passed to the
  464. * functions hooked to `$hook_name` are supplied using an array.
  465. *
  466. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  467. * @global int[] $wp_actions Stores the number of times each action was triggered.
  468. * @global string[] $wp_current_filter Stores the list of current filters with the current one last.
  469. *
  470. * @param string $hook_name The name of the action to be executed.
  471. * @param array $args The arguments supplied to the functions hooked to `$hook_name`.
  472. */
  473. function do_action_ref_array( $hook_name, $args ) {
  474. global $wp_filter, $wp_actions, $wp_current_filter;
  475. if ( ! isset( $wp_actions[ $hook_name ] ) ) {
  476. $wp_actions[ $hook_name ] = 1;
  477. } else {
  478. ++$wp_actions[ $hook_name ];
  479. }
  480. // Do 'all' actions first.
  481. if ( isset( $wp_filter['all'] ) ) {
  482. $wp_current_filter[] = $hook_name;
  483. $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
  484. _wp_call_all_hook( $all_args );
  485. }
  486. if ( ! isset( $wp_filter[ $hook_name ] ) ) {
  487. if ( isset( $wp_filter['all'] ) ) {
  488. array_pop( $wp_current_filter );
  489. }
  490. return;
  491. }
  492. if ( ! isset( $wp_filter['all'] ) ) {
  493. $wp_current_filter[] = $hook_name;
  494. }
  495. $wp_filter[ $hook_name ]->do_action( $args );
  496. array_pop( $wp_current_filter );
  497. }
  498. /**
  499. * Checks if any action has been registered for a hook.
  500. *
  501. * When using the `$callback` argument, this function may return a non-boolean value
  502. * that evaluates to false (e.g. 0), so use the `===` operator for testing the return value.
  503. *
  504. * @since 2.5.0
  505. *
  506. * @see has_filter() has_action() is an alias of has_filter().
  507. *
  508. * @param string $hook_name The name of the action hook.
  509. * @param callable|string|array|false $callback Optional. The callback to check for.
  510. * This function can be called unconditionally to speculatively check
  511. * a callback that may or may not exist. Default false.
  512. * @return bool|int If `$callback` is omitted, returns boolean for whether the hook has
  513. * anything registered. When checking a specific function, the priority
  514. * of that hook is returned, or false if the function is not attached.
  515. */
  516. function has_action( $hook_name, $callback = false ) {
  517. return has_filter( $hook_name, $callback );
  518. }
  519. /**
  520. * Removes a callback function from an action hook.
  521. *
  522. * This can be used to remove default functions attached to a specific action
  523. * hook and possibly replace them with a substitute.
  524. *
  525. * To remove a hook, the `$callback` and `$priority` arguments must match
  526. * when the hook was added. This goes for both filters and actions. No warning
  527. * will be given on removal failure.
  528. *
  529. * @since 1.2.0
  530. *
  531. * @param string $hook_name The action hook to which the function to be removed is hooked.
  532. * @param callable|string|array $callback The name of the function which should be removed.
  533. * This function can be called unconditionally to speculatively remove
  534. * a callback that may or may not exist.
  535. * @param int $priority Optional. The exact priority used when adding the original
  536. * action callback. Default 10.
  537. * @return bool Whether the function is removed.
  538. */
  539. function remove_action( $hook_name, $callback, $priority = 10 ) {
  540. return remove_filter( $hook_name, $callback, $priority );
  541. }
  542. /**
  543. * Removes all of the callback functions from an action hook.
  544. *
  545. * @since 2.7.0
  546. *
  547. * @param string $hook_name The action to remove callbacks from.
  548. * @param int|false $priority Optional. The priority number to remove them from.
  549. * Default false.
  550. * @return true Always returns true.
  551. */
  552. function remove_all_actions( $hook_name, $priority = false ) {
  553. return remove_all_filters( $hook_name, $priority );
  554. }
  555. /**
  556. * Retrieves the name of the current action hook.
  557. *
  558. * @since 3.9.0
  559. *
  560. * @return string Hook name of the current action.
  561. */
  562. function current_action() {
  563. return current_filter();
  564. }
  565. /**
  566. * Returns whether or not an action hook is currently being processed.
  567. *
  568. * The function current_action() only returns the most recent action being executed.
  569. * did_action() returns the number of times an action has been fired during
  570. * the current request.
  571. *
  572. * This function allows detection for any action currently being executed
  573. * (regardless of whether it's the most recent action to fire, in the case of
  574. * hooks called from hook callbacks) to be verified.
  575. *
  576. * @since 3.9.0
  577. *
  578. * @see current_action()
  579. * @see did_action()
  580. *
  581. * @param string|null $hook_name Optional. Action hook to check. Defaults to null,
  582. * which checks if any action is currently being run.
  583. * @return bool Whether the action is currently in the stack.
  584. */
  585. function doing_action( $hook_name = null ) {
  586. return doing_filter( $hook_name );
  587. }
  588. /**
  589. * Retrieves the number of times an action has been fired during the current request.
  590. *
  591. * @since 2.1.0
  592. *
  593. * @global int[] $wp_actions Stores the number of times each action was triggered.
  594. *
  595. * @param string $hook_name The name of the action hook.
  596. * @return int The number of times the action hook has been fired.
  597. */
  598. function did_action( $hook_name ) {
  599. global $wp_actions;
  600. if ( ! isset( $wp_actions[ $hook_name ] ) ) {
  601. return 0;
  602. }
  603. return $wp_actions[ $hook_name ];
  604. }
  605. /**
  606. * Fires functions attached to a deprecated filter hook.
  607. *
  608. * When a filter hook is deprecated, the apply_filters() call is replaced with
  609. * apply_filters_deprecated(), which triggers a deprecation notice and then fires
  610. * the original filter hook.
  611. *
  612. * Note: the value and extra arguments passed to the original apply_filters() call
  613. * must be passed here to `$args` as an array. For example:
  614. *
  615. * // Old filter.
  616. * return apply_filters( 'wpdocs_filter', $value, $extra_arg );
  617. *
  618. * // Deprecated.
  619. * return apply_filters_deprecated( 'wpdocs_filter', array( $value, $extra_arg ), '4.9.0', 'wpdocs_new_filter' );
  620. *
  621. * @since 4.6.0
  622. *
  623. * @see _deprecated_hook()
  624. *
  625. * @param string $hook_name The name of the filter hook.
  626. * @param array $args Array of additional function arguments to be passed to apply_filters().
  627. * @param string $version The version of WordPress that deprecated the hook.
  628. * @param string $replacement Optional. The hook that should have been used. Default empty.
  629. * @param string $message Optional. A message regarding the change. Default empty.
  630. */
  631. function apply_filters_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
  632. if ( ! has_filter( $hook_name ) ) {
  633. return $args[0];
  634. }
  635. _deprecated_hook( $hook_name, $version, $replacement, $message );
  636. return apply_filters_ref_array( $hook_name, $args );
  637. }
  638. /**
  639. * Fires functions attached to a deprecated action hook.
  640. *
  641. * When an action hook is deprecated, the do_action() call is replaced with
  642. * do_action_deprecated(), which triggers a deprecation notice and then fires
  643. * the original hook.
  644. *
  645. * @since 4.6.0
  646. *
  647. * @see _deprecated_hook()
  648. *
  649. * @param string $hook_name The name of the action hook.
  650. * @param array $args Array of additional function arguments to be passed to do_action().
  651. * @param string $version The version of WordPress that deprecated the hook.
  652. * @param string $replacement Optional. The hook that should have been used. Default empty.
  653. * @param string $message Optional. A message regarding the change. Default empty.
  654. */
  655. function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
  656. if ( ! has_action( $hook_name ) ) {
  657. return;
  658. }
  659. _deprecated_hook( $hook_name, $version, $replacement, $message );
  660. do_action_ref_array( $hook_name, $args );
  661. }
  662. //
  663. // Functions for handling plugins.
  664. //
  665. /**
  666. * Gets the basename of a plugin.
  667. *
  668. * This method extracts the name of a plugin from its filename.
  669. *
  670. * @since 1.5.0
  671. *
  672. * @global array $wp_plugin_paths
  673. *
  674. * @param string $file The filename of plugin.
  675. * @return string The name of a plugin.
  676. */
  677. function plugin_basename( $file ) {
  678. global $wp_plugin_paths;
  679. // $wp_plugin_paths contains normalized paths.
  680. $file = wp_normalize_path( $file );
  681. arsort( $wp_plugin_paths );
  682. foreach ( $wp_plugin_paths as $dir => $realdir ) {
  683. if ( strpos( $file, $realdir ) === 0 ) {
  684. $file = $dir . substr( $file, strlen( $realdir ) );
  685. }
  686. }
  687. $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
  688. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
  689. // Get relative path from plugins directory.
  690. $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file );
  691. $file = trim( $file, '/' );
  692. return $file;
  693. }
  694. /**
  695. * Register a plugin's real path.
  696. *
  697. * This is used in plugin_basename() to resolve symlinked paths.
  698. *
  699. * @since 3.9.0
  700. *
  701. * @see wp_normalize_path()
  702. *
  703. * @global array $wp_plugin_paths
  704. *
  705. * @param string $file Known path to the file.
  706. * @return bool Whether the path was able to be registered.
  707. */
  708. function wp_register_plugin_realpath( $file ) {
  709. global $wp_plugin_paths;
  710. // Normalize, but store as static to avoid recalculation of a constant value.
  711. static $wp_plugin_path = null, $wpmu_plugin_path = null;
  712. if ( ! isset( $wp_plugin_path ) ) {
  713. $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
  714. $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
  715. }
  716. $plugin_path = wp_normalize_path( dirname( $file ) );
  717. $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
  718. if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
  719. return false;
  720. }
  721. if ( $plugin_path !== $plugin_realpath ) {
  722. $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
  723. }
  724. return true;
  725. }
  726. /**
  727. * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
  728. *
  729. * @since 2.8.0
  730. *
  731. * @param string $file The filename of the plugin (__FILE__).
  732. * @return string the filesystem path of the directory that contains the plugin.
  733. */
  734. function plugin_dir_path( $file ) {
  735. return trailingslashit( dirname( $file ) );
  736. }
  737. /**
  738. * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
  739. *
  740. * @since 2.8.0
  741. *
  742. * @param string $file The filename of the plugin (__FILE__).
  743. * @return string the URL path of the directory that contains the plugin.
  744. */
  745. function plugin_dir_url( $file ) {
  746. return trailingslashit( plugins_url( '', $file ) );
  747. }
  748. /**
  749. * Set the activation hook for a plugin.
  750. *
  751. * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
  752. * called. In the name of this hook, PLUGINNAME is replaced with the name
  753. * of the plugin, including the optional subdirectory. For example, when the
  754. * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
  755. * the name of this hook will become 'activate_sampleplugin/sample.php'.
  756. *
  757. * When the plugin consists of only one file and is (as by default) located at
  758. * wp-content/plugins/sample.php the name of this hook will be
  759. * 'activate_sample.php'.
  760. *
  761. * @since 2.0.0
  762. *
  763. * @param string $file The filename of the plugin including the path.
  764. * @param callable $callback The function hooked to the 'activate_PLUGIN' action.
  765. */
  766. function register_activation_hook( $file, $callback ) {
  767. $file = plugin_basename( $file );
  768. add_action( 'activate_' . $file, $callback );
  769. }
  770. /**
  771. * Sets the deactivation hook for a plugin.
  772. *
  773. * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
  774. * called. In the name of this hook, PLUGINNAME is replaced with the name
  775. * of the plugin, including the optional subdirectory. For example, when the
  776. * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
  777. * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
  778. *
  779. * When the plugin consists of only one file and is (as by default) located at
  780. * wp-content/plugins/sample.php the name of this hook will be
  781. * 'deactivate_sample.php'.
  782. *
  783. * @since 2.0.0
  784. *
  785. * @param string $file The filename of the plugin including the path.
  786. * @param callable $callback The function hooked to the 'deactivate_PLUGIN' action.
  787. */
  788. function register_deactivation_hook( $file, $callback ) {
  789. $file = plugin_basename( $file );
  790. add_action( 'deactivate_' . $file, $callback );
  791. }
  792. /**
  793. * Sets the uninstallation hook for a plugin.
  794. *
  795. * Registers the uninstall hook that will be called when the user clicks on the
  796. * uninstall link that calls for the plugin to uninstall itself. The link won't
  797. * be active unless the plugin hooks into the action.
  798. *
  799. * The plugin should not run arbitrary code outside of functions, when
  800. * registering the uninstall hook. In order to run using the hook, the plugin
  801. * will have to be included, which means that any code laying outside of a
  802. * function will be run during the uninstallation process. The plugin should not
  803. * hinder the uninstallation process.
  804. *
  805. * If the plugin can not be written without running code within the plugin, then
  806. * the plugin should create a file named 'uninstall.php' in the base plugin
  807. * folder. This file will be called, if it exists, during the uninstallation process
  808. * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
  809. * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
  810. * executing.
  811. *
  812. * @since 2.7.0
  813. *
  814. * @param string $file Plugin file.
  815. * @param callable $callback The callback to run when the hook is called. Must be
  816. * a static method or function.
  817. */
  818. function register_uninstall_hook( $file, $callback ) {
  819. if ( is_array( $callback ) && is_object( $callback[0] ) ) {
  820. _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );
  821. return;
  822. }
  823. /*
  824. * The option should not be autoloaded, because it is not needed in most
  825. * cases. Emphasis should be put on using the 'uninstall.php' way of
  826. * uninstalling the plugin.
  827. */
  828. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
  829. $plugin_basename = plugin_basename( $file );
  830. if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) {
  831. $uninstallable_plugins[ $plugin_basename ] = $callback;
  832. update_option( 'uninstall_plugins', $uninstallable_plugins );
  833. }
  834. }
  835. /**
  836. * Calls the 'all' hook, which will process the functions hooked into it.
  837. *
  838. * The 'all' hook passes all of the arguments or parameters that were used for
  839. * the hook, which this function was called for.
  840. *
  841. * This function is used internally for apply_filters(), do_action(), and
  842. * do_action_ref_array() and is not meant to be used from outside those
  843. * functions. This function does not check for the existence of the all hook, so
  844. * it will fail unless the all hook exists prior to this function call.
  845. *
  846. * @since 2.5.0
  847. * @access private
  848. *
  849. * @global WP_Hook[] $wp_filter Stores all of the filters and actions.
  850. *
  851. * @param array $args The collected parameters from the hook that was called.
  852. */
  853. function _wp_call_all_hook( $args ) {
  854. global $wp_filter;
  855. $wp_filter['all']->do_all_hook( $args );
  856. }
  857. /**
  858. * Builds Unique ID for storage and retrieval.
  859. *
  860. * The old way to serialize the callback caused issues and this function is the
  861. * solution. It works by checking for objects and creating a new property in
  862. * the class to keep track of the object and new objects of the same class that
  863. * need to be added.
  864. *
  865. * It also allows for the removal of actions and filters for objects after they
  866. * change class properties. It is possible to include the property $wp_filter_id
  867. * in your class and set it to "null" or a number to bypass the workaround.
  868. * However this will prevent you from adding new classes and any new classes
  869. * will overwrite the previous hook by the same class.
  870. *
  871. * Functions and static method callbacks are just returned as strings and
  872. * shouldn't have any speed penalty.
  873. *
  874. * @link https://core.trac.wordpress.org/ticket/3875
  875. *
  876. * @since 2.2.3
  877. * @since 5.3.0 Removed workarounds for spl_object_hash().
  878. * `$hook_name` and `$priority` are no longer used,
  879. * and the function always returns a string.
  880. *
  881. * @access private
  882. *
  883. * @param string $hook_name Unused. The name of the filter to build ID for.
  884. * @param callable|string|array $callback The callback to generate ID for. The callback may
  885. * or may not exist.
  886. * @param int $priority Unused. The order in which the functions
  887. * associated with a particular action are executed.
  888. * @return string Unique function ID for usage as array key.
  889. */
  890. function _wp_filter_build_unique_id( $hook_name, $callback, $priority ) {
  891. if ( is_string( $callback ) ) {
  892. return $callback;
  893. }
  894. if ( is_object( $callback ) ) {
  895. // Closures are currently implemented as objects.
  896. $callback = array( $callback, '' );
  897. } else {
  898. $callback = (array) $callback;
  899. }
  900. if ( is_object( $callback[0] ) ) {
  901. // Object class calling.
  902. return spl_object_hash( $callback[0] ) . $callback[1];
  903. } elseif ( is_string( $callback[0] ) ) {
  904. // Static calling.
  905. return $callback[0] . '::' . $callback[1];
  906. }
  907. }