PluginProbe
Gutenberg / 10.1.1
Gutenberg v10.1.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / query-utils.php

query-utils.php in Gutenberg 10.1.1, at lib/query-utils.php

68 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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