PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.6.2
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.6.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.admin.manage.php

class.admin.manage.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.6.2, at inc/class.admin.manage.php

1,018 lines 34.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_Admin_Manage {
4
5 const MENU_SLUG = 'st_options';
6
7 // class instance
8 static $instance;
9
10 // WP_List_Table object
11 public $terms_table;
12
13 /**
14 * Constructor
15 *
16 * @return void
17 * @author WebFactory Ltd
18 */
19 public function __construct() {
20
21 add_filter( 'set-screen-option', [ __CLASS__, 'set_screen' ], 10, 3 );
22 // Admin menu
23 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
24
25 // Register taxo, parent method...
26 SimpleTags_Admin::register_taxonomy();
27
28 // Javascript
29 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ), 11 );
30 }
31
32 /**
33 * Init somes JS and CSS need for this feature
34 *
35 * @return void
36 * @author WebFactory Ltd
37 */
38 public static function admin_enqueue_scripts() {
39 wp_register_script( 'st-helper-manage', STAGS_URL . '/assets/js/helper-manage.js', array( 'jquery' ), STAGS_VERSION );
40
41 // add JS for manage click tags
42 if ( isset( $_GET['page'] ) && $_GET['page'] == 'st_manage' ) {
43 wp_enqueue_script( 'st-helper-manage' );
44 }
45 }
46
47 public static function set_screen( $status, $option, $value ) {
48 return $value;
49 }
50
51 /**
52 * Add WP admin menu for Tags
53 *
54 * @return void
55 * @author WebFactory Ltd
56 */
57 public function admin_menu() {
58 $hook = add_submenu_page(
59 self::MENU_SLUG,
60 __( 'TaxoPress: Manage Terms', 'simple-tags' ),
61 __( 'Manage Terms', 'simple-tags' ),
62 'simple_tags',
63 'st_manage',
64 array(
65 $this,
66 'page_manage_tags',
67 )
68 );
69
70 add_action( "load-$hook", [ $this, 'screen_option' ] );
71 }
72
73 /**
74 * Screen options
75 */
76 public function screen_option() {
77
78 $option = 'per_page';
79 $args = [
80 'label' => esc_html__( 'Number of items per page', 'simple-tags' ),
81 'default' => 10,
82 'option' => 'termcloud_per_page'
83 ];
84
85 add_screen_option( $option, $args );
86
87 $this->terms_table = new Termcloud_List();
88 }
89
90 /**
91 * Method for build the page HTML manage tags
92 *
93 * @return void
94 * @author WebFactory Ltd
95 */
96 public function page_manage_tags() {
97 $default_tab = '';
98 // Control Post data
99 if ( isset( $_POST['term_action'] ) ) {
100 if(!current_user_can('simple_tags')){
101 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'Permission denied!', 'simple-tags' ), 'error' );
102 }else if ( ! wp_verify_nonce( sanitize_text_field($_POST['term_nonce']), 'simpletags_admin' ) ) { // Origination and intention
103
104 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'Security problem. Try again.', 'simple-tags' ), 'error' );
105
106 } elseif ( ! isset( SimpleTags_Admin::$taxonomy ) || ! taxonomy_exists( SimpleTags_Admin::$taxonomy ) ) { // Valid taxo ?
107
108 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'Missing valid taxonomy for work. Try again.', 'simple-tags' ), 'error' );
109
110 } elseif ( $_POST['term_action'] == 'renameterm' ) {
111
112 $oldtag = ( isset( $_POST['renameterm_old'] ) ) ? sanitize_text_field($_POST['renameterm_old']) : '';
113 $newtag = ( isset( $_POST['renameterm_new'] ) ) ? sanitize_text_field($_POST['renameterm_new']) : '';
114 self::renameTerms( SimpleTags_Admin::$taxonomy, $oldtag, $newtag );
115 $default_tab = '.st-rename-terms';
116
117 } elseif ( $_POST['term_action'] == 'mergeterm' ) {
118
119 $oldtag = ( isset( $_POST['renameterm_old'] ) ) ? sanitize_text_field($_POST['renameterm_old']) : '';
120 $newtag = ( isset( $_POST['renameterm_new'] ) ) ? sanitize_text_field($_POST['renameterm_new']) : '';
121 self::mergeTerms( SimpleTags_Admin::$taxonomy, $oldtag, $newtag );
122 $default_tab = '.st-merge-terms';
123
124 } elseif ( $_POST['term_action'] == 'removeterms' ) {
125
126 $tag = ( isset( $_POST['remove_term_input'] ) ) ? sanitize_text_field($_POST['remove_term_input']) : '';
127 self::removeTerms( SimpleTags_Admin::$taxonomy, SimpleTags_Admin::$post_type, $tag );
128 $default_tab = '.st-remove-terms';
129
130 } elseif ( $_POST['term_action'] == 'deleteterm' ) {
131
132 $todelete = ( isset( $_POST['deleteterm_name'] ) ) ? sanitize_text_field($_POST['deleteterm_name']) : '';
133 self::deleteTermsByTermList( SimpleTags_Admin::$taxonomy, $todelete );
134 $default_tab = '.st-delete-terms';
135
136 } elseif ( $_POST['term_action'] == 'addterm' ) {
137
138 $matchtag = ( isset( $_POST['addterm_match'] ) ) ? sanitize_text_field($_POST['addterm_match']) : '';
139 $newtag = ( isset( $_POST['addterm_new'] ) ) ? sanitize_text_field($_POST['addterm_new']) : '';
140 self::addMatchTerms( SimpleTags_Admin::$taxonomy, $matchtag, $newtag );
141 $default_tab = '.st-add-terms';
142
143 } elseif ( $_POST['term_action'] == 'remove-rarelyterms' ) {
144
145 self::removeRarelyUsed( SimpleTags_Admin::$taxonomy, (int) $_POST['number-rarely'] );
146 $default_tab = '.st-delete-unuused-terms';
147
148 } /* elseif ( $_POST['term_action'] == 'editslug' ) {
149
150 $matchtag = (isset($_POST['tagname_match'])) ? $_POST['tagname_match'] : '';
151 $newslug = (isset($_POST['tagslug_new'])) ? $_POST['tagslug_new'] : '';
152 self::editTermSlug( SimpleTags_Admin::$taxonomy, $matchtag, $newslug );
153
154 }*/
155 }
156
157 if($default_tab && !empty($default_tab)){
158 //trigger default tab click on load
159 echo '<div class="load-st-default-tab" data-page="'.esc_attr($default_tab).'"></div>';
160 }
161
162 // Default order
163 if ( ! isset( $_GET['order'] ) ) {
164 $_GET['order'] = 'name-asc';
165 }
166
167 settings_errors( __CLASS__ );
168 ?>
169 <div class="clear"></div>
170 <div class="wrap st_wrap st-manage-terms-page">
171 <?php SimpleTags_Admin::boxSelectorTaxonomy( 'st_manage' ); ?>
172
173 <h2><?php _e( 'TaxoPress: Manage Terms', 'simple-tags' ); ?></h2>
174
175 <div class="clear"></div>
176 <div id="">
177 <h3><?php _e( 'Click terms list:', 'simple-tags' ); ?></h3>
178
179
180 <?php
181 if (isset($_REQUEST['s']) && $search = sanitize_text_field(wp_unslash($_REQUEST['s']))) {
182 /* translators: %s: search keywords */
183 printf(' <span class="subtitle">' . esc_html__('Search results for &#8220;%s&#8221;', 'simple-tags') . '</span>', esc_html($search));
184 }
185 ?>
186 <?php
187
188 //the terms table instance
189 $this->terms_table->prepare_items();
190 ?>
191
192
193 <hr class="wp-header-end">
194 <div id="ajax-response"></div>
195 <form class="search-form wp-clearfix st-tag-cloud-search-form" method="get">
196 <?php $this->terms_table->search_box(sprintf(esc_html__('Search %s ', 'simple-tags'), SimpleTags_Admin::$taxo_name), 'term'); ?>
197 </form>
198
199 <fieldset class="manage-term-screen-options">
200 <label for="termcloud_per_page_dummy"><?php echo esc_html__('Pagination ', 'simple-tags'); ?></label><br />
201 <input type="number" step="1" min="1" max="999" class="screen-per-page" id="termcloud_per_page_dummy" maxlength="3" value="<?php echo (int) get_user_option( 'termcloud_per_page' ); ?>">
202 <input type="submit" id="termcloud_per_page_dummy_apply" class="button button-primary" value="Apply">
203 </fieldset>
204
205 <div class="clear"></div>
206
207 <div id="col-container" class="wp-clearfix">
208
209
210 <div id="col-left">
211
212 <div class="col-wrap">
213 <form action="<?php echo esc_url(add_query_arg('', '')); ?>" method="post">
214 <?php $this->terms_table->display(); //Display the table ?>
215 </form>
216 <div class="form-wrap edit-term-notes">
217 <p><?php esc_html__('Description here.', 'simple-tags') ?></p>
218 </div>
219 </div>
220
221 </div>
222
223
224 <div id="col-right">
225
226
227
228
229 <div class="col-wrap">
230 <div class="form-wrap">
231 <ul class="simple-tags-nav-tab-wrapper">
232 <li class="nav-tab nav-tab-active" data-page=".st-add-terms"><?php echo esc_html__( 'Add terms', 'simple-tags' ); ?></li>
233 <li class="nav-tab" data-page=".st-rename-terms"><?php echo esc_html__( 'Rename terms', 'simple-tags' ); ?></li>
234 <li class="nav-tab" data-page=".st-merge-terms"><?php echo esc_html__( 'Merge terms', 'simple-tags' ); ?></li>
235 <li class="nav-tab" data-page=".st-remove-terms"><?php echo esc_html__( 'Remove terms', 'simple-tags' ); ?></li>
236 <li class="nav-tab" data-page=".st-delete-terms"><?php echo esc_html__( 'Delete terms', 'simple-tags' ); ?></li>
237 <li class="nav-tab" data-page=".st-delete-unuused-terms"><?php echo esc_html__( 'Delete unused terms', 'simple-tags' ); ?></li>
238 </ul>
239 <div class="clear"></div>
240
241
242
243 <table class="form-table">
244
245
246
247 <tr valign="top" class="auto-terms-content st-add-terms">
248 <td>
249 <h2><?php _e( 'Add Terms', 'simple-tags' ); ?></h2>
250 <p><?php printf(esc_html__('This feature lets you add one or more new terms to all %s which match any of the terms given.', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)); ?></p>
251
252 <p><?php printf(esc_html__('Terms will be added to all %s If no "Term(s) to match" is specified.', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)); ?></p>
253
254 <fieldset>
255 <form action="" method="post">
256 <input type="hidden" name="taxo"
257 value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
258 <input type="hidden" name="cpt"
259 value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
260
261 <input type="hidden" name="term_action" value="addterm"/>
262 <input type="hidden" name="term_nonce"
263 value="<?php echo esc_attr(wp_create_nonce( 'simpletags_admin' )); ?>"/>
264
265 <p class="terms-type-options">
266 <label><input type="radio" id="addterm_type" class="addterm_type_all_posts" name="addterm_type" value="all_posts" checked="checked"><?php printf(esc_html__('Add terms to all %s.', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)); ?></label><br>
267
268 <label><input type="radio" id="addterm_type" class="addterm_type_matched_only" name="addterm_type" value="matched_only"><?php _e( 'Add terms only to posts with specific terms attached.', 'simple-tags' ); ?></label>
269 </p>
270
271 <p class="terms-to-maatch-input" style="display: none;">
272 <label for="addterm_match"><?php _e( 'Term(s) to match:', 'simple-tags' ); ?></label>
273 <br/>
274 <input type="text" class="autocomplete-input tag-cloud-input" id="addterm_match"
275 name="addterm_match"
276 value="" size="80"/>
277 </p>
278
279 <p>
280 <label for="addterm_new"><?php _e( 'Term(s) to add:', 'simple-tags' ); ?></label>
281 <br/>
282 <input type="text" class="autocomplete-input" id="addterm_new" name="addterm_new"
283 value="" size="80"/>
284 </p>
285
286 <input class="button-primary" type="submit" name="Add"
287 value="<?php _e( 'Add', 'simple-tags' ); ?>"/>
288 </form>
289 </fieldset>
290 </td>
291 </tr>
292
293 <tr valign="top" style="display:none;" class="auto-terms-content st-rename-terms">
294 <td>
295 <h2><?php _e( 'Rename Terms', 'simple-tags' ); ?> </h2>
296 <p><?php _e( 'Enter the terms to rename and their new names.', 'simple-tags' ); ?></p>
297
298 <fieldset>
299 <form action="" method="post">
300 <input type="hidden" name="taxo"
301 value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
302 <input type="hidden" name="cpt"
303 value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
304
305 <input type="hidden" name="term_action" value="renameterm"/>
306 <input type="hidden" name="term_nonce"
307 value="<?php echo esc_attr(wp_create_nonce( 'simpletags_admin' )); ?>"/>
308
309 <p>
310 <label
311 for="renameterm_old"><?php _e( 'Term(s) to rename:', 'simple-tags' ); ?></label>
312 <br/>
313 <input type="text" class="autocomplete-input tag-cloud-input" id="renameterm_old"
314 name="renameterm_old"
315 value="" size="80"/>
316 </p>
317
318 <p>
319 <label for="renameterm_new"><?php _e( 'New term name(s):', 'simple-tags' ); ?>
320 <br/>
321 <input type="text" class="autocomplete-input" id="renameterm_new"
322 name="renameterm_new" value="" size="80"/>
323 </p>
324
325 <input class="button-primary" type="submit" name="rename"
326 value="<?php _e( 'Rename', 'simple-tags' ); ?>"/>
327 </form>
328 </fieldset>
329 </td>
330 </tr>
331
332 <tr valign="top" style="display:none;" class="auto-terms-content st-merge-terms">
333 <td>
334 <h2><?php _e( 'Merge Terms', 'simple-tags' ); ?> </h2>
335 <p><?php printf(esc_html__('Enter the term to merge and its new value. Click "Merge" and all %s which use this term will be updated.', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)); ?></p>
336
337
338 <fieldset>
339 <form action="" method="post">
340 <input type="hidden" name="taxo"
341 value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
342 <input type="hidden" name="cpt"
343 value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
344
345 <input type="hidden" name="term_action" value="mergeterm"/>
346 <input type="hidden" name="term_nonce"
347 value="<?php echo esc_attr(wp_create_nonce( 'simpletags_admin' )); ?>"/>
348
349 <p>
350 <label
351 for="renameterm_old"><?php _e( 'Term(s) to merge:', 'simple-tags' ); ?></label>
352 <br/>
353 <input type="text" class="autocomplete-input tag-cloud-input" id="mergeterm_old"
354 name="renameterm_old"
355 value="" size="80"/>
356 </p>
357
358 <p>
359 <label for="renameterm_new"><?php _e( 'New term name:', 'simple-tags' ); ?>
360 <br/>
361 <input type="text" class="autocomplete-input" id="renameterm_new"
362 name="renameterm_new" value="" size="80"/>
363 </p>
364
365 <input class="button-primary" type="submit" name="merge"
366 value="<?php _e( 'Merge', 'simple-tags' ); ?>"/>
367 </form>
368 </fieldset>
369 </td>
370 </tr>
371
372 <tr valign="top" style="display:none;" class="auto-terms-content st-remove-terms">
373 <td>
374 <h2><?php echo sprintf(esc_html__('Remove Terms from %s ', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)) ?></h2>
375 <p><?php echo sprintf(esc_html__('Enter the terms to remove from all %s ', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)) ?></p>
376
377
378 <fieldset>
379 <form action="" method="post">
380 <input type="hidden" name="taxo"
381 value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
382 <input type="hidden" name="cpt"
383 value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
384
385 <input type="hidden" name="term_action" value="removeterms"/>
386 <input type="hidden" name="term_nonce"
387 value="<?php echo esc_attr(wp_create_nonce( 'simpletags_admin' )); ?>"/>
388
389 <p>
390 <label
391 for="renameterm_old"><?php _e( 'Term(s) to remove:', 'simple-tags' ); ?></label>
392 <br/>
393 <input type="text" class="autocomplete-input tag-cloud-input" id="remove_term_input"
394 name="remove_term_input"
395 value="" size="80"/>
396 </p>
397
398 <input class="button-primary" type="submit" name="rename"
399 value="<?php _e( 'Remove', 'simple-tags' ); ?>"/>
400 </form>
401 </fieldset>
402 </td>
403 </tr>
404
405 <tr valign="top" style="display:none;" class="auto-terms-content st-delete-terms">
406 <td>
407 <h2><?php _e( 'Delete Terms', 'simple-tags' ); ?></h2>
408 <p><?php _e( 'Enter the name of terms to delete.', 'simple-tags' ); ?></p>
409
410
411 <fieldset>
412 <form action="" method="post">
413 <input type="hidden" name="taxo"
414 value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
415 <input type="hidden" name="cpt"
416 value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
417
418 <input type="hidden" name="term_action" value="deleteterm"/>
419 <input type="hidden" name="term_nonce"
420 value="<?php echo esc_attr(wp_create_nonce( 'simpletags_admin' )); ?>"/>
421
422 <p>
423 <label
424 for="deleteterm_name"><?php _e( 'Term(s) to delete:', 'simple-tags' ); ?></label>
425 <br/>
426 <input type="text" class="autocomplete-input tag-cloud-input" id="deleteterm_name"
427 name="deleteterm_name" value="" size="80"/>
428 </p>
429
430 <input class="button-primary" type="submit" name="delete"
431 value="<?php _e( 'Delete', 'simple-tags' ); ?>"/>
432 </form>
433 </fieldset>
434 </td>
435 </tr>
436
437 <tr valign="top" style="display:none;" class="auto-terms-content st-delete-unuused-terms">
438 <td>
439 <h2><?php esc_html_e( 'Remove rarely used terms', 'simple-tags' ); ?></h2>
440 <p><?php esc_html_e( 'This feature allows you to remove rarely used terms.', 'simple-tags' ); ?></p>
441
442 <p><?php printf(esc_html__('If you choose 5, Taxopress will delete all terms attached to less than 5 %s.', 'simple-tags'), esc_html(SimpleTags_Admin::$post_type_name)); ?></p>
443
444 <fieldset>
445 <form action="" method="post">
446 <input type="hidden" name="taxo"
447 value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
448 <input type="hidden" name="cpt"
449 value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
450
451 <input type="hidden" name="term_action" value="remove-rarelyterms"/>
452 <input type="hidden" name="term_nonce"
453 value="<?php echo esc_attr(wp_create_nonce( 'simpletags_admin' )); ?>"/>
454
455 <p>
456 <label for="number-delete"><?php _e( 'Minimum number of uses for each term:', 'simple-tags' ); ?></label>
457 <br/>
458 <select name="number-rarely" id="number-delete">
459 <?php for ( $i = 1; $i <= 100; $i ++ ) : ?>
460 <option value="<?php echo esc_attr($i); ?>"><?php echo esc_html($i); ?></option>
461 <?php endfor; ?>
462 </select>
463 </p>
464
465 <input class="button-primary" type="submit" name="Delete"
466 value="<?php esc_attr_e( 'Delete rarely used', 'simple-tags' ); ?>"/>
467 </form>
468 </fieldset>
469 </td>
470 </tr>
471
472 <?php /*
473 <tr valign="top">
474 <th scope="row"><strong><?php _e('Edit Term Slug', 'simple-tags'); ?></strong></th>
475 <td>
476 <p><?php _e('Enter the term name to edit and its new slug. <a href="http://codex.wordpress.org/Glossary#Slug">Slug definition</a>', 'simple-tags'); ?></p>
477
478 <fieldset>
479 <form action="" method="post">
480 <input type="hidden" name="taxo" value="<?php echo esc_attr(SimpleTags_Admin::$taxonomy); ?>" />
481 <input type="hidden" name="cpt" value="<?php echo esc_attr(SimpleTags_Admin::$post_type); ?>" />
482
483 <input type="hidden" name="term_action" value="editslug" />
484 <input type="hidden" name="term_nonce" value="<?php echo wp_create_nonce('simpletags_admin'); ?>" />
485
486 <p>
487 <label for="tagname_match"><?php _e('Term(s) to match:', 'simple-tags'); ?></label>
488 <br />
489 <input type="text" class="autocomplete-input" id="tagname_match" name="tagname_match" value="" size="80" />
490 </p>
491
492 <p>
493 <label for="tagslug_new"><?php _e('Slug(s) to set:', 'simple-tags'); ?></label>
494 <br />
495 <input type="text" class="autocomplete-input" id="tagslug_new" name="tagslug_new" value="" size="80" />
496 </p>
497
498 <input class="button-primary" type="submit" name="edit" value="<?php _e('Edit', 'simple-tags'); ?>" />
499 </form>
500 </fieldset>
501 </td>
502 </tr>
503 */
504 ?>
505
506 </table>
507
508
509
510 </div>
511 </div>
512
513
514 </div>
515
516
517
518
519 <div class="clear"></div>
520
521 </div>
522
523
524 </div>
525
526
527
528 <?php SimpleTags_Admin::printAdminFooter(); ?>
529 </div>
530
531
532
533 <?php
534 do_action( 'simpletags-manage_terms', SimpleTags_Admin::$taxonomy );
535 }
536
537 /**
538 * Method to merge tags
539 *
540 * @param string $taxonomy
541 * @param string $old
542 * @param string $new
543 *
544 * @return boolean
545 * @author olatechpro
546 */
547 public static function mergeTerms( $taxonomy = 'post_tag', $old = '', $new = '' ) {
548 if ( trim( str_replace( ',', '', stripslashes( $new ) ) ) == '' ) {
549 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No new term specified!', 'simple-tags' ), 'error' );
550
551 return false;
552 }
553
554 // String to array
555 $old_terms = explode( ',', $old );
556 $new_terms = explode( ',', $new );
557
558 // Remove empty element and trim
559 $old_terms = array_filter( $old_terms, '_delete_empty_element' );
560 $new_terms = array_filter( $new_terms, '_delete_empty_element' );
561
562 // If old/new tag are empty => exit !
563 if ( empty( $old_terms ) || empty( $new_terms ) ) {
564 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No new/old valid term specified!', 'simple-tags' ), 'error' );
565
566 return false;
567 }
568
569 $counter = 0;
570 if ( count( $new_terms ) == 1 ) { // Merge
571 // Set new tag
572 $new_tag = sanitize_text_field($new_terms[0]);
573 if ( empty( $new_tag ) ) {
574 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No valid new term.', 'simple-tags' ), 'error' );
575
576 return false;
577 }
578
579 // Get terms ID from old terms names
580 $terms_id = array();
581 foreach ( (array) $old_terms as $old_tag ) {
582 $term = get_term_by( 'name', addslashes( sanitize_text_field($old_tag) ), $taxonomy );
583 $terms_id[] = (int) $term->term_id;
584 }
585
586 // Get objects from terms ID
587 $objects_id = get_objects_in_term( $terms_id, $taxonomy, array( 'fields' => 'all_with_object_id' ) );
588
589 // No objects ? exit !
590 if ( ! $objects_id ) {
591
592 // Delete old terms
593 foreach ( (array) $terms_id as $term_id ) {
594 wp_delete_term( $term_id, $taxonomy );
595 }
596
597 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Merge term(s) &laquo;%1$s&raquo; to &laquo;%2$s&raquo;. %3$s objects edited.', 'simple-tags' ), $old, $new, $counter ), 'updated' );
598 return true;
599 }
600
601 // Delete old terms
602 foreach ( (array) $terms_id as $term_id ) {
603 wp_delete_term( $term_id, $taxonomy );
604 }
605
606 // Set objects to new term ! (Append no replace)
607 foreach ( (array) $objects_id as $object_id ) {
608 wp_set_object_terms( $object_id, $new_tag, $taxonomy, true );
609 $counter ++;
610 }
611
612 // Test if term is also a category
613 /*
614 if ( is_term($new_tag, 'category') ) {
615 // Edit the slug to use the new term
616 self::editTermSlug( $new_tag, sanitize_title($new_tag) );
617 }
618 */
619
620 // Clean cache
621 clean_object_term_cache( $objects_id, $taxonomy );
622 clean_term_cache( $terms_id, $taxonomy );
623
624 if ( $counter == 0 ) {
625 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term merged.', 'simple-tags' ), 'updated' );
626 } else {
627 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Merge term(s) &laquo;%1$s&raquo; to &laquo;%2$s&raquo;. %3$s objects edited.', 'simple-tags' ), $old, $new, $counter ), 'updated' );
628 }
629 } else { // Error
630 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Error. You need to enter a single term to merge to in new term name !', 'simple-tags' ), $old ), 'error' );
631 }
632
633 return true;
634 }
635
636 /**
637 * Method for remove tags
638 *
639 * @param string $taxonomy
640 * @param string $post_type
641 * @param string $tag
642 *
643 * @return boolean
644 * @author WebFactory Ltd
645 */
646 public static function removeTerms( $taxonomy = 'post_tag', $post_type = 'posts', $new = '' ) {
647 if ( trim( str_replace( ',', '', stripslashes( $new ) ) ) == '' ) {
648 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term specified!', 'simple-tags' ), 'error' );
649
650 return false;
651 }
652
653 // String to array
654 $new_terms = explode( ',', $new );
655
656 // Remove empty element and trim
657 $new_terms = array_filter( $new_terms, '_delete_empty_element' );
658
659 // If new tag are empty => exit !
660 if ( empty( $new_terms ) ) {
661 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No valid term specified!', 'simple-tags' ), 'error' );
662
663 return false;
664 }
665
666 $counter = 0;
667 if ( count($new_terms) > 0 ) {
668 foreach ( (array) $new_terms as $term ) {
669 $term = get_term_by('name', sanitize_text_field($term), $taxonomy);
670 if( empty($term) || !is_object($term) ){
671 continue;
672 }
673
674 $term = $term->term_id;
675
676 $args = array(
677 'post_type' => $post_type, // post_type
678 'posts_per_page' => -1,
679 'tax_query' => array(
680 array(
681 'taxonomy' => $taxonomy,
682 'field' => 'id',
683 'terms' => $term
684 )
685 )
686 );
687 $posts = get_posts($args);
688 foreach ( $posts as $post ){
689 $remove = wp_remove_object_terms( $post->ID, $term, $taxonomy );
690 if($remove){
691 clean_object_term_cache( $post->ID, $taxonomy );
692 clean_term_cache( $term, $taxonomy );
693 $counter ++;
694 }
695 }
696 }
697
698 if ( $counter == 0 ) {
699 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'This term is not associated with any %1$s.', 'simple-tags' ), SimpleTags_Admin::$post_type_name ), 'error' );
700 } else {
701 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Removed term(s) &laquo;%1$s&raquo; from %2$s %3$s', 'simple-tags' ), $new, $counter, SimpleTags_Admin::$post_type_name ), 'updated' );
702 }
703 } else { // Error
704 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Error. No enough terms specified.', 'simple-tags' ), $old ), 'error' );
705 }
706
707 return true;
708 }
709
710 /**
711 * Method for rename tags
712 *
713 * @param string $taxonomy
714 * @param string $old
715 * @param string $new
716 *
717 * @return boolean
718 * @author WebFactory Ltd
719 */
720 public static function renameTerms( $taxonomy = 'post_tag', $old = '', $new = '' ) {
721 if ( trim( str_replace( ',', '', stripslashes( $new ) ) ) == '' ) {
722 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No new term specified!', 'simple-tags' ), 'error' );
723
724 return false;
725 }
726
727 // String to array
728 $old_terms = explode( ',', $old );
729 $new_terms = explode( ',', $new );
730
731 // Remove empty element and trim
732 $old_terms = array_filter($old_terms, '_delete_empty_element');
733 $new_terms = array_filter($new_terms, '_delete_empty_element');
734
735 // If old/new tag are empty => exit !
736 if ( empty( $old_terms ) || empty( $new_terms ) ) {
737 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No new/old valid term specified!', 'simple-tags' ), 'error' );
738
739 return false;
740 }
741
742 $counter = 0;
743 if ( count($old_terms) === count( $new_terms ) ) { // Rename only
744 foreach ( (array) $old_terms as $i => $old_tag ) {
745 $new_name = sanitize_text_field($new_terms[ $i ]);
746
747 // Get term by name
748 $term = get_term_by( 'name', sanitize_text_field($old_tag), $taxonomy );
749 if ( ! $term ) {
750 continue;
751 }
752
753 // Get objects from term ID
754 $objects_id = get_objects_in_term( $term->term_id, $taxonomy, array( 'fields' => 'all_with_object_id' ) );
755
756 // Create the new term
757 if ( ! $term_info = term_exists( $new_name, $taxonomy ) ) {
758 $term_info = wp_insert_term( $new_name, $taxonomy );
759 }
760
761 // If default category, update the ID for new term...
762 if ( 'category' == $taxonomy && $term->term_id == get_option( 'default_category' ) ) {
763 update_option( 'default_category', $term_info['term_id'] );
764 clean_term_cache( $term_info['term_id'], $taxonomy );
765 }
766
767 // Delete old term
768 wp_delete_term( $term->term_id, $taxonomy );
769
770 // Set objects to new term ! (Append no replace)
771 foreach ( (array) $objects_id as $object_id ) {
772 wp_set_object_terms( $object_id, $new_name, $taxonomy, true );
773 }
774
775 // Clean cache
776 clean_object_term_cache( $objects_id, $taxonomy );
777 clean_term_cache( $term->term_id, $taxonomy );
778
779 // Increment
780 $counter ++;
781 }
782
783 if ( $counter == 0 ) {
784 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term renamed.', 'simple-tags' ), 'updated' );
785 } else {
786 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Renamed term(s) &laquo;%1$s&raquo; to &laquo;%2$s&raquo;', 'simple-tags' ), $old, $new ), 'updated' );
787 }
788 } else { // Error
789 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Error. No enough terms for rename.', 'simple-tags' ), $old ), 'error' );
790 }
791
792 return true;
793 }
794
795 /**
796 * Method for delete a list of terms
797 *
798 * @param string $taxonomy
799 * @param string $delete
800 *
801 * @return boolean
802 * @author WebFactory Ltd
803 */
804 public static function deleteTermsByTermList( $taxonomy = 'post_tag', $delete = '' ) {
805 if ( trim( str_replace( ',', '', stripslashes( $delete ) ) ) == '' ) {
806 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term specified!', 'simple-tags' ), 'error' );
807
808 return false;
809 }
810
811 // In array + filter
812 $delete_terms = explode( ',', $delete );
813 $delete_terms = array_filter( $delete_terms, '_delete_empty_element' );
814
815 // Delete tags
816 $counter = 0;
817 foreach ( (array) $delete_terms as $term ) {
818 $term = get_term_by( 'name', sanitize_text_field($term), $taxonomy );
819 $term_id = (int) $term->term_id;
820
821 if ( $term_id != 0 ) {
822 wp_delete_term( $term_id, $taxonomy );
823 clean_term_cache( $term_id, $taxonomy );
824 $counter ++;
825 }
826 }
827
828 if ( $counter == 0 ) {
829 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term deleted.', 'simple-tags' ), 'updated' );
830 } else {
831 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( '%1s term(s) deleted.', 'simple-tags' ), $counter ), 'updated' );
832 }
833
834 return true;
835 }
836
837 /**
838 * Method for add terms for all or specified posts
839 *
840 * @param string $taxonomy
841 * @param string $match
842 * @param string $new
843 *
844 * @return boolean
845 * @author WebFactory Ltd
846 */
847 public static function addMatchTerms( $taxonomy = 'post_tag', $match = '', $new = '' ) {
848 if ( trim( str_replace( ',', '', stripslashes( $new ) ) ) == '' ) {
849 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No new term(s) specified!', 'simple-tags' ), 'error' );
850
851 return false;
852 }
853
854 $match_terms = explode( ',', $match );
855 $new_terms = explode( ',', $new );
856
857 $match_terms = array_filter( $match_terms, '_delete_empty_element' );
858 $new_terms = array_filter( $new_terms, '_delete_empty_element' );
859
860 $counter = 0;
861 if ( ! empty( $match_terms ) ) { // Match and add
862 // Get terms ID from old match names
863 $terms_id = array();
864 foreach ( (array) $match_terms as $match_term ) {
865 $term = get_term_by( 'name', sanitize_text_field($match_term), $taxonomy );
866 $terms_id[] = (int) $term->term_id;
867 }
868
869 // Get object ID with terms ID
870 $objects_id = get_objects_in_term( $terms_id, $taxonomy, array( 'fields' => 'all_with_object_id' ) );
871
872 // Add new tags for specified post
873 foreach ( (array) $objects_id as $object_id ) {
874 wp_set_object_terms( $object_id, $new_terms, $taxonomy, true ); // Append terms
875 $counter ++;
876 }
877
878 // Clean cache
879 clean_object_term_cache( $objects_id, $taxonomy );
880 clean_term_cache( $terms_id, $taxonomy );
881 } else { // Add for all posts
882 // Page or not ?
883 $post_type_sql = "(post_status = 'publish' OR post_status = 'inherit') AND post_type = '".SimpleTags_Admin::$post_type."'";
884
885 // Get all posts ID
886 global $wpdb;
887 $objects_id = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE {$post_type_sql}" );
888
889 // Add new tags for all posts
890 foreach ( (array) $objects_id as $object_id ) {
891 wp_set_object_terms( $object_id, $new_terms, $taxonomy, true ); // Append terms
892 clean_object_term_cache( $object_id, $taxonomy );
893 clean_term_cache( $new_terms, $taxonomy );
894 $counter ++;
895 }
896
897 // Clean cache
898 clean_object_term_cache( $objects_id, $taxonomy );
899 }
900
901 if ( $counter == 0 ) {
902 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term added.', 'simple-tags' ), 'updated' );
903 } else {
904 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( 'Term(s) added to %1s %2s.', 'simple-tags' ), $counter, SimpleTags_Admin::$post_type_name ), 'updated' );
905 }
906
907 return true;
908 }
909
910 /**
911 * Delete terms when counter if inferior to a specific number
912 *
913 * @param string $taxonomy
914 * @param integer $number
915 *
916 * @return boolean
917 * @author WebFactory Ltd
918 */
919 public static function removeRarelyUsed( $taxonomy = 'post_tag', $number = 0 ) {
920 global $wpdb;
921
922 if ( (int) $number > 100 ) {
923 wp_die( 'Tcheater ?' );
924 }
925
926 // Get terms with counter inferior to...
927 $terms_id = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count < %d", $taxonomy, (int) $number ) );
928
929 // Delete terms
930 $counter = 0;
931 foreach ( (array) $terms_id as $term_id ) {
932 if ( $term_id != 0 ) {
933 wp_delete_term( $term_id, $taxonomy );
934 clean_term_cache( $term_id, $taxonomy );
935 $counter ++;
936 }
937 }
938
939 if ( $counter == 0 ) {
940 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'No term deleted.', 'simple-tags' ), 'updated' );
941 } else {
942 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( '%1s term(s) deleted.', 'simple-tags' ), $counter ), 'updated' );
943 }
944
945 return true;
946 }
947
948 /**
949 * Method for edit one or more terms slug
950 *
951 * @param string $taxonomy
952 * @param string $names
953 * @param string $slugs
954 *
955 * @return boolean
956 * @author WebFactory Ltd
957 */
958 /*
959 public static function editTermSlug( $taxonomy = 'post_tag', $names = '', $slugs = '') {
960 if ( trim( str_replace(',', '', stripslashes($slugs)) ) == '' ) {
961 add_settings_error( __CLASS__, __CLASS__, esc_html__('No new slug(s) specified!', 'simple-tags'), 'error' );
962 return false;
963 }
964
965 $match_names = explode(',', $names);
966 $new_slugs = explode(',', $slugs);
967
968 $match_names = array_filter($match_names, '_delete_empty_element');
969 $new_slugs = array_filter($new_slugs, '_delete_empty_element');
970
971 if ( count($match_names) != count($new_slugs) ) {
972 add_settings_error( __CLASS__, __CLASS__, esc_html__('Terms number and slugs number isn\'t the same!', 'simple-tags'), 'error' );
973 return false;
974 } else {
975 $counter = 0;
976 foreach ( (array) $match_names as $i => $match_name ) {
977 // Sanitize slug + Escape
978 $new_slug = sanitize_title($new_slugs[$i]);
979
980 // Get term by name
981 $term = get_term_by('name', $match_name, $taxonomy);
982 if ( !$term ) {
983 continue;
984 }
985
986 // Increment
987 $counter++;
988
989 // Update term
990 wp_update_term($term->term_id, $taxonomy, array('slug' => $new_slug));
991
992 // Clean cache
993 clean_term_cache($term->term_id, $taxonomy);
994 }
995 }
996
997 if ( $counter == 0 ) {
998 add_settings_error( __CLASS__, __CLASS__, esc_html__('No slug edited.', 'simple-tags'), 'updated' );
999 } else {
1000 add_settings_error( __CLASS__, __CLASS__, sprintf(esc_html__('%s slug(s) edited.', 'simple-tags'), $counter), 'updated' );
1001 }
1002
1003 return true;
1004 }
1005 */
1006
1007
1008 /** Singleton instance */
1009 public static function get_instance() {
1010 if ( ! isset( self::$instance ) ) {
1011 self::$instance = new self();
1012 }
1013
1014 return self::$instance;
1015 }
1016
1017 }
1018