PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/plugin.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 931 lines | 271 code | 88 blank | 572 comment | 51 complexity | baa4281950e4073984b26f72994bd2cf MD5 | raw file
  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, $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.
  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. Default 10.
  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 and actions.
  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
  154. * * that's 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 and actions.
  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 and actions.
  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 and actions.
  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 and actions.
  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
  387. * * that's 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 and actions.
  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 and actions.
  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.0', '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 null.
  564. * @param string $message Optional. A message regarding the change. Default null.
  565. */
  566. function apply_filters_deprecated( $tag, $args, $version, $replacement = null, $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. Default null.
  588. * @param string $message Optional. A message regarding the change. Default null.
  589. */
  590. function do_action_deprecated( $tag, $args, $version, $replacement = null, $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. // Get relative path from plugins directory.
  625. $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file );
  626. $file = trim( $file, '/' );
  627. return $file;
  628. }
  629. /**
  630. * Register a plugin's real path.
  631. *
  632. * This is used in plugin_basename() to resolve symlinked paths.
  633. *
  634. * @since 3.9.0
  635. *
  636. * @see wp_normalize_path()
  637. *
  638. * @global array $wp_plugin_paths
  639. *
  640. * @staticvar string $wp_plugin_path
  641. * @staticvar string $wpmu_plugin_path
  642. *
  643. * @param string $file Known path to the file.
  644. * @return bool Whether the path was able to be registered.
  645. */
  646. function wp_register_plugin_realpath( $file ) {
  647. global $wp_plugin_paths;
  648. // Normalize, but store as static to avoid recalculation of a constant value.
  649. static $wp_plugin_path = null, $wpmu_plugin_path = null;
  650. if ( ! isset( $wp_plugin_path ) ) {
  651. $wp_plugin_path = wp_normalize_path( WP_PLUGIN_DIR );
  652. $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR );
  653. }
  654. $plugin_path = wp_normalize_path( dirname( $file ) );
  655. $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
  656. if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) {
  657. return false;
  658. }
  659. if ( $plugin_path !== $plugin_realpath ) {
  660. $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
  661. }
  662. return true;
  663. }
  664. /**
  665. * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
  666. *
  667. * @since 2.8.0
  668. *
  669. * @param string $file The filename of the plugin (__FILE__).
  670. * @return string the filesystem path of the directory that contains the plugin.
  671. */
  672. function plugin_dir_path( $file ) {
  673. return trailingslashit( dirname( $file ) );
  674. }
  675. /**
  676. * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
  677. *
  678. * @since 2.8.0
  679. *
  680. * @param string $file The filename of the plugin (__FILE__).
  681. * @return string the URL path of the directory that contains the plugin.
  682. */
  683. function plugin_dir_url( $file ) {
  684. return trailingslashit( plugins_url( '', $file ) );
  685. }
  686. /**
  687. * Set the activation hook for a plugin.
  688. *
  689. * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
  690. * called. In the name of this hook, PLUGINNAME is replaced with the name
  691. * of the plugin, including the optional subdirectory. For example, when the
  692. * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
  693. * the name of this hook will become 'activate_sampleplugin/sample.php'.
  694. *
  695. * When the plugin consists of only one file and is (as by default) located at
  696. * wp-content/plugins/sample.php the name of this hook will be
  697. * 'activate_sample.php'.
  698. *
  699. * @since 2.0.0
  700. *
  701. * @param string $file The filename of the plugin including the path.
  702. * @param callable $function The function hooked to the 'activate_PLUGIN' action.
  703. */
  704. function register_activation_hook( $file, $function ) {
  705. $file = plugin_basename( $file );
  706. add_action( 'activate_' . $file, $function );
  707. }
  708. /**
  709. * Set the deactivation hook for a plugin.
  710. *
  711. * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
  712. * called. In the name of this hook, PLUGINNAME is replaced with the name
  713. * of the plugin, including the optional subdirectory. For example, when the
  714. * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
  715. * the name of this hook will become 'deactivate_sampleplugin/sample.php'.
  716. *
  717. * When the plugin consists of only one file and is (as by default) located at
  718. * wp-content/plugins/sample.php the name of this hook will be
  719. * 'deactivate_sample.php'.
  720. *
  721. * @since 2.0.0
  722. *
  723. * @param string $file The filename of the plugin including the path.
  724. * @param callable $function The function hooked to the 'deactivate_PLUGIN' action.
  725. */
  726. function register_deactivation_hook( $file, $function ) {
  727. $file = plugin_basename( $file );
  728. add_action( 'deactivate_' . $file, $function );
  729. }
  730. /**
  731. * Set the uninstallation hook for a plugin.
  732. *
  733. * Registers the uninstall hook that will be called when the user clicks on the
  734. * uninstall link that calls for the plugin to uninstall itself. The link won't
  735. * be active unless the plugin hooks into the action.
  736. *
  737. * The plugin should not run arbitrary code outside of functions, when
  738. * registering the uninstall hook. In order to run using the hook, the plugin
  739. * will have to be included, which means that any code laying outside of a
  740. * function will be run during the uninstallation process. The plugin should not
  741. * hinder the uninstallation process.
  742. *
  743. * If the plugin can not be written without running code within the plugin, then
  744. * the plugin should create a file named 'uninstall.php' in the base plugin
  745. * folder. This file will be called, if it exists, during the uninstallation process
  746. * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
  747. * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
  748. * executing.
  749. *
  750. * @since 2.7.0
  751. *
  752. * @param string $file Plugin file.
  753. * @param callable $callback The callback to run when the hook is called. Must be
  754. * a static method or function.
  755. */
  756. function register_uninstall_hook( $file, $callback ) {
  757. if ( is_array( $callback ) && is_object( $callback[0] ) ) {
  758. _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1.0' );
  759. return;
  760. }
  761. /*
  762. * The option should not be autoloaded, because it is not needed in most
  763. * cases. Emphasis should be put on using the 'uninstall.php' way of
  764. * uninstalling the plugin.
  765. */
  766. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
  767. $plugin_basename = plugin_basename( $file );
  768. if ( ! isset( $uninstallable_plugins[ $plugin_basename ] ) || $uninstallable_plugins[ $plugin_basename ] !== $callback ) {
  769. $uninstallable_plugins[ $plugin_basename ] = $callback;
  770. update_option( 'uninstall_plugins', $uninstallable_plugins );
  771. }
  772. }
  773. /**
  774. * Call the 'all' hook, which will process the functions hooked into it.
  775. *
  776. * The 'all' hook passes all of the arguments or parameters that were used for
  777. * the hook, which this function was called for.
  778. *
  779. * This function is used internally for apply_filters(), do_action(), and
  780. * do_action_ref_array() and is not meant to be used from outside those
  781. * functions. This function does not check for the existence of the all hook, so
  782. * it will fail unless the all hook exists prior to this function call.
  783. *
  784. * @since 2.5.0
  785. * @access private
  786. *
  787. * @global array $wp_filter Stores all of the filters and actions.
  788. *
  789. * @param array $args The collected parameters from the hook that was called.
  790. */
  791. function _wp_call_all_hook( $args ) {
  792. global $wp_filter;
  793. $wp_filter['all']->do_all_hook( $args );
  794. }
  795. /**
  796. * Build Unique ID for storage and retrieval.
  797. *
  798. * The old way to serialize the callback caused issues and this function is the
  799. * solution. It works by checking for objects and creating a new property in
  800. * the class to keep track of the object and new objects of the same class that
  801. * need to be added.
  802. *
  803. * It also allows for the removal of actions and filters for objects after they
  804. * change class properties. It is possible to include the property $wp_filter_id
  805. * in your class and set it to "null" or a number to bypass the workaround.
  806. * However this will prevent you from adding new classes and any new classes
  807. * will overwrite the previous hook by the same class.
  808. *
  809. * Functions and static method callbacks are just returned as strings and
  810. * shouldn't have any speed penalty.
  811. *
  812. * @link https://core.trac.wordpress.org/ticket/3875
  813. *
  814. * @since 2.2.3
  815. * @since 5.3.0 Removed workarounds for spl_object_hash().
  816. * `$tag` and `$priority` are no longer used,
  817. * and the function always returns a string.
  818. * @access private
  819. *
  820. * @param string $tag Unused. The name of the filter to build ID for.
  821. * @param callable $function The function to generate ID for.
  822. * @param int $priority Unused. The order in which the functions
  823. * associated with a particular action are executed.
  824. * @return string Unique function ID for usage as array key.
  825. */
  826. function _wp_filter_build_unique_id( $tag, $function, $priority ) {
  827. if ( is_string( $function ) ) {
  828. return $function;
  829. }
  830. if ( is_object( $function ) ) {
  831. // Closures are currently implemented as objects.
  832. $function = array( $function, '' );
  833. } else {
  834. $function = (array) $function;
  835. }
  836. if ( is_object( $function[0] ) ) {
  837. // Object class calling.
  838. return spl_object_hash( $function[0] ) . $function[1];
  839. } elseif ( is_string( $function[0] ) ) {
  840. // Static calling.
  841. return $function[0] . '::' . $function[1];
  842. }
  843. }