screen = convert_to_screen( $screen ); add_filter( 'post_column_taxonomy_links', [$this, 'column_taxonomy_links'], 11, 3 ); } public function column_taxonomy_links( $term_links, $taxonomy, $terms ) { if ( $taxonomy !== 'doc_category' ) { return $term_links; } $term_links = []; if ( ! empty( $terms ) ) { $taxonomy_object = get_taxonomy( $taxonomy ); foreach ( $terms as $t ) { $posts_in_term_qv = []; if ( $taxonomy_object->query_var ) { $posts_in_term_qv[$taxonomy_object->query_var] = $t->slug; } else { $posts_in_term_qv['taxonomy'] = $taxonomy; $posts_in_term_qv['term'] = $t->slug; } $label = esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) ); $term_links[] = $this->get_edit_link( $posts_in_term_qv, $label ); } } return $term_links; } /** * Helper to create links to edit.php with params. * * @since 4.4.0 * * @param string[] $args Associative array of URL parameters for the link. * @param string $label Link text. * @param string $class Optional. Class attribute. Default empty string. * @return string The formatted link string. */ protected function get_edit_link( $args, $label, $class = '' ) { global $current; $url = add_query_arg( $args, 'admin.php?page=betterdocs-admin' ); $class_html = ''; $aria_current = ''; if ( ! empty( $class ) ) { $class_html = sprintf( ' class="%s"', esc_attr( $class ) ); if ( 'current' === $class ) { $aria_current = ' aria-current="page"'; } } return sprintf( '%s', esc_url( $url ), $class_html, $aria_current, $label ); } public function docs_wp_query( $per_page ) { $paged = isset( $_REQUEST['paged'] ) ? $_REQUEST['paged'] : 1; $args = [ 'post_type' => 'docs', 'posts_per_page' => $per_page, 'orderby' => 'date', 'paged' => $paged ]; if ( isset( $_GET['s'] ) && strlen( $_GET['s'] ) ) { $args['s'] = $_GET['s']; } if ( isset( $_GET['order'] ) && $_GET['order'] === 'ASC' ) { $args['order'] = 'ASC'; } else { $args['order'] = 'DESC'; } if ( isset( $_GET['author'] ) && $_GET['author'] != 'all' ) { $args['author'] = $_GET['author']; } if ( isset( $_GET['post_status'] ) ) { $args['post_status'] = $_GET['post_status']; } if ( isset( $_GET['view'] ) && ! empty( $_GET['view'] ) ) { $args['meta_key'] = '_betterdocs_meta_views'; $args['orderby'] = 'meta_value_num'; if ( $_GET['view'] === 'most_viewed' ) { $args['order'] = 'DESC'; } else if ( $_GET['view'] === 'least_viewed' ) { $args['order'] = 'ASC'; } } if ( isset( $_GET['date'] ) && ! empty( $_GET['date'] ) ) { $args['orderby'] = $_GET['date']; if ( $_GET['date'] === 'most_recent' ) { $args['order'] = 'DESC'; } else if ( $_GET['date'] === 'least_recent' ) { $args['order'] = 'ASC'; } else if ( $_GET['date'] === 'custom_date' ) { $after = $before = ''; $date_range = explode( '-', $_GET['date_range'] ); if ( is_array( $date_range ) && array_key_exists( 0, $date_range ) ) { $after = rtrim( $date_range[0] ); } if ( is_array( $date_range ) && array_key_exists( 1, $date_range ) ) { $before = ltrim( $date_range[1] ); } $start_date = new DateTime( $after ); $end_date = new DateTime( $before ); if ( $after === $before ) { $start_date = $start_date->format( 'Ymd' ); $args['date_query'] = [ [ 'year' => substr( $start_date, 0, 4 ), 'month' => substr( $start_date, 4, 2 ), 'day' => substr( $start_date, 6, 2 ) ] ]; } else { $start_date = $start_date->modify( '-1 day' ); $end_date = $end_date->modify( '+1 day' ); $args['date_query'] = [ [ 'after' => $start_date->format( 'M d, Y' ), 'before' => $end_date->format( 'M d, Y' ), 'inclusive' => true ] ]; } } } $args['tax_query'] = [ 'relation' => 'AND' ]; if ( isset( $_GET['doc_category'] ) && ! empty( $_GET['doc_category'] ) && ( $_GET['doc_category'] != 'all' ) ) { $args['tax_query'][] = [ 'taxonomy' => 'doc_category', 'field' => 'slug', 'operator' => 'IN', 'terms' => [$_GET['doc_category']], 'include_children' => true ]; } if ( isset( $_GET['knowledgebase'] ) && ! empty( $_GET['knowledgebase'] ) && ( $_GET['knowledgebase'] != 'all' ) ) { $args['tax_query'][] = [ 'taxonomy' => 'knowledge_base', 'field' => 'slug', 'terms' => [$_GET['knowledgebase']], 'operator' => 'IN', 'include_children' => true ]; } return new WP_Query( $args ); } /** * @global string $mode List table view mode. * @global array $avail_post_stati * @global WP_Query $wp_query WordPress Query object. * @global int $per_page */ public function prepare_items() { global $mode, $avail_post_stati, $wp_query, $per_page; $wp_query = $this->docs_wp_query( $per_page ); if ( ! empty( $_REQUEST['mode'] ) ) { $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list'; set_user_setting( 'posts_list_mode', $mode ); } else { $mode = get_user_setting( 'posts_list_mode', 'list' ); } // Is going to call wp(). $avail_post_stati = wp_edit_posts_query(); $this->set_hierarchical_display( is_post_type_hierarchical( $this->screen->post_type ) && 'menu_order title' === $wp_query->query['orderby'] ); $post_type = $this->screen->post_type; $per_page = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' ); /** This filter is documented in wp-admin/includes/post.php */ $per_page = apply_filters( 'edit_posts_per_page', $per_page, $post_type ); if ( $this->hierarchical_display ) { $total_items = $wp_query->post_count; } elseif ( $wp_query->found_posts || $this->get_pagenum() === 1 ) { $total_items = $wp_query->found_posts; } else { $post_counts = (array) wp_count_posts( $post_type, 'readable' ); if ( isset( $_REQUEST['post_status'] ) && in_array( $_REQUEST['post_status'], $avail_post_stati, true ) ) { $total_items = $post_counts[$_REQUEST['post_status']]; } elseif ( isset( $_REQUEST['show_sticky'] ) && $_REQUEST['show_sticky'] ) { $total_items = $this->sticky_posts_count; } elseif ( isset( $_GET['author'] ) && get_current_user_id() == $_GET['author'] ) { $total_items = $this->user_posts_count; } else { $total_items = array_sum( $post_counts ); // Subtract post types that are not included in the admin all list. foreach ( get_post_stati( ['show_in_admin_all_list' => false] ) as $state ) { $total_items -= $post_counts[$state]; } } } $this->is_trash = isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status']; $this->set_pagination_args( [ 'total_items' => $total_items, 'per_page' => $per_page ] ); } /** * Displays the table. * */ public function display() { $singular = $this->_args['singular']; $this->screen->render_screen_reader_content( 'heading_list' ); ?>