Add: Automatic editor styles generation #18028
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes: #17860
This PR implements a mechanism to auto-generate colors and font sizes styles (and gradients will also support this when merged).
The API is the following:
The block editor adds the styles in an inline style element. This behavior is far from perfect because the browser does not cache the styles. WordPress passes these styles to the browser in every page request. I checked how the customizer loads the custom user style sheet -- It also uses inline styles. Themes that allow the user to dynamically change colors (e.g: 2019) also load the styles in inline styles. More recently, the block styles server API relied on the same system.
I think it would be good if the core could offer an alternative used in these and other similar cases.
I thought about some possibilities:
Create an endpoint that outputs CSS with a never expiring cache header. We would generate a hash of the styles and append it to the URL. When the styles change (e.g: the theme has set dynamic colors depending on the post type ) even though the caching was never expiring, the browser will make a new request because the URL is different.
The problem with this approach is that during the first visit, we load WordPress 2 times one for generating the HTML page and another one to generate the styles. If the styles are very dynamic, the cache would also not be useful, and WordPress may end up loading many times during the visit of a user.
Generate a hash of the styles and verify if a CSS file exists on the file system for that hash; if not, WordPress generates the styles and writes them to the file-system.
Then we enqueue a style sheet that references the file in the file system.
In this approach, we load WordPress one time, but we add an overhead on the loading: hashing things, check the file system, maybe write to the filesystem.
The styles may be very dynamic (e.g., a theme may change colors depending on the day of the month). We would need logic to clean old style files and to decide what is considered old. Another problem is that WordPress may not have permission to write files in some cases, and there are also certainly security concerns when writing files, so this option does not seem very viable.
The possibilities I thought are not perfect either, so this PR follows the typical approach used until now.
How has this been tested?
I added a color palette:
I added editor font sizes:
I enabled __experimental-editor-automatic-styles theme support:
I verified that the block editor added the styles for the color pallet and font sizes on the editor and the frontend. We can find the styles by searching on the pages source code for
wp-editor-automatic-styles-inline-css
.I enabled __experimental-editor-automatic-styles theme support just for font sizes.
I verified that the block editor added the styles for the font sizes on the editor and the frontend. We can find the styles by searching on the pages source code for
wp-editor-automatic-styles-inline-css
.