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

601 lines 22.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Legacy TaxoPress file: keep behavior unchanged while documenting existing PHPCS exceptions.
4
5 if (!class_exists('WP_List_Table')) {
6 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
7 }
8
9 class Autoterms_Logs extends WP_List_Table
10 {
11 /** Class constructor */
12 public function __construct()
13 {
14
15 parent::__construct([
16 'singular' => __('autotermslog', 'simple-tags'), //singular name of the listed records
17 'plural' => __('autotermslogs', 'simple-tags'), //plural name of the listed records
18 'ajax' => false //does this table support ajax?
19 ]);
20 }
21
22 /**
23 * Retrieve st_autoterms data from the database
24 *
25 * @param bool $count_only
26 *
27 * @return mixed
28 */
29 public function get_st_autoterms()
30 {
31 $per_page = $this->get_items_per_page('st_autoterms_logs_per_page', 20);
32 $current_page = $this->get_pagenum();
33
34 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field(wp_unslash($_REQUEST['orderby'])) : 'ID'; //If no sort, default to role
35 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field(wp_unslash($_REQUEST['order'])) : 'desc'; //If no order, default to asc
36
37
38 return taxopress_autoterms_logs_data($per_page, $current_page, $orderby, $order);
39 }
40
41 /**
42 * Returns the count of records in the database.
43 *
44 * @return null|string
45 */
46 public static function record_count()
47 {
48 return get_st_autoterms()['counts'];
49 }
50
51 /**
52 * Show single row item
53 *
54 * @param array $item
55 */
56 public function single_row($item)
57 {
58 $class = ['st-autoterm-log-tr'];
59 $id = 'st-autoterm-log-' . md5($item->ID);
60 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
61 $this->single_row_columns($item);
62 echo '</tr>';
63 }
64
65 /**
66 * Add custom pagination side item
67 *
68 * @param string $which
69 */
70 protected function pagination($which)
71 {
72
73 parent::pagination($which);
74
75 if ('top' === $which) {
76 ?>
77
78 <br class="clear">
79
80 <div class="alignright actions autoterms-log-table-buttons">
81
82 </div>
83 <?php
84 }
85 }
86
87 /**
88 * Add custom filter to tablenav
89 *
90 * @param string $which
91 */
92 protected function extra_tablenav($which)
93 {
94
95 if ('top' === $which) {
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 'weekly_cron_schedule' => esc_html__('Scheduled weekly cron', 'simple-tags')
102 ];
103
104 $post_types = get_post_types(['public' => true], 'objects');
105
106 $taxonomies = get_all_taxopress_public_taxonomies();
107
108 $status_messages = [
109 'terms_added' => esc_html__('Terms added successfully', 'simple-tags'),
110 'invalid_option' => esc_html__('Auto Terms settings do not exist.', 'simple-tags'),
111 'term_only_option' => esc_html__('Auto Terms settings are configured to skip posts with terms.', 'simple-tags'),
112 'empty_post_content' => esc_html__('Post content is empty.', 'simple-tags'),
113 'empty_terms' => esc_html__('No new matching terms for Auto Terms settings and the post content.', 'simple-tags'),
114 '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')
115 ];
116
117 $autoterm_settings = taxopress_get_autoterm_data();
118
119 $selected_source = (!empty($_REQUEST['log_source_filter'])) ? sanitize_text_field(wp_unslash($_REQUEST['log_source_filter'])) : '';
120 $selected_post_type = (!empty($_REQUEST['log_filter_post_type'])) ? sanitize_text_field(wp_unslash($_REQUEST['log_filter_post_type'])) : '';
121 $selected_taxonomy = (!empty($_REQUEST['log_filter_taxonomy'])) ? sanitize_text_field(wp_unslash($_REQUEST['log_filter_taxonomy'])) : '';
122 $selected_status_message = (!empty($_REQUEST['log_filter_status_message'])) ? sanitize_text_field(wp_unslash($_REQUEST['log_filter_status_message'])) : '';
123 $selected_settings = (!empty($_REQUEST['log_filter_settings'])) ? (int)$_REQUEST['log_filter_settings'] : 0;
124 ?>
125
126
127 <div class="alignleft actions autoterms-log-table-filter">
128
129 <select class="auto-terms-log-filter-select" name="log_filter_select_post_type" id="log_filter_select_post_type">
130 <option value=""><?php esc_html_e('Post type', 'simple-tags'); ?></option>
131 <?php
132 foreach ($post_types as $post_type) {
133 echo '<option value="' . esc_attr($post_type->name) . '" ' . selected($selected_post_type, $post_type->name, false) . '>' . esc_html($post_type->label) . '</option>';
134 }
135 ?>
136 </select>
137
138 <select class="auto-terms-log-filter-select" name="log_filter_select_taxonomy" id="log_filter_select_taxonomy">
139 <option value=""><?php esc_html_e('Taxonomy', 'simple-tags'); ?></option>
140 <?php
141 foreach ($taxonomies as $taxonomy) {
142 echo '<option value="' . esc_attr($taxonomy->name) . '" ' . selected($selected_taxonomy, $taxonomy->name, false) . '>' . esc_html($taxonomy->labels->name) . '</option>';
143 }
144 ?>
145 </select>
146
147 <select class="auto-terms-log-filter-select" name="log_source_filter_select" id="log_source_filter_select">
148 <option value=""><?php esc_html_e('Source', 'simple-tags'); ?></option>
149 <?php
150 foreach ($log_actions as $key => $label) {
151 echo '<option value="' . esc_attr($key) . '" ' . selected($selected_source, $key, false) . '>' . esc_html($label) . '</option>';
152 }
153 ?>
154 </select>
155
156 <select class="auto-terms-log-filter-select" name="log_filter_select_status_message" id="log_filter_select_status_message">
157 <option value=""><?php esc_html_e('Status message', 'simple-tags'); ?></option>
158 <?php
159 foreach ($status_messages as $key => $label) {
160 echo '<option value="' . esc_attr($key) . '" ' . selected($selected_status_message, $key, false) . '>' . esc_html($label) . '</option>';
161 }
162 ?>
163 </select>
164
165 <select class="auto-terms-log-filter-select" name="log_filter_select_settings" id="log_filter_select_settings">
166 <option value=""><?php esc_html_e('Settings', 'simple-tags'); ?></option>
167 <?php
168 foreach ($autoterm_settings as $autoterm_setting) {
169 echo '<option value="' . esc_attr($autoterm_setting['ID']) . '" ' . selected($selected_settings, $autoterm_setting['ID'], false) . '>' . esc_html($autoterm_setting['title']) . '</option>';
170 }
171 ?>
172 </select>
173
174 <a href="javascript:void(0)" class="taxopress-logs-tablenav-filter button"><?php esc_html_e('Filter', 'simple-tags'); ?></a>
175
176 </div>
177 <?php
178 }
179 }
180
181 /**
182 * Associative array of columns
183 *
184 * @return array
185 */
186 public function get_columns()
187 {
188 $columns = [
189 'cb' => '<input type="checkbox"/>', //Render a checkbox instead of text
190 'title' => esc_html__('Post', 'simple-tags'),
191 'post_type' => esc_html__('Post type', 'simple-tags'),
192 'taxonomy' => esc_html__('Taxonomy', 'simple-tags'),
193 'source' => esc_html__('Source', 'simple-tags'),
194 'terms' => esc_html__('Terms added', 'simple-tags'),
195 'status_message' => esc_html__('Status message', 'simple-tags'),
196 'date' => esc_html__('Date', 'simple-tags'),
197 'settings' => esc_html__('Settings', 'simple-tags')
198 ];
199
200 return $columns;
201 }
202
203 /**
204 * Render a column when no column specific method exist.
205 *
206 * @param array $item
207 * @param string $column_name
208 *
209 * @return mixed
210 */
211 public function column_default($item, $column_name)
212 {
213 return isset($item->$column_name) ? $item->$column_name : '&mdash;';
214 }
215
216 /** Text displayed when no stterm data is available */
217 public function no_items()
218 {
219 esc_html_e('No item avaliable.', 'simple-tags');
220 }
221
222 /**
223 * The checkbox column
224 *
225 * @param object $item
226 *
227 * @return string|void
228 */
229 protected function column_cb($item)
230 {
231 return sprintf('<input type="checkbox" name="%1$s[]" value="%2$s" />', 'taxopress_autoterms_logs', $item->ID);
232 }
233
234 /**
235 * Get the bulk actions to show in the top page dropdown
236 *
237 * @return array
238 */
239 protected function get_bulk_actions()
240 {
241 $actions = [
242 'taxopress-autoterms-delete-logs' => esc_html__('Delete', 'simple-tags')
243 ];
244
245 return $actions;
246 }
247
248 /**
249 * Process bulk actions
250 */
251 public function process_bulk_action()
252 {
253
254 $query_arg = '_wpnonce';
255 $action = 'bulk-' . $this->_args['plural'];
256 $checked = $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce(sanitize_key(wp_unslash($_REQUEST[$query_arg])), $action) : false;
257
258 if (!$checked || !current_user_can('simple_tags')) {
259 return;
260 }
261
262 if ($this->current_action() === 'taxopress-autoterms-delete-logs') {
263 $taxopress_autoterms_logs = array_map('sanitize_text_field', (array) wp_unslash($_REQUEST['taxopress_autoterms_logs']));
264 if (!empty($taxopress_autoterms_logs)) {
265 foreach ($taxopress_autoterms_logs as $taxopress_autoterms_log) {
266 wp_delete_post($taxopress_autoterms_log, true);
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 [
317 'page' => 'st_autoterms',
318 'tab' => 'logs',
319 'action' => 'taxopress-delete-autoterm-log',
320 'taxopress_autoterms_log' => esc_attr($item->ID),
321 '_wpnonce' => wp_create_nonce('autoterm-action-request-nonce')
322 ],
323 admin_url('admin.php')
324 ),
325 __('Delete Log', 'simple-tags')
326 ),
327 ];
328
329 return $column_name === $primary ? $this->row_actions($actions, false) : '';
330 }
331
332 /**
333 * Method for title column
334 *
335 * @param array $item
336 *
337 * @return string
338 */
339 protected function column_title($item)
340 {
341 $auto_term_log_post_id = get_post_meta($item->ID, '_taxopress_log_post_id', true);
342
343 $title = sprintf(
344 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
345 add_query_arg(
346 [
347 'action' => 'edit',
348 'post' => $auto_term_log_post_id,
349 ],
350 admin_url('post.php')
351 ),
352 esc_html(get_the_title($auto_term_log_post_id))
353 );
354
355 return $title;
356 }
357
358 /**
359 * Method for post_type column
360 *
361 * @param array $item
362 *
363 * @return string
364 */
365 protected function column_post_type($item)
366 {
367 $auto_term_log_post_id = get_post_meta($item->ID, '_taxopress_log_post_id', true);
368 $auto_term_log_posttype = get_post_type_object(get_post_type($auto_term_log_post_id));
369
370 return ($auto_term_log_posttype && !is_wp_error($auto_term_log_posttype)) ? $auto_term_log_posttype->labels->singular_name : '&mdash;';
371 }
372
373 /**
374 * Method for taxonomy column
375 *
376 * @param array $item
377 *
378 * @return string
379 */
380 protected function column_taxonomy($item)
381 {
382 $taxopress_log_taxonomy = get_post_meta($item->ID, '_taxopress_log_taxonomy', true);
383 $taxopress_log_taxonomy_data = get_taxonomy($taxopress_log_taxonomy);
384
385 return ($taxopress_log_taxonomy_data && !is_wp_error($taxopress_log_taxonomy_data)) ? $taxopress_log_taxonomy_data->labels->singular_name : '&mdash;';
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 'weekly_cron_schedule' => esc_html__('Scheduled weekly cron', 'simple-tags')
406 ];
407
408 if (array_key_exists($taxopress_log_action, $log_action_texts)) {
409 return esc_html($log_action_texts[$taxopress_log_action]);
410 } else {
411 return esc_html($taxopress_log_action);
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 * Displays the search box.
504 *
505 * @param string $text The 'submit' button label.
506 * @param string $input_id ID attribute value for the search input field.
507 *
508 *
509 */
510 public function search_box($text, $input_id)
511 {
512 if (!isset($_REQUEST['s']) && !$this->has_items()) {
513 return;
514 }
515
516 $input_id = $input_id . '-search-input';
517
518 if (!empty($_REQUEST['orderby'])) {
519 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['orderby']))) . '" />';
520 }
521 if (!empty($_REQUEST['order'])) {
522 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['order']))) . '" />';
523 }
524 if (!empty($_REQUEST['page'])) {
525 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['page']))) . '" />';
526 }
527 if (!empty($_REQUEST['tab'])) {
528 echo '<input type="hidden" name="tab" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['tab']))) . '" />';
529 }
530
531 $custom_filters = ['log_source_filter', 'log_filter_post_type', 'log_filter_taxonomy', 'log_filter_status_message', 'log_filter_settings'];
532
533 foreach ($custom_filters as $custom_filter) {
534 $filter_value = !empty($_REQUEST[$custom_filter]) ? sanitize_text_field(wp_unslash($_REQUEST[$custom_filter])) : '';
535 echo '<input type="hidden" name="' . esc_attr($custom_filter) . '" value="' . esc_attr($filter_value) . '" />';
536 }
537
538 $log_limit_link = add_query_arg(
539 [
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