PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/plugin.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 788 lines | 270 code | 87 blank | 431 comment | 67 complexity | ebf7e01c08ed49a2fd54537d30cd8fc7 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.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
  20. */
  21. /**
  22. * Hooks a function or method to a specific filter action.
  23. *
  24. * Filters are the hooks that WordPress launches to modify text of various types
  25. * before adding it to the database or sending it to the browser screen. Plugins
  26. * can specify that one or more of its PHP functions is executed to
  27. * modify specific types of text at these times, using the Filter API.
  28. *
  29. * To use the API, the following code should be used to bind a callback to the
  30. * filter.
  31. *
  32. * <code>
  33. * function example_hook($example) { echo $example; }
  34. * add_filter('example_filter', 'example_hook');
  35. * </code>
  36. *
  37. * In WordPress 1.5.1+, hooked functions can take extra arguments that are set
  38. * when the matching do_action() or apply_filters() call is run. The
  39. * $accepted_args allow for calling functions only when the number of args
  40. * match. Hooked functions can take extra arguments that are set when the
  41. * matching do_action() or apply_filters() call is run. For example, the action
  42. * comment_id_not_found will pass any functions that hook onto it the ID of the
  43. * requested comment.
  44. *
  45. * <strong>Note:</strong> the function will return true no matter if the
  46. * function was hooked fails or not. There are no checks for whether the
  47. * function exists beforehand and no checks to whether the <tt>$function_to_add
  48. * is even a string. It is up to you to take care and this is done for
  49. * optimization purposes, so everything is as quick as possible.
  50. *
  51. * @package WordPress
  52. * @subpackage Plugin
  53. * @since 0.71
  54. * @global array $wp_filter Stores all of the filters added in the form of
  55. * wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)']']
  56. * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, it doesn't need to run through that process.
  57. *
  58. * @param string $tag The name of the filter to hook the $function_to_add to.
  59. * @param callback $function_to_add The name of the function to be called when the filter is applied.
  60. * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
  61. * @param int $accepted_args optional. The number of arguments the function accept (default 1).
  62. * @return boolean true
  63. */
  64. function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
  65. global $wp_filter, $merged_filters;
  66. $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
  67. $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
  68. unset( $merged_filters[ $tag ] );
  69. return true;
  70. }
  71. /**
  72. * Check if any filter has been registered for a hook.
  73. *
  74. * @package WordPress
  75. * @subpackage Plugin
  76. * @since 2.5
  77. * @global array $wp_filter Stores all of the filters
  78. *
  79. * @param string $tag The name of the filter hook.
  80. * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
  81. * @return int|boolean Optionally returns the priority on that hook for the specified function.
  82. */
  83. function has_filter($tag, $function_to_check = false) {
  84. global $wp_filter;
  85. $has = !empty($wp_filter[$tag]);
  86. if ( false === $function_to_check || false == $has )
  87. return $has;
  88. if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
  89. return false;
  90. foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
  91. if ( isset($wp_filter[$tag][$priority][$idx]) )
  92. return $priority;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Call the functions added to a filter hook.
  98. *
  99. * The callback functions attached to filter hook $tag are invoked by calling
  100. * this function. This function can be used to create a new filter hook by
  101. * simply calling this function with the name of the new hook specified using
  102. * the $tag parameter.
  103. *
  104. * The function allows for additional arguments to be added and passed to hooks.
  105. * <code>
  106. * function example_hook($string, $arg1, $arg2)
  107. * {
  108. * //Do stuff
  109. * return $string;
  110. * }
  111. * $value = apply_filters('example_filter', 'filter me', 'arg1', 'arg2');
  112. * </code>
  113. *
  114. * @package WordPress
  115. * @subpackage Plugin
  116. * @since 0.71
  117. * @global array $wp_filter Stores all of the filters
  118. * @global array $merged_filters Merges the filter hooks using this function.
  119. * @global array $wp_current_filter stores the list of current filters with the current one last
  120. *
  121. * @param string $tag The name of the filter hook.
  122. * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
  123. * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
  124. * @return mixed The filtered value after all hooked functions are applied to it.
  125. */
  126. function apply_filters($tag, $value) {
  127. global $wp_filter, $merged_filters, $wp_current_filter;
  128. $args = array();
  129. // Do 'all' actions first
  130. if ( isset($wp_filter['all']) ) {
  131. $wp_current_filter[] = $tag;
  132. $args = func_get_args();
  133. _wp_call_all_hook($args);
  134. }
  135. if ( !isset($wp_filter[$tag]) ) {
  136. if ( isset($wp_filter['all']) )
  137. array_pop($wp_current_filter);
  138. return $value;
  139. }
  140. if ( !isset($wp_filter['all']) )
  141. $wp_current_filter[] = $tag;
  142. // Sort
  143. if ( !isset( $merged_filters[ $tag ] ) ) {
  144. ksort($wp_filter[$tag]);
  145. $merged_filters[ $tag ] = true;
  146. }
  147. reset( $wp_filter[ $tag ] );
  148. if ( empty($args) )
  149. $args = func_get_args();
  150. do {
  151. foreach( (array) current($wp_filter[$tag]) as $the_ )
  152. if ( !is_null($the_['function']) ){
  153. $args[1] = $value;
  154. $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
  155. }
  156. } while ( next($wp_filter[$tag]) !== false );
  157. array_pop( $wp_current_filter );
  158. return $value;
  159. }
  160. /**
  161. * Execute functions hooked on a specific filter hook, specifying arguments in an array.
  162. *
  163. * @see apply_filters() This function is identical, but the arguments passed to the
  164. * functions hooked to <tt>$tag</tt> are supplied using an array.
  165. *
  166. * @package WordPress
  167. * @subpackage Plugin
  168. * @since 3.0.0
  169. * @global array $wp_filter Stores all of the filters
  170. * @global array $merged_filters Merges the filter hooks using this function.
  171. * @global array $wp_current_filter stores the list of current filters with the current one last
  172. *
  173. * @param string $tag The name of the filter hook.
  174. * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
  175. * @return mixed The filtered value after all hooked functions are applied to it.
  176. */
  177. function apply_filters_ref_array($tag, $args) {
  178. global $wp_filter, $merged_filters, $wp_current_filter;
  179. // Do 'all' actions first
  180. if ( isset($wp_filter['all']) ) {
  181. $wp_current_filter[] = $tag;
  182. $all_args = func_get_args();
  183. _wp_call_all_hook($all_args);
  184. }
  185. if ( !isset($wp_filter[$tag]) ) {
  186. if ( isset($wp_filter['all']) )
  187. array_pop($wp_current_filter);
  188. return $args[0];
  189. }
  190. if ( !isset($wp_filter['all']) )
  191. $wp_current_filter[] = $tag;
  192. // Sort
  193. if ( !isset( $merged_filters[ $tag ] ) ) {
  194. ksort($wp_filter[$tag]);
  195. $merged_filters[ $tag ] = true;
  196. }
  197. reset( $wp_filter[ $tag ] );
  198. do {
  199. foreach( (array) current($wp_filter[$tag]) as $the_ )
  200. if ( !is_null($the_['function']) )
  201. $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
  202. } while ( next($wp_filter[$tag]) !== false );
  203. array_pop( $wp_current_filter );
  204. return $args[0];
  205. }
  206. /**
  207. * Removes a function from a specified filter hook.
  208. *
  209. * This function removes a function attached to a specified filter hook. This
  210. * method can be used to remove default functions attached to a specific filter
  211. * hook and possibly replace them with a substitute.
  212. *
  213. * To remove a hook, the $function_to_remove and $priority arguments must match
  214. * when the hook was added. This goes for both filters and actions. No warning
  215. * will be given on removal failure.
  216. *
  217. * @package WordPress
  218. * @subpackage Plugin
  219. * @since 1.2
  220. *
  221. * @param string $tag The filter hook to which the function to be removed is hooked.
  222. * @param callback $function_to_remove The name of the function which should be removed.
  223. * @param int $priority optional. The priority of the function (default: 10).
  224. * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
  225. * @return boolean Whether the function existed before it was removed.
  226. */
  227. function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
  228. $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
  229. $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
  230. if ( true === $r) {
  231. unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
  232. if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
  233. unset($GLOBALS['wp_filter'][$tag][$priority]);
  234. unset($GLOBALS['merged_filters'][$tag]);
  235. }
  236. return $r;
  237. }
  238. /**
  239. * Remove all of the hooks from a filter.
  240. *
  241. * @since 2.7
  242. *
  243. * @param string $tag The filter to remove hooks from.
  244. * @param int $priority The priority number to remove.
  245. * @return bool True when finished.
  246. */
  247. function remove_all_filters($tag, $priority = false) {
  248. global $wp_filter, $merged_filters;
  249. if( isset($wp_filter[$tag]) ) {
  250. if( false !== $priority && isset($wp_filter[$tag][$priority]) )
  251. unset($wp_filter[$tag][$priority]);
  252. else
  253. unset($wp_filter[$tag]);
  254. }
  255. if( isset($merged_filters[$tag]) )
  256. unset($merged_filters[$tag]);
  257. return true;
  258. }
  259. /**
  260. * Retrieve the name of the current filter or action.
  261. *
  262. * @package WordPress
  263. * @subpackage Plugin
  264. * @since 2.5
  265. *
  266. * @return string Hook name of the current filter or action.
  267. */
  268. function current_filter() {
  269. global $wp_current_filter;
  270. return end( $wp_current_filter );
  271. }
  272. /**
  273. * Hooks a function on to a specific action.
  274. *
  275. * Actions are the hooks that the WordPress core launches at specific points
  276. * during execution, or when specific events occur. Plugins can specify that
  277. * one or more of its PHP functions are executed at these points, using the
  278. * Action API.
  279. *
  280. * @uses add_filter() Adds an action. Parameter list and functionality are the same.
  281. *
  282. * @package WordPress
  283. * @subpackage Plugin
  284. * @since 1.2
  285. *
  286. * @param string $tag The name of the action to which the $function_to_add is hooked.
  287. * @param callback $function_to_add The name of the function you wish to be called.
  288. * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
  289. * @param int $accepted_args optional. The number of arguments the function accept (default 1).
  290. */
  291. function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
  292. return add_filter($tag, $function_to_add, $priority, $accepted_args);
  293. }
  294. /**
  295. * Execute functions hooked on a specific action hook.
  296. *
  297. * This function invokes all functions attached to action hook $tag. It is
  298. * possible to create new action hooks by simply calling this function,
  299. * specifying the name of the new hook using the <tt>$tag</tt> parameter.
  300. *
  301. * You can pass extra arguments to the hooks, much like you can with
  302. * apply_filters().
  303. *
  304. * @see apply_filters() This function works similar with the exception that
  305. * nothing is returned and only the functions or methods are called.
  306. *
  307. * @package WordPress
  308. * @subpackage Plugin
  309. * @since 1.2
  310. * @global array $wp_filter Stores all of the filters
  311. * @global array $wp_actions Increments the amount of times action was triggered.
  312. *
  313. * @param string $tag The name of the action to be executed.
  314. * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
  315. * @return null Will return null if $tag does not exist in $wp_filter array
  316. */
  317. function do_action($tag, $arg = '') {
  318. global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
  319. if ( ! isset($wp_actions) )
  320. $wp_actions = array();
  321. if ( ! isset($wp_actions[$tag]) )
  322. $wp_actions[$tag] = 1;
  323. else
  324. ++$wp_actions[$tag];
  325. // Do 'all' actions first
  326. if ( isset($wp_filter['all']) ) {
  327. $wp_current_filter[] = $tag;
  328. $all_args = func_get_args();
  329. _wp_call_all_hook($all_args);
  330. }
  331. if ( !isset($wp_filter[$tag]) ) {
  332. if ( isset($wp_filter['all']) )
  333. array_pop($wp_current_filter);
  334. return;
  335. }
  336. if ( !isset($wp_filter['all']) )
  337. $wp_current_filter[] = $tag;
  338. $args = array();
  339. if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
  340. $args[] =& $arg[0];
  341. else
  342. $args[] = $arg;
  343. for ( $a = 2; $a < func_num_args(); $a++ )
  344. $args[] = func_get_arg($a);
  345. // Sort
  346. if ( !isset( $merged_filters[ $tag ] ) ) {
  347. ksort($wp_filter[$tag]);
  348. $merged_filters[ $tag ] = true;
  349. }
  350. reset( $wp_filter[ $tag ] );
  351. do {
  352. foreach ( (array) current($wp_filter[$tag]) as $the_ )
  353. if ( !is_null($the_['function']) )
  354. call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
  355. } while ( next($wp_filter[$tag]) !== false );
  356. array_pop($wp_current_filter);
  357. }
  358. /**
  359. * Retrieve the number times an action is fired.
  360. *
  361. * @package WordPress
  362. * @subpackage Plugin
  363. * @since 2.1
  364. * @global array $wp_actions Increments the amount of times action was triggered.
  365. *
  366. * @param string $tag The name of the action hook.
  367. * @return int The number of times action hook <tt>$tag</tt> is fired
  368. */
  369. function did_action($tag) {
  370. global $wp_actions;
  371. if ( ! isset( $wp_actions ) || ! isset( $wp_actions[$tag] ) )
  372. return 0;
  373. return $wp_actions[$tag];
  374. }
  375. /**
  376. * Execute functions hooked on a specific action hook, specifying arguments in an array.
  377. *
  378. * @see do_action() This function is identical, but the arguments passed to the
  379. * functions hooked to <tt>$tag</tt> are supplied using an array.
  380. *
  381. * @package WordPress
  382. * @subpackage Plugin
  383. * @since 2.1
  384. * @global array $wp_filter Stores all of the filters
  385. * @global array $wp_actions Increments the amount of times action was triggered.
  386. *
  387. * @param string $tag The name of the action to be executed.
  388. * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
  389. * @return null Will return null if $tag does not exist in $wp_filter array
  390. */
  391. function do_action_ref_array($tag, $args) {
  392. global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
  393. if ( ! isset($wp_actions) )
  394. $wp_actions = array();
  395. if ( ! isset($wp_actions[$tag]) )
  396. $wp_actions[$tag] = 1;
  397. else
  398. ++$wp_actions[$tag];
  399. // Do 'all' actions first
  400. if ( isset($wp_filter['all']) ) {
  401. $wp_current_filter[] = $tag;
  402. $all_args = func_get_args();
  403. _wp_call_all_hook($all_args);
  404. }
  405. if ( !isset($wp_filter[$tag]) ) {
  406. if ( isset($wp_filter['all']) )
  407. array_pop($wp_current_filter);
  408. return;
  409. }
  410. if ( !isset($wp_filter['all']) )
  411. $wp_current_filter[] = $tag;
  412. // Sort
  413. if ( !isset( $merged_filters[ $tag ] ) ) {
  414. ksort($wp_filter[$tag]);
  415. $merged_filters[ $tag ] = true;
  416. }
  417. reset( $wp_filter[ $tag ] );
  418. do {
  419. foreach( (array) current($wp_filter[$tag]) as $the_ )
  420. if ( !is_null($the_['function']) )
  421. call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
  422. } while ( next($wp_filter[$tag]) !== false );
  423. array_pop($wp_current_filter);
  424. }
  425. /**
  426. * Check if any action has been registered for a hook.
  427. *
  428. * @package WordPress
  429. * @subpackage Plugin
  430. * @since 2.5
  431. * @see has_filter() has_action() is an alias of has_filter().
  432. *
  433. * @param string $tag The name of the action hook.
  434. * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
  435. * @return int|boolean Optionally returns the priority on that hook for the specified function.
  436. */
  437. function has_action($tag, $function_to_check = false) {
  438. return has_filter($tag, $function_to_check);
  439. }
  440. /**
  441. * Removes a function from a specified action hook.
  442. *
  443. * This function removes a function attached to a specified action hook. This
  444. * method can be used to remove default functions attached to a specific filter
  445. * hook and possibly replace them with a substitute.
  446. *
  447. * @package WordPress
  448. * @subpackage Plugin
  449. * @since 1.2
  450. *
  451. * @param string $tag The action hook to which the function to be removed is hooked.
  452. * @param callback $function_to_remove The name of the function which should be removed.
  453. * @param int $priority optional The priority of the function (default: 10).
  454. * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
  455. * @return boolean Whether the function is removed.
  456. */
  457. function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
  458. return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
  459. }
  460. /**
  461. * Remove all of the hooks from an action.
  462. *
  463. * @since 2.7
  464. *
  465. * @param string $tag The action to remove hooks from.
  466. * @param int $priority The priority number to remove them from.
  467. * @return bool True when finished.
  468. */
  469. function remove_all_actions($tag, $priority = false) {
  470. return remove_all_filters($tag, $priority);
  471. }
  472. //
  473. // Functions for handling plugins.
  474. //
  475. /**
  476. * Gets the basename of a plugin.
  477. *
  478. * This method extracts the name of a plugin from its filename.
  479. *
  480. * @package WordPress
  481. * @subpackage Plugin
  482. * @since 1.5
  483. *
  484. * @access private
  485. *
  486. * @param string $file The filename of plugin.
  487. * @return string The name of a plugin.
  488. * @uses WP_PLUGIN_DIR
  489. */
  490. function plugin_basename($file) {
  491. $file = str_replace('\\','/',$file); // sanitize for Win32 installs
  492. $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
  493. $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
  494. $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
  495. $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
  496. $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
  497. $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
  498. $file = trim($file, '/');
  499. return $file;
  500. }
  501. /**
  502. * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
  503. * @package WordPress
  504. * @subpackage Plugin
  505. * @since 2.8
  506. *
  507. * @param string $file The filename of the plugin (__FILE__)
  508. * @return string the filesystem path of the directory that contains the plugin
  509. */
  510. function plugin_dir_path( $file ) {
  511. return trailingslashit( dirname( $file ) );
  512. }
  513. /**
  514. * Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in
  515. * @package WordPress
  516. * @subpackage Plugin
  517. * @since 2.8
  518. *
  519. * @param string $file The filename of the plugin (__FILE__)
  520. * @return string the URL path of the directory that contains the plugin
  521. */
  522. function plugin_dir_url( $file ) {
  523. return trailingslashit( plugins_url( '', $file ) );
  524. }
  525. /**
  526. * Set the activation hook for a plugin.
  527. *
  528. * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
  529. * activated. In the name of this hook, PLUGINNAME is replaced with the name of
  530. * the plugin, including the optional subdirectory. For example, when the plugin
  531. * is located in wp-content/plugin/sampleplugin/sample.php, then the name of
  532. * this hook will become 'activate_sampleplugin/sample.php'. When the plugin
  533. * consists of only one file and is (as by default) located at
  534. * wp-content/plugin/sample.php the name of this hook will be
  535. * 'activate_sample.php'.
  536. *
  537. * @package WordPress
  538. * @subpackage Plugin
  539. * @since 2.0
  540. *
  541. * @param string $file The filename of the plugin including the path.
  542. * @param callback $function the function hooked to the 'activate_PLUGIN' action.
  543. */
  544. function register_activation_hook($file, $function) {
  545. $file = plugin_basename($file);
  546. add_action('activate_' . $file, $function);
  547. }
  548. /**
  549. * Set the deactivation hook for a plugin.
  550. *
  551. * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
  552. * deactivated. In the name of this hook, PLUGINNAME is replaced with the name
  553. * of the plugin, including the optional subdirectory. For example, when the
  554. * plugin is located in wp-content/plugin/sampleplugin/sample.php, then
  555. * the name of this hook will become 'activate_sampleplugin/sample.php'.
  556. *
  557. * When the plugin consists of only one file and is (as by default) located at
  558. * wp-content/plugin/sample.php the name of this hook will be
  559. * 'activate_sample.php'.
  560. *
  561. * @package WordPress
  562. * @subpackage Plugin
  563. * @since 2.0
  564. *
  565. * @param string $file The filename of the plugin including the path.
  566. * @param callback $function the function hooked to the 'activate_PLUGIN' action.
  567. */
  568. function register_deactivation_hook($file, $function) {
  569. $file = plugin_basename($file);
  570. add_action('deactivate_' . $file, $function);
  571. }
  572. /**
  573. * Set the uninstallation hook for a plugin.
  574. *
  575. * Registers the uninstall hook that will be called when the user clicks on the
  576. * uninstall link that calls for the plugin to uninstall itself. The link won't
  577. * be active unless the plugin hooks into the action.
  578. *
  579. * The plugin should not run arbitrary code outside of functions, when
  580. * registering the uninstall hook. In order to run using the hook, the plugin
  581. * will have to be included, which means that any code laying outside of a
  582. * function will be run during the uninstall process. The plugin should not
  583. * hinder the uninstall process.
  584. *
  585. * If the plugin can not be written without running code within the plugin, then
  586. * the plugin should create a file named 'uninstall.php' in the base plugin
  587. * folder. This file will be called, if it exists, during the uninstall process
  588. * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
  589. * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
  590. * executing.
  591. *
  592. * @since 2.7
  593. *
  594. * @param string $file
  595. * @param callback $callback The callback to run when the hook is called. Must be a static method or function.
  596. */
  597. function register_uninstall_hook( $file, $callback ) {
  598. if ( is_array( $callback ) && is_object( $callback[0] ) ) {
  599. _doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
  600. return;
  601. }
  602. // The option should not be autoloaded, because it is not needed in most
  603. // cases. Emphasis should be put on using the 'uninstall.php' way of
  604. // uninstalling the plugin.
  605. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  606. $uninstallable_plugins[plugin_basename($file)] = $callback;
  607. update_option('uninstall_plugins', $uninstallable_plugins);
  608. }
  609. /**
  610. * Calls the 'all' hook, which will process the functions hooked into it.
  611. *
  612. * The 'all' hook passes all of the arguments or parameters that were used for
  613. * the hook, which this function was called for.
  614. *
  615. * This function is used internally for apply_filters(), do_action(), and
  616. * do_action_ref_array() and is not meant to be used from outside those
  617. * functions. This function does not check for the existence of the all hook, so
  618. * it will fail unless the all hook exists prior to this function call.
  619. *
  620. * @package WordPress
  621. * @subpackage Plugin
  622. * @since 2.5
  623. * @access private
  624. *
  625. * @uses $wp_filter Used to process all of the functions in the 'all' hook
  626. *
  627. * @param array $args The collected parameters from the hook that was called.
  628. * @param string $hook Optional. The hook name that was used to call the 'all' hook.
  629. */
  630. function _wp_call_all_hook($args) {
  631. global $wp_filter;
  632. reset( $wp_filter['all'] );
  633. do {
  634. foreach( (array) current($wp_filter['all']) as $the_ )
  635. if ( !is_null($the_['function']) )
  636. call_user_func_array($the_['function'], $args);
  637. } while ( next($wp_filter['all']) !== false );
  638. }
  639. /**
  640. * Build Unique ID for storage and retrieval.
  641. *
  642. * The old way to serialize the callback caused issues and this function is the
  643. * solution. It works by checking for objects and creating an a new property in
  644. * the class to keep track of the object and new objects of the same class that
  645. * need to be added.
  646. *
  647. * It also allows for the removal of actions and filters for objects after they
  648. * change class properties. It is possible to include the property $wp_filter_id
  649. * in your class and set it to "null" or a number to bypass the workaround.
  650. * However this will prevent you from adding new classes and any new classes
  651. * will overwrite the previous hook by the same class.
  652. *
  653. * Functions and static method callbacks are just returned as strings and
  654. * shouldn't have any speed penalty.
  655. *
  656. * @package WordPress
  657. * @subpackage Plugin
  658. * @access private
  659. * @since 2.2.3
  660. * @link http://trac.wordpress.org/ticket/3875
  661. *
  662. * @global array $wp_filter Storage for all of the filters and actions
  663. * @param string $tag Used in counting how many hooks were applied
  664. * @param callback $function Used for creating unique id
  665. * @param int|bool $priority Used in counting how many hooks were applied. If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
  666. * @return string|bool Unique ID for usage as array key or false if $priority === false and $function is an object reference, and it does not already have a uniqe id.
  667. */
  668. function _wp_filter_build_unique_id($tag, $function, $priority) {
  669. global $wp_filter;
  670. static $filter_id_count = 0;
  671. if ( is_string($function) )
  672. return $function;
  673. if ( is_object($function) ) {
  674. // Closures are currently implemented as objects
  675. $function = array( $function, '' );
  676. } else {
  677. $function = (array) $function;
  678. }
  679. if (is_object($function[0]) ) {
  680. // Object Class Calling
  681. if ( function_exists('spl_object_hash') ) {
  682. return spl_object_hash($function[0]) . $function[1];
  683. } else {
  684. $obj_idx = get_class($function[0]).$function[1];
  685. if ( !isset($function[0]->wp_filter_id) ) {
  686. if ( false === $priority )
  687. return false;
  688. $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
  689. $function[0]->wp_filter_id = $filter_id_count;
  690. ++$filter_id_count;
  691. } else {
  692. $obj_idx .= $function[0]->wp_filter_id;
  693. }
  694. return $obj_idx;
  695. }
  696. } else if ( is_string($function[0]) ) {
  697. // Static Calling
  698. return $function[0].$function[1];
  699. }
  700. }
  701. ?>