trigger()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 1
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit; // Exit if accessed directly.
4
}
5
6
/**
7
 * Failed Renewal/Pre-Order Authentication Notification
8
 *
9
 * @extends WC_Email_Customer_Invoice
10
 */
11
class WC_Stripe_Email_Failed_Renewal_Authentication extends WC_Stripe_Email_Failed_Authentication {
12
	/**
13
	 * Constructor.
14
	 *
15
	 * @param WC_Email[] $email_classes All existing instances of WooCommerce emails.
16
	 */
17 View Code Duplication
	public function __construct( $email_classes = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
		$this->id             = 'failed_renewal_authentication';
19
		$this->title          = __( 'Failed Subscription Renewal SCA Authentication', 'woocommerce-gateway-stripe' );
20
		$this->description    = __( 'Sent to a customer when a renewal fails because the transaction requires an SCA verification. The email contains renewal order information and payment links.', 'woocommerce-gateway-stripe' );
21
		$this->customer_email = true;
22
23
		$this->template_html  = 'emails/failed-renewal-authentication.php';
24
		$this->template_plain = 'emails/plain/failed-renewal-authentication.php';
25
		$this->template_base  = plugin_dir_path( WC_STRIPE_MAIN_FILE ) . 'templates/';
26
27
		// Triggers the email at the correct hook.
28
		add_action( 'wc_gateway_stripe_process_payment_authentication_required', array( $this, 'trigger' ) );
29
30
		if ( isset( $email_classes['WCS_Email_Customer_Renewal_Invoice'] ) ) {
31
			$this->original_email = $email_classes['WCS_Email_Customer_Renewal_Invoice'];
32
		}
33
34
		// We want all the parent's methods, with none of its properties, so call its parent's constructor, rather than my parent constructor.
35
		parent::__construct();
36
	}
37
38
	/**
39
	 * Triggers the email while also disconnecting the original Subscriptions email.
40
	 *
41
	 * @param WC_Order $order The order that is being paid.
42
	 */
43
	public function trigger( $order ) {
44
		if ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order->get_id() ) || wcs_is_subscription( $order->get_id() ) || wcs_order_contains_renewal( $order->get_id() ) ) ) {
45
			parent::trigger( $order );
46
47
			// Prevent the renewal email from WooCommerce Subscriptions from being sent.
48
			if ( isset( $this->original_email ) ) {
49
				remove_action( 'woocommerce_generated_manual_renewal_order_renewal_notification', array( $this->original_email, 'trigger' ) );
50
				remove_action( 'woocommerce_order_status_failed_renewal_notification', array( $this->original_email, 'trigger' ) );
51
			}
52
53
			// Prevent the retry email from WooCommerce Subscriptions from being sent.
54
			add_filter( 'wcs_get_retry_rule_raw', array( $this, 'prevent_retry_notification_email' ), 100, 3 );
55
56
			// Send email to store owner indicating communication is happening with the customer to request authentication.
57
			add_filter( 'wcs_get_retry_rule_raw', array( $this, 'set_store_owner_custom_email' ), 100, 3 );
58
		}
59
	}
60
61
	/**
62
	 * Returns the default subject of the email (modifyable in settings).
63
	 *
64
	 * @return string
65
	 */
66
	public function get_default_subject() {
67
		return __( 'Payment authorization needed for renewal of {site_title} order {order_number}', 'woocommerce-gateway-stripe' );
68
	}
69
70
	/**
71
	 * Returns the default heading of the email (modifyable in settings).
72
	 *
73
	 * @return string
74
	 */
75
	public function get_default_heading() {
76
		return __( 'Payment authorization needed for renewal of order {order_number}', 'woocommerce-gateway-stripe' );
77
	}
78
79
	/**
80
	 * Prevent all customer-facing retry notifications from being sent after this email.
81
	 *
82
	 * @param array $rule_array   The raw details about the retry rule.
83
	 * @param int   $retry_number The number of the retry.
84
	 * @param int   $order_id     The ID of the order that needs payment.
85
	 * @return array
86
	 */
87
	public function prevent_retry_notification_email( $rule_array, $retry_number, $order_id ) {
88
		if ( wcs_get_objects_property( $this->object, 'id' ) === $order_id ) {
89
			$rule_array['email_template_customer'] = '';
90
		}
91
92
		return $rule_array;
93
	}
94
95
	/**
96
	 * Send store owner a different email when the retry is related to an authentication required error.
97
	 *
98
	 * @param array $rule_array   The raw details about the retry rule.
99
	 * @param int   $retry_number The number of the retry.
100
	 * @param int   $order_id     The ID of the order that needs payment.
101
	 * @return array
102
	 */
103
	public function set_store_owner_custom_email( $rule_array, $retry_number, $order_id ) {
104
		if (
105
			wcs_get_objects_property( $this->object, 'id' ) === $order_id &&
106
			'' !== $rule_array['email_template_admin'] // Only send our email if a retry admin email was already going to be sent.
107
		) {
108
			$rule_array['email_template_admin'] = 'WC_Stripe_Email_Failed_Authentication_Retry';
109
		}
110
111
		return $rule_array;
112
	}
113
}
114