Directory

Moves "Patterns" command to site editor main navigation by bacoords · Pull Request #61416 · WordPress/gutenberg · GitHub
Skip to content
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

Moves "Patterns" command to site editor main navigation #61416

Merged

Conversation

bacoords
Copy link
Contributor

@bacoords bacoords commented May 6, 2024

What?

As a follow up to this comment, this PR moves the "Patterns" Command from admin-navigation-commands to site-editor-navigation-commands.

Why?

The cleans up the code needed for the admin commands file as well as provides consistency in the navigation experience. Now the initial list of commands exactly matches the global navigation in the site editor.

It's worth noting that this removes the command from non-administrator users, even if they could technically access edit.php?post_type=wp_block and be directed there.

That said there are a number of commands that should be added for non-admin users that could send them to the non-site editor screens and that's really outside the scope of this PR.

How?

Testing Instructions

  1. Open the site editor.
  2. Click cmd-k to trigger the Command Palette
  3. Start searching for Patterns
  4. Select Patterns and confirm you're navigated to the correct screen

Testing Instructions for Keyboard

Screenshots or screencast

Screenshot 2024-05-06 at 3 21 56 PM

Copy link

github-actions bot commented May 6, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: bacoords <bacoords@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think the original Patterns command was available to all themes and to all users. With this PR, if you cannot access the site editor or are not using a block theme, you will not be able to use the Patterns commands.

My understanding is that this PR is a refactoring and I think the previous behavior should be fully preserved.

In my opinion, we need an approach like below.

function useSiteEditorBasicNavigationCommands() {
	const commands = useMemo( () => {
		const result = [];

		if ( isTemplatesAccessible && isBlockBasedTheme ) {
			// Command for when the current theme is a block theme and the user has access to the site editor
			Navigation, Styles, Pages, Templates
		}

		result.push( {
			...
			callback: ( { close } ) => {
				// First, check if the current user has access to the site editor.
				if ( isTemplatesAccessible ) {
					const args = {
						path: '/patterns',
					};
					if ( isSiteEditor ) {
						// If you are already in the site editor
						history.push( args );
					} else {
						// If you are outside the site editor
						document.location = addQueryArgs( 'site-editor.php', args );
					}
					close();
				} else {
					// If a user cannot access the site editor
					document.location.href = 'edit.php?post_type=wp_block';
				}
			},
		} );

		return result;
	}, [] );

	return {
		commands,
		isLoading: false,
	};
}

@@ -338,6 +339,24 @@ function useSiteEditorBasicNavigationCommands() {
},
} );

result.push( {
name: 'core/manage-reusable-blocks',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: 'core/manage-reusable-blocks',
name: 'core/edit-site/open-patterns',

There are no hard and fast rules regarding this property, but it is a good idea to make it consistent with other commands.

@bacoords
Copy link
Contributor Author

bacoords commented May 7, 2024

Thanks for the feedback @t-hamano ! I've updated the PR but the main issue I've had is that .canUser( 'read', 'templates' ) was returning true for "Editor" level users in the block editor so they were sent to the Site Editor, (perhaps because they're able to "read" and pull in the templates for the template switching and preview functionality)?

I added an additional hook based on .canUser( 'create', 'templates' ) to better determine if the user is able to open the site editor or should be sent to the original admin screen for Patterns, but would love some testing on this.

@t-hamano
Copy link
Contributor

t-hamano commented May 7, 2024

the main issue I've had is that .canUser( 'read', 'templates' ) was returning true for "Editor" level users in the block editor so they were sent to the Site Editor

After researching this issue, I believe that returning true is an unintended regression. See #61419.

I think we need to fix this permission issue before this PR and #61287.

@t-hamano t-hamano self-requested a review May 8, 2024 08:32
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue with user permissions was fixed in #61444. Could you rebase this branch with the latest trunk?

Comment on lines 265 to 266
const isTemplatesAccessible = useIsTemplatesAccessible();
const isTemplatesEditable = useIsTemplatesEditable();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this discussion, it seems better not to create custom hooks. You will only need to check whether it is possible to create a template, as shown below.

const canCreateTemplate = useSelect( ( select ) => {
	return select( coreStore ).canUser( 'create', 'templates' );
}, [] );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - this makes sense and uses the same canCreateTemplate value to determine whether the user should be sent to the classic admin screen for patterns or to the site editor's Pattern screen.

@bacoords
Copy link
Contributor Author

bacoords commented May 8, 2024

Could you rebase this branch with the latest trunk?

This has been rebased.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! All commands are working as before.

Admin, Block theme

  • Navigation, Styles, Pages, Templates: displayed
  • Patterns: wp-admin/site-editor.php?path=/patterns

Editor, Block theme

  • Navigation, Styles, Pages, Templates: Not displayed
  • Pattern: > wp-admin/edit.php?post_type=wp_block

Admin, Classic theme

  • Navigation, Styles, Pages, Templates: Not displayed
  • Pattern: > wp-admin/site-editor.php?path=/patterns

Editor, Classic theme

  • Navigation, Styles, Pages, Templates: Not displayed
  • Pattern: > wp-admin/edit.php?post_type=wp_block

@t-hamano t-hamano merged commit c3f74da into WordPress:trunk May 9, 2024
62 of 63 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.4 milestone May 9, 2024
@ajlende ajlende added [Type] Enhancement A suggestion for improvement. [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Package] Core commands labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Package] Core commands [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants