You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

911 lines
30 KiB

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