rawky

Plugin.php

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