PluginProbe
Block Editor Colors / 1.2.3
Block Editor Colors v1.2.3
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 1.2.3, at includes/ColorsService.php

117 lines 3.4 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
30 // since WP 5.8.0 'block_editor_settings' filter is deprecated
31 if ( function_exists( 'get_block_editor_settings' ) ) {
32 add_filter( 'block_editor_settings_all', array( $this, 'filter_block_editor_settings' ) );
33 } else {
34 add_filter( 'block_editor_settings', array( $this, 'filter_block_editor_settings' ) );
35 }
36 }
37
38 public function generate_colors_css( $is_editor = false ) {
39
40 $custom_colors = $this->custom_colors_service->get_colors();
41 $edited_initial_colors = $this->default_colors_service->get_edited_colors();
42
43 $colors = array_merge( $edited_initial_colors, $custom_colors );
44 $prefix = $is_editor ? 'body .editor-styles-wrapper' : $this->options_service->get_style_classes_prefix();
45
46 $variables = '';
47 $css = '';
48 foreach ( $colors as $color ) {
49 $css .= PHP_EOL;
50 $css .= <<<CSS
51 {$prefix} .has-{$color['slug']}-color {
52 color: var(--bec-color-{$color['slug']}, {$color['color']}) !important;
53 }
54 {$prefix} .has-{$color['slug']}-background-color {
55 background-color: var(--bec-color-{$color['slug']}, {$color['color']}) !important;
56 }
57 CSS;
58 $css .= PHP_EOL;
59
60 $variables .= '--bec-color-' . $color['slug'] . ': ' . $color['color'] . ';' . PHP_EOL;
61 }
62
63 if ( $css ) {
64 $css = $prefix . ' {' . PHP_EOL . $variables . '}' . PHP_EOL . $css;
65 }
66
67 return $css;
68 }
69
70 public function print_head_styles() {
71
72 $style = $this->generate_colors_css();
73 if ( $style == '' ) {
74 return;
75 }
76
77 ?><style id="bec-color-style" type="text/css">
78 /* Block Editor Colors generated css */
79 <?php echo esc_html($style); ?>
80 </style><?php
81
82 }
83
84 public function add_editor_styles() {
85 $style = $this->generate_colors_css( true );
86 if ( $style == '' ) {
87 return;
88 }
89
90 wp_register_style( 'bec-editor-styles', false, array(), BEC_PLUGIN_VERSION, true );
91 wp_add_inline_style( 'bec-editor-styles', $style );
92 wp_enqueue_style( 'bec-editor-styles' );
93 }
94
95 public function filter_block_editor_settings( $settings ) {
96
97 $initial_colors = $this->default_colors_service->get_colors();
98 $new_colors = $this->custom_colors_service->get_colors();
99
100 $initial_colors = array_values( $initial_colors );
101 $new_colors = array_values( $new_colors );
102 $settings['colors'] = array_merge( $initial_colors, $new_colors );
103
104 if ( function_exists( 'get_block_editor_settings' ) ) {
105 $settings['__experimentalFeatures']['color']['palette']['user']
106 = $settings['__experimentalFeatures']['color']['palette']['theme']
107 = array_merge($initial_colors, $new_colors);
108 }
109
110 return $settings;
111
112 }
113
114 }
115
116 ColorsService::getInstance();
117