source: trunk/www.guidonia.net/wp/wp-content/plugins/simple-tags/2.3/simple-tags.client.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 61.6 KB
Line 
1<?php
2Class SimpleTags {
3 var $version = '1.5.6 - 1.3.9.5';
4
5 var $info;
6 var $options;
7 var $default_options;
8 var $db_options = 'simpletags';
9 var $dateformat;
10
11 // Stock Post ID for current view
12 var $posts = array();
13 var $tags_currentposts = array();
14 var $link_tags = 'null';
15
16 // WP Object Cache
17 var $use_cache = false;
18
19 /**
20 * PHP4 constructor - Initialize ST
21 *
22 * @return SimpleTags
23 */
24 function SimpleTags() {
25 // Determine installation path & url
26 $path = str_replace('\\','/',dirname(__FILE__));
27 $path = substr($path, strpos($path, 'plugins') + 8, strlen($path));
28
29 $info['siteurl'] = get_option('siteurl');
30 if ( $this->isMuPlugin() ) {
31 $info['install_url'] = $info['siteurl'] . '/wp-content/mu-plugins';
32 $info['install_dir'] = ABSPATH . 'wp-content/mu-plugins';
33
34 if ( $path != 'mu-plugins' ) {
35 $info['install_url'] .= '/' . $path;
36 $info['install_dir'] .= '/' . $path;
37 }
38 } else {
39 $info['install_url'] = $info['siteurl'] . '/wp-content/plugins';
40 $info['install_dir'] = ABSPATH . 'wp-content/plugins';
41
42 if ( $path != 'plugins' ) {
43 $info['install_url'] .= '/' . $path;
44 $info['install_dir'] .= '/' . $path;
45 }
46 }
47
48 // Set informations
49 $this->info = array(
50 'home' => get_option('home'),
51 'siteurl' => $info['siteurl'],
52 'install_url' => $info['install_url'],
53 'install_dir' => $info['install_dir']
54 );
55 unset($info);
56
57 // Localization
58 $locale = get_locale();
59 if ( !empty( $locale ) ) {
60 $mofile = str_replace('/2.3', '', $this->info['install_dir']).'/languages/simpletags-'.$locale.'.mo';
61 load_textdomain('simpletags', $mofile);
62 }
63
64 // Options
65 $default_options = array(
66 // General
67 'inc_page_tag_search' => 1,
68 'allow_embed_tcloud' => 0,
69 'auto_link_tags' => 0,
70 'auto_link_min' => 1,
71 'auto_link_case' => 1,
72 'no_follow' => 0,
73 // Administration
74 'use_tag_pages' => 1,
75 'use_click_tags' => 1,
76 'use_suggested_tags' => 1,
77 'use_autocompletion' => 1,
78 // Embedded Tags
79 'use_embed_tags' => 0,
80 'start_embed_tags' => '[tags]',
81 'end_embed_tags' => '[/tags]',
82 // Related Posts
83 'rp_feed' => 0,
84 'rp_embedded' => 'no',
85 'rp_order' => 'count-desc',
86 'rp_limit_qty' => 5,
87 'rp_notagstext' => __('No related posts.', 'simpletags'),
88 'rp_title' => __('<h4>Related posts</h4>', 'simpletags'),
89 'rp_xformat' => __('<a href="%post_permalink%" title="%post_title% (%post_date%)">%post_title%</a> (%post_comment%)', 'simpletags'),
90 'rp_adv_usage' => '',
91 // Tag cloud
92 'cloud_selection' => 'count-desc',
93 'cloud_sort' => 'random',
94 'cloud_limit_qty' => 45,
95 'cloud_notagstext' => __('No tags.', 'simpletags'),
96 'cloud_title' => __('<h4>Tag Cloud</h4>', 'simpletags'),
97 'cloud_format' => 'flat',
98 'cloud_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>', 'simpletags'),
99 'cloud_max_color' => '#000000',
100 'cloud_min_color' => '#CCCCCC',
101 'cloud_max_size' => 22,
102 'cloud_min_size' => 8,
103 'cloud_unit' => 'pt',
104 'cloud_inc_cats' => 0,
105 'cloud_adv_usage' => '',
106 // The tags
107 'tt_feed' => 0,
108 'tt_embedded' => 'no',
109 'tt_separator' => ', ',
110 'tt_before' => __('Tags: ', 'simpletags'),
111 'tt_after' => '<br />',
112 'tt_notagstext' => __('No tag for this post.', 'simpletags'),
113 'tt_number' => 0,
114 'tt_inc_cats' => 0,
115 'tt_xformat' => __('<a href="%tag_link%" title="%tag_name%" %tag_rel%>%tag_name%</a>', 'simpletags'),
116 'tt_adv_usage' => '',
117 // Related tags
118 'rt_number' => 5,
119 'rt_order' => 'count-desc',
120 'rt_separator' => ' ',
121 'rt_format' => 'list',
122 'rt_method' => 'OR',
123 'rt_title' => __('<h4>Related tags</h4>', 'simpletags'),
124 'rt_notagstext' => __('No related tag found.', 'simpletags'),
125 'rt_xformat' => __('<span>%tag_count%</span> <a href="%tag_link_add%">+</a> <a href="%tag_link%">%tag_name%</a>', 'simpletags'),
126 // Remove related tags
127 'rt_remove_separator' => ' ',
128 'rt_remove_format' => 'list',
129 'rt_remove_notagstext' => ' ',
130 'rt_remove_xformat' => __('&raquo; <a href="%tag_link_remove%" title="Remove %tag_name_attribute% from search">Remove %tag_name%</a>', 'simpletags'),
131 // Meta keywords
132 'meta_autoheader' => 1,
133 'meta_always_include' => '',
134 'meta_keywords_qty' => 0,
135 // Auto tags
136 'use_auto_tags' => 0,
137 'at_all' => 0,
138 'at_empty' => 0,
139 'auto_list' => ''
140 );
141
142 // Set class property for default options
143 $this->default_options = $default_options;
144
145 // Get options from WP options
146 $options_from_table = get_option( $this->db_options );
147
148 // Update default options by getting not empty values from options table
149 foreach( (array) $default_options as $default_options_name => $default_options_value ) {
150 if ( !is_null($options_from_table[$default_options_name]) ) {
151 if ( is_int($default_options_value) ) {
152 $default_options[$default_options_name] = (int) $options_from_table[$default_options_name];
153 } else {
154 $default_options[$default_options_name] = $options_from_table[$default_options_name];
155 }
156 }
157 }
158
159 // Set the class property and unset no used variable
160 $this->options = $default_options;
161 unset($default_options);
162 unset($options_from_table);
163 unset($default_options_value);
164
165 // Use WP Object ? Or not ?
166 global $wp_object_cache;
167 $this->use_cache = ( $wp_object_cache->cache_enabled === true ) ? true : false;
168
169 // Set date for class
170 $this->dateformat = get_option('date_format');
171
172 // Add pages in WP_Query
173 if ( $this->options['use_tag_pages'] == 1 ) {
174 // Remove default taxonomy
175 global $wp_taxonomies;
176 unset($wp_taxonomies['post_tag']);
177 // Add the same taxonomy with an another callback who allow page and post
178 register_taxonomy( 'post_tag', 'post', array('hierarchical' => false, 'update_count_callback' => array(&$this, '_update_post_and_page_term_count')) );
179 add_filter('posts_where', array(&$this, 'prepareQuery'));
180 }
181
182 // Remove embedded tags in posts display
183 if ( $this->options['use_embed_tags'] == 1 ) {
184 add_filter('the_content', array(&$this, 'filterEmbedTags'), 0);
185 }
186
187 // Add related posts in post ( all / feedonly / blogonly / homeonly / singularonly / singleonly / pageonly /no )
188 if ( $this->options['tt_embedded'] != 'no' || $this->options['tt_feed'] == 1 ) {
189 add_filter('the_content', array(&$this, 'inlinePostTags'), 999992);
190 }
191
192 // Add post tags in post ( all / feedonly / blogonly / homeonly / singularonly / singleonly / pageonly /no )
193 if ( $this->options['rp_embedded'] != 'no' || $this->options['rp_feed'] == 1 ) {
194 add_filter('the_content', array(&$this, 'inlineRelatedPosts'), 999993);
195 }
196
197 // Embedded tag cloud
198 if ( $this->options['allow_embed_tcloud'] == 1 ) {
199 add_filter('the_content', array(&$this, 'inlineTagCloud'));
200 }
201
202 // Stock Posts ID (useful for autolink and metakeywords)
203 add_filter( 'the_posts', array(&$this, 'getPostIds') );
204
205 // Add keywords to header
206 if ( ( $this->options['meta_autoheader'] == 1 && !class_exists('All_in_One_SEO_Pack') && apply_filters('st_meta_header', true) ) ) {
207 add_action('wp_head', array(&$this, 'displayMetaKeywords'));
208 }
209
210 // Auto link tags
211 if ( $this->options['auto_link_tags'] == '1' ) {
212 add_filter('the_content', array(&$this, 'autoLinkTags'), 9);
213 }
214 return true;
215 }
216
217 /**
218 * Get links for each tag for auto link feature
219 *
220 */
221 function prepareAutoLinkTags() {
222 $this->getTagsFromCurrentPosts();
223
224 $auto_link_min = (int) $this->options['auto_link_min'];
225 if ( $auto_link_min == 0 ) {
226 $auto_link_min = 1;
227 }
228
229 $this->link_tags = array();
230 foreach ( (array) $this->tags_currentposts as $term ) {
231 if ( $term->count >= $auto_link_min ) {
232 $this->link_tags[$term->name] = clean_url(get_tag_link( $term->term_id ));
233 }
234 }
235 return;
236 }
237
238 function autoLinkTags( $content = '' ) {
239 // ST Prefix/Suffix content
240 $content = '<div>'.$content.'</div>';
241
242 // Get currents tags if no exists
243 if ( $this->link_tags == 'null' ) {
244 $this->prepareAutoLinkTags();
245 }
246
247 // HTML Rel (tag/no-follow)
248 $rel = '';
249
250 global $wp_rewrite;
251 $rel .= ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
252
253 $no_follow = (int) $this->options['no_follow'];
254 if ( $no_follow == 1 ) { // No follow ?
255 $rel .= ( empty($rel) ) ? 'nofollow' : ' nofollow';
256 }
257
258 if ( !empty($rel) ) {
259 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
260 }
261
262 // only continue if the database actually returned any links
263 if ( isset($this->link_tags) && is_array($this->link_tags) && count($this->link_tags) > 0 ) {
264 $must_tokenize = TRUE; // will perform basic tokenization
265 $tokens = NULL; // two kinds of tokens: markup and text
266
267 $case = ( $this->options['auto_link_case'] == 1 ) ? 'i' : '';
268
269 foreach ( (array) $this->link_tags as $term_name => $term_link ) {
270 $filtered = ""; // will filter text token by token
271 $match = "/\b" . preg_quote($term_name, "/") . "\b/".$case;
272 $substitute = '<a href="'.$term_link.'" class="st_tag internal_tag" '.$rel.' title="'. attribute_escape( sprintf( __('Posts tagged with %s', 'simpletags'), $term_name ) )."\">$0</a>";
273
274 // for efficiency only tokenize if forced to do so
275 if ( $must_tokenize ) {
276 // this regexp is taken from PHP Markdown by Michel Fortin: http://www.michelf.com/projects/php-markdown/
277 $comment = '(?s:<!(?:--.*?--\s*)+>)|';
278 $processing_instruction = '(?s:<\?.*?\?>)|';
279 $tag = '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)';
280 $markup = $comment . $processing_instruction . $tag;
281 $flags = PREG_SPLIT_DELIM_CAPTURE;
282 $tokens = preg_split("{($markup)}", $content, -1, $flags);
283 $must_tokenize = FALSE;
284 }
285
286 // there should always be at least one token, but check just in case
287 if ( isset($tokens) && is_array($tokens) && count($tokens) > 0 ) {
288 $i = 0;
289 foreach ($tokens as $token) {
290 if (++$i % 2 && $token != '') { // this token is (non-markup) text
291 if ($anchor_level == 0) { // linkify if not inside anchor tags
292 if ( preg_match($match, $token) ) { // use preg_match for compatibility with PHP 4
293 $token = preg_replace($match, $substitute, $token); // only PHP 5 supports calling preg_replace with 5 arguments
294 $must_tokenize = TRUE; // re-tokenize next time around
295 }
296 }
297 }
298 else { // this token is markup
299 if ( preg_match("#<\s*a\s+[^>]*>#i", $token) ) { // found <a ...>
300 $anchor_level++;
301 } elseif ( preg_match("#<\s*/\s*a\s*>#i", $token) ) { // found </a>
302 $anchor_level--;
303 }
304 }
305 $filtered .= $token; // this token has now been filtered
306 }
307 $content = $filtered; // filtering completed for this link
308 }
309 }
310 }
311
312 // Remove ST Prefix/Suffix content
313 $content = substr($content, 5, strlen($content) - 11);
314
315 return $content;
316 }
317
318 /**
319 * Replace marker by a tag cloud in post content
320 *
321 * @param string $content
322 * @return string
323 */
324 function inlineTagCloud( $content = '' ) {
325 if ( strpos($content, '<!--st_tag_cloud-->') ) {
326 $content = str_replace('<!--st_tag_cloud-->', $this->extendedTagCloud( '', false ), $content);
327 }
328 return $content;
329 }
330
331 /**
332 * Stock posts ID as soon as possible
333 *
334 * @param array $posts
335 * @return array
336 */
337 function getPostIds( $posts = array() ) {
338 if ( !empty($posts) && is_array($posts) ) {
339 foreach( (array) $posts as $post) {
340 $this->posts[] = $post->ID;
341 }
342 }
343 return $posts;
344 }
345
346 function getTagsFromCurrentPosts() {
347 if ( is_array($this->posts) && count($this->posts) > 0 ) {
348 // Generate SQL from post id
349 $postlist = implode( "', '", $this->posts );
350
351 if ( $this->use_cache === true ) { // Use cache
352 // Generate key cache
353 $key = md5(maybe_serialize($postlist));
354
355 // Get cache if exist
356 if ( $cache = wp_cache_get( 'generate_keywords', 'simpletags' ) ) {
357 if ( isset( $cache[$key] ) ) {
358 $this->tags_currentposts = $cache[$key];
359 return;
360 }
361 }
362 }
363
364 // If cache not exist, get datas and set cache
365 global $wpdb;
366 $results = $wpdb->get_results("
367 SELECT t.name AS name, t.term_id AS term_id, tt.count AS count
368 FROM {$wpdb->term_relationships} AS tr
369 INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
370 INNER JOIN {$wpdb->terms} AS t ON (tt.term_id = t.term_id)
371 WHERE tt.taxonomy = 'post_tag'
372 AND ( tr.object_id IN ('{$postlist}') )
373 GROUP BY t.term_id
374 ORDER BY tt.count DESC");
375
376 if ( $this->use_cache === true ) { // Use cache
377 $cache[$key] = $results;
378 wp_cache_set('generate_keywords', $cache, 'simpletags');
379 }
380
381 $this->tags_currentposts = $results;
382 unset($results, $key);
383 }
384 }
385
386 /**
387 * Generate keywords for meta data
388 *
389 * @return string
390 */
391 function generateKeywords() {
392 // Get tags for current posts
393 if ( empty($this->tags_currentposts) ) {
394 $this->getTagsFromCurrentPosts();
395 }
396
397 $results = array();
398 foreach ( (array) $this->tags_currentposts as $term ) {
399 $results[] = $term->name;
400 }
401 unset($this->tags_currentposts);
402
403 $always_list = trim($this->options['meta_always_include']); // Static keywords
404 $always_array = (array) explode(',', $always_list);
405
406 // Trim
407 foreach ( $always_array as $keyword ) {
408 if ( empty($keyword) ) {
409 continue;
410 }
411 $results[] = trim($keyword);
412 }
413 unset($always_list, $always_array);
414
415 // Unique keywords
416 $results = array_unique($results);
417
418 // Return if empty
419 if ( empty($results) ) {
420 return '';
421 }
422
423 // Limit to max quantity if set
424 $number = (int) $this->options['meta_keywords_qty'];
425 if ( $number != 0 && is_array($results) && !empty($results) && count($results) > 1 ) {
426 $results = $this->randomArray($results); // Randomize keywords
427 $results = array_slice( $results, 0, $number );
428 }
429
430 return strip_tags(implode(', ', $results));
431 }
432
433 /**
434 * Display meta keywords
435 *
436 */
437 function displayMetaKeywords() {
438 $terms_list = $this->generateKeywords();
439 if ( !empty($terms_list) ) {
440 echo "\n\t" . '<!-- Generated by Simple Tags ' . $this->version . ' - http://wordpress.org/extend/plugins/simple-tags -->' ."\n\t". '<meta name="keywords" content="' . $terms_list . '" />' ."\n";
441 }
442 return;
443 }
444
445 /**
446 * Auto add related posts to post content
447 *
448 * @param string $content
449 * @return string
450 */
451 function inlineRelatedPosts( $content = '' ) {
452 $marker = false;
453 if ( is_feed() ) {
454 if ( $this->options['rp_feed'] == '1' ) {
455 $marker = true;
456 }
457 } else {
458 switch ( $this->options['rp_embedded'] ) {
459 case 'blogonly' :
460 $marker = ( is_feed() ) ? false : true;
461 break;
462 case 'homeonly' :
463 $marker = ( is_home() ) ? true : false;
464 break;
465 case 'singularonly' :
466 $marker = ( is_singular() ) ? true : false;
467 break;
468 case 'singleonly' :
469 $marker = ( is_single() ) ? true : false;
470 break;
471 case 'pageonly' :
472 $marker = ( is_page() ) ? true : false;
473 break;
474 case 'all' :
475 $marker = true;
476 break;
477 case 'no' :
478 default:
479 $marker = false;
480 break;
481 }
482 }
483
484 if ( $marker === true ) {
485 return ( $content . $this->relatedPosts( '', false ) );
486 }
487 return $content;
488 }
489
490 /**
491 * Auto add current tags post to post content
492 *
493 * @param string $content
494 * @return string
495 */
496 function inlinePostTags( $content = '' ) {
497 $marker = false;
498 if ( is_feed() ) {
499 if ( $this->options['tt_feed'] == '1' ) {
500 $marker = true;
501 }
502 } else {
503 switch ( $this->options['tt_embedded'] ) {
504 case 'blogonly' :
505 $marker = ( is_feed() ) ? false : true;
506 break;
507 case 'homeonly' :
508 $marker = ( is_home() ) ? true : false;
509 break;
510 case 'singularonly' :
511 $marker = ( is_singular() ) ? true : false;
512 break;
513 case 'singleonly' :
514 $marker = ( is_single() ) ? true : false;
515 break;
516 case 'pageonly' :
517 $marker = ( is_page() ) ? true : false;
518 break;
519 case 'all' :
520 $marker = true;
521 break;
522 case 'no' :
523 default:
524 $marker = false;
525 break;
526 }
527 }
528
529 if ( $marker === true ) {
530 return ( $content . $this->extendedPostTags( '', false ) );
531 }
532 return $content;
533 }
534
535
536 /**
537 * Generate related posts
538 *
539 * @param string $user_args
540 * @return string|array
541 */
542 function relatedPosts( $user_args = '', $copyright = true ) {
543 $defaults = array(
544 'number' => 5,
545 'order' => 'count-desc',
546 'format' => 'list',
547 'include_page' => 'true',
548 'exclude_posts' => '',
549 'exclude_tags' => '',
550 'post_id' => '',
551 'except_wrap' => 55,
552 'limit_days' => 0,
553 'min_shared' => 1,
554 'title' => __('<h4>Related posts</h4>', 'simpletags'),
555 'nopoststext' => __('No related posts.', 'simpletags'),
556 'dateformat' => $this->dateformat,
557 'xformat' => __('<a href="%post_permalink%" title="%post_title% (%post_date%)">%post_title%</a> (%post_comment%)', 'simpletags')
558 );
559
560 // Get values in DB
561 $defaults['number'] = $this->options['rp_limit_qty'];
562 $defaults['order'] = $this->options['rp_order'];
563 $defaults['nopoststext'] = $this->options['rp_notagstext'];
564 $defaults['title'] = $this->options['rp_title'];
565 $defaults['xformat'] = $this->options['rp_xformat'];
566
567 if( empty($user_args) ) {
568 $user_args = $this->options['rp_adv_usage'];
569 }
570
571 // Replace old markers by new
572 $markers = array('%date%' => '%post_date%', '%permalink%' => '%post_permalink%', '%title%' => '%post_title%', '%commentcount%' => '%post_comment%', '%tagcount%' => '%post_tagcount%', '%postid%' => '%post_id%');
573 $user_args = strtr($user_args, $markers);
574
575 $args = wp_parse_args( $user_args, $defaults );
576 extract($args);
577
578 // Get current post data
579 $post_id = (int) $post_id;
580 if ( $post_id == 0 ) {
581 global $post;
582 $object_id = (int) $post->ID;
583 }
584
585 // Get cache if exist
586 $results = false;
587 if ( $this->use_cache === true ) { // Use cache
588 // Generate key cache
589 $key = md5(maybe_serialize($user_args.'-'.$object_id));
590
591 if ( $cache = wp_cache_get( 'related_posts', 'simpletags' ) ) {
592 if ( isset( $cache[$key] ) ) {
593 $results = $cache[$key];
594 }
595 }
596 }
597
598 // If cache not exist, get datas and set cache
599 if ( $results === false || $results === null ) {
600 // Get get tags
601 $current_tags = get_the_tags( (int) $object_id );
602
603 if ( $current_tags === false ) {
604 return $this->outputContent( 'st-related-posts', $format, $title, $nopoststext, $copyright );
605 }
606
607 // Number - Limit
608 $number = (int) $number;
609 if ( $number == 0 ) {
610 $number = 5;
611 } elseif( $number > 50 ) {
612 $number = 50;
613 }
614 $limit_sql = 'LIMIT 0, '.$number;
615 unset($number);
616
617 // Order tags before output (count-asc/count-desc/date-asc/date-desc/name-asc/name-desc/random)
618 $order_by = '';
619 $order = strtolower($order);
620 switch ( $order ) {
621 case 'count-asc':
622 $order_by = 'counter ASC, p.post_title DESC';
623 break;
624 case 'random':
625 $order_by = 'RAND()';
626 break;
627 case 'date-asc':
628 $order_by = 'p.post_date ASC';
629 break;
630 case 'date-desc':
631 $order_by = 'p.post_date DESC';
632 break;
633 case 'name-asc':
634 $order_by = 'p.post_title ASC';
635 break;
636 case 'name-desc':
637 $order_by = 'p.post_title DESC';
638 break;
639 default: // count-desc
640 $order_by = 'counter DESC, p.post_title DESC';
641 break;
642 }
643
644 // Limit days - 86400 seconds = 1 day
645 $limit_days = (int) $limit_days;
646 $limit_days_sql = '';
647 if ( $limit_days != 0 ) {
648 $limit_days_sql = 'AND p.post_date > "' .date( 'Y-m-d H:i:s', time() - $limit_days * 86400 ). '"';
649 }
650 unset($limit_days);
651
652 // Include_page
653 $include_page = strtolower($include_page);
654 if ( $include_page == 'true' ) {
655 $restrict_sql = "AND p.post_type IN ('page', 'post')";
656 } else {
657 $restrict_sql = "AND p.post_type = 'post'";
658 }
659 unset($include_page);
660
661 // Restrict posts
662 $exclude_posts_sql = '';
663 if ( $exclude_posts != '' ) {
664 $exclude_posts = (array) explode(',', $exclude_posts);
665 $exclude_posts_sql = "AND p.ID NOT IN (";
666 foreach ( $exclude_posts as $value ) {
667 $value = (int) $value;
668 if( $value != 0 ) {
669 $exclude_posts_sql .= '"'.$value.'", ';
670 }
671 }
672 $exclude_posts_sql .= '"'.$post_id.'")';
673 } else {
674 $exclude_posts_sql = "AND p.ID <> {$object_id}";
675 }
676 unset($exclude_posts);
677
678 // Restricts tags
679 $tags_to_exclude = array();
680 if ( $exclude_tags != '' ) {
681 $exclude_tags = (array) explode(',', $exclude_tags);
682 foreach ( $exclude_tags as $value ) {
683 $tags_to_exclude[] = trim($value);
684 }
685 }
686 unset($exclude_tags);
687
688 // SQL Tags list
689 $tag_list = '';
690 foreach ( (array) $current_tags as $tag ) {
691 if ( !in_array($tag->name, $tags_to_exclude) ) {
692 $tag_list .= '"'.(int) $tag->term_id.'", ';
693 }
694 }
695
696 // If empty return no posts text
697 if ( empty($tag_list) ) {
698 return $this->outputContent( 'st-related-posts', $format, $title, $nopoststext, $copyright );
699 }
700
701 // Remove latest ", "
702 $tag_list = substr($tag_list, 0, strlen($tag_list) - 2);
703
704 // If empty use default xformat !
705 if ( empty($xformat) ) {
706 $xformat = $defaults['xformat'];
707 }
708
709 // Group Concat only for MySQL > 4.1 and check if post_relatedtags is used by xformat...
710 $select_gp_concat = '';
711 if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') && ( strpos($xformat,'%post_relatedtags%') || $min_shared > 1 ) ) {
712 $select_gp_concat = ', GROUP_CONCAT(tt.term_id) as terms_id';
713 } else {
714 $xformat = str_replace('%post_relatedtags%', '', $xformat); // Group Concat only for MySQL > 4.1, remove related tags
715 }
716
717 // Check if post_excerpt is used by xformat...
718 $select_excerpt = '';
719 if ( strpos( $xformat, '%post_excerpt%' ) ) {
720 $select_excerpt = ', p.post_content, p.post_excerpt, p.post_password';
721 }
722
723 // Posts: title, comments_count, date, permalink, post_id, counter
724 global $wpdb;
725 $results = $wpdb->get_results("
726 SELECT p.post_title, p.comment_count, p.post_date, p.ID, COUNT(tr.object_id) AS counter {$select_excerpt} {$select_gp_concat}
727 FROM {$wpdb->posts} AS p
728 INNER JOIN {$wpdb->term_relationships} AS tr ON (p.ID = tr.object_id)
729 INNER JOIN {$wpdb->term_taxonomy} AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
730 WHERE tt.taxonomy = 'post_tag'
731 AND (tt.term_id IN ({$tag_list}))
732 {$exclude_posts_sql}
733 AND p.post_status = 'publish'
734 AND p.post_date_gmt < '".current_time('mysql')."'
735 {$limit_days_sql}
736 {$restrict_sql}
737 GROUP BY tr.object_id
738 ORDER BY {$order_by}
739 {$limit_sql}");
740
741 if ( $this->use_cache === true ) { // Use cache
742 $cache[$key] = $results;
743 wp_cache_set('related_posts', $cache, 'simpletags');
744 }
745 }
746
747 if ( $results === false || empty($results) ) {
748 return $this->outputContent( 'st-related-posts', $format, $title, $nopoststext, $copyright );
749 } elseif ( $format == 'array' ) {
750 return $this->outputContent( 'st-related-posts', 'array', '', $results, $copyright );
751 }
752
753 if ( empty($dateformat) ) {
754 $dateformat = $this->dateformat;
755 }
756
757 $output = array();
758 // Replace placeholders
759 foreach ( (array) $results as $result ) {
760 if ( $min_shared > 1 && ( count(explode(',', $result->terms_id)) < $min_shared ) ) {
761 continue;
762 }
763
764 $element_loop = $xformat;
765
766 $post_title = apply_filters( 'the_title', $result->post_title );
767 $element_loop = str_replace('%post_date%', mysql2date($dateformat, $result->post_date), $element_loop);
768 $element_loop = str_replace('%post_permalink%', get_permalink($result->ID), $element_loop);
769 $element_loop = str_replace('%post_title%', $post_title, $element_loop);
770 $element_loop = str_replace('%post_title_attribute%', wp_specialchars(strip_tags($post_title)), $element_loop);
771 $element_loop = str_replace('%post_comment%', $result->comment_count, $element_loop);
772 $element_loop = str_replace('%post_tagcount%', $result->counter, $element_loop);
773 $element_loop = str_replace('%post_id%', $result->ID, $element_loop);
774 $element_loop = str_replace('%post_relatedtags%', $this->getTagsFromID($result->terms_id), $element_loop);
775 $element_loop = str_replace('%post_excerpt%', $this->getExcerptPost( $result->post_excerpt, $result->post_content, $result->post_password, $except_wrap ), $element_loop);
776
777 $output[] = $element_loop;
778 }
779 unset($results, $result);
780 return $this->outputContent( 'st-related-posts', $format, $title, $output, $copyright );
781 }
782
783 function getExcerptPost( $excerpt = '', $content = '', $password = '', $excerpt_length = 55 ) {
784 if ( !empty($password) ) { // if there's a password
785 if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $password ) { // and it doesn't match the cookie
786 return __('There is no excerpt because this is a protected post.', 'simpletags');
787 }
788 }
789
790 if ( !empty($excerpt) ) {
791 return apply_filters('get_the_excerpt', $excerpt);
792 } else { // Fake excerpt
793 $content = str_replace(']]>', ']]&gt;', $content);
794 $content = strip_tags($content);
795
796 $excerpt_length = (int) $excerpt_length;
797 if ( $excerpt_length == 0 ) {
798 $excerpt_length = 55;
799 }
800
801 $words = explode(' ', $content, $excerpt_length + 1);
802 if ( count($words) > $excerpt_length ) {
803 array_pop($words);
804 array_push($words, '[...]');
805 $content = implode(' ', $words);
806 }
807 return $content;
808 }
809 }
810
811 //Get and format tags from list ID (SQL Group Concat)
812 function getTagsFromID( $terms = '' ) {
813 if ( empty($terms) ) {
814 return '';
815 }
816
817 // Get tags since Term ID.
818 $terms = (array) get_terms('post_tag', 'include='.$terms);
819 if ( empty($terms) ) {
820 return '';
821 }
822
823 // HTML Rel (tag/no-follow)
824 $rel = '';
825
826 global $wp_rewrite;
827 $rel .= ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
828
829 $no_follow = (int) $this->options['no_follow'];
830 if ( $no_follow == 1 ) { // No follow ?
831 $rel .= ( empty($rel) ) ? 'nofollow' : ' nofollow';
832 }
833
834 if ( !empty($rel) ) {
835 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
836 }
837
838 $output = '';
839 foreach ( $terms as $term ) {
840 $output .= '<a href="'.get_tag_link($term->term_id).'" title="'.attribute_escape(sprintf( __ngettext('%d topic', '%d topics', $term->count, 'simpletags'), $term->count )).'" '.$rel.'>'.wp_specialchars($term->name).'</a>, ';
841 }
842 $output = substr($output, 0, strlen($output) - 2); // Remove latest ", "
843 return $output;
844 }
845
846 /**
847 * Check is page is a tag view, even if tags haven't post
848 *
849 * @return boolean
850 */
851 function isTag() {
852 $slugs = get_query_var('tag');
853
854 if ( empty($slugs) ) {
855 return false;
856 }
857 return true;
858 }
859
860 /**
861 * Get related tags for a tags view
862 *
863 * @param string $user_args
864 * @return string|array
865 */
866 function relatedTags( $user_args = '' ) {
867 $defaults = array(
868 'number' => 5,
869 'order' => 'count-desc',
870 'separator' => ' ',
871 'format' => 'list',
872 'method' => 'OR',
873 'no_follow' => 0,
874 'title' => __('<h4>Related tags</h4>', 'simpletags'),
875 'notagstext' => __('No related tag found.', 'simpletags'),
876 'xformat' => __('<span>%tag_count%</span> <a %tag_rel% href="%tag_link_add%">+</a> <a %tag_rel% href="%tag_link%" title="See posts with %tag_name_attribute%">%tag_name%</a>', 'simpletags')
877 );
878
879 // Get values in DB
880 $defaults['no_follow'] = $this->options['no_follow'];
881 $defaults['number'] = $this->options['rt_number'];
882 $defaults['order'] = $this->options['rt_order'];
883 $defaults['separator'] = $this->options['rt_separator'];
884 $defaults['format'] = $this->options['rt_format'];
885 $defaults['method'] = $this->options['rt_method'];
886 $defaults['title'] = $this->options['rt_title'];
887 $defaults['notagstext'] = $this->options['rt_notagstext'];
888 $defaults['xformat'] = $this->options['rt_xformat'];
889
890 if( empty($user_args) ) {
891 $user_args = $this->options['rt_adv_usage'];
892 }
893
894 $args = wp_parse_args( $user_args, $defaults );
895 extract($args);
896
897 if ( !is_tag() && !$this->isTag() ) {
898 return $this->outputContent( 'st-related-tags', $format, $title, '', true );
899 }
900
901 // Method union/intersection
902 $method = strtoupper($method);
903 if ( $method == 'AND' ) {
904 $url_tag_sep = '+';
905 } else {
906 $url_tag_sep = ',';
907 }
908
909 // Get currents slugs
910 $slugs = get_query_var('tag');
911 if ( strpos( $slugs, ',') ) {
912 $current_slugs = explode(',', $slugs);
913 } elseif ( strpos( $slugs, '+') ) {
914 $current_slugs = explode('+', $slugs);
915 } elseif ( strpos( $slugs, ' ') ) {
916 $current_slugs = explode(' ', $slugs);
917 }else {
918 $current_slugs[] = $slugs;
919 }
920
921 // Get cache if exist
922 $related_tags = false;
923 if ( $this->use_cache === true ) { // Use cache
924 // Generate key cache
925 $key = md5(maybe_serialize($user_args.$slugs));
926
927 if ( $cache = wp_cache_get( 'related_tags', 'simpletags' ) ) {
928 if ( isset( $cache[$key] ) ) {
929 $related_tags = $cache[$key];
930 }
931 }
932 }
933
934 // If cache not exist, get datas and set cache
935 if ( $related_tags === false || $related_tags === null ) {
936 // Order tags before selection (count-asc/count-desc/name-asc/name-desc/random)
937 $order_tmp = strtolower($order);
938 $order_by = $order = '';
939 switch ( $order_tmp ) {
940 case 'count-asc':
941 $order_by = 'count';
942 $order = 'ASC';
943 break;
944 case 'random':
945 $order_by = 'RAND()';
946 $order = '';
947 break;
948 case 'name-asc':
949 $order_by = 'name';
950 $order = 'ASC';
951 break;
952 case 'name-desc':
953 $order_by = 'name';
954 $order = 'DESC';
955 break;
956 default: // count-desc
957 $order_by = 'count';
958 $order = 'DESC';
959 break;
960 }
961
962 // Get objets
963 $terms = "'" . implode("', '", $current_slugs) . "'";
964 global $wpdb;
965 $object_ids = $wpdb->get_col("
966 SELECT tr.object_id
967 FROM {$wpdb->term_relationships} AS tr
968 INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
969 INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id
970 WHERE tt.taxonomy = 'post_tag'
971 AND t.slug IN ({$terms})
972 GROUP BY tr.object_id
973 ORDER BY tr.object_id ASC");
974 unset($terms);
975
976 // Get tags for specified objects
977 $all_related_tags = wp_get_object_terms( $object_ids, 'post_tag', array('orderby' => $order_by, 'order' => $order) );
978
979 // Remove duplicates tags
980 $all_related_tags = array_intersect_key($all_related_tags, array_unique(array_map('serialize', $all_related_tags)));
981
982 // Exclude current tags
983 foreach ( (array) $all_related_tags as $tag ) {
984 if ( !in_array($tag->slug, $current_slugs) ) {
985 $related_tags[] = $tag;
986 }
987 }
988
989 if ( $this->use_cache === true ) { // Use cache
990 $cache[$key] = $related_tags;
991 wp_cache_set('related_tags', $cache, 'simpletags');
992 }
993 }
994
995 if ( empty($related_tags) ) {
996 return $this->outputContent( 'st-related-tags', $format, $title, $notagstext );
997 } elseif ( $format == 'array' ) {
998 return $this->outputContent( 'st-related-tags', 'array', '', $related_tags );
999 }
1000
1001 // Limit to max quantity if set
1002 $number = (int) $number;
1003 if ( $number != 0 ) {
1004 $related_tags = array_slice( $related_tags, 0, $number );
1005 }
1006
1007 // HTML Rel (tag/no-follow)
1008 $rel = '';
1009
1010 global $wp_rewrite;
1011 $rel .= ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
1012
1013 $no_follow = (int) $no_follow;
1014 if ( $no_follow == 1 ) { // No follow ?
1015 $rel .= ( empty($rel) ) ? 'nofollow' : ' nofollow';
1016 }
1017
1018 if ( !empty($rel) ) {
1019 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
1020 }
1021
1022 // If empty use default xformat !
1023 if ( empty($xformat) ) {
1024 $xformat = $defaults['xformat'];
1025 }
1026
1027 // Build outpout
1028 $output = array();
1029 foreach( $related_tags as $tag ) {
1030 $element_loop = $xformat;
1031
1032 $element_loop = str_replace('%tag_link%', clean_url(get_tag_link($tag->term_id)), $element_loop);
1033 $element_loop = str_replace('%tag_feed%', clean_url(get_tag_feed_link($tag->term_id)), $element_loop);
1034 $element_loop = str_replace('%tag_id%', $tag->term_id, $element_loop);
1035 $element_loop = str_replace('%tag_count%', $tag->count, $element_loop);
1036 $element_loop = str_replace('%tag_name%', wp_specialchars( $tag->name ), $element_loop);
1037 $element_loop = str_replace('%tag_name_attribute%', wp_specialchars(strip_tags($tag->name)), $element_loop);
1038 $element_loop = str_replace('%tag_rel%', $rel, $element_loop);
1039 $element_loop = str_replace('%tag_link_add%', $this->getAddTagToLink( $current_slugs, $tag->slug, $url_tag_sep ), $element_loop);
1040
1041 $output[] = $element_loop;
1042 }
1043 unset($related_tags, $tag, $element_loop);
1044 return $this->outputContent( 'st-related-tags', $format, $title, $output );
1045 }
1046
1047 function getAddTagToLink( $current_slugs = array(), $tag_slug = '', $separator = ',' ) {
1048 // Add new tag slug to current slug
1049 $current_slugs[] = $tag_slug;
1050
1051 // Array to string with good separator
1052 $slugs = implode( $separator, $current_slugs );
1053
1054 global $wp_rewrite;
1055 $taglink = $wp_rewrite->get_tag_permastruct();
1056
1057 if ( empty($taglink) ) { // No permalink
1058 $taglink = $this->info['home'] . '/?tag=' . $slugs;
1059 } else { // Custom permalink
1060 $taglink = $this->info['home'] . user_trailingslashit( str_replace('%tag%', $slugs, $taglink), 'category');
1061 }
1062
1063 return apply_filters('st_add_tag_link', clean_url($taglink));
1064 }
1065
1066 /**
1067 * Get tags to remove in related tags
1068 *
1069 * @param string $user_args
1070 * @return string|array
1071 */
1072 function removeRelatedTags( $user_args = '' ) {
1073 $defaults = array(
1074 'separator' => ' ',
1075 'format' => 'list',
1076 'notagstext' => ' ',
1077 'no_follow' => 0,
1078 'xformat' => __('&raquo; <a %tag_rel% href="%tag_link_remove%" title="Remove %tag_name_attribute% from search">Remove %tag_name%</a>', 'simpletags')
1079 );
1080
1081 // Get values in DB
1082 $defaults['no_follow'] = $this->options['no_follow'];
1083 $defaults['separator'] = $this->options['rt_remove_separator'];
1084 $defaults['format'] = $this->options['rt_remove_format'];
1085 $defaults['notagstext'] = $this->options['rt_remove_notagstext'];
1086 $defaults['xformat'] = $this->options['rt_remove_xformat'];
1087
1088 if( empty($user_args) ) {
1089 $user_args = $this->options['rt_remove_adv_usage'];
1090 }
1091
1092 $args = wp_parse_args( $user_args, $defaults );
1093 extract($args);
1094
1095 if ( !is_tag() && !$this->isTag() ) {
1096 return $this->outputContent( 'st-remove-related-tags', $format, '', '', true );
1097 }
1098
1099 // Get currents slugs
1100 $slugs = get_query_var('tag');
1101 if ( strpos( $slugs, ',') ) {
1102 $current_slugs = explode(',', $slugs);
1103 $url_tag_sep = ',';
1104 } elseif ( strpos( $slugs, '+') ) {
1105 $current_slugs = explode('+', $slugs);
1106 $url_tag_sep = '+';
1107 } elseif ( strpos( $slugs, ' ') ) {
1108 $current_slugs = explode(' ', $slugs);
1109 $url_tag_sep = '+';
1110 } else {
1111 return $this->outputContent( 'st-remove-related-tags', $format, '', $notagstext, true );
1112 }
1113
1114 if ( $format == 'array' ) {
1115 return $this->outputContent( 'st-remove-related-tags', 'array', '', $current_slugs, true );
1116 }
1117
1118 // HTML Rel (tag/no-follow)
1119 $rel = '';
1120
1121 global $wp_rewrite;
1122 $rel .= ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
1123
1124 $no_follow = (int) $no_follow;
1125 if ( $no_follow == 1 ) { // No follow ?
1126 $rel .= ( empty($rel) ) ? 'nofollow' : ' nofollow';
1127 }
1128
1129 if ( !empty($rel) ) {
1130 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
1131 }
1132
1133 // If empty use default xformat !
1134 if ( empty($xformat) ) {
1135 $xformat = $defaults['xformat'];
1136 }
1137
1138 foreach ( $current_slugs as $slug ) {
1139 // Get term by slug
1140 $term = get_term_by('slug', $slug, 'post_tag');
1141
1142 $element_loop = $xformat;
1143
1144 $element_loop = str_replace('%tag_link%', clean_url(get_tag_link($term->term_id)), $element_loop);
1145 $element_loop = str_replace('%tag_feed%', clean_url(get_tag_feed_link($term->term_id)), $element_loop);
1146 $element_loop = str_replace('%tag_id%', $term->term_id, $element_loop);
1147 $element_loop = str_replace('%tag_count%', $term->count, $element_loop);
1148 $element_loop = str_replace('%tag_name%', wp_specialchars( $term->name ), $element_loop);
1149 $element_loop = str_replace('%tag_name_attribute%', wp_specialchars(strip_tags($term->name)), $element_loop);
1150 $element_loop = str_replace('%tag_rel%', $rel, $element_loop);
1151
1152 $element_loop = str_replace('%tag_link_remove%', $this->getRemoveTagToLink( $current_slugs, $term->slug, $url_tag_sep ), $element_loop);
1153
1154 $output[] = $element_loop;
1155 }
1156 return $this->outputContent( 'st-remove-related-tags', $format, '', $output );
1157 }
1158
1159 /**
1160 * Build tag url without a specifik tag
1161 *
1162 * @param array $current_slugs
1163 * @param string $tag_slug
1164 * @param string $separator
1165 * @return string
1166 */
1167 function getRemoveTagToLink( $current_slugs = array(), $tag_slug = '', $separator = ',' ) {
1168 // Remove tag slug to current slugs
1169 $key = array_search($tag_slug, $current_slugs);
1170 unset($current_slugs[$key]);
1171
1172 // Array to string with good separator
1173 $slugs = implode( $separator, $current_slugs );
1174
1175 global $wp_rewrite;
1176 $taglink = $wp_rewrite->get_tag_permastruct();
1177
1178 if ( empty($taglink) ) { // No permalink
1179 $taglink = $this->info['home'] . '/?tag=' . $slugs;
1180 } else { // Custom permalink
1181 $taglink = $this->info['home'] . user_trailingslashit( str_replace('%tag%', $slugs, $taglink), 'category');
1182 }
1183 return apply_filters('st_remove_tag_link', clean_url($taglink));
1184 }
1185
1186 /**
1187 * Sort an array without accent for naturel order :)
1188 *
1189 * @param string $a
1190 * @param string $b
1191 * @return boolean
1192 */
1193 function uksortByName( $a = '', $b = '' ) {
1194 return strnatcasecmp( remove_accents($a), remove_accents($b) );
1195 }
1196
1197 /**
1198 * Generate extended tag cloud
1199 *
1200 * @param string $args
1201 * @return string|array
1202 */
1203 function extendedTagCloud( $args = '', $copyright = true ) {
1204 $defaults = array(
1205 'size' => 'true',
1206 'smallest' => 8,
1207 'largest' => 22,
1208 'unit' => 'pt',
1209 'color' => 'true',
1210 'maxcolor' => '#000000',
1211 'mincolor' => '#CCCCCC',
1212 'number' => 45,
1213 'format' => 'flat',
1214 'cloud_selection' => 'count-desc',
1215 'cloud_sort' => 'random',
1216 'exclude' => '',
1217 'include' => '',
1218 'no_follow' => 0,
1219 'limit_days' => 0,
1220 'min_usage' => 0,
1221 'inc_cats' => 0,
1222 'notagstext' => __('No tags.', 'simpletags'),
1223 '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>', 'simpletags'),
1224 'title' => __('<h4>Tag Cloud</h4>', 'simpletags'),
1225 'category' => 0
1226 );
1227
1228 // Get values in DB
1229 $defaults['no_follow'] = $this->options['no_follow'];
1230 $defaults['cloud_selection'] = $this->options['cloud_selection'];
1231 $defaults['cloud_sort'] = $this->options['cloud_sort'];
1232 $defaults['number'] = $this->options['cloud_limit_qty'];
1233 $defaults['notagstext'] = $this->options['cloud_notagstext'];
1234 $defaults['title'] = $this->options['cloud_title'];
1235 $defaults['maxcolor'] = $this->options['cloud_max_color'];
1236 $defaults['mincolor'] = $this->options['cloud_min_color'];
1237 $defaults['largest'] = $this->options['cloud_max_size'];
1238 $defaults['smallest'] = $this->options['cloud_min_size'];
1239 $defaults['unit'] = $this->options['cloud_unit'];
1240 $defaults['xformat'] = $this->options['cloud_xformat'];
1241 $defaults['format'] = $this->options['cloud_format'];
1242 $defaults['inc_cats'] = $this->options['cloud_inc_cats'];
1243
1244 if ( empty($args) ) {
1245 $args = $this->options['cloud_adv_usage'];
1246 }
1247 $args = wp_parse_args( $args, $defaults );
1248
1249 // Get categories ?
1250 $inc_cats = (int) $args['inc_cats'];
1251 $taxonomy = ( $inc_cats == 0 ) ? 'post_tag' : array('post_tag', 'category');
1252
1253 // Get terms
1254 $terms = $this->getTags( $args, $this->use_cache, $taxonomy );
1255 extract($args); // Params to variables
1256
1257 if ( empty($terms) ) {
1258 return $this->outputContent( 'st-tag-cloud', $format, $title, $notagstext, $copyright );
1259 }
1260
1261 $counts = $term_links = $term_ids = $taxonomies = array();
1262 foreach ( (array) $terms as $term ) {
1263 $counts[$term->name] = $term->count;
1264 if ( $term->taxonomy == 'post_tag' ) { // Tag
1265 $term_links[$term->name] = get_tag_link( $term->term_id );
1266 } else { // Category
1267 $term_links[$term->name] = get_category_link( $term->term_id );
1268 }
1269 $term_ids[$term->name] = $term->term_id;
1270 $taxonomies[$term->name] = $term->taxonomy;
1271 }
1272
1273 // Use full RBG code
1274 if ( strlen($maxcolor) == 4 ) {
1275 $maxcolor = $maxcolor . substr($maxcolor, 1, strlen($maxcolor));
1276 }
1277 if ( strlen($mincolor) == 4 ) {
1278 $mincolor = $mincolor . substr($mincolor, 1, strlen($mincolor));
1279 }
1280
1281 // Check as smallest inferior or egal to largest
1282 if ( $smallest > $largest ) {
1283 $smallest = $largest;
1284 }
1285
1286 // Scaling - Hard value for the moment
1287 $scale_min = 1;
1288 $scale_max = 10;
1289
1290 $minval = min($counts);
1291 $maxval = max($counts);;
1292
1293 $minout = max($scale_min, 0);
1294 $maxout = max($scale_max, $minout);
1295
1296 $scale = ($maxval > $minval) ? (($maxout - $minout) / ($maxval - $minval)) : 0;
1297
1298 // HTML Rel (tag/no-follow)
1299 $rel = '';
1300
1301 global $wp_rewrite;
1302 $rel .= ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
1303
1304 $no_follow = (int) $no_follow;
1305 if ( $no_follow == 1 ) { // No follow ?
1306 $rel .= ( empty($rel) ) ? 'nofollow' : ' nofollow';
1307 }
1308
1309 if ( !empty($rel) ) {
1310 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
1311 }
1312
1313 // If empty use default xformat !
1314 if ( empty($xformat) ) {
1315 $xformat = $defaults['xformat'];
1316 }
1317
1318 // Remove color marquer if color = false
1319 if ( $color == 'false' ) {
1320 $xformat = str_replace('%tag_color%', '', $xformat);
1321 }
1322
1323 // Remove size marquer if size = false
1324 if ( $size == 'false' ) {
1325 $xformat = str_replace('%tag_size%', '', $xformat);
1326 }
1327
1328 // Order terms before output
1329 // count-asc/count-desc/name-asc/name-desc/random
1330 $cloud_sort = strtolower($cloud_sort);
1331 switch ( $cloud_sort ) {
1332 case 'count-asc':
1333 asort($counts);
1334 break;
1335 case 'count-desc':
1336 arsort($counts);
1337 break;
1338 case 'name-asc':
1339 uksort($counts, array( &$this, 'uksortByName'));
1340 break;
1341 case 'name-desc':
1342 uksort($counts, array( &$this, 'uksortByName'));
1343 array_reverse($counts);
1344 break;
1345 default: // random
1346 $counts = $this->randomArray($counts);
1347 break;
1348 }
1349
1350 $output = array();
1351 foreach ( (array) $counts as $term => $count ) {
1352 $scale_result = (int) (($count - $minval) * $scale + $minout);
1353
1354 $element_loop = $xformat;
1355
1356 $element_loop = str_replace('%tag_link%', clean_url($term_links[$term]), $element_loop);
1357 if ( $taxonomies[$term] == 'post_tag' ) { // Tag post
1358 $element_loop = str_replace('%tag_feed%', clean_url(get_tag_feed_link($term_ids[$term])), $element_loop);
1359 } else { // Category
1360 $element_loop = str_replace('%tag_feed%', clean_url(get_category_rss_link(false, $term_ids[$term], '')), $element_loop);
1361 }
1362 $element_loop = str_replace('%tag_id%', $term_ids[$term], $element_loop);
1363 $element_loop = str_replace('%tag_count%', $count, $element_loop);
1364 $element_loop = str_replace('%tag_size%', 'font-size:'.round(($scale_result - $scale_min)*($largest-$smallest)/($scale_max - $scale_min) + $smallest, 2).$unit.';', $element_loop);
1365 $element_loop = str_replace('%tag_color%', 'color:'.$this->getColorByScale(round(($scale_result - $scale_min)*(100)/($scale_max - $scale_min), 2),$mincolor,$maxcolor).';', $element_loop);
1366 $element_loop = str_replace('%tag_name%', wp_specialchars( $term ), $element_loop);
1367 $element_loop = str_replace('%tag_name_attribute%', wp_specialchars(strip_tags($term)), $element_loop);
1368 $element_loop = str_replace('%tag_rel%', $rel, $element_loop);
1369 $element_loop = str_replace('%tag_scale%', $scale_result, $element_loop);
1370 $element_loop = str_replace('%tag_technorati%', $this->formatLink( 'technorati', $term ), $element_loop);
1371 $element_loop = str_replace('%tag_flickr%', $this->formatLink( 'flickr', $term ), $element_loop);
1372 $element_loop = str_replace('%tag_delicious%', $this->formatLink( 'delicious', $term ), $element_loop);
1373
1374 $output[] = $element_loop;
1375 }
1376 unset($counts, $term_links, $term_ids, $taxonomies, $element_loop);
1377 return $this->outputContent( 'st-tag-cloud', $format, $title, $output, $copyright );
1378 }
1379
1380 /**
1381 * Randomize an array and keep association
1382 *
1383 * @param array $data
1384 * @return array
1385 */
1386 function randomArray( $data_in = array() ) {
1387 if ( empty($data_in) ) {
1388 return $data_in;
1389 }
1390
1391 srand( (float) microtime() * 1000000 ); // For PHP < 4.2
1392 $rand_keys = array_rand($data_in, count($data_in));
1393
1394 foreach( (array) $rand_keys as $key ) {
1395 $data_out[$key] = $data_in[$key];
1396 }
1397
1398 return $data_out;
1399 }
1400
1401 /**
1402 * Format nice URL depending service
1403 *
1404 * @param string $type
1405 * @param string $tag_name
1406 * @return string
1407 */
1408 function formatLink( $type = '', $term_name = '' ) {
1409 if ( empty($term_name) ) {
1410 return '';
1411 }
1412
1413 $term_name = wp_specialchars($term_name);
1414 switch ( $type ) {
1415 case 'technorati':
1416 $link = clean_url('http://technorati.com/tag/'.str_replace(' ', '+', $term_name));
1417 return '<a class="tag_technorati" href="'.$link.'" rel="tag">'.$term_name.'</a>';
1418 break;
1419 case 'flickr':
1420 $link = clean_url('http://www.flickr.com/photos/tags/'.preg_replace('/[^a-zA-Z0-9]/', '', strtolower($term_name)).'/');
1421 return '<a class="tag_flickr" href="'.$link.'" rel="tag">'.$term_name.'</a>';
1422 break;
1423 case 'delicious':
1424 $link = clean_url('http://del.icio.us/popular/'.strtolower(str_replace(' ', '', $term_name)));
1425 return '<a class="tag_delicious" href="'.$link.'" rel="tag">'.$term_name.'</a>';
1426 break;
1427 }
1428 return '';
1429 }
1430
1431 /**
1432 * Generate current post tags
1433 *
1434 * @param string $args
1435 * @return string
1436 */
1437 function extendedPostTags( $args = '', $copyright = true ) {
1438 $defaults = array(
1439 'before' => __('Tags: ', 'simpletags'),
1440 'separator' => ', ',
1441 'after' => '<br />',
1442 'post_id' => 0,
1443 'no_follow' => 0,
1444 'inc_cats' => 0,
1445 'xformat' => __('<a href="%tag_link%" title="%tag_name%" %tag_rel%>%tag_name%</a>', 'simpletags'),
1446 'notagtext' => __('No tag for this post.', 'simpletags'),
1447 'number' => 0
1448 );
1449
1450 // Get values in DB
1451 $defaults['no_follow'] = $this->options['no_follow'];
1452 $defaults['before'] = $this->options['tt_before'];
1453 $defaults['separator'] = $this->options['tt_separator'];
1454 $defaults['after'] = $this->options['tt_after'];
1455 $defaults['notagtext'] = $this->options['tt_notagstext'];
1456 $defaults['number'] = $this->options['tt_number'];
1457 $defaults['inc_cats'] = $this->options['tt_inc_cats'];
1458 $defaults['xformat'] = $this->options['tt_xformat'];
1459
1460 if ( empty($args) ) {
1461 $args = $this->options['tt_adv_usage'];
1462 }
1463
1464 $args = wp_parse_args( $args, $defaults );
1465 extract($args);
1466
1467 // Choose post ID
1468 $post_id = (int) $post_id;
1469 if ( $post_id != 0 ) {
1470 $id = (int) $post_id;
1471 } else {
1472 global $post;
1473 $id = (int) $post->ID;
1474 }
1475
1476 // Get categories ?
1477 $inc_cats = (int) $args['inc_cats'];
1478 $taxonomy = ( $inc_cats == 0 ) ? 'post_tag' : array('post_tag', 'category');
1479
1480 // Get terms
1481 $terms = apply_filters( 'get_the_tags', wp_get_object_terms($id, $taxonomy) );
1482
1483 // If no terms, return text nothing.
1484 if ( empty($terms) ) {
1485 return $notagtext;
1486 }
1487
1488 // Limit to max quantity if set
1489 $number = (int) $number;
1490 if ( $number != 0 ) {
1491 $terms = $this->randomArray($terms); // Randomize terms
1492 $terms = array_slice( $terms, 0, $number );
1493 }
1494
1495 // If empty use default xformat !
1496 if ( empty($xformat) ) {
1497 $xformat = $defaults['xformat'];
1498 }
1499
1500 // HTML Rel (tag/no-follow)
1501 $rel = '';
1502
1503 global $wp_rewrite;
1504 $rel .= ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
1505
1506 $no_follow = (int) $no_follow;
1507 if ( $no_follow == 1 ) { // No follow ?
1508 $rel .= ( empty($rel) ) ? 'nofollow' : ' nofollow';
1509 }
1510
1511 if ( !empty($rel) ) {
1512 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
1513 }
1514
1515 foreach ( (array) $terms as $term ) {
1516 $element_loop = $xformat;
1517 if ( $term->taxonomy == 'post_tag' ) { // Tag
1518 $element_loop = str_replace('%tag_link%', clean_url(get_tag_link($term->term_id)), $element_loop);
1519 $element_loop = str_replace('%tag_feed%', clean_url(get_tag_feed_link($term->term_id)), $element_loop);
1520 } else { // Category
1521 $element_loop = str_replace('%tag_link%', clean_url(get_category_link($term->term_id)), $element_loop);
1522 $element_loop = str_replace('%tag_feed%', clean_url(get_category_rss_link(false, $term->term_id, '')), $element_loop);
1523 }
1524 $element_loop = str_replace('%tag_id%', $term->term_id, $element_loop);
1525 $element_loop = str_replace('%tag_name%', wp_specialchars($term->name), $element_loop);
1526 $element_loop = str_replace('%tag_rel%', $rel, $element_loop);
1527 $element_loop = str_replace('%tag_count%', $term->count, $element_loop);
1528
1529 $element_loop = str_replace('%tag_technorati%', $this->formatLink( 'technorati', $term->name ), $element_loop);
1530 $element_loop = str_replace('%tag_flickr%', $this->formatLink( 'flickr', $term->name ), $element_loop);
1531 $element_loop = str_replace('%tag_delicious%', $this->formatLink( 'delicious', $term->name ), $element_loop);
1532
1533 $output[] = $element_loop;
1534 }
1535 unset($terms, $term, $element_loop);
1536 $output = apply_filters( 'the_tags', implode($separator, $output) );
1537
1538 if ( $copyright === true )
1539 return "\n\t" . '<!-- Generated by Simple Tags ' . $this->version . ' - http://wordpress.org/extend/plugins/simple-tags -->' ."\n\t". $before . $output . $after ."\n";
1540 else
1541 return "\n\t". $before . $output . $after ."\n";
1542 }
1543
1544 /**
1545 * Delete embedded tags
1546 *
1547 * @param string $content
1548 * @return string
1549 */
1550 function filterEmbedTags( $content ) {
1551 $tag_start = $this->options['start_embed_tags'];
1552 $tag_end = $this->options['end_embed_tags'];
1553 $len_tagend = strlen($tag_end);
1554
1555 while ( strpos($content, $tag_start) != false && strpos($content, $tag_end) != false ) {
1556 $pos1 = strpos($content, $tag_start);
1557 $pos2 = strpos($content, $tag_end);
1558 $content = str_replace(substr($content, $pos1, ($pos2 - $pos1 + $len_tagend)), '', $content);
1559 }
1560 return $content;
1561 }
1562
1563 /**
1564 * This is pretty filthy. Doing math in hex is much too weird. It's more likely to work, this way!
1565 * Provided from UTW. Thanks.
1566 *
1567 * @param integer $scale_color
1568 * @param string $min_color
1569 * @param string $max_color
1570 * @return string
1571 */
1572 function getColorByScale($scale_color, $min_color, $max_color) {
1573 $scale_color = $scale_color / 100;
1574
1575 $minr = hexdec(substr($min_color, 1, 2));
1576 $ming = hexdec(substr($min_color, 3, 2));
1577 $minb = hexdec(substr($min_color, 5, 2));
1578
1579 $maxr = hexdec(substr($max_color, 1, 2));
1580 $maxg = hexdec(substr($max_color, 3, 2));
1581 $maxb = hexdec(substr($max_color, 5, 2));
1582
1583 $r = dechex(intval((($maxr - $minr) * $scale_color) + $minr));
1584 $g = dechex(intval((($maxg - $ming) * $scale_color) + $ming));
1585 $b = dechex(intval((($maxb - $minb) * $scale_color) + $minb));
1586
1587 if (strlen($r) == 1) $r = '0'.$r;
1588 if (strlen($g) == 1) $g = '0'.$g;
1589 if (strlen($b) == 1) $b = '0'.$b;
1590
1591 return '#'.$r.$g.$b;
1592 }
1593
1594 /**
1595 * Add page in tag search
1596 *
1597 * @param string $where
1598 * @return string
1599 */
1600 function prepareQuery( $where ) {
1601 if ( is_tag() ) {
1602 $where = str_replace('post_type = \'post\'', 'post_type IN(\'page\', \'post\')', $where);
1603 }
1604 return $where;
1605 }
1606
1607 /**
1608 * Reset to default options
1609 *
1610 */
1611 function resetToDefaultOptions() {
1612 update_option($this->db_options, $this->default_options);
1613 $this->options = $this->default_options;
1614 }
1615
1616 /**
1617 * Update taxonomy counter for post AND page
1618 *
1619 * @param array $terms
1620 */
1621 function _update_post_and_page_term_count( $terms ) {
1622 global $wpdb;
1623 foreach ( (array) $terms as $term ) {
1624 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('page', 'post') AND term_taxonomy_id = '$term'");
1625 $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term'");
1626 }
1627 }
1628
1629 /**
1630 * Extended get_tags function that use getTerms function
1631 *
1632 * @param string $args
1633 * @return array
1634 */
1635 function getTags( $args = '', $skip_cache = false, $taxonomy = 'post_tag' ) {
1636 $key = md5(serialize($args));
1637
1638 if ( $skip_cache == true ) {
1639 $terms = $this->getTerms( $taxonomy, $args, $skip_cache );
1640 } else {
1641 // Get cache if exist
1642 if ( $cache = wp_cache_get( 'st_get_tags', 'simpletags' ) ) {
1643 if ( isset( $cache[$key] ) ) {
1644 return apply_filters('get_tags', $cache[$key], $args);
1645 }
1646 }
1647
1648 // Get tags
1649 $terms = $this->getTerms( $taxonomy, $args, $skip_cache );
1650
1651 if ( empty($terms) ) {
1652 return array();
1653 }
1654
1655 $cache[$key] = $terms;
1656 wp_cache_set( 'st_get_tags', $cache, 'simpletags' );
1657 }
1658
1659 $terms = apply_filters('get_tags', $terms, $args);
1660 return $terms;
1661 }
1662
1663 /**
1664 * Extended get_terms function support
1665 * - Limit category
1666 * - Limit days
1667 * - Selection restrict
1668 * - Min usage
1669 *
1670 * @param string|array $taxonomies
1671 * @param string $args
1672 * @return array
1673 */
1674 function getTerms( $taxonomies, $args = '', $skip_cache = false ) {
1675 global $wpdb;
1676
1677 $single_taxonomy = false;
1678 if ( !is_array($taxonomies) ) {
1679 $single_taxonomy = true;
1680 $taxonomies = array($taxonomies);
1681 }
1682
1683 foreach ( (array) $taxonomies as $taxonomy ) {
1684 if ( !is_taxonomy($taxonomy) ) {
1685 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
1686 }
1687 }
1688
1689 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
1690
1691 $defaults = array(
1692 'cloud_selection' => 'count-desc',
1693 'hide_empty' => true,
1694 'exclude' => '',
1695 'include' => '',
1696 'number' => '',
1697 'fields' => 'all',
1698 'slug' => '',
1699 'parent' => '',
1700 'hierarchical' => true,
1701 'child_of' => 0,
1702 'get' => '',
1703 'name__like' => '',
1704 'st_name_like' => '',
1705 'pad_counts' => false,
1706 'limit_days' => 0,
1707 'category' => 0,
1708 'min_usage' => 0
1709 );
1710
1711 $args = wp_parse_args( $args, $defaults );
1712
1713 if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) || '' != $args['parent'] ) {
1714 $args['child_of'] = 0;
1715 $args['hierarchical'] = false;
1716 $args['pad_counts'] = false;
1717 }
1718
1719 if ( 'all' == $args['get'] ) {
1720 $args['child_of'] = 0;
1721 $args['hide_empty'] = 0;
1722 $args['hierarchical'] = false;
1723 $args['pad_counts'] = false;
1724 }
1725 extract($args, EXTR_SKIP);
1726
1727 if ( $child_of ) {
1728 $hierarchy = _get_term_hierarchy($taxonomies[0]);
1729 if ( !isset($hierarchy[$child_of]) ) {
1730 return array();
1731 }
1732 }
1733
1734 if ( $parent ) {
1735 $hierarchy = _get_term_hierarchy($taxonomies[0]);
1736 if ( !isset($hierarchy[$parent]) ) {
1737 return array();
1738 }
1739 }
1740
1741 if ( $skip_cache != true ) {
1742 // Get cache if exist
1743 $key = md5( serialize( $args ) . serialize( $taxonomies ) );
1744 if ( $cache = wp_cache_get( 'get_terms', 'terms' ) ) {
1745 if ( isset( $cache[$key] ) ) {
1746 return apply_filters('get_terms', $cache[$key], $taxonomies, $args);
1747 }
1748 }
1749 }
1750
1751 // Restrict category
1752 $category_sql = '';
1753 if ( !empty($category) && $category != '0' ) {
1754 $incategories = preg_split('/[\s,]+/', $category);
1755
1756 $objects_id = get_objects_in_term( $incategories, 'category' );
1757 $objects_id = array_unique ($objects_id); // to be sure haven't duplicates
1758
1759 if ( empty($objects_id) ) { // No posts for this category = no tags for this category
1760 return array();
1761 }
1762
1763 foreach ( (array) $objects_id as $object_id ) {
1764 $category_sql .= "'". $object_id . "', ";
1765 }
1766
1767 $category_sql = substr($category_sql, 0, strlen($category_sql) - 2); // Remove latest ", "
1768 $category_sql = 'AND p.ID IN ('.$category_sql.')';
1769 }
1770
1771 // count-asc/count-desc/name-asc/name-desc/random
1772 $cloud_selection = strtolower($cloud_selection);
1773 switch ( $cloud_selection ) {
1774 case 'count-asc':
1775 $order_by = 'tt.count ASC';
1776 break;
1777 case 'random':
1778 $order_by = 'RAND()';
1779 break;
1780 case 'name-asc':
1781 $order_by = 't.name ASC';
1782 break;
1783 case 'name-desc':
1784 $order_by = 't.name DESC';
1785 break;
1786 default: // count-desc
1787 $order_by = 'tt.count DESC';
1788 break;
1789 }
1790
1791 // Min usage
1792 $restict_usage = '';
1793 $min_usage = (int) $min_usage;
1794 if ( $min_usage != 0 ) {
1795 $restict_usage = ' AND tt.count >= '. $min_usage;
1796 }
1797
1798 $where = '';
1799 $inclusions = '';
1800 if ( !empty($include) ) {
1801 $exclude = '';
1802 $interms = preg_split('/[\s,]+/',$include);
1803 foreach ( (array) $interms as $interm ) {
1804 if (empty($inclusions)) {
1805 $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
1806 } else {
1807 $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
1808 }
1809 }
1810 }
1811
1812 if ( !empty($inclusions) ) {
1813 $inclusions .= ')';
1814 }
1815 $where .= $inclusions;
1816
1817 $exclusions = '';
1818 if ( !empty($exclude) ) {
1819 $exterms = preg_split('/[\s,]+/',$exclude);
1820 foreach ( (array) $exterms as $exterm ) {
1821 if (empty($exclusions)) {
1822 $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
1823 } else {
1824 $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
1825 }
1826 }
1827 }
1828
1829 if ( !empty($exclusions) ) {
1830 $exclusions .= ')';
1831 }
1832 $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
1833 $where .= $exclusions;
1834
1835 if ( !empty($slug) ) {
1836 $slug = sanitize_title($slug);
1837 $where .= " AND t.slug = '$slug'";
1838 }
1839
1840 if ( !empty($name__like) ) {
1841 $where .= " AND t.name LIKE '{$name__like}%'";
1842 }
1843
1844 if ( strpos($st_name_like, ' ') != false || strpos($st_name_like, ' ') != null ) {
1845 $tmp = '';
1846 $sts = explode(' ', $st_name_like);
1847 foreach ( (array) $sts as $st ) {
1848 if ( empty($st) )
1849 continue;
1850
1851 $st = addslashes_gpc($st);
1852 $tmp .= " t.name LIKE '%{$st}%' OR ";
1853 }
1854 // Remove latest OR
1855 $tmp = substr( $tmp, 0, strlen($tmp) - 4);
1856
1857 $where .= " AND ( $tmp ) ";
1858 unset($tmp) ;
1859 } elseif ( !empty($st_name_like) ) {
1860 $where .= " AND t.name LIKE '%{$st_name_like}%'";
1861 }
1862
1863 if ( '' != $parent ) {
1864 $parent = (int) $parent;
1865 $where .= " AND tt.parent = '$parent'";
1866 }
1867
1868 if ( $hide_empty && !$hierarchical ) {
1869 $where .= ' AND tt.count > 0';
1870 }
1871
1872 $number_sql = '';
1873 if ( strpos($number, ',') != false || strpos($number, ',') != null ) {
1874 $number_sql = $number;
1875 } else {
1876 $number = (int) $number;
1877 if ( $number != 0 ) {
1878 $number_sql = 'LIMIT ' . $number;
1879 }
1880 }
1881
1882 if ( 'all' == $fields ) {
1883 $select_this = 't.*, tt.*';
1884 } else if ( 'ids' == $fields ) {
1885 $select_this = 't.term_id';
1886 } else if ( 'names' == $fields ) {
1887 $select_this = 't.name';
1888 }
1889
1890 // Limit posts date
1891 $limitdays_sql = '';
1892 $limit_days = (int) $limit_days;
1893 if ( $limit_days != 0 ) {
1894 $limitdays_sql = 'AND p.post_date_gmt > "' .date( 'Y-m-d H:i:s', time() - $limit_days * 86400 ). '"';
1895 }
1896
1897 $query = "SELECT {$select_this}
1898 FROM {$wpdb->terms} AS t
1899 INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
1900 INNER JOIN {$wpdb->term_relationships} AS tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
1901 INNER JOIN {$wpdb->posts} AS p ON tr.object_id = p.ID
1902 WHERE tt.taxonomy IN ( {$in_taxonomies} )
1903 AND p.post_date_gmt < '".current_time('mysql')."'
1904 {$limitdays_sql}
1905 {$category_sql}
1906 {$where}
1907 {$restict_usage}
1908 GROUP BY t.term_id
1909 ORDER BY {$order_by}
1910 {$number_sql}";
1911
1912 if ( 'all' == $fields ) {
1913 $terms = $wpdb->get_results($query);
1914 if ( $skip_cache != true ) {
1915 update_term_cache($terms);
1916 }
1917 } elseif ( 'ids' == $fields ) {
1918 $terms = $wpdb->get_col($query);
1919 }
1920
1921 if ( empty($terms) ) {
1922 return array();
1923 }
1924
1925 if ( $child_of || $hierarchical ) {
1926 $children = _get_term_hierarchy($taxonomies[0]);
1927 if ( ! empty($children) ) {
1928 $terms = & _get_term_children($child_of, $terms, $taxonomies[0]);
1929 }
1930 }
1931
1932 // Update term counts to include children.
1933 if ( $pad_counts ) {
1934 _pad_term_counts($terms, $taxonomies[0]);
1935 }
1936
1937 // Make sure we show empty categories that have children.
1938 if ( $hierarchical && $hide_empty ) {
1939 foreach ( (array) $terms as $k => $term ) {
1940 if ( ! $term->count ) {
1941 $children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
1942 foreach ( (array) $children as $child ) {
1943 if ( $child->count ) {
1944 continue 2;
1945 }
1946 }
1947
1948 // It really is empty
1949 unset($terms[$k]);
1950 }
1951 }
1952 }
1953 reset($terms);
1954
1955 if ( $skip_cache != true ) {
1956 $cache[$key] = $terms;
1957 wp_cache_set( 'get_terms', $cache, 'terms' );
1958 }
1959
1960 $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
1961 return $terms;
1962 }
1963
1964 function outputContent( $html_class= '', $format = 'list', $title = '', $content = '', $copyright = true ) {
1965 if ( empty($content) ) {
1966 return ''; // return nothing
1967 }
1968
1969 if ( is_array($content) ) {
1970 switch ( $format ) {
1971 case 'array' :
1972 $output =& $content;
1973 break;
1974 case 'list' :
1975 $output = "<ul class='{$html_class}'>\n\t<li>";
1976 $output .= join("</li>\n\t<li>", $content);
1977 $output .= "</li>\n</ul>\n";
1978 break;
1979 default :
1980 $output = "<div class='{$html_class}'>\n\t";
1981 $output .= join("\n", $content);
1982 $output .= "</div>\n";
1983 break;
1984 }
1985 } else {
1986 $content = trim($content);
1987 switch ( $format ) {
1988 case 'list' :
1989 $output = "<ul class='{$html_class}'>\n\t";
1990 $output .= '<li>'.$content."</li>\n\t";
1991 $output .= "</ul>\n";
1992 break;
1993 default :
1994 $output = "<div class='{$html_class}'>\n\t";
1995 $output .= $content;
1996 $output .= "</div>\n";
1997 break;
1998 }
1999 }
2000
2001
2002 // Replace false by empty
2003 $title = trim($title);
2004 if ( strtolower($title) == 'false' ) {
2005 $title = '';
2006 }
2007
2008 // Put title if exist
2009 if ( !empty($title) ) {
2010 $title .= "\n\t";
2011 }
2012
2013 if ( $copyright === true )
2014 return "\n" . '<!-- Generated by Simple Tags ' . $this->version . ' - http://wordpress.org/extend/plugins/simple-tags -->' ."\n\t". $title . $output. "\n";
2015 else
2016 return "\n\t". $title . $output. "\n";
2017 }
2018
2019 /**
2020 * Test if local installation is mu-plugin or a classic plugin
2021 *
2022 * @return boolean
2023 */
2024 function isMuPlugin() {
2025 if ( strpos(dirname(__FILE__), 'mu-plugins') ) {
2026 return true;
2027 }
2028 return false;
2029 }
2030}
2031
2032// Init ST
2033global $simple_tags;
2034function st_init() {
2035 global $simple_tags;
2036 $simple_tags = new SimpleTags();
2037
2038 // Old method for is_admin function (fix for WP 2.3.2 and sup !)
2039 if ( !function_exists('is_admin_old') ) {
2040 function is_admin_old() {
2041 return (stripos($_SERVER['REQUEST_URI'], 'wp-admin/') !== false);
2042 }
2043 }
2044
2045 // Admin and XML-RPC
2046 if ( is_admin() || is_admin_old() || ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) ) {
2047 require(dirname(__FILE__).'/inc/simple-tags.admin.php');
2048 $simple_tags_admin = new SimpleTagsAdmin( $simple_tags->default_options, $simple_tags->version, $simple_tags->info );
2049
2050 // Installation
2051 register_activation_hook(__FILE__, array(&$simple_tags_admin, 'installSimpleTags') );
2052 }
2053
2054 // Templates functions
2055 require(dirname(__FILE__).'/inc/simple-tags.functions.php');
2056
2057 // Widgets
2058 require(dirname(__FILE__).'/inc/simple-tags.widgets.php');
2059}
2060add_action('plugins_loaded', 'st_init');
2061?>
Note: See TracBrowser for help on using the repository browser.