PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.50.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.50.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.50.0, at inc/class.client.php

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