PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.40.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.40.0
3.54.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 All 178 releases
simple-tags / inc / class.admin.mass.php

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

418 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_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 if ($show_slug) {
77 $tags = array_map( function($tag) {
78 return trim(preg_replace('/\s*\([^)]+\)$/', '', $tag));
79 }, $tags );
80 }
81
82 // Add new tag (no append ! replace !)
83 wp_set_object_terms( $object_id, $tags, SimpleTags_Admin::$taxonomy );
84 $counter ++;
85
86 // Clean cache
87 clean_post_cache( $object_id );
88 }
89
90 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' );
91
92 return true;
93 }
94 }
95
96 return false;
97 }
98
99 /**
100 * Helper function to extract term name (ignoring slug in brackets)
101 */
102 private static function extractTermName($term) {
103 return trim(preg_replace('/\s*\(.*?\)$/', '', $term));
104 }
105
106 /**
107 * WP Page - Mass edit tags
108 *
109 */
110 public static function pageMassEditTags() {
111 global $wpdb, $wp_locale, $wp_query;
112 list( $post_stati, $avail_post_stati ) = self::edit_data_query();
113
114 if ( ! isset( $_GET['paged'] ) ) {
115 $_GET['paged'] = 1;
116 }
117
118 // Display message
119 settings_errors( __CLASS__ );
120 ?>
121 <div class="taxopress-block-wrap">
122 <div class="wrap st_wrap tagcloudui st_mass_terms-page admin-settings">
123 <form id="posts-filter" action="" method="get">
124 <input type="hidden" name="page" value="st_mass_terms"/>
125 <input type="hidden" name="taxo" value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/>
126 <input type="hidden" name="cpt" value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/>
127
128 <h1><?php _e( 'Mass edit terms', 'simple-tags' ); ?></h1>
129 <br>
130 <div class="taxopress-description"><?php esc_html_e('This feature allows users to mass edit terms while creating and editing content.', 'simple-tags'); ?></div>
131 <br>
132
133 <ul class="subsubsub">
134 <?php
135 $status_links = array();
136 $num_posts = wp_count_posts( SimpleTags_Admin::$post_type, 'readable' );
137 $class = ( empty( $_GET['post_status'] ) && empty( $_GET['post_type'] ) ) ? ' class="current"' : '';
138 $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>';
139 foreach ( $post_stati as $status => $label ) {
140 $class = '';
141
142 if ( ! in_array( $status, $avail_post_stati ) ) {
143 continue;
144 }
145
146 if ( empty( $num_posts->$status ) ) {
147 continue;
148 }
149 if ( isset( $_GET['post_status'] ) && $status == $_GET['post_status'] ) {
150 $class = ' class="current"';
151 }
152
153 $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>';
154 }
155 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
156 echo implode( ' |</li>', $status_links ) . '</li>';
157 unset( $status_links );
158
159 $class = ( ! empty( $_GET['post_type'] ) ) ? ' class="current"' : '';
160 ?>
161 </ul>
162
163 <?php if ( isset( $_GET['post_status'] ) ) : ?>
164 <input type="hidden" name="post_status" value="<?php echo esc_attr( sanitize_text_field($_GET['post_status']) ) ?>"/>
165 <?php endif; ?>
166
167
168 <p class="search-box">
169 <input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>"/>
170 <input type="submit" value="<?php _e( 'Search', 'simple-tags' ); ?>" class="button"/>
171 </p>
172
173 <div class="tablenav custom-nav">
174 <?php
175 $posts_per_page = ( isset( $_GET['posts_per_page'] ) ) ? (int) $_GET['posts_per_page'] : 0;
176 if ( (int) $posts_per_page == 0 ) {
177 $posts_per_page = 15;
178 }
179
180 $page_links = paginate_links( array(
181 'base' => add_query_arg( 'paged', '%#%' ),
182 'format' => '',
183 'total' => ceil( $wp_query->found_posts / $posts_per_page ),
184 'current' => ( (int) $_GET['paged'] )
185 ) );
186
187 if ( $page_links ) {
188 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
189 echo "<div class='tablenav-pages'>". $page_links ."</div>";
190 }
191 ?>
192
193 <div style="float: left">
194 <?php
195 if ( ! is_singular() ) {
196 $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 ) );
197
198 $month_count = count( $arc_result );
199
200 if ( ! isset( $_GET['m'] ) ) {
201 $_GET['m'] = '';
202 }
203
204 if ( $month_count && ! ( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
205 ?>
206 <select name='m'>
207 <option <?php selected( @sanitize_text_field($_GET['m']), 0 ); ?>
208 value='0'><?php _e( 'Show all dates', 'simple-tags' ); ?></option>
209 <?php
210 foreach ( $arc_result as $arc_row ) {
211 if ( $arc_row->yyear == 0 ) {
212 continue;
213 }
214 $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
215
216 if ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) {
217 $default = ' selected="selected"';
218 } else {
219 $default = '';
220 }
221 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
222 echo "<option$default value='$arc_row->yyear$arc_row->mmonth'>";
223 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
224 echo esc_html($wp_locale->get_month( $arc_row->mmonth )) . " ".esc_html($arc_row->yyear)."";
225 echo "</option>\n";
226 }
227 ?>
228 </select>
229 <?php
230 }
231 ?>
232
233 <select name="posts_per_page" id="posts_per_page">
234 <option <?php if ( ! isset( $_GET['posts_per_page'] ) ) {
235 echo 'selected="selected"';
236 } ?> value=""><?php _e( 'Quantity&hellip;', 'simple-tags' ); ?></option>
237 <option <?php selected( $posts_per_page, 10 ); ?> value="10">10</option>
238 <option <?php selected( $posts_per_page, 20 ); ?> value="20">20</option>
239 <option <?php selected( $posts_per_page, 30 ); ?> value="30">30</option>
240 <option <?php selected( $posts_per_page, 40 ); ?> value="40">40</option>
241 <option <?php selected( $posts_per_page, 50 ); ?> value="50">50</option>
242 <option <?php selected( $posts_per_page, 100 ); ?> value="100">100</option>
243 <option <?php selected( $posts_per_page, 200 ); ?> value="200">200</option>
244 </select>
245
246 <input type="submit" id="post-query-submit" value="<?php _e( 'Filter', 'simple-tags' ); ?>"
247 class="button-secondary"/>
248 <?php } ?>
249 <?php SimpleTags_Admin::boxSelectorTaxonomy( 'st_mass_terms' ); ?>
250 </div>
251 <br style="clear:both;"/>
252 </div>
253 </form>
254
255 <div style="clear:both;margin-bottom: 6px;"></div>
256
257 <?php if ( have_posts() ) :
258 add_filter( 'the_title', 'esc_html' );
259 ?>
260 <form name="post" id="post" method="post" class="st-mass-edit">
261 <table class="widefat post fixed">
262 <thead>
263 <tr>
264 <th class="manage-column"><?php esc_html_e( 'Post title', 'simple-tags' ); ?></th>
265 <th class="manage-column"><?php printf( esc_html__( 'Terms : %s', 'simple-tags' ), esc_html( SimpleTags_Admin::$taxo_name ) ); ?></th>
266 </tr>
267 </thead>
268 <tbody>
269 <?php
270 $class = 'alternate';
271 while ( have_posts() ) {
272 the_post();
273 $class = ( $class == 'alternate' ) ? '' : 'alternate';
274 ?>
275 <tr valign="top" class="<?php echo esc_attr($class); ?>">
276 <th scope="row"><a
277 href="<?php echo esc_url(admin_url( 'post.php?action=edit&amp;post=' . get_the_ID() )); ?>"
278 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>
279 </th>
280 <td><input id="tags-input<?php the_ID(); ?>" class="autocomplete-input tags_input taxopress-mass-edit-input"
281 type="text" size="100" name="tags[<?php echo (int)get_the_ID(); ?>]"
282 value="<?php echo esc_attr(SimpleTags_Admin::getTermsToEdit( SimpleTags_Admin::$taxonomy, get_the_ID() )); ?>"/>
283 </td>
284 </tr>
285 <?php
286 }
287 ?>
288 </tbody>
289 </table>
290
291 <p class="submit">
292 <input type="hidden" name="secure_mass"
293 value="<?php echo esc_attr(wp_create_nonce( 'st_mass_terms' )); ?>"/>
294 <input class="button-primary" type="submit" name="update_mass"
295 value="<?php esc_attr_e( 'Update all &raquo;', 'simple-tags' ); ?>"/>
296 </p>
297 </form>
298
299 <?php else: ?>
300
301 <p><?php _e( 'No content to edit.', 'simple-tags' ); ?>
302
303 <?php endif; ?>
304
305 <?php SimpleTags_Admin::printAdminFooter(); ?>
306 </div>
307
308 <div class="taxopress-right-sidebar admin-settings-sidebar">
309 <?php do_action('taxopress_admin_after_sidebar'); ?>
310 </div>
311
312 </div>
313
314 <?php
315 do_action( 'simpletags-mass_terms', SimpleTags_Admin::$taxonomy );
316 }
317
318 /**
319 * Clone the core WP function, add the possibility to manage the post type
320 *
321 * @param bool $q
322 *
323 * @return array
324 * @author WebFactory Ltd
325 */
326 public static function edit_data_query( $q = false ) {
327 if ( false === $q ) {
328 $q = $_GET;
329 }
330
331 // Date
332 if ( isset( $q['m'] ) ) {
333 $q['m'] = (int) $q['m'];
334 }
335
336 // Category
337 if ( isset( $q['cat'] ) ) {
338 $q['cat'] = (int) $q['cat'];
339 }
340
341 // Quantity
342 $q['posts_per_page'] = ( isset( $q['posts_per_page'] ) ) ? (int) $q['posts_per_page'] : 0;
343 if ( $q['posts_per_page'] == 0 ) {
344 $q['posts_per_page'] = 15;
345 }
346
347 // Content type
348 $q['post_type'] = SimpleTags_Admin::$post_type;
349
350 // Post status
351 $post_stati = array( // array( adj, noun )
352 'publish' => array(
353 _x( 'Published', 'post' ),
354 __( 'Published posts' ),
355 _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' )
356 ),
357 'future' => array(
358 _x( 'Scheduled', 'post' ),
359 __( 'Scheduled posts' ),
360 _n_noop( 'Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' )
361 ),
362 'pending' => array(
363 _x( 'Pending Review', 'post' ),
364 __( 'Pending posts' ),
365 _n_noop( 'Pending Review <span class="count">(%s)</span>', 'Pending Review <span class="count">(%s)</span>' )
366 ),
367 'draft' => array(
368 _x( 'Draft', 'post' ),
369 _x( 'Drafts', 'manage posts header' ),
370 _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' )
371 ),
372 'private' => array(
373 _x( 'Private', 'post' ),
374 __( 'Private posts' ),
375 _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' )
376 ),
377 'inherit' => array(
378 _x( 'Inherit', 'post' ),
379 __( 'Inherit posts' ),
380 _n_noop( 'Inherit <span class="count">(%s)</span>', 'Inherit <span class="count">(%s)</span>' )
381 ),
382 );
383
384 $post_stati = apply_filters( 'post_stati', $post_stati );
385 $avail_post_stati = get_available_post_statuses( SimpleTags_Admin::$post_type );
386
387
388 if($q['post_type'] === 'attachment'){
389 $q['post_status'] = 'inherit';
390 }
391
392 $post_status_q = '';
393 if ( isset( $q['post_status'] ) && in_array( $q['post_status'], array_keys( $post_stati ) ) ) {
394 $post_status_q = '&post_status=' . $q['post_status'];
395 $post_status_q .= '&perm=readable';
396 } elseif ( ! isset( $q['post_status'] ) ) {
397 $q['post_status'] = '';
398 }
399
400
401 if ( 'pending' === $q['post_status'] ) {
402 $order = 'ASC';
403 $orderby = 'modified';
404 } elseif ( 'draft' === $q['post_status'] ) {
405 $order = 'DESC';
406 $orderby = 'modified';
407 } else {
408 $order = 'DESC';
409 $orderby = 'date';
410 }
411
412 wp( "post_type={$q['post_type']}&what_to_show=posts$post_status_q&posts_per_page={$q['posts_per_page']}&order=$order&orderby=$orderby" );
413
414 return array( $post_stati, $avail_post_stati );
415 }
416
417 }
418