fix: get repository itens with `post_status` param

This commit is contained in:
vnmedeiros 2022-01-28 16:35:58 -03:00
parent 233a33607e
commit 0edabe1356
1 changed files with 16 additions and 7 deletions

View File

@ -353,7 +353,7 @@ class Items extends Repository {
$args = apply_filters( 'tainacan_fetch_args', $args, 'items' ); $args = apply_filters( 'tainacan_fetch_args', $args, 'items' );
$should_filter = is_user_logged_in() && ! isset($args['post_status']) && sizeof($cpt) > 1; $should_filter = is_user_logged_in() && sizeof($cpt) > 1;
if ( $should_filter ) { if ( $should_filter ) {
add_filter('posts_where', [$this, '_filter_where'], 10, 2); add_filter('posts_where', [$this, '_filter_where'], 10, 2);
@ -407,6 +407,7 @@ class Items extends Repository {
global $wpdb; global $wpdb;
$clauses = []; $clauses = [];
$user_id = get_current_user_id(); $user_id = get_current_user_id();
$post_status = $wp_query->get( 'post_status' );
foreach ($this->fetching_from_collections as $collection) { foreach ($this->fetching_from_collections as $collection) {
@ -415,24 +416,32 @@ class Items extends Repository {
$clause = '('; $clause = '(';
$clause .= "{$wpdb->posts}.post_type = '{$collection->get_db_identifier()}' AND ("; $clause .= "{$wpdb->posts}.post_type = '{$collection->get_db_identifier()}' AND (";
$status_clause = [];
// public status // public status
$public_states = get_post_stati( array( 'public' => true ) ); $public_states = get_post_stati( array( 'public' => true ) );
$status_clause = [];
foreach ( (array) $public_states as $state ) { foreach ( (array) $public_states as $state ) {
$status_clause[] = "{$wpdb->posts}.post_status = '$state'"; if( empty($post_status) || in_array($state, $post_status) )
$status_clause[] = "{$wpdb->posts}.post_status = '$state'";
} }
$clause .= implode(' OR ', $status_clause);
// private statuses // private statuses
$private_states = get_post_stati( array( 'private' => true ) ); $private_states = get_post_stati( array( 'private' => true ) );
foreach ( (array) $private_states as $state ) { foreach ( (array) $private_states as $state ) {
$clause .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'"; if( empty($post_status) || in_array($state, $post_status) )
$status_clause[] = current_user_can( $read_private_cap ) ? " {$wpdb->posts}.post_status = '$state'" : " {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'";
} }
//draft
// $draft_states = get_post_stati( array( 'draft' => true ) );
$draft_states = ['draft'];
foreach ( (array) $draft_states as $state ) {
if( in_array($state, $post_status) )
$status_clause[] = current_user_can( $read_private_cap ) ? " {$wpdb->posts}.post_status = '$state'" : " {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'";
}
$clause .= implode(' OR ', $status_clause);
$clause .= ')'; $clause .= ')';
$clause .= ')'; $clause .= ')';
$clauses[] = $clause; $clauses[] = $clause;