-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Check ability to create patterns on the add new pattern modal. #62633
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,21 +36,29 @@ export default function AddNewPattern() { | |
const [ showPatternModal, setShowPatternModal ] = useState( false ); | ||
const [ showTemplatePartModal, setShowTemplatePartModal ] = | ||
useState( false ); | ||
// eslint-disable-next-line @wordpress/no-unused-vars-before-return | ||
const { createPatternFromFile } = unlock( useDispatch( patternsStore ) ); | ||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
const patternUploadInputRef = useRef(); | ||
const { isBlockBasedTheme, addNewPatternLabel, addNewTemplatePartLabel } = | ||
useSelect( ( select ) => { | ||
const { getCurrentTheme, getPostType } = select( coreStore ); | ||
return { | ||
isBlockBasedTheme: getCurrentTheme()?.is_block_theme, | ||
addNewPatternLabel: getPostType( PATTERN_TYPES.user )?.labels | ||
?.add_new_item, | ||
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE ) | ||
?.labels?.add_new_item, | ||
}; | ||
}, [] ); | ||
const { | ||
isBlockBasedTheme, | ||
addNewPatternLabel, | ||
addNewTemplatePartLabel, | ||
canCreatePattern, | ||
canCreateTemplatePart, | ||
} = useSelect( ( select ) => { | ||
const { getCurrentTheme, getPostType, canUser } = select( coreStore ); | ||
return { | ||
isBlockBasedTheme: getCurrentTheme()?.is_block_theme, | ||
addNewPatternLabel: getPostType( PATTERN_TYPES.user )?.labels | ||
?.add_new_item, | ||
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE ) | ||
?.labels?.add_new_item, | ||
canCreatePattern: canUser( 'create', 'blocks' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be confusing that 'blocks' here represents patterns. I wonder if this needs a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, blocks refer to wp_blocks CPT, one needs to be able to create a post there to create a pattern otherwise things fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is why I think a comment about it would help. |
||
canCreateTemplatePart: canUser( 'create', 'template-parts' ), | ||
}; | ||
}, [] ); | ||
|
||
function handleCreatePattern( { pattern } ) { | ||
setShowPatternModal( false ); | ||
|
@@ -78,31 +86,37 @@ export default function AddNewPattern() { | |
setShowTemplatePartModal( false ); | ||
} | ||
|
||
const controls = [ | ||
{ | ||
const controls = []; | ||
if ( canCreatePattern ) { | ||
controls.push( { | ||
icon: symbol, | ||
onClick: () => setShowPatternModal( true ), | ||
title: addNewPatternLabel, | ||
}, | ||
]; | ||
} ); | ||
} | ||
|
||
if ( isBlockBasedTheme ) { | ||
if ( isBlockBasedTheme && canCreateTemplatePart ) { | ||
controls.push( { | ||
icon: symbolFilled, | ||
onClick: () => setShowTemplatePartModal( true ), | ||
title: addNewTemplatePartLabel, | ||
} ); | ||
} | ||
|
||
controls.push( { | ||
icon: upload, | ||
onClick: () => { | ||
patternUploadInputRef.current.click(); | ||
}, | ||
title: __( 'Import pattern from JSON' ), | ||
} ); | ||
if ( canCreatePattern ) { | ||
controls.push( { | ||
icon: upload, | ||
onClick: () => { | ||
patternUploadInputRef.current.click(); | ||
}, | ||
title: __( 'Import pattern from JSON' ), | ||
} ); | ||
} | ||
|
||
const { categoryMap, findOrCreateTerm } = useAddPatternCategory(); | ||
if ( controls.length === 0 ) { | ||
return null; | ||
} | ||
return ( | ||
<> | ||
{ addNewPatternLabel && ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another trick is to use
useRegistry
, call dispatch and unlock in the action callback.