Nenhuma Descrição

plugin.php 32KB

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