em-taxonomy-terms.php
15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
class EM_Taxonomy_Terms extends EM_Object implements Iterator, Countable{
protected $is_ms_global = false;
protected $meta_key = 'event-taxonomy';
protected $taxonomy = 'event-taxonomy';
protected $terms_name = 'taxonomies';
protected $term_class = 'EM_Taxonomy';
/**
* String representing the search action used in AJAX searches which will be available in child function when PHP 5.3 brings us LSB
* @var string
*/
protected $ajax_search_action = 'search_taxonomy';
/**
* Blank instance of this class used in the static functions until PHP 5.3 brings us LSB
* @var EM_Taxonomy_Terms
*/
protected static $instance;
/**
* Array of EM_Taxonomy_Term child objects for a specific event
* @var array
*/
public $terms = array();
/**
* Event ID of this set of taxonomy terms
* @var int
*/
public $event_id;
/**
* Post ID of this set of taxonomy terms
* @var int
*/
public $post_id;
/**
* Creates an EM_Taxonomy_Terms instance, currently accepts an EM_Event object (gets all Taxonomy Terms for that event) or array of any EM_Taxonomy_Term objects, which can be manipulated in bulk with helper functions.
* @param mixed $data
* @return null
*/
public function __construct( $data = false ){
global $wpdb;
if( $this->is_ms_global ) self::ms_global_switch();
if( is_object($data) && get_class($data) == "EM_Event" && !empty($data->post_id) ){ //Creates a blank taxonomies object if needed
$this->event_id = $data->event_id;
$this->post_id = $data->post_id;
if( EM_MS_GLOBAL && $this->is_ms_global && !is_main_site($data->blog_id) ){
$term_ids = $wpdb->get_col('SELECT meta_value FROM '.EM_META_TABLE." WHERE object_id='{$this->event_id}' AND meta_key='{$this->meta_key}'");
foreach($term_ids as $term_id){
$this->terms[$term_id] = new $this->term_class($term_id);
}
}else{
if( EM_MS_GLOBAL && (get_current_blog_id() !== $data->blog_id || (!$data->blog_id && !is_main_site()) ) ){
if( !$this->blog_id ) $this->blog_id = get_current_site()->blog_id;
switch_to_blog($this->blog_id);
$results = get_the_terms( $data->post_id, $this->taxonomy );
restore_current_blog();
}else{
$results = get_the_terms( $data->post_id, $this->taxonomy );
}
if( is_array($results) ){
foreach($results as $result){
$this->terms[$result->term_id] = new $this->term_class($result);
}
}
}
}elseif( is_array($data) && self::array_is_numeric($data) ){
foreach($data as $term_id){
$this->terms[$term_id] = new $this->term_class($term_id);
}
}elseif( is_array($data) ){
foreach( $data as $EM_Taxonomy_Term ){
if( get_class($EM_Taxonomy_Term) == $this->term_class){
$this->terms[] = $EM_Taxonomy_Term;
}
}
}
if( $this->is_ms_global ) self::ms_global_switch_back();
do_action('em_'.$this->terms_name, $this);
}
public function get_post(){
self::ms_global_switch();
$this->terms = array();
if(!empty($_POST['event_'.$this->terms_name]) && self::array_is_numeric($_POST['event_'.$this->terms_name])){
foreach( $_POST['event_'.$this->terms_name] as $term ){
$this->terms[$term] = new $this->term_class($term);
}
}
self::ms_global_switch_back();
do_action('em_'. $this->terms_name .'_get_post', $this);
}
public function save(){
if( empty($this->terms) ){
$EM_Taxonomy_Term = new $this->term_class();
$opt = 'dbem_default_'.$EM_Taxonomy_Term->option_name;
$default_category = EM_MS_GLOBAL && $this->is_ms_global ? get_blog_option( get_current_site()->blog_id, $opt ) : get_option($opt);
if( $default_category > 0 ){
$EM_Taxonomy_Term = new $this->term_class($default_category);
if( !empty($EM_Taxonomy_Term->slug) ){
$this->terms[] = $EM_Taxonomy_Term;
}
}
}
if( is_multisite() && $this->is_ms_global ){
//In MS Global mode, we also save taxonomy meta information for global lookups
if( EM_MS_GLOBAL && !empty($this->event_id) ){
//delete terms from event
$this->save_index();
}
if( !EM_MS_GLOBAL || is_main_site() ){
wp_set_object_terms($this->post_id, $this->get_slugs(), $this->taxonomy);
}
}else{
wp_set_object_terms($this->post_id, $this->get_slugs(), $this->taxonomy);
}
do_action('em_'. $this->terms_name .'_save', $this);
}
public function save_index(){
global $wpdb;
$wpdb->query('DELETE FROM '.EM_META_TABLE." WHERE object_id='{$this->event_id}' AND meta_key='{$this->meta_key}'");
foreach($this->terms as $EM_Taxonomy_Term){
$wpdb->insert(EM_META_TABLE, array('meta_value'=>$EM_Taxonomy_Term->term_id,'object_id'=>$this->event_id,'meta_key'=>$this->meta_key));
}
}
public function has( $search ){
if( is_numeric($search) ){
foreach($this->terms as $EM_Taxonomy_Term){
if($EM_Taxonomy_Term->term_id == $search) return apply_filters('em_'. $this->terms_name .'_has', true, $search, $this);
}
}else{
foreach($this->terms as $EM_Taxonomy_Term){
if($EM_Taxonomy_Term->slug == $search || $EM_Taxonomy_Term->name == $search ) return apply_filters('em_'. $this->terms_name .'_has', true, $search, $this);
}
}
return apply_filters('em_'. $this->terms_name .'_has', false, $search, $this);
}
public function get_first(){
foreach($this->terms as $EM_Taxonomy_Term){
return $EM_Taxonomy_Term;
}
return false;
}
public function get_ids(){
$ids = array();
foreach($this->terms as $EM_Taxonomy_Term){
if( !empty($EM_Taxonomy_Term->term_id) ){
$ids[] = $EM_Taxonomy_Term->term_id;
}
}
return $ids;
}
public function get_slugs(){
$term_slugs = array();
foreach( $this->terms as $EM_Taxonomy_Term ){ /* @var EM_Taxonomy_Term $EM_Taxonomy_Term */
$term_slugs[] = $EM_Taxonomy_Term->slug;
}
return $term_slugs;
}
/**
* Gets the event for this object, or a blank event if none exists
* @return EM_Event
*/
public function get_event(){
if( is_numeric($this->event_id) ){
return em_get_event($this->event_id);
}else{
return new EM_Event();
}
}
public static function get( $args = array() ) {
//Quick version, we can accept an array of IDs, which is easy to retrieve
self::ms_global_switch();
if( self::array_is_numeric($args) ){ //Array of numbers, assume they are taxonomy IDs to retreive
$term_args = self::get_default_search( array('include' => $args ));
$results = get_terms( $term_args );
}else{
//We assume it's either an empty array or array of search arguments to merge with defaults
$term_args = self::get_default_search($args);
$results = get_terms( $term_args );
//If we want results directly in an array, why not have a shortcut here? We don't use this in code, so if you're using it and filter the em_{taxonomy}_get hook, you may want to do this one too.
if( !empty($args['array']) ){
return apply_filters('em_'. self::$instance->terms_name .'_get_array', $results, $args);
}
}
//Make returned results EM_Taxonomy_Term child objects
$results = (is_array($results)) ? $results:array();
$terms = array();
foreach ( $results as $term ){
$terms[$term->term_id] = new self::$instance->term_class($term);
}
self::ms_global_switch_back();
return apply_filters('em_'. self::$instance->terms_name .'_get', $terms, $args);
}
public static function output( $args = array() ){
$EM_Taxonomy_Term_old = !empty($GLOBALS[self::$instance->term_class]) ? $GLOBALS[self::$instance->term_class] : false; //When looping, we can replace EM_Taxonomy_Term global with the current event in the loop
//get page number if passed on by request (still needs pagination enabled to have effect)
$page_queryvar = !empty($args['page_queryvar']) ? $args['page_queryvar'] : 'pno';
if( !array_key_exists('page',$args) && !empty($args['pagination']) && !empty($_REQUEST[$page_queryvar]) && is_numeric($_REQUEST[$page_queryvar]) ){
$page = $args['page'] = $_REQUEST[$page_queryvar];
}
//Can be either an array for the get search or an array of EM_Taxonomy_Term objects
if( is_object(current($args)) && get_class((current($args))) == self::$instance->term_class ){
$func_args = func_get_args();
$terms = $func_args[0];
$args = (!empty($func_args[1])) ? $func_args[1] : array();
$args = apply_filters('em_'. self::$instance->terms_name .'_output_args', self::get_default_search($args), $terms);
$limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false;
$offset = ( !empty($args['offset']) && is_numeric($args['offset']) ) ? $args['offset']:0;
$page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1;
}else{
$args = apply_filters('em_'. self::$instance->terms_name .'_output_args', self::get_default_search($args) );
$limit = ( !empty($args['limit']) && is_numeric($args['limit']) ) ? $args['limit']:false;
$offset = ( !empty($args['offset']) && is_numeric($args['offset']) ) ? $args['offset']:0;
$page = ( !empty($args['page']) && is_numeric($args['page']) ) ? $args['page']:1;
$args['limit'] = $args['offset'] = $args['page'] = false; //we count overall terms here
$terms = self::get( $args );
$args['limit'] = $limit;
$args['offset'] = $offset;
$args['page'] = $page;
}
//What format shall we output this to, or use default
$format = ( $args['format'] == '' ) ? get_option( 'dbem_'. self::$instance->terms_name .'_list_item_format' ) : $args['format'] ;
$output = "";
$terms_count = count($terms);
$terms = apply_filters('em_'. self::$instance->terms_name .'_output_'. self::$instance->terms_name, $terms);
if ( count($terms) > 0 ) {
$term_count = 0;
$terms_shown = 0;
foreach ( $terms as $EM_Taxonomy_Term ) {
$GLOBALS[self::$instance->term_class] = $EM_Taxonomy_Term;
if( ($terms_shown < $limit || empty($limit)) && ($term_count >= $offset || $offset === 0) ){
$output .= $EM_Taxonomy_Term->output($format);
$terms_shown++;
}
$term_count++;
}
//Add headers and footers to output
if( $format == get_option( 'dbem_'. self::$instance->terms_name .'_list_item_format' ) ){
//we're using the default format, so if a custom format header or footer is supplied, we can override it, if not use the default
$format_header = empty($args['format_header']) ? get_option('dbem_'. self::$instance->terms_name .'_list_item_format_header') : $args['format_header'];
$format_footer = empty($args['format_footer']) ? get_option('dbem_'. self::$instance->terms_name .'_list_item_format_footer') : $args['format_footer'];
}else{
//we're using a custom format, so if a header or footer isn't specifically supplied we assume it's blank
$format_header = !empty($args['format_header']) ? $args['format_header'] : '' ;
$format_footer = !empty($args['format_footer']) ? $args['format_footer'] : '' ;
}
$output = $format_header . $output . $format_footer;
//Pagination (if needed/requested)
if( !empty($args['pagination']) && !empty($limit) && $terms_count >= $limit ){
//Show the pagination links (unless there's less than 10 events, or the custom limit)
$output .= self::get_pagination_links($args, $terms_count);
}
} else {
$output = get_option ( 'dbem_no_'. self::$instance->terms_name .'_message' );
}
//FIXME check if reference is ok when restoring object, due to changes in php5 v 4
if( !empty($EM_Taxonomy_Term_old ) ) $GLOBALS[self::$instance->term_class] = $EM_Taxonomy_Term_old;
return apply_filters('em_'. self::$instance->terms_name .'_output', $output, $terms, $args);
}
public static function get_pagination_links($args, $count, $search_action = false, $default_args = array()){
//get default args if we're in a search, supply to parent since we can't depend on late static binding until WP requires PHP 5.3 or later
if( $search_action === false ) $search_action = self::$instance->ajax_search_action;
if( empty($default_args) && (!empty($args['ajax']) || !empty($_REQUEST['action']) && $_REQUEST['action'] == $search_action) ){
$default_args = self::get_default_search();
$default_args['limit'] = get_option('dbem_'. self::$instance->terms_name .'_default_limit');
}
return parent::get_pagination_links($args, $count, $search_action, $default_args);
}
public static function get_post_search($args = array(), $filter = false, $request = array(), $accepted_args = array()){
//supply $accepted_args to parent argument since we can't depend on late static binding until WP requires PHP 5.3 or later
$accepted_args = !empty($accepted_args) ? $accepted_args : array_keys(self::get_default_search());
return apply_filters('em_'. self::$instance->terms_name .'_get_post_search', parent::get_post_search($args, $filter, $request, $accepted_args));
}
/*
* Adds custom calendar search defaults
* @param array $array_or_defaults may be the array to override defaults
* @param array $array
* @return array
* @uses EM_Object#get_default_search()
*/
public static function get_default_search( $array_or_defaults = array(), $array = array() ){
$defaults = array(
//added from get_terms, so they don't get filtered out
'orderby' => get_option('dbem_'. self::$instance->terms_name .'_default_orderby'), 'order' => get_option('dbem_'. self::$instance->terms_name .'_default_order'),
'hide_empty' => false, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core'
);
//sort out whether defaults were supplied or just the array of search values
if( empty($array) ){
$array = $array_or_defaults;
}else{
$defaults = array_merge($defaults, $array_or_defaults);
}
$return = parent::get_default_search($defaults,$array);
$return['taxonomy'] = self::$instance->taxonomy; //this shouldn't change regardless
if( is_array($return['orderby']) ) $return['orderby'] = implode(',', $return['orderby']); //clean up for WP functions
return apply_filters('em_'. self::$instance->terms_name .'_get_default_search', $return, $array, $defaults);
}
//Iterator Implementation
#[\ReturnTypeWillChange]
public function rewind(){
reset($this->terms);
}
#[\ReturnTypeWillChange]
public function current(){
$var = current($this->terms);
return $var;
}
#[\ReturnTypeWillChange]
public function key(){
$var = key($this->terms);
return $var;
}
#[\ReturnTypeWillChange]
public function next(){
$var = next($this->terms);
return $var;
}
#[\ReturnTypeWillChange]
public function valid(){
$key = key($this->terms);
$var = ($key !== NULL && $key !== FALSE);
return $var;
}
#[\ReturnTypeWillChange]
public function count(){
return count($this->terms);
}
}