PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/plugin.php

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