DeferredEmailQueue.php
4 weeks ago
EmailColors.php
11 months ago
EmailFont.php
1 year ago
EmailLogger.php
4 weeks ago
EmailStyleSync.php
11 months ago
OrderPriceFormatter.php
1 year ago
EmailStyleSync.php
135 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Email; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 7 | |
| 8 | /** |
| 9 | * Helper class for syncing email styles with theme styles. |
| 10 | * |
| 11 | * @internal Just for internal use. |
| 12 | */ |
| 13 | class EmailStyleSync implements RegisterHooksInterface { |
| 14 | |
| 15 | /** |
| 16 | * Option name for auto-sync setting. |
| 17 | */ |
| 18 | const AUTO_SYNC_OPTION = 'woocommerce_email_auto_sync_with_theme'; |
| 19 | |
| 20 | /** |
| 21 | * Flag to prevent recursive syncing. |
| 22 | * |
| 23 | * @var bool |
| 24 | */ |
| 25 | private $is_syncing = false; |
| 26 | |
| 27 | /** |
| 28 | * Register hooks and filters. |
| 29 | */ |
| 30 | public function register() { |
| 31 | // Hook into theme change events. |
| 32 | add_action( 'after_switch_theme', array( $this, 'sync_email_styles_with_theme' ) ); |
| 33 | add_action( 'customize_save_after', array( $this, 'sync_email_styles_with_theme' ) ); |
| 34 | |
| 35 | // Hook into theme.json and global styles changes. |
| 36 | add_action( 'wp_theme_json_data_updated', array( $this, 'sync_email_styles_with_theme' ) ); |
| 37 | add_action( 'rest_after_insert_global_styles', array( $this, 'sync_email_styles_with_theme' ) ); |
| 38 | add_action( 'update_option_wp_global_styles', array( $this, 'sync_email_styles_with_theme' ) ); |
| 39 | add_action( 'save_post_wp_global_styles', array( $this, 'sync_email_styles_with_theme' ) ); |
| 40 | |
| 41 | // Hook into the theme editor save action. |
| 42 | add_action( 'wp_ajax_wp_save_styles', array( $this, 'sync_email_styles_with_theme' ), 999 ); |
| 43 | |
| 44 | // Hook into auto-sync option update to trigger sync when enabled. |
| 45 | add_action( 'update_option_' . self::AUTO_SYNC_OPTION, array( $this, 'maybe_sync_on_option_update' ), 10, 3 ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Trigger sync when auto-sync option is enabled. |
| 50 | * |
| 51 | * @param mixed $old_value The old option value. |
| 52 | * @param mixed $new_value The new option value. |
| 53 | * @param string $option The option name. |
| 54 | */ |
| 55 | public function maybe_sync_on_option_update( $old_value, $new_value, $option ) { |
| 56 | if ( 'yes' === $new_value && 'yes' !== $old_value ) { |
| 57 | // Force sync regardless of current auto-sync setting since we know it's being enabled. |
| 58 | $this->is_syncing = true; |
| 59 | try { |
| 60 | $this->update_email_colors(); |
| 61 | } finally { |
| 62 | $this->is_syncing = false; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check if auto-sync is enabled. |
| 69 | * |
| 70 | * @return bool Whether auto-sync is enabled. |
| 71 | */ |
| 72 | public function is_auto_sync_enabled() { |
| 73 | return 'yes' === get_option( self::AUTO_SYNC_OPTION, 'no' ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set auto-sync enabled status. |
| 78 | * |
| 79 | * @param bool $enabled Whether auto-sync should be enabled. |
| 80 | * @return bool Whether the option was updated. |
| 81 | */ |
| 82 | public function set_auto_sync( bool $enabled ) { |
| 83 | return update_option( self::AUTO_SYNC_OPTION, $enabled ? 'yes' : 'no' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Sync email styles with theme styles if auto-sync is enabled. |
| 88 | * |
| 89 | * Uses a flag to prevent recursive calls. |
| 90 | */ |
| 91 | public function sync_email_styles_with_theme() { |
| 92 | if ( $this->is_syncing || ! $this->is_auto_sync_enabled() || ! wp_theme_has_theme_json() ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $this->is_syncing = true; |
| 97 | |
| 98 | try { |
| 99 | $this->update_email_colors(); |
| 100 | } finally { |
| 101 | $this->is_syncing = false; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Update email colors from theme colors. |
| 107 | */ |
| 108 | protected function update_email_colors() { |
| 109 | $colors = EmailColors::get_default_colors(); |
| 110 | if ( empty( $colors ) ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | if ( ! empty( $colors['base'] ) ) { |
| 115 | update_option( 'woocommerce_email_base_color', $colors['base'] ); |
| 116 | } |
| 117 | |
| 118 | if ( ! empty( $colors['bg'] ) ) { |
| 119 | update_option( 'woocommerce_email_background_color', $colors['bg'] ); |
| 120 | } |
| 121 | |
| 122 | if ( ! empty( $colors['body_bg'] ) ) { |
| 123 | update_option( 'woocommerce_email_body_background_color', $colors['body_bg'] ); |
| 124 | } |
| 125 | |
| 126 | if ( ! empty( $colors['body_text'] ) ) { |
| 127 | update_option( 'woocommerce_email_text_color', $colors['body_text'] ); |
| 128 | } |
| 129 | |
| 130 | if ( ! empty( $colors['footer_text'] ) ) { |
| 131 | update_option( 'woocommerce_email_footer_text_color', $colors['footer_text'] ); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 |