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

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

458 lines 15.3 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 // Load translation
11 add_action( 'init', array( __CLASS__, 'init_translation' ) );
12
13 // Register media tags taxonomy
14 add_action( 'init', array( $this, 'simple_tags_register_media_tag' ) );
15
16 require( STAGS_DIR . '/inc/class.client.autolinks.php' );
17 new SimpleTags_Client_Autolinks();
18
19 // Call tag clouds ?
20 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_terms_display' ) ) {
21 require_once STAGS_DIR . '/inc/tag-clouds-action.php';
22 }
23
24 // Call post tags ?
25 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_post_tags' ) ) {
26 require_once STAGS_DIR . '/inc/post-tags-action.php';
27 }
28
29 // Call related posts ?
30 if ( (int) SimpleTags_Plugin::get_option_value( 'active_related_posts_new' ) === 1 ) {
31 require_once STAGS_DIR . '/inc/related-posts-action.php';
32 }
33 if ( (int) SimpleTags_Plugin::get_option_value( 'active_related_posts' ) === 1 || (int) SimpleTags_Plugin::get_option_value( 'active_related_posts_new' ) === 1 ) {
34 require( STAGS_DIR . '/inc/class.client.related_posts.php' );
35 new SimpleTags_Client_RelatedPosts();
36 }
37
38 // Call auto terms ?
39 require( STAGS_DIR . '/inc/class.client.autoterms.php' );
40 new SimpleTags_Client_Autoterms();
41
42 // Call post tags ?
43 require( STAGS_DIR . '/inc/class.client.post_tags.php' );
44 new SimpleTags_Client_PostTags();
45
46
47 if ( (int)SimpleTags_Plugin::get_option_value( 'active_taxonomies' ) === 1 ) {
48 require_once STAGS_DIR . '/inc/taxonomies-action.php';
49 add_action( 'parse_query', array( __CLASS__, 'cpt_taxonomy_parse_query' ) );
50 if (defined('STAGS_OPTIONS_NAME')) {
51 $saved_option = (array)get_option( STAGS_OPTIONS_NAME );
52 if ((array_key_exists('use_tag_pages', $saved_option))) {
53 if((int)$saved_option['use_tag_pages'] === 1){
54 if ( !array_key_exists('post_tag', taxopress_get_extername_taxonomy_data()) ) {
55 add_action( 'init', array( __CLASS__, 'init' ), 11 );
56 add_action( 'parse_query', array( __CLASS__, 'parse_query' ) );
57 }
58 }
59 }
60 }
61 }
62
63 return true;
64 }
65
66
67 /**
68 * Add cpt to taxonomy during the query
69 *
70 * @param WP_Query $query
71 *
72 * @return void
73 * @author Olatechpro
74 */
75 public static function cpt_taxonomy_parse_query( $query ) {
76 if(function_exists('get_current_screen')){
77 $screen = get_current_screen();
78 }
79
80 if(is_admin()){
81 return $query;
82 }
83 if(isset($screen->id) && $screen->id == 'edit-post'){
84 return $query;
85 }
86 if ( $query->is_category == true || $query->is_tag == true || $query->is_tax == true ) {
87 $get_queried_object = @get_queried_object();
88 if(is_object($get_queried_object)){
89 if(!isset($get_queried_object->taxonomy)){
90 return $query;
91 }
92 if(!taxopress_show_all_cpt_in_archive_result($get_queried_object->taxonomy)){
93 return $query;
94 }
95 $get_taxonomy = get_taxonomy( $get_queried_object->taxonomy );
96 if(is_object($get_taxonomy)){
97 $post_types = $get_taxonomy->object_type;
98 if ( isset( $query->query_vars['post_type'] )){
99 if(is_array( $query->query_vars['post_type'] )){
100 $post_types = array_filter(array_merge($query->query_vars['post_type'],$post_types));
101 }elseif(is_string( $query->query_vars['post_type'] )){
102 $original_post_type = $query->query_vars['post_type'];
103 $get_post_type_object = get_post_type_object( $original_post_type );
104 if(is_object($get_post_type_object)){
105 if((int)$get_post_type_object->public === 0){
106 return $query;
107 }
108 }
109 $post_types[] = $query->query_vars['post_type'];
110 }
111 $new_post_object = $post_types;
112 }else{
113 $new_post_object = $post_types;
114 }
115 $query->query_vars['post_type'] = $new_post_object;
116 }
117 }
118 }
119 return $query;
120 }
121
122 /**
123 * Taxonomy: Media Tags.
124 */
125 public function simple_tags_register_media_tag() {
126
127 if((int)get_option('taxopress_media_tag_deleted') === 0){
128 $labels = [
129 "name" => __( "Media Tags", "simple-tags" ),
130 "singular_name" => __( "Media Tag", "simple-tags" ),
131 "menu_name" => __( "Media Tags", "simple-tags" ),
132 "all_items" => __( "All Media Tags", "simple-tags" ),
133 "edit_item" => __( "Edit Media Tag", "simple-tags" ),
134 "view_item" => __( "View Media Tag", "simple-tags" ),
135 "update_item" => __( "Update Media Tag name", "simple-tags" ),
136 "add_new_item" => __( "Add new Media Tag", "simple-tags" ),
137 "new_item_name" => __( "New Media Tag name", "simple-tags" ),
138 "parent_item" => __( "Parent Media Tag", "simple-tags" ),
139 "parent_item_colon" => __( "Parent Media Tag:", "simple-tags" ),
140 "search_items" => __( "Search Media Tags", "simple-tags" ),
141 "popular_items" => __( "Popular Media Tags", "simple-tags" ),
142 "separate_items_with_commas" => __( "Separate Media Tags with commas", "simple-tags" ),
143 "add_or_remove_items" => __( "Add or remove Media Tags", "simple-tags" ),
144 "choose_from_most_used" => __( "Choose from the most used Media Tags", "simple-tags" ),
145 "not_found" => __( "No Media Tags found", "simple-tags" ),
146 "no_terms" => __( "No Media Tags", "simple-tags" ),
147 "items_list_navigation" => __( "Media Tags list navigation", "simple-tags" ),
148 "items_list" => __( "Media Tags list", "simple-tags" ),
149 "back_to_items" => __( "Back to Media Tags", "simple-tags" ),
150 ];
151
152 $args = [
153 "label" => __( "Media Tags", "simple-tags" ),
154 "labels" => $labels,
155 "public" => true,
156 "publicly_queryable" => true,
157 "hierarchical" => false,
158 "show_ui" => true,
159 "show_in_menu" => true,
160 "show_in_nav_menus" => true,
161 "query_var" => true,
162 "update_count_callback" => '_update_generic_term_count',
163 "rewrite" => [ 'slug' => 'media_tag', 'with_front' => true, ],
164 "show_admin_column" => false,
165 "show_in_rest" => true,
166 "rest_base" => "media_tag",
167 "rest_controller_class" => "WP_REST_Terms_Controller",
168 "show_in_quick_edit" => false,
169 ];
170 register_taxonomy( "media_tag", [ "attachment" ], $args );
171 }
172 }
173
174 /**
175 * Load translations
176 */
177 public static function init_translation() {
178 load_plugin_textdomain( 'simple-tags', false, basename( STAGS_DIR ) . '/languages' );
179 }
180
181 /**
182 * Register taxonomy post_tags for page post type
183 *
184 * @return void
185 * @author WebFactory Ltd
186 */
187 public static function init() {
188 register_taxonomy_for_object_type( 'post_tag', 'page' );
189 }
190
191 /**
192 * Add page post type during the query
193 *
194 * @param WP_Query $query
195 *
196 * @return void
197 * @author WebFactory Ltd
198 */
199 public static function parse_query( $query ) {
200 if(function_exists('get_current_screen')){
201 $screen = get_current_screen();
202 }
203
204 if(isset($screen->id) && $screen->id == 'edit-post'){
205 return $query;
206 }
207
208 if ( $query->is_tag == true ) {
209 if ( isset( $query->query_vars['post_type'] ) && is_array( $query->query_vars['post_type'] ) ) {
210 $query->query_vars['post_type'][] = 'page';
211 }else{
212 $query->query_vars['post_type'] = array( 'post', 'page' );
213 }
214 }
215 }
216
217 /**
218 * Randomize an array and keep association
219 *
220 * @param array $array
221 *
222 * @return boolean
223 */
224 public static function random_array( &$array ) {
225 if ( ! is_array( $array ) || empty( $array ) ) {
226 return false;
227 }
228
229 $keys = array_keys( $array );
230 shuffle( $keys );
231
232 $new = array();
233 foreach ( (array) $keys as $key ) {
234 $new[ $key ] = $array[ $key ];
235 }
236
237 $array = $new;
238
239 return true;
240 }
241
242 /**
243 * Build rel for tag link
244 *
245 * @return string
246 */
247 public static function get_rel_attribut() {
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 * Format data for output
259 *
260 * @param string $html_class
261 * @param string $format
262 * @param string $title
263 * @param string $content
264 * @param boolean $copyright
265 * @param string $separator
266 *
267 * @return string|array
268 */
269 public static function output_content( $html_class = '', $format = 'list', $title = '', $content = '', $copyright = true, $separator = '', $div_class = '', $a_class = '' ) {
270 if ( empty( $content ) ) {
271 return ''; // return nothing
272 }
273
274 if ( $format == 'array' && is_array( $content ) ) {
275 return $content; // Return PHP array if format is array
276 }
277
278 if ( is_array( $content ) ) {
279 switch ( $format ) {
280 case 'list' :
281 $output = '<ul class="' . $html_class . '">' . "\n\t" . '<li>' . implode( "</li>\n\t<li>", $content ) . "</li>\n</ul>\n";
282 break;
283 default :
284 $output = '<div class="' . $html_class . '">' . "\n\t" . implode( "{$separator}\n", $content ) . "</div>\n";
285 break;
286 }
287 } else {
288 $content = trim( $content );
289 switch ( $format ) {
290 case 'string' :
291 $output = $content;
292 break;
293 case 'list' :
294 $output = '<ul class="' . $html_class . '">' . "\n\t" . '<li>' . $content . "</li>\n\t" . "</ul>\n";
295 break;
296 default :
297 $output = '<div class="' . $html_class . '">' . "\n\t" . $content . "</div>\n";
298 break;
299 }
300 }
301
302 //wrap class
303 if(!empty(trim($div_class))){
304 $wrap_div_class_open = '<div class="'.taxopress_format_class($div_class).'">';
305 $wrap_div_class_close = '</div>';
306 }else{
307 $wrap_div_class_open = '';
308 $wrap_div_class_close = '';
309 }
310 // Replace false by empty
311 $title = trim( $title );
312 if ( strtolower( $title ) == 'false' ) {
313 $title = '';
314 }
315
316 // Put title if exist
317 if ( ! empty( $title ) ) {
318 $title .= "\n\t";
319 }
320
321 if ( $copyright === true ) {
322 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";
323 } else {
324 return "\n\t" . $wrap_div_class_open . $title . $output . $wrap_div_class_close . "\n";
325 }
326 }
327
328 /**
329 * Remplace marker by dynamic values (use for related tags, current tags and tag cloud)
330 *
331 * @param string $element_loop
332 * @param object $term
333 * @param string $rel
334 * @param integer $scale_result
335 * @param integer $scale_max
336 * @param integer $scale_min
337 * @param integer $largest
338 * @param integer $smallest
339 * @param string $unit
340 * @param string $maxcolor
341 * @param string $mincolor
342 *
343 * @return string
344 */
345 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 = '' ) {
346 // Need term object
347 $element_loop = str_replace( '%tag_link%', esc_url( get_term_link( $term, $term->taxonomy ) ), $element_loop );
348 $element_loop = str_replace( '%tag_feed%', esc_url( get_term_feed_link( $term->term_id, $term->taxonomy, '' ) ), $element_loop );
349
350 $element_loop = str_replace( '%tag_name%', esc_html( $term->name ), $element_loop );
351 $element_loop = str_replace( '%tag_name_attribute%', esc_html( strip_tags( $term->name ) ), $element_loop );
352 $element_loop = str_replace( '%tag_id%', $term->term_id, $element_loop );
353 $element_loop = str_replace( '%tag_count%', (int) $term->count, $element_loop );
354
355 // Need rel
356 $element_loop = str_replace( '%tag_rel%', $rel, $element_loop );
357
358 // Need max/min/scale and other :)
359 if ( $scale_result !== null ) {
360 $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 );
361 $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 );
362 $element_loop = str_replace( '%tag_scale%', $scale_result, $element_loop );
363 }
364
365 // External link
366 $element_loop = str_replace( '%tag_technorati%', self::format_external_tag( 'technorati', $term->name ), $element_loop );
367 $element_loop = str_replace( '%tag_flickr%', self::format_external_tag( 'flickr', $term->name ), $element_loop );
368 $element_loop = str_replace( '%tag_delicious%', self::format_external_tag( 'delicious', $term->name ), $element_loop );
369
370 return $element_loop;
371 }
372
373 /**
374 * This is pretty filthy. Doing math in hex is much too weird. It's more likely to work, this way!
375 * Provided from UTW. Thanks.
376 *
377 * @param integer $scale_color
378 * @param string $min_color
379 * @param string $max_color
380 *
381 * @return string
382 */
383 public static function get_color_by_scale( $scale_color, $min_color, $max_color ) {
384 $scale_color = $scale_color / 100;
385
386 $minr = hexdec( substr( $min_color, 1, 2 ) );
387 $ming = hexdec( substr( $min_color, 3, 2 ) );
388 $minb = hexdec( substr( $min_color, 5, 2 ) );
389
390 $maxr = hexdec( substr( $max_color, 1, 2 ) );
391 $maxg = hexdec( substr( $max_color, 3, 2 ) );
392 $maxb = hexdec( substr( $max_color, 5, 2 ) );
393
394 $r = dechex( intval( ( ( $maxr - $minr ) * $scale_color ) + $minr ) );
395 $g = dechex( intval( ( ( $maxg - $ming ) * $scale_color ) + $ming ) );
396 $b = dechex( intval( ( ( $maxb - $minb ) * $scale_color ) + $minb ) );
397
398 if ( strlen( $r ) == 1 ) {
399 $r = '0' . $r;
400 }
401 if ( strlen( $g ) == 1 ) {
402 $g = '0' . $g;
403 }
404 if ( strlen( $b ) == 1 ) {
405 $b = '0' . $b;
406 }
407
408 return '#' . $r . $g . $b;
409 }
410
411 /**
412 * Extend the round PHP public static function for force a dot for all locales instead the comma.
413 *
414 * @param string $value
415 * @param string $approximation
416 *
417 * @return float
418 * @author WebFactory Ltd
419 */
420 public static function round( $value, $approximation ) {
421 $value = round( $value, $approximation );
422 $value = str_replace( ',', '.', $value ); // Fixes locale comma
423 $value = str_replace( ' ', '', $value ); // No space
424
425 return $value;
426 }
427
428 /**
429 * Format nice URL depending service
430 *
431 * @param string $type
432 * @param string $term_name
433 *
434 * @return string
435 */
436 public static function format_external_tag( $type = '', $term_name = '' ) {
437 if ( empty( $term_name ) ) {
438 return '';
439 }
440
441 $term_name = esc_html( $term_name );
442 switch ( $type ) {
443 case 'technorati':
444 return '<a class="tag_technorati" href="' . esc_url( 'http://technorati.com/tag/' . str_replace( ' ', '+', $term_name ) ) . '" rel="tag">' . $term_name . '</a>';
445 break;
446 case 'flickr':
447 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>';
448 break;
449 case 'delicious':
450 return '<a class="tag_delicious" href="' . esc_url( 'http://del.icio.us/popular/' . strtolower( str_replace( ' ', '', $term_name ) ) ) . '" rel="tag">' . $term_name . '</a>';
451 break;
452 default:
453 return '';
454 break;
455 }
456 }
457 }
458