From 6af02a96e9dfbb39bed6e8a9f2d711b1ffc331e4 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 25 Mar 2025 14:49:56 +0400 Subject: [PATCH 1/3] Query Loop: Don't overwrite the 'query.inherit' attribute value --- .../src/query/edit/query-content.js | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 55a2b24cfd1a51..6ef24a2b3b1cd2 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -7,11 +7,12 @@ import { useEffect, useCallback } from '@wordpress/element'; import { BlockControls, InspectorControls, + Warning, useBlockProps, store as blockEditorStore, useInnerBlocksProps, } from '@wordpress/block-editor'; -import { SelectControl } from '@wordpress/components'; +import { Button, SelectControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { store as coreStore } from '@wordpress/core-data'; @@ -101,21 +102,15 @@ export default function QueryContent( { } else if ( ! query.perPage && postsPerPage ) { newQuery.perPage = postsPerPage; } - // We need to reset the `inherit` value if in a singular template, as queries - // are not inherited when in singular content (e.g. post, page, 404, blank). - if ( isSingular && query.inherit ) { - newQuery.inherit = false; - } + if ( !! Object.keys( newQuery ).length ) { __unstableMarkNextChangeAsNotPersistent(); updateQuery( newQuery ); } }, [ query.perPage, - query.inherit, - postsPerPage, inherit, - isSingular, + postsPerPage, __unstableMarkNextChangeAsNotPersistent, updateQuery, ] ); @@ -137,6 +132,8 @@ export default function QueryContent( { displayLayout: { ...displayLayout, ...newDisplayLayout }, } ); + const hasInheritanceWarning = isSingular && inherit; + return ( <> - + { hasInheritanceWarning ? ( +
+ + updateQuery( { inherit: false } ) + } + variant="primary" + > + { __( 'Switch to Custom' ) } + , + ] } + > + { __( + 'Cannot inherit the global queries when placed inside the singular content' + ) } + +
+ ) : ( + + ) } ); } From 7a8f632394f0bab3ee97c1d2e769682f0792e330 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 1 Apr 2025 14:29:18 +0400 Subject: [PATCH 2/3] A misc adjustments --- packages/block-library/src/query/edit/query-content.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 6ef24a2b3b1cd2..1cec9245f054b9 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -132,6 +132,8 @@ export default function QueryContent( { displayLayout: { ...displayLayout, ...newDisplayLayout }, } ); + // The block cannot inherit a default WordPress query in singular content (e.g., post, page, 404, blank). + // Warn users but still permit this type of query for exceptional cases in Classic and Hybrid themes. const hasInheritanceWarning = isSingular && inherit; return ( @@ -195,7 +197,7 @@ export default function QueryContent( { ] } > { __( - 'Cannot inherit the global queries when placed inside the singular content' + 'Cannot inherit the default WordPress query when placed inside the singular content (e.g., post, page, 404, blank).' ) } From 45310be801bab56f03107c16d576d572c93e9326 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 4 Apr 2025 15:34:58 +0400 Subject: [PATCH 3/3] Display warning next to 'Query Type' control --- .../query/edit/inspector-controls/index.js | 84 +++++++++++-------- .../src/query/edit/query-content.js | 32 +------ 2 files changed, 52 insertions(+), 64 deletions(-) diff --git a/packages/block-library/src/query/edit/inspector-controls/index.js b/packages/block-library/src/query/edit/inspector-controls/index.js index cf0b80b6145632..66ba962a074608 100644 --- a/packages/block-library/src/query/edit/inspector-controls/index.js +++ b/packages/block-library/src/query/edit/inspector-controls/index.js @@ -5,11 +5,12 @@ import { TextControl, SelectControl, RangeControl, - __experimentalToggleGroupControl as ToggleGroupControl, - __experimentalToggleGroupControlOption as ToggleGroupControlOption, Notice, + __experimentalVStack as VStack, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, + __experimentalToggleGroupControl as ToggleGroupControl, + __experimentalToggleGroupControlOption as ToggleGroupControlOption, } from '@wordpress/components'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; @@ -113,8 +114,7 @@ export default function QueryInspectorControls( props ) { }, [ querySearch, onChangeDebounced ] ); const orderByOptions = useOrderByOptions( postType ); - const showInheritControl = - ! isSingular && isControlAllowed( allowedControls, 'inherit' ); + const showInheritControl = isControlAllowed( allowedControls, 'inherit' ); const showPostTypeControl = ! inherit && isControlAllowed( allowedControls, 'postType' ); const postTypeControlLabel = __( 'Post type' ); @@ -185,6 +185,10 @@ export default function QueryInspectorControls( props ) { const showDisplayPanel = showPostCountControl || showOffSetControl || showPagesControl; + // The block cannot inherit a default WordPress query in singular content (e.g., post, page, 404, blank). + // Warn users but still permit this type of query for exceptional cases in Classic and Hybrid themes. + const hasInheritanceWarning = isSingular && inherit; + return ( <> { showSettingsPanel && ( @@ -208,36 +212,48 @@ export default function QueryInspectorControls( props ) { onDeselect={ () => setQuery( { inherit: true } ) } isShownByDefault > - { - setQuery( { - inherit: value === 'default', - } ); - } } - help={ - inherit - ? __( - 'Display a list of posts or custom post types based on the current template.' - ) - : __( - 'Display a list of posts or custom post types based on specific criteria.' - ) - } - value={ !! inherit ? 'default' : 'custom' } - > - - - + + { + setQuery( { + inherit: value === 'default', + } ); + } } + help={ + inherit + ? __( + 'Display a list of posts or custom post types based on the current template.' + ) + : __( + 'Display a list of posts or custom post types based on specific criteria.' + ) + } + value={ !! inherit ? 'default' : 'custom' } + > + + + + { hasInheritanceWarning && ( + + { __( + 'Cannot inherit the current template query when placed inside the singular content (e.g., post, page, 404, blank).' + ) } + + ) } + ) } diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 1cec9245f054b9..dc4edd16d849ee 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -7,12 +7,11 @@ import { useEffect, useCallback } from '@wordpress/element'; import { BlockControls, InspectorControls, - Warning, useBlockProps, store as blockEditorStore, useInnerBlocksProps, } from '@wordpress/block-editor'; -import { Button, SelectControl } from '@wordpress/components'; +import { SelectControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { store as coreStore } from '@wordpress/core-data'; @@ -132,10 +131,6 @@ export default function QueryContent( { displayLayout: { ...displayLayout, ...newDisplayLayout }, } ); - // The block cannot inherit a default WordPress query in singular content (e.g., post, page, 404, blank). - // Warn users but still permit this type of query for exceptional cases in Classic and Hybrid themes. - const hasInheritanceWarning = isSingular && inherit; - return ( <> - { hasInheritanceWarning ? ( -
- - updateQuery( { inherit: false } ) - } - variant="primary" - > - { __( 'Switch to Custom' ) } - , - ] } - > - { __( - 'Cannot inherit the default WordPress query when placed inside the singular content (e.g., post, page, 404, blank).' - ) } - -
- ) : ( - - ) } + ); }