PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.5.2
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.5.2
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 All 177 releases
simple-tags / inc / class.client.tagcloud.php

class.client.tagcloud.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.5.2, at inc/class.client.tagcloud.php

959 lines 26.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_Client_TagCloud {
4 /**
5 * Init Embedded tag cloud
6 *
7 * SimpleTags_Client_TagCloud constructor.
8 */
9 public function __construct() {
10 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'allow_embed_tcloud' ) ) {
11 add_shortcode( 'st_tag_cloud', array( __CLASS__, 'shortcode' ) );
12 add_shortcode( 'st-tag-cloud', array( __CLASS__, 'shortcode' ) );
13 }
14 }
15
16 /**
17 * Replace marker by a tag cloud in post content, use ShortCode
18 *
19 * @param array $atts
20 *
21 * @return string
22 */
23 public static function shortcode( $atts ) {
24 $atts = shortcode_atts( array( 'param' => '' ), $atts );
25 extract( $atts );
26
27 $param = html_entity_decode( $param );
28 $param = trim( $param );
29
30 if ( empty( $param ) ) {
31 $param = 'title=';
32 }
33
34 return self::extendedTagCloud( $param, false );
35 }
36
37 /**
38 * Sort an array without accent for naturel order :)
39 *
40 * @param string $a
41 * @param string $b
42 *
43 * @return boolean
44 */
45 public static function uksort_by_name( $a = '', $b = '' ) {
46 return strnatcasecmp( remove_accents( $a ), remove_accents( $b ) );
47 }
48
49 /**
50 * Generate extended tag cloud
51 *
52 * @param string $args
53 *
54 * @return string|array
55 */
56 public static function extendedTagCloud( $args = '', $copyright = true ) {
57 $defaults = array(
58 // Simple Tag global options defaults
59 'selectionby' => 'count',
60 'selection' => 'desc',
61 'orderby' => 'random',
62 'order' => 'asc',
63 'format' => 'flat',
64 'xformat' => __( '<a href="%tag_link%" id="tag-link-%tag_id%" class="st-tags t%tag_scale%" title="%tag_count% topics" %tag_rel% style="%tag_size% %tag_color%">%tag_name%</a>', 'simple-tags' ),
65 'number' => 45,
66 'notagstext' => __( 'No tags.', 'simple-tags' ),
67 'title' => __( '<h4>Tag Cloud</h4>', 'simple-tags' ),
68 'maxcolor' => '#000000',
69 'mincolor' => '#CCCCCC',
70 'largest' => 22,
71 'smallest' => 8,
72 'unit' => 'pt',
73 'taxonomy' => 'post_tag', // Note: saved as an option but no UI to set it
74 // Simple Tag other defaults
75 'size' => 'true',
76 'color' => 'true',
77 'exclude' => '',
78 'include' => '',
79 'limit_days' => 0,
80 'min_usage' => 0,
81 'category' => 0,
82 'ID' => 0,
83 'hide_title' => 0,
84 'hide_output' => 0,
85 'post_type' => '',
86 'wrap_class' => '',
87 'link_class' => '',
88 );
89
90 // Get options
91 $options = SimpleTags_Plugin::get_option();
92
93 // Get values in DB
94 $defaults['selectionby'] = $options['cloud_selectionby'];
95 $defaults['selection'] = $options['cloud_selection'];
96 $defaults['orderby'] = $options['cloud_orderby'];
97 $defaults['order'] = $options['cloud_order'];
98 $defaults['format'] = $options['cloud_format'];
99 $defaults['xformat'] = $options['cloud_xformat'];
100 $defaults['number'] = $options['cloud_limit_qty'];
101 $defaults['notagstext'] = $options['cloud_notagstext'];
102 $defaults['title'] = $options['cloud_title'];
103 $defaults['maxcolor'] = $options['cloud_max_color'];
104 $defaults['mincolor'] = $options['cloud_min_color'];
105 $defaults['largest'] = $options['cloud_max_size'];
106 $defaults['smallest'] = $options['cloud_min_size'];
107 $defaults['unit'] = $options['cloud_unit'];
108 $defaults['taxonomy'] = $options['cloud_taxonomy'];
109
110 $adv_usage = $options['cloud_adv_usage'];
111 if ( empty( $args ) ) {
112 $args = $adv_usage;
113 } else {
114 $args = $adv_usage . "&" . $args;
115 }
116 $args = wp_parse_args( $args, $defaults );
117
118 // Add compatibility tips with old field syntax
119 if ( isset( $args['cloud_sort'] ) ) {
120 $args['cloud_order'] = $args['cloud_sort'];
121 unset( $args['cloud_sort'] );
122 }
123
124 // Translate selection order
125 if ( isset( $args['cloud_order'] ) ) {
126 $args['orderby'] = self::compatOldOrder( $args['cloud_order'], 'orderby' );
127 $args['order'] = self::compatOldOrder( $args['cloud_order'], 'order' );
128 }
129
130 // Category names to ID codes
131 if ( isset( $args['category'] ) ) {
132 $category = explode( ",", $args['category'] );
133 foreach ( $category as $key => $name ) {
134 $category[ $key ] = is_numeric( $name ) ? $name : get_cat_ID( $name );
135 if ( $category[ $key ] == 0 ) {
136 unset( $category[ $key ] );
137 }
138 }
139 $args['category'] = implode( ",", $category );
140 }
141
142 // Get correct taxonomy ?
143 $taxonomy = self::_get_current_taxonomy( $args['taxonomy'] );
144
145 // Get terms
146 $terms = self::getTags( $args, $taxonomy );
147 extract( $args ); // Params to variables
148
149 // If empty use default xformat !
150 if ( empty( $xformat ) ) {
151 $xformat = $defaults['xformat'];
152 }
153
154 //remove title if in settings
155 if((int)$hide_title > 0){
156 $title = '';
157 }
158
159 if ( empty( $terms ) ) {
160 if((int)$hide_output === 0){
161 return SimpleTags_Client::output_content( 'st-tag-cloud', $format, $title, $notagstext, $copyright, '', $wrap_class, $link_class );
162 }else{
163 return '';
164 }
165 }
166
167 $counts = $terms_data = array();
168 foreach ( (array) $terms as $term ) {
169 $counts[ $term->name ] = $term->count;
170 $terms_data[ $term->name ] = $term;
171 }
172
173 // Remove temp data from memory
174 $terms = array();
175 unset( $terms );
176
177 // Use full RBG code
178 if ( strlen( $maxcolor ) == 4 ) {
179 $maxcolor = $maxcolor . substr( $maxcolor, 1, strlen( $maxcolor ) );
180 }
181 if ( strlen( $mincolor ) == 4 ) {
182 $mincolor = $mincolor . substr( $mincolor, 1, strlen( $mincolor ) );
183 }
184
185 // Check as smallest inferior or egal to largest
186 if ( $smallest > $largest ) {
187 $smallest = $largest;
188 }
189
190 // Scaling - Hard value for the moment
191 $scale_min = 0;
192 $scale_max = 10;
193
194 $minval = min( $counts );
195 $maxval = max( $counts );
196
197 $minout = max( $scale_min, 0 );
198 $maxout = max( $scale_max, $minout );
199
200 $scale = ( $maxval > $minval ) ? ( ( $maxout - $minout ) / ( $maxval - $minval ) ) : 0;
201
202 // HTML Rel (tag/no-follow)
203 $rel = SimpleTags_Client::get_rel_attribut();
204
205 // Remove color marquer if color = false
206 if ( $color == 'false' ) {
207 $xformat = str_replace( '%tag_color%', '', $xformat );
208 }
209
210 // Remove size marquer if size = false
211 if ( $size == 'false' ) {
212 $xformat = str_replace( '%tag_size%', '', $xformat );
213 }
214
215 // Order terms before output
216 // count, name, rand | asc, desc
217
218 $orderby = strtolower( $orderby );
219 if ( $orderby == 'count' ) {
220 asort( $counts );
221 } elseif ( $orderby == 'name' ) {
222 uksort( $counts, array( __CLASS__, 'uksort_by_name' ) );
223 } else { // rand
224 SimpleTags_Client::random_array( $counts );
225 }
226
227 $order = strtolower( $order );
228 if ( $order == 'desc' && $orderby != 'random' ) {
229 $counts = array_reverse( $counts );
230 }
231
232 $output = array();
233
234 //update xformat with class link class
235 if(!empty(trim($link_class))){
236 $link_class = taxopress_format_class($link_class);
237 $xformat = taxopress_add_class_to_format($xformat, $link_class);
238 }
239
240 foreach ( (array) $counts as $term_name => $count ) {
241 if ( ! is_object( $terms_data[ $term_name ] ) ) {
242 continue;
243 }
244
245 $term = $terms_data[ $term_name ];
246 $scale_result = (int) ( $scale <> 0 ? ( ( $term->count - $minval ) * $scale + $minout ) : ( $scale_max - $scale_min ) / 2 );
247 $output[] = SimpleTags_Client::format_internal_tag( $xformat, $term, $rel, $scale_result, $scale_max, $scale_min, $largest, $smallest, $unit, $maxcolor, $mincolor );
248 }
249
250 return SimpleTags_Client::output_content( 'st-tag-cloud', $format, $title, $output, $copyright, '', $wrap_class, $link_class );
251 }
252
253
254 /**
255 * Generate extended tag result
256 *
257 * @param string $args
258 *
259 * @return array
260 */
261 public static function extendedTagResult( $args = '', $copyright = true ) {
262 $defaults = array(
263 // Simple Tag global options defaults
264 'selectionby' => 'count',
265 'selection' => 'desc',
266 'orderby' => 'random',
267 'order' => 'asc',
268 'format' => 'flat',
269 'xformat' => __( '<a href="%tag_link%" id="tag-link-%tag_id%" class="st-tags t%tag_scale%" title="%tag_count% topics" %tag_rel% style="%tag_size% %tag_color%">%tag_name%</a>', 'simple-tags' ),
270 'number' => 45,
271 'notagstext' => __( 'No tags.', 'simple-tags' ),
272 'title' => __( '<h4>Tag Cloud</h4>', 'simple-tags' ),
273 'maxcolor' => '#000000',
274 'mincolor' => '#CCCCCC',
275 'largest' => 22,
276 'smallest' => 8,
277 'unit' => 'pt',
278 'taxonomy' => 'post_tag', // Note: saved as an option but no UI to set it
279 // Simple Tag other defaults
280 'size' => 'true',
281 'color' => 'true',
282 'exclude' => '',
283 'include' => '',
284 'limit_days' => 0,
285 'min_usage' => 0,
286 'category' => 0
287 );
288
289 // Get options
290 $options = SimpleTags_Plugin::get_option();
291
292 // Get values in DB
293 $defaults['selectionby'] = $options['cloud_selectionby'];
294 $defaults['selection'] = $options['cloud_selection'];
295 $defaults['orderby'] = $options['cloud_orderby'];
296 $defaults['order'] = $options['cloud_order'];
297 $defaults['format'] = $options['cloud_format'];
298 $defaults['xformat'] = $options['cloud_xformat'];
299 $defaults['number'] = $options['cloud_limit_qty'];
300 $defaults['notagstext'] = $options['cloud_notagstext'];
301 $defaults['title'] = $options['cloud_title'];
302 $defaults['maxcolor'] = $options['cloud_max_color'];
303 $defaults['mincolor'] = $options['cloud_min_color'];
304 $defaults['largest'] = $options['cloud_max_size'];
305 $defaults['smallest'] = $options['cloud_min_size'];
306 $defaults['unit'] = $options['cloud_unit'];
307 $defaults['taxonomy'] = $options['cloud_taxonomy'];
308
309 $adv_usage = $options['cloud_adv_usage'];
310 if ( empty( $args ) ) {
311 $args = $adv_usage;
312 } else {
313 $args = $adv_usage . "&" . $args;
314 }
315 $args = wp_parse_args( $args, $defaults );
316
317 // Add compatibility tips with old field syntax
318 if ( isset( $args['cloud_sort'] ) ) {
319 $args['cloud_order'] = $args['cloud_sort'];
320 unset( $args['cloud_sort'] );
321 }
322
323 // Translate selection order
324 if ( isset( $args['cloud_order'] ) ) {
325 $args['orderby'] = self::compatOldOrder( $args['cloud_order'], 'orderby' );
326 $args['order'] = self::compatOldOrder( $args['cloud_order'], 'order' );
327 }
328
329 // Category names to ID codes
330 if ( isset( $args['category'] ) ) {
331 $category = explode( ",", $args['category'] );
332 foreach ( $category as $key => $name ) {
333 $category[ $key ] = is_numeric( $name ) ? $name : get_cat_ID( $name );
334 if ( $category[ $key ] == 0 ) {
335 unset( $category[ $key ] );
336 }
337 }
338 $args['category'] = implode( ",", $category );
339 }
340
341 // Get correct taxonomy ?
342 $taxonomy = self::_get_current_taxonomy( $args['taxonomy'] );
343
344 // Get terms
345 $terms = self::getTags( $args, $taxonomy );
346 extract( $args ); // Params to variables
347
348 // If empty use default xformat !
349 if ( empty( $xformat ) ) {
350 $xformat = $defaults['xformat'];
351 }
352
353 if ( empty( $terms ) ) {
354 return [];
355 }
356
357 $counts = $terms_data = array();
358 foreach ( (array) $terms as $term ) {
359 $counts[ $term->name ] = $term->count;
360 $terms_data[ $term->name ] = $term;
361 }
362
363 // Remove temp data from memory
364 $terms = array();
365 unset( $terms );
366
367 // Use full RBG code
368 if ( strlen( $maxcolor ) == 4 ) {
369 $maxcolor = $maxcolor . substr( $maxcolor, 1, strlen( $maxcolor ) );
370 }
371 if ( strlen( $mincolor ) == 4 ) {
372 $mincolor = $mincolor . substr( $mincolor, 1, strlen( $mincolor ) );
373 }
374
375 // Check as smallest inferior or egal to largest
376 if ( $smallest > $largest ) {
377 $smallest = $largest;
378 }
379
380 // Scaling - Hard value for the moment
381 $scale_min = 0;
382 $scale_max = 10;
383
384 $minval = min( $counts );
385 $maxval = max( $counts );
386
387 $minout = max( $scale_min, 0 );
388 $maxout = max( $scale_max, $minout );
389
390 $scale = ( $maxval > $minval ) ? ( ( $maxout - $minout ) / ( $maxval - $minval ) ) : 0;
391
392 // HTML Rel (tag/no-follow)
393 $rel = SimpleTags_Client::get_rel_attribut();
394
395 // Remove color marquer if color = false
396 if ( $color == 'false' ) {
397 $xformat = str_replace( '%tag_color%', '', $xformat );
398 }
399
400 // Remove size marquer if size = false
401 if ( $size == 'false' ) {
402 $xformat = str_replace( '%tag_size%', '', $xformat );
403 }
404
405 // Order terms before output
406 // count, name, rand | asc, desc
407
408 $orderby = strtolower( $orderby );
409 if ( $orderby == 'count' ) {
410 asort( $counts );
411 } elseif ( $orderby == 'name' ) {
412 uksort( $counts, array( __CLASS__, 'uksort_by_name' ) );
413 } else { // rand
414 SimpleTags_Client::random_array( $counts );
415 }
416
417 $order = strtolower( $order );
418 if ( $order == 'desc' && $orderby != 'random' ) {
419 $counts = array_reverse( $counts );
420 }
421
422 $output = array();
423 foreach ( (array) $counts as $term_name => $count ) {
424 if ( ! is_object( $terms_data[ $term_name ] ) ) {
425 continue;
426 }
427 $output[] = $terms_data[ $term_name ];
428 }
429
430
431 return $output;
432 }
433
434 /**
435 * Check if taxonomy exist and return it, otherwise return default post tags.
436 *
437 * @param string|array $taxonomies
438 * @param boolean $force_single
439 *
440 * @return array|string
441 * @author WebFactory Ltd
442 */
443 public static function _get_current_taxonomy( $taxonomies, $force_single = false ) {
444 if ( is_array( $taxonomies ) ) {
445 foreach ( $taxonomies as $key => $value ) {
446 if ( ! taxonomy_exists( $value ) ) { // Remove from array is taxonomy not exist !
447 unset( $taxonomies[ $key ] );
448 } elseif ( true === $force_single ) {// Force single instead array ?
449 return $value;
450 }
451 }
452
453 return $taxonomies;
454 } elseif ( taxonomy_exists( $taxonomies ) ) {
455 return $taxonomies;
456 }
457
458 return 'post_tag';
459 }
460
461 /**
462 * Extended get_tags public static function that use getTerms function
463 *
464 * @param string|array $args
465 * @param string|array $taxonomy
466 *
467 * @return array
468 */
469 public static function getTags( $args = '', $taxonomy = 'post_tag' ) {
470 $key = md5( maybe_serialize( $args ) . $taxonomy );
471
472 // Get cache if exist
473 if ( $cache = wp_cache_get( 'st_get_tags', 'simple-tags' ) ) {
474 if ( isset( $cache[ $key ] ) ) {
475 return apply_filters( 'get_tags', $cache[ $key ], $args );
476 }
477 }
478
479 // Get tags
480 $terms = self::getTerms( $taxonomy, $args );
481 if ( empty( $terms ) ) {
482 return array();
483 }
484
485 $cache[ $key ] = $terms;
486 wp_cache_set( 'st_get_tags', $cache, 'simple-tags' );
487
488 $terms = apply_filters( 'st_get_tags', $terms, $args );
489
490 return $terms;
491 }
492
493 /**
494 * Helper public static function for keep compatibility with old options TaxoPress widgets
495 *
496 * @param string $old_value
497 * @param string $key
498 *
499 * @return string
500 * @author WebFactory Ltd
501 */
502 public static function compatOldOrder( $old_value = '', $key = '' ) {
503 $return = array();
504
505 switch ( strtolower( $old_value ) ) { // count-asc/count-desc/name-asc/name-desc/random
506 case 'count-asc':
507 $return = array( 'orderby' => 'count', 'order' => 'ASC' );
508 break;
509 case 'rand':
510 case 'random':
511 $return = array( 'orderby' => 'random', 'order' => '' );
512 break;
513 case 'name-asc':
514 $return = array( 'orderby' => 'name', 'order' => 'ASC' );
515 break;
516 case 'name-desc':
517 $return = array( 'orderby' => 'name', 'order' => 'DESC' );
518 break;
519 case 'count-desc':
520 $return = array( 'orderby' => 'count', 'order' => 'DESC' );
521 break;
522 }
523
524 if ( empty( $return ) || ! isset( $return[ $key ] ) ) {
525 return $old_value;
526 }
527
528 return $return[ $key ];
529 }
530
531 /**
532 * Extended get_terms public static function support
533 * - Limit category
534 * - Limit days
535 * - Selection restrict
536 * - Min usage
537 *
538 * @param string|array $taxonomies
539 * @param string $args
540 *
541 * @return array|WP_Error
542 */
543 public static function getTerms( $taxonomies, $args = '' ) {
544 global $wpdb;
545 $empty_array = array();
546 $join_relation = false;
547
548 $single_taxonomy = false;
549 if ( ! is_array( $taxonomies ) ) {
550 $single_taxonomy = true;
551 $taxonomies = array( $taxonomies );
552 }
553
554 foreach ( (array) $taxonomies as $taxonomy ) {
555 if ( ! taxonomy_exists( $taxonomy ) ) {
556 $error = new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
557
558 return $error;
559 }
560 }
561 $in_taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
562
563 $defaults = array(
564 'orderby' => 'name',
565 'order' => 'ASC',
566 'hide_empty' => true,
567 'exclude' => array(),
568 'exclude_tree' => array(),
569 'include' => array(),
570 'number' => '',
571 'fields' => 'all',
572 'slug' => '',
573 'parent' => '',
574 'hierarchical' => true,
575 'child_of' => 0,
576 'get' => '',
577 'name__like' => '',
578 'pad_counts' => false,
579 'offset' => '',
580 'search' => '',
581 // TaxoPress added
582 'limit_days' => 0,
583 'category' => 0,
584 'min_usage' => 0,
585 'st_name__like' => '',
586 'post_type' => false
587 );
588
589 $args = wp_parse_args( $args, $defaults );
590
591 // Translate selection order
592 $args['orderby'] = self::compatOldOrder( $args['selectionby'], 'orderby' );
593 $args['order'] = self::compatOldOrder( $args['selection'], 'order' );
594
595 $args['number'] = absint( $args['number'] );
596 $args['offset'] = absint( $args['offset'] );
597 $args['limit_days'] = absint( $args['limit_days'] );
598 $args['min_usage'] = absint( $args['min_usage'] );
599
600 if ( ! $single_taxonomy || ! is_taxonomy_hierarchical( $taxonomies[0] ) ||
601 '' !== $args['parent']
602 ) {
603 $args['child_of'] = 0;
604 $args['hierarchical'] = false;
605 $args['pad_counts'] = false;
606 }
607
608 if ( 'all' == $args['get'] ) {
609 $args['child_of'] = 0;
610 $args['hide_empty'] = 0;
611 $args['hierarchical'] = false;
612 $args['pad_counts'] = false;
613 }
614 extract( $args, EXTR_SKIP );
615
616 if ( $child_of ) {
617 $hierarchy = _get_term_hierarchy( $taxonomies[0] );
618 if ( ! isset( $hierarchy[ $child_of ] ) ) {
619 return $empty_array;
620 }
621 }
622
623 if ( $parent ) {
624 $hierarchy = _get_term_hierarchy( $taxonomies[0] );
625 if ( ! isset( $hierarchy[ $parent ] ) ) {
626 return $empty_array;
627 }
628 }
629
630 // $args can be whatever, only use the args defined in defaults to compute the key
631 $filter_key = ( has_filter( 'list_terms_exclusions' ) ) ? serialize( $GLOBALS['wp_filter']['list_terms_exclusions'] ) : '';
632 $key = md5( serialize( compact( array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
633 $last_changed = wp_cache_get( 'last_changed', 's-terms' );
634 if ( ! $last_changed ) {
635 $last_changed = time();
636 wp_cache_set( 'last_changed', $last_changed, 's-terms' );
637 }
638 $cache_key = "get_terms:$key:$last_changed";
639 $cache = wp_cache_get( $cache_key, 's-terms' );
640 if ( false !== $cache ) {
641 $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args );
642
643 return $cache;
644 }
645
646 $_orderby = strtolower( $orderby );
647 if ( 'count' == $_orderby ) {
648 $orderby = 'tt.count';
649 }
650 if ( 'random' == $_orderby ) {
651 $orderby = 'RAND()';
652 } else if ( 'name' == $_orderby ) {
653 $orderby = 't.name';
654 } else if ( 'slug' == $_orderby ) {
655 $orderby = 't.slug';
656 } else if ( 'term_group' == $_orderby ) {
657 $orderby = 't.term_group';
658 } elseif ( empty( $_orderby ) || 'id' == $_orderby ) {
659 $orderby = 't.term_id';
660 }
661 $orderby = apply_filters( 'get_terms_orderby', $orderby, $args );
662
663 if ( ! empty( $orderby ) ) {
664 $orderby = "ORDER BY $orderby";
665 } else {
666 $order = '';
667 }
668
669 $where = '';
670 $inclusions = '';
671 if ( ! empty( $include ) ) {
672 $exclude = '';
673 $exclude_tree = '';
674 $interms = wp_parse_id_list( $include );
675 foreach ( $interms as $interm ) {
676 if ( empty( $inclusions ) ) {
677 $inclusions = ' AND ( t.term_id = ' . intval( $interm ) . ' ';
678 } else {
679 $inclusions .= ' OR t.term_id = ' . intval( $interm ) . ' ';
680 }
681 }
682 }
683
684 if ( ! empty( $inclusions ) ) {
685 $inclusions .= ')';
686 }
687 $where .= $inclusions;
688
689 $exclusions = '';
690 if ( ! empty( $exclude_tree ) ) {
691 $excluded_trunks = wp_parse_id_list( $exclude_tree );
692 foreach ( $excluded_trunks as $extrunk ) {
693 $excluded_children = (array) get_terms( $taxonomies[0], array(
694 'child_of' => intval( $extrunk ),
695 'fields' => 'ids'
696 ) );
697 $excluded_children[] = $extrunk;
698 foreach ( $excluded_children as $exterm ) {
699 if ( empty( $exclusions ) ) {
700 $exclusions = ' AND ( t.term_id <> ' . intval( $exterm ) . ' ';
701 } else {
702 $exclusions .= ' AND t.term_id <> ' . intval( $exterm ) . ' ';
703 }
704 }
705 }
706 }
707
708 if ( ! empty( $exclude ) ) {
709 $exterms = wp_parse_id_list( $exclude );
710 foreach ( $exterms as $exterm ) {
711 if ( empty( $exclusions ) ) {
712 $exclusions = ' AND ( t.term_id <> ' . intval( $exterm ) . ' ';
713 } else {
714 $exclusions .= ' AND t.term_id <> ' . intval( $exterm ) . ' ';
715 }
716 }
717 }
718
719 if ( ! empty( $exclusions ) ) {
720 $exclusions .= ')';
721 }
722 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args );
723 $where .= $exclusions;
724
725 // ST Features : Restrict category
726 if (!empty($category)) {
727 if ( ! is_array( $taxonomies ) ) {
728 $taxonomies = array( $taxonomies );
729 }
730
731 $incategories = wp_parse_id_list( $category );
732
733 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
734 $incategories = "'" . implode( "', '", $incategories ) . "'";
735
736 $where .= " AND tr.object_id IN ( ";
737 $where .= "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts as p ON tr.object_id=p.ID WHERE tt.term_id IN ($incategories) AND p.post_status='publish'";
738 $where .= " ) ";
739 $join_relation = true;
740 unset( $incategories, $category );
741 }
742
743 // ST Features : Limit posts date
744 if ( (int)$limit_days > 0 ) {
745 if($post_type){
746 $post_type = "AND post_type = '$post_type'";
747 }else{
748 $post_type = '';
749 }
750 $where .= " AND tr.object_id IN ( ";
751 $where .= "SELECT DISTINCT ID FROM $wpdb->posts AS p WHERE p.post_date_gmt > '" . date( 'Y-m-d H:i:s', time() - $limit_days * 86400 ) . "' $post_type";
752 $where .= " ) ";
753 $join_relation = true;
754 unset( $limit_days );
755 }else{
756 if($post_type){
757 $where .= " AND tr.object_id IN ( ";
758 $where .= "SELECT DISTINCT ID FROM $wpdb->posts AS p WHERE post_type = '$post_type'";
759 $where .= " ) ";
760 $join_relation = true;
761 }
762 }
763
764 if ( ! empty( $slug ) ) {
765 $slug = sanitize_title( $slug );
766 $where .= " AND t.slug = '$slug'";
767 }
768
769 if ( ! empty( $name__like ) ) {
770 $where .= " AND t.name LIKE '{$name__like}%'";
771 }
772
773 if ( '' !== $parent ) {
774 $parent = (int) $parent;
775 $where .= " AND tt.parent = '$parent'";
776 }
777
778 // ST Features : Another way to search
779 if ( strpos( $st_name__like, ' ' ) !== false ) {
780
781 $st_terms_formatted = array();
782 $st_terms = preg_split( '/[\s,]+/', $st_name_like );
783 foreach ( (array) $st_terms as $st_term ) {
784 if ( empty( $st_term ) ) {
785 continue;
786 }
787 $st_terms_formatted[] = "t.name LIKE '%" . like_escape( $st_term ) . "%'";
788 }
789
790 $where .= " AND ( " . explode( ' OR ', $st_terms_formatted ) . " ) ";
791 unset( $st_term, $st_terms_formatted, $st_terms );
792
793 } elseif ( ! empty( $st_name__like ) ) {
794
795 $where .= " AND t.name LIKE '%{$st_name__like}%'";
796
797 }
798
799 if(in_array($taxonomies[0], ['post_tag', 'category'])){
800 //TODO - count not working for attachment and/or CPT ?
801 // ST Features : Add min usage
802 if ( $hide_empty && ! $hierarchical ) {
803 if ( $min_usage == 0 ) {
804 $where .= ' AND tt.count > 0';
805 } else {
806 $where .= $wpdb->prepare( ' AND tt.count >= %d', $min_usage );
807 }
808 }
809 }
810
811
812 if ( ! empty( $search ) ) {
813 $search = like_escape( $search );
814 $where .= " AND (t.name LIKE '%$search%')";
815 }
816
817 $selects = array();
818 switch ( $fields ) {
819 case 'all':
820 $selects = array( 't.*', 'tt.*' );
821 break;
822 case 'ids':
823 case 'id=>parent':
824 $selects = array( 't.term_id', 'tt.parent', 'tt.count' );
825 break;
826 case 'names':
827 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
828 break;
829 case 'count':
830 $orderby = '';
831 $order = '';
832 $selects = array( 'COUNT(*)' );
833 }
834 $select_this = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args ) );
835
836 // Add inner to relation table ?
837 $join_relation = $join_relation == false ? '' : "INNER JOIN $wpdb->term_relationships AS tr ON tt.term_taxonomy_id = tr.term_taxonomy_id";
838 // Query parts are individually escaped above, $taxonomies are individually checked if they exists
839
840
841 // don't limit the query results when we have to descend the family tree
842 if ( ! empty( $number ) && ! $hierarchical && empty( $child_of ) && '' == $parent ) {
843 if ( $offset ) {
844 $query = $wpdb->prepare("SELECT $select_this
845 FROM $wpdb->terms AS t
846 INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
847 $join_relation
848 WHERE 1 = %d AND tt.taxonomy IN ($in_taxonomies)
849 $where
850 $orderby $order
851 LIMIT %d, %d",
852 1,
853 $offset,
854 $number );
855 } else {
856 $query = $wpdb->prepare("SELECT $select_this
857 FROM $wpdb->terms AS t
858 INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
859 $join_relation
860 WHERE 1 = %d AND tt.taxonomy IN ($in_taxonomies)
861 $where
862 $orderby $order
863 LIMIT %d",
864 1,
865 $number );
866 }
867 } else {
868 $query = $wpdb->prepare("SELECT $select_this
869 FROM $wpdb->terms AS t
870 INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
871 $join_relation
872 WHERE 1 = %d AND tt.taxonomy IN ($in_taxonomies)
873 $where
874 $orderby $order",
875 1 );
876 }
877
878 // GROUP BY t.term_id
879
880 if ( 'count' == $fields ) {
881 $term_count = $wpdb->get_var( $query );
882
883 return $term_count;
884 }
885
886 $terms = $wpdb->get_results( $query );
887 if ( 'all' == $fields ) {
888 update_term_cache( $terms );
889 }
890
891 if ( empty( $terms ) ) {
892 wp_cache_add( $cache_key, array(), 's-terms' );
893 $terms = apply_filters( 'get_terms', array(), $taxonomies, $args );
894
895 return $terms;
896 }
897
898 if ( $child_of ) {
899 $children = _get_term_hierarchy( $taxonomies[0] );
900 if ( ! empty( $children ) ) {
901 $terms = &_get_term_children( $child_of, $terms, $taxonomies[0] );
902 }
903 }
904
905 // Update term counts to include children.
906 if ( $pad_counts && 'all' == $fields ) {
907 _pad_term_counts( $terms, $taxonomies[0] );
908 }
909
910 // Make sure we show empty categories that have children.
911 if ( $hierarchical && $hide_empty && is_array( $terms ) ) {
912 foreach ( $terms as $k => $term ) {
913 if ( ! $term->count ) {
914 $children = _get_term_children( $term->term_id, $terms, $taxonomies[0] );
915 if ( is_array( $children ) ) {
916 foreach ( $children as $child ) {
917 if ( $child->count ) {
918 continue 2;
919 }
920 }
921 }
922
923 // It really is empty
924 unset( $terms[ $k ] );
925 }
926 }
927 }
928 reset( $terms );
929
930 $_terms = array();
931 if ( 'id=>parent' == $fields ) {
932 while ( $term = array_shift( $terms ) ) {
933 $_terms[ $term->term_id ] = $term->parent;
934 }
935 $terms = $_terms;
936 } elseif ( 'ids' == $fields ) {
937 while ( $term = array_shift( $terms ) ) {
938 $_terms[] = $term->term_id;
939 }
940 $terms = $_terms;
941 } elseif ( 'names' == $fields ) {
942 while ( $term = array_shift( $terms ) ) {
943 $_terms[] = $term->name;
944 }
945 $terms = $_terms;
946 }
947
948 if ( 0 < $number && intval( @count( $terms ) ) > $number ) {
949 $terms = array_slice( $terms, $offset, $number );
950 }
951
952 wp_cache_add( $cache_key, $terms, 's-terms' );
953
954 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
955
956 return $terms;
957 }
958 }
959