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

Global Styles: allow read access to users with edit_posts capabilities #7336

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Sep 12, 2024

This PR syncs WordPress/gutenberg#65071

What

The patch allows any role that can

  1. edit a post, including custom post types
  2. edit theme options

to read global styles from the API and in the editor.

Why

Block style variations and other global styles are not available to non-admin editors. See WordPress/gutenberg#64755 and also read-only style book ideas in WordPress/gutenberg#41119

Furthermore, having global styles available in the post editor will, one day, allow block controls to reflect any values inherited from the theme/global styles. See: WordPress/gutenberg#64670

Testing

Manual testing in the editor is limited without package changes, however you can login using different roles (e.g., admin/editor) and try to fetch global styles in the console.

// Admin only
await wp.data.resolveSelect( 'core' ).getEntityRecord( 'root', 'globalStyles', await wp.data.resolveSelect( 'core' ).__experimentalGetCurrentGlobalStylesId(), { context: 'edit' } )
// Edit post/theme options
await wp.data.resolveSelect( 'core' ).getEntityRecord( 'root', 'globalStyles', await wp.data.resolveSelect( 'core' ).__experimentalGetCurrentGlobalStylesId(), { context: 'view' } )

Trac ticket: https://core.trac.wordpress.org/ticket/62042


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Left some notes inline.

As the global styles are essentially public via the HTML header's <style id='global-styles-inline-css'> element, it might be possible to use the read capability rather than worry too much about the ability to edit post.

Although it would extend the use of read beyond the purpose currently documented so it may be a terrible idea.

@@ -123,14 +123,6 @@ static function ( $classes ) {
'before'
);

// Set Heartbeat interval to 10 seconds, used to refresh post locks.
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks unintentional, are you able to check you forked from an up-to-date version of trunk?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh yeah, that's not supposed to be there. I'll rebase. Thanks for the heads up.

@ramonjd ramonjd force-pushed the try/extend-read-access-to-edit-capabilities-for-global-styles branch from 208cc45 to a3078eb Compare September 13, 2024 02:12
@@ -632,7 +626,7 @@ public function get_theme_items( $request ) {
);
}

$response = array();
$response = array();
Copy link
Member Author

Choose a reason for hiding this comment

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

Random space removed.

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Nittiest of nitty nit picks.

@ramonjd ramonjd force-pushed the try/extend-read-access-to-edit-capabilities-for-global-styles branch from 0f5c3e0 to b183c97 Compare September 18, 2024 00:29
Add edit_theme_options cap check for reading items with accompanying test coverage.

t
@ramonjd ramonjd force-pushed the try/extend-read-access-to-edit-capabilities-for-global-styles branch from a87926f to 790e8d1 Compare September 18, 2024 00:32
@ramonjd ramonjd self-assigned this Sep 18, 2024
@ramonjd ramonjd marked this pull request as ready for review September 18, 2024 00:40
Copy link

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly, peterwilsoncc, mukesh27.

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

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

These changes look good to me.

As the upstream pull request, WordPress/gutenberg#65071, includes JavaScript changes to implement the additional access I haven't been able to test that styles appear correctly when editing a post on a WordPress-Develop install.

However, I have been able to verify that the view context request does return the custom styles when logged in as a contributor.

Edit: To be clear, I can test the upstream PR and it works as expected.

@ramonjd
Copy link
Member Author

ramonjd commented Sep 18, 2024

Committed in r59048 / 52d46a9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants