暫無描述

plugin.php 34KB

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