esc_html__('Post', 'simple-tags'), //singular name of the listed records 'plural' => esc_html__('Posts', 'simple-tags'), //plural name of the listed records 'ajax' => true //does this table support ajax? ]); } /** * Associative array of columns * * @return array */ public function get_columns() { $columns = [ 'title' => esc_html__('Title', 'simple-tags'), 'taxonomy_terms' => esc_html__('Terms', 'simple-tags'), 'post_types' => esc_html__('Post Type', 'simple-tags'), 'post_status' => esc_html__('Post Status', 'simple-tags'), 'author' => esc_html__('Author', 'simple-tags'), 'date' => esc_html__('Date', 'simple-tags') ]; return $columns; } /** * Columns to make sortable. * * @return array */ protected function get_sortable_columns() { $sortable_columns = [ 'title' => 'title', 'date' => ['date', true], ]; return $sortable_columns; } public function get_all_posts() { $search = (!empty($_REQUEST['s'])) ? sanitize_text_field(wp_unslash($_REQUEST['s'])) : ''; $term_filter = (!empty($_REQUEST['posts_term_filter'])) ? (int) $_REQUEST['posts_term_filter'] : ''; $post_types = (!empty($_REQUEST['posts_post_type_filter'])) ? sanitize_text_field(wp_unslash($_REQUEST['posts_post_type_filter'])) : ''; $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field(wp_unslash($_REQUEST['orderby'])) : 'date'; $order = (!empty($_REQUEST['order'])) ? sanitize_text_field(wp_unslash($_REQUEST['order'])) : 'DESC'; $posts_per_page = $this->get_items_per_page('st_posts_per_page', 20); $page = $this->get_pagenum(); if (empty($post_types)) { $post_types = get_post_types(array('public' => true), 'names'); if (isset($post_types['attachment'])) { unset($post_types['attachment']); } $post_types = array_keys($post_types); } $args = array( 'post_type' => $post_types, 'post_status' => 'any', 'posts_per_page' => $posts_per_page, 'order' => $order, 'orderby' => $orderby, 'paged' => $page ); if (!empty($search)) { $args['s'] = $search; } if (!empty($term_filter)) { $term_filter_data = get_term($term_filter); if ($term_filter_data instanceof WP_Term) { $args['tax_query'] = array( 'relation' => 'AND', array( 'taxonomy' => $term_filter_data->taxonomy, 'field' => 'term_id', 'terms' => $term_filter_data->term_id, ) ); } } $query = new WP_Query($args); $results = [ 'posts' => [], 'max_num_posts' => 0, 'max_num_pages' => 1 ]; if ($query->have_posts()) { $results['posts'] = $query->posts; $results['max_num_posts'] = $query->found_posts; $results['max_num_pages'] = $query->max_num_pages; wp_reset_postdata(); } return $results; } /** * Show single row item * * @param object $post */ public function single_row($post) { $class = ['st-posts-tr']; $id = 'post-' . $post->post_id . ''; echo sprintf('
'taxopress-posts-search-submit']); ?>
_column_headers = $this->get_column_info(); $this->process_bulk_action(); /** * First, lets decide how many records per page to show */ $per_page = $this->get_items_per_page('st_posts_per_page', 20); /** * Fetch the data */ $posts_data = $this->get_all_posts(); $data = $posts_data['posts']; /** * Pagination. */ $current_page = $this->get_pagenum(); $total_items = $posts_data['max_num_posts']; /** * Now we can add the data to the items property, where it can be used by the rest of the class. */ $this->items = $data; /** * We also have to register our pagination options & calculations. */ $this->set_pagination_args([ 'total_items' => $total_items, //calculate the total number of items 'per_page' => $per_page, //depostine how many items to show on a page 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages ]); } /** * Method for name column * * @param object $post * * @return string */ protected function column_title($post) { if (current_user_can('edit_post', $post->ID)) { $title = sprintf( '%2$s', add_query_arg( [ 'post' => $post->ID, 'action' => 'edit' ], admin_url('post.php') ), esc_html(get_the_title($post)) ); } else { $title = '' . get_the_title($post) . ''; } return $title; } /** * The action column * * @param $post * * @return string */ protected function column_author($post) { if (function_exists(('get_post_authors'))) { $authors = get_post_authors($post->ID, true); $author_name = []; foreach ($authors as $author) { if (is_object($author) && isset($author->display_name)) { $author_name[] = $author->display_name; } } $out = join(', ', $author_name); } else { $out = get_the_author_meta('display_name', $post->post_author); } return $out; } /** * The action column * * @param $post * * @return string */ protected function column_post_types($post) { $post_type = $post->post_type; $post_type_object = get_post_type_object($post->post_type); if (is_object($post_type_object)) { $post_type = $post_type_object->label; } $out = '' . $post_type . ''; return $out; } /** * The action column * * @param $post * * @return string */ protected function column_taxonomy_terms($post) { $out = ''; $taxonomy_type = SimpleTags_Plugin::get_option_value('post_terms_taxonomy_type'); // Get all the taxonomies for the post $taxonomies = get_object_taxonomies($post->post_type, 'objects'); if (!empty($taxonomies)) { foreach ($taxonomies as $taxonomy_slug => $taxonomy) { // Check if the taxonomy should be displayed based on $taxonomy_type if ($taxonomy_type === 'public' && $taxonomy->public === false) { continue; } elseif ($taxonomy_type === 'private' && $taxonomy->public === true) { continue; } // Get the assigned terms for each taxonomy $terms = get_the_terms($post->ID, $taxonomy_slug); if (!empty($terms) && !is_wp_error($terms)) { $out .= '