| 1 |
<?php |
| 2 |
/** |
| 3 |
* Adds settings to the block editor. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Replaces core 'styles' and '__experimentalFeatures' block editor settings from |
| 10 |
* wordpress-develop/block-editor.php with the Gutenberg versions. Much of the |
| 11 |
* code is copied from get_block_editor_settings() in that file. |
| 12 |
* |
| 13 |
* This hook should run first as it completely replaces the core settings that |
| 14 |
* other hooks may need to update. |
| 15 |
* |
| 16 |
* Note: The settings that are WP version specific should be handled inside the `compat` directory. |
| 17 |
* |
| 18 |
* @param array $settings Existing block editor settings. |
| 19 |
* |
| 20 |
* @return array New block editor settings. |
| 21 |
*/ |
| 22 |
function gutenberg_get_block_editor_settings( $settings ) { |
| 23 |
$global_styles = array(); |
| 24 |
$presets = array( |
| 25 |
array( |
| 26 |
'css' => 'variables', |
| 27 |
'__unstableType' => 'presets', |
| 28 |
'isGlobalStyles' => true, |
| 29 |
), |
| 30 |
array( |
| 31 |
'css' => 'presets', |
| 32 |
'__unstableType' => 'presets', |
| 33 |
'isGlobalStyles' => true, |
| 34 |
), |
| 35 |
); |
| 36 |
foreach ( $presets as $preset_style ) { |
| 37 |
$actual_css = gutenberg_get_global_stylesheet( array( $preset_style['css'] ) ); |
| 38 |
if ( '' !== $actual_css ) { |
| 39 |
$preset_style['css'] = $actual_css; |
| 40 |
$global_styles[] = $preset_style; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if ( wp_theme_has_theme_json() ) { |
| 45 |
$block_classes = array( |
| 46 |
'css' => 'styles', |
| 47 |
'__unstableType' => 'theme', |
| 48 |
'isGlobalStyles' => true, |
| 49 |
); |
| 50 |
$actual_css = gutenberg_get_global_stylesheet( array( $block_classes['css'] ) ); |
| 51 |
if ( '' !== $actual_css ) { |
| 52 |
$block_classes['css'] = $actual_css; |
| 53 |
$global_styles[] = $block_classes; |
| 54 |
} |
| 55 |
|
| 56 |
// Get any additional css from the customizer and add it before global styles custom CSS. |
| 57 |
$global_styles[] = array( |
| 58 |
'css' => wp_get_custom_css(), |
| 59 |
'__unstableType' => 'user', |
| 60 |
'isGlobalStyles' => false, |
| 61 |
); |
| 62 |
|
| 63 |
/* |
| 64 |
* Add the custom CSS as a separate stylesheet so any invalid CSS |
| 65 |
* entered by users does not break other global styles. |
| 66 |
*/ |
| 67 |
$global_styles[] = array( |
| 68 |
'css' => gutenberg_get_global_stylesheet( array( 'custom-css' ) ), |
| 69 |
'__unstableType' => 'user', |
| 70 |
'isGlobalStyles' => true, |
| 71 |
); |
| 72 |
} else { |
| 73 |
// If there is no `theme.json` file, ensure base layout styles are still available. |
| 74 |
$block_classes = array( |
| 75 |
'css' => 'base-layout-styles', |
| 76 |
'__unstableType' => 'base-layout', |
| 77 |
'isGlobalStyles' => true, |
| 78 |
); |
| 79 |
$actual_css = gutenberg_get_global_stylesheet( array( $block_classes['css'] ) ); |
| 80 |
if ( '' !== $actual_css ) { |
| 81 |
$block_classes['css'] = $actual_css; |
| 82 |
$global_styles[] = $block_classes; |
| 83 |
} |
| 84 |
// Get any additional css from the customizer. |
| 85 |
$global_styles[] = array( |
| 86 |
'css' => wp_get_custom_css(), |
| 87 |
'__unstableType' => 'user', |
| 88 |
'isGlobalStyles' => false, |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
$settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); |
| 93 |
|
| 94 |
$settings['__experimentalFeatures'] = gutenberg_get_global_settings(); |
| 95 |
// These settings may need to be updated based on data coming from theme.json sources. |
| 96 |
if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) { |
| 97 |
$colors_by_origin = $settings['__experimentalFeatures']['color']['palette']; |
| 98 |
$settings['colors'] = isset( $colors_by_origin['custom'] ) ? |
| 99 |
$colors_by_origin['custom'] : ( |
| 100 |
isset( $colors_by_origin['theme'] ) ? |
| 101 |
$colors_by_origin['theme'] : |
| 102 |
$colors_by_origin['default'] |
| 103 |
); |
| 104 |
} |
| 105 |
if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) { |
| 106 |
$gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients']; |
| 107 |
$settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? |
| 108 |
$gradients_by_origin['custom'] : ( |
| 109 |
isset( $gradients_by_origin['theme'] ) ? |
| 110 |
$gradients_by_origin['theme'] : |
| 111 |
$gradients_by_origin['default'] |
| 112 |
); |
| 113 |
} |
| 114 |
if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { |
| 115 |
$font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes']; |
| 116 |
$settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? |
| 117 |
$font_sizes_by_origin['custom'] : ( |
| 118 |
isset( $font_sizes_by_origin['theme'] ) ? |
| 119 |
$font_sizes_by_origin['theme'] : |
| 120 |
$font_sizes_by_origin['default'] |
| 121 |
); |
| 122 |
} |
| 123 |
if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) { |
| 124 |
$settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom']; |
| 125 |
unset( $settings['__experimentalFeatures']['color']['custom'] ); |
| 126 |
} |
| 127 |
if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) { |
| 128 |
$settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient']; |
| 129 |
unset( $settings['__experimentalFeatures']['color']['customGradient'] ); |
| 130 |
} |
| 131 |
if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { |
| 132 |
$settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize']; |
| 133 |
unset( $settings['__experimentalFeatures']['typography']['customFontSize'] ); |
| 134 |
} |
| 135 |
if ( isset( $settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { |
| 136 |
$settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['lineHeight']; |
| 137 |
unset( $settings['__experimentalFeatures']['typography']['lineHeight'] ); |
| 138 |
} |
| 139 |
if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) { |
| 140 |
$settings['enableCustomUnits'] = $settings['__experimentalFeatures']['spacing']['units']; |
| 141 |
unset( $settings['__experimentalFeatures']['spacing']['units'] ); |
| 142 |
} |
| 143 |
if ( isset( $settings['__experimentalFeatures']['spacing']['padding'] ) ) { |
| 144 |
$settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['padding']; |
| 145 |
unset( $settings['__experimentalFeatures']['spacing']['padding'] ); |
| 146 |
} |
| 147 |
if ( isset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) { |
| 148 |
$settings['disableCustomSpacingSizes'] = ! $settings['__experimentalFeatures']['spacing']['customSpacingSize']; |
| 149 |
unset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( isset( $settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) { |
| 153 |
$spacing_sizes_by_origin = $settings['__experimentalFeatures']['spacing']['spacingSizes']; |
| 154 |
$settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ? |
| 155 |
$spacing_sizes_by_origin['custom'] : ( |
| 156 |
isset( $spacing_sizes_by_origin['theme'] ) ? |
| 157 |
$spacing_sizes_by_origin['theme'] : |
| 158 |
$spacing_sizes_by_origin['default'] |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
return $settings; |
| 163 |
} |
| 164 |
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', 0 ); |
| 165 |
|