PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/plugin.php

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