root/tags/2.1/wp-includes/plugin.php

Revision 4630, 5.1 kB (checked in by ryan, 2 years ago)

did_action()

  • Property svn:eol-style set to native
Line 
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     // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]']
21     $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args);
22     return true;
23 }
24
25 function apply_filters($tag, $string) {
26     global $wp_filter;
27
28     $args = array();
29     for ( $a = 2; $a < func_num_args(); $a++ )
30         $args[] = func_get_arg($a);
31
32     merge_filters($tag);
33
34     if ( !isset($wp_filter[$tag]) )
35         return $string;
36
37     foreach ( (array) $wp_filter[$tag] as $priority => $functions ) {
38         if ( !is_null($functions) ) {
39             foreach ( (array) $functions as $function ) {
40                 $function_name = $function['function'];
41                 $accepted_args = $function['accepted_args'];
42                 $the_args = $args;
43                 array_unshift($the_args, $string);
44                 if ( $accepted_args > 0 )
45                     $the_args = array_slice($the_args, 0, $accepted_args);
46                 elseif ( 0 == $accepted_args )
47                     $the_args = NULL;
48                 $string = call_user_func_array($function_name, $the_args);
49             }
50         }
51     }
52     return $string;
53 }
54
55 function merge_filters($tag) {
56     global $wp_filter;
57     if ( isset($wp_filter['all']) ) {
58         foreach ( (array) $wp_filter['all'] as $priority => $functions ) {
59             if ( isset($wp_filter[$tag][$priority]) )
60                 $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]);
61             else
62                 $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array());
63             $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]);
64         }
65     }
66
67     if ( isset($wp_filter[$tag]) )
68         uksort( $wp_filter[$tag], "strnatcasecmp" );
69 }
70
71 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
72     global $wp_filter;
73
74     // rebuild the list of filters
75     if ( isset($wp_filter[$tag]["$priority"]) ) {
76         $new_function_list = array();
77         foreach ( (array) $wp_filter[$tag]["$priority"] as $filter ) {
78             if ( $filter['function'] != $function_to_remove )
79                 $new_function_list[] = $filter;
80         }
81         $wp_filter[$tag]["$priority"] = $new_function_list;
82     }
83     return true;
84 }
85
86 //
87 // Action functions
88 //
89
90 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
91     add_filter($tag, $function_to_add, $priority, $accepted_args);
92 }
93
94 function do_action($tag, $arg = '') {
95     global $wp_filter, $wp_actions;
96
97     $args = array();
98     if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
99         $args[] =& $arg[0];
100     else
101         $args[] = $arg;
102     for ( $a = 2; $a < func_num_args(); $a++ )
103         $args[] = func_get_arg($a);
104
105     merge_filters($tag);
106
107     if ( !isset($wp_filter[$tag]) )
108         return;
109
110     foreach ( (array) $wp_filter[$tag] as $priority => $functions ) {
111         if ( !is_null($functions) ) {
112             foreach ( (array) $functions as $function ) {
113                 $function_name = $function['function'];
114                 $accepted_args = $function['accepted_args'];
115
116                 if ( $accepted_args > 0 )
117                     $the_args = array_slice($args, 0, $accepted_args);
118                 elseif ( $accepted_args == 0 )
119                     $the_args = NULL;
120                 else
121                     $the_args = $args;
122
123                 call_user_func_array($function_name, $the_args);
124             }
125         }
126     }
127
128     if ( is_array($wp_actions) )
129         $wp_actions[] = $tag;
130     else
131         $wp_actions = array($tag);
132 }
133
134 // Returns the number of times an action has been done
135 function did_action($tag) {
136     global $wp_actions;
137
138     return count(array_keys($wp_actions, $tag));
139 }
140
141 function do_action_ref_array($tag, $args) {
142     global $wp_filter, $wp_actions;
143
144     if ( !is_array($wp_actions) )
145         $wp_actions = array($tag);
146     else
147         $wp_actions[] = $tag;
148
149     merge_filters($tag);
150
151     if ( !isset($wp_filter[$tag]) )
152         return;
153
154     foreach ( (array) $wp_filter[$tag] as $priority => $functions ) {
155         if ( !is_null($functions) ) {
156             foreach ( (array) $functions as $function ) {
157                 $function_name = $function['function'];
158                 $accepted_args = $function['accepted_args'];
159                 if ( $accepted_args > 0 )
160                     $the_args = array_slice($args, 0, $accepted_args);
161                 elseif ( 0 == $accepted_args )
162                     $the_args = NULL;
163                 else
164                     $the_args = $args;
165
166                 call_user_func_array($function_name, $the_args);
167             }
168         }
169     }
170 }
171
172 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
173     remove_filter($tag, $function_to_remove, $priority, $accepted_args);
174 }
175
176 //
177 // Functions for handling plugins.
178 //
179
180 function plugin_basename($file) {
181     $file = preg_replace('|\\\\+|', '\\\\', $file);
182     $file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file);
183     return $file;
184 }
185
186 function register_activation_hook($file, $function) {
187     $file = plugin_basename($file);
188     add_action('activate_' . $file, $function);
189 }
190
191 function register_deactivation_hook($file, $function) {
192     $file = plugin_basename($file);
193     add_action('deactivate_' . $file, $function);
194 }
195
196 ?>
Note: See TracBrowser for help on using the browser.