Skip to content

Commit

Permalink
Move isTemplate logic to QueryContent
Browse files Browse the repository at this point in the history
  • Loading branch information
mikachan committed Sep 18, 2024
1 parent 52aaf5a commit 47c84a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ import { useToolsPanelDropdownMenuProps } from '../../../utils/hooks';
const { BlockInfo } = unlock( blockEditorPrivateApis );

export default function QueryInspectorControls( props ) {
const { attributes, setQuery, setDisplayLayout, postTypeFromContext } =
props;
const { attributes, setQuery, setDisplayLayout, isTemplate } = props;
const { query, displayLayout } = attributes;
const {
order,
Expand Down Expand Up @@ -121,25 +120,6 @@ export default function QueryInspectorControls( props ) {
return onChangeDebounced.cancel;
}, [ querySearch, onChangeDebounced ] );

const isTemplate = useSelect(
( select ) => {
const currentTemplate =
select( coreStore ).__experimentalGetTemplateForLink()?.type;
const isInTemplate = 'wp_template' === currentTemplate;
const isInSingularContent = postTypeFromContext !== undefined;
return isInTemplate && ! isInSingularContent;
},
[ postTypeFromContext ]
);

// 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).
useEffect( () => {
if ( ! isTemplate && query.inherit ) {
setQuery( { inherit: false } );
}
}, [ isTemplate, query.inherit, setQuery ] );

const showInheritControl =
isTemplate && isControlAllowed( allowedControls, 'inherit' );
const showPostTypeControl =
Expand Down
19 changes: 17 additions & 2 deletions packages/block-library/src/query/edit/query-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export default function QueryContent( {
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
} );
const isTemplate = useSelect(
( select ) => {
const currentTemplate =
select( coreStore ).__experimentalGetTemplateForLink()?.type;
const isInTemplate = 'wp_template' === currentTemplate;
const isInSingularContent = postType !== undefined;
return isInTemplate && ! isInSingularContent;
},
[ postType ]
);
const { postsPerPage } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const { getEntityRecord, getEntityRecordEdits, canUser } =
Expand Down Expand Up @@ -92,11 +102,16 @@ 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 ) {
newQuery.inherit = false;
}
if ( !! Object.keys( newQuery ).length ) {
__unstableMarkNextChangeAsNotPersistent();
updateQuery( newQuery );
}
}, [ query.perPage, postsPerPage, inherit ] );
}, [ query.perPage, postsPerPage, inherit, isTemplate ] );
// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.
useEffect( () => {
Expand Down Expand Up @@ -137,7 +152,7 @@ export default function QueryContent( {
setDisplayLayout={ updateDisplayLayout }
setAttributes={ setAttributes }
clientId={ clientId }
postTypeFromContext={ postType }
isTemplate={ isTemplate }
/>
</InspectorControls>
<BlockControls>
Expand Down

0 comments on commit 47c84a7

Please sign in to comment.