[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-includes/ -> plugin.php (source)

   1  <?php
   2  
   3  //
   4  // Filter functions, the core of the WP plugin architecture.
   5  //
   6  
   7  function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
   8      global $wp_filter;
   9  
  10      // check that we don't already have the same filter at the same priority
  11      if ( isset($wp_filter[$tag]["$priority"]) ) {
  12          foreach($wp_filter[$tag]["$priority"] as $filter) {
  13              // uncomment if we want to match function AND accepted_args
  14              // if ( $filter == array($function, $accepted_args) ) {
  15              if ( $filter['function'] == $function_to_add ) {
  16                  return true;
  17              }
  18          }
  19      }
  20  
  21      // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]']
  22      $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args);
  23      return true;
  24  }
  25  
  26  function apply_filters($tag, $string) {
  27      global $wp_filter;
  28  
  29      $args = array_slice(func_get_args(), 2);
  30  
  31      merge_filters($tag);
  32  
  33      if ( !isset($wp_filter[$tag]) ) {
  34          return $string;
  35      }
  36      foreach ($wp_filter[$tag] as $priority => $functions) {
  37          if ( !is_null($functions) ) {
  38              foreach($functions as $function) {
  39  
  40                  $all_args = array_merge(array($string), $args);
  41                  $function_name = $function['function'];
  42                  $accepted_args = $function['accepted_args'];
  43  
  44                  if ( $accepted_args == 1 )
  45                      $the_args = array($string);
  46                  elseif ( $accepted_args > 1 )
  47                      $the_args = array_slice($all_args, 0, $accepted_args);
  48                  elseif ( $accepted_args == 0 )
  49                      $the_args = NULL;
  50                  else
  51                      $the_args = $all_args;
  52  
  53                  $string = call_user_func_array($function_name, $the_args);
  54              }
  55          }
  56      }
  57      return $string;
  58  }
  59  
  60  
  61  function merge_filters($tag) {
  62      global $wp_filter;
  63      if ( isset($wp_filter['all']) ) {
  64          foreach ($wp_filter['all'] as $priority => $functions) {
  65              if ( isset($wp_filter[$tag][$priority]) )
  66                  $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]);
  67              else
  68                  $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array());
  69              $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]);
  70          }
  71      }
  72  
  73      if ( isset($wp_filter[$tag]) )
  74          ksort( $wp_filter[$tag] );
  75  }
  76  
  77  
  78  
  79  function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
  80      global $wp_filter;
  81  
  82      // rebuild the list of filters
  83      if ( isset($wp_filter[$tag]["$priority"]) ) {
  84          $new_function_list = array();
  85          foreach($wp_filter[$tag]["$priority"] as $filter) {
  86              if ( $filter['function'] != $function_to_remove ) {
  87                  $new_function_list[] = $filter;
  88              }
  89          }
  90          $wp_filter[$tag]["$priority"] = $new_function_list;
  91      }
  92      return true;
  93  }
  94  
  95  //
  96  // Action functions
  97  //
  98  
  99  function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
 100      add_filter($tag, $function_to_add, $priority, $accepted_args);
 101  }
 102  
 103  function do_action($tag, $arg = '') {
 104      global $wp_filter;
 105      $extra_args = array_slice(func_get_args(), 2);
 106       if ( is_array($arg) )
 107           $args = array_merge($arg, $extra_args);
 108      else
 109          $args = array_merge(array($arg), $extra_args);
 110  
 111      merge_filters($tag);
 112  
 113      if ( !isset($wp_filter[$tag]) ) {
 114          return;
 115      }
 116      foreach ($wp_filter[$tag] as $priority => $functions) {
 117          if ( !is_null($functions) ) {
 118              foreach($functions as $function) {
 119  
 120                  $function_name = $function['function'];
 121                  $accepted_args = $function['accepted_args'];
 122  
 123                  if ( $accepted_args == 1 ) {
 124                      if ( is_array($arg) )
 125                          $the_args = $arg;
 126                      else
 127                          $the_args = array($arg);
 128                  } elseif ( $accepted_args > 1 ) {
 129                      $the_args = array_slice($args, 0, $accepted_args);
 130                  } elseif ( $accepted_args == 0 ) {
 131                      $the_args = NULL;
 132                  } else {
 133                      $the_args = $args;
 134                  }
 135  
 136                  $string = call_user_func_array($function_name, $the_args);
 137              }
 138          }
 139      }
 140  }
 141  
 142  function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
 143      remove_filter($tag, $function_to_remove, $priority, $accepted_args);
 144  }
 145  
 146  //
 147  // Functions for handling plugins.
 148  //
 149  
 150  function plugin_basename($file) {
 151      $file = preg_replace('|\\\\+|', '\\\\', $file);
 152      $file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file);
 153      return $file;
 154  }
 155  
 156  function register_activation_hook($file, $function) {
 157      $file = plugin_basename($file);
 158  
 159      add_action('activate_' . $file, $function);
 160  }
 161  
 162  function register_deactivation_hook($file, $function) {
 163      $file = plugin_basename($file);
 164  
 165      add_action('deactivate_' . $file, $function);
 166  }
 167  
 168  ?>

Your comment here...

Name: Location:
Comments:


List: Classes | Functions | Variables | Constants | Tables

Generated: Sat Jul 15 11:57:04 2006 Courtesy of Taragana