PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/plugin.php

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