| 1 |
<?php |
| 2 |
/** |
| 3 |
* Temporary compatibility shims for block APIs present in Gutenberg. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Update allowed inline style attributes list. |
| 10 |
* |
| 11 |
* Note: This should be removed when the minimum required WP version is >= 6.2. |
| 12 |
* |
| 13 |
* @param string[] $attrs Array of allowed CSS attributes. |
| 14 |
* @return string[] CSS attributes. |
| 15 |
*/ |
| 16 |
function gutenberg_safe_style_attrs_6_2( $attrs ) { |
| 17 |
$attrs[] = 'position'; |
| 18 |
$attrs[] = 'top'; |
| 19 |
$attrs[] = 'right'; |
| 20 |
$attrs[] = 'bottom'; |
| 21 |
$attrs[] = 'left'; |
| 22 |
$attrs[] = 'z-index'; |
| 23 |
$attrs[] = 'box-shadow'; |
| 24 |
$attrs[] = 'aspect-ratio'; |
| 25 |
|
| 26 |
return $attrs; |
| 27 |
} |
| 28 |
add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs_6_2' ); |
| 29 |
|
| 30 |
/** |
| 31 |
* Helper function that constructs a WP_Query args array from |
| 32 |
* a `Query` block properties. |
| 33 |
* |
| 34 |
* It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. |
| 35 |
* |
| 36 |
* `build_query_vars_from_query_block` was introduced in 5.8, for 6.1 we just need |
| 37 |
* to update that function and not create a new one. |
| 38 |
* |
| 39 |
* @param WP_Block $block Block instance. |
| 40 |
* @param int $page Current query's page. |
| 41 |
* |
| 42 |
* @return array Returns the constructed WP_Query arguments. |
| 43 |
*/ |
| 44 |
function gutenberg_build_query_vars_from_query_block( $block, $page ) { |
| 45 |
$query = array( |
| 46 |
'post_type' => 'post', |
| 47 |
'order' => 'DESC', |
| 48 |
'orderby' => 'date', |
| 49 |
'post__not_in' => array(), |
| 50 |
); |
| 51 |
|
| 52 |
if ( isset( $block->context['query'] ) ) { |
| 53 |
if ( ! empty( $block->context['query']['postType'] ) ) { |
| 54 |
$post_type_param = $block->context['query']['postType']; |
| 55 |
if ( is_post_type_viewable( $post_type_param ) ) { |
| 56 |
$query['post_type'] = $post_type_param; |
| 57 |
} |
| 58 |
} |
| 59 |
if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { |
| 60 |
$sticky = get_option( 'sticky_posts' ); |
| 61 |
if ( 'only' === $block->context['query']['sticky'] ) { |
| 62 |
/** |
| 63 |
* Passing an empty array to post__in will return have_posts() as true (and all posts will be returned). |
| 64 |
* Logic should be used before hand to determine if WP_Query should be used in the event that the array |
| 65 |
* being passed to post__in is empty. |
| 66 |
* |
| 67 |
* @see https://core.trac.wordpress.org/ticket/28099 |
| 68 |
*/ |
| 69 |
$query['post__in'] = ! empty( $sticky ) ? $sticky : array( 0 ); |
| 70 |
$query['ignore_sticky_posts'] = 1; |
| 71 |
} else { |
| 72 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); |
| 73 |
} |
| 74 |
} |
| 75 |
if ( ! empty( $block->context['query']['exclude'] ) ) { |
| 76 |
$excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); |
| 77 |
$excluded_post_ids = array_filter( $excluded_post_ids ); |
| 78 |
$query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); |
| 79 |
} |
| 80 |
if ( |
| 81 |
isset( $block->context['query']['perPage'] ) && |
| 82 |
is_numeric( $block->context['query']['perPage'] ) |
| 83 |
) { |
| 84 |
$per_page = absint( $block->context['query']['perPage'] ); |
| 85 |
$offset = 0; |
| 86 |
|
| 87 |
if ( |
| 88 |
isset( $block->context['query']['offset'] ) && |
| 89 |
is_numeric( $block->context['query']['offset'] ) |
| 90 |
) { |
| 91 |
$offset = absint( $block->context['query']['offset'] ); |
| 92 |
} |
| 93 |
|
| 94 |
$query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; |
| 95 |
$query['posts_per_page'] = $per_page; |
| 96 |
} |
| 97 |
|
| 98 |
// Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility. |
| 99 |
if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) { |
| 100 |
$tax_query = array(); |
| 101 |
if ( ! empty( $block->context['query']['categoryIds'] ) ) { |
| 102 |
$tax_query[] = array( |
| 103 |
'taxonomy' => 'category', |
| 104 |
'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ), |
| 105 |
'include_children' => false, |
| 106 |
); |
| 107 |
} |
| 108 |
if ( ! empty( $block->context['query']['tagIds'] ) ) { |
| 109 |
$tax_query[] = array( |
| 110 |
'taxonomy' => 'post_tag', |
| 111 |
'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ), |
| 112 |
'include_children' => false, |
| 113 |
); |
| 114 |
} |
| 115 |
$query['tax_query'] = $tax_query; |
| 116 |
} |
| 117 |
if ( ! empty( $block->context['query']['taxQuery'] ) ) { |
| 118 |
$query['tax_query'] = array(); |
| 119 |
foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) { |
| 120 |
if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) { |
| 121 |
$query['tax_query'][] = array( |
| 122 |
'taxonomy' => $taxonomy, |
| 123 |
'terms' => array_filter( array_map( 'intval', $terms ) ), |
| 124 |
'include_children' => false, |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
if ( |
| 130 |
isset( $block->context['query']['order'] ) && |
| 131 |
in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) |
| 132 |
) { |
| 133 |
$query['order'] = strtoupper( $block->context['query']['order'] ); |
| 134 |
} |
| 135 |
if ( isset( $block->context['query']['orderBy'] ) ) { |
| 136 |
$query['orderby'] = $block->context['query']['orderBy']; |
| 137 |
} |
| 138 |
if ( |
| 139 |
isset( $block->context['query']['author'] ) && |
| 140 |
(int) $block->context['query']['author'] > 0 |
| 141 |
) { |
| 142 |
$query['author'] = (int) $block->context['query']['author']; |
| 143 |
} |
| 144 |
if ( ! empty( $block->context['query']['search'] ) ) { |
| 145 |
$query['s'] = $block->context['query']['search']; |
| 146 |
} |
| 147 |
if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) { |
| 148 |
$query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Filters the arguments which will be passed to `WP_Query` for the Query Loop Block. |
| 154 |
* |
| 155 |
* Anything to this filter should be compatible with the `WP_Query` API to form |
| 156 |
* the query context which will be passed down to the Query Loop Block's children. |
| 157 |
* This can help, for example, to include additional settings or meta queries not |
| 158 |
* directly supported by the core Query Loop Block, and extend its capabilities. |
| 159 |
* |
| 160 |
* Please note that this will only influence the query that will be rendered on the |
| 161 |
* front-end. The editor preview is not affected by this filter. Also, worth noting |
| 162 |
* that the editor preview uses the REST API, so, ideally, one should aim to provide |
| 163 |
* attributes which are also compatible with the REST API, in order to be able to |
| 164 |
* implement identical queries on both sides. |
| 165 |
* |
| 166 |
* @since 6.1.0 |
| 167 |
* |
| 168 |
* @param array $query Array containing parameters for `WP_Query` as parsed by the block context. |
| 169 |
* @param WP_Block $block Block instance. |
| 170 |
* @param int $page Current query's page. |
| 171 |
*/ |
| 172 |
return apply_filters( 'query_loop_block_query_vars', $query, $block, $page ); |
| 173 |
} |
| 174 |
|