Skip to content

Enhanced query filter blocks with new display types #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [ "." ]
}
8 changes: 8 additions & 0 deletions build/post-type/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"showLabel": {
"type": "boolean",
"default": true
},
"displayType": {
"type": "string",
"default": "select"
},
"layoutDirection": {
"type": "string",
"default": "vertical"
}
},
"textdomain": "query-filter",
Expand Down
1 change: 1 addition & 0 deletions build/post-type/index-rtl.css

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

2 changes: 1 addition & 1 deletion build/post-type/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => 'ad8227c21a432607ccaf');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => '9f82697921081e520599');
1 change: 1 addition & 0 deletions build/post-type/index.css

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

2 changes: 1 addition & 1 deletion build/post-type/index.js

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

98 changes: 69 additions & 29 deletions build/post-type/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,93 @@
global $wp_query;

$id = 'query-filter-' . wp_generate_uuid4();
$display_type = $attributes['displayType'] ?? 'select';
$layout_direction = $attributes['layoutDirection'] ?? 'vertical';

if ( $block->context['query']['inherit'] ) {
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 ] ) );
$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 ] );
$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' ) );
$post_types = array_map('trim', explode(',', $block->context['query']['postType'] ?? 'post'));

// Support for enhanced query block.
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'] );
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' ] );
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 );
$post_types = array_unique($post_types);
$post_types = array_map('get_post_type_object', $post_types);

if ( empty( $post_types ) ) {
if (empty($post_types)) {
return;
}
?>

<div <?php echo get_block_wrapper_attributes( [ 'class' => 'wp-block-query-filter' ] ); ?> data-wp-interactive="query-filter" data-wp-context="{}">
<label class="wp-block-query-filter-post-type__label wp-block-query-filter__label<?php echo $attributes['showLabel'] ? '' : ' screen-reader-text' ?>" for="<?php echo esc_attr( $id ); ?>">
<?php echo esc_html( $attributes['label'] ?? __( 'Content Type', 'query-filter' ) ); ?>
<div <?php echo get_block_wrapper_attributes(['class' => 'wp-block-query-filter']); ?> data-wp-interactive="query-filter" data-wp-context="{}">
<label class="wp-block-query-filter-post-type__label wp-block-query-filter__label<?php echo $attributes['showLabel'] ? '' : ' screen-reader-text' ?>" for="<?php echo esc_attr($id); ?>">
<?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( $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, $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>

<?php if ($display_type === 'select') : ?>
<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($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, $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>
<?php elseif ($display_type === 'radio') : ?>
<div class="wp-block-query-filter-post-type__radio-group wp-block-query-filter__radio-group<?php echo $layout_direction === 'horizontal' ? ' horizontal' : ''; ?>">
<label>
<input type="radio" id="<?php echo esc_attr($id); ?>" name="<?php echo esc_attr($id); ?>" value="<?php echo esc_attr($base_url) ?>" data-wp-on--change="actions.navigate" <?php checked(empty($_GET[$query_var])); ?> />
<?php echo esc_html($attributes['emptyLabel'] ?: __('All', 'query-filter')); ?>
</label>
<?php foreach ($post_types as $post_type) : ?>
<label>
<input type="radio" name="<?php echo esc_attr($id); ?>" value="<?php echo esc_attr(add_query_arg([$query_var => $post_type->name, $page_var => false], $base_url)) ?>" data-wp-on--change="actions.navigate" <?php checked($post_type->name, wp_unslash($_GET[$query_var] ?? '')); ?> />
<?php echo esc_html($post_type->label); ?>
</label>
<?php endforeach; ?>
</div>
<?php elseif ($display_type === 'checkbox') : ?>
<div class="wp-block-query-filter-post-type__checkbox-group wp-block-query-filter__checkbox-group<?php echo $layout_direction === 'horizontal' ? ' horizontal' : ''; ?>">
<?php
$selected_types = isset($_GET[$query_var]) ? explode(',', wp_unslash($_GET[$query_var])) : [];
?>
<?php foreach ($post_types as $post_type) : ?>
<?php
$is_checked = in_array($post_type->name, $selected_types);
$new_types = $is_checked
? array_diff($selected_types, [$post_type->name])
: array_merge($selected_types, [$post_type->name]);
$new_types = array_filter($new_types);
$checkbox_url = empty($new_types)
? $base_url
: add_query_arg([$query_var => implode(',', $new_types), $page_var => false], $base_url);
?>
<label>
<input type="checkbox" value="<?php echo esc_attr($checkbox_url); ?>" data-wp-on--change="actions.navigate" <?php checked($is_checked); ?> />
<?php echo esc_html($post_type->label); ?>
</label>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
10 changes: 9 additions & 1 deletion build/taxonomy/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@
"showLabel": {
"type": "boolean",
"default": true
},
"displayType": {
"type": "string",
"default": "select"
},
"layoutDirection": {
"type": "string",
"default": "vertical"
}
},
"textdomain": "query-filter",
"editorScript": "file:./index.js",
"style": "query-filter-view",
"style": "file:./index.css",
"viewScriptModule": "file:./view.js",
"render": "file:./render.php"
}
2 changes: 1 addition & 1 deletion build/taxonomy/index-rtl.css

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/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => 'f1456d24ac8e3da497aa');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-i18n'), 'version' => '2f97bf84853a01e2ed52');
2 changes: 1 addition & 1 deletion build/taxonomy/index.css

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

Loading