From f6c53bacea81496842e8396b435b3cf051114e2d Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 25 Sep 2024 13:16:33 +0100 Subject: [PATCH 1/6] Use templateSlug for additional context in the editor --- packages/block-library/src/query/block.json | 2 +- .../src/query/edit/query-content.js | 40 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index b2225192c6b218..2379a1d1da53cf 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -42,7 +42,7 @@ "default": false } }, - "usesContext": [ "postType" ], + "usesContext": [ "postType", "templateSlug" ], "providesContext": { "queryId": "queryId", "query": "query", diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 8b3ff09b17934b..0c493ca9f00d90 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -42,7 +42,7 @@ export default function QueryContent( { tagName: TagName = 'div', query: { inherit } = {}, } = attributes; - const { postType } = context; + const { postType, templateSlug } = context; const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); const instanceId = useInstanceId( QueryContent ); @@ -50,15 +50,31 @@ export default function QueryContent( { const innerBlocksProps = useInnerBlocksProps( blockProps, { template: TEMPLATE, } ); - const isTemplate = useSelect( + const { isTemplate } = useSelect( ( select ) => { - const currentTemplate = + const editorPostType = select( coreStore ).__experimentalGetTemplateForLink()?.type; - const isInTemplate = 'wp_template' === currentTemplate; - const isInSingularContent = postType !== undefined; - return isInTemplate && ! isInSingularContent; + const isInTemplate = 'wp_template' === editorPostType; + const singularTemplates = [ '404', 'single', 'page', 'wp' ]; + const typeOfQueryFromSlug = templateSlug.includes( '-' ) + ? templateSlug.split( '-', 1 )[ 0 ] + : templateSlug; + const queryFromTemplateSlug = templateSlug.includes( '-' ) + ? templateSlug.split( '-' ).slice( 1 ).join( '-' ) + : ''; + let typeOfTemplate = templateSlug; + if ( queryFromTemplateSlug ) { + typeOfTemplate = typeOfQueryFromSlug; + } + const isSingularTemplate = + singularTemplates.includes( typeOfTemplate ) || + postType !== undefined; + + // isTemplate is true if we are in a template and it's + // not a singular content template. + return { isTemplate: isInTemplate && ! isSingularTemplate }; }, - [ postType ] + [ postType, templateSlug ] ); const { postsPerPage } = useSelect( ( select ) => { const { getSettings } = select( blockEditorStore ); @@ -110,6 +126,11 @@ export default function QueryContent( { // are not inherited when outside a template (e.g. when in singular content). if ( ! isTemplate && query.inherit ) { newQuery.inherit = false; + + // We need to update the query in the Editor if not on a singular template. + if ( postType && query.postType !== postType ) { + newQuery.postType = postType; + } } if ( !! Object.keys( newQuery ).length ) { __unstableMarkNextChangeAsNotPersistent(); @@ -117,10 +138,13 @@ export default function QueryContent( { } }, [ query.perPage, + query.inherit, + query.postType, postsPerPage, inherit, isTemplate, - query.inherit, + templateSlug, + postType, __unstableMarkNextChangeAsNotPersistent, updateQuery, ] ); From 713bd7d1ac488af19f054d86a144f01010050c2d Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 30 Sep 2024 17:22:41 +0100 Subject: [PATCH 2/6] Rename isTemplate to isSingularTemplate --- .../query/edit/inspector-controls/index.js | 11 +++---- .../src/query/edit/query-content.js | 30 +++++++++++-------- 2 files changed, 24 insertions(+), 17 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 4085128e9aef1a..33f40f51ee86f8 100644 --- a/packages/block-library/src/query/edit/inspector-controls/index.js +++ b/packages/block-library/src/query/edit/inspector-controls/index.js @@ -45,7 +45,8 @@ import { useToolsPanelDropdownMenuProps } from '../../../utils/hooks'; const { BlockInfo } = unlock( blockEditorPrivateApis ); export default function QueryInspectorControls( props ) { - const { attributes, setQuery, setDisplayLayout, isTemplate } = props; + const { attributes, setQuery, setDisplayLayout, isSingularTemplate } = + props; const { query, displayLayout } = attributes; const { order, @@ -121,10 +122,10 @@ export default function QueryInspectorControls( props ) { }, [ querySearch, onChangeDebounced ] ); const showInheritControl = - isTemplate && isControlAllowed( allowedControls, 'inherit' ); + ! isSingularTemplate && isControlAllowed( allowedControls, 'inherit' ); const showPostTypeControl = ( ! inherit && isControlAllowed( allowedControls, 'postType' ) ) || - ! isTemplate; + isSingularTemplate; const postTypeControlLabel = __( 'Post type' ); const postTypeControlHelp = __( 'Select the type of content to display: posts, pages, or custom post types.' @@ -132,12 +133,12 @@ export default function QueryInspectorControls( props ) { const showColumnsControl = false; const showOrderControl = ( ! inherit && isControlAllowed( allowedControls, 'order' ) ) || - ! isTemplate; + isSingularTemplate; const showStickyControl = ( ! inherit && showSticky && isControlAllowed( allowedControls, 'sticky' ) ) || - ( showSticky && ! isTemplate ); + ( showSticky && isSingularTemplate ); const showSettingsPanel = showInheritControl || showPostTypeControl || diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 0c493ca9f00d90..88f98709a5a694 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -50,12 +50,18 @@ export default function QueryContent( { const innerBlocksProps = useInnerBlocksProps( blockProps, { template: TEMPLATE, } ); - const { isTemplate } = useSelect( + const isSingularTemplate = useSelect( ( select ) => { const editorPostType = select( coreStore ).__experimentalGetTemplateForLink()?.type; const isInTemplate = 'wp_template' === editorPostType; - const singularTemplates = [ '404', 'single', 'page', 'wp' ]; + const singularTemplates = [ + '404', + 'blank', + 'single', + 'page', + 'wp', + ]; const typeOfQueryFromSlug = templateSlug.includes( '-' ) ? templateSlug.split( '-', 1 )[ 0 ] : templateSlug; @@ -66,13 +72,13 @@ export default function QueryContent( { if ( queryFromTemplateSlug ) { typeOfTemplate = typeOfQueryFromSlug; } - const isSingularTemplate = + const isInSingularTemplate = singularTemplates.includes( typeOfTemplate ) || postType !== undefined; - // isTemplate is true if we are in a template and it's - // not a singular content template. - return { isTemplate: isInTemplate && ! isSingularTemplate }; + // Returns true if we are in a template and it's + // for singular content (e.g. post, page, 404, blank). + return isInTemplate && isInSingularTemplate; }, [ postType, templateSlug ] ); @@ -122,12 +128,12 @@ export default function QueryContent( { } else if ( ! query.perPage && postsPerPage ) { newQuery.perPage = postsPerPage; } - // We need to reset the `inherit` value if not in a template, as queries - // are not inherited when outside a template (e.g. when in singular content). - if ( ! isTemplate && query.inherit ) { + // 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 ( isSingularTemplate && query.inherit ) { newQuery.inherit = false; - // We need to update the query in the Editor if not on a singular template. + // We need to update the query in the Editor if a specific post type is set. if ( postType && query.postType !== postType ) { newQuery.postType = postType; } @@ -142,7 +148,7 @@ export default function QueryContent( { query.postType, postsPerPage, inherit, - isTemplate, + isSingularTemplate, templateSlug, postType, __unstableMarkNextChangeAsNotPersistent, @@ -191,7 +197,7 @@ export default function QueryContent( { setDisplayLayout={ updateDisplayLayout } setAttributes={ setAttributes } clientId={ clientId } - isTemplate={ isTemplate } + isSingularTemplate={ isSingularTemplate } /> From 8fe08a17dfe22d84809fa504375cfd4e736fc268 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 30 Sep 2024 19:48:38 +0100 Subject: [PATCH 3/6] Move singular logic to getQueryContext utils function --- .../query/edit/inspector-controls/index.js | 11 ++- .../src/query/edit/query-content.js | 51 ++++---------- .../block-library/src/query/test/utils.js | 67 ++++++++++++++++++- packages/block-library/src/query/utils.js | 30 +++++++++ 4 files changed, 114 insertions(+), 45 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 33f40f51ee86f8..d495f7272cb185 100644 --- a/packages/block-library/src/query/edit/inspector-controls/index.js +++ b/packages/block-library/src/query/edit/inspector-controls/index.js @@ -45,8 +45,7 @@ import { useToolsPanelDropdownMenuProps } from '../../../utils/hooks'; const { BlockInfo } = unlock( blockEditorPrivateApis ); export default function QueryInspectorControls( props ) { - const { attributes, setQuery, setDisplayLayout, isSingularTemplate } = - props; + const { attributes, setQuery, setDisplayLayout, isSingular } = props; const { query, displayLayout } = attributes; const { order, @@ -122,10 +121,10 @@ export default function QueryInspectorControls( props ) { }, [ querySearch, onChangeDebounced ] ); const showInheritControl = - ! isSingularTemplate && isControlAllowed( allowedControls, 'inherit' ); + ! isSingular && isControlAllowed( allowedControls, 'inherit' ); const showPostTypeControl = ( ! inherit && isControlAllowed( allowedControls, 'postType' ) ) || - isSingularTemplate; + isSingular; const postTypeControlLabel = __( 'Post type' ); const postTypeControlHelp = __( 'Select the type of content to display: posts, pages, or custom post types.' @@ -133,12 +132,12 @@ export default function QueryInspectorControls( props ) { const showColumnsControl = false; const showOrderControl = ( ! inherit && isControlAllowed( allowedControls, 'order' ) ) || - isSingularTemplate; + isSingular; const showStickyControl = ( ! inherit && showSticky && isControlAllowed( allowedControls, 'sticky' ) ) || - ( showSticky && isSingularTemplate ); + ( showSticky && isSingular ); const showSettingsPanel = showInheritControl || showPostTypeControl || diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 88f98709a5a694..55a2d3d1e9569f 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -22,6 +22,7 @@ import EnhancedPaginationControl from './inspector-controls/enhanced-pagination- import QueryToolbar from './query-toolbar'; import QueryInspectorControls from './inspector-controls'; import EnhancedPaginationModal from './enhanced-pagination-modal'; +import { getQueryContext } from '../utils'; const DEFAULTS_POSTS_PER_PAGE = 3; @@ -42,7 +43,8 @@ export default function QueryContent( { tagName: TagName = 'div', query: { inherit } = {}, } = attributes; - const { postType, templateSlug } = context; + const { templateSlug, postType } = context; + const { isSingular } = getQueryContext( templateSlug, postType ); const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); const instanceId = useInstanceId( QueryContent ); @@ -50,38 +52,6 @@ export default function QueryContent( { const innerBlocksProps = useInnerBlocksProps( blockProps, { template: TEMPLATE, } ); - const isSingularTemplate = useSelect( - ( select ) => { - const editorPostType = - select( coreStore ).__experimentalGetTemplateForLink()?.type; - const isInTemplate = 'wp_template' === editorPostType; - const singularTemplates = [ - '404', - 'blank', - 'single', - 'page', - 'wp', - ]; - const typeOfQueryFromSlug = templateSlug.includes( '-' ) - ? templateSlug.split( '-', 1 )[ 0 ] - : templateSlug; - const queryFromTemplateSlug = templateSlug.includes( '-' ) - ? templateSlug.split( '-' ).slice( 1 ).join( '-' ) - : ''; - let typeOfTemplate = templateSlug; - if ( queryFromTemplateSlug ) { - typeOfTemplate = typeOfQueryFromSlug; - } - const isInSingularTemplate = - singularTemplates.includes( typeOfTemplate ) || - postType !== undefined; - - // Returns true if we are in a template and it's - // for singular content (e.g. post, page, 404, blank). - return isInTemplate && isInSingularTemplate; - }, - [ postType, templateSlug ] - ); const { postsPerPage } = useSelect( ( select ) => { const { getSettings } = select( blockEditorStore ); const { getEntityRecord, getEntityRecordEdits, canUser } = @@ -130,11 +100,17 @@ export default function QueryContent( { } // 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 ( isSingularTemplate && query.inherit ) { + if ( isSingular && query.inherit ) { newQuery.inherit = false; // We need to update the query in the Editor if a specific post type is set. - if ( postType && query.postType !== postType ) { + // Unless the post type is `page`, as it usually doesn't make sense to loop + // through pages. + if ( + postType && + postType !== 'page' && + query.postType !== postType + ) { newQuery.postType = postType; } } @@ -148,8 +124,7 @@ export default function QueryContent( { query.postType, postsPerPage, inherit, - isSingularTemplate, - templateSlug, + isSingular, postType, __unstableMarkNextChangeAsNotPersistent, updateQuery, @@ -197,7 +172,7 @@ export default function QueryContent( { setDisplayLayout={ updateDisplayLayout } setAttributes={ setAttributes } clientId={ clientId } - isSingularTemplate={ isSingularTemplate } + isSingular={ isSingular } /> diff --git a/packages/block-library/src/query/test/utils.js b/packages/block-library/src/query/test/utils.js index 1b711850086774..7be353a3b804f6 100644 --- a/packages/block-library/src/query/test/utils.js +++ b/packages/block-library/src/query/test/utils.js @@ -2,7 +2,11 @@ * Internal dependencies */ import { terms } from './fixtures'; -import { getEntitiesInfo, getValueFromObjectPath } from '../utils'; +import { + getEntitiesInfo, + getValueFromObjectPath, + getQueryContext, +} from '../utils'; describe( 'Query block utils', () => { describe( 'getEntitiesInfo', () => { @@ -61,4 +65,65 @@ describe( 'Query block utils', () => { expect( result ).toBe( 'test' ); } ); } ); + + describe( 'getQueryContext', () => { + it( 'should return the correct query context based on template slug and available post types', () => { + expect( getQueryContext( '404', undefined ) ).toStrictEqual( { + isSingular: true, + templateType: '404', + postType: '', + } ); + expect( getQueryContext( 'blank', undefined ) ).toStrictEqual( { + isSingular: true, + templateType: 'blank', + postType: '', + } ); + expect( getQueryContext( 'single', 'post' ) ).toStrictEqual( { + isSingular: true, + templateType: 'single', + postType: 'post', + } ); + expect( getQueryContext( 'single-film', undefined ) ).toStrictEqual( + { + isSingular: true, + templateType: 'single', + postType: 'film', + } + ); + expect( getQueryContext( 'page', 'page' ) ).toStrictEqual( { + isSingular: true, + templateType: 'page', + postType: 'page', + } ); + expect( getQueryContext( 'wp', undefined ) ).toStrictEqual( { + isSingular: true, + templateType: 'custom', + postType: '', + } ); + expect( getQueryContext( 'category', undefined ) ).toStrictEqual( { + isSingular: false, + templateType: 'category', + postType: '', + } ); + expect( + getQueryContext( 'category-dog', undefined ) + ).toStrictEqual( { + isSingular: false, + templateType: 'category', + postType: 'dog', + } ); + expect( getQueryContext( 'archive', undefined ) ).toStrictEqual( { + isSingular: false, + templateType: 'archive', + postType: '', + } ); + expect( + getQueryContext( 'archive-film', undefined ) + ).toStrictEqual( { + isSingular: false, + templateType: 'archive', + postType: 'film', + } ); + } ); + } ); } ); diff --git a/packages/block-library/src/query/utils.js b/packages/block-library/src/query/utils.js index 68da2573bab0f6..68dadd6e64c1a5 100644 --- a/packages/block-library/src/query/utils.js +++ b/packages/block-library/src/query/utils.js @@ -435,3 +435,33 @@ export const useUnsupportedBlocks = ( clientId ) => { [ clientId ] ); }; + +/** + * Helper function that returns the query context from the editor based on the + * available template slug and post type. + * + * @param {string} templateSlug Current template slug based on context. + * @param {string} postType Current post type based on context. + * @return {Object} An object with isSingular, templateType, and postType properties. + */ +export function getQueryContext( templateSlug = '', postType ) { + let isSingular = false; + let templateType = templateSlug === 'wp' ? 'custom' : templateSlug; + + const singularTemplates = [ '404', 'blank', 'single', 'page', 'custom' ]; + const templateTypeFromSlug = templateSlug.includes( '-' ) + ? templateSlug.split( '-', 1 )[ 0 ] + : templateSlug; + const queryFromTemplateSlug = templateSlug.includes( '-' ) + ? templateSlug.split( '-' ).slice( 1 ).join( '-' ) + : ''; + if ( queryFromTemplateSlug ) { + templateType = templateTypeFromSlug; + } + + isSingular = + singularTemplates.includes( templateType ) || postType !== undefined; + postType = postType ? postType : queryFromTemplateSlug; + + return { isSingular, templateType, postType }; +} From 08a3ad4820dd6655ce1be5346a575cce127c61fe Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 1 Oct 2024 13:00:21 +0100 Subject: [PATCH 4/6] Simplify function by removing postType logic --- .../src/query/edit/query-content.js | 21 ++++----- .../block-library/src/query/test/utils.js | 46 ++++++++----------- packages/block-library/src/query/utils.js | 15 ++---- 3 files changed, 31 insertions(+), 51 deletions(-) diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 55a2d3d1e9569f..5253044243dcb5 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -22,7 +22,7 @@ import EnhancedPaginationControl from './inspector-controls/enhanced-pagination- import QueryToolbar from './query-toolbar'; import QueryInspectorControls from './inspector-controls'; import EnhancedPaginationModal from './enhanced-pagination-modal'; -import { getQueryContext } from '../utils'; +import { getQueryContextFromTemplate } from '../utils'; const DEFAULTS_POSTS_PER_PAGE = 3; @@ -44,7 +44,7 @@ export default function QueryContent( { query: { inherit } = {}, } = attributes; const { templateSlug, postType } = context; - const { isSingular } = getQueryContext( templateSlug, postType ); + const { isSingular } = getQueryContextFromTemplate( templateSlug ); const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); const instanceId = useInstanceId( QueryContent ); @@ -102,17 +102,12 @@ export default function QueryContent( { // are not inherited when in singular content (e.g. post, page, 404, blank). if ( isSingular && query.inherit ) { newQuery.inherit = false; - - // We need to update the query in the Editor if a specific post type is set. - // Unless the post type is `page`, as it usually doesn't make sense to loop - // through pages. - if ( - postType && - postType !== 'page' && - query.postType !== postType - ) { - newQuery.postType = postType; - } + } + // We need to update the query in the Editor if a specific post type is set. + // Unless the post type is `page`, as it usually doesn't make sense to loop + // through pages. + if ( postType && postType !== 'page' && query.postType !== postType ) { + newQuery.postType = postType; } if ( !! Object.keys( newQuery ).length ) { __unstableMarkNextChangeAsNotPersistent(); diff --git a/packages/block-library/src/query/test/utils.js b/packages/block-library/src/query/test/utils.js index 7be353a3b804f6..e2619f2f70e88f 100644 --- a/packages/block-library/src/query/test/utils.js +++ b/packages/block-library/src/query/test/utils.js @@ -5,7 +5,7 @@ import { terms } from './fixtures'; import { getEntitiesInfo, getValueFromObjectPath, - getQueryContext, + getQueryContextFromTemplate, } from '../utils'; describe( 'Query block utils', () => { @@ -66,63 +66,53 @@ describe( 'Query block utils', () => { } ); } ); - describe( 'getQueryContext', () => { - it( 'should return the correct query context based on template slug and available post types', () => { - expect( getQueryContext( '404', undefined ) ).toStrictEqual( { + describe( 'getQueryContextFromTemplate', () => { + it( 'should return the correct query context based on template slug', () => { + expect( getQueryContextFromTemplate( '404' ) ).toStrictEqual( { isSingular: true, templateType: '404', - postType: '', } ); - expect( getQueryContext( 'blank', undefined ) ).toStrictEqual( { + expect( getQueryContextFromTemplate( 'blank' ) ).toStrictEqual( { isSingular: true, templateType: 'blank', - postType: '', } ); - expect( getQueryContext( 'single', 'post' ) ).toStrictEqual( { + expect( getQueryContextFromTemplate( 'single' ) ).toStrictEqual( { isSingular: true, templateType: 'single', - postType: 'post', } ); - expect( getQueryContext( 'single-film', undefined ) ).toStrictEqual( - { - isSingular: true, - templateType: 'single', - postType: 'film', - } - ); - expect( getQueryContext( 'page', 'page' ) ).toStrictEqual( { + expect( + getQueryContextFromTemplate( 'single-film' ) + ).toStrictEqual( { + isSingular: true, + templateType: 'single', + } ); + expect( getQueryContextFromTemplate( 'page' ) ).toStrictEqual( { isSingular: true, templateType: 'page', - postType: 'page', } ); - expect( getQueryContext( 'wp', undefined ) ).toStrictEqual( { + expect( getQueryContextFromTemplate( 'wp' ) ).toStrictEqual( { isSingular: true, templateType: 'custom', - postType: '', } ); - expect( getQueryContext( 'category', undefined ) ).toStrictEqual( { + expect( getQueryContextFromTemplate( 'category' ) ).toStrictEqual( { isSingular: false, templateType: 'category', - postType: '', } ); expect( - getQueryContext( 'category-dog', undefined ) + getQueryContextFromTemplate( 'category-dog' ) ).toStrictEqual( { isSingular: false, templateType: 'category', - postType: 'dog', } ); - expect( getQueryContext( 'archive', undefined ) ).toStrictEqual( { + expect( getQueryContextFromTemplate( 'archive' ) ).toStrictEqual( { isSingular: false, templateType: 'archive', - postType: '', } ); expect( - getQueryContext( 'archive-film', undefined ) + getQueryContextFromTemplate( 'archive-film' ) ).toStrictEqual( { isSingular: false, templateType: 'archive', - postType: 'film', } ); } ); } ); diff --git a/packages/block-library/src/query/utils.js b/packages/block-library/src/query/utils.js index 68dadd6e64c1a5..ae1fccc6701e98 100644 --- a/packages/block-library/src/query/utils.js +++ b/packages/block-library/src/query/utils.js @@ -438,16 +438,14 @@ export const useUnsupportedBlocks = ( clientId ) => { /** * Helper function that returns the query context from the editor based on the - * available template slug and post type. + * available template slug. * * @param {string} templateSlug Current template slug based on context. - * @param {string} postType Current post type based on context. - * @return {Object} An object with isSingular, templateType, and postType properties. + * @return {Object} An object with isSingular and templateType properties. */ -export function getQueryContext( templateSlug = '', postType ) { +export function getQueryContextFromTemplate( templateSlug = '' ) { let isSingular = false; let templateType = templateSlug === 'wp' ? 'custom' : templateSlug; - const singularTemplates = [ '404', 'blank', 'single', 'page', 'custom' ]; const templateTypeFromSlug = templateSlug.includes( '-' ) ? templateSlug.split( '-', 1 )[ 0 ] @@ -458,10 +456,7 @@ export function getQueryContext( templateSlug = '', postType ) { if ( queryFromTemplateSlug ) { templateType = templateTypeFromSlug; } + isSingular = singularTemplates.includes( templateType ); - isSingular = - singularTemplates.includes( templateType ) || postType !== undefined; - postType = postType ? postType : queryFromTemplateSlug; - - return { isSingular, templateType, postType }; + return { isSingular, templateType }; } From aec7cba4f37baa3707e6cf7deebf7bd690cd9ce1 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 30 Oct 2024 17:23:15 +0000 Subject: [PATCH 5/6] Get postType from context --- .../src/post-template/block.json | 3 ++- .../block-library/src/post-template/edit.js | 6 +++++- .../query/edit/inspector-controls/index.js | 20 +++++++++++++++++-- .../src/query/edit/query-content.js | 9 +-------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/post-template/block.json b/packages/block-library/src/post-template/block.json index 6a575855583527..da576a83312a45 100644 --- a/packages/block-library/src/post-template/block.json +++ b/packages/block-library/src/post-template/block.json @@ -13,7 +13,8 @@ "displayLayout", "templateSlug", "previewPostType", - "enhancedPagination" + "enhancedPagination", + "postType" ], "supports": { "reusable": false, diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js index a9e279ee2c3052..4e7e514ed82b12 100644 --- a/packages/block-library/src/post-template/edit.js +++ b/packages/block-library/src/post-template/edit.js @@ -100,6 +100,7 @@ export default function PostTemplateEdit( { } = {}, templateSlug, previewPostType, + postType: postTypeFromContext, }, attributes: { layout }, __unstableLayoutClassNames, @@ -186,7 +187,10 @@ export default function PostTemplateEdit( { } // When we preview Query Loop blocks we should prefer the current // block's postType, which is passed through block context. - const usedPostType = previewPostType || postType; + const usedPostType = + postTypeFromContext !== 'page' + ? postTypeFromContext + : previewPostType || postType; return { posts: getEntityRecords( 'postType', usedPostType, { ...query, 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 7d5745e190c9a7..bc900c073f648d 100644 --- a/packages/block-library/src/query/edit/inspector-controls/index.js +++ b/packages/block-library/src/query/edit/inspector-controls/index.js @@ -45,9 +45,15 @@ import { useToolsPanelDropdownMenuProps } from '../../../utils/hooks'; const { BlockInfo } = unlock( blockEditorPrivateApis ); export default function QueryInspectorControls( props ) { - const { attributes, setQuery, setDisplayLayout, isSingular } = props; - const { query, displayLayout } = attributes; const { + attributes, + setQuery, + setDisplayLayout, + postTypeFromContext, + isSingular, + } = props; + const { query, displayLayout } = attributes; + let { order, orderBy, author: authorIds, @@ -61,6 +67,16 @@ export default function QueryInspectorControls( props ) { parents, format, } = query; + // If a post type is set in context, update `postType` to match it, + // unless the post type is `page`, as it usually doesn't make sense to loop + // through pages. + if ( + postTypeFromContext && + postTypeFromContext !== 'page' && + postTypeFromContext !== postType + ) { + postType = postTypeFromContext; + } const allowedControls = useAllowedControls( attributes ); const showSticky = postType === 'post'; const { diff --git a/packages/block-library/src/query/edit/query-content.js b/packages/block-library/src/query/edit/query-content.js index 5253044243dcb5..bdfcbe0498ec4a 100644 --- a/packages/block-library/src/query/edit/query-content.js +++ b/packages/block-library/src/query/edit/query-content.js @@ -103,12 +103,6 @@ export default function QueryContent( { if ( isSingular && query.inherit ) { newQuery.inherit = false; } - // We need to update the query in the Editor if a specific post type is set. - // Unless the post type is `page`, as it usually doesn't make sense to loop - // through pages. - if ( postType && postType !== 'page' && query.postType !== postType ) { - newQuery.postType = postType; - } if ( !! Object.keys( newQuery ).length ) { __unstableMarkNextChangeAsNotPersistent(); updateQuery( newQuery ); @@ -116,11 +110,9 @@ export default function QueryContent( { }, [ query.perPage, query.inherit, - query.postType, postsPerPage, inherit, isSingular, - postType, __unstableMarkNextChangeAsNotPersistent, updateQuery, ] ); @@ -167,6 +159,7 @@ export default function QueryContent( { setDisplayLayout={ updateDisplayLayout } setAttributes={ setAttributes } clientId={ clientId } + postTypeFromContext={ postType } isSingular={ isSingular } /> From a43b176ba015c8b0a3a207b7d9354a7b689c33a6 Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Thu, 31 Oct 2024 08:55:45 +0100 Subject: [PATCH 6/6] Fix for Post Editor --- packages/block-library/src/query/test/utils.js | 3 +++ packages/block-library/src/query/utils.js | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query/test/utils.js b/packages/block-library/src/query/test/utils.js index e2619f2f70e88f..8c2d88ade80518 100644 --- a/packages/block-library/src/query/test/utils.js +++ b/packages/block-library/src/query/test/utils.js @@ -68,6 +68,9 @@ describe( 'Query block utils', () => { describe( 'getQueryContextFromTemplate', () => { it( 'should return the correct query context based on template slug', () => { + expect( getQueryContextFromTemplate() ).toStrictEqual( { + isSingular: true, + } ); expect( getQueryContextFromTemplate( '404' ) ).toStrictEqual( { isSingular: true, templateType: '404', diff --git a/packages/block-library/src/query/utils.js b/packages/block-library/src/query/utils.js index ae1fccc6701e98..fc22ca46d471c0 100644 --- a/packages/block-library/src/query/utils.js +++ b/packages/block-library/src/query/utils.js @@ -443,7 +443,11 @@ export const useUnsupportedBlocks = ( clientId ) => { * @param {string} templateSlug Current template slug based on context. * @return {Object} An object with isSingular and templateType properties. */ -export function getQueryContextFromTemplate( templateSlug = '' ) { +export function getQueryContextFromTemplate( templateSlug ) { + // In the Post Editor, the template slug is not available. + if ( ! templateSlug ) { + return { isSingular: true }; + } let isSingular = false; let templateType = templateSlug === 'wp' ? 'custom' : templateSlug; const singularTemplates = [ '404', 'blank', 'single', 'page', 'custom' ];