| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for features present in Gutenberg. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Helper function that constructs a WP_Query args array from |
| 10 |
* a `Query` block properties. |
| 11 |
* |
| 12 |
* It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. |
| 13 |
* |
| 14 |
* `build_query_vars_from_query_block` was introduced in 5.8, for 6.0 we just need |
| 15 |
* to update that function and not create a new one. |
| 16 |
* |
| 17 |
* @param WP_Block $block Block instance. |
| 18 |
* @param int $page Current query's page. |
| 19 |
* |
| 20 |
* @return array Returns the constructed WP_Query arguments. |
| 21 |
*/ |
| 22 |
function gutenberg_build_query_vars_from_query_block( $block, $page ) { |
| 23 |
$query = array( |
| 24 |
'post_type' => 'post', |
| 25 |
'order' => 'DESC', |
| 26 |
'orderby' => 'date', |
| 27 |
'post__not_in' => array(), |
| 28 |
); |
| 29 |
|
| 30 |
if ( isset( $block->context['query'] ) ) { |
| 31 |
if ( ! empty( $block->context['query']['postType'] ) ) { |
| 32 |
$post_type_param = $block->context['query']['postType']; |
| 33 |
if ( is_post_type_viewable( $post_type_param ) ) { |
| 34 |
$query['post_type'] = $post_type_param; |
| 35 |
} |
| 36 |
} |
| 37 |
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { |
| 38 |
$sticky = get_option( 'sticky_posts' ); |
| 39 |
if ( 'only' === $block->context['query']['sticky'] ) { |
| 40 |
$query['post__in'] = $sticky; |
| 41 |
} else { |
| 42 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); |
| 43 |
} |
| 44 |
} |
| 45 |
if ( ! empty( $block->context['query']['exclude'] ) ) { |
| 46 |
$excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); |
| 47 |
$excluded_post_ids = array_filter( $excluded_post_ids ); |
| 48 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); |
| 49 |
} |
| 50 |
if ( |
| 51 |
isset( $block->context['query']['perPage'] ) && |
| 52 |
is_numeric( $block->context['query']['perPage'] ) |
| 53 |
) { |
| 54 |
$per_page = absint( $block->context['query']['perPage'] ); |
| 55 |
$offset = 0; |
| 56 |
|
| 57 |
if ( |
| 58 |
isset( $block->context['query']['offset'] ) && |
| 59 |
is_numeric( $block->context['query']['offset'] ) |
| 60 |
) { |
| 61 |
$offset = absint( $block->context['query']['offset'] ); |
| 62 |
} |
| 63 |
|
| 64 |
$query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; |
| 65 |
$query['posts_per_page'] = $per_page; |
| 66 |
} |
| 67 |
|
| 68 |
// We need to migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility. |
| 69 |
if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) { |
| 70 |
$tax_query = array(); |
| 71 |
if ( ! empty( $block->context['query']['categoryIds'] ) ) { |
| 72 |
$tax_query[] = array( |
| 73 |
'taxonomy' => 'category', |
| 74 |
'terms' => $block->context['query']['categoryIds'], |
| 75 |
'include_children' => false, |
| 76 |
); |
| 77 |
} |
| 78 |
if ( ! empty( $block->context['query']['tagIds'] ) ) { |
| 79 |
$tax_query[] = array( |
| 80 |
'taxonomy' => 'post_tag', |
| 81 |
'terms' => $block->context['query']['tagIds'], |
| 82 |
'include_children' => false, |
| 83 |
); |
| 84 |
} |
| 85 |
$query['tax_query'] = $tax_query; |
| 86 |
} |
| 87 |
if ( ! empty( $block->context['query']['taxQuery'] ) ) { |
| 88 |
$query['tax_query'] = array(); |
| 89 |
foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) { |
| 90 |
if ( ! empty( $terms ) ) { |
| 91 |
$term_ids = array_map( 'intval', $terms ); |
| 92 |
$term_ids = array_filter( $term_ids ); |
| 93 |
|
| 94 |
$query['tax_query'][] = array( |
| 95 |
'taxonomy' => $taxonomy, |
| 96 |
'terms' => $terms, |
| 97 |
'include_children' => false, |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
if ( |
| 103 |
isset( $block->context['query']['order'] ) && |
| 104 |
in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) |
| 105 |
) { |
| 106 |
$query['order'] = strtoupper( $block->context['query']['order'] ); |
| 107 |
} |
| 108 |
if ( isset( $block->context['query']['orderBy'] ) ) { |
| 109 |
$query['orderby'] = $block->context['query']['orderBy']; |
| 110 |
} |
| 111 |
if ( ! empty( $block->context['query']['author'] ) ) { |
| 112 |
$query['author'] = $block->context['query']['author']; |
| 113 |
} |
| 114 |
if ( ! empty( $block->context['query']['search'] ) ) { |
| 115 |
$query['s'] = $block->context['query']['search']; |
| 116 |
} |
| 117 |
} |
| 118 |
return $query; |
| 119 |
} |
| 120 |
|