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
EmailColors.php
113 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EmailColors class file |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types = 1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Email; |
| 9 | |
| 10 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 11 | |
| 12 | /** |
| 13 | * Helper class for email colors. |
| 14 | * |
| 15 | * @internal Just for internal use. |
| 16 | */ |
| 17 | class EmailColors { |
| 18 | |
| 19 | /** |
| 20 | * Get default colors for emails. |
| 21 | * |
| 22 | * @param bool|null $email_improvements_enabled Whether the email improvements feature is enabled. |
| 23 | * @return array Array of default email colors. |
| 24 | */ |
| 25 | public static function get_default_colors( ?bool $email_improvements_enabled = null ) { |
| 26 | if ( null === $email_improvements_enabled ) { |
| 27 | $email_improvements_enabled = FeaturesUtil::feature_is_enabled( 'email_improvements' ); |
| 28 | } |
| 29 | |
| 30 | $base = '#720eec'; |
| 31 | $bg = '#f7f7f7'; |
| 32 | $body_bg = '#ffffff'; |
| 33 | $body_text = '#3c3c3c'; |
| 34 | $footer_text = '#3c3c3c'; |
| 35 | |
| 36 | if ( $email_improvements_enabled ) { |
| 37 | $base = '#8526ff'; |
| 38 | $bg = '#ffffff'; |
| 39 | $body_bg = '#ffffff'; |
| 40 | $body_text = '#1e1e1e'; |
| 41 | $footer_text = '#787c82'; |
| 42 | |
| 43 | $global_colors = static::get_colors_from_global_styles(); |
| 44 | |
| 45 | if ( $global_colors ) { |
| 46 | $base = $global_colors['base']; |
| 47 | $bg = $global_colors['bg']; |
| 48 | $body_bg = $global_colors['body_bg']; |
| 49 | $body_text = $global_colors['body_text']; |
| 50 | $footer_text = $global_colors['footer_text']; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return compact( |
| 55 | 'base', |
| 56 | 'bg', |
| 57 | 'body_bg', |
| 58 | 'body_text', |
| 59 | 'footer_text', |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get email colors from global styles. |
| 65 | * |
| 66 | * @return array|null Array of colors or null if global styles are not available or complete. |
| 67 | */ |
| 68 | public static function get_colors_from_global_styles() { |
| 69 | $styles = static::get_global_styles_data(); |
| 70 | |
| 71 | if ( ! $styles ) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | $bg = $styles['color']['background'] ?? null; |
| 76 | $body_bg = $styles['color']['background'] ?? null; |
| 77 | $body_text = $styles['color']['text'] ?? null; |
| 78 | $base = $styles['elements']['button']['color']['background'] ?? null; |
| 79 | $footer_text = $styles['elements']['caption']['color']['text'] ?? null; |
| 80 | |
| 81 | $bg = is_string( $bg ) ? sanitize_hex_color( $bg ) : ''; |
| 82 | $body_bg = is_string( $body_bg ) ? sanitize_hex_color( $body_bg ) : ''; |
| 83 | $body_text = is_string( $body_text ) ? sanitize_hex_color( $body_text ) : ''; |
| 84 | $base = is_string( $base ) ? sanitize_hex_color( $base ) : $body_text; |
| 85 | $footer_text = is_string( $footer_text ) ? sanitize_hex_color( $footer_text ) : $body_text; |
| 86 | |
| 87 | // Only return colors if all are set, otherwise email styles might not match and the email can become unreadable. |
| 88 | if ( ! $bg || ! $body_bg || ! $body_text || ! $base || ! $footer_text ) { |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | return compact( |
| 93 | 'base', |
| 94 | 'bg', |
| 95 | 'body_bg', |
| 96 | 'body_text', |
| 97 | 'footer_text', |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Method to retrieve global styles data. |
| 103 | * |
| 104 | * @return array|null |
| 105 | */ |
| 106 | protected static function get_global_styles_data() { |
| 107 | if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() || ! function_exists( 'wp_get_global_styles' ) ) { |
| 108 | return null; |
| 109 | } |
| 110 | return wp_get_global_styles( array(), array( 'transforms' => array( 'resolve-variables' ) ) ); |
| 111 | } |
| 112 | } |
| 113 |