Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created August 25, 2024 06:58
Show Gist options
  • Save goranefbl/996eb9e2f08c94f2afdddd3cc5bbca82 to your computer and use it in GitHub Desktop.
Save goranefbl/996eb9e2f08c94f2afdddd3cc5bbca82 to your computer and use it in GitHub Desktop.
new raf email class
<?php
class WPGens_RAF_Email
{
private $email;
private $coupon_code;
private $name;
private $order_id;
private $type;
/**
* Constructor.
*
* @param array $options
*/
public function __construct($email, $coupon_code_or_referral, $order_id, $type = "referrer")
{
$this->email = $email;
$this->coupon_code = $coupon_code_or_referral;
$this->order_id = $order_id;
$this->type = $type;
$this->name = $this->get_user_name();
}
/**
* Send Email to user
*
* @since 1.0.0
*/
public function send_email()
{
$mailer = WC()->mailer();
$subject = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_email_subject' ));
$heading = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_email_heading' ));
$use_woo_template = get_option( 'gens_raf_use_woo_mail' );
$color = get_option( 'woocommerce_email_base_color' );
$footer_text = get_option( 'woocommerce_email_footer_text' );
$header_image = get_option( 'woocommerce_email_header_image' );
$expiry = get_option( 'gens_raf_coupon_duration' );
$my_account_link = get_option('gens_raf_my_account_url');
$type = $this->type;
$coupon_code = $this->coupon_code;
$referral_code = $this->coupon_code;
$referral_link = esc_url(add_query_arg('raf', $referral_code, trailingslashit($my_account_link)));
// Referrer or friend?
if($type === "friend") {
$subject = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_buyer_email_subject' ));
$user_message = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_buyer_email_message' ));
} else if($type === "reminder") {
$subject = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_reminder_email_subject' ));
$heading = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_reminder_email_heading' ));
$user_message = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_reminder_email_body' ));
$user_message = str_replace( '{{referral_code}}', $referral_code, $user_message);
$user_message = str_replace( '{{referral_link}}', $referral_link, $user_message);
} else {
$user_message = str_replace( '{{name}}', $this->name, get_option( 'gens_raf_email_message' ));
}
// Fallback for {{code}} which is depricated
$user_message = str_replace( '{{code}}', $this->coupon_code, $user_message);
// Expiry date
if($expiry) {
$expiry = date_i18n(wc_date_format(), strtotime('+'.$expiry.' days'));
}
ob_start();
if($use_woo_template === "yes" || $use_woo_template === "1") {
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $subject ) );
} else {
include WPGens_RAF::get_template_path('email-header.php','/emails');
}
if($type === "reminder") {
include WPGens_RAF::get_template_path('email-body-reminder.php','/emails');
} else {
include WPGens_RAF::get_template_path('email-body-coupon.php','/emails');
}
if($use_woo_template === "yes" || $use_woo_template === "1") {
wc_get_template( 'emails/email-footer.php' );
} else {
include WPGens_RAF::get_template_path('email-footer.php','/emails');
}
$message = ob_get_clean();
if($mailer->send( $this->email, $subject, $message)){
if($type === "reminder") {
// @todo
do_action('new_raf_data', 'email_sent', array('email' => $this->email, 'subject' => $subject, 'type' => $type, 'coupon_code' => $coupon_code ) );
} else {
do_action('new_raf_data', 'email_sent', array('email' => $this->email, 'subject' => $subject, 'type' => $type, 'coupon_code' => $coupon_code ) );
}
}
do_action('gens_raf_send_referral_coupon', $this->email, $this->coupon_code, $type);
}
public function get_user_name()
{
$user = get_user_by( 'email', $this->email );
if(!empty($user)) {
return ucwords($user->first_name);
} else {
$order = wc_get_order( $this->order_id );
if(!$order) {
return '';
}
$billing_first_name = ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_first_name : $order->get_billing_first_name();
return ucwords($billing_first_name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment