Site Editor Navigation Commands: Add permission check#63798
Conversation
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { useSelect } from '@wordpress/data'; | ||
|
|
||
| export function useIsBlockBasedTheme() { |
There was a problem hiding this comment.
Since this hook was not exposed and felt redundant, I removed it and switched to an ad-hoc approach.
|
Size Change: +24 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
|
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. |
| select( coreStore ).getCurrentTheme()?.is_block_theme, | ||
| canCreateTemplate: select( coreStore ).canUser( 'create', { | ||
| kind: 'postType', | ||
| name: 'wp_template', |
There was a problem hiding this comment.
Should we be checking "postType" here rather than wp_template
There was a problem hiding this comment.
If we change this to postType, it won't work as intended. For example, non-admin users will be able to create Pages/Posts but won't have access to the site editor.
There was a problem hiding this comment.
Maybe there's something I'm missing. Oh so the links go to the site editor? Maybe the links should go to the post editor in that case (if you're not already in the site editor)?
| if ( | ||
| ! canCreateTemplate || | ||
| postType === 'post' || | ||
| ( postType === 'page' && ! isBlockBasedTheme ) |
There was a problem hiding this comment.
Why do we need the custom postType checks here? I know it's pre-existent but maybe we can remove this with the canCreateCheck (if it relies on post type)
There was a problem hiding this comment.
I think this is because the site editor only has the page post type screen yet - if the post type is post, we need to link to the post editor and not the site editor, even if it's a block theme or admin user.
|
I confirm the fix works but I feel like we should make it more generic (check postType) |
| ! isBlockBasedTheme && | ||
| ! templateType === 'wp_template_part' | ||
| ! canCreateTemplate || | ||
| ( ! isBlockBasedTheme && ! templateType === 'wp_template_part' ) |
There was a problem hiding this comment.
I actually wonder if this second condition is needed.
There was a problem hiding this comment.
( ! isBlockBasedTheme && ! templateType === 'wp_template_part' )
This may be an edge case, but it's here to prevent themes like the one below from unintentionally accessing the site editor.
- The theme has template part files (e.g.
parts/header.html) - But the theme is not opted in to
add_theme_support( 'block-template-parts' )
If a theme does not support block-template-parts, the theme will not have access to the site editor and should not expose template parts in the commands.
youknowriad
left a comment
There was a problem hiding this comment.
Approving this PR. That said, I think there are some improvements and simplifications we can do:
- If you're in the post editor, just navigate to the post editor for all entities that supports it (everything but patterns, template, template part, navigation).
- If you're in the site editor, navigate to the site editor for all entities that support it.
I guess this is a temporary issue until we merge post and site editors entirely
|
Thanks for the review!
I agree with this and would like to discover and fix the inconsistencies in a follow-up. |
Fixes #62460
What?
This PR prevents users without
edit_theme_optionspermissions from unintentionally seeing commands related to accessing the site editor.The commands that are displayed unintentionally are Page, Template, and Template Part. Users without the
edit_theme_optionscapability don't have access to the site editor, so they will get a warning when running these commands:Why?
The current command palette checks whether the site editor is accessible via code like the following:
However, this check did not exist for the three commands mentioned above.
How?
Check whether the template can be created, i.e. whether the site editor is accessible.
If the site editor is not accessible, I either link to the post editor or don't display the command at all.
Whether a template can be created and whether the site editor is accessible may not necessarily coincide in the future, but I believe this is the best approach for now.
Testing Instructions
Note: The scenarios marked with ✅ mark are the ones that will be fixed by this PR.
Hello World(post) > Post EditorSample page(page) > Post EditorHello World(post) > Post EditorSample page(page) > Site EditorBlog Home(template) > Site EditorPost Meta(template part) > Site EditorHello World(post) > Post EditorSample page(page) > Post EditorBlog Home(template) > Do not showPost Meta(template part) > Do not show