|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Adds settings to the block editor. |
| 4 | + * |
| 5 | + * @package gutenberg |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Adds styles and __experimentalFeatures to the block editor settings. |
| 10 | + * |
| 11 | + * @param array $settings Existing block editor settings. |
| 12 | + * |
| 13 | + * @return array New block editor settings. |
| 14 | + */ |
| 15 | +function gutenberg_get_block_editor_settings( $settings ) { |
| 16 | + // Set what is the context for this data request. |
| 17 | + $context = 'other'; |
| 18 | + if ( |
| 19 | + defined( 'REST_REQUEST' ) && |
| 20 | + REST_REQUEST && |
| 21 | + isset( $_GET['context'] ) && |
| 22 | + 'mobile' === $_GET['context'] |
| 23 | + ) { |
| 24 | + $context = 'mobile'; |
| 25 | + } |
| 26 | + |
| 27 | + if ( 'other' === $context ) { |
| 28 | + global $wp_version; |
| 29 | + $is_wp_5_8 = version_compare( $wp_version, '5.8', '>=' ) && version_compare( $wp_version, '5.9', '<' ); |
| 30 | + $is_wp_5_9 = version_compare( $wp_version, '5.9', '>=' ) && version_compare( $wp_version, '6.0-beta1', '<' ); |
| 31 | + $is_wp_6_0 = version_compare( $wp_version, '6.0-beta1', '>=' ); |
| 32 | + |
| 33 | + // Make sure the styles array exists. |
| 34 | + // In some contexts, like the navigation editor, it doesn't. |
| 35 | + if ( ! isset( $settings['styles'] ) ) { |
| 36 | + $settings['styles'] = array(); |
| 37 | + } |
| 38 | + |
| 39 | + // Remove existing global styles provided by core. |
| 40 | + $styles_without_existing_global_styles = array(); |
| 41 | + foreach ( $settings['styles'] as $style ) { |
| 42 | + if ( |
| 43 | + ( $is_wp_5_8 && ! gutenberg_is_global_styles_in_5_8( $style ) ) || // Can be removed when plugin minimum version is 5.9. |
| 44 | + ( $is_wp_5_9 && ! gutenberg_is_global_styles_in_5_9( $style ) ) || // Can be removed when plugin minimum version is 6.0. |
| 45 | + ( $is_wp_6_0 && ( ! isset( $style['isGlobalStyles'] ) || ! $style['isGlobalStyles'] ) ) |
| 46 | + ) { |
| 47 | + $styles_without_existing_global_styles[] = $style; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Recreate global styles. |
| 52 | + $new_global_styles = array(); |
| 53 | + $presets = array( |
| 54 | + array( |
| 55 | + 'css' => 'variables', |
| 56 | + '__unstableType' => 'presets', |
| 57 | + 'isGlobalStyles' => true, |
| 58 | + ), |
| 59 | + array( |
| 60 | + 'css' => 'presets', |
| 61 | + '__unstableType' => 'presets', |
| 62 | + 'isGlobalStyles' => true, |
| 63 | + ), |
| 64 | + ); |
| 65 | + foreach ( $presets as $preset_style ) { |
| 66 | + $actual_css = gutenberg_get_global_stylesheet( array( $preset_style['css'] ) ); |
| 67 | + if ( '' !== $actual_css ) { |
| 68 | + $preset_style['css'] = $actual_css; |
| 69 | + $new_global_styles[] = $preset_style; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if ( WP_Theme_JSON_Resolver::theme_has_support() ) { |
| 74 | + $block_classes = array( |
| 75 | + 'css' => 'styles', |
| 76 | + '__unstableType' => 'theme', |
| 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 | + $new_global_styles[] = $block_classes; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $settings['styles'] = array_merge( $new_global_styles, $styles_without_existing_global_styles ); |
| 87 | + } |
| 88 | + |
| 89 | + // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. |
| 90 | + $settings['__experimentalFeatures'] = gutenberg_get_global_settings(); |
| 91 | + |
| 92 | + if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) { |
| 93 | + $colors_by_origin = $settings['__experimentalFeatures']['color']['palette']; |
| 94 | + $settings['colors'] = isset( $colors_by_origin['custom'] ) ? |
| 95 | + $colors_by_origin['custom'] : ( |
| 96 | + isset( $colors_by_origin['theme'] ) ? |
| 97 | + $colors_by_origin['theme'] : |
| 98 | + $colors_by_origin['default'] |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) { |
| 103 | + $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients']; |
| 104 | + $settings['gradients'] = isset( $gradients_by_origin['custom'] ) ? |
| 105 | + $gradients_by_origin['custom'] : ( |
| 106 | + isset( $gradients_by_origin['theme'] ) ? |
| 107 | + $gradients_by_origin['theme'] : |
| 108 | + $gradients_by_origin['default'] |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { |
| 113 | + $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes']; |
| 114 | + $settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ? |
| 115 | + $font_sizes_by_origin['custom'] : ( |
| 116 | + isset( $font_sizes_by_origin['theme'] ) ? |
| 117 | + $font_sizes_by_origin['theme'] : |
| 118 | + $font_sizes_by_origin['default'] |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) { |
| 123 | + $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom']; |
| 124 | + unset( $settings['__experimentalFeatures']['color']['custom'] ); |
| 125 | + } |
| 126 | + if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) { |
| 127 | + $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient']; |
| 128 | + unset( $settings['__experimentalFeatures']['color']['customGradient'] ); |
| 129 | + } |
| 130 | + if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { |
| 131 | + $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize']; |
| 132 | + unset( $settings['__experimentalFeatures']['typography']['customFontSize'] ); |
| 133 | + } |
| 134 | + if ( isset( $settings['__experimentalFeatures']['typography']['lineHeight'] ) ) { |
| 135 | + $settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['lineHeight']; |
| 136 | + unset( $settings['__experimentalFeatures']['typography']['lineHeight'] ); |
| 137 | + } |
| 138 | + if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) { |
| 139 | + $settings['enableCustomUnits'] = $settings['__experimentalFeatures']['spacing']['units']; |
| 140 | + unset( $settings['__experimentalFeatures']['spacing']['units'] ); |
| 141 | + } |
| 142 | + if ( isset( $settings['__experimentalFeatures']['spacing']['padding'] ) ) { |
| 143 | + $settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['padding']; |
| 144 | + unset( $settings['__experimentalFeatures']['spacing']['padding'] ); |
| 145 | + } |
| 146 | + if ( isset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ) ) { |
| 147 | + $settings['disableCustomSpacingSize'] = ! $settings['__experimentalFeatures']['spacing']['customSpacingSize']; |
| 148 | + unset( $settings['__experimentalFeatures']['spacing']['customSpacingSize'] ); |
| 149 | + } |
| 150 | + |
| 151 | + if ( isset( $settings['__experimentalFeatures']['spacing']['spacingSizes'] ) ) { |
| 152 | + $spacing_sizes_by_origin = $settings['__experimentalFeatures']['spacing']['spacingSizes']; |
| 153 | + $settings['spacingSizes'] = isset( $spacing_sizes_by_origin['custom'] ) ? |
| 154 | + $spacing_sizes_by_origin['custom'] : ( |
| 155 | + isset( $spacing_sizes_by_origin['theme'] ) ? |
| 156 | + $spacing_sizes_by_origin['theme'] : |
| 157 | + $spacing_sizes_by_origin['default'] |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + $settings['localAutosaveInterval'] = 15; |
| 162 | + |
| 163 | + return $settings; |
| 164 | +} |
| 165 | + |
| 166 | +add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX ); |
0 commit comments