From 662966217b3376b7193f7eddf2ebe368306fe0ea Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 Nov 2020 10:19:15 +0200 Subject: [PATCH 01/17] Add a "customize query" checkbox --- .../block-library/src/query-loop/index.php | 7 ++ .../query/edit/query-inspector-controls.js | 116 +++++++++++------- 2 files changed, 76 insertions(+), 47 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 5acc340bd66e23..399b6fe047506a 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -65,6 +65,13 @@ function render_block_core_query_loop( $attributes, $content, $block ) { if ( isset( $block->context['query']['search'] ) ) { $query['s'] = $block->context['query']['search']; } + + if ( isset( $block->context['query']['customQuery'] ) && false === $block->context['query']['customQuery'] ) { + global $wp_query; + if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { + $query = $wp_query->query_vars; + } + } } $posts = get_posts( $query ); diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index a558884ca1f917..03943c5cea0f82 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -13,6 +13,7 @@ import { TextControl, FormTokenField, SelectControl, + CheckboxControl, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; @@ -38,6 +39,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { author: selectedAuthorId, postType, sticky, + customQuery, } = query; const [ showCategories, setShowCategories ] = useState( true ); const [ showTags, setShowTags ] = useState( true ); @@ -126,6 +128,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { debounce( () => setQuery( { search: querySearch } ), 250 ), [ querySearch ] ); + useEffect( () => { onChangeDebounced(); return onChangeDebounced.cancel; @@ -133,19 +136,35 @@ export default function QueryInspectorControls( { query, setQuery } ) { return ( - - setQuery( { order: value } ) } - onOrderByChange={ ( value ) => - setQuery( { orderBy: value } ) + + setQuery( { customQuery: !! value } ) } /> + { customQuery && ( + + ) } + { customQuery && ( + + setQuery( { order: value } ) + } + onOrderByChange={ ( value ) => + setQuery( { orderBy: value } ) + } + /> + ) } { showSticky && ( ) } - - { showCategories && categories?.terms?.length > 0 && ( - ( { - id: categoryId, - value: categories.mapById[ categoryId ].name, + { customQuery && ( + + { showCategories && categories?.terms?.length > 0 && ( + ( { + id: categoryId, + value: + categories.mapById[ categoryId ].name, + } ) + ) } + suggestions={ categories.names } + onChange={ onCategoriesChange } + /> + ) } + { showTags && tags?.terms?.length > 0 && ( + ( { + id: tagId, + value: tags.mapById[ tagId ].name, + } ) ) } + suggestions={ tags.names } + onChange={ onTagsChange } + /> + ) } + + setQuery( { + author: value !== '' ? +value : undefined, } ) - ) } - suggestions={ categories.names } - onChange={ onCategoriesChange } + } /> - ) } - { showTags && tags?.terms?.length > 0 && ( - ( { - id: tagId, - value: tags.mapById[ tagId ].name, - } ) ) } - suggestions={ tags.names } - onChange={ onTagsChange } + - ) } - - setQuery( { - author: value !== '' ? +value : undefined, - } ) - } - /> - - + + ) } ); } From 9d3d83595ecfe7ec69e11e47cc781d6d10ba8940 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 Nov 2020 10:58:21 +0200 Subject: [PATCH 02/17] Add overrides --- .../block-library/src/query-loop/index.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 399b6fe047506a..dd28fe5f48ef74 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -66,11 +66,36 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $query['s'] = $block->context['query']['search']; } + // Override the custom query with the global query if needed. if ( isset( $block->context['query']['customQuery'] ) && false === $block->context['query']['customQuery'] ) { + + // Get the global query. global $wp_query; + + // Make sure the query has vars before using it. if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { $query = $wp_query->query_vars; } + + // Override global query for sticky posts. + 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 ); + } + } + + // Override offset. + if ( isset( $block->context['query']['perPage'] ) ) { + $query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset']; + } + + // Override posts_per_page. + if ( isset( $block->context['query']['perPage'] ) ) { + $query['posts_per_page'] = $block->context['query']['perPage']; + } } } From 166d52595a8089cd2f97e22adf38760fb106a99c Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 Nov 2020 11:03:05 +0200 Subject: [PATCH 03/17] improve query overrides --- .../block-library/src/query-loop/index.php | 56 ++++++------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index dd28fe5f48ef74..1618e1ff53c049 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -26,8 +26,18 @@ function render_block_core_query_loop( $attributes, $content, $block ) { 'post__not_in' => array(), ); + $use_global_query = ( isset( $block->context['query']['customQuery'] ) && false === $block->context['query']['customQuery'] ); + + // Override the custom query with the global query if needed. + if ( $use_global_query ) { + global $wp_query; + if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { + $query = $wp_query->query_vars; + } + } + if ( isset( $block->context['query'] ) ) { - if ( isset( $block->context['query']['postType'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['postType'] ) ) { $query['post_type'] = $block->context['query']['postType']; } if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { @@ -44,59 +54,27 @@ function render_block_core_query_loop( $attributes, $content, $block ) { if ( isset( $block->context['query']['perPage'] ) ) { $query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset']; } - if ( isset( $block->context['query']['categoryIds'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['categoryIds'] ) ) { $query['category__in'] = $block->context['query']['categoryIds']; } - if ( isset( $block->context['query']['tagIds'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['tagIds'] ) ) { $query['tag__in'] = $block->context['query']['tagIds']; } - if ( isset( $block->context['query']['order'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['order'] ) ) { $query['order'] = strtoupper( $block->context['query']['order'] ); } - if ( isset( $block->context['query']['orderBy'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['orderBy'] ) ) { $query['orderby'] = $block->context['query']['orderBy']; } if ( isset( $block->context['query']['perPage'] ) ) { $query['posts_per_page'] = $block->context['query']['perPage']; } - if ( isset( $block->context['query']['author'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['author'] ) ) { $query['author'] = $block->context['query']['author']; } - if ( isset( $block->context['query']['search'] ) ) { + if ( ! $use_global_query && isset( $block->context['query']['search'] ) ) { $query['s'] = $block->context['query']['search']; } - - // Override the custom query with the global query if needed. - if ( isset( $block->context['query']['customQuery'] ) && false === $block->context['query']['customQuery'] ) { - - // Get the global query. - global $wp_query; - - // Make sure the query has vars before using it. - if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { - $query = $wp_query->query_vars; - } - - // Override global query for sticky posts. - 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 ); - } - } - - // Override offset. - if ( isset( $block->context['query']['perPage'] ) ) { - $query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset']; - } - - // Override posts_per_page. - if ( isset( $block->context['query']['perPage'] ) ) { - $query['posts_per_page'] = $block->context['query']['perPage']; - } - } } $posts = get_posts( $query ); From 17c91507d77b60391cc60daa2d5aed2f7832cd6f Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 Nov 2020 13:25:01 +0200 Subject: [PATCH 04/17] Maybe just toggle the labels? :thinking: --- .../query/edit/query-inspector-controls.js | 115 +++++++++--------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 03943c5cea0f82..98201777c9c17c 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -146,25 +146,23 @@ export default function QueryInspectorControls( { query, setQuery } ) { setQuery( { customQuery: !! value } ) } /> - { customQuery && ( - - ) } - { customQuery && ( - - setQuery( { order: value } ) - } - onOrderByChange={ ( value ) => - setQuery( { orderBy: value } ) - } - /> - ) } + + setQuery( { order: value } ) } + onOrderByChange={ ( value ) => + setQuery( { orderBy: value } ) + } + /> { showSticky && ( ) } - { customQuery && ( - - { showCategories && categories?.terms?.length > 0 && ( - ( { - id: categoryId, - value: - categories.mapById[ categoryId ].name, - } ) - ) } - suggestions={ categories.names } - onChange={ onCategoriesChange } - /> - ) } - { showTags && tags?.terms?.length > 0 && ( - ( { - id: tagId, - value: tags.mapById[ tagId ].name, - } ) ) } - suggestions={ tags.names } - onChange={ onTagsChange } - /> - ) } - - setQuery( { - author: value !== '' ? +value : undefined, + + { showCategories && categories?.terms?.length > 0 && ( + ( { + id: categoryId, + value: categories.mapById[ categoryId ].name, } ) - } + ) } + suggestions={ categories.names } + onChange={ onCategoriesChange } /> - 0 && ( + ( { + id: tagId, + value: tags.mapById[ tagId ].name, + } ) ) } + suggestions={ tags.names } + onChange={ onTagsChange } /> - - ) } + ) } + + setQuery( { + author: value !== '' ? +value : undefined, + } ) + } + /> + + ); } From 8ccec48a00fbd259c5db0756559c995075436c19 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 Nov 2020 16:52:39 +0200 Subject: [PATCH 05/17] Change to ToggleControl --- .../block-library/src/query/edit/query-inspector-controls.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 98201777c9c17c..3ecdeca3416cb7 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -13,7 +13,7 @@ import { TextControl, FormTokenField, SelectControl, - CheckboxControl, + ToggleControl, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; @@ -136,7 +136,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { return ( - Date: Fri, 20 Nov 2020 17:12:42 +0200 Subject: [PATCH 06/17] reverse logic --- .../block-library/src/query-loop/index.php | 2 +- .../src/query/edit/query-inspector-controls.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 1618e1ff53c049..61f64671109caa 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -26,7 +26,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { 'post__not_in' => array(), ); - $use_global_query = ( isset( $block->context['query']['customQuery'] ) && false === $block->context['query']['customQuery'] ); + $use_global_query = ( isset( $block->context['query']['useGlobalQuery'] ) && $block->context['query']['useGlobalQuery'] ); // Override the custom query with the global query if needed. if ( $use_global_query ) { diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 3ecdeca3416cb7..c7c74f614335d3 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -39,7 +39,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { author: selectedAuthorId, postType, sticky, - customQuery, + useGlobalQuery, } = query; const [ showCategories, setShowCategories ] = useState( true ); const [ showTags, setShowTags ] = useState( true ); @@ -137,22 +137,22 @@ export default function QueryInspectorControls( { query, setQuery } ) { - setQuery( { customQuery: !! value } ) + setQuery( { useGlobalQuery: !! value } ) } /> @@ -174,7 +174,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { { showCategories && categories?.terms?.length > 0 && ( From 8a3c9e826aeca7d47198193bebc4dcbfac394818 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 Nov 2020 17:16:07 +0200 Subject: [PATCH 07/17] combine conditions --- .../block-library/src/query-loop/index.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 61f64671109caa..0aed656caa95de 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -37,8 +37,28 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } if ( isset( $block->context['query'] ) ) { - if ( ! $use_global_query && isset( $block->context['query']['postType'] ) ) { - $query['post_type'] = $block->context['query']['postType']; + if ( ! $use_global_query ) { + if ( isset( $block->context['query']['postType'] ) ) { + $query['post_type'] = $block->context['query']['postType']; + } + 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']; + } } if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { $sticky = get_option( 'sticky_posts' ); @@ -52,29 +72,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $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']; - } - if ( ! $use_global_query && isset( $block->context['query']['categoryIds'] ) ) { - $query['category__in'] = $block->context['query']['categoryIds']; - } - if ( ! $use_global_query && isset( $block->context['query']['tagIds'] ) ) { - $query['tag__in'] = $block->context['query']['tagIds']; - } - if ( ! $use_global_query && isset( $block->context['query']['order'] ) ) { - $query['order'] = strtoupper( $block->context['query']['order'] ); - } - if ( ! $use_global_query && isset( $block->context['query']['orderBy'] ) ) { - $query['orderby'] = $block->context['query']['orderBy']; - } - 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 ( ! $use_global_query && isset( $block->context['query']['author'] ) ) { - $query['author'] = $block->context['query']['author']; - } - if ( ! $use_global_query && isset( $block->context['query']['search'] ) ) { - $query['s'] = $block->context['query']['search']; - } } $posts = get_posts( $query ); From 20e23ac63a98a32e769adf09cf8e9f23baef81ea Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 23 Nov 2020 17:09:34 +0200 Subject: [PATCH 08/17] Hide controls when using global query --- .../query/edit/query-inspector-controls.js | 112 +++++++++--------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 65979f12650d38..5c9c7b54216582 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -152,16 +152,14 @@ export default function QueryInspectorControls( { setQuery( { useGlobalQuery: !! value } ) } /> - + { ! useGlobalQuery && ( + + ) } { layout?.type === 'flex' && ( <> ) } - setQuery( { order: value } ) } - onOrderByChange={ ( value ) => - setQuery( { orderBy: value } ) - } - /> + { ! useGlobalQuery && ( + setQuery( { order: value } ) } + onOrderByChange={ ( value ) => + setQuery( { orderBy: value } ) + } + /> + ) } { showSticky && ( ) } - - { showCategories && categories?.terms?.length > 0 && ( - ( { - id: categoryId, - value: categories.mapById[ categoryId ].name, + { ! useGlobalQuery && ( + + { showCategories && categories?.terms?.length > 0 && ( + ( { + id: categoryId, + value: categories.mapById[ categoryId ].name, + } ) + ) } + suggestions={ categories.names } + onChange={ onCategoriesChange } + /> + ) } + { showTags && tags?.terms?.length > 0 && ( + ( { + id: tagId, + value: tags.mapById[ tagId ].name, + } ) ) } + suggestions={ tags.names } + onChange={ onTagsChange } + /> + ) } + + setQuery( { + author: value !== '' ? +value : undefined, } ) - ) } - suggestions={ categories.names } - onChange={ onCategoriesChange } + } /> - ) } - { showTags && tags?.terms?.length > 0 && ( - ( { - id: tagId, - value: tags.mapById[ tagId ].name, - } ) ) } - suggestions={ tags.names } - onChange={ onTagsChange } + - ) } - - setQuery( { - author: value !== '' ? +value : undefined, - } ) - } - /> - - + + ) } ); } From 406442d40ed32b725ae53a8c93c2315e88fcb19e Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 23 Nov 2020 17:13:56 +0200 Subject: [PATCH 09/17] Inherit query from template --- packages/block-library/src/query-loop/edit.js | 10 ++++++++ .../block-library/src/query-loop/utils.js | 23 +++++++++++++++++++ .../query/edit/query-inspector-controls.js | 7 ++++-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 packages/block-library/src/query-loop/utils.js diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index a32ffd67410507..9cc61fc6f73845 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -20,6 +20,7 @@ import { * Internal dependencies */ import { useQueryContext } from '../query'; +import { getTemplateQuery } from './utils'; const TEMPLATE = [ [ 'core/post-title' ], @@ -41,6 +42,7 @@ export default function QueryLoopEdit( { search, exclude, sticky, + useGlobalQuery, } = {}, queryContext, layout: { type: layoutType = 'flex', columns = 1 } = {}, @@ -78,6 +80,14 @@ export default function QueryLoopEdit( { if ( sticky ) { query.sticky = sticky === 'only'; } + + if ( useGlobalQuery ) { + const templateQuery = getTemplateQuery(); + Object.keys( templateQuery ).forEach( ( key ) => { + query[ key ] = templateQuery[ key ]; + } ); + } + return { posts: getEntityRecords( 'postType', postType, query ), blocks: getBlocks( clientId ), diff --git a/packages/block-library/src/query-loop/utils.js b/packages/block-library/src/query-loop/utils.js new file mode 100644 index 00000000000000..1982ab68bb162b --- /dev/null +++ b/packages/block-library/src/query-loop/utils.js @@ -0,0 +1,23 @@ +/* global wp */ +/** + * Returns arguments inherited from the template-type. + */ +export const getTemplateQuery = () => { + const templateType = wp.data.select( 'core/edit-site' ).getTemplateType(); + if ( 'wp_template' === templateType ) { + const templateId = wp.data.select( 'core/edit-site' ).getTemplateId(); + const template = wp.data + .select( 'core' ) + .getEntityRecord( 'postType', 'wp_template', templateId ); + + // Change the post-type if needed. + if ( + 'attachment' === template.slug || + 0 === template.slug.indexOf( 'archive-' ) + ) { + return { postType: template.slug.replace( 'archive-', '' ) }; + } + } + + return {}; +}; diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 5c9c7b54216582..a3875a80947130 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -183,7 +183,9 @@ export default function QueryInspectorControls( { { ! useGlobalQuery && ( setQuery( { order: value } ) } + onOrderChange={ ( value ) => + setQuery( { order: value } ) + } onOrderByChange={ ( value ) => setQuery( { orderBy: value } ) } @@ -206,7 +208,8 @@ export default function QueryInspectorControls( { value={ ( query.categoryIds || [] ).map( ( categoryId ) => ( { id: categoryId, - value: categories.mapById[ categoryId ].name, + value: + categories.mapById[ categoryId ].name, } ) ) } suggestions={ categories.names } From fd700670166144c0005ae5ebae6e2837ae6b8946 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 23 Nov 2020 17:27:08 +0200 Subject: [PATCH 10/17] attachment is not an archive --- packages/block-library/src/query-loop/utils.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/block-library/src/query-loop/utils.js b/packages/block-library/src/query-loop/utils.js index 1982ab68bb162b..6ecaecc1d25c46 100644 --- a/packages/block-library/src/query-loop/utils.js +++ b/packages/block-library/src/query-loop/utils.js @@ -11,10 +11,7 @@ export const getTemplateQuery = () => { .getEntityRecord( 'postType', 'wp_template', templateId ); // Change the post-type if needed. - if ( - 'attachment' === template.slug || - 0 === template.slug.indexOf( 'archive-' ) - ) { + if ( 0 === template.slug.indexOf( 'archive-' ) ) { return { postType: template.slug.replace( 'archive-', '' ) }; } } From 7ca7b4a7ebde68a84ae18c8c1639f22ed137f817 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 24 Nov 2020 15:28:25 +0200 Subject: [PATCH 11/17] Simplify implementation - minimal changes compared to master --- .../block-library/src/query-loop/index.php | 61 +++++++++---------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index df336fcc4ecd2d..45bba97f0994a9 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -26,39 +26,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) { 'post__not_in' => array(), ); - $use_global_query = ( isset( $block->context['query']['useGlobalQuery'] ) && $block->context['query']['useGlobalQuery'] ); - - // Override the custom query with the global query if needed. - if ( $use_global_query ) { - global $wp_query; - if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { - $query = $wp_query->query_vars; - } - } - if ( isset( $block->context['query'] ) ) { - if ( ! $use_global_query ) { - if ( isset( $block->context['query']['postType'] ) ) { - $query['post_type'] = $block->context['query']['postType']; - } - 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']; - } + 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' ); @@ -75,6 +45,33 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $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']; + } + } + + // Override the custom query with the global query if needed. + $use_global_query = ( isset( $block->context['query']['useGlobalQuery'] ) && $block->context['query']['useGlobalQuery'] ); + if ( $use_global_query ) { + global $wp_query; + if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { + $query = wp_parse_args( $wp_query->query_vars, $query ); + } } $posts = get_posts( $query ); From 5b4fc7b0e5e93bd0e29391450e4c733294eaebef Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 24 Nov 2020 16:17:03 +0200 Subject: [PATCH 12/17] Rename useGlobalQuery to isGlobalQuery --- packages/block-library/src/query-loop/edit.js | 4 ++-- packages/block-library/src/query-loop/index.php | 2 +- .../src/query/edit/query-inspector-controls.js | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index 9cc61fc6f73845..a38f74a3a13043 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -42,7 +42,7 @@ export default function QueryLoopEdit( { search, exclude, sticky, - useGlobalQuery, + isGlobalQuery, } = {}, queryContext, layout: { type: layoutType = 'flex', columns = 1 } = {}, @@ -81,7 +81,7 @@ export default function QueryLoopEdit( { query.sticky = sticky === 'only'; } - if ( useGlobalQuery ) { + if ( isGlobalQuery ) { const templateQuery = getTemplateQuery(); Object.keys( templateQuery ).forEach( ( key ) => { query[ key ] = templateQuery[ key ]; diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 45bba97f0994a9..21361fd4dbf321 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -66,7 +66,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } // Override the custom query with the global query if needed. - $use_global_query = ( isset( $block->context['query']['useGlobalQuery'] ) && $block->context['query']['useGlobalQuery'] ); + $use_global_query = ( isset( $block->context['query']['isGlobalQuery'] ) && $block->context['query']['isGlobalQuery'] ); if ( $use_global_query ) { global $wp_query; if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index a3875a80947130..b0baed2ce46f7a 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -45,7 +45,7 @@ export default function QueryInspectorControls( { author: selectedAuthorId, postType, sticky, - useGlobalQuery, + isGlobalQuery, } = query; const [ showCategories, setShowCategories ] = useState( true ); const [ showTags, setShowTags ] = useState( true ); @@ -147,12 +147,12 @@ export default function QueryInspectorControls( { help={ __( 'Disable the option to customize the query arguments. Leave enabled to inherit the global query depending on the URL.' ) } - checked={ !! useGlobalQuery } + checked={ !! isGlobalQuery } onChange={ ( value ) => - setQuery( { useGlobalQuery: !! value } ) + setQuery( { isGlobalQuery: !! value } ) } /> - { ! useGlobalQuery && ( + { ! isGlobalQuery && ( ) } - { ! useGlobalQuery && ( + { ! isGlobalQuery && ( @@ -200,7 +200,7 @@ export default function QueryInspectorControls( { /> ) } - { ! useGlobalQuery && ( + { ! isGlobalQuery && ( { showCategories && categories?.terms?.length > 0 && ( Date: Tue, 24 Nov 2020 18:45:12 +0100 Subject: [PATCH 13/17] Refactor code that updates the global query --- packages/block-library/src/query-loop/edit.js | 27 ++++++++++++++----- .../block-library/src/query-loop/utils.js | 20 -------------- packages/block-library/src/query/block.json | 3 ++- 3 files changed, 23 insertions(+), 27 deletions(-) delete mode 100644 packages/block-library/src/query-loop/utils.js diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index a38f74a3a13043..4e5db5a1a75b82 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -20,7 +20,6 @@ import { * Internal dependencies */ import { useQueryContext } from '../query'; -import { getTemplateQuery } from './utils'; const TEMPLATE = [ [ 'core/post-title' ], @@ -81,11 +80,26 @@ export default function QueryLoopEdit( { query.sticky = sticky === 'only'; } - if ( isGlobalQuery ) { - const templateQuery = getTemplateQuery(); - Object.keys( templateQuery ).forEach( ( key ) => { - query[ key ] = templateQuery[ key ]; - } ); + // When you insert this block outside of the edit site then store + // does not exist therefore we check for its existence. + if ( isGlobalQuery && select( 'core/edit-site' ) ) { + // This should be passed from the context exposed by edit site. + const { getTemplateId, getTemplateType } = select( + 'core/edit-site' + ); + + if ( 'wp_template' === getTemplateType() ) { + const { slug } = select( 'core' ).getEntityRecord( + 'postType', + 'wp_template', + getTemplateId() + ); + + // Change the post-type if needed. + if ( slug?.startsWith( 'archive-' ) ) { + query.postType = slug.replace( 'archive-', '' ); + } + } } return { @@ -107,6 +121,7 @@ export default function QueryLoopEdit( { postType, exclude, sticky, + isGlobalQuery, ] ); diff --git a/packages/block-library/src/query-loop/utils.js b/packages/block-library/src/query-loop/utils.js deleted file mode 100644 index 6ecaecc1d25c46..00000000000000 --- a/packages/block-library/src/query-loop/utils.js +++ /dev/null @@ -1,20 +0,0 @@ -/* global wp */ -/** - * Returns arguments inherited from the template-type. - */ -export const getTemplateQuery = () => { - const templateType = wp.data.select( 'core/edit-site' ).getTemplateType(); - if ( 'wp_template' === templateType ) { - const templateId = wp.data.select( 'core/edit-site' ).getTemplateId(); - const template = wp.data - .select( 'core' ) - .getEntityRecord( 'postType', 'wp_template', templateId ); - - // Change the post-type if needed. - if ( 0 === template.slug.indexOf( 'archive-' ) ) { - return { postType: template.slug.replace( 'archive-', '' ) }; - } - } - - return {}; -}; diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index 7477e03d6f8485..b8060e95ee1dc8 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -20,7 +20,8 @@ "author": "", "search": "", "exclude": [], - "sticky": "" + "sticky": "", + "isGlobalQuery": true } }, "layout": { From f1cb044d1d5486644232427638b9bcc3528605b6 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 26 Nov 2020 12:53:15 +0200 Subject: [PATCH 14/17] properly modify the postType passed-on to the query --- packages/block-library/src/query-loop/edit.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index 4e5db5a1a75b82..1b88106d9829cf 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -98,6 +98,7 @@ export default function QueryLoopEdit( { // Change the post-type if needed. if ( slug?.startsWith( 'archive-' ) ) { query.postType = slug.replace( 'archive-', '' ); + postType = query.postType; } } } From c82e06344dd29939615f078f3b3b4ea1c1b5b6df Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 8 Dec 2020 15:01:19 +0200 Subject: [PATCH 15/17] fix e2e --- packages/e2e-tests/fixtures/blocks/core__query.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/e2e-tests/fixtures/blocks/core__query.json b/packages/e2e-tests/fixtures/blocks/core__query.json index 235fcefff51e21..81d1a0e5c06b19 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query.json +++ b/packages/e2e-tests/fixtures/blocks/core__query.json @@ -5,6 +5,7 @@ "isValid": true, "attributes": { "query": { + "isGlobalQuery": true, "perPage": null, "pages": 1, "offset": 0, From 334beae688c136aeee27531552157a0ac35babe6 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 8 Dec 2020 15:35:09 +0200 Subject: [PATCH 16/17] indentation fix --- packages/e2e-tests/fixtures/blocks/core__query.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/fixtures/blocks/core__query.json b/packages/e2e-tests/fixtures/blocks/core__query.json index 81d1a0e5c06b19..823dd92269fb0f 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query.json +++ b/packages/e2e-tests/fixtures/blocks/core__query.json @@ -5,7 +5,7 @@ "isValid": true, "attributes": { "query": { - "isGlobalQuery": true, + "isGlobalQuery": true, "perPage": null, "pages": 1, "offset": 0, From ccf3513e4af3ebe3154fcc061a09b8fc9b6f5601 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 8 Dec 2020 15:57:48 +0200 Subject: [PATCH 17/17] rename isGlobalQuery to inherit --- packages/block-library/src/query-loop/edit.js | 6 +++--- packages/block-library/src/query-loop/index.php | 4 ++-- packages/block-library/src/query/block.json | 2 +- .../src/query/edit/query-inspector-controls.js | 14 ++++++-------- .../e2e-tests/fixtures/blocks/core__query.json | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index 1b88106d9829cf..4d62b24356bb59 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -41,7 +41,7 @@ export default function QueryLoopEdit( { search, exclude, sticky, - isGlobalQuery, + inherit, } = {}, queryContext, layout: { type: layoutType = 'flex', columns = 1 } = {}, @@ -82,7 +82,7 @@ export default function QueryLoopEdit( { // When you insert this block outside of the edit site then store // does not exist therefore we check for its existence. - if ( isGlobalQuery && select( 'core/edit-site' ) ) { + if ( inherit && select( 'core/edit-site' ) ) { // This should be passed from the context exposed by edit site. const { getTemplateId, getTemplateType } = select( 'core/edit-site' @@ -122,7 +122,7 @@ export default function QueryLoopEdit( { postType, exclude, sticky, - isGlobalQuery, + inherit, ] ); diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 21361fd4dbf321..f611e4fbd81d70 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -66,7 +66,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } // Override the custom query with the global query if needed. - $use_global_query = ( isset( $block->context['query']['isGlobalQuery'] ) && $block->context['query']['isGlobalQuery'] ); + $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); if ( $use_global_query ) { global $wp_query; if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { @@ -96,7 +96,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { ) ) )->render( array( 'dynamic' => false ) ); - $content .= "
  • {$block_content}
  • "; + $content .= "
  • {$block_content}
  • "; } return sprintf( '
      %2$s
    ', diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index b8060e95ee1dc8..016fa44fbcc0ec 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -21,7 +21,7 @@ "search": "", "exclude": [], "sticky": "", - "isGlobalQuery": true + "inherit": true } }, "layout": { diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 670f6cd8ab8395..06159080575829 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -45,7 +45,7 @@ export default function QueryInspectorControls( { author: selectedAuthorId, postType, sticky, - isGlobalQuery, + inherit, } = query; const [ showCategories, setShowCategories ] = useState( true ); const [ showTags, setShowTags ] = useState( true ); @@ -151,12 +151,10 @@ export default function QueryInspectorControls( { help={ __( 'Disable the option to customize the query arguments. Leave enabled to inherit the global query depending on the URL.' ) } - checked={ !! isGlobalQuery } - onChange={ ( value ) => - setQuery( { isGlobalQuery: !! value } ) - } + checked={ !! inherit } + onChange={ ( value ) => setQuery( { inherit: !! value } ) } /> - { ! isGlobalQuery && ( + { ! inherit && ( ) } - { ! isGlobalQuery && ( + { ! inherit && ( @@ -204,7 +202,7 @@ export default function QueryInspectorControls( { /> ) }
    - { ! isGlobalQuery && ( + { ! inherit && ( { showCategories && categories?.terms?.length > 0 && (