| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraps Global Styles. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns the theme presets registered via add_theme_support, if any. |
| 10 |
* |
| 11 |
* @param array $settings Existing editor settings. |
| 12 |
* |
| 13 |
* @return array Config that adheres to the theme.json schema. |
| 14 |
*/ |
| 15 |
function gutenberg_experimental_global_styles_get_theme_support_settings( $settings ) { |
| 16 |
$all_blocks = WP_Theme_JSON::ALL_BLOCKS_NAME; |
| 17 |
$theme_settings = array(); |
| 18 |
$theme_settings['settings'] = array(); |
| 19 |
$theme_settings['settings'][ $all_blocks ] = array(); |
| 20 |
|
| 21 |
// Deprecated theme supports. |
| 22 |
if ( isset( $settings['disableCustomColors'] ) ) { |
| 23 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) { |
| 24 |
$theme_settings['settings'][ $all_blocks ]['color'] = array(); |
| 25 |
} |
| 26 |
$theme_settings['settings'][ $all_blocks ]['color']['custom'] = ! $settings['disableCustomColors']; |
| 27 |
} |
| 28 |
|
| 29 |
if ( isset( $settings['disableCustomGradients'] ) ) { |
| 30 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) { |
| 31 |
$theme_settings['settings'][ $all_blocks ]['color'] = array(); |
| 32 |
} |
| 33 |
$theme_settings['settings'][ $all_blocks ]['color']['customGradient'] = ! $settings['disableCustomGradients']; |
| 34 |
} |
| 35 |
|
| 36 |
if ( isset( $settings['disableCustomFontSizes'] ) ) { |
| 37 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['typography'] ) ) { |
| 38 |
$theme_settings['settings'][ $all_blocks ]['typography'] = array(); |
| 39 |
} |
| 40 |
$theme_settings['settings'][ $all_blocks ]['typography']['customFontSize'] = ! $settings['disableCustomFontSizes']; |
| 41 |
} |
| 42 |
|
| 43 |
if ( isset( $settings['enableCustomLineHeight'] ) ) { |
| 44 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['typography'] ) ) { |
| 45 |
$theme_settings['settings'][ $all_blocks ]['typography'] = array(); |
| 46 |
} |
| 47 |
$theme_settings['settings'][ $all_blocks ]['typography']['customLineHeight'] = $settings['enableCustomLineHeight']; |
| 48 |
} |
| 49 |
|
| 50 |
if ( isset( $settings['enableCustomUnits'] ) ) { |
| 51 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['spacing'] ) ) { |
| 52 |
$theme_settings['settings'][ $all_blocks ]['spacing'] = array(); |
| 53 |
} |
| 54 |
$theme_settings['settings'][ $all_blocks ]['spacing']['units'] = ( true === $settings['enableCustomUnits'] ) ? |
| 55 |
array( 'px', 'em', 'rem', 'vh', 'vw' ) : |
| 56 |
$settings['enableCustomUnits']; |
| 57 |
} |
| 58 |
|
| 59 |
if ( isset( $settings['colors'] ) ) { |
| 60 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) { |
| 61 |
$theme_settings['settings'][ $all_blocks ]['color'] = array(); |
| 62 |
} |
| 63 |
$theme_settings['settings'][ $all_blocks ]['color']['palette'] = $settings['colors']; |
| 64 |
} |
| 65 |
|
| 66 |
if ( isset( $settings['gradients'] ) ) { |
| 67 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) { |
| 68 |
$theme_settings['settings'][ $all_blocks ]['color'] = array(); |
| 69 |
} |
| 70 |
$theme_settings['settings'][ $all_blocks ]['color']['gradients'] = $settings['gradients']; |
| 71 |
} |
| 72 |
|
| 73 |
if ( isset( $settings['fontSizes'] ) ) { |
| 74 |
$font_sizes = $settings['fontSizes']; |
| 75 |
// Back-compatibility for presets without units. |
| 76 |
foreach ( $font_sizes as &$font_size ) { |
| 77 |
if ( is_numeric( $font_size['size'] ) ) { |
| 78 |
$font_size['size'] = $font_size['size'] . 'px'; |
| 79 |
} |
| 80 |
} |
| 81 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['typography'] ) ) { |
| 82 |
$theme_settings['settings'][ $all_blocks ]['typography'] = array(); |
| 83 |
} |
| 84 |
$theme_settings['settings'][ $all_blocks ]['typography']['fontSizes'] = $font_sizes; |
| 85 |
} |
| 86 |
|
| 87 |
// This allows to make the plugin work with WordPress 5.7 beta |
| 88 |
// as well as lower versions. The second check can be removed |
| 89 |
// as soon as the minimum WordPress version for the plugin |
| 90 |
// is bumped to 5.7. |
| 91 |
if ( isset( $settings['enableCustomSpacing'] ) ) { |
| 92 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['spacing'] ) ) { |
| 93 |
$theme_settings['settings'][ $all_blocks ]['spacing'] = array(); |
| 94 |
} |
| 95 |
$theme_settings['settings'][ $all_blocks ]['spacing']['customPadding'] = $settings['enableCustomSpacing']; |
| 96 |
} elseif ( current( (array) get_theme_support( 'custom-spacing' ) ) ) { |
| 97 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['spacing'] ) ) { |
| 98 |
$theme_settings['settings'][ $all_blocks ]['spacing'] = array(); |
| 99 |
} |
| 100 |
$theme_settings['settings'][ $all_blocks ]['spacing']['customPadding'] = true; |
| 101 |
} |
| 102 |
|
| 103 |
// Things that didn't land in core yet, so didn't have a setting assigned. |
| 104 |
if ( current( (array) get_theme_support( 'experimental-link-color' ) ) ) { |
| 105 |
if ( ! isset( $theme_settings['settings'][ $all_blocks ]['color'] ) ) { |
| 106 |
$theme_settings['settings'][ $all_blocks ]['color'] = array(); |
| 107 |
} |
| 108 |
$theme_settings['settings'][ $all_blocks ]['color']['link'] = true; |
| 109 |
} |
| 110 |
|
| 111 |
return $theme_settings; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Takes a tree adhering to the theme.json schema and generates |
| 116 |
* the corresponding stylesheet. |
| 117 |
* |
| 118 |
* @param WP_Theme_JSON $tree Input tree. |
| 119 |
* @param string $type Type of stylesheet we want accepts 'all', 'block_styles', and 'css_variables'. |
| 120 |
* |
| 121 |
* @return string Stylesheet. |
| 122 |
*/ |
| 123 |
function gutenberg_experimental_global_styles_get_stylesheet( $tree, $type = 'all' ) { |
| 124 |
// Check if we can use cached. |
| 125 |
$can_use_cached = ( |
| 126 |
( 'all' === $type ) && |
| 127 |
( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && |
| 128 |
( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && |
| 129 |
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && |
| 130 |
! is_admin() |
| 131 |
); |
| 132 |
|
| 133 |
if ( $can_use_cached ) { |
| 134 |
// Check if we have the styles already cached. |
| 135 |
$cached = get_transient( 'global_styles' ); |
| 136 |
if ( $cached ) { |
| 137 |
return $cached; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$stylesheet = $tree->get_stylesheet( $type ); |
| 142 |
|
| 143 |
if ( ( 'all' === $type || 'block_styles' === $type ) && WP_Theme_JSON_Resolver::theme_has_support() ) { |
| 144 |
// To support all themes, we added in the block-library stylesheet |
| 145 |
// a style rule such as .has-link-color a { color: var(--wp--style--color--link, #00e); } |
| 146 |
// so that existing link colors themes used didn't break. |
| 147 |
// We add this here to make it work for themes that opt-in to theme.json |
| 148 |
// In the future, we may do this differently. |
| 149 |
$stylesheet .= 'a{color:var(--wp--style--color--link, #00e);}'; |
| 150 |
} |
| 151 |
|
| 152 |
if ( $can_use_cached ) { |
| 153 |
// Cache for a minute. |
| 154 |
// This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites. |
| 155 |
set_transient( 'global_styles', $stylesheet, MINUTE_IN_SECONDS ); |
| 156 |
} |
| 157 |
|
| 158 |
return $stylesheet; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Fetches the preferences for each origin (core, theme, user) |
| 163 |
* and enqueues the resulting stylesheet. |
| 164 |
*/ |
| 165 |
function gutenberg_experimental_global_styles_enqueue_assets() { |
| 166 |
if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$settings = gutenberg_get_common_block_editor_settings(); |
| 171 |
$theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); |
| 172 |
|
| 173 |
$all = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data ); |
| 174 |
|
| 175 |
$stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all ); |
| 176 |
if ( empty( $stylesheet ) ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
wp_register_style( 'global-styles', false, array(), true, true ); |
| 181 |
wp_add_inline_style( 'global-styles', $stylesheet ); |
| 182 |
wp_enqueue_style( 'global-styles' ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Adds the necessary data for the Global Styles client UI to the block settings. |
| 187 |
* |
| 188 |
* @param array $settings Existing block editor settings. |
| 189 |
* @return array New block editor settings |
| 190 |
*/ |
| 191 |
function gutenberg_experimental_global_styles_settings( $settings ) { |
| 192 |
$theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings ); |
| 193 |
unset( $settings['colors'] ); |
| 194 |
unset( $settings['disableCustomColors'] ); |
| 195 |
unset( $settings['disableCustomFontSizes'] ); |
| 196 |
unset( $settings['disableCustomGradients'] ); |
| 197 |
unset( $settings['enableCustomLineHeight'] ); |
| 198 |
unset( $settings['enableCustomUnits'] ); |
| 199 |
unset( $settings['enableCustomSpacing'] ); |
| 200 |
unset( $settings['fontSizes'] ); |
| 201 |
unset( $settings['gradients'] ); |
| 202 |
|
| 203 |
$origin = 'theme'; |
| 204 |
if ( |
| 205 |
WP_Theme_JSON_Resolver::theme_has_support() && |
| 206 |
gutenberg_is_fse_theme() |
| 207 |
) { |
| 208 |
// Only lookup for the user data if we need it. |
| 209 |
$origin = 'user'; |
| 210 |
} |
| 211 |
$tree = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, $origin ); |
| 212 |
|
| 213 |
// STEP 1: ADD FEATURES |
| 214 |
// |
| 215 |
// These need to be always added to the editor settings, |
| 216 |
// even for themes that don't support theme.json. |
| 217 |
// An example of this is that the presets are configured |
| 218 |
// from the theme support data. |
| 219 |
$settings['__experimentalFeatures'] = $tree->get_settings(); |
| 220 |
|
| 221 |
// STEP 2 - IF EDIT-SITE, ADD DATA REQUIRED FOR GLOBAL STYLES SIDEBAR |
| 222 |
// |
| 223 |
// In the site editor, the user can change styles, so the client |
| 224 |
// needs the ability to create them. Hence, we pass it some data |
| 225 |
// for this: base styles (core+theme) and the ID of the user CPT. |
| 226 |
$screen = get_current_screen(); |
| 227 |
if ( |
| 228 |
! empty( $screen ) && |
| 229 |
function_exists( 'gutenberg_is_edit_site_page' ) && |
| 230 |
gutenberg_is_edit_site_page( $screen->id ) && |
| 231 |
WP_Theme_JSON_Resolver::theme_has_support() && |
| 232 |
gutenberg_is_fse_theme() |
| 233 |
) { |
| 234 |
$user_cpt_id = WP_Theme_JSON_Resolver::get_user_custom_post_type_id(); |
| 235 |
$base_styles = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, 'theme' )->get_raw_data(); |
| 236 |
|
| 237 |
$settings['__experimentalGlobalStylesUserEntityId'] = $user_cpt_id; |
| 238 |
$settings['__experimentalGlobalStylesBaseStyles'] = $base_styles; |
| 239 |
} elseif ( WP_Theme_JSON_Resolver::theme_has_support() ) { |
| 240 |
// STEP 3 - ADD STYLES IF THEME HAS SUPPORT |
| 241 |
// |
| 242 |
// If we are in a block editor context, but not in edit-site, |
| 243 |
// we add the styles via the settings, so the editor knows that |
| 244 |
// some of these should be added the wrapper class, |
| 245 |
// as if they were added via add_editor_styles. |
| 246 |
$settings['styles'][] = array( |
| 247 |
'css' => gutenberg_experimental_global_styles_get_stylesheet( $tree, 'css_variables' ), |
| 248 |
'__experimentalNoWrapper' => true, |
| 249 |
); |
| 250 |
$settings['styles'][] = array( |
| 251 |
'css' => gutenberg_experimental_global_styles_get_stylesheet( $tree, 'block_styles' ), |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
return $settings; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Register CPT to store/access user data. |
| 260 |
* |
| 261 |
* @return array|undefined |
| 262 |
*/ |
| 263 |
function gutenberg_experimental_global_styles_register_user_cpt() { |
| 264 |
if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
WP_Theme_JSON_Resolver::register_user_custom_post_type(); |
| 269 |
} |
| 270 |
|
| 271 |
add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' ); |
| 272 |
add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); |
| 273 |
add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Sanitizes global styles user content removing unsafe rules. |
| 278 |
* |
| 279 |
* @param string $content Post content to filter. |
| 280 |
* @return string Filtered post content with unsafe rules removed. |
| 281 |
*/ |
| 282 |
function gutenberg_global_styles_filter_post( $content ) { |
| 283 |
$decoded_data = json_decode( stripslashes( $content ), true ); |
| 284 |
$json_decoding_error = json_last_error(); |
| 285 |
if ( |
| 286 |
JSON_ERROR_NONE === $json_decoding_error && |
| 287 |
is_array( $decoded_data ) && |
| 288 |
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && |
| 289 |
$decoded_data['isGlobalStylesUserThemeJSON'] |
| 290 |
) { |
| 291 |
unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); |
| 292 |
$theme_json = new WP_Theme_JSON( $decoded_data ); |
| 293 |
$theme_json->remove_insecure_properties(); |
| 294 |
$data_to_encode = $theme_json->get_raw_data(); |
| 295 |
$data_to_encode['isGlobalStylesUserThemeJSON'] = true; |
| 296 |
return wp_json_encode( $data_to_encode ); |
| 297 |
} |
| 298 |
return $content; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Adds the filters to filter global styles user theme.json. |
| 303 |
*/ |
| 304 |
function gutenberg_global_styles_kses_init_filters() { |
| 305 |
add_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Removes the filters to filter global styles user theme.json. |
| 310 |
*/ |
| 311 |
function gutenberg_global_styles_kses_remove_filters() { |
| 312 |
remove_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Register global styles kses filters if the user does not have unfiltered_html capability. |
| 317 |
* |
| 318 |
* @uses render_block_core_navigation() |
| 319 |
* @throws WP_Error An WP_Error exception parsing the block definition. |
| 320 |
*/ |
| 321 |
function gutenberg_global_styles_kses_init() { |
| 322 |
gutenberg_global_styles_kses_remove_filters(); |
| 323 |
if ( ! current_user_can( 'unfiltered_html' ) ) { |
| 324 |
gutenberg_global_styles_kses_init_filters(); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* This filter is the last being executed on force_filtered_html_on_import. |
| 330 |
* If the input of the filter is true it means we are in an import situation and should |
| 331 |
* enable kses, independently of the user capabilities. |
| 332 |
* So in that case we call gutenberg_global_styles_kses_init_filters; |
| 333 |
* |
| 334 |
* @param string $arg Input argument of the filter. |
| 335 |
* @return string Exactly what was passed as argument. |
| 336 |
*/ |
| 337 |
function gutenberg_global_styles_force_filtered_html_on_import_filter( $arg ) { |
| 338 |
// force_filtered_html_on_import is true we need to init the global styles kses filters. |
| 339 |
if ( $arg ) { |
| 340 |
gutenberg_global_styles_kses_init_filters(); |
| 341 |
} |
| 342 |
return $arg; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* This filter is the last being executed on force_filtered_html_on_import. |
| 347 |
* If the input of the filter is true it means we are in an import situation and should |
| 348 |
* enable kses, independently of the user capabilities. |
| 349 |
* So in that case we call gutenberg_global_styles_kses_init_filters; |
| 350 |
* |
| 351 |
* @param bool $allow_css Whether the CSS in the test string is considered safe. |
| 352 |
* @param bool $css_test_string The CSS string to test.. |
| 353 |
* @return bool If $allow_css is true it returns true. |
| 354 |
* If $allow_css is false and the CSS rule is referencing a WordPress css variable it returns true. |
| 355 |
* Otherwise the function return false. |
| 356 |
*/ |
| 357 |
function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $css_test_string ) { |
| 358 |
if ( $allow_css ) { |
| 359 |
return $allow_css; |
| 360 |
} |
| 361 |
$allowed_preset_attributes = array( |
| 362 |
'background', |
| 363 |
'background-color', |
| 364 |
'color', |
| 365 |
'font-family', |
| 366 |
'font-size', |
| 367 |
); |
| 368 |
$parts = explode( ':', $css_test_string, 2 ); |
| 369 |
|
| 370 |
if ( ! in_array( trim( $parts[0] ), $allowed_preset_attributes, true ) ) { |
| 371 |
return $allow_css; |
| 372 |
} |
| 373 |
return ! ! preg_match( '/^var\(--wp-[a-zA-Z0-9\-]+\)$/', trim( $parts[1] ) ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
add_action( 'init', 'gutenberg_global_styles_kses_init' ); |
| 378 |
add_action( 'set_current_user', 'gutenberg_global_styles_kses_init' ); |
| 379 |
add_filter( 'force_filtered_html_on_import', 'gutenberg_global_styles_force_filtered_html_on_import_filter', 999 ); |
| 380 |
add_filter( 'safecss_filter_attr_allow_css', 'gutenberg_global_styles_include_support_for_wp_variables', 10, 2 ); |
| 381 |
// This filter needs to be executed last. |
| 382 |
|
| 383 |
|