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

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

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