From 84325234ca45c2f79c3b9e194dd4442b1a16455e Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 8 Sep 2020 16:10:50 +0300 Subject: [PATCH 1/3] add filter by author support in Query block --- packages/block-library/src/query-loop/edit.js | 16 +++++++++++++++- .../block-library/src/query-loop/index.php | 3 +++ packages/block-library/src/query/block.json | 3 ++- .../src/query/edit/query-inspector-controls.js | 18 +++++++++++++++--- .../e2e-tests/fixtures/blocks/core__query.json | 3 ++- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index 06a7949a87e4db..d6d22c53658214 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -26,6 +26,7 @@ export default function QueryLoopEdit( { tagIds = [], order, orderBy, + author, } = {}, queryContext, }, @@ -45,6 +46,9 @@ export default function QueryLoopEdit( { if ( perPage ) { query.per_page = perPage; } + if ( author ) { + query.author = author; + } return { posts: select( 'core' ).getEntityRecords( 'postType', @@ -54,7 +58,17 @@ export default function QueryLoopEdit( { blocks: select( 'core/block-editor' ).getBlocks( clientId ), }; }, - [ perPage, page, offset, categoryIds, tagIds, order, orderBy, clientId ] + [ + perPage, + page, + offset, + categoryIds, + tagIds, + order, + orderBy, + clientId, + author, + ] ); const blockContexts = useMemo( diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index 45c1622627e290..78660188163c28 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -44,6 +44,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) { if ( isset( $block->context['query']['perPage'] ) ) { $query['posts_per_page'] = $block->context['query']['perPage']; } + if ( isset( $block->context['query']['author'] ) ) { + $query['author'] = $block->context['query']['author']; + } } $posts = get_posts( $query ); diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index a4d39f0c7b3dda..ece5a390a5f0fd 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -14,7 +14,8 @@ "categoryIds": [], "tagIds": [], "order": "desc", - "orderBy": "date" + "orderBy": "date", + "author": null } } }, 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 50e7a38ea73591..1b60ade9235113 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -4,18 +4,30 @@ import { PanelBody, QueryControls } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; export default function QueryInspectorControls( { query, setQuery } ) { - const { order, orderBy } = query; + const { order, orderBy, author: selectedAuthorId } = query; + const { authorList } = useSelect( ( select ) => { + const { getEntityRecords } = select( 'core' ); + return { + authorList: getEntityRecords( 'root', 'user', { per_page: -1 } ), + }; + } ); return ( - + setQuery( { order: value } ) } onOrderByChange={ ( value ) => setQuery( { orderBy: value } ) } + onAuthorChange={ ( value ) => + setQuery( { + author: value !== '' ? +value : undefined, + } ) + } /> diff --git a/packages/e2e-tests/fixtures/blocks/core__query.json b/packages/e2e-tests/fixtures/blocks/core__query.json index e06e002a5a3141..d4882792623121 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query.json +++ b/packages/e2e-tests/fixtures/blocks/core__query.json @@ -11,7 +11,8 @@ "categoryIds": [], "tagIds": [], "order": "desc", - "orderBy": "date" + "orderBy": "date", + "author": null } }, "innerBlocks": [], From 3835538444e01491a7b3088465251cc459b60ef4 Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Tue, 8 Sep 2020 17:44:52 +0300 Subject: [PATCH 2/3] provide useSelect dependencies Co-authored-by: Miguel Fonseca --- .../block-library/src/query/edit/query-inspector-controls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1b60ade9235113..93167f84e10d34 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 @@ export default function QueryInspectorControls( { query, setQuery } ) { return { authorList: getEntityRecords( 'root', 'user', { per_page: -1 } ), }; - } ); + }, [] ); return ( From 26f4c2c2457842b06f4492b45fb5c3b8c76538d9 Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Tue, 8 Sep 2020 17:45:24 +0300 Subject: [PATCH 3/3] Update packages/block-library/src/query/edit/query-inspector-controls.js change description Co-authored-by: Miguel Fonseca --- .../block-library/src/query/edit/query-inspector-controls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 93167f84e10d34..ec6a28d6721897 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -16,7 +16,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { }, [] ); return ( - + setQuery( { order: value } ) }