plugin.php 32 KB

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