PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.38.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.38.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 / autoterms-logs-table.php

autoterms-logs-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.38.0, at inc/autoterms-logs-table.php

603 lines 22.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!class_exists('WP_List_Table')) {
3 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
4 }
5
6 class Autoterms_Logs extends WP_List_Table
7 {
8
9 /** Class constructor */
10 public function __construct()
11 {
12
13 parent::__construct([
14 'singular' => __('autotermslog', 'simple-tags'), //singular name of the listed records
15 'plural' => __('autotermslogs', 'simple-tags'), //plural name of the listed records
16 'ajax' => false //does this table support ajax?
17 ]);
18
19 }
20
21 /**
22 * Retrieve st_autoterms data from the database
23 *
24 * @param bool $count_only
25 *
26 * @return mixed
27 */
28 public function get_st_autoterms()
29 {
30 $per_page = $this->get_items_per_page('st_autoterms_logs_per_page', 20);
31 $current_page = $this->get_pagenum();
32
33 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'ID'; //If no sort, default to role
34 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to asc
35
36
37 return taxopress_autoterms_logs_data($per_page, $current_page, $orderby, $order);
38 }
39
40 /**
41 * Returns the count of records in the database.
42 *
43 * @return null|string
44 */
45 public static function record_count()
46 {
47 return get_st_autoterms()['counts'];
48 }
49
50 /**
51 * Show single row item
52 *
53 * @param array $item
54 */
55 public function single_row($item)
56 {
57 $class = ['st-autoterm-log-tr'];
58 $id = 'st-autoterm-log-' . md5($item->ID);
59 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
60 $this->single_row_columns($item);
61 echo '</tr>';
62 }
63
64 /**
65 * Add custom pagination side item
66 *
67 * @param string $which
68 */
69 protected function pagination( $which ) {
70
71 parent::pagination($which);
72
73 if ('top' === $which) {
74 ?>
75
76 <br class="clear">
77
78 <div class="alignright actions autoterms-log-table-buttons">
79
80 </div>
81 <?php
82 }
83
84 }
85
86 /**
87 * Add custom filter to tablenav
88 *
89 * @param string $which
90 */
91 protected function extra_tablenav( $which ) {
92
93 if ( 'top' === $which ) {
94
95
96 $log_actions = [
97 'save_posts' => esc_html__( 'Manual post update', 'simple-tags' ),
98 'existing_content' => esc_html__( 'Existing content', 'simple-tags' ),
99 'daily_cron_schedule' => esc_html__( 'Scheduled daily cron', 'simple-tags' ),
100 'hourly_cron_schedule' => esc_html__( 'Scheduled hourly cron', 'simple-tags' )
101 ];
102
103 $post_types = get_post_types(['public' => true], 'objects');
104
105 $taxonomies = get_all_taxopress_public_taxonomies();
106
107 $status_messages = [
108 'terms_added' => esc_html__( 'Terms added successfully', 'simple-tags' ),
109 'invalid_option' => esc_html__( 'Auto Terms settings do not exist.', 'simple-tags' ),
110 'term_only_option' => esc_html__( 'Auto Terms settings are configured to skip posts with terms.', 'simple-tags' ),
111 'empty_post_content' => esc_html__( 'Post content is empty.', 'simple-tags' ),
112 'empty_terms' => esc_html__( 'No new matching terms for Auto Terms settings and the post content.', 'simple-tags' ),
113 'empty_terms_remove_all' => esc_html__( 'No new matching terms for Auto Terms settings and the post content. However, the existing terms were removed due to the Auto Terms replacement settings.', 'simple-tags' )
114 ];
115
116 $autoterm_settings = taxopress_get_autoterm_data();
117
118 $selected_source = (!empty($_REQUEST['log_source_filter'])) ? sanitize_text_field($_REQUEST['log_source_filter']) : '';
119 $selected_post_type = (!empty($_REQUEST['log_filter_post_type'])) ? sanitize_text_field($_REQUEST['log_filter_post_type']) : '';
120 $selected_taxonomy = (!empty($_REQUEST['log_filter_taxonomy'])) ? sanitize_text_field($_REQUEST['log_filter_taxonomy']) : '';
121 $selected_status_message = (!empty($_REQUEST['log_filter_status_message'])) ? sanitize_text_field($_REQUEST['log_filter_status_message']) : '';
122 $selected_settings = (!empty($_REQUEST['log_filter_settings'])) ? (int)$_REQUEST['log_filter_settings'] : 0;
123 ?>
124
125
126 <div class="alignleft actions autoterms-log-table-filter">
127
128 <select class="auto-terms-log-filter-select" name="log_filter_select_post_type" id="log_filter_select_post_type">
129 <option value=""><?php esc_html_e('Post type', 'simple-tags'); ?></option>
130 <?php
131 foreach ( $post_types as $post_type ) {
132 echo '<option value="'. esc_attr($post_type->name) .'" '.selected($selected_post_type, $post_type->name, false).'>'. esc_html($post_type->label) .'</option>';
133 }
134 ?>
135 </select>
136
137 <select class="auto-terms-log-filter-select" name="log_filter_select_taxonomy" id="log_filter_select_taxonomy">
138 <option value=""><?php esc_html_e('Taxonomy', 'simple-tags'); ?></option>
139 <?php
140 foreach ( $taxonomies as $taxonomy ) {
141 echo '<option value="'. esc_attr($taxonomy->name) .'" '.selected($selected_taxonomy, $taxonomy->name, false).'>'. esc_html($taxonomy->labels->name) .'</option>';
142 }
143 ?>
144 </select>
145
146 <select class="auto-terms-log-filter-select" name="log_source_filter_select" id="log_source_filter_select">
147 <option value=""><?php esc_html_e('Source', 'simple-tags'); ?></option>
148 <?php
149 foreach ( $log_actions as $key => $label ) {
150 echo '<option value="'. esc_attr($key) .'" '.selected($selected_source, $key, false).'>'. esc_html($label) .'</option>';
151 }
152 ?>
153 </select>
154
155 <select class="auto-terms-log-filter-select" name="log_filter_select_status_message" id="log_filter_select_status_message">
156 <option value=""><?php esc_html_e('Status message', 'simple-tags'); ?></option>
157 <?php
158 foreach ( $status_messages as $key => $label ) {
159 echo '<option value="'. esc_attr($key) .'" '.selected($selected_status_message, $key, false).'>'. esc_html($label) .'</option>';
160 }
161 ?>
162 </select>
163
164 <select class="auto-terms-log-filter-select" name="log_filter_select_settings" id="log_filter_select_settings">
165 <option value=""><?php esc_html_e('Settings', 'simple-tags'); ?></option>
166 <?php
167 foreach ( $autoterm_settings as $autoterm_setting ) {
168 echo '<option value="'. esc_attr($autoterm_setting['ID']) .'" '.selected($selected_settings, $autoterm_setting['ID'], false).'>'. esc_html($autoterm_setting['title']) .'</option>';
169 }
170 ?>
171 </select>
172
173 <a href="javascript:void(0)" class="taxopress-logs-tablenav-filter button"><?php esc_html_e('Filter', 'simple-tags'); ?></a>
174
175 </div>
176 <?php
177 }
178 }
179
180 /**
181 * Associative array of columns
182 *
183 * @return array
184 */
185 function get_columns()
186 {
187 $columns = [
188 'cb' => '<input type="checkbox"/>', //Render a checkbox instead of text
189 'title' => esc_html__( 'Post', 'simple-tags' ),
190 'post_type' => esc_html__( 'Post type', 'simple-tags' ),
191 'taxonomy' => esc_html__( 'Taxonomy', 'simple-tags' ),
192 'source' => esc_html__( 'Source', 'simple-tags' ),
193 'terms' => esc_html__( 'Terms added', 'simple-tags' ),
194 'status_message' => esc_html__( 'Status message', 'simple-tags' ),
195 'date' => esc_html__( 'Date', 'simple-tags' ),
196 'settings' => esc_html__( 'Settings', 'simple-tags' )
197 ];
198
199 return $columns;
200 }
201
202 /**
203 * Render a column when no column specific method exist.
204 *
205 * @param array $item
206 * @param string $column_name
207 *
208 * @return mixed
209 */
210 public function column_default($item, $column_name)
211 {
212 return isset($item->$column_name) ? $item->$column_name : '&mdash;';
213 }
214
215 /** Text displayed when no stterm data is available */
216 public function no_items()
217 {
218 esc_html_e('No item avaliable.', 'simple-tags');
219 }
220
221 /**
222 * The checkbox column
223 *
224 * @param object $item
225 *
226 * @return string|void
227 */
228 protected function column_cb($item)
229 {
230 return sprintf('<input type="checkbox" name="%1$s[]" value="%2$s" />', 'taxopress_autoterms_logs', $item->ID);
231 }
232
233 /**
234 * Get the bulk actions to show in the top page dropdown
235 *
236 * @return array
237 */
238 protected function get_bulk_actions()
239 {
240 $actions = [
241 'taxopress-autoterms-delete-logs' => esc_html__('Delete', 'simple-tags')
242 ];
243
244 return $actions;
245 }
246
247 /**
248 * Process bulk actions
249 */
250 public function process_bulk_action()
251 {
252
253 $query_arg = '_wpnonce';
254 $action = 'bulk-' . $this->_args['plural'];
255 $checked = $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce(sanitize_key($_REQUEST[$query_arg]), $action) : false;
256
257 if (!$checked || !current_user_can('simple_tags')) {
258 return;
259 }
260
261 if($this->current_action() === 'taxopress-autoterms-delete-logs'){
262 $taxopress_autoterms_logs = array_map('sanitize_text_field', (array)$_REQUEST['taxopress_autoterms_logs']);
263 if (!empty($taxopress_autoterms_logs)) {
264 foreach($taxopress_autoterms_logs as $taxopress_autoterms_log){
265 wp_delete_post($taxopress_autoterms_log, true);
266 }
267 }
268 }
269
270 }
271
272 /**
273 * Columns to make sortable.
274 *
275 * @return array
276 */
277 protected function get_sortable_columns()
278 {
279 $sortable_columns = [
280 'title' => ['title', true],
281 'date' => ['date', true]
282 ];
283
284 return $sortable_columns;
285 }
286
287 /**
288 * Generates and display row actions links for the list table.
289 *
290 * @param object $item The item being acted upon.
291 * @param string $column_name Current column name.
292 * @param string $primary Primary column name.
293 *
294 * @return string The row actions HTML, or an empty string if the current column is the primary column.
295 */
296 protected function handle_row_actions($item, $column_name, $primary)
297 {
298 $auto_term_log_post_id = get_post_meta($item->ID, '_taxopress_log_post_id', true);
299
300 //Build row actions
301 $actions = [
302 'edit' => sprintf(
303 '<a href="%s">%s</a>',
304 add_query_arg(
305 [
306 'action' => 'edit',
307 'post' => $auto_term_log_post_id,
308 ],
309 admin_url('post.php')
310 ),
311 __('Edit Main Post', 'simple-tags')
312 ),
313 'delete' => sprintf(
314 '<a href="%s" class="delete-autoterm">%s</a>',
315 add_query_arg([
316 'page' => 'st_autoterms',
317 'tab' => 'logs',
318 'action' => 'taxopress-delete-autoterm-log',
319 'taxopress_autoterms_log'=> esc_attr($item->ID),
320 '_wpnonce' => wp_create_nonce('autoterm-action-request-nonce')
321 ],
322 admin_url('admin.php')),
323 __('Delete Log', 'simple-tags')
324 ),
325 ];
326
327 return $column_name === $primary ? $this->row_actions($actions, false) : '';
328 }
329
330 /**
331 * Method for title column
332 *
333 * @param array $item
334 *
335 * @return string
336 */
337 protected function column_title($item)
338 {
339 $auto_term_log_post_id = get_post_meta($item->ID, '_taxopress_log_post_id', true);
340
341 $title = sprintf(
342 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
343 add_query_arg(
344 [
345 'action' => 'edit',
346 'post' => $auto_term_log_post_id,
347 ],
348 admin_url('post.php')
349 ),
350 esc_html(get_the_title($auto_term_log_post_id))
351 );
352
353 return $title;
354 }
355
356 /**
357 * Method for post_type column
358 *
359 * @param array $item
360 *
361 * @return string
362 */
363 protected function column_post_type($item)
364 {
365 $auto_term_log_post_id = get_post_meta($item->ID, '_taxopress_log_post_id', true);
366 $auto_term_log_posttype = get_post_type_object(get_post_type($auto_term_log_post_id));
367
368 return ($auto_term_log_posttype && !is_wp_error($auto_term_log_posttype)) ? $auto_term_log_posttype->labels->singular_name : '&mdash;';
369
370 }
371
372 /**
373 * Method for taxonomy column
374 *
375 * @param array $item
376 *
377 * @return string
378 */
379 protected function column_taxonomy($item)
380 {
381 $taxopress_log_taxonomy = get_post_meta($item->ID, '_taxopress_log_taxonomy', true);
382 $taxopress_log_taxonomy_data = get_taxonomy($taxopress_log_taxonomy);
383
384 return ($taxopress_log_taxonomy_data && !is_wp_error($taxopress_log_taxonomy_data)) ? $taxopress_log_taxonomy_data->labels->singular_name : '&mdash;';
385
386 }
387
388 /**
389 * Method for source column
390 *
391 * @param array $item
392 *
393 * @return string
394 */
395 protected function column_source($item)
396 {
397
398 $taxopress_log_action = get_post_meta($item->ID, '_taxopress_log_action', true);
399
400 $log_action_texts = [
401 'save_posts' => esc_html__( 'Manual post update', 'simple-tags' ),
402 'existing_content' => esc_html__( 'Existing content', 'simple-tags' ),
403 'daily_cron_schedule' => esc_html__( 'Scheduled daily cron', 'simple-tags' ),
404 'hourly_cron_schedule' => esc_html__( 'Scheduled hourly cron', 'simple-tags' )
405 ];
406
407 if(array_key_exists($taxopress_log_action, $log_action_texts)){
408 return esc_html($log_action_texts[$taxopress_log_action]);
409 }else{
410 return esc_html($taxopress_log_action);
411 }
412
413 }
414
415 /**
416 * Method for terms column
417 *
418 * @param array $item
419 *
420 * @return string
421 */
422 protected function column_terms($item)
423 {
424 $taxopress_log_terms = get_post_meta($item->ID, '_taxopress_log_terms', true);
425
426 if($taxopress_log_terms && !empty(trim($taxopress_log_terms))){
427 return '<font color="green"> '.esc_html(ucwords($taxopress_log_terms)).' </font>';
428 }else{
429 return '<font color="red"> '. esc_html__('None', 'simple-tags') .' </font>';
430 }
431 }
432
433 /**
434 * Method for settings column
435 *
436 * @param array $item
437 *
438 * @return string
439 */
440 protected function column_settings($item)
441 {
442 $taxopress_log_settings = get_post_meta($item->ID, '_taxopress_log_options', true);
443 if(!empty($taxopress_log_settings) && is_array($taxopress_log_settings)){
444 return sprintf(
445 '<a href="%s">%s</a>',
446 add_query_arg(
447 [
448 'page' => 'st_autoterms',
449 'add' => 'new_item',
450 'action' => 'edit',
451 'taxopress_autoterms' => $taxopress_log_settings['ID'],
452 ],
453 admin_url('admin.php')
454 ),
455 $taxopress_log_settings['title']
456 );
457 }else{
458 return '&mdash;';
459 }
460 }
461
462 /**
463 * Method for status_message column
464 *
465 * @param array $item
466 *
467 * @return string
468 */
469 protected function column_status_message($item)
470 {
471 $taxopress_log_status_message = get_post_meta($item->ID, '_taxopress_log_status_message', true);
472
473 $status_message_text = [
474 'invalid_option' => esc_html__( 'Auto Terms settings do not exist.', 'simple-tags' ),
475 'term_only_option' => esc_html__( 'Auto Terms settings are configured to skip posts with terms.', 'simple-tags' ),
476 'empty_post_content' => esc_html__( 'Post content is empty.', 'simple-tags' ),
477 'terms_added' => esc_html__( 'Terms added successfully', 'simple-tags' ),
478 'empty_terms' => esc_html__( 'No new matching terms for Auto Terms settings and the post content.', 'simple-tags' ),
479 'empty_terms_remove_all' => esc_html__( 'No new matching terms for Auto Terms settings and the post content. However, the existing terms were removed due to the Auto Terms replacement settings.', 'simple-tags' )
480 ];
481
482 if(array_key_exists($taxopress_log_status_message, $status_message_text)){
483 return esc_html($status_message_text[$taxopress_log_status_message]);
484 }else{
485 return esc_html($taxopress_log_status_message);
486 }
487 }
488
489 /**
490 * Method for date column
491 *
492 * @param array $item
493 *
494 * @return string
495 */
496 protected function column_date($item)
497 {
498
499 return get_the_date('l F j, Y h:i A', $item->ID);
500
501 }
502
503 /**
504 * Displays the search box.
505 *
506 * @param string $text The 'submit' button label.
507 * @param string $input_id ID attribute value for the search input field.
508 *
509 *
510 */
511 public function search_box($text, $input_id)
512 {
513 if (!isset($_REQUEST['s']) && !$this->has_items()) {
514 return;
515 }
516
517 $input_id = $input_id . '-search-input';
518
519 if (!empty($_REQUEST['orderby'])) {
520 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
521 }
522 if (!empty($_REQUEST['order'])) {
523 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
524 }
525 if (!empty($_REQUEST['page'])) {
526 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
527 }
528 if (!empty($_REQUEST['tab'])) {
529 echo '<input type="hidden" name="tab" value="' . esc_attr(sanitize_text_field($_REQUEST['tab'])) . '" />';
530 }
531
532 $custom_filters = ['log_source_filter', 'log_filter_post_type', 'log_filter_taxonomy', 'log_filter_status_message', 'log_filter_settings'];
533
534 foreach ($custom_filters as $custom_filter) {
535 $filter_value = !empty($_REQUEST[$custom_filter]) ? sanitize_text_field($_REQUEST[$custom_filter]) : '';
536 echo '<input type="hidden" name="' . esc_attr($custom_filter) . '" value="' . esc_attr($filter_value) . '" />';
537 }
538
539 $log_limit_link = add_query_arg([
540 'page' => 'st_autoterms',
541 'tab' => 'logs',
542 'action' => 'taxopress-update-autoterm-limit',
543 '_wpnonce' => wp_create_nonce('autoterm-action-request-nonce')
544 ],
545 admin_url('admin.php')
546 );
547 ?>
548 <p class="search-box">
549 <span class="autoterms-log-table-limit-settings">
550 <label for="taxopress_auto_terms_logs_limit"><?php esc_html_e( 'Limit the number of logs', 'simple-tags' ); ?></label>
551 <input data-link="<?php echo esc_attr($log_limit_link); ?>" type="number" step="1" min="1" name="taxopress_auto_terms_logs_limit" id="taxopress_auto_terms_logs_limit" value="<?php echo (int)get_option('taxopress_auto_terms_logs_limit', 1000); ?>" />
552 <a href="javascript:void(0)" class="taxopress-logs-limit-update button"><?php esc_html_e('Update', 'simple-tags'); ?></a>
553 </span>
554
555 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
556 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
557 value="<?php _admin_search_query(); ?>"/>
558 <?php submit_button($text, '', '', false, ['id' => 'taxopress-log-search-submit']); ?>
559 </p>
560 <?php
561 }
562
563 /**
564 * Sets up the items (roles) to list.
565 */
566 public function prepare_items()
567 {
568
569 $this->_column_headers = $this->get_column_info();
570 $this->process_bulk_action();
571
572 /**
573 * First, lets decide how many records per page to show
574 */
575 $per_page = $this->get_items_per_page('st_autoterms_logs_per_page', 20);
576
577 /**
578 * Fetch the data
579 */
580 $results = $this->get_st_autoterms();
581 $data = $results['posts'];
582 $total_items = $results['counts'];
583 $current_page = $this->get_pagenum();
584
585
586 /**
587 * Now we can add the data to the items property, where it can be used by the rest of the class.
588 */
589 $this->items = $data;
590
591 /**
592 * We also have to register our pagination options & calculations.
593 */
594 $this->set_pagination_args([
595 'total_items' => $total_items, //calculate the total number of items
596 'per_page' => $per_page, //determine how many items to show on a page
597 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
598 ]);
599 }
600
601
602 }
603