From f06dc4954e44efc9b9894f0ffd96f3e42a4f88c0 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 20 Aug 2020 16:30:39 +0300 Subject: [PATCH 1/5] add basic order support in Query block --- packages/block-library/src/query-loop/edit.js | 8 ++++++-- packages/block-library/src/query-loop/index.php | 9 ++++++--- packages/block-library/src/query/block.json | 3 ++- .../block-library/src/query/edit/query-toolbar.js | 13 +++++++++++++ packages/e2e-tests/fixtures/blocks/core__query.json | 3 ++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index 905e92012255e..593cb248c1a21 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -18,7 +18,10 @@ import { useQueryContext } from '../query'; const TEMPLATE = [ [ 'core/post-title' ], [ 'core/post-content' ] ]; export default function QueryLoopEdit( { clientId, - context: { query: { perPage, offset, categoryIds } = {}, queryContext }, + context: { + query: { perPage, offset, categoryIds, order } = {}, + queryContext, + }, } ) { const [ { page } ] = useQueryContext() || queryContext || [ {} ]; const [ activeBlockContext, setActiveBlockContext ] = useState(); @@ -28,6 +31,7 @@ export default function QueryLoopEdit( { const query = { offset: perPage ? perPage * ( page - 1 ) + offset : 0, categories: categoryIds, + order, }; if ( perPage ) { query.per_page = perPage; @@ -41,7 +45,7 @@ export default function QueryLoopEdit( { blocks: select( 'core/block-editor' ).getBlocks( clientId ), }; }, - [ perPage, page, offset, categoryIds, clientId ] + [ perPage, page, offset, categoryIds, order, clientId ] ); const blockContexts = useMemo( diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index b838d1024804c..ad2c3a5f554f0 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -19,10 +19,13 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); $query = array( - 'post_type' => 'post', - 'offset' => isset( $block->context['query']['perPage'] ) ? ( $block->context['query']['perPage'] * ( $page - 1 ) + $block->context['query']['offset'] ) : 0, - 'category__in' => $block->context['query']['categoryIds'], + 'post_type' => 'post', + 'offset' => isset( $block->context['query']['perPage'] ) ? ( $block->context['query']['perPage'] * ( $page - 1 ) + $block->context['query']['offset'] ) : 0, + 'order' => isset( $block->context['query']['order'] ) ? strtoupper( $block->context['query']['order'] ) : 'DESC', ); + if ( isset( $block->context['query']['categoryIds'] ) ) { + $query['category__in'] = $block->context['query']['categoryIds']; + } if ( isset( $block->context['query']['perPage'] ) ) { $query['posts_per_page'] = $block->context['query']['perPage']; } diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index f7d998c0ffcad..36e4875e3e9d7 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -11,7 +11,8 @@ "perPage": 3, "pages": 1, "offset": 0, - "categoryIds": [] + "categoryIds": [], + "order": "desc" } } }, diff --git a/packages/block-library/src/query/edit/query-toolbar.js b/packages/block-library/src/query/edit/query-toolbar.js index b80e9a4f8a29c..3916624922a53 100644 --- a/packages/block-library/src/query/edit/query-toolbar.js +++ b/packages/block-library/src/query/edit/query-toolbar.js @@ -7,6 +7,7 @@ import { Dropdown, ToolbarButton, RangeControl, + SelectControl, FormTokenField, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; @@ -38,6 +39,10 @@ export default function QueryToolbar( { query, setQuery } ) { }, [] ); + const orderOptions = [ + { value: 'desc', label: __( 'Descending' ) }, + { value: 'asc', label: __( 'Ascending' ) }, + ]; return ( + + setQuery( { order: value } ) + } + /> { categories && ( Date: Thu, 20 Aug 2020 17:07:17 +0300 Subject: [PATCH 2/5] move sort to block inspector --- .../block-library/src/query/edit/index.js | 11 ++++----- .../query/edit/query-inspector-controls.js | 23 +++++++++++++++++++ .../src/query/edit/query-toolbar.js | 13 ----------- 3 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 packages/block-library/src/query/edit/query-inspector-controls.js diff --git a/packages/block-library/src/query/edit/index.js b/packages/block-library/src/query/edit/index.js index 6bb8b053352b4..d1ced87d63891 100644 --- a/packages/block-library/src/query/edit/index.js +++ b/packages/block-library/src/query/edit/index.js @@ -14,6 +14,7 @@ import { */ import QueryToolbar from './query-toolbar'; import QueryProvider from './query-provider'; +import QueryInspectorControls from './query-inspector-controls'; const TEMPLATE = [ [ 'core/query-loop' ], [ 'core/query-pagination' ] ]; export default function QueryEdit( { @@ -28,15 +29,13 @@ export default function QueryEdit( { setAttributes( { queryId: instanceId } ); } }, [ queryId, instanceId ] ); + const updateQuery = ( newQuery ) => + setAttributes( { query: { ...query, ...newQuery } } ); return ( <> + - - setAttributes( { query: { ...query, ...newQuery } } ) - } - /> + diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js new file mode 100644 index 0000000000000..50e7a38ea7359 --- /dev/null +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -0,0 +1,23 @@ +/** + * WordPress dependencies + */ +import { PanelBody, QueryControls } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { InspectorControls } from '@wordpress/block-editor'; + +export default function QueryInspectorControls( { query, setQuery } ) { + const { order, orderBy } = query; + return ( + + + setQuery( { order: value } ) } + onOrderByChange={ ( value ) => + setQuery( { orderBy: value } ) + } + /> + + + ); +} diff --git a/packages/block-library/src/query/edit/query-toolbar.js b/packages/block-library/src/query/edit/query-toolbar.js index 3916624922a53..b80e9a4f8a29c 100644 --- a/packages/block-library/src/query/edit/query-toolbar.js +++ b/packages/block-library/src/query/edit/query-toolbar.js @@ -7,7 +7,6 @@ import { Dropdown, ToolbarButton, RangeControl, - SelectControl, FormTokenField, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; @@ -39,10 +38,6 @@ export default function QueryToolbar( { query, setQuery } ) { }, [] ); - const orderOptions = [ - { value: 'desc', label: __( 'Descending' ) }, - { value: 'asc', label: __( 'Ascending' ) }, - ]; return ( - - setQuery( { order: value } ) - } - /> { categories && ( Date: Thu, 20 Aug 2020 17:13:58 +0300 Subject: [PATCH 3/5] add orderby param in php --- .../block-library/src/query-loop/index.php | 1 + packages/block-library/src/query/block.json | 3 +- .../fixtures/blocks/core__query.json | 31 ++++++++++--------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index ad2c3a5f554f0..b7235223f2111 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -22,6 +22,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { 'post_type' => 'post', 'offset' => isset( $block->context['query']['perPage'] ) ? ( $block->context['query']['perPage'] * ( $page - 1 ) + $block->context['query']['offset'] ) : 0, 'order' => isset( $block->context['query']['order'] ) ? strtoupper( $block->context['query']['order'] ) : 'DESC', + 'orderby' => isset( $block->context['query']['orderBy'] ) ? $block->context['query']['orderBy'] : 'date', ); if ( isset( $block->context['query']['categoryIds'] ) ) { $query['category__in'] = $block->context['query']['categoryIds']; diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index 36e4875e3e9d7..2561ccbe1f844 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -12,7 +12,8 @@ "pages": 1, "offset": 0, "categoryIds": [], - "order": "desc" + "order": "desc", + "orderBy": "date" } } }, diff --git a/packages/e2e-tests/fixtures/blocks/core__query.json b/packages/e2e-tests/fixtures/blocks/core__query.json index 1dc4508540dbb..9e2d6307436c1 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query.json +++ b/packages/e2e-tests/fixtures/blocks/core__query.json @@ -1,18 +1,19 @@ [ - { - "clientId": "_clientId_0", - "name": "core/query", - "isValid": true, - "attributes": { - "query": { - "perPage": 3, - "pages": 1, - "offset": 0, + { + "clientId": "_clientId_0", + "name": "core/query", + "isValid": true, + "attributes": { + "query": { + "perPage": 3, + "pages": 1, + "offset": 0, "categoryIds": [], - "order": "desc" - } - }, - "innerBlocks": [], - "originalContent": "" - } + "order": "desc", + "orderBy": "date" + } + }, + "innerBlocks": [], + "originalContent": "" + } ] From c0f5dca887ecf4a99e7d62222a404872b5ec6309 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 20 Aug 2020 19:41:20 +0300 Subject: [PATCH 4/5] add orderBy in query loop --- packages/block-library/src/query-loop/edit.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index 593cb248c1a21..4f993327f191f 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -19,7 +19,7 @@ const TEMPLATE = [ [ 'core/post-title' ], [ 'core/post-content' ] ]; export default function QueryLoopEdit( { clientId, context: { - query: { perPage, offset, categoryIds, order } = {}, + query: { perPage, offset, categoryIds, order, orderBy } = {}, queryContext, }, } ) { @@ -32,6 +32,7 @@ export default function QueryLoopEdit( { offset: perPage ? perPage * ( page - 1 ) + offset : 0, categories: categoryIds, order, + orderby: orderBy, }; if ( perPage ) { query.per_page = perPage; @@ -45,7 +46,7 @@ export default function QueryLoopEdit( { blocks: select( 'core/block-editor' ).getBlocks( clientId ), }; }, - [ perPage, page, offset, categoryIds, order, clientId ] + [ perPage, page, offset, categoryIds, order, orderBy, clientId ] ); const blockContexts = useMemo( From 3fc613ac903b889153bb300a491e5aed7abd1e34 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 20 Aug 2020 19:44:31 +0300 Subject: [PATCH 5/5] lint php --- packages/block-library/src/query-loop/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index b7235223f2111..be6aa0c6a34ca 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -20,9 +20,9 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $query = array( 'post_type' => 'post', - 'offset' => isset( $block->context['query']['perPage'] ) ? ( $block->context['query']['perPage'] * ( $page - 1 ) + $block->context['query']['offset'] ) : 0, - 'order' => isset( $block->context['query']['order'] ) ? strtoupper( $block->context['query']['order'] ) : 'DESC', - 'orderby' => isset( $block->context['query']['orderBy'] ) ? $block->context['query']['orderBy'] : 'date', + 'offset' => isset( $block->context['query']['perPage'] ) ? ( $block->context['query']['perPage'] * ( $page - 1 ) + $block->context['query']['offset'] ) : 0, + 'order' => isset( $block->context['query']['order'] ) ? strtoupper( $block->context['query']['order'] ) : 'DESC', + 'orderby' => isset( $block->context['query']['orderBy'] ) ? $block->context['query']['orderBy'] : 'date', ); if ( isset( $block->context['query']['categoryIds'] ) ) { $query['category__in'] = $block->context['query']['categoryIds'];