| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail; |
| 4 |
|
| 5 |
use YayMail\Utils\Helpers; |
| 6 |
use YayMail\Utils\SingletonTrait; |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles WooCommerce email preview |
| 10 |
* |
| 11 |
* @method static WooHandler get_instance() |
| 12 |
*/ |
| 13 |
class WooHandler { |
| 14 |
|
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
protected function __construct() { |
| 18 |
add_filter( 'woocommerce_prepare_email_for_preview', [ $this, 'display_preview_notice' ] ); |
| 19 |
add_filter( 'woocommerce_mail_content', [ $this, 'handle_default_preview_content' ] ); |
| 20 |
// Add settings to WooCommerce email options section |
| 21 |
add_filter( 'woocommerce_get_settings_email', [ $this, 'add_settings' ], 10, 2 ); |
| 22 |
add_filter( |
| 23 |
'woocommerce_get_settings_advanced', |
| 24 |
function( $settings ) { |
| 25 |
foreach ( $settings as $index => $setting ) { |
| 26 |
if ( $setting['id'] === 'woocommerce_feature_block_email_editor_enabled' ) { |
| 27 |
$introduction_text = sprintf( __( 'You can customize WooCommerce emails with <a href="%s" target="_blank">YayMail - WooCommerce Email Customizer</a>', 'yaymail' ), esc_url( admin_url( 'admin.php?page=yaymail-settings#' ), 'yaymail' ) ); |
| 28 |
$settings[ $index ]['desc'] .= '<br/><br/>' . $introduction_text . '<br/>'; |
| 29 |
} |
| 30 |
} |
| 31 |
return $settings; |
| 32 |
} |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
public function display_preview_notice( $email ) { |
| 37 |
|
| 38 |
if ( ! ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'preview-mail' ) ) ) { |
| 39 |
return $email; |
| 40 |
} |
| 41 |
|
| 42 |
if ( isset( $_GET['preview_woocommerce_mail'] ) && ! Helpers::is_true( sanitize_text_field( wp_unslash( $_GET['preview_woocommerce_mail'] ) ) ) ) { |
| 43 |
return $email; |
| 44 |
} |
| 45 |
|
| 46 |
if ( isset( $_GET['rest_route'] ) && $_GET['rest_route'] === '/wc-admin-email/settings/email/send-preview' ) { |
| 47 |
return $email; |
| 48 |
} |
| 49 |
if ( ! isset( $email->id ) ) { |
| 50 |
return $email; |
| 51 |
} |
| 52 |
$email_id = $email->id; |
| 53 |
|
| 54 |
$yaymail_template = new YayMailTemplate( $email_id ); |
| 55 |
|
| 56 |
if ( ! $yaymail_template->is_exists() ) { |
| 57 |
return $email; |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! $yaymail_template->is_enabled() ) { |
| 61 |
return $email; |
| 62 |
} |
| 63 |
|
| 64 |
add_filter( 'yaymail_previewing_template_is_yaymail_template', '__return_true' ); |
| 65 |
ob_start(); |
| 66 |
?> |
| 67 |
<div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; max-width: 600px; margin: 20px auto; padding: 20px; text-align: center;"> |
| 68 |
<div style="background-color: #f7f4fa; padding: 12px 12px 24px 12px; border-radius: 4px; margin-bottom: 20px;"> |
| 69 |
<h2 style="color: <?php echo esc_attr( YAYMAIL_COLOR_WC_DEFAULT ); ?>; font-size: 24px; margin-bottom: 8px;"><?php esc_html_e( 'YayMail Template Preview', 'yaymail' ); ?></h2> |
| 70 |
<p style="color:rgb(110, 110, 110); font-size: 14px; margin-bottom: 24px;"><?php esc_html_e( 'This is one of your WooCommerce email templates customized with YayMail. You can modify its colors, layout, and content in the YayMail editor.', 'yaymail' ); ?></p> |
| 71 |
<a href="<?php echo esc_url( admin_url( 'admin.php?page=yaymail-settings#/customizer/?template=' . $email_id ) ); ?>" target="_blank" style="display: inline-block; background-color: <?php echo esc_attr( YAYMAIL_COLOR_WC_DEFAULT ); ?>; color: #fff; font-size: 12px; padding: 8px 12px; border-radius: 3px; text-decoration: none;"><?php esc_html_e( 'Customized Template', 'yaymail' ); ?></a> |
| 72 |
</div> |
| 73 |
</div> |
| 74 |
<?php |
| 75 |
$content = ob_get_clean(); |
| 76 |
yaymail_kses_post_e( $content ); |
| 77 |
return $email; |
| 78 |
} |
| 79 |
|
| 80 |
public function handle_default_preview_content( $content ) { |
| 81 |
|
| 82 |
if ( ! ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'preview-mail' ) ) ) { |
| 83 |
return $content; |
| 84 |
} |
| 85 |
|
| 86 |
if ( isset( $_GET['preview_woocommerce_mail'] ) && ! Helpers::is_true( sanitize_text_field( wp_unslash( $_GET['preview_woocommerce_mail'] ) ) ) ) { |
| 87 |
return $content; |
| 88 |
} |
| 89 |
|
| 90 |
if ( isset( $_GET['rest_route'] ) && $_GET['rest_route'] === '/wc-admin-email/settings/email/send-preview' ) { |
| 91 |
return $content; |
| 92 |
} |
| 93 |
|
| 94 |
if ( apply_filters( 'yaymail_previewing_template_is_yaymail_template', false ) ) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
return $content; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Add YayMail settings to WooCommerce email options. |
| 102 |
* |
| 103 |
* @param array $settings WooCommerce email settings. |
| 104 |
* @param string $current_section Current settings section. |
| 105 |
* @return array Modified settings. |
| 106 |
*/ |
| 107 |
public function add_settings( $settings, $current_section ) { |
| 108 |
// Only add to the email options section (empty section) |
| 109 |
if ( $current_section !== '' ) { |
| 110 |
return $settings; |
| 111 |
} |
| 112 |
|
| 113 |
$yaymail_settings = [ |
| 114 |
[ |
| 115 |
'title' => __( 'WooCommerce Email Designer', 'yaymail' ), |
| 116 |
'type' => 'title', |
| 117 |
'id' => 'yaymail_email_designer', |
| 118 |
], |
| 119 |
[ |
| 120 |
'title' => __( 'Customize WooCommerce Emails', 'yaymail' ), |
| 121 |
'desc' => '', |
| 122 |
'id' => 'woocommerce_customizer_emails', |
| 123 |
'type' => 'yaymail_button', |
| 124 |
'desc_tip' => true, |
| 125 |
], |
| 126 |
[ |
| 127 |
'type' => 'sectionend', |
| 128 |
'id' => 'yaymail_email_designer', |
| 129 |
], |
| 130 |
]; |
| 131 |
|
| 132 |
// Add custom button HTML |
| 133 |
add_action( 'woocommerce_admin_field_yaymail_button', [ $this, 'output_button' ] ); |
| 134 |
|
| 135 |
$settings = array_merge( $settings, $yaymail_settings ); |
| 136 |
|
| 137 |
return $settings; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Output the custom button HTML |
| 142 |
* |
| 143 |
* @param array $value Button field settings. |
| 144 |
*/ |
| 145 |
public function output_button( $value ) { |
| 146 |
?> |
| 147 |
<tr valign="top"> |
| 148 |
<th scope="row" class="titledesc"> |
| 149 |
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label> |
| 150 |
</th> |
| 151 |
<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 152 |
<button type="button" |
| 153 |
class="button" |
| 154 |
id="<?php echo esc_attr( $value['id'] ); ?>" |
| 155 |
onclick="window.open('<?php echo esc_url( admin_url( 'admin.php?page=yaymail-settings' ) ); ?>', '_blank')" |
| 156 |
> |
| 157 |
<?php esc_html_e( 'Open YayMail', 'yaymail' ); ?> |
| 158 |
</button> |
| 159 |
<p class="description"><?php esc_html_e( 'Make Woocommerce Emails match your brand. ', 'yaymail' ); ?><a href="https://yaycommerce.com/yaymail-woocommerce-email-customizer/" target="_blank"><?php esc_html_e( 'YayMail - WooCommerce Email Customizer', 'yaymail' ); ?></a> <?php esc_html_e( ' plugin by ', 'yaymail' ); ?> <a href="https://yaycommerce.com/" target="_blank"><?php esc_html_e( 'YayCommerce', 'yaymail' ); ?></a>.</p> |
| 160 |
</td> |
| 161 |
</tr> |
| 162 |
<?php |
| 163 |
} |
| 164 |
} |
| 165 |
|