PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.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.51.0, at inc/autoterms-logs-table.php

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