| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global styles functions, filters and actions, etc. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Function that given a branch of the global styles object recursively generates |
| 10 |
* an array defining all the css vars that the global styles object represents. |
| 11 |
* |
| 12 |
* @param array $global_styles_branch Array representing a brach of the global styles object. |
| 13 |
* @param string $prefix Prefix to append to each variable. |
| 14 |
* @param bool $is_start Indicates if we are on the first call to gutenberg_get_css_vars (outside the recursion). |
| 15 |
* @return array An array whose keys are css variable names and whose values are the css variables value. |
| 16 |
*/ |
| 17 |
function gutenberg_get_css_vars( $global_styles_branch, $prefix = '', $is_start = true ) { |
| 18 |
$result = array(); |
| 19 |
foreach ( $global_styles_branch as $key => $value ) { |
| 20 |
$processed_key = str_replace( '/', '-', $key ); |
| 21 |
$separator = $is_start ? '' : '--'; |
| 22 |
$new_key = ( $prefix ? $prefix . $separator : '' ) . $processed_key; |
| 23 |
|
| 24 |
if ( is_array( $value ) ) { |
| 25 |
$result = array_merge( |
| 26 |
$result, |
| 27 |
gutenberg_get_css_vars( $value, $new_key, false ) |
| 28 |
); |
| 29 |
} else { |
| 30 |
$result[ $new_key ] = $value; |
| 31 |
} |
| 32 |
} |
| 33 |
return $result; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Function responsible for enqueuing the style that define the global styles css variables. |
| 38 |
*/ |
| 39 |
function gutenberg_enqueue_global_styles_assets() { |
| 40 |
if ( ! locate_template( 'experimental-theme.json' ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
$default_global_styles_path = dirname( dirname( __FILE__ ) ) . '/experimental-default-global-styles.json'; |
| 44 |
$default_global_styles = null; |
| 45 |
|
| 46 |
if ( file_exists( $default_global_styles_path ) ) { |
| 47 |
$default_global_styles = json_decode( |
| 48 |
file_get_contents( $default_global_styles_path ), |
| 49 |
true |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
$theme_json_path = locate_template( 'experimental-theme.json' ); |
| 54 |
$theme_json_global_styles = null; |
| 55 |
if ( $theme_json_path ) { |
| 56 |
$theme_json_global_styles = json_decode( |
| 57 |
file_get_contents( $theme_json_path ), |
| 58 |
true |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
// To-do: Load user customizations from a CPT. |
| 63 |
$css_vars = array(); |
| 64 |
foreach ( |
| 65 |
array( |
| 66 |
$default_global_styles, |
| 67 |
$theme_json_global_styles, |
| 68 |
) as $global_styles_definition |
| 69 |
) { |
| 70 |
if ( ! $global_styles_definition ) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
if ( isset( $global_styles_definition['global'] ) ) { |
| 74 |
$css_vars = array_merge( |
| 75 |
$css_vars, |
| 76 |
gutenberg_get_css_vars( $global_styles_definition['global'], '--wp-' ) |
| 77 |
); |
| 78 |
} |
| 79 |
if ( isset( $global_styles_definition['blocks'] ) ) { |
| 80 |
$css_vars = array_merge( |
| 81 |
$css_vars, |
| 82 |
gutenberg_get_css_vars( $global_styles_definition['blocks'], '--wp-block-' ) |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if ( empty( $css_vars ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$inline_style = ":root {\n"; |
| 92 |
foreach ( $css_vars as $var => $value ) { |
| 93 |
$inline_style .= "\t" . $var . ': ' . $value . ";\n"; |
| 94 |
} |
| 95 |
$inline_style .= '}'; |
| 96 |
|
| 97 |
wp_register_style( 'global-styles', false, array(), true, true ); |
| 98 |
wp_add_inline_style( 'global-styles', $inline_style ); |
| 99 |
wp_enqueue_style( 'global-styles' ); |
| 100 |
} |
| 101 |
add_action( 'enqueue_block_assets', 'gutenberg_enqueue_global_styles_assets' ); |
| 102 |
|
| 103 |
/** |
| 104 |
* Adds class wp-gs to the frontend body class if the theme defines a experimental-theme.json. |
| 105 |
* |
| 106 |
* @param array $classes Existing body classes. |
| 107 |
* @return array The filtered array of body classes. |
| 108 |
*/ |
| 109 |
function gutenberg_add_wp_gs_class_front_end( $classes ) { |
| 110 |
if ( locate_template( 'experimental-theme.json' ) ) { |
| 111 |
return array_merge( $classes, array( 'wp-gs' ) ); |
| 112 |
} |
| 113 |
return $classes; |
| 114 |
} |
| 115 |
add_filter( 'body_class', 'gutenberg_add_wp_gs_class_front_end' ); |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Adds class wp-gs to the block-editor body class if the theme defines a experimental-theme.json. |
| 120 |
* |
| 121 |
* @param string $classes Existing body classes separated by space. |
| 122 |
* @return string The filtered string of body classes. |
| 123 |
*/ |
| 124 |
function gutenberg_add_wp_gs_class_editor( $classes ) { |
| 125 |
global $current_screen; |
| 126 |
if ( $current_screen->is_block_editor() && locate_template( 'experimental-theme.json' ) ) { |
| 127 |
return $classes . ' wp-gs'; |
| 128 |
} |
| 129 |
return $classes; |
| 130 |
} |
| 131 |
add_filter( 'admin_body_class', 'gutenberg_add_wp_gs_class_editor' ); |
| 132 |
|