Skip to content

Commit

Permalink
Merge pull request #9 from humanmade/support-inherited-queries
Browse files Browse the repository at this point in the history
Support for inherited queries by filters
  • Loading branch information
roborourke authored Nov 5, 2024
2 parents 327ecb8 + 2fcee87 commit 366f064
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 59 deletions.
33 changes: 28 additions & 5 deletions build/post-type/render.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
<?php
global $wp_query;

$id = 'query-filter-' . wp_generate_uuid4();

$query_id = $block->context['queryId'] ?? 0;
$query_var = sprintf( 'query-%d-post_type', $query_id );
if ( $block->context['query']['inherit'] ) {
$query_var = 'query-post_type';
$page_var = 'page';
$base_url = str_replace( '/page/' . get_query_var( 'paged' ), '', remove_query_arg( [ $query_var, $page_var ] ) );
} else {
$query_id = $block->context['queryId'] ?? 0;
$query_var = sprintf( 'query-%d-post_type', $query_id );
$page_var = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$base_url = remove_query_arg( [ $query_var, $page_var ] );
}

$post_types = array_map( 'trim', explode( ',', $block->context['query']['postType'] ?? 'post' ) );

// Support for enhanced query block.
if ( isset( $block->context['query']['multiple_posts'] ) ) {
if ( isset( $block->context['query']['multiple_posts'] ) && is_array( $block->context['query']['multiple_posts'] ) ) {
$post_types = array_merge( $post_types, $block->context['query']['multiple_posts'] );
}

// Fill in inherited query types.
if ( $block->context['query']['inherit'] ) {
$inherited_post_types = $wp_query->get( 'query-filter-post_type' ) === 'any'
? get_post_types( [ 'public' => true, 'exclude_from_search' => false ] )
: (array) $wp_query->get( 'query-filter-post_type' );

$post_types = array_merge( $post_types, $inherited_post_types );
if ( ! get_option( 'wp_attachment_pages_enabled' ) ) {
$post_types = array_diff( $post_types, [ 'attachment' ] );
}
}

$post_types = array_unique( $post_types );
$post_types = array_map( 'get_post_type_object', $post_types );

if ( empty( $post_types ) ) {
Expand All @@ -23,9 +46,9 @@
<?php echo esc_html( $attributes['label'] ?? __( 'Content Type', 'query-filter' ) ); ?>
</label>
<select class="wp-block-query-filter-post-type__select wp-block-query-filter__select" id="<?php echo esc_attr( $id ); ?>" data-wp-on--change="actions.navigate">
<option value="<?php echo esc_attr( remove_query_arg( [ $query_var, "query-{$query_id}-page" ] ) ) ?>"><?php echo esc_html( $attributes['emptyLabel'] ?: __( 'All', 'query-filter' ) ); ?></option>
<option value="<?php echo esc_attr( $base_url ) ?>"><?php echo esc_html( $attributes['emptyLabel'] ?: __( 'All', 'query-filter' ) ); ?></option>
<?php foreach ( $post_types as $post_type ) : ?>
<option value="<?php echo esc_attr( add_query_arg( [ $query_var => $post_type->name, "query-{$query_id}-page" => false ] ) ) ?>" <?php selected( $post_type->name, wp_unslash( $_GET[ $query_var ] ?? '' ) ); ?>><?php echo esc_html( $post_type->label ); ?></option>
<option value="<?php echo esc_attr( add_query_arg( [ $query_var => $post_type->name, $page_var => false ], $base_url ) ) ?>" <?php selected( $post_type->name, wp_unslash( $_GET[ $query_var ] ?? '' ) ); ?>><?php echo esc_html( $post_type->label ); ?></option>
<?php endforeach; ?>
</select>
</div>
2 changes: 1 addition & 1 deletion build/taxonomy/index.js.map

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions build/taxonomy/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@

$id = 'query-filter-' . wp_generate_uuid4();

$query_id = $block->context['queryId'] ?? 0;
$query_var = sprintf( 'query-%d-%s', $query_id, $attributes['taxonomy'] );

$taxonomy = get_taxonomy( $attributes['taxonomy'] );

if ( $block->context['query']['inherit'] ) {
$query_var = sprintf( 'query-%s', $attributes['taxonomy'] );
$page_var = 'page';
$base_url = str_replace( '/page/' . get_query_var( 'paged' ), '', remove_query_arg( [ $query_var, $page_var ] ) );
} else {
$query_id = $block->context['queryId'] ?? 0;
$query_var = sprintf( 'query-%d-%s', $query_id, $attributes['taxonomy'] );
$page_var = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$base_url = remove_query_arg( [ $query_var, $page_var ] );
}

$terms = get_terms( [
'hide_empty' => true,
'taxonomy' => $attributes['taxonomy'],
Expand All @@ -26,9 +34,9 @@
<?php echo esc_html( $attributes['label'] ?? $taxonomy->label ); ?>
</label>
<select class="wp-block-query-filter-post-type__select wp-block-query-filter__select" id="<?php echo esc_attr( $id ); ?>" data-wp-on--change="actions.navigate">
<option value="<?php echo esc_attr( remove_query_arg( [ $query_var, "query-{$query_id}-page" ] ) ) ?>"><?php echo esc_html( $attributes['emptyLabel'] ?: __( 'All', 'query-filter' ) ); ?></option>
<option value="<?php echo esc_attr( $base_url ) ?>"><?php echo esc_html( $attributes['emptyLabel'] ?: __( 'All', 'query-filter' ) ); ?></option>
<?php foreach ( $terms as $term ) : ?>
<option value="<?php echo esc_attr( add_query_arg( [ $query_var => $term->slug, "query-{$query_id}-page" => false ] ) ) ?>" <?php selected( $term->slug, wp_unslash( $_GET[ $query_var ] ?? '' ) ); ?>><?php echo esc_html( $term->name ); ?></option>
<option value="<?php echo esc_attr( add_query_arg( [ $query_var => $term->slug, $page_var => false ], $base_url ) ) ?>" <?php selected( $term->slug, wp_unslash( $_GET[ $query_var ] ?? '' ) ); ?>><?php echo esc_html( $term->name ); ?></option>
<?php endforeach; ?>
</select>
</div>
2 changes: 1 addition & 1 deletion build/taxonomy/view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'efddb0d32ddebe37933e', 'type' => 'module');
<?php return array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'e7ba522c0e73d74a56f1', 'type' => 'module');
2 changes: 1 addition & 1 deletion build/taxonomy/view.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/taxonomy/view.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "humanmade/query-filter",
"description": "Query Loop Block filters",
"type": "wordpress-plugin",
"require": {
"composer/installers": "^1 || ^2"
},
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Human Made Limited",
"email": "hello@humanmade.com"
}
]
"name": "humanmade/query-filter",
"description": "Query Loop Block filters",
"type": "wordpress-plugin",
"require": {
"composer/installers": "^1 || ^2"
},
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Human Made Limited",
"email": "hello@humanmade.com"
}
]
}
Loading

0 comments on commit 366f064

Please sign in to comment.