-
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
Fix: Check ability to create patterns on the add new pattern modal. #62633
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +42 B (0%) Total Size: 1.76 MB
ℹ️ View Unchanged
|
?.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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is blocks
referring to the CPT?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This is why I think a comment about it would help.
This PR does hide the "Add new pattern" button, but the Duplicate option is still available from the action menu, is there a separate PR for that? |
@@ -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 ) ); |
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.
label={ | ||
canCreatePattern | ||
? addNewPatternLabel | ||
: addNewTemplatePartLabel |
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.
Why are we switching here now? What if you can do neither?
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.
It felt strange to have a menu saying Add New Pattern, while adding a template part was the option. If the user can not add template parts and can not add patterns, the condition if ( controls.length === 0 ) { will be true and we will not render any menu.
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.
There's also a check for addNewPatternLabel
which is maybe a bit strange now.
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.
Hi @ellatrix, I reverted this change and now we just focus on checking permissions before rendering controls.
f808c13
to
a2e93e7
Compare
Hi @carolinan, thank you for the review. I plan on fixing the duplicate pattern action in a separate PR following the same approach we ended up deciding on for the trash post-action that is being discussed at #62633. |
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.
…62633) Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: carolinan <poena@git.wordpress.org> Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
I just cherry-picked this PR to the wp/6.6-rc-1 branch to get it included in the next release: d493024 |
…62633) Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: carolinan <poena@git.wordpress.org> Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
A user may have permission to edit patterns and should see the patterns section but may not have permission to create new patterns.
If the user has no permission to create patterns we currently show the option to create and import anyway but when using these features there is a crash.
This PR adds a check for the capability to create patterns, and also a check for the capability to create template parts. The check for the capability to create template parts would not be strictly necessary because currently the capability to create template parts just depends on edit_theme_options, and if edit_theme_options is not available the site editor does not even load. But let us add the check anyway for template parts in case things change or some capability management plugin does something we are not expecting.
Testing Instructions
I pasted the following test code that removes the ability to create patterns (while still allowing to edit them).
I went to wp-admin/site-editor.php?postType=wp_block verified patterns loaded well and I could edit them, but the ability to create new patterns was not available.