PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.38.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.38.0
3.54.0 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 All 178 releases
simple-tags / inc / class.client.php

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

704 lines 23.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 {
4 /**
5 * Initialize TaxoPress client
6 *
7 * @return boolean
8 */
9 public function __construct() {
10
11 // Load translation
12 add_action( 'init', array( __CLASS__, 'init_translation' ) );
13
14 // Enqueue frontend scripts
15 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_displayformat_scripts' ) );
16
17 require( STAGS_DIR . '/inc/class.client.autolinks.php' );
18 new SimpleTags_Client_Autolinks();
19
20 // Call tag clouds ?
21 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_terms_display' ) ) {
22 require_once STAGS_DIR . '/inc/tag-clouds-action.php';
23 }
24
25 // Call post tags ?
26 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_post_tags' ) ) {
27 require_once STAGS_DIR . '/inc/post-tags-action.php';
28 }
29
30 // Call related posts ?
31 if ( (int) SimpleTags_Plugin::get_option_value( 'active_related_posts_new' ) === 1 ) {
32 require_once STAGS_DIR . '/inc/related-posts-action.php';
33 }
34 if ( (int) SimpleTags_Plugin::get_option_value( 'active_related_posts_new' ) === 1 ) {
35 require( STAGS_DIR . '/inc/class.client.related_posts.php' );
36 new SimpleTags_Client_RelatedPosts();
37 }
38
39 // Call auto terms ?
40 require( STAGS_DIR . '/inc/class.client.autoterms.php' );
41 new SimpleTags_Client_Autoterms();
42
43 // Call post tags ?
44 require( STAGS_DIR . '/inc/class.client.post_tags.php' );
45 new SimpleTags_Client_PostTags();
46
47
48 if ( (int)SimpleTags_Plugin::get_option_value( 'active_taxonomies' ) === 1 ) {
49 require_once STAGS_DIR . '/inc/taxonomies-action.php';
50 add_action( 'parse_query', array( __CLASS__, 'cpt_taxonomy_parse_query' ) );
51 if (defined('STAGS_OPTIONS_NAME')) {
52 $saved_option = (array)get_option( STAGS_OPTIONS_NAME );
53 if ((array_key_exists('use_tag_pages', $saved_option))) {
54 if((int)$saved_option['use_tag_pages'] === 1){
55 if ( !array_key_exists('post_tag', taxopress_get_extername_taxonomy_data()) ) {
56 add_action( 'init', array( __CLASS__, 'init' ), 11 );
57 add_action( 'parse_query', array( __CLASS__, 'parse_query' ) );
58 }
59 }
60 }
61 }
62 }
63
64 return true;
65 }
66
67 function enqueue_displayformat_scripts() {
68
69 wp_register_script('taxopress-frontend-js', STAGS_URL . '/assets/frontend/js/frontend.js', array('jquery'), STAGS_VERSION);
70 wp_register_style('taxopress-frontend-css', STAGS_URL . '/assets/frontend/css/frontend.css', array(), STAGS_VERSION, 'all');
71
72 wp_enqueue_script('taxopress-frontend-js');
73 wp_enqueue_style('taxopress-frontend-css');
74
75 }
76
77
78 /**
79 * Retrieves the currently queried object.
80 *
81 * Wrapper for WP_Query::get_queried_object().
82 *
83 * @since 3.1.0
84 *
85 * @global WP_Query $wp_query WordPress Query object.
86 *
87 * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object.
88 */
89 public static function taxopress_get_queried_object() {
90 global $wp_query;
91
92 if (!is_object($wp_query)) {
93 return null;
94 }
95
96 return $wp_query->get_queried_object();
97 }
98
99 /**
100 * Add cpt to taxonomy during the query
101 *
102 * @param WP_Query $query
103 *
104 * @return void
105 * @author Olatechpro
106 */
107 public static function cpt_taxonomy_parse_query( $query ) {
108 if(function_exists('get_current_screen')){
109 $screen = get_current_screen();
110 }
111
112 if(is_admin()){
113 return $query;
114 }
115 if(isset($screen->id) && $screen->id == 'edit-post'){
116 return $query;
117 }
118 if ( $query->is_category == true || $query->is_tag == true || $query->is_tax == true ) {
119 $get_queried_object = self::taxopress_get_queried_object();
120 if(is_object($get_queried_object)){
121 if(!isset($get_queried_object->taxonomy)){
122 return $query;
123 }
124 if(!taxopress_show_all_cpt_in_archive_result($get_queried_object->taxonomy)){
125 return $query;
126 }
127 $get_taxonomy = get_taxonomy( $get_queried_object->taxonomy );
128 if(is_object($get_taxonomy)){
129 $post_types = $get_taxonomy->object_type;
130 if ( isset( $query->query_vars['post_type'] )){
131 if(is_array( $query->query_vars['post_type'] )){
132 $post_types = array_filter(array_merge($query->query_vars['post_type'],$post_types));
133 }elseif(is_string( $query->query_vars['post_type'] )){
134 $original_post_type = $query->query_vars['post_type'];
135 $get_post_type_object = get_post_type_object( $original_post_type );
136 if(is_object($get_post_type_object)){
137 if((int)$get_post_type_object->public === 0){
138 return $query;
139 }
140 }
141 $post_types[] = $query->query_vars['post_type'];
142 }
143 $new_post_object = $post_types;
144 }else{
145 $new_post_object = $post_types;
146 }
147 $query->query_vars['post_type'] = $new_post_object;
148 }
149 }
150 }
151 return $query;
152 }
153
154 /**
155 * Load translations
156 */
157 public static function init_translation() {
158 load_plugin_textdomain( 'simple-tags', false, basename( STAGS_DIR ) . '/languages' );
159 }
160
161 /**
162 * Register taxonomy post_tags for page post type
163 *
164 * @return void
165 * @author WebFactory Ltd
166 */
167 public static function init() {
168 register_taxonomy_for_object_type( 'post_tag', 'page' );
169 }
170
171 /**
172 * Add page post type during the query
173 *
174 * @param WP_Query $query
175 *
176 * @return void
177 * @author WebFactory Ltd
178 */
179 public static function parse_query( $query ) {
180 if(function_exists('get_current_screen')){
181 $screen = get_current_screen();
182 }
183
184 if(isset($screen->id) && $screen->id == 'edit-post'){
185 return $query;
186 }
187
188 if ( $query->is_tag == true ) {
189 if ( isset( $query->query_vars['post_type'] ) && is_array( $query->query_vars['post_type'] ) ) {
190 $query->query_vars['post_type'][] = 'page';
191 }else{
192 $query->query_vars['post_type'] = array( 'post', 'page' );
193 }
194 }
195 }
196
197 /**
198 * Randomize an array and keep association
199 *
200 * @param array $array
201 *
202 * @return boolean
203 */
204 public static function random_array( &$array ) {
205 if ( ! is_array( $array ) || empty( $array ) ) {
206 return false;
207 }
208
209 $keys = array_keys( $array );
210 shuffle( $keys );
211
212 $new = array();
213 foreach ( (array) $keys as $key ) {
214 $new[ $key ] = $array[ $key ];
215 }
216
217 $array = $new;
218
219 return true;
220 }
221
222 /**
223 * Build rel for tag link
224 *
225 * @return string
226 */
227 public static function get_rel_attribut() {
228 global $wp_rewrite;
229 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'tag' : ''; // Tag ?
230 if ( ! empty( $rel ) ) {
231 $rel = 'rel="' . $rel . '"'; // Add HTML Tag
232 }
233
234 return $rel;
235 }
236
237 /**
238 * Retrieve the post count for a term across all taxonomies.
239 *
240 * This function queries terms based on the given term name and returns the number of posts associated with the first matching term.
241 * It does not require specifying a taxonomy and will search across all public taxonomies.
242 *
243 * @param string $item
244 * @return int The number of posts associated with the first matching term. Returns 0 if no term is found or if it has no posts.
245 */
246 public static function get_term_post_counts( $item ) {
247
248 $terms = get_terms( array(
249 'name' => strip_tags($item),
250 'hide_empty' => false,
251 'fields' => 'all',
252 'number' => 1,
253 ) );
254
255 // If terms exist, return the first matching term's count
256 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
257 return $terms[0]->count;
258 }
259
260 return 0;
261 }
262
263 /**
264 * Format data for output
265 *
266 * @param string $html_class
267 * @param string $format
268 * @param string $title
269 * @param string|array $content
270 * @param boolean $copyright
271 * @param string $separator
272 *
273 * @return string|array
274 */
275 public static function output_content( $html_class = '', $format = 'list', $title = '', $content = '', $copyright = true, $separator = '', $div_class = '', $a_class = '', $before = '', $after = '', $taxonomy = '') {
276 if ( empty( $content ) ) {
277 return ''; // return nothing
278 }
279
280 if ( $format == 'array' && is_array( $content ) ) {
281 return $content; // Return PHP array if format is array
282 }
283
284 $title = taxopress_sanitize_text_field($title);
285
286 if ( is_array( $content ) ) {
287 switch ( $format ) {
288 case 'list' :
289 $output = ''. $before .' <ul class="' . $html_class . '">' . "\n\t" . '<li>' . implode( "</li>\n\t<li>", $content ) . "</li>\n</ul> {$after}\n";
290 break;
291 case 'ol' :
292 $output = ''. $before .' <ol class="' . $html_class . '">' . "\n\t" . '<li>' . implode( "</li>\n\t<li>", $content ) . "</li>\n</ol> {$after}\n";
293 break;
294 case 'comma':
295 $output = ''. ''. ''. ''. $before . implode(', ', $content) . " {$after}\n";
296 break;
297 case 'table' :
298 $output = $before . '<table class="' . $html_class . ' taxopress-table-container">' . "\n\t";
299 $count = 0;
300 foreach ($content as $item) {
301
302 // $term_name = strip_tags($item);
303 // $post_count = self::get_term_post_counts( $term_name );
304 // If $item is an array (from extendedTagCloud), use its fields
305 if (is_array($item) && isset($item['html'], $item['count'])) {
306 $term_html = $item['html'];
307 $post_count = $item['count'];
308 } else {
309 // fallback for legacy
310 $term_html = $item;
311 $post_count = self::get_term_post_counts(strip_tags($item));
312 }
313
314 // if ( $post_count === 0 ) {
315 // continue;
316 // }
317
318 $display_class = $count >= 6 ? 'hidden' : '';
319 $output .= '<tr class="taxopress-table-row ' . $display_class . '"><td>' . $term_html . '</td><td class="taxopress-post-count">' . $post_count . '</td></tr>' . "\n\t";
320 $count++;
321 }
322 if ($count > 6) {
323 $output .= '<tr><td class="taxopress-see-more-container" colspan="2">';
324 $output .= '<span class="taxopress-see-more-link">see more <span class="taxopress-arrow right"></span></span>';
325 $output .= '<span class="taxopress-close-table-link">close table <span class="taxopress-arrow down"></span></span>';
326 $output .= '</td></tr>' . "\n";
327 }
328 $output .= "</table>" . $after . "\n";
329 break;
330 case 'border':
331 $output = '<div class="taxopress-border-cloud ' . $html_class . '">'. $before .' ' . "\n\t" . implode( "{$separator}\n", $content ) . " {$after}</div>\n";
332 break;
333 case 'box':
334 $output = '<div class="taxopress-box-list ' . $html_class . '">'. $before .' ' . "\n\t" . implode( "{$separator}\n", $content ) . " {$after}</div>\n";
335 break;
336 case 'parent/child':
337 $output = $before . '<ul class="' . $html_class . ' taxopress-parent-child-list">' . "\n";
338
339 // Cache term hierarchy to avoid repeated queries
340 static $term_hierarchy = [];
341 $cache_key = md5($taxonomy . serialize($content));
342
343 if (!isset($term_hierarchy[$cache_key])) {
344 $terms_by_parent = [];
345 $all_terms = [];
346
347 $term_names = array_map(function($term_html) {
348 return strip_tags($term_html);
349 }, array_filter($content, 'trim'));
350
351 if (!empty($term_names)) {
352 $terms = get_terms([
353 'taxonomy' => $taxonomy,
354 'name' => $term_names,
355 'hide_empty' => false,
356 'update_term_meta_cache' => false
357 ]);
358
359 if (!is_wp_error($terms)) {
360 foreach ($terms as $term) {
361 $all_terms[$term->term_id] = [
362 'term' => $term,
363 'html' => $content[array_search($term->name, $term_names)],
364 'is_parent' => false
365 ];
366
367 $parent_id = $term->parent ?: '0';
368 $terms_by_parent[$parent_id][] = $term->term_id;
369 }
370
371 foreach ($terms_by_parent as $child_ids) {
372 foreach ($child_ids as $child_id) {
373 if (isset($terms_by_parent[$child_id])) {
374 $all_terms[$child_id]['is_parent'] = true;
375 }
376 }
377 }
378 }
379 }
380
381 $term_hierarchy[$cache_key] = [
382 'terms' => $all_terms,
383 'hierarchy' => $terms_by_parent
384 ];
385 }
386
387 // Use cached hierarchy
388 $data = $term_hierarchy[$cache_key];
389 $all_terms = $data['terms'];
390 $terms_by_parent = $data['hierarchy'];
391
392 // Group terms by parent
393 $terms_by_parent = [];
394 $all_terms = [];
395
396 // First pass - collect all terms
397 foreach ($content as $term_html) {
398 if (empty(trim($term_html))) continue;
399
400 $term_name = strip_tags($term_html);
401 $term = get_term_by('name', $term_name, $taxonomy);
402
403 if (!$term) continue;
404
405 // Store term for reference
406 $all_terms[$term->term_id] = [
407 'term' => $term,
408 'html' => $term_html,
409 'is_parent' => false
410 ];
411
412 $parent_id = $term->parent ?: '0';
413 if (!isset($terms_by_parent[$parent_id])) {
414 $terms_by_parent[$parent_id] = [];
415 }
416 $terms_by_parent[$parent_id][] = $term->term_id;
417 }
418
419 foreach ($terms_by_parent as $parent_id => $children) {
420 foreach ($children as $child_id) {
421 if (isset($terms_by_parent[$child_id])) {
422 $all_terms[$child_id]['is_parent'] = true;
423 }
424 }
425 }
426
427 $output_term = function($term_id, $level = 0) use (&$output_term, &$all_terms, &$terms_by_parent) {
428 if (!isset($all_terms[$term_id])) return '';
429
430 $term_data = $all_terms[$term_id];
431 $html = str_repeat("\t", $level);
432 $html .= '<li class="taxopress-parent-term">' . $term_data['html'];
433
434 if (isset($terms_by_parent[$term_id])) {
435 $html .= "\n" . str_repeat("\t", $level + 1);
436 $html .= '<ul class="taxopress-child-list">' . "\n";
437 foreach ($terms_by_parent[$term_id] as $child_id) {
438 $html .= $output_term($child_id, $level + 2);
439 }
440 $html .= str_repeat("\t", $level + 1) . "</ul>\n";
441 }
442
443 $html .= str_repeat("\t", $level) . "</li>\n";
444 return $html;
445 };
446 $display_mode = isset($current['display_mode']) ? $current['display_mode'] : 'parents_and_sub';
447
448 if ($display_mode === 'parents_only') {
449 foreach ($all_terms as $term_id => $term_data) {
450 if ($term_data['is_parent']) {
451 $output .= '<li class="taxopress-parent-term">' . $term_data['html'] . "</li>\n";
452 }
453 }
454 } elseif ($display_mode === 'sub_terms_only') {
455 foreach ($all_terms as $term_id => $term_data) {
456 if ($term_data['term']->parent > 0) {
457 $output .= '<li class="taxopress-child-term">' . $term_data['html'] . "</li>\n";
458 }
459 }
460 } else {
461 // Display full hierarchy
462 // Start with root terms or terms whose parents aren't in our set
463 foreach ($all_terms as $term_id => $term_data) {
464 $term = $term_data['term'];
465 if ($term->parent === 0 || !isset($all_terms[$term->parent])) {
466 $output .= $output_term($term_id);
467 }
468 }
469 }
470
471 $output .= "</ul>" . $after . "\n";
472 return $output;
473 break;
474 default :
475 $output = '<div class="' . $html_class . '">'. $before .' ' . "\n\t" . implode( "{$separator}\n", $content ) . " {$after}</div>\n";
476 break;
477 }
478 } else {
479 $content = trim( $content );
480 switch ( $format ) {
481 case 'string' :
482 $output = $content;
483 break;
484 case 'list' :
485 $output = ''. $before .' <ul class="' . $html_class . '">' . "\n\t" . '<li>' . $content . "</li>\n\t" . "</ul> {$after}\n";
486 break;
487 case 'comma':
488 $output = ''. ''. ''. ''. $before . $content . " {$after}\n";
489 break;
490 case 'table':
491 $output = $before . '<table class="' . $html_class . '">' . "\n\t"
492 . '<tr><td>' . $content . '</td></tr>' . "\n\t"
493 . "</table>" . $after . "\n";
494 break;
495 case 'border':
496 $output = '<div class="taxopress-border-cloud ' . $html_class . '">'. $before .' ' . "\n\t" . $content . " {$after} </div>\n";
497 break;
498 case 'box':
499 $output = '<div class="taxopress-box-list ' . $html_class . '">'. $before .' ' . "\n\t" . $content . " {$after} </div>\n";
500 break;
501 case 'parent/child':
502 $output = $before . '<ul class="' . esc_attr($html_class) . ' taxopress-parent-child-list">' . "\n";
503 $lines = explode("\n", $content);
504 $inside_sublist = false;
505
506 foreach ($lines as $line) {
507 $line = trim($line);
508 if (empty($line)) {
509 continue;
510 }
511
512 if (strpos($line, 'taxopress-parent-term') !== false) {
513 if ($inside_sublist) {
514 $output .= "</ul></li>\n";
515 $inside_sublist = false;
516 }
517
518 $output .= '<li class="parent-item" style="list-style-type: disc; color: black;">' . $line;
519
520 $output .= "\n<ul class=\"child-items\" style=\"list-style-type: circle; color: black;\">\n";
521 $inside_sublist = true;
522 } else {
523 $output .= '<li class="child-item">' . $line . "</li>\n";
524 }
525 }
526
527 // Close any open tags
528 if ($inside_sublist) {
529 $output .= "</ul></li>\n";
530 }
531
532 $output .= "</ul>" . $after . "\n";
533 break;
534 default :
535 $output = '<div class="' . $html_class . '">'. $before .' ' . "\n\t" . $content . " {$after} </div>\n";
536 break;
537 }
538 }
539
540 //wrap class
541 if(!empty(trim($div_class))){
542 $wrap_div_class_open = '<div class="'.taxopress_format_class($div_class).'">';
543 $wrap_div_class_close = '</div>';
544 }else{
545 $wrap_div_class_open = '<div class="taxopress-output-wrapper"> ';
546 $wrap_div_class_close = '</div>';
547 }
548 // Replace false by empty
549 $title = trim( $title );
550 if ( strtolower( $title ) == 'false' ) {
551 $title = '';
552 }
553
554 // Put title if exist
555 if ( ! empty( $title ) ) {
556 $title .= "\n\t";
557 }
558
559 if ( $copyright === true ) {
560 return "\n" . '<!-- Generated by TaxoPress ' . STAGS_VERSION . ' - https://wordpress.org/plugins/simple-tags/ -->' . "\n\t" . $wrap_div_class_open . $title . $output . $wrap_div_class_close . "\n";
561 } else {
562 return "\n\t" . $wrap_div_class_open . $title . $output . $wrap_div_class_close . "\n";
563 }
564 }
565
566 /**
567 * Remplace marker by dynamic values (use for related tags, current tags and tag cloud)
568 *
569 * @param string $element_loop
570 * @param object $term
571 * @param string $rel
572 * @param integer $scale_result
573 * @param integer $scale_max
574 * @param integer $scale_min
575 * @param integer $largest
576 * @param integer $smallest
577 * @param string $unit
578 * @param string $maxcolor
579 * @param string $mincolor
580 *
581 * @return string
582 */
583 public static function format_internal_tag( $element_loop = '', $term = null, $rel = '', $scale_result = 0, $scale_max = null, $scale_min = 0, $largest = 0, $smallest = 0, $unit = '', $maxcolor = '', $mincolor = '' ) {
584 // Need term object
585 $tag_link = get_term_link( $term, $term->taxonomy );
586 $element_loop = str_replace( '%tag_link%', esc_url( $tag_link ), $element_loop );
587 $element_loop = str_replace( '%tag_feed%', esc_url( get_term_feed_link( $term->term_id, $term->taxonomy, '' ) ), $element_loop );
588
589 $element_loop = str_replace( '%tag_name%', esc_html( $term->name ), $element_loop );
590 $element_loop = str_replace( '%tag_name_attribute%', esc_html( strip_tags( $term->name ) ), $element_loop );
591 $element_loop = str_replace( '%tag_id%', $term->term_id, $element_loop );
592 $element_loop = str_replace( '%tag_count%', (int) $term->count, $element_loop );
593 $element_loop = str_replace( '%tag_description%', esc_html( $term->description ), $element_loop );
594
595 // Need rel
596 $element_loop = str_replace( '%tag_rel%', $rel, $element_loop );
597
598 // Need max/min/scale and other :)
599 if ( $scale_result !== null ) {
600 $scale_result = (int) $scale_result;
601 $scale_min = (int) $scale_min;
602 $scale_max = (int) $scale_max;
603 $largest = (int) $largest;
604 $smallest = (int) $smallest;
605
606 $element_loop = str_replace( '%tag_size%', 'font-size:' . self::round( ( $scale_result - $scale_min ) * ( $largest - $smallest ) / ( $scale_max - $scale_min ) + $smallest, 2 ) . $unit . ';', $element_loop );
607 $element_loop = str_replace( '%tag_color%', 'color:' . self::get_color_by_scale( self::round( ( $scale_result - $scale_min ) * ( 100 ) / ( $scale_max - $scale_min ), 2 ), $mincolor, $maxcolor ) . ';', $element_loop );
608 $element_loop = str_replace( '%tag_scale%', $scale_result, $element_loop );
609 }
610
611 // External link
612 $element_loop = str_replace( '%tag_technorati%', self::format_external_tag( 'technorati', $term->name ), $element_loop );
613 $element_loop = str_replace( '%tag_flickr%', self::format_external_tag( 'flickr', $term->name ), $element_loop );
614 $element_loop = str_replace( '%tag_delicious%', self::format_external_tag( 'delicious', $term->name ), $element_loop );
615
616 return $element_loop;
617 }
618
619 /**
620 * This is pretty filthy. Doing math in hex is much too weird. It's more likely to work, this way!
621 * Provided from UTW. Thanks.
622 *
623 * @param integer $scale_color
624 * @param string $min_color
625 * @param string $max_color
626 *
627 * @return string
628 */
629 public static function get_color_by_scale( $scale_color, $min_color, $max_color ) {
630 $scale_color = $scale_color / 100;
631
632 $minr = hexdec( substr( $min_color, 1, 2 ) );
633 $ming = hexdec( substr( $min_color, 3, 2 ) );
634 $minb = hexdec( substr( $min_color, 5, 2 ) );
635
636 $maxr = hexdec( substr( $max_color, 1, 2 ) );
637 $maxg = hexdec( substr( $max_color, 3, 2 ) );
638 $maxb = hexdec( substr( $max_color, 5, 2 ) );
639
640 $r = dechex( intval( ( ( $maxr - $minr ) * $scale_color ) + $minr ) );
641 $g = dechex( intval( ( ( $maxg - $ming ) * $scale_color ) + $ming ) );
642 $b = dechex( intval( ( ( $maxb - $minb ) * $scale_color ) + $minb ) );
643
644 if ( strlen( $r ) == 1 ) {
645 $r = '0' . $r;
646 }
647 if ( strlen( $g ) == 1 ) {
648 $g = '0' . $g;
649 }
650 if ( strlen( $b ) == 1 ) {
651 $b = '0' . $b;
652 }
653
654 return '#' . $r . $g . $b;
655 }
656
657 /**
658 * Extend the round PHP public static function for force a dot for all locales instead the comma.
659 *
660 * @param string $value
661 * @param string $approximation
662 *
663 * @return float
664 * @author WebFactory Ltd
665 */
666 public static function round( $value, $approximation ) {
667 $value = round( $value, $approximation );
668 $value = str_replace( ',', '.', $value ); // Fixes locale comma
669 $value = str_replace( ' ', '', $value ); // No space
670
671 return $value;
672 }
673
674 /**
675 * Format nice URL depending service
676 *
677 * @param string $type
678 * @param string $term_name
679 *
680 * @return string
681 */
682 public static function format_external_tag( $type = '', $term_name = '' ) {
683 if ( empty( $term_name ) ) {
684 return '';
685 }
686
687 $term_name = esc_html( $term_name );
688 switch ( $type ) {
689 case 'technorati':
690 return '<a class="tag_technorati" href="' . esc_url( 'http://technorati.com/tag/' . str_replace( ' ', '+', $term_name ) ) . '" rel="tag">' . $term_name . '</a>';
691 break;
692 case 'flickr':
693 return '<a class="tag_flickr" href="' . esc_url( 'http://www.flickr.com/photos/tags/' . preg_replace( '/[^a-zA-Z0-9]/', '', strtolower( $term_name ) ) . '/' ) . '" rel="tag">' . $term_name . '</a>';
694 break;
695 case 'delicious':
696 return '<a class="tag_delicious" href="' . esc_url( 'http://del.icio.us/popular/' . strtolower( str_replace( ' ', '', $term_name ) ) ) . '" rel="tag">' . $term_name . '</a>';
697 break;
698 default:
699 return '';
700 break;
701 }
702 }
703 }
704