PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.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.mass.php

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

527 lines 18.9 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_Mass {
4
5 const MENU_SLUG = 'st_options';
6
7 /**
8 * SimpleTags_Admin_Mass constructor.
9 */
10 public function __construct() {
11 // Ajax action, JS Helper and admin action
12 add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
13
14 // Admin menu
15 add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
16
17 // Register taxo, parent method...
18 SimpleTags_Admin::register_taxonomy();
19 }
20
21 /**
22 * Add WP admin menu for Tags
23 *
24 * @return void
25 * @author WebFactory Ltd
26 */
27 public static function admin_menu() {
28 add_submenu_page(
29 self::MENU_SLUG,
30 esc_html__( 'Mass Edit Terms', 'simple-tags' ),
31 esc_html__( 'Mass Edit Terms', 'simple-tags' ),
32 'simple_tags',
33 'st_mass_terms',
34 array(
35 __CLASS__,
36 'pageMassEditTags',
37 )
38 );
39 }
40
41 /**
42 * Control POST data for mass edit tags
43 *
44 */
45 public static function admin_init() {
46 if ( ! current_user_can( 'simple_tags' ) ) {
47 return false;
48 }
49
50 // Get GET data
51 if ( isset( $_GET['post_type'] ) ) {
52 $type = stripslashes( sanitize_text_field($_GET['post_type']) );
53 }
54
55 if ( isset( $_POST['update_mass'] ) ) {
56 // origination and intention
57 if ( ! ( wp_verify_nonce( sanitize_text_field($_POST['secure_mass']), 'st_mass_terms' ) ) ) {
58 add_settings_error( __CLASS__, __CLASS__, esc_html__( 'Security problem. Try again.', 'simple-tags' ), 'error taxopress-notice' );
59
60 return false;
61 }
62
63 if ( isset( $_POST['tags'] ) ) {
64 $counter = 0;
65 foreach ( (array) array_map('sanitize_text_field', $_POST['tags']) as $object_id => $tag_list ) {
66 // Trim data
67 $tag_list = trim( stripslashes( $tag_list ) );
68
69 // String to array
70 $tags = explode( ',', $tag_list );
71
72 // Remove empty and trim tag
73 $tags = array_filter( $tags, '_delete_empty_element' );
74
75 $show_slug = SimpleTags_Plugin::get_option_value('enable_mass-edit_terms_slug');
76
77 $term_ids = array();
78 foreach ($tags as $tag) {
79 $tag = trim($tag);
80 if (empty($tag)) continue;
81
82 if ($show_slug && preg_match('/^(.+?)\s*\(([^)]+)\)$/', $tag, $matches)) {
83 $term_name = trim($matches[1]);
84 $term_slug = trim($matches[2]);
85
86 $term = get_term_by('slug', $term_slug, SimpleTags_Admin::$taxonomy);
87
88 if ($term) {
89 if ($term->name === $term_name) {
90 $term_ids[] = $term->term_id;
91 } else {
92 $unique_slug = $term_slug;
93 $counter = 1;
94 while (get_term_by('slug', $unique_slug, SimpleTags_Admin::$taxonomy)) {
95 $unique_slug = $term_slug . '-' . $counter;
96 $counter++;
97 }
98 $new_term = wp_insert_term($term_name, SimpleTags_Admin::$taxonomy, array('slug' => $unique_slug));
99 if (!is_wp_error($new_term)) {
100 $term_ids[] = $new_term['term_id'];
101 }
102 }
103 } else {
104 $new_term = wp_insert_term($term_name, SimpleTags_Admin::$taxonomy, array('slug' => $term_slug));
105 if (!is_wp_error($new_term)) {
106 $term_ids[] = $new_term['term_id'];
107 }
108 }
109 } else {
110 if ($show_slug) {
111 $tag = trim(preg_replace('/\s*\([^)]+\)$/', '', $tag));
112 }
113
114 $term = get_term_by('name', $tag, SimpleTags_Admin::$taxonomy);
115 if ($term) {
116 $term_ids[] = $term->term_id;
117 } else {
118 $new_term = wp_insert_term($tag, SimpleTags_Admin::$taxonomy);
119 if (!is_wp_error($new_term)) {
120 $term_ids[] = $new_term['term_id'];
121 }
122 }
123 }
124 }
125
126 wp_set_object_terms( $object_id, $term_ids, SimpleTags_Admin::$taxonomy );
127 $counter ++;
128
129 // Clean cache
130 clean_post_cache( $object_id );
131 }
132
133 add_settings_error( __CLASS__, __CLASS__, sprintf( esc_html__( '%1$s %2$s(s) terms updated with success !', 'simple-tags' ), (int) $counter, strtolower( SimpleTags_Admin::$post_type_name ) ), 'updated taxopress-notice' );
134
135 return true;
136 }
137 }
138
139 return false;
140 }
141
142 /**
143 * Helper function to extract term name (ignoring slug in brackets)
144 */
145 private static function extractTermName($term) {
146 return trim(preg_replace('/\s*\(.*?\)$/', '', $term));
147 }
148
149 /**
150 * WP Page - Mass edit tags
151 *
152 */
153 public static function pageMassEditTags() {
154 global $wpdb, $wp_locale, $wp_query;
155 list( $post_stati, $avail_post_stati ) = self::edit_data_query();
156
157 if ( ! isset( $_GET['paged'] ) ) {
158 $_GET['paged'] = 1;
159 }
160
161 // Display message
162 settings_errors( __CLASS__ );
163 ?>
164 <div class="taxopress-block-wrap">
165 <div class="wrap st_wrap tagcloudui st_mass_terms-page admin-settings">
166 <form id="posts-filter" action="" method="get">
167 <input type="hidden" name="page" value="st_mass_terms"/>
168 <input type="hidden" name="taxo" value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
169 <input type="hidden" name="cpt" value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
170
171 <h1><?php _e( 'Mass Edit Terms', 'simple-tags' ); ?></h1>
172 <br>
173 <div class="taxopress-description"><?php esc_html_e('This feature allows users to quickly add or remove terms from multiple posts.', 'simple-tags'); ?></div>
174 <br>
175
176 <ul class="subsubsub">
177 <?php
178 $status_links = array();
179 $num_posts = wp_count_posts( SimpleTags_Admin::$post_type, 'readable' );
180 $class = ( empty( $_GET['post_status'] ) && empty( $_GET['post_type'] ) ) ? ' class="current"' : '';
181 $status_links[] = '<li><a href="' . admin_url( 'admin.php' ) . '?page=st_mass_terms&amp;cpt=' . SimpleTags_Admin::$post_type . '&amp;taxo=' . SimpleTags_Admin::$taxonomy . '"' . $class . '>' . esc_html__( 'All', 'simple-tags' ) . '</a>';
182 foreach ( $post_stati as $status => $label ) {
183 $class = '';
184
185 if ( ! in_array( $status, $avail_post_stati ) ) {
186 continue;
187 }
188
189 if ( empty( $num_posts->$status ) ) {
190 continue;
191 }
192 if ( isset( $_GET['post_status'] ) && $status == $_GET['post_status'] ) {
193 $class = ' class="current"';
194 }
195
196 $status_links[] = '<li><a href="' . admin_url( 'admin.php' ) . '?page=st_mass_terms&amp;cpt=' . SimpleTags_Admin::$post_type . '&amp;taxo=' . SimpleTags_Admin::$taxonomy . '&amp;post_status=' . $status . '"' . $class . '>' . sprintf( _n( $label[2][0], $label[2][1], (int) $num_posts->$status ), number_format_i18n( $num_posts->$status ) ) . '</a>';
197 }
198 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
199 echo implode( ' |</li>', $status_links ) . '</li>';
200 unset( $status_links );
201
202 $class = ( ! empty( $_GET['post_type'] ) ) ? ' class="current"' : '';
203 ?>
204 </ul>
205
206 <?php if ( isset( $_GET['post_status'] ) ) : ?>
207 <input type="hidden" name="post_status" value="<?php echo esc_attr( sanitize_text_field($_GET['post_status']) ) ?>"/>
208 <?php endif; ?>
209
210
211 <p class="search-box">
212 <input type="text" id="post-search-input" placeholder="<?php esc_attr_e('Search post title and content', 'simple-tags'); ?>" name="s" value="<?php the_search_query(); ?>"/>
213 <input type="submit" value="<?php _e( 'Search', 'simple-tags' ); ?>" class="button"/>
214 </p>
215
216 <div class="tablenav custom-nav">
217 <?php
218 $posts_per_page = ( isset( $_GET['posts_per_page'] ) ) ? (int) $_GET['posts_per_page'] : 0;
219 if ( (int) $posts_per_page == 0 ) {
220 $posts_per_page = 15;
221 }
222
223 $page_links = paginate_links( array(
224 'base' => add_query_arg( 'paged', '%#%' ),
225 'format' => '',
226 'total' => ceil( $wp_query->found_posts / $posts_per_page ),
227 'current' => ( (int) $_GET['paged'] )
228 ) );
229
230 if ( $page_links ) {
231 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
232 echo "<div class='tablenav-pages'>". $page_links ."</div>";
233 }
234 ?>
235
236 <div style="float: left">
237 <?php
238 if ( ! is_singular() ) {
239 $arc_result = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = %s ORDER BY post_date DESC", SimpleTags_Admin::$post_type ) );
240
241 $month_count = count( $arc_result );
242
243 if ( ! isset( $_GET['m'] ) ) {
244 $_GET['m'] = '';
245 }
246
247 if ( $month_count && ! ( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
248 ?>
249 <select name='m'>
250 <option <?php selected( @sanitize_text_field($_GET['m']), 0 ); ?>
251 value='0'><?php _e( 'Show all dates', 'simple-tags' ); ?></option>
252 <?php
253 foreach ( $arc_result as $arc_row ) {
254 if ( $arc_row->yyear == 0 ) {
255 continue;
256 }
257 $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
258
259 if ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) {
260 $default = ' selected="selected"';
261 } else {
262 $default = '';
263 }
264 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
265 echo "<option$default value='$arc_row->yyear$arc_row->mmonth'>";
266 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
267 echo esc_html($wp_locale->get_month( $arc_row->mmonth )) . " ".esc_html($arc_row->yyear)."";
268 echo "</option>\n";
269 }
270 ?>
271 </select>
272 <?php
273 }
274 ?>
275
276 <select name="posts_per_page" id="posts_per_page">
277 <option <?php if ( ! isset( $_GET['posts_per_page'] ) ) {
278 echo 'selected="selected"';
279 } ?> value=""><?php _e( 'Quantity&hellip;', 'simple-tags' ); ?></option>
280 <option <?php selected( $posts_per_page, 10 ); ?> value="10">10</option>
281 <option <?php selected( $posts_per_page, 20 ); ?> value="20">20</option>
282 <option <?php selected( $posts_per_page, 30 ); ?> value="30">30</option>
283 <option <?php selected( $posts_per_page, 40 ); ?> value="40">40</option>
284 <option <?php selected( $posts_per_page, 50 ); ?> value="50">50</option>
285 <option <?php selected( $posts_per_page, 100 ); ?> value="100">100</option>
286 <option <?php selected( $posts_per_page, 200 ); ?> value="200">200</option>
287 </select>
288
289 <?php
290
291 $current_filter_term = isset($_GET['massedit_filter_term']) ? sanitize_text_field($_GET['massedit_filter_term']) : '';
292 $selected_term_label = '';
293
294 if ($current_filter_term) {
295 $existing_term = get_term_by('slug', $current_filter_term, SimpleTags_Admin::$taxonomy);
296 if ($existing_term && !is_wp_error($existing_term)) {
297 $show_slug = (int) SimpleTags_Plugin::get_option_value('enable_mass-edit_terms_slug') === 1;
298
299 if ( $show_slug ) {
300 $selected_term_label = sprintf('%s (%s)', $existing_term->name, $existing_term->slug);
301 } else {
302 $selected_term_label = $existing_term->name;
303 }
304 }
305 }
306 ?>
307
308 <select name="massedit_filter_term" id="<?php echo esc_attr(SimpleTags_Admin::$taxonomy); ?>"
309 class="taxopress-select2-term-filter"
310 data-placeholder="<?php echo esc_attr__('Filter by term', 'simple-tags'); ?>"
311 style="min-width:200px;">
312 <option value=""><?php esc_html_e('All terms', 'simple-tags'); ?></option>
313 <?php if ($current_filter_term && $selected_term_label) : ?>
314 <option value="<?php echo esc_attr($current_filter_term); ?>" selected="selected">
315 <?php echo esc_html($selected_term_label); ?>
316 </option>
317 <?php endif; ?>
318 </select>
319
320 <input type="submit" id="post-query-submit" value="<?php _e( 'Filter', 'simple-tags' ); ?>"
321 class="button-secondary"/>
322 <?php } ?>
323 <?php SimpleTags_Admin::boxSelectorTaxonomy( 'st_mass_terms' ); ?>
324 </div>
325 <br style="clear:both;"/>
326 </div>
327 </form>
328
329 <div style="clear:both;margin-bottom: 6px;"></div>
330
331 <?php if ( have_posts() ) :
332 add_filter( 'the_title', 'esc_html' );
333 ?>
334 <form name="post" id="post" method="post" class="st-mass-edit">
335 <table class="widefat post fixed">
336 <thead>
337 <tr>
338 <th class="manage-column"><?php esc_html_e( 'Post title', 'simple-tags' ); ?></th>
339 <th class="manage-column"><?php printf( esc_html__( 'Terms : %s', 'simple-tags' ), esc_html( SimpleTags_Admin::$taxo_name ) ); ?></th>
340 </tr>
341 </thead>
342 <tbody>
343 <?php
344 $class = 'alternate';
345 while ( have_posts() ) {
346 the_post();
347 $class = ( $class == 'alternate' ) ? '' : 'alternate';
348 ?>
349 <tr valign="top" class="<?php echo esc_attr($class); ?>">
350 <th scope="row"><a
351 href="<?php echo esc_url(admin_url( 'post.php?action=edit&amp;post=' . get_the_ID() )); ?>"
352 title="<?php esc_attr_e( 'Edit', 'simple-tags' ); ?>"><?php echo ( esc_html(get_the_title()) == '' ) ? (int)get_the_ID() : esc_html(get_the_title()); ?></a>
353 </th>
354 <td><input id="tags-input<?php the_ID(); ?>" class="autocomplete-input tags_input taxopress-mass-edit-input"
355 type="text" size="100" name="tags[<?php echo (int)get_the_ID(); ?>]"
356 value="<?php echo esc_attr(SimpleTags_Admin::getTermsToEdit( SimpleTags_Admin::$taxonomy, get_the_ID() )); ?>"/>
357 </td>
358 </tr>
359 <?php
360 }
361 ?>
362 </tbody>
363 </table>
364
365 <p class="submit">
366 <input type="hidden" name="secure_mass"
367 value="<?php echo esc_attr(wp_create_nonce( 'st_mass_terms' )); ?>"/>
368 <input class="button-primary" type="submit" name="update_mass"
369 value="<?php esc_attr_e( 'Update all &raquo;', 'simple-tags' ); ?>"/>
370 </p>
371 </form>
372
373 <?php else: ?>
374
375 <p><?php _e( 'No content to edit.', 'simple-tags' ); ?>
376
377 <?php endif; ?>
378
379 <?php SimpleTags_Admin::printAdminFooter(); ?>
380 </div>
381
382 <div class="taxopress-right-sidebar admin-settings-sidebar">
383 <?php do_action('taxopress_admin_after_sidebar'); ?>
384 </div>
385
386 </div>
387
388 <?php
389 do_action( 'simpletags-mass_terms', SimpleTags_Admin::$taxonomy );
390 }
391
392 /**
393 * Clone the core WP function, add the possibility to manage the post type
394 *
395 * @param bool $q
396 *
397 * @return array
398 * @author WebFactory Ltd
399 */
400 public static function edit_data_query( $q = false ) {
401 if ( false === $q ) {
402 $q = $_GET;
403 }
404
405 if ( ! isset( $q['massedit_filter_term'] ) ) {
406 $q['massedit_filter_term'] = '';
407 }
408
409 // Date
410 if ( isset( $q['m'] ) ) {
411 $q['m'] = (int) $q['m'];
412 }
413
414 // Category
415 if ( isset( $q['cat'] ) ) {
416 $q['cat'] = (int) $q['cat'];
417 }
418
419 // Quantity
420 $q['posts_per_page'] = ( isset( $q['posts_per_page'] ) ) ? (int) $q['posts_per_page'] : 0;
421 if ( $q['posts_per_page'] == 0 ) {
422 $q['posts_per_page'] = 15;
423 }
424
425 // Content type
426 $q['post_type'] = SimpleTags_Admin::$post_type;
427
428 // Post status
429 $post_stati = array( // array( adj, noun )
430 'publish' => array(
431 _x( 'Published', 'post' ),
432 __( 'Published posts' ),
433 _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' )
434 ),
435 'future' => array(
436 _x( 'Scheduled', 'post' ),
437 __( 'Scheduled posts' ),
438 _n_noop( 'Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' )
439 ),
440 'pending' => array(
441 _x( 'Pending Review', 'post' ),
442 __( 'Pending posts' ),
443 _n_noop( 'Pending Review <span class="count">(%s)</span>', 'Pending Review <span class="count">(%s)</span>' )
444 ),
445 'draft' => array(
446 _x( 'Draft', 'post' ),
447 _x( 'Drafts', 'manage posts header' ),
448 _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' )
449 ),
450 'private' => array(
451 _x( 'Private', 'post' ),
452 __( 'Private posts' ),
453 _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' )
454 ),
455 'inherit' => array(
456 _x( 'Inherit', 'post' ),
457 __( 'Inherit posts' ),
458 _n_noop( 'Inherit <span class="count">(%s)</span>', 'Inherit <span class="count">(%s)</span>' )
459 ),
460 );
461
462 $post_stati = apply_filters( 'post_stati', $post_stati );
463 $avail_post_stati = get_available_post_statuses( SimpleTags_Admin::$post_type );
464
465
466 if($q['post_type'] === 'attachment'){
467 $q['post_status'] = 'inherit';
468 }
469
470 $post_status_q = '';
471 if ( isset( $q['post_status'] ) && in_array( $q['post_status'], array_keys( $post_stati ) ) ) {
472 $post_status_q = '&post_status=' . $q['post_status'];
473 $post_status_q .= '&perm=readable';
474 } elseif ( ! isset( $q['post_status'] ) ) {
475 $q['post_status'] = '';
476 }
477
478
479 if ( 'pending' === $q['post_status'] ) {
480 $order = 'ASC';
481 $orderby = 'modified';
482 } elseif ( 'draft' === $q['post_status'] ) {
483 $order = 'DESC';
484 $orderby = 'modified';
485 } else {
486 $order = 'DESC';
487 $orderby = 'date';
488 }
489
490 $args = array(
491 'post_type' => $q['post_type'],
492 'what_to_show' => 'posts',
493 'posts_per_page' => $q['posts_per_page'],
494 'order' => $order,
495 'orderby' => $orderby,
496 );
497
498 if ( ! empty( $q['post_status'] ) && in_array( $q['post_status'], array_keys( $post_stati ), true ) ) {
499 $args['post_status'] = $q['post_status'];
500 $args['perm'] = 'readable';
501 }
502
503 if ( ! empty( $q['s'] ) ) {
504 $args['s'] = sanitize_text_field( $q['s'] );
505 }
506
507 if ( ! empty( $q['m'] ) && (int) $q['m'] > 0 ) {
508 $args['m'] = (int) $q['m'];
509 }
510
511 if ( ! empty( $q['massedit_filter_term'] ) ) {
512 $args['tax_query'] = array(
513 array(
514 'taxonomy' => SimpleTags_Admin::$taxonomy,
515 'field' => 'slug',
516 'terms' => sanitize_text_field( $q['massedit_filter_term'] ),
517 ),
518 );
519 }
520
521 query_posts( $args );
522
523 return array( $post_stati, $avail_post_stati );
524 }
525
526 }
527