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 | // So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] |
---|
11 | $wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); |
---|
12 | return true; |
---|
13 | } |
---|
14 | |
---|
15 | function apply_filters($tag, $string) { |
---|
16 | global $wp_filter; |
---|
17 | |
---|
18 | merge_filters($tag); |
---|
19 | |
---|
20 | if ( !isset($wp_filter[$tag]) ) |
---|
21 | return $string; |
---|
22 | |
---|
23 | $args = func_get_args(); |
---|
24 | |
---|
25 | do{ |
---|
26 | foreach( (array) current($wp_filter[$tag]) as $the_ ) |
---|
27 | if ( !is_null($the_['function']) ){ |
---|
28 | $args[1] = $string; |
---|
29 | $string = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); |
---|
30 | } |
---|
31 | |
---|
32 | } while ( next($wp_filter[$tag]) ); |
---|
33 | |
---|
34 | return $string; |
---|
35 | } |
---|
36 | |
---|
37 | function merge_filters($tag) { |
---|
38 | global $wp_filter; |
---|
39 | |
---|
40 | if ( isset($wp_filter['all']) ) |
---|
41 | $wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]); |
---|
42 | |
---|
43 | if ( isset($wp_filter[$tag]) ){ |
---|
44 | reset($wp_filter[$tag]); |
---|
45 | uksort($wp_filter[$tag], "strnatcasecmp"); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { |
---|
50 | global $wp_filter; |
---|
51 | |
---|
52 | unset($GLOBALS['wp_filter'][$tag][$priority][serialize($function_to_remove)]); |
---|
53 | |
---|
54 | return true; |
---|
55 | } |
---|
56 | |
---|
57 | // |
---|
58 | // Action functions |
---|
59 | // |
---|
60 | |
---|
61 | function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { |
---|
62 | add_filter($tag, $function_to_add, $priority, $accepted_args); |
---|
63 | } |
---|
64 | |
---|
65 | function do_action($tag, $arg = '') { |
---|
66 | global $wp_filter, $wp_actions; |
---|
67 | |
---|
68 | $args = array(); |
---|
69 | if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this) |
---|
70 | $args[] =& $arg[0]; |
---|
71 | else |
---|
72 | $args[] = $arg; |
---|
73 | for ( $a = 2; $a < func_num_args(); $a++ ) |
---|
74 | $args[] = func_get_arg($a); |
---|
75 | |
---|
76 | merge_filters($tag); |
---|
77 | |
---|
78 | if ( !isset($wp_filter[$tag]) ) |
---|
79 | return; |
---|
80 | |
---|
81 | do{ |
---|
82 | foreach( (array) current($wp_filter[$tag]) as $the_ ) |
---|
83 | if ( !is_null($the_['function']) ) |
---|
84 | call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); |
---|
85 | |
---|
86 | } while ( next($wp_filter[$tag]) ); |
---|
87 | |
---|
88 | if ( is_array($wp_actions) ) |
---|
89 | $wp_actions[] = $tag; |
---|
90 | else |
---|
91 | $wp_actions = array($tag); |
---|
92 | } |
---|
93 | |
---|
94 | // Returns the number of times an action has been done |
---|
95 | function did_action($tag) { |
---|
96 | global $wp_actions; |
---|
97 | |
---|
98 | return count(array_keys($wp_actions, $tag)); |
---|
99 | } |
---|
100 | |
---|
101 | function do_action_ref_array($tag, $args) { |
---|
102 | global $wp_filter, $wp_actions; |
---|
103 | |
---|
104 | if ( !is_array($wp_actions) ) |
---|
105 | $wp_actions = array($tag); |
---|
106 | else |
---|
107 | $wp_actions[] = $tag; |
---|
108 | |
---|
109 | merge_filters($tag); |
---|
110 | |
---|
111 | if ( !isset($wp_filter[$tag]) ) |
---|
112 | return; |
---|
113 | |
---|
114 | do{ |
---|
115 | foreach( (array) current($wp_filter[$tag]) as $the_ ) |
---|
116 | if ( !is_null($the_['function']) ) |
---|
117 | call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args'])); |
---|
118 | |
---|
119 | } while ( next($wp_filter[$tag]) ); |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { |
---|
124 | remove_filter($tag, $function_to_remove, $priority, $accepted_args); |
---|
125 | } |
---|
126 | |
---|
127 | // |
---|
128 | // Functions for handling plugins. |
---|
129 | // |
---|
130 | |
---|
131 | function plugin_basename($file) { |
---|
132 | $file = preg_replace('|\\\\+|', '\\\\', $file); |
---|
133 | $file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file); |
---|
134 | return $file; |
---|
135 | } |
---|
136 | |
---|
137 | function register_activation_hook($file, $function) { |
---|
138 | $file = plugin_basename($file); |
---|
139 | add_action('activate_' . $file, $function); |
---|
140 | } |
---|
141 | |
---|
142 | function register_deactivation_hook($file, $function) { |
---|
143 | $file = plugin_basename($file); |
---|
144 | add_action('deactivate_' . $file, $function); |
---|
145 | } |
---|
146 | |
---|
147 | ?> |
---|