| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\PreviewEmail\Integration; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
* @method static WcSubscriptions get_instance() |
| 10 |
*/ |
| 11 |
class WcSubscriptions { |
| 12 |
use SingletonTrait; |
| 13 |
|
| 14 |
private function __construct() { |
| 15 |
add_filter( 'yaymail_preview_email_woo_additional_order_id', [ $this, 'check_email_subscription' ], 10, 3 ); |
| 16 |
add_action( 'yaymail_preview_email_woo_additional_order_trigger', [ $this, 'trigger_email' ], 10, 3 ); |
| 17 |
} |
| 18 |
|
| 19 |
public function trigger_email( $email, $additional_data, $order_id ) { |
| 20 |
if ( isset( $additional_data['error'] ) ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$email_class = get_class( $email ); |
| 25 |
|
| 26 |
if ( $this->is_email_trigger_order( $email_class ) ) { |
| 27 |
$email->trigger( $order_id, wc_get_order( $order_id ) ); |
| 28 |
} elseif ( $this->is_email_trigger_subscription( $email_class ) && function_exists( 'wcs_get_subscriptions_for_order' ) ) { |
| 29 |
$order_subscriptions = wcs_get_subscriptions_for_order( $order_id ); |
| 30 |
$subscription = array_pop( $order_subscriptions ); |
| 31 |
$email->trigger( $subscription ); |
| 32 |
} else { |
| 33 |
$email->trigger( $order_id, wc_get_order( $order_id ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function check_email_subscription( $result, $email_class, $order_id ) { |
| 38 |
$error_text = sprintf( __( 'This is not a valid subscription order. Please select a valid subscription order ID. <br><br>Go to orders page to check %s.', 'yaymail' ), '<a target="_blank" href="' . admin_url( 'edit.php?post_type=shop_order' ) . '">Order ID</a>' ); |
| 39 |
|
| 40 |
if ( $this->is_email_trigger_subscription( $email_class ) || $this->is_email_trigger_order( $email_class ) ) { |
| 41 |
if ( ! wcs_order_contains_subscription( $order_id ) ) { |
| 42 |
$result = [ 'error' => $error_text ]; |
| 43 |
return $result; |
| 44 |
} |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
return $result; |
| 49 |
} |
| 50 |
|
| 51 |
private function is_email_trigger_order( $class_email ) { |
| 52 |
$array = [ |
| 53 |
'WCS_Email_Completed_Renewal_Order', |
| 54 |
'WCS_Email_Completed_Switch_Order', |
| 55 |
'WCS_Email_Customer_On_Hold_Renewal_Order', |
| 56 |
'WCS_Email_New_Renewal_Order', |
| 57 |
'WCS_Email_New_Switch_Order', |
| 58 |
'WCS_Email_Processing_Renewal_Order', |
| 59 |
'WCS_Email_Payment_Retry', |
| 60 |
'WCS_Email_Customer_Payment_Retry', |
| 61 |
]; |
| 62 |
return in_array( $class_email, $array, true ); |
| 63 |
} |
| 64 |
|
| 65 |
private function is_email_trigger_subscription( $class_email ) { |
| 66 |
$array = [ |
| 67 |
'WCS_Email_Cancelled_Subscription', |
| 68 |
'WCS_Email_Expired_Subscription', |
| 69 |
'WCS_Email_On_Hold_Subscription', |
| 70 |
]; |
| 71 |
return in_array( $class_email, $array, true ); |
| 72 |
} |
| 73 |
} |
| 74 |
|