Skip to content

Commit bb73da7

Browse files
jorgefilipecostacarolinanellatrix
authored
Fix: Check ability to create patterns on the add new pattern modal. (#62633)
Co-authored-by: jorgefilipecosta <[email protected]> Co-authored-by: carolinan <[email protected]> Co-authored-by: ellatrix <[email protected]>
1 parent 5738e9b commit bb73da7

File tree

1 file changed

+37
-23
lines changed
  • packages/edit-site/src/components/add-new-pattern

1 file changed

+37
-23
lines changed

packages/edit-site/src/components/add-new-pattern/index.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,29 @@ export default function AddNewPattern() {
3636
const [ showPatternModal, setShowPatternModal ] = useState( false );
3737
const [ showTemplatePartModal, setShowTemplatePartModal ] =
3838
useState( false );
39+
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
3940
const { createPatternFromFile } = unlock( useDispatch( patternsStore ) );
4041
const { createSuccessNotice, createErrorNotice } =
4142
useDispatch( noticesStore );
4243
const patternUploadInputRef = useRef();
43-
const { isBlockBasedTheme, addNewPatternLabel, addNewTemplatePartLabel } =
44-
useSelect( ( select ) => {
45-
const { getCurrentTheme, getPostType } = select( coreStore );
46-
return {
47-
isBlockBasedTheme: getCurrentTheme()?.is_block_theme,
48-
addNewPatternLabel: getPostType( PATTERN_TYPES.user )?.labels
49-
?.add_new_item,
50-
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE )
51-
?.labels?.add_new_item,
52-
};
53-
}, [] );
44+
const {
45+
isBlockBasedTheme,
46+
addNewPatternLabel,
47+
addNewTemplatePartLabel,
48+
canCreatePattern,
49+
canCreateTemplatePart,
50+
} = useSelect( ( select ) => {
51+
const { getCurrentTheme, getPostType, canUser } = select( coreStore );
52+
return {
53+
isBlockBasedTheme: getCurrentTheme()?.is_block_theme,
54+
addNewPatternLabel: getPostType( PATTERN_TYPES.user )?.labels
55+
?.add_new_item,
56+
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE )
57+
?.labels?.add_new_item,
58+
canCreatePattern: canUser( 'create', 'blocks' ),
59+
canCreateTemplatePart: canUser( 'create', 'template-parts' ),
60+
};
61+
}, [] );
5462

5563
function handleCreatePattern( { pattern } ) {
5664
setShowPatternModal( false );
@@ -78,31 +86,37 @@ export default function AddNewPattern() {
7886
setShowTemplatePartModal( false );
7987
}
8088

81-
const controls = [
82-
{
89+
const controls = [];
90+
if ( canCreatePattern ) {
91+
controls.push( {
8392
icon: symbol,
8493
onClick: () => setShowPatternModal( true ),
8594
title: addNewPatternLabel,
86-
},
87-
];
95+
} );
96+
}
8897

89-
if ( isBlockBasedTheme ) {
98+
if ( isBlockBasedTheme && canCreateTemplatePart ) {
9099
controls.push( {
91100
icon: symbolFilled,
92101
onClick: () => setShowTemplatePartModal( true ),
93102
title: addNewTemplatePartLabel,
94103
} );
95104
}
96105

97-
controls.push( {
98-
icon: upload,
99-
onClick: () => {
100-
patternUploadInputRef.current.click();
101-
},
102-
title: __( 'Import pattern from JSON' ),
103-
} );
106+
if ( canCreatePattern ) {
107+
controls.push( {
108+
icon: upload,
109+
onClick: () => {
110+
patternUploadInputRef.current.click();
111+
},
112+
title: __( 'Import pattern from JSON' ),
113+
} );
114+
}
104115

105116
const { categoryMap, findOrCreateTerm } = useAddPatternCategory();
117+
if ( controls.length === 0 ) {
118+
return null;
119+
}
106120
return (
107121
<>
108122
{ addNewPatternLabel && (

0 commit comments

Comments
 (0)