Skip to content

Commit

Permalink
don't allow inherit query out of edit-site context
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jan 4, 2021
1 parent a373f45 commit 0ab3361
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"layout": "layout"
},
"usesContext": [
"postId"
"postId",
"templateSlug"
],
"supports": {
"html": false
Expand Down
10 changes: 8 additions & 2 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DEFAULTS_POSTS_PER_PAGE } from '../constants';
const TEMPLATE = [ [ 'core/query-loop' ] ];
export function QueryContent( {
attributes,
context: { postId },
context: { postId, templateSlug },
setAttributes,
} ) {
const { queryId, query, layout } = attributes;
Expand All @@ -47,10 +47,15 @@ export function QueryContent( {
if ( ! query.perPage && postsPerPage ) {
newQuery.perPage = postsPerPage;
}
// Show and allow inherit Query options only when
// in site-editing context.
if ( ! templateSlug && query.inherit ) {
newQuery.inherit = false;
}
if ( !! Object.keys( newQuery ).length ) {
updateQuery( newQuery );
}
}, [ query.perPage, query.exclude, postId ] );
}, [ query.perPage, query.exclude, query.inherit, postId, templateSlug ] );
// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.
useEffect( () => {
Expand All @@ -68,6 +73,7 @@ export function QueryContent( {
attributes={ attributes }
setQuery={ updateQuery }
setLayout={ updateLayout }
allowInheritQuery={ !! templateSlug }
/>
<BlockControls>
<QueryToolbar
Expand Down
21 changes: 13 additions & 8 deletions packages/block-library/src/query/edit/query-inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default function QueryInspectorControls( {
attributes: { query, layout },
setQuery,
setLayout,
allowInheritQuery,
} ) {
const {
order,
Expand Down Expand Up @@ -170,14 +171,18 @@ export default function QueryInspectorControls( {
<InspectorControls>
<CreateNewPostLink type={ postType } />
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
label={ __( 'Inherit query from URL' ) }
help={ __(
'Disable the option to customize the query arguments. Leave enabled to inherit the global query depending on the URL.'
) }
checked={ !! inherit }
onChange={ ( value ) => setQuery( { inherit: !! value } ) }
/>
{ allowInheritQuery && (
<ToggleControl
label={ __( 'Inherit query from URL' ) }
help={ __(
'Disable the option to customize the query arguments. Leave enabled to inherit the global query depending on the URL.'
) }
checked={ !! inherit }
onChange={ ( value ) =>
setQuery( { inherit: !! value } )
}
/>
) }
{ ! inherit && (
<SelectControl
options={ postTypesSelectOptions }
Expand Down

0 comments on commit 0ab3361

Please sign in to comment.