| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base for the free plugin's admin cancellation notices. |
| 4 |
* |
| 5 |
* Free deliberately reports less than Pro: the reason and the shape of what |
| 6 |
* happened, but never which customer and never a link to the subscription. The |
| 7 |
* closing block points at Reports, where Pro shows the full churn history. |
| 8 |
* |
| 9 |
* Registered only while Pro is inactive (see Illuminate\Email), so a store never |
| 10 |
* lists both this and Pro's richer equivalent. |
| 11 |
* |
| 12 |
* @package SpringDevs\Subscription\Illuminate\Emails |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace SpringDevs\Subscription\Illuminate\Emails; |
| 16 |
|
| 17 |
use SpringDevs\Subscription\Illuminate\Cancellation; |
| 18 |
use WC_Email; |
| 19 |
|
| 20 |
defined( 'ABSPATH' ) || exit; |
| 21 |
|
| 22 |
/** |
| 23 |
* Base for the free plugin's admin cancellation notices. |
| 24 |
*/ |
| 25 |
abstract class AdminCancellationEmail extends WC_Email { |
| 26 |
|
| 27 |
/** |
| 28 |
* Subscription post ID the mail is about. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
public $subscription_id = 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* Reason context for the mail, as stored with the cancellation. |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $context = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize the class. |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
// Admin-only email - never addressed to the customer. |
| 46 |
$this->customer_email = false; |
| 47 |
$this->recipient = $this->get_default_recipient(); |
| 48 |
|
| 49 |
parent::__construct(); |
| 50 |
|
| 51 |
$this->template_base = SUBSCRPT_TEMPLATES; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Opening sentence of the mail. |
| 56 |
* |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
abstract protected function get_intro(); |
| 60 |
|
| 61 |
/** |
| 62 |
* Add a recipient field to the WooCommerce email settings panel. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function init_form_fields() { |
| 67 |
parent::init_form_fields(); |
| 68 |
|
| 69 |
unset( $this->form_fields['additional_content'] ); |
| 70 |
|
| 71 |
$this->form_fields['recipient'] = array( |
| 72 |
'title' => __( 'Recipient(s)', 'subscription' ), |
| 73 |
'type' => 'text', |
| 74 |
'description' => __( 'Enter recipients (comma-separated) for this email. Defaults to the WooCommerce store email.', 'subscription' ), |
| 75 |
'placeholder' => $this->get_default_recipient(), |
| 76 |
'default' => '', |
| 77 |
'desc_tip' => true, |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the admin recipient address. |
| 83 |
* |
| 84 |
* Prefers the WooCommerce store email; falls back to the WordPress admin email. |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function get_default_recipient() { |
| 89 |
$wc_email = get_option( 'woocommerce_email_from_address' ); |
| 90 |
return ! empty( $wc_email ) ? $wc_email : get_option( 'admin_email' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* The reason the customer gave, or an empty string. |
| 95 |
* |
| 96 |
* Prefers the reason passed with the event; falls back to the stored row so |
| 97 |
* a cancellation still reports its reason when called without context. |
| 98 |
* |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
protected function get_reason_label() { |
| 102 |
if ( ! empty( $this->context['reason_label'] ) ) { |
| 103 |
return (string) $this->context['reason_label']; |
| 104 |
} |
| 105 |
|
| 106 |
$feedback = Cancellation::get_feedback( $this->subscription_id ); |
| 107 |
|
| 108 |
return ! empty( $feedback['reason_label'] ) ? (string) $feedback['reason_label'] : ''; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Variables shared by the HTML and plain templates. |
| 113 |
* |
| 114 |
* Deliberately carries no customer name, customer email or subscription link. |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
protected function get_template_vars() { |
| 119 |
$reason = $this->get_reason_label(); |
| 120 |
|
| 121 |
return array( |
| 122 |
'email_heading' => $this->get_heading(), |
| 123 |
'intro' => $this->get_intro(), |
| 124 |
'reason' => $reason, |
| 125 |
'reported_at' => date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ), |
| 126 |
'reports_url' => admin_url( 'admin.php?page=wp-subscription-stats' ), |
| 127 |
'upgrade_url' => 'https://wpsubscription.co/?utm_source=plugin&utm_medium=admin&utm_campaign=upgrade_pro', |
| 128 |
'email' => $this, |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get the HTML email content. |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public function get_content_html() { |
| 138 |
return wc_get_template_html( $this->template_html, $this->get_template_vars(), '', $this->template_base ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the plain-text email content. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function get_content_plain() { |
| 147 |
return wc_get_template_html( $this->template_plain, $this->get_template_vars(), '', $this->template_base ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Send the mail for one subscription. |
| 152 |
* |
| 153 |
* @param int $subscription_id Subscription post ID. |
| 154 |
* @param array $context Event context (reason_label, offer_accepted…). |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public function trigger( $subscription_id, $context = array() ) { |
| 159 |
$subscription_id = (int) $subscription_id; |
| 160 |
|
| 161 |
if ( ! $subscription_id || ! $this->is_enabled() ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$this->subscription_id = $subscription_id; |
| 166 |
$this->context = is_array( $context ) ? $context : array(); |
| 167 |
$this->placeholders['{subscription_id}'] = (string) $subscription_id; |
| 168 |
|
| 169 |
$saved = $this->get_option( 'recipient' ); |
| 170 |
$this->recipient = ! empty( $saved ) ? $saved : $this->get_default_recipient(); |
| 171 |
|
| 172 |
if ( empty( $this->recipient ) ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
$this->setup_locale(); |
| 177 |
$this->send( $this->recipient, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 178 |
$this->restore_locale(); |
| 179 |
} |
| 180 |
} |
| 181 |
|