|
@@ -20,6 +20,10 @@ class GlobalStylesColorCustomizer {
|
|
|
wp_enqueue_script( 'customizer-preview-color', get_template_directory_uri() . '/inc/customizer/wp-customize-colors-preview.js', array( 'customize-preview' ) );
|
|
|
wp_add_inline_script( 'customizer-preview-color', 'var userColorSectionKey="' . $this->section_key . '";', 'before' );
|
|
|
wp_localize_script( 'customizer-preview-color', 'userColorPalette', $this->user_color_palette );
|
|
|
+ if ( $this->theme_duotone_settings ) {
|
|
|
+ wp_enqueue_script( 'colord', get_template_directory_uri() . '/inc/customizer/vendors/colord.min.js' );
|
|
|
+ wp_localize_script( 'customizer-preview-color', 'userColorDuotone', $this->theme_duotone_settings );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function update_user_color_palette( $wp_customize ) {
|
|
@@ -48,9 +52,21 @@ class GlobalStylesColorCustomizer {
|
|
|
|
|
|
function initialize( $wp_customize ) {
|
|
|
$this->user_color_palette = $this->build_user_color_palette();
|
|
|
+ $this->theme_duotone_settings = $this->get_theme_duotone_settings();
|
|
|
$this->register_color_controls( $wp_customize, $this->user_color_palette );
|
|
|
}
|
|
|
|
|
|
+ function get_theme_duotone_settings() {
|
|
|
+ // Get the merged theme.json.
|
|
|
+ $theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
|
|
|
+
|
|
|
+ if ( array_key_exists( 'settings', $theme_json ) && array_key_exists( 'color', $theme_json['settings'] ) && array_key_exists( 'duotone', $theme_json['settings']['color'] ) && array_key_exists( 'theme', $theme_json['settings']['color']['duotone'] ) ) {
|
|
|
+ return $theme_json['settings']['color']['duotone']['theme'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
function build_user_color_palette() {
|
|
|
// Get the merged theme.json.
|
|
|
$theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
|
|
@@ -161,6 +177,50 @@ class GlobalStylesColorCustomizer {
|
|
|
array( 'settings', 'color', 'palette' ),
|
|
|
$this->user_color_palette
|
|
|
);
|
|
|
+
|
|
|
+ $primary_key = array_search('primary', array_column($this->user_color_palette, 'slug'));
|
|
|
+ $background_key = array_search('background', array_column($this->user_color_palette, 'slug'));
|
|
|
+
|
|
|
+ if ( $this->theme_duotone_settings && $primary_key !== null && $background_key !== null ) {
|
|
|
+
|
|
|
+ $primary = $this->user_color_palette[$primary_key];
|
|
|
+ $background = $this->user_color_palette[$background_key];
|
|
|
+
|
|
|
+ //we invert the colors when the background is darker than the primary color
|
|
|
+ if( colorLuminescence($primary['color']) > colorLuminescence($background['color']) ) {
|
|
|
+ $primary = $this->user_color_palette[$background_key];
|
|
|
+ $background = $this->user_color_palette[$primary_key];
|
|
|
+ }
|
|
|
+
|
|
|
+ $custom_duotone_filter = array(
|
|
|
+ array(
|
|
|
+ "colors" => array( $primary['color'], $background['color'] ),
|
|
|
+ "slug" => "custom-filter",
|
|
|
+ "name" => "Custom filter"
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ $custom_duotone_filter_variable = "var(--wp--preset--duotone--custom-filter)";
|
|
|
+ $user_theme_json_post_content = set_settings_array(
|
|
|
+ $user_theme_json_post_content,
|
|
|
+ array( 'settings', 'color', 'duotone' ),
|
|
|
+ array_merge( $custom_duotone_filter, $this->theme_duotone_settings )
|
|
|
+ );
|
|
|
+
|
|
|
+ //replace the new filter in all blocks using duotone
|
|
|
+ $theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
|
|
|
+ if ( $theme_json['styles'] && $theme_json['styles']['blocks'] ) {
|
|
|
+ foreach ( $theme_json['styles']['blocks'] as $key => $block ) {
|
|
|
+ if( $block['filter'] ) {
|
|
|
+ $user_theme_json_post_content = set_settings_array(
|
|
|
+ $user_theme_json_post_content,
|
|
|
+ array( 'styles', 'blocks', $key, 'filter', 'duotone' ),
|
|
|
+ $custom_duotone_filter_variable
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Update the theme.json with the new settings.
|
|
@@ -168,8 +228,10 @@ class GlobalStylesColorCustomizer {
|
|
|
wp_update_post( $user_theme_json_post );
|
|
|
delete_transient( 'global_styles' );
|
|
|
delete_transient( 'gutenberg_global_styles' );
|
|
|
+ delete_transient( 'gutenberg_global_styles_' . get_stylesheet() );
|
|
|
}
|
|
|
|
|
|
+
|
|
|
function check_if_colors_are_default() {
|
|
|
foreach ( $this->user_color_palette as $palette_color ) {
|
|
|
if ( strtoupper( $palette_color['color'] ) !== strtoupper( $palette_color['default'] ) ) {
|