PluginProbe
Block Editor Colors / trunk
Block Editor Colors vtrunk
1.2.7 1.2.4 1.2.5 1.2.6 trunk 1.2.0 1.2.1 1.2.2 1.2.3
block-editor-colors / includes / ColorsService.php

ColorsService.php in Block Editor Colors trunk, at includes/ColorsService.php

128 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BlockEditorColors;
4
5
6 class ColorsService {
7
8 private $default_colors_service = null;
9 private $custom_colors_service = null;
10 private $options_service = null;
11 private static $_instance = null;
12
13 public static function getInstance() {
14 if ( is_null( self::$_instance ) ) {
15 self::$_instance = new self();
16 }
17
18 return self::$_instance;
19 }
20
21 public function __construct() {
22
23 $this->default_colors_service = DefaultColorsService::getInstance();
24 $this->custom_colors_service = CustomColorsService::getInstance();
25 $this->options_service = OptionsService::getInstance();
26
27 add_action( 'wp_head', array( $this, 'print_head_styles' ) );
28 add_action( 'enqueue_block_editor_assets', array( $this, 'add_editor_styles' ) );
29 add_action( 'enqueue_block_assets', array( $this, 'add_editor_styles' ) );
30
31 // add Gutenberg plugin filter with less priority
32 if ( has_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings' ) ) {
33 remove_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX - 1 );
34 add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX - 1 );
35 }
36
37 // since WP 5.8.0 'block_editor_settings' filter is deprecated
38 if ( function_exists( 'get_block_editor_settings' ) ) {
39 add_filter( 'block_editor_settings_all', array( $this, 'filter_block_editor_settings' ), PHP_INT_MAX );
40 } else {
41 add_filter( 'block_editor_settings', array( $this, 'filter_block_editor_settings' ) );
42 }
43 }
44
45 public function generate_colors_css( $is_editor = false ) {
46
47 $custom_colors = $this->custom_colors_service->get_colors();
48 $edited_initial_colors = $this->default_colors_service->get_edited_colors();
49
50 $colors = array_merge( $edited_initial_colors, $custom_colors );
51 $prefix = $is_editor ? '.editor-styles-wrapper' : $this->options_service->get_style_classes_prefix();
52
53 $variables = '';
54 $css = '';
55 foreach ( $colors as $color ) {
56 $css .= PHP_EOL;
57 $css .= <<<CSS
58 {$prefix} .has-{$color['slug']}-color {
59 color: var(--bec-color-{$color['slug']}, {$color['color']}) !important;
60 }
61 {$prefix} .has-{$color['slug']}-background-color {
62 background-color: var(--bec-color-{$color['slug']}, {$color['color']}) !important;
63 }
64 CSS;
65 $css .= PHP_EOL;
66
67 $variables .= '--bec-color-' . $color['slug'] . ': ' . $color['color'] . ';' . PHP_EOL;
68 }
69
70 if ( $css ) {
71 $css = $prefix . ' {' . PHP_EOL . $variables . '}' . PHP_EOL . $css;
72 }
73
74 return $css;
75 }
76
77 public function print_head_styles() {
78
79 $style = $this->generate_colors_css();
80 if ( $style == '' ) {
81 return;
82 }
83
84 ?><style id="bec-color-style" type="text/css">
85 /* Block Editor Colors generated css */
86 <?php echo esc_html($style); ?>
87 </style><?php
88
89 }
90
91 public function add_editor_styles() {
92 if ( !is_admin() ) {
93 return;
94 }
95
96 $style = $this->generate_colors_css( true );
97 if ( $style == '' ) {
98 return;
99 }
100
101 wp_register_style( 'bec-editor-styles', false, array(), BEC_PLUGIN_VERSION, true );
102 wp_add_inline_style( 'bec-editor-styles', $style );
103 wp_enqueue_style( 'bec-editor-styles' );
104 }
105
106 public function filter_block_editor_settings( $settings ) {
107
108 $initial_colors = $this->default_colors_service->get_colors();
109 $new_colors = $this->custom_colors_service->get_colors();
110
111 $initial_colors = array_values( $initial_colors );
112 $new_colors = array_values( $new_colors );
113 $settings['colors'] = array_merge( $initial_colors, $new_colors );
114
115 if ( function_exists( 'get_block_editor_settings' ) ) {
116 $settings['__experimentalFeatures']['color']['palette']['user']
117 = $settings['__experimentalFeatures']['color']['palette']['theme']
118 = array_merge($initial_colors, $new_colors);
119 }
120
121 return $settings;
122
123 }
124
125 }
126
127 ColorsService::getInstance();
128