| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for features present in Gutenberg. |
| 4 |
* This file should be removed when WordPress 5.8.0 becomes the lowest |
| 5 |
* supported version by this plugin. |
| 6 |
* |
| 7 |
* @package gutenberg |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! class_exists( 'WP_Block_Editor_Context' ) ) { |
| 11 |
require_once __DIR__ . '/class-wp-block-editor-context.php'; |
| 12 |
} |
| 13 |
|
| 14 |
require_once __DIR__ . '/block-editor.php'; |
| 15 |
require_once __DIR__ . '/blocks.php'; |
| 16 |
|
| 17 |
if ( ! function_exists( 'build_query_vars_from_query_block' ) ) { |
| 18 |
/** |
| 19 |
* Helper function that constructs a WP_Query args array from |
| 20 |
* a `Query` block properties. |
| 21 |
* |
| 22 |
* It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. |
| 23 |
* |
| 24 |
* @param WP_Block $block Block instance. |
| 25 |
* @param int $page Current query's page. |
| 26 |
* |
| 27 |
* @return array Returns the constructed WP_Query arguments. |
| 28 |
*/ |
| 29 |
function build_query_vars_from_query_block( $block, $page ) { |
| 30 |
$query = array( |
| 31 |
'post_type' => 'post', |
| 32 |
'order' => 'DESC', |
| 33 |
'orderby' => 'date', |
| 34 |
'post__not_in' => array(), |
| 35 |
); |
| 36 |
|
| 37 |
if ( isset( $block->context['query'] ) ) { |
| 38 |
if ( ! empty( $block->context['query']['postType'] ) ) { |
| 39 |
$post_type_param = $block->context['query']['postType']; |
| 40 |
if ( is_post_type_viewable( $post_type_param ) ) { |
| 41 |
$query['post_type'] = $post_type_param; |
| 42 |
} |
| 43 |
} |
| 44 |
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { |
| 45 |
$sticky = get_option( 'sticky_posts' ); |
| 46 |
if ( 'only' === $block->context['query']['sticky'] ) { |
| 47 |
$query['post__in'] = $sticky; |
| 48 |
} else { |
| 49 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); |
| 50 |
} |
| 51 |
} |
| 52 |
if ( ! empty( $block->context['query']['exclude'] ) ) { |
| 53 |
$excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); |
| 54 |
$excluded_post_ids = array_filter( $excluded_post_ids ); |
| 55 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); |
| 56 |
} |
| 57 |
if ( |
| 58 |
isset( $block->context['query']['perPage'] ) && |
| 59 |
is_numeric( $block->context['query']['perPage'] ) |
| 60 |
) { |
| 61 |
$per_page = absint( $block->context['query']['perPage'] ); |
| 62 |
$offset = 0; |
| 63 |
|
| 64 |
if ( |
| 65 |
isset( $block->context['query']['offset'] ) && |
| 66 |
is_numeric( $block->context['query']['offset'] ) |
| 67 |
) { |
| 68 |
$offset = absint( $block->context['query']['offset'] ); |
| 69 |
} |
| 70 |
|
| 71 |
$query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; |
| 72 |
$query['posts_per_page'] = $per_page; |
| 73 |
} |
| 74 |
if ( ! empty( $block->context['query']['categoryIds'] ) ) { |
| 75 |
$term_ids = array_map( 'intval', $block->context['query']['categoryIds'] ); |
| 76 |
$term_ids = array_filter( $term_ids ); |
| 77 |
$query['category__in'] = $term_ids; |
| 78 |
} |
| 79 |
if ( ! empty( $block->context['query']['tagIds'] ) ) { |
| 80 |
$term_ids = array_map( 'intval', $block->context['query']['tagIds'] ); |
| 81 |
$term_ids = array_filter( $term_ids ); |
| 82 |
$query['tag__in'] = $term_ids; |
| 83 |
} |
| 84 |
if ( |
| 85 |
isset( $block->context['query']['order'] ) && |
| 86 |
in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) |
| 87 |
) { |
| 88 |
$query['order'] = strtoupper( $block->context['query']['order'] ); |
| 89 |
} |
| 90 |
if ( isset( $block->context['query']['orderBy'] ) ) { |
| 91 |
$query['orderby'] = $block->context['query']['orderBy']; |
| 92 |
} |
| 93 |
if ( |
| 94 |
isset( $block->context['query']['author'] ) && |
| 95 |
(int) $block->context['query']['author'] > 0 |
| 96 |
) { |
| 97 |
$query['author'] = (int) $block->context['query']['author']; |
| 98 |
} |
| 99 |
if ( ! empty( $block->context['query']['search'] ) ) { |
| 100 |
$query['s'] = $block->context['query']['search']; |
| 101 |
} |
| 102 |
} |
| 103 |
return $query; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! function_exists( 'gutenberg_render_legacy_query_loop_block' ) ) { |
| 108 |
/** |
| 109 |
* Renders the legacy `core/query-loop` block on the server. |
| 110 |
* It triggers a developer warning and then calls the renamed |
| 111 |
* block's `render_callback` function output. |
| 112 |
* |
| 113 |
* @param array $attributes Block attributes. |
| 114 |
* @param string $content Block default content. |
| 115 |
* @param WP_Block $block Block instance. |
| 116 |
* |
| 117 |
* @return string Returns the output of the query, structured using the layout defined by the block's inner blocks. |
| 118 |
*/ |
| 119 |
function gutenberg_render_legacy_query_loop_block( $attributes, $content, $block ) { |
| 120 |
trigger_error( |
| 121 |
/* translators: %1$s: Block type */ |
| 122 |
sprintf( __( 'Block %1$s has been renamed to Post Template. %1$s will be supported until WordPress version 5.9.', 'gutenberg' ), $block->name ), |
| 123 |
headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE |
| 124 |
); |
| 125 |
return gutenberg_render_block_core_post_template( $attributes, $content, $block ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! function_exists( 'gutenberg_register_legacy_query_loop_block' ) ) { |
| 130 |
/** |
| 131 |
* Complements the renaming of `Query Loop` to `Post Template`. |
| 132 |
* This ensures backwards compatibility for any users running the Gutenberg |
| 133 |
* plugin who have used Query Loop prior to its renaming. |
| 134 |
* |
| 135 |
* @see https://github.com/WordPress/gutenberg/pull/32514 |
| 136 |
*/ |
| 137 |
function gutenberg_register_legacy_query_loop_block() { |
| 138 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 139 |
if ( $registry->is_registered( 'core/query-loop' ) ) { |
| 140 |
unregister_block_type( 'core/query-loop' ); |
| 141 |
} |
| 142 |
register_block_type( |
| 143 |
'core/query-loop', |
| 144 |
array( |
| 145 |
'category' => 'design', |
| 146 |
'uses_context' => array( |
| 147 |
'queryId', |
| 148 |
'query', |
| 149 |
'queryContext', |
| 150 |
'displayLayout', |
| 151 |
'templateSlug', |
| 152 |
), |
| 153 |
'supports' => array( |
| 154 |
'reusable' => false, |
| 155 |
'html' => false, |
| 156 |
'align' => true, |
| 157 |
), |
| 158 |
'style' => 'wp-block-post-template', |
| 159 |
'render_callback' => 'gutenberg_render_legacy_query_loop_block', |
| 160 |
'skip_inner_blocks' => true, |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
add_action( 'init', 'gutenberg_register_legacy_query_loop_block' ); |
| 166 |
} |
| 167 |
|