PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/plugin.php

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