-
Notifications
You must be signed in to change notification settings - Fork 58
feat(emails): sync site styles into WC classic email options + preview thumbnails (NPPD-1537) #4758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
kmwilkerson
wants to merge
4
commits into
nppd-1524-card-expiry-warning-email
from
nppd-1537-sync-site-styles-into-wc-email-options
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ce7b7f
feat(emails): sync site styles into WC classic email options + previe…
kmwilkerson 158ee07
fix(emails): prevent style sync test stubs from leaking into other te…
kmwilkerson a2f887c
fix(emails): address Copilot review feedback (NPPD-1537)
kmwilkerson 43a03aa
test(emails): remove dead first-run-skip test (NPPD-1537)
kmwilkerson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
includes/plugins/woocommerce/class-woocommerce-email-style-sync.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| <?php | ||
| /** | ||
| * Sync site brand styles into WooCommerce classic email template options. | ||
| * | ||
| * Writes the site's primary color and logo into WC's classic email options | ||
| * on first run, then keeps them in sync when theme colors change. | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| /** | ||
| * WooCommerce Email Style Sync class. | ||
| */ | ||
| class WooCommerce_Email_Style_Sync { | ||
|
|
||
| /** | ||
| * Option name to track the sync version. | ||
| * | ||
| * @var string | ||
| */ | ||
| const SYNCED_VERSION_OPTION = 'newspack_wc_email_style_sync_version'; | ||
|
|
||
| /** | ||
| * Current sync version. Bump this to re-trigger sync on all sites. | ||
| * | ||
| * @var string | ||
| */ | ||
| const CURRENT_VERSION = 'v1'; | ||
|
|
||
| /** | ||
| * Initialize hooks. | ||
| */ | ||
| public static function init(): void { | ||
| // First-run sync on admin_init (after WC is loaded). | ||
| add_action( 'admin_init', [ __CLASS__, 'maybe_sync_on_first_run' ] ); | ||
|
|
||
| // Re-sync when Customizer colors change or when themes are switched. | ||
| // Using theme-agnostic hooks (not update_option_theme_mods_{theme}) | ||
| // so the hooks survive a theme switch. | ||
| add_action( 'customize_save_after', [ __CLASS__, 'sync_styles' ] ); | ||
| add_action( 'after_switch_theme', [ __CLASS__, 'sync_styles' ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Run the sync if this is the first time (or the version has been bumped). | ||
| */ | ||
| public static function maybe_sync_on_first_run(): void { | ||
| if ( ! class_exists( 'WooCommerce' ) ) { | ||
| return; | ||
| } | ||
| if ( self::CURRENT_VERSION === get_option( self::SYNCED_VERSION_OPTION ) ) { | ||
| return; | ||
| } | ||
| self::sync_styles(); | ||
| update_option( self::SYNCED_VERSION_OPTION, self::CURRENT_VERSION ); | ||
| } | ||
|
|
||
| /** | ||
| * Write site brand colors and logo into WC classic email options. | ||
| */ | ||
| public static function sync_styles(): void { | ||
| if ( ! class_exists( 'WooCommerce' ) ) { | ||
| return; | ||
| } | ||
| $colors = self::get_site_colors(); | ||
| foreach ( $colors as $option_name => $value ) { | ||
| update_option( $option_name, $value ); | ||
| } | ||
| $logo_url = self::get_site_logo_url(); | ||
| update_option( 'woocommerce_email_header_image', $logo_url ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the site brand colors mapped to WC email options. | ||
| * | ||
| * Only syncs woocommerce_email_base_color (header background, links, accents). | ||
| * We intentionally do NOT sync: | ||
| * - woocommerce_email_text_color: the theme's primary_text_color is contrast-computed | ||
| * against the primary brand color, not against white, so mapping it to body text | ||
| * would break readability on sites with dark primaries. | ||
| * - woocommerce_email_background_color: WC's #f7f7f7 surround works across all palettes. | ||
| * - woocommerce_email_body_background_color: WC's #ffffff body works across all palettes. | ||
| * | ||
| * @return array<string, string> WC option name => color hex value. | ||
| */ | ||
| private static function get_site_colors(): array { | ||
| if ( ! function_exists( 'newspack_get_theme_colors' ) ) { | ||
| return []; | ||
| } | ||
| return [ | ||
| 'woocommerce_email_base_color' => newspack_get_theme_colors()['primary_color'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the site logo URL for WC email header image. | ||
| * | ||
| * @return string Logo URL, or empty string if no logo is set. | ||
| */ | ||
| private static function get_site_logo_url(): string { | ||
| $custom_logo_id = get_theme_mod( 'custom_logo' ); | ||
| if ( $custom_logo_id ) { | ||
| $url = wp_get_attachment_url( $custom_logo_id ); | ||
| return $url ? $url : ''; | ||
| } | ||
| return ''; | ||
| } | ||
| } | ||
| WooCommerce_Email_Style_Sync::init(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.