Skip to content

Commit

Permalink
Add: Post type restriction API for patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 21, 2022
1 parent db8b006 commit 42f9062
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
17 changes: 13 additions & 4 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@ export default function BlockEditor( { setIsInserterOpen } ) {
[]
);

const blockPatterns = useMemo(
() => unionBy( settingsBlockPatterns, restBlockPatterns, 'name' ),
[ settingsBlockPatterns, restBlockPatterns ]
);
const blockPatterns = useMemo( () => {
return unionBy(
settingsBlockPatterns,
restBlockPatterns,
'name'
).filter( ( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( templateType ) )
);
} );
}, [ settingsBlockPatterns, restBlockPatterns, templateType ] );

const blockPatternCategories = useMemo(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ function useBlockEditorSettings( settings, hasTemplate ) {
canUseUnfilteredHTML,
userCanCreatePages,
pageOnFront,
postType,
} = useSelect( ( select ) => {
const { canUserUseUnfilteredHTML } = select( editorStore );
const { canUserUseUnfilteredHTML, getCurrentPostType } =
select( editorStore );
const isWeb = Platform.OS === 'web';
const { canUser, getEntityRecord } = select( coreStore );

Expand All @@ -55,6 +57,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
hasUploadPermissions: canUser( 'create', 'media' ) ?? true,
userCanCreatePages: canUser( 'create', 'pages' ),
pageOnFront: siteSettings?.page_on_front,
postType: getCurrentPostType(),
};
}, [] );

Expand All @@ -74,10 +77,18 @@ function useBlockEditorSettings( settings, hasTemplate ) {
[]
);

const blockPatterns = useMemo(
() => unionBy( settingsBlockPatterns, restBlockPatterns, 'name' ),
[ settingsBlockPatterns, restBlockPatterns ]
);
const blockPatterns = useMemo( () => {
return unionBy(
settingsBlockPatterns,
restBlockPatterns,
'name'
).filter( ( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) && postTypes.includes( postType ) )
);
} );
}, [ settingsBlockPatterns, restBlockPatterns, postType ] );

const blockPatternCategories = useMemo(
() =>
Expand Down

0 comments on commit 42f9062

Please sign in to comment.