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 / posts-table.php

posts-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.44.0, at inc/posts-table.php

539 lines 18.0 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 Taxopress_Posts_List extends WP_List_Table
7 {
8 /**
9 * Current level for output.
10 *
11 * @since 4.3.0
12 * @var int
13 */
14 protected $current_level = 0;
15
16 /** Class constructor */
17 public function __construct()
18 {
19
20 parent::__construct([
21 'singular' => esc_html__('Post', 'simple-tags'), //singular name of the listed records
22 'plural' => esc_html__('Posts', 'simple-tags'), //plural name of the listed records
23 'ajax' => true //does this table support ajax?
24 ]);
25 }
26
27 /**
28 * Associative array of columns
29 *
30 * @return array
31 */
32 function get_columns()
33 {
34 $columns = [
35 'title' => esc_html__('Title', 'simple-tags'),
36 'taxonomy_terms' => esc_html__('Terms', 'simple-tags'),
37 'post_types' => esc_html__('Post Type', 'simple-tags'),
38 'post_status' => esc_html__('Post Status', 'simple-tags'),
39 'author' => esc_html__('Author', 'simple-tags'),
40 'date' => esc_html__('Date', 'simple-tags')
41 ];
42
43 return $columns;
44 }
45
46 /**
47 * Columns to make sortable.
48 *
49 * @return array
50 */
51 protected function get_sortable_columns()
52 {
53 $sortable_columns = [
54 'title' => 'title',
55 'date' => ['date', true],
56 ];
57
58 return $sortable_columns;
59 }
60
61 public function get_all_posts()
62 {
63
64 $search = (!empty($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : '';
65 $term_filter = (!empty($_REQUEST['posts_term_filter'])) ? (int) $_REQUEST['posts_term_filter'] : '';
66 $post_types = (!empty($_REQUEST['posts_post_type_filter'])) ? sanitize_text_field($_REQUEST['posts_post_type_filter']) : '';
67
68 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'date';
69 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'DESC';
70
71
72 $posts_per_page = $this->get_items_per_page('st_posts_per_page', 20);
73
74 $page = $this->get_pagenum();
75
76 if (empty($post_types)) {
77 $post_types = get_post_types(array('public' => true), 'names');
78 if (isset($post_types['attachment'])) {
79 unset($post_types['attachment']);
80 }
81 $post_types = array_keys($post_types);
82 }
83
84 $args = array(
85 'post_type' => $post_types,
86 'post_status' => 'any',
87 'posts_per_page' => $posts_per_page,
88 'order' => $order,
89 'orderby' => $orderby,
90 'paged' => $page
91 );
92
93 if (!empty($search)) {
94 $args['s'] = $search;
95 }
96
97 if (!empty($term_filter)) {
98 $term_filter_data = get_term($term_filter);
99 if ($term_filter_data instanceof WP_Term) {
100 $args['tax_query'] = array(
101 'relation' => 'AND',
102 array(
103 'taxonomy' => $term_filter_data->taxonomy,
104 'field' => 'term_id',
105 'terms' => $term_filter_data->term_id,
106 )
107 );
108 }
109 }
110
111 $query = new WP_Query($args);
112
113 $results = [
114 'posts' => [],
115 'max_num_posts' => 0,
116 'max_num_pages' => 1
117 ];
118
119 if ($query->have_posts()) {
120 $results['posts'] = $query->posts;
121 $results['max_num_posts'] = $query->found_posts;
122 $results['max_num_pages'] = $query->max_num_pages;
123 wp_reset_postdata();
124 }
125
126
127 return $results;
128 }
129
130 /**
131 * Show single row item
132 *
133 * @param object $post
134 */
135 public function single_row($post)
136 {
137 $class = ['st-posts-tr'];
138 $id = 'post-' . $post->post_id . '';
139 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
140 $this->single_row_columns($post);
141 echo '</tr>';
142 }
143
144 /**
145 * Add custom filter to tablenav
146 *
147 * @param string $which
148 */
149 protected function extra_tablenav($which)
150 {
151
152 if ('top' === $which) {
153
154 $post_types = get_post_types(['public' => true], 'objects');
155
156 $posts_term_filter = (!empty($_REQUEST['posts_term_filter'])) ? (int) $_REQUEST['posts_term_filter'] : '';
157 $posts_post_type_filter = (!empty($_REQUEST['posts_post_type_filter'])) ? sanitize_text_field($_REQUEST['posts_post_type_filter']) : '';
158 ?>
159
160
161 <div class="alignleft actions autoposts-posts-table-filter">
162 <select data-nonce="<?php echo esc_attr(wp_create_nonce('taxopress-term-search')); ?>"
163 class="posts-term-filter-select taxopress-select2 taxopress-term-search"
164 data-placeholder="<?php esc_attr_e('Terms Filter', 'simple-tags'); ?>"
165 name="posts_term_filter"
166 id="posts_term_filter"
167 >
168 <option value=""><?php esc_html_e('', 'simple-tags'); ?></option>
169 <?php
170 if (!empty($posts_term_filter)) {
171 $posts_term_filter_data = get_term($posts_term_filter);
172 if (is_object($posts_term_filter_data) && !is_wp_error($posts_term_filter_data) && isset($posts_term_filter_data->term_id)) {
173 echo '<option value="' . esc_attr($posts_term_filter_data->term_id) . '" selected>' . esc_html($posts_term_filter_data->name) . '</option>';
174 }
175
176 }
177 ?>
178 </select>
179
180 <select class="posts-post-type-filter-select taxopress-select2 taxopress-simple-select2"
181 name="posts_post_type_filter"
182 id="posts_post_type_filter"
183 data-placeholder="<?php esc_attr_e('Post Types Filter', 'simple-tags'); ?>">
184 <option value=""><?php esc_html_e('All Post Types', 'simple-tags'); ?></option>
185 <?php
186 foreach ($post_types as $post_type) {
187 echo '<option value="' . esc_attr($post_type->name) . '" ' . selected($posts_post_type_filter, $post_type->name, false) . '>' . esc_html($post_type->label) . '</option>';
188 }
189 ?>
190 </select>
191
192 <a href="javascript:void(0)" class="taxopress-posts-tablenav-filter button"><?php esc_html_e('Filter', 'simple-tags'); ?></a>
193
194 </div>
195 <?php
196 }
197 }
198
199 /**
200 * Render a column when no column specific method exist.
201 *
202 * @param object $post
203 * @param string $column_name
204 *
205 * @return mixed
206 */
207 public function column_default($post, $column_name)
208 {
209 return !empty($post->$column_name) ? $post->$column_name : '&mdash;';
210 }
211
212 /** Text displayed when no stpost data is available */
213 public function no_items()
214 {
215 esc_html_e('No posts found.', 'simple-tags');
216 }
217
218 /**
219 * Displays the search box.
220 *
221 * @param string $text The 'submit' button label.
222 * @param string $input_id ID attribute value for the search input field.
223 *
224 *
225 */
226 public function search_box($text, $input_id)
227 {
228 if (empty($_REQUEST['s']) && !$this->has_items()) {
229 //return;
230 }
231
232 $input_id = $input_id . '-search-input';
233
234 if (!empty($_REQUEST['orderby'])) {
235 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
236 }
237 if (!empty($_REQUEST['order'])) {
238 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
239 }
240 if (!empty($_REQUEST['page'])) {
241 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
242 }
243
244 $custom_filters = ['posts_term_filter', 'posts_post_type_filter'];
245
246 foreach ($custom_filters as $custom_filter) {
247 $filter_value = !empty($_REQUEST[$custom_filter]) ? sanitize_text_field($_REQUEST[$custom_filter]) : '';
248 echo '<input type="hidden" name="' . esc_attr($custom_filter) . '" value="' . esc_attr($filter_value) . '" />';
249 }
250 ?>
251 <p class="search-box">
252 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
253 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s" value="<?php _admin_search_query(); ?>" />
254 <?php submit_button($text, '', '', false, ['id' => 'taxopress-posts-search-submit']); ?>
255 </p>
256 <?php
257 }
258
259 /**
260 * Sets up the items (roles) to list.
261 */
262 public function prepare_items()
263 {
264
265 $this->_column_headers = $this->get_column_info();
266 $this->process_bulk_action();
267
268 /**
269 * First, lets decide how many records per page to show
270 */
271 $per_page = $this->get_items_per_page('st_posts_per_page', 20);
272
273 /**
274 * Fetch the data
275 */
276 $posts_data = $this->get_all_posts();
277 $data = $posts_data['posts'];
278
279 /**
280 * Pagination.
281 */
282 $current_page = $this->get_pagenum();
283 $total_items = $posts_data['max_num_posts'];
284
285 /**
286 * Now we can add the data to the items property, where it can be used by the rest of the class.
287 */
288 $this->items = $data;
289
290 /**
291 * We also have to register our pagination options & calculations.
292 */
293 $this->set_pagination_args([
294 'total_items' => $total_items, //calculate the total number of items
295 'per_page' => $per_page, //depostine how many items to show on a page
296 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
297 ]);
298 }
299
300 /**
301 * Method for name column
302 *
303 * @param object $post
304 *
305 * @return string
306 */
307 protected function column_title($post)
308 {
309
310 if (current_user_can('edit_post', $post->ID)) {
311 $title = sprintf(
312 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
313 add_query_arg(
314 [
315 'post' => $post->ID,
316 'action' => 'edit'
317 ],
318 admin_url('post.php')
319 ),
320 esc_html(get_the_title($post))
321 );
322 } else {
323 $title = '<strong><span class="row-title">'. get_the_title($post) .'</span></strong>';
324 }
325
326 return $title;
327 }
328
329 /**
330 * The action column
331 *
332 * @param $post
333 *
334 * @return string
335 */
336 protected function column_author($post)
337 {
338
339 if (function_exists(('get_post_authors'))) {
340 $authors = get_post_authors($post->ID, true);
341 $author_name = [];
342 foreach ($authors as $author) {
343 if (is_object($author) && isset($author->display_name)) {
344 $author_name[] = $author->display_name;
345 }
346 }
347 $out = join(', ', $author_name);
348 } else {
349 $out = get_the_author_meta('display_name', $post->post_author);
350 }
351
352 return $out;
353 }
354
355 /**
356 * The action column
357 *
358 * @param $post
359 *
360 * @return string
361 */
362 protected function column_post_types($post)
363 {
364 $post_type = $post->post_type;
365 $post_type_object = get_post_type_object($post->post_type);
366 if (is_object($post_type_object)) {
367 $post_type = $post_type_object->label;
368 }
369
370 $out = '<a href="'. $this->update_or_add_url_parameter('posts_post_type_filter', $post->post_type) .'">'. $post_type .'</a>';
371
372 return $out;
373 }
374
375 /**
376 * The action column
377 *
378 * @param $post
379 *
380 * @return string
381 */
382 protected function column_taxonomy_terms($post)
383 {
384 $out = '';
385
386 $taxonomy_type = SimpleTags_Plugin::get_option_value('post_terms_taxonomy_type');
387
388 // Get all the taxonomies for the post
389 $taxonomies = get_object_taxonomies($post->post_type, 'objects');
390
391 if (!empty($taxonomies)) {
392 foreach ($taxonomies as $taxonomy_slug => $taxonomy) {
393 // Check if the taxonomy should be displayed based on $taxonomy_type
394 if ($taxonomy_type === 'public' && $taxonomy->public === false) {
395 continue;
396 } elseif ($taxonomy_type === 'private' && $taxonomy->public === true) {
397 continue;
398 }
399
400 // Get the assigned terms for each taxonomy
401 $terms = get_the_terms($post->ID, $taxonomy_slug);
402 if (!empty($terms) && !is_wp_error($terms)) {
403 $out .='<div class="taxopress-post-taxonomy">';
404 $out .="<strong>{$taxonomy->labels->name}:</strong> ";
405 $out .='<span class="taxopress-post-taxonomy-terms">';
406
407 $term_links = [];
408 foreach ($terms as $term) {
409 $link_label = esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy_slug, 'display'));
410 $link_url = $this->update_or_add_url_parameter('posts_term_filter', $term->term_id);
411 $term_links[] = sprintf(
412 '<a href="%s">%s</a>',
413 esc_url($link_url),
414 esc_html($link_label)
415 );
416 }
417 $out .= implode(', ', $term_links);
418 $out .="</span>";
419 $out .='</div>';
420 }
421 }
422 }
423
424 return $out;
425 }
426
427 protected function update_or_add_url_parameter($param_name, $param_value) {
428
429 $url = sanitize_text_field($_SERVER['REQUEST_URI']);
430
431 // Check if the parameter already exists in the URL
432 $existing_param = get_query_var($param_name);
433
434 if ($existing_param) {
435 // Update the parameter value
436 $url = add_query_arg($param_name, $param_value, $url);
437 } else {
438 // Add the parameter with its value
439 $url = add_query_arg(array($param_name => $param_value), $url);
440 }
441
442 return $url;
443 }
444
445 /**
446 * The action column
447 *
448 * @param $post
449 *
450 * @return string
451 */
452 protected function column_post_status($post)
453 {
454 $post_status = $post->post_status;
455 $post_status_object = get_post_status_object($post->post_status);
456 if (is_object($post_status_object)) {
457 $post_status = $post_status_object->label;
458 }
459
460 $out = $post_status;
461
462 return $out;
463 }
464
465 /**
466 * Handles the post date column output.
467 *
468 * @since 4.3.0
469 *
470 * @global string $mode List table view mode.
471 *
472 * @param WP_Post $post The current WP_Post object.
473 */
474 public function column_date( $post ) {
475
476 $mode = get_user_setting( 'posts_list_mode', 'list' );
477 if ( '0000-00-00 00:00:00' === $post->post_date ) {
478 $t_time = __( 'Unpublished' );
479 $time_diff = 0;
480 } else {
481 $t_time = sprintf(
482 /* translators: 1: Post date, 2: Post time. */
483 __( '%1$s at %2$s' ),
484 /* translators: Post date format. See https://www.php.net/manual/datetime.format.php */
485 get_the_time( __( 'Y/m/d' ), $post ),
486 /* translators: Post time format. See https://www.php.net/manual/datetime.format.php */
487 get_the_time( __( 'g:i a' ), $post )
488 );
489
490 $time = get_post_timestamp( $post );
491 $time_diff = time() - $time;
492 }
493
494 if ( 'publish' === $post->post_status ) {
495 $status = __( 'Published' );
496 } elseif ( 'future' === $post->post_status ) {
497 if ( $time_diff > 0 ) {
498 $status = '<strong class="error-message">' . __( 'Missed schedule' ) . '</strong>';
499 } else {
500 $status = __( 'Scheduled' );
501 }
502 } else {
503 $status = __( 'Last Modified' );
504 }
505
506 /**
507 * Filters the status text of the post.
508 *
509 * @since 4.8.0
510 *
511 * @param string $status The status text.
512 * @param WP_Post $post Post object.
513 * @param string $column_name The column name.
514 * @param string $mode The list display mode ('excerpt' or 'list').
515 */
516 $status = apply_filters( 'post_date_column_status', $status, $post, 'date', $mode );
517
518 if ( $status ) {
519 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
520 echo $status . '<br />';
521 }
522
523 /**
524 * Filters the published time of the post.
525 *
526 * @since 2.5.1
527 * @since 5.5.0 Removed the difference between 'excerpt' and 'list' modes.
528 * The published time and date are both displayed now,
529 * which is equivalent to the previous 'excerpt' mode.
530 *
531 * @param string $t_time The published time.
532 * @param WP_Post $post Post object.
533 * @param string $column_name The column name.
534 * @param string $mode The list display mode ('excerpt' or 'list').
535 */
536 echo apply_filters( 'post_date_column_time', $t_time, $post, 'date', $mode ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
537 }
538 }
539