| 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 |
|
| 29 |
// since WP 5.8.0 'block_editor_settings' filter is deprecated |
| 30 |
if ( function_exists( 'get_block_editor_settings' ) ) { |
| 31 |
add_filter( 'block_editor_settings_all', array( $this, 'filter_block_editor_settings' ) ); |
| 32 |
} else { |
| 33 |
add_filter( 'block_editor_settings', array( $this, 'filter_block_editor_settings' ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function generate_colors_css() { |
| 38 |
|
| 39 |
$custom_colors = $this->custom_colors_service->get_colors(); |
| 40 |
$edited_initial_colors = $this->default_colors_service->get_edited_colors(); |
| 41 |
|
| 42 |
$colors = array_merge( $edited_initial_colors, $custom_colors ); |
| 43 |
$prefix = $this->options_service->get_style_classes_prefix(); |
| 44 |
|
| 45 |
$css = ''; |
| 46 |
foreach ( $colors as $color ) { |
| 47 |
$css .= PHP_EOL; |
| 48 |
$css .= <<<CSS |
| 49 |
{$prefix} .has-{$color['slug']}-color{ |
| 50 |
color: {$color['color']}; |
| 51 |
} |
| 52 |
{$prefix} .has-{$color['slug']}-background-color{ |
| 53 |
background-color: {$color['color']}; |
| 54 |
} |
| 55 |
CSS; |
| 56 |
$css .= PHP_EOL; |
| 57 |
} |
| 58 |
|
| 59 |
return $css; |
| 60 |
} |
| 61 |
|
| 62 |
public function print_head_styles() { |
| 63 |
|
| 64 |
$style = $this->generate_colors_css(); |
| 65 |
if ( $style == '' ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
?><style id="bec-color-style" type="text/css"> |
| 70 |
/* Block Editor Colors generated css */ |
| 71 |
<?php echo esc_html($style); ?> |
| 72 |
</style><?php |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public function filter_block_editor_settings( $settings ) { |
| 77 |
|
| 78 |
$initial_colors = $this->default_colors_service->get_colors(); |
| 79 |
$new_colors = $this->custom_colors_service->get_colors(); |
| 80 |
|
| 81 |
$initial_colors = array_values( $initial_colors ); |
| 82 |
$new_colors = array_values( $new_colors ); |
| 83 |
$settings['colors'] = array_merge( $initial_colors, $new_colors ); |
| 84 |
|
| 85 |
if ( function_exists( 'get_block_editor_settings' ) ) { |
| 86 |
$settings['__experimentalFeatures']['color']['palette']['user'] = array_merge($initial_colors, $new_colors); |
| 87 |
} |
| 88 |
|
| 89 |
return $settings; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
ColorsService::getInstance(); |
| 96 |
|