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

Custom CSS: add option to disable in theme.json #50761

Open
glendaviesnz opened this issue May 19, 2023 · 5 comments
Open

Custom CSS: add option to disable in theme.json #50761

glendaviesnz opened this issue May 19, 2023 · 5 comments
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement.

Comments

@glendaviesnz
Copy link
Contributor

What problem does this address?

As noted here, currently it is not possible to disable the global styles custom css via theme.json.

What is your proposed solution?

Add the option to set something like

{
    "styles": {
        "css": false
    }
}

and remove the option from the site editor if it is set to false.

In the mean time it is possible to disable it for some or all users with the following php:

add_filter( 'map_meta_cap', 'remove_edit_custom_css_cap', 10, 3 );

function remove_edit_custom_css_cap( $caps, $cap, $user_id ) {

	if ( 'edit_css' === $cap ) {
		$caps = array( 'do_not_allow' );
	}

	return $caps;
}
@glendaviesnz glendaviesnz added [Type] Enhancement A suggestion for improvement. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels May 19, 2023
@carolinan
Copy link
Contributor

Is there a use case for this, that can't be covered by user permissions? Wouldn't this turn it off for all users including admin?

@glendaviesnz
Copy link
Contributor Author

glendaviesnz commented May 21, 2023

Is there a use case for this, that can't be covered by user permissions? Wouldn't this turn it off for all users including admin?

I am not sure. I guess there could be instances in some more heavily locked down corporate sites, etc. where you don't want anyone adding additional custom CSS via the admin interface. Many of the other global styles can be disabled via theme.json, so allowing it for custom CSS makes sense in terms of consistency.

@spencerm
Copy link

Is there a use case for this, that can't be covered by user permissions? Wouldn't this turn it off for all users including admin?

Hi, I was the original commenter! :)

So yes I have admin users and want to block them from adding CSS to a block. Yes I could make a user role that's somewhere between editor & admin, but easier and more readable in my opinion to just disable in the theme.json.

I could also see a theme author with a very stylized, highly opinionated theme and they want to block users from adding CSS to say the heading block which would break precise block patterns in unexpected ways.

@colorful-tones
Copy link
Member

I thought I would take a stab at some discovery here. I'm likely in over my head 😄

Some items worth discussing:

Location of the new setting in theme.json

Proposed entry:

{
    "styles": {
        "css": false
    }
}

Possible alternative:

{
    "settings": {
        "css": false
    }
}

Another possible alternative:

{
    "css": false
}

Considerations for nested overrides

Would it be possible for a single block to enable custom CSS even if the theme.json has styles.css: false?

An example:

{
    "settings": {
        "css": false,
        "blocks": {
            "block/button": {
                "css": true
            }
        }
    },
    "styles": {
        "blocks": {
            "core/button": {
                "css": "& :where(svg) {fill:currentColor;}"
            }
        }
    }
}

Or, perhaps the single block's override is implied. So, once the styles.blocks.core/button.css is added then the custom CSS for that block is enabled and recognized.

{
    "settings": {
        "css": false
    },
    "styles": {
        "blocks": {
            "core/button": {
                "css": "& :where(svg) {fill:currentColor;}"
            }
        }
    }
}

However, I feel like the first example with settings.blocks.core/button.css: true is likely a better implementation, because we're talking about enabling/disabling the Custom CSS in the site editor as well. This could potentially be a way for theme authors to disable Custom CSS, but re-enable it for specific blocks only. 🤔

Perhaps all of this is out of scope for this particular task and might even overlap with some of the items on #53326, and I just wanted to mention it as I started to explore some discoveries. Thanks

@colorful-tones
Copy link
Member

colorful-tones commented Sep 14, 2023

I discussed this briefly with @Mamaduka and he provided me some areas to explore in the codebase that could possibly be extended or referenced for any of this work. He mentioned that he had worked on some of the user capabilities for Custom CSS implementation and pointed me to search for wp:action-edit-css in the codebase, or https://github.com/search?q=repo%3AWordPress%2Fgutenberg+%22wp%3Aaction-edit-css%22&type=code

Another possible consideration would be elevating the work to include a new set of capabilities for the site editor, which would then control parts of its UI. That is likely out of scope though for this particular ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants