| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utility functions used for handling Query block and blocks |
| 4 |
* that |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Helper function that constructs a WP_Query args object from |
| 11 |
* a `Query` block properties. |
| 12 |
* |
| 13 |
* It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. |
| 14 |
* |
| 15 |
* @param WP_Block $block Block instance. |
| 16 |
* @param int $page Curren query's page. |
| 17 |
* |
| 18 |
* @return object Returns the constructed WP_Query object. |
| 19 |
*/ |
| 20 |
function construct_wp_query_args( $block, $page ) { |
| 21 |
$query = array( |
| 22 |
'post_type' => 'post', |
| 23 |
'order' => 'DESC', |
| 24 |
'orderby' => 'date', |
| 25 |
'post__not_in' => array(), |
| 26 |
); |
| 27 |
|
| 28 |
if ( isset( $block->context['query'] ) ) { |
| 29 |
if ( isset( $block->context['query']['postType'] ) ) { |
| 30 |
$query['post_type'] = $block->context['query']['postType']; |
| 31 |
} |
| 32 |
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { |
| 33 |
$sticky = get_option( 'sticky_posts' ); |
| 34 |
if ( 'only' === $block->context['query']['sticky'] ) { |
| 35 |
$query['post__in'] = $sticky; |
| 36 |
} else { |
| 37 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); |
| 38 |
} |
| 39 |
} |
| 40 |
if ( isset( $block->context['query']['exclude'] ) ) { |
| 41 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $block->context['query']['exclude'] ); |
| 42 |
} |
| 43 |
if ( isset( $block->context['query']['perPage'] ) ) { |
| 44 |
$query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset']; |
| 45 |
$query['posts_per_page'] = $block->context['query']['perPage']; |
| 46 |
} |
| 47 |
if ( isset( $block->context['query']['categoryIds'] ) ) { |
| 48 |
$query['category__in'] = $block->context['query']['categoryIds']; |
| 49 |
} |
| 50 |
if ( isset( $block->context['query']['tagIds'] ) ) { |
| 51 |
$query['tag__in'] = $block->context['query']['tagIds']; |
| 52 |
} |
| 53 |
if ( isset( $block->context['query']['order'] ) ) { |
| 54 |
$query['order'] = strtoupper( $block->context['query']['order'] ); |
| 55 |
} |
| 56 |
if ( isset( $block->context['query']['orderBy'] ) ) { |
| 57 |
$query['orderby'] = $block->context['query']['orderBy']; |
| 58 |
} |
| 59 |
if ( isset( $block->context['query']['author'] ) ) { |
| 60 |
$query['author'] = $block->context['query']['author']; |
| 61 |
} |
| 62 |
if ( isset( $block->context['query']['search'] ) ) { |
| 63 |
$query['s'] = $block->context['query']['search']; |
| 64 |
} |
| 65 |
} |
| 66 |
return $query; |
| 67 |
} |
| 68 |
|