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