PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.4.4
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.4.4
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.4.4, at inc/class.admin.manage.php

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