/var/www/html_it/wp-content/plugins/woocommerce/src/Internal/Email/EmailStyleSync.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare( strict_types );

namespace 
Automattic\WooCommerce\Internal\Email;

use 
Automattic\WooCommerce\Internal\RegisterHooksInterface;

/**
 * Helper class for syncing email styles with theme styles.
 *
 * @internal Just for internal use.
 */
class EmailStyleSync implements RegisterHooksInterface {

    
/**
     * Option name for auto-sync setting.
     */
    
const AUTO_SYNC_OPTION 'woocommerce_email_auto_sync_with_theme';

    
/**
     * Flag to prevent recursive syncing.
     *
     * @var bool
     */
    
private $is_syncing false;

    
/**
     * Register hooks and filters.
     */
    
public function register() {
        
// Hook into theme change events.
        
add_action'after_switch_theme', array( $this'sync_email_styles_with_theme' ) );
        
add_action'customize_save_after', array( $this'sync_email_styles_with_theme' ) );

        
// Hook into theme.json and global styles changes.
        
add_action'wp_theme_json_data_updated', array( $this'sync_email_styles_with_theme' ) );
        
add_action'rest_after_insert_global_styles', array( $this'sync_email_styles_with_theme' ) );
        
add_action'update_option_wp_global_styles', array( $this'sync_email_styles_with_theme' ) );
        
add_action'save_post_wp_global_styles', array( $this'sync_email_styles_with_theme' ) );

        
// Hook into the theme editor save action.
        
add_action'wp_ajax_wp_save_styles', array( $this'sync_email_styles_with_theme' ), 999 );

        
// Hook into auto-sync option update to trigger sync when enabled.
        
add_action'update_option_' self::AUTO_SYNC_OPTION, array( $this'maybe_sync_on_option_update' ), 10);
    }

    
/**
     * Trigger sync when auto-sync option is enabled.
     *
     * @param mixed  $old_value The old option value.
     * @param mixed  $new_value The new option value.
     * @param string $option    The option name.
     */
    
public function maybe_sync_on_option_update$old_value$new_value$option ) {
        if ( 
'yes' === $new_value && 'yes' !== $old_value ) {
            
// Force sync regardless of current auto-sync setting since we know it's being enabled.
            
$this->is_syncing true;
            try {
                
$this->update_email_colors();
            } finally {
                
$this->is_syncing false;
            }
        }
    }

    
/**
     * Check if auto-sync is enabled.
     *
     * @return bool Whether auto-sync is enabled.
     */
    
public function is_auto_sync_enabled() {
        return 
'yes' === get_optionself::AUTO_SYNC_OPTION'no' );
    }

    
/**
     * Set auto-sync enabled status.
     *
     * @param bool $enabled Whether auto-sync should be enabled.
     * @return bool Whether the option was updated.
     */
    
public function set_auto_syncbool $enabled ) {
        return 
update_optionself::AUTO_SYNC_OPTION$enabled 'yes' 'no' );
    }

    
/**
     * Sync email styles with theme styles if auto-sync is enabled.
     *
     * Uses a flag to prevent recursive calls.
     */
    
public function sync_email_styles_with_theme() {
        if ( 
$this->is_syncing || ! $this->is_auto_sync_enabled() || ! wp_theme_has_theme_json() ) {
            return;
        }

        
$this->is_syncing true;

        try {
            
$this->update_email_colors();
        } finally {
            
$this->is_syncing false;
        }
    }

    
/**
     * Update email colors from theme colors.
     */
    
protected function update_email_colors() {
        
$colors EmailColors::get_default_colors();
        if ( empty( 
$colors ) ) {
            return;
        }

        if ( ! empty( 
$colors['base'] ) ) {
            
update_option'woocommerce_email_base_color'$colors['base'] );
        }

        if ( ! empty( 
$colors['bg'] ) ) {
            
update_option'woocommerce_email_background_color'$colors['bg'] );
        }

        if ( ! empty( 
$colors['body_bg'] ) ) {
            
update_option'woocommerce_email_body_background_color'$colors['body_bg'] );
        }

        if ( ! empty( 
$colors['body_text'] ) ) {
            
update_option'woocommerce_email_text_color'$colors['body_text'] );
        }

        if ( ! empty( 
$colors['footer_text'] ) ) {
            
update_option'woocommerce_email_footer_text_color'$colors['footer_text'] );
        }
    }
}