is_search = true; } parent::__construct( array( 'plural' => __( 'Co-Authors', 'co-authors-plus' ), 'singular' => __( 'Co-Author', 'co-authors-plus' ), ) ); } /** * Perform Co-Authors Query */ function prepare_items() { global $coauthors_plus; $columns = $this->get_columns(); $hidden = array(); $sortable = array( 'display_name' => array( 'display_name', 'ASC' ), 'first_name' => array( 'first_name', 'ASC' ), 'last_name' => array( 'last_name', 'ASC' ), ); $_sortable = apply_filters( 'coauthors_guest_author_sortable_columns', $this->get_sortable_columns() ); foreach ( (array) $_sortable as $id => $data ) { if ( empty( $data ) ) { continue; } $data = (array) $data; if ( ! isset( $data[1] ) ) { $data[1] = false; } $sortable[ $id ] = $data; } $this->_column_headers = array( $columns, $hidden, $sortable ); $paged = ( isset( $_REQUEST['paged'] ) ) ? intval( $_REQUEST['paged'] ) : 1; $per_page = 20; $args = array( 'paged' => $paged, 'posts_per_page' => $per_page, 'post_type' => $coauthors_plus->guest_authors->post_type, 'post_status' => 'any', 'orderby' => 'title', 'order' => 'ASC', ); $args = apply_filters( 'coauthors_guest_author_query_args', $args ); if ( isset( $_REQUEST['orderby'] ) ) { switch ( $_REQUEST['orderby'] ) { case 'display_name': $args['orderby'] = 'title'; break; case 'first_name': case 'last_name': $args['orderby'] = 'meta_value'; $args['meta_key'] = $coauthors_plus->guest_authors->get_post_meta_key( $_REQUEST['orderby'] ); break; } } if ( isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ) ) ) { $args['order'] = strtoupper( $_REQUEST['order'] ); } $this->filters = array( 'show-all' => __( 'Show all', 'co-authors-plus' ), 'with-linked-account' => __( 'With linked account', 'co-authors-plus' ), 'without-linked-account' => __( 'Without linked account', 'co-authors-plus' ), ); if ( isset( $_REQUEST['filter'] ) && array_key_exists( $_REQUEST['filter'], $this->filters ) ) { $this->active_filter = sanitize_key( $_REQUEST['filter'] ); } else { $this->active_filter = 'show-all'; } switch ( $this->active_filter ) { case 'with-linked-account': case 'without-linked-account': $args['meta_key'] = $coauthors_plus->guest_authors->get_post_meta_key( 'linked_account' ); if ( 'with-linked-account' == $this->active_filter ) { $args['meta_compare'] = '!='; } else { $args['meta_compare'] = '='; } $args['meta_value'] = '0'; break; } if ( $this->is_search ) { add_filter( 'posts_where', array( $this, 'filter_query_for_search' ) ); } $author_posts = new WP_Query( $args ); $items = array(); foreach ( $author_posts->get_posts() as $author_post ) { $items[] = $coauthors_plus->guest_authors->get_guest_author_by( 'ID', $author_post->ID ); } if ( $this->is_search ) { remove_filter( 'posts_where', array( $this, 'filter_query_for_search' ) ); } $this->items = $items; $this->set_pagination_args( array( 'total_items' => $author_posts->found_posts, 'per_page' => $per_page, ) ); } function filter_query_for_search( $where ) { global $wpdb; $var = '%' . sanitize_text_field( $_REQUEST['s'] ) . '%'; $where .= $wpdb->prepare( ' AND (post_title LIKE %s OR post_name LIKE %s )', $var, $var ); return $where; } /** * Either there are no co-authors, or the search doesn't match any */ function no_items() { esc_html_e( 'No matching co-authors were found.', 'co-authors-plus' ); } /** * Generate the columns of information to be displayed on our list table */ function get_columns() { $columns = array( 'display_name' => __( 'Display Name', 'co-authors-plus' ), 'first_name' => __( 'First Name', 'co-authors-plus' ), 'last_name' => __( 'Last Name', 'co-authors-plus' ), 'user_email' => __( 'E-mail', 'co-authors-plus' ), 'linked_account' => __( 'Linked Account', 'co-authors-plus' ), 'posts' => __( 'Posts', 'co-authors-plus' ), ); $columns = apply_filters( 'coauthors_guest_author_manage_columns', $columns ); return $columns; } /** * Render a single row */ function single_row( $item ) { static $alternate_class = ''; $alternate_class = ( '' === $alternate_class ? ' alternate' : '' ); $row_class = 'guest-author-static' . $alternate_class . '"'; echo '