PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.4.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.4.0
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 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.0, at inc/class.admin.manage.php

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