| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\PreviewEmail; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
use YayMail\YayMailTemplate; |
| 7 |
use YayMail\SupportedPlugins; |
| 8 |
use YayMail\Models\TemplateModel; |
| 9 |
|
| 10 |
/** |
| 11 |
* |
| 12 |
* @method static PreviewEmailWoo get_instance() |
| 13 |
*/ |
| 14 |
class PreviewEmailWoo { |
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
public static $recipient; |
| 18 |
|
| 19 |
private function __construct() {} |
| 20 |
|
| 21 |
public static function email_preview_output( $order_id, $email_id, $email_address = '', $variant = '' ) { |
| 22 |
$current_email = yaymail_get_email( $email_id ); |
| 23 |
$current_email = ! empty( $current_email ) && method_exists( $current_email, 'get_root_email' ) ? $current_email->get_root_email() : ''; |
| 24 |
|
| 25 |
if ( empty( $current_email ) ) { |
| 26 |
return [ |
| 27 |
'html' => yaymail_get_content( '/templates/preview-email/notice.php' ), |
| 28 |
'subject' => str_replace( '(N/A)', '', str_replace( '{blogname}', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), '[' . wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '] - ' . self::get_email_title_by_id( $email_id ) ) ), |
| 29 |
'is_disabled_send_mail' => true, |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
$email_class = get_class( $current_email ); |
| 34 |
|
| 35 |
$order = wc_get_order( $order_id ); |
| 36 |
$template_data = new YayMailTemplate( $email_id, '', $variant ); |
| 37 |
// Enabled-ness always comes from the default design; the variant only swaps the layout. |
| 38 |
$default_template = '' === $variant ? $template_data : new YayMailTemplate( $email_id ); |
| 39 |
|
| 40 |
if ( ! $default_template->is_enabled() ) { |
| 41 |
return self::render_default_preview( $current_email, $email_class, $order ); |
| 42 |
} |
| 43 |
|
| 44 |
self::$recipient = is_email( $email_address ) ? $email_address : ''; |
| 45 |
|
| 46 |
WC()->payment_gateways(); |
| 47 |
WC()->shipping(); |
| 48 |
|
| 49 |
add_filter( 'woocommerce_email_recipient_' . $current_email->id, [ __CLASS__, 'no_recipient' ] ); |
| 50 |
add_filter( 'woocommerce_new_order_email_allows_resend', '__return_true' ); |
| 51 |
add_filter( 'yaymail_is_preview_email', '__return_true' ); |
| 52 |
if ( '' !== $variant ) { |
| 53 |
// Preview the variant being edited; rule-based resolution is skipped while previewing. |
| 54 |
add_filter( |
| 55 |
'yaymail_email_get_variant', |
| 56 |
function () use ( $variant ) { |
| 57 |
return $variant; |
| 58 |
}, |
| 59 |
20 |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
$core_template_ids = SupportedPlugins::get_instance()->get_template_ids_from_core(); |
| 64 |
$addon_template_ids = SupportedPlugins::get_instance()->get_all_addon_supported_template_ids(); |
| 65 |
$supported_template_ids = array_merge( $core_template_ids, $addon_template_ids ); |
| 66 |
|
| 67 |
if ( in_array( $email_id, $supported_template_ids, true ) ) { |
| 68 |
// Sample order fallback |
| 69 |
if ( empty( $order ) ) { |
| 70 |
return self::render_sample_preview( $template_data, $current_email ); |
| 71 |
} |
| 72 |
|
| 73 |
self::trigger_email( $email_class, $current_email, $order_id ); |
| 74 |
$content = self::get_email_content( $current_email ); |
| 75 |
$is_disabled_send_mail = false; |
| 76 |
} else { |
| 77 |
$content = yaymail_get_content( '/templates/preview-email/notice.php', [ 'template_data' => $template_data ] ); |
| 78 |
$is_disabled_send_mail = true; |
| 79 |
} |
| 80 |
|
| 81 |
remove_filter( 'woocommerce_new_order_email_allows_resend', '__return_true', 10 ); |
| 82 |
|
| 83 |
return [ |
| 84 |
'html' => yaymail_kses_post( $content ), |
| 85 |
'subject' => self::get_subject( $current_email, $order ), |
| 86 |
'is_disabled_send_mail' => $is_disabled_send_mail, |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
public static function get_email_title_by_id( $email_id ) { |
| 91 |
$templates = TemplateModel::get_instance()->find_all(); |
| 92 |
foreach ( $templates as $template ) { |
| 93 |
if ( $template['name'] === $email_id ) { |
| 94 |
return $template['template_title']; |
| 95 |
} |
| 96 |
} |
| 97 |
return __( 'Email Subject', 'yaymail' ); |
| 98 |
} |
| 99 |
|
| 100 |
private static function get_subject( $current_email, $order ) { |
| 101 |
$text_replace = [ |
| 102 |
'{order_number}' => ! empty( $order ) ? '#' . $order->get_order_number() : '#1', |
| 103 |
'{order_date}' => ! empty( $order ) ? gmdate( get_option( 'date_format' ), strtotime( $order->get_date_created() ) ) : gmdate( get_option( 'date_format' ) ), |
| 104 |
'{order_id}' => ! empty( $order ) ? '#' . $order->get_id() : '#1', |
| 105 |
'{blogname}' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), |
| 106 |
]; |
| 107 |
|
| 108 |
$subject = ! empty( $current_email->subject ) ? str_replace( array_keys( $text_replace ), $text_replace, $current_email->subject ) : $current_email->get_subject(); |
| 109 |
return $subject; |
| 110 |
} |
| 111 |
|
| 112 |
private static function render_sample_preview( $template_data, $current_email ) { |
| 113 |
$render_data = [ |
| 114 |
'is_sample' => true, |
| 115 |
'is_customized_preview' => true, |
| 116 |
]; |
| 117 |
$content = $template_data->get_content( $render_data ); |
| 118 |
|
| 119 |
return [ |
| 120 |
'html' => yaymail_kses_post( $content ), |
| 121 |
'subject' => self::get_subject( $current_email, '' ), |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
private static function render_default_preview( $current_email, $email_class, $order ) { |
| 126 |
add_filter( |
| 127 |
'woocommerce_email_preview_dummy_order', |
| 128 |
function ( $dummy_order ) use ( $order ) { |
| 129 |
return ! empty( $order ) ? $order : $dummy_order; |
| 130 |
}, |
| 131 |
10, |
| 132 |
2 |
| 133 |
); |
| 134 |
try { |
| 135 |
$email_preview = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview::class ); |
| 136 |
$email_preview->set_email_type( $email_class ); |
| 137 |
$message = $email_preview->render(); |
| 138 |
$message = $email_preview->ensure_links_open_in_new_tab( $message ); |
| 139 |
$content = $message; |
| 140 |
$is_disabled_send_mail = false; |
| 141 |
} catch ( \Throwable $e ) { |
| 142 |
ob_end_clean(); |
| 143 |
$content = yaymail_get_content( '/templates/preview-email/notice.php' ); |
| 144 |
$is_disabled_send_mail = true; |
| 145 |
} |
| 146 |
|
| 147 |
return [ |
| 148 |
'html' => yaymail_kses_post( $content ), |
| 149 |
'subject' => self::get_subject( $current_email, $order ), |
| 150 |
'is_disabled_send_mail' => $is_disabled_send_mail, |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
private static function trigger_email( $email_class, $email, $order_id ) { |
| 155 |
try { |
| 156 |
/** |
| 157 |
* Filter: yaymail_trigger_to_preview_email |
| 158 |
* |
| 159 |
* Allows developers to control whether to use custom email trigger logic |
| 160 |
* instead of the default WooCommerce email trigger. |
| 161 |
* |
| 162 |
* @param bool $should_use_custom_trigger Whether to use custom trigger logic (default: false) |
| 163 |
* @param object $email The email object instance |
| 164 |
* @param int $order_id The order ID |
| 165 |
* |
| 166 |
* @return bool True to use custom trigger via 'yaymail_trigger_email' action, false to use default WooCommerce trigger |
| 167 |
*/ |
| 168 |
if ( apply_filters( 'yaymail_trigger_to_preview_email', false, $email, $order_id ) ) { |
| 169 |
/** |
| 170 |
* Action: yaymail_trigger_email |
| 171 |
* |
| 172 |
* Fired when custom email trigger logic should be used instead of |
| 173 |
* the default WooCommerce email trigger. This allows addons and |
| 174 |
* custom implementations to handle email triggering differently. |
| 175 |
* |
| 176 |
* @param object $email The email object instance |
| 177 |
* @param int $order_id The order ID |
| 178 |
*/ |
| 179 |
do_action( 'yaymail_trigger_email', $email, $order_id ); |
| 180 |
} else { |
| 181 |
// Default WooCommerce email trigger logic |
| 182 |
$order = wc_get_order( $order_id ); |
| 183 |
$email->set_object( $order ); |
| 184 |
$email->trigger( $order_id ); |
| 185 |
}//end if |
| 186 |
} catch ( \Exception $e ) { |
| 187 |
return [ 'error' => $e ]; |
| 188 |
}//end try |
| 189 |
} |
| 190 |
|
| 191 |
private static function get_email_content( $email ) { |
| 192 |
$email->email_type = 'html'; |
| 193 |
$content = $email->get_content(); |
| 194 |
return apply_filters( 'woocommerce_mail_content', $email->style_inline( $content ) ); |
| 195 |
} |
| 196 |
|
| 197 |
public static function no_recipient( $recipient ): string { |
| 198 |
if ( self::$recipient !== '' ) { |
| 199 |
$recipient = self::$recipient; |
| 200 |
} else { |
| 201 |
$recipient = ''; |
| 202 |
} |
| 203 |
return $recipient; |
| 204 |
} |
| 205 |
} |
| 206 |
|