| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin notice that a subscription was cancelled (free). |
| 4 |
* |
| 5 |
* Fires the moment the customer confirms, not when the cancellation finally |
| 6 |
* takes effect, so the store owner hears about it while there is still time to |
| 7 |
* act. Reports the reason and nothing identifying. |
| 8 |
* |
| 9 |
* @package SpringDevs\Subscription\Illuminate\Emails |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SpringDevs\Subscription\Illuminate\Emails; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Admin notice that a subscription was cancelled (free). |
| 18 |
*/ |
| 19 |
class CancellationAdmin extends AdminCancellationEmail { |
| 20 |
|
| 21 |
/** |
| 22 |
* Post meta marking that this cancellation has already been reported, so the |
| 23 |
* pending and final steps cannot both mail. |
| 24 |
*/ |
| 25 |
const SENT_FLAG = '_subscrpt_cancel_admin_emailed'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initialize the class. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->id = 'subscrpt_cancellation_admin'; |
| 32 |
$this->title = __( 'Subscription cancelled ( Admin )', 'subscription' ); |
| 33 |
$this->description = __( 'Sent to the store owner when a customer cancels a subscription.', 'subscription' ); |
| 34 |
|
| 35 |
// A customer's confirm puts an active subscription into `pe_cancelled`, and |
| 36 |
// the cron only finalises it to `cancelled` up to a day later. Listen to |
| 37 |
// both, and let trigger() make sure only the first one sends. |
| 38 |
add_action( 'subscrpt_subscription_pending_cancellation', array( $this, 'trigger' ) ); |
| 39 |
add_action( 'subscrpt_subscription_cancelled_email_notification', array( $this, 'trigger' ) ); |
| 40 |
|
| 41 |
// A reactivated subscription can be cancelled again, and should mail again. |
| 42 |
add_action( 'subscrpt_subscription_resumed', array( $this, 'clear_sent_flag' ) ); |
| 43 |
|
| 44 |
parent::__construct(); |
| 45 |
|
| 46 |
$this->template_html = 'emails/cancellation-admin-html.php'; |
| 47 |
$this->template_plain = 'emails/plains/cancellation-admin-plain.php'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get default subject. |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
public function get_default_subject() { |
| 56 |
return __( '[{site_title}] A subscription was cancelled', 'subscription' ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get default heading. |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function get_default_heading() { |
| 65 |
return __( 'A subscription was cancelled', 'subscription' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Opening sentence of the mail. |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
protected function get_intro() { |
| 74 |
return __( 'A customer has cancelled their subscription.', 'subscription' ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Send once per cancellation, at whichever step comes first. |
| 79 |
* |
| 80 |
* @param int $subscription_id Subscription post ID. |
| 81 |
* @param array $context Event context. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function trigger( $subscription_id, $context = array() ) { |
| 86 |
$subscription_id = (int) $subscription_id; |
| 87 |
|
| 88 |
if ( $subscription_id && get_post_meta( $subscription_id, self::SENT_FLAG, true ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
parent::trigger( $subscription_id, $context ); |
| 93 |
|
| 94 |
if ( $subscription_id ) { |
| 95 |
update_post_meta( $subscription_id, self::SENT_FLAG, '1' ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Let a resumed subscription be reported again if it is cancelled a second time. |
| 101 |
* |
| 102 |
* @param int $subscription_id Subscription post ID. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function clear_sent_flag( $subscription_id ) { |
| 107 |
delete_post_meta( (int) $subscription_id, self::SENT_FLAG ); |
| 108 |
} |
| 109 |
} |
| 110 |
|