Skip to content

Commit

Permalink
Plugin: Guard construct_wp_query_args with check if implemented in Wo…
Browse files Browse the repository at this point in the history
…rdPress core (#32008)
  • Loading branch information
gziolo authored and jffng committed May 19, 2021
1 parent 6db83a3 commit 2d7efc7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 68 deletions.
70 changes: 70 additions & 0 deletions lib/compat/wordpress-5.8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Temporary compatibility shims for features present in Gutenberg.
* This file should be removed when WordPress 5.8.0 becomes the lowest
* supported version by this plugin.
*
* @package gutenberg
*/

if ( ! function_exists( 'construct_wp_query_args' ) ) {
/**
* Helper function that constructs a WP_Query args array from
* a `Query` block properties.
*
* It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks.
*
* @param WP_Block $block Block instance.
* @param int $page Current query's page.
*
* @return array Returns the constructed WP_Query arguments.
*/
function construct_wp_query_args( $block, $page ) {
$query = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
);

if ( isset( $block->context['query'] ) ) {
if ( isset( $block->context['query']['postType'] ) ) {
$query['post_type'] = $block->context['query']['postType'];
}
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) {
$sticky = get_option( 'sticky_posts' );
if ( 'only' === $block->context['query']['sticky'] ) {
$query['post__in'] = $sticky;
} else {
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
}
}
if ( isset( $block->context['query']['exclude'] ) ) {
$query['post__not_in'] = array_merge( $query['post__not_in'], $block->context['query']['exclude'] );
}
if ( isset( $block->context['query']['perPage'] ) ) {
$query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset'];
$query['posts_per_page'] = $block->context['query']['perPage'];
}
if ( isset( $block->context['query']['categoryIds'] ) ) {
$query['category__in'] = $block->context['query']['categoryIds'];
}
if ( isset( $block->context['query']['tagIds'] ) ) {
$query['tag__in'] = $block->context['query']['tagIds'];
}
if ( isset( $block->context['query']['order'] ) ) {
$query['order'] = strtoupper( $block->context['query']['order'] );
}
if ( isset( $block->context['query']['orderBy'] ) ) {
$query['orderby'] = $block->context['query']['orderBy'];
}
if ( isset( $block->context['query']['author'] ) ) {
$query['author'] = $block->context['query']['author'];
}
if ( isset( $block->context['query']['search'] ) ) {
$query['s'] = $block->context['query']['search'];
}
}
return $query;
}
}
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/widgets-page.php';

require __DIR__ . '/compat.php';
require __DIR__ . '/compat/wordpress-5.8.php';
require __DIR__ . '/utils.php';
require __DIR__ . '/editor-settings.php';

Expand Down Expand Up @@ -120,7 +121,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/navigation-page.php';
require __DIR__ . '/experiments-page.php';
require __DIR__ . '/global-styles.php';
require __DIR__ . '/query-utils.php';

require __DIR__ . '/block-supports/generated-classname.php';
require __DIR__ . '/block-supports/colors.php';
Expand Down
67 changes: 0 additions & 67 deletions lib/query-utils.php

This file was deleted.

0 comments on commit 2d7efc7

Please sign in to comment.