| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit(); |
| 3 |
|
| 4 |
if ( ! class_exists( 'LP_Email_Refund_Request_Order_Admin' ) ) { |
| 5 |
/** |
| 6 |
* Class LP_Email_Refund_Request_Order_Admin |
| 7 |
* |
| 8 |
* @package LearnPress/Classes |
| 9 |
*/ |
| 10 |
class LP_Email_Refund_Request_Order_Admin extends LP_Email_Type_Order_Admin { |
| 11 |
/** |
| 12 |
* LP_Email_Refund_Request_Order_Admin constructor. |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
$this->id = 'refund-request-order-admin'; |
| 16 |
$this->title = __( 'Admin', 'learnpress' ); |
| 17 |
$this->description = __( 'Send an email to admin when a customer submits a refund request.', 'learnpress' ); |
| 18 |
$this->default_subject = __( 'A refund request was submitted on {{order_date}}', 'learnpress' ); |
| 19 |
$this->default_heading = __( 'A customer submitted a refund request', 'learnpress' ); |
| 20 |
$this->recipient = LP_Settings::instance()->get( 'emails_' . $this->id . '.recipients', $this->_get_admin_email() ); |
| 21 |
$this->template_html = 'emails/refund/refund-request-order-admin.php'; |
| 22 |
|
| 23 |
parent::__construct(); |
| 24 |
|
| 25 |
$this->support_variables = array_merge( |
| 26 |
$this->support_variables, |
| 27 |
array( |
| 28 |
'{{refund_request_status}}', |
| 29 |
'{{refund_requested_by}}', |
| 30 |
'{{refund_requested_email}}', |
| 31 |
'{{refund_requested_at}}', |
| 32 |
'{{refund_reason}}', |
| 33 |
'{{admin_order_edit_url}}', |
| 34 |
) |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Trigger email. |
| 40 |
* |
| 41 |
* @param array $params |
| 42 |
*/ |
| 43 |
public function handle( array $params ) { |
| 44 |
try { |
| 45 |
$order = $this->check_and_get_order( $params ); |
| 46 |
if ( ! $order ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$event_data = $params[2] ?? array(); |
| 51 |
if ( ! is_array( $event_data ) ) { |
| 52 |
$event_data = array(); |
| 53 |
} |
| 54 |
|
| 55 |
$this->set_data_content( $order ); |
| 56 |
$this->send_email(); |
| 57 |
} catch ( Throwable $e ) { |
| 58 |
LP_Debug::error_log( $e ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Set variables for content email. |
| 64 |
* |
| 65 |
* @param LP_Order $order |
| 66 |
*/ |
| 67 |
public function set_data_content( LP_Order $order ) { |
| 68 |
parent::set_data_content( $order ); |
| 69 |
|
| 70 |
$event_data = learn_press_get_order_refund_event_data( $order ); |
| 71 |
$requested_by = absint( $event_data['requested_by'] ?? 0 ); |
| 72 |
$requested_at = (string) get_post_meta( $order->get_id(), '_lp_refund_requested_at', true ); |
| 73 |
$reason = (string) get_post_meta( $order->get_id(), LP_Order::META_KEY_REFUND_REQUEST_REASON, true ); |
| 74 |
$request_status = $order->get_refund_request(); |
| 75 |
$admin_order_edit_url = add_query_arg( |
| 76 |
array( |
| 77 |
'post' => $order->get_id(), |
| 78 |
'action' => 'edit', |
| 79 |
), |
| 80 |
admin_url( 'post.php' ) |
| 81 |
); |
| 82 |
|
| 83 |
$requested_email = (string) ( $event_data['requester_email'] ?? '' ); |
| 84 |
if ( empty( $requested_email ) && ! empty( $requested_by ) ) { |
| 85 |
$requested_user = get_user_by( 'id', $requested_by ); |
| 86 |
if ( $requested_user instanceof WP_User ) { |
| 87 |
$requested_email = $requested_user->user_email; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$requested_at_display = ''; |
| 92 |
if ( ! empty( $requested_at ) ) { |
| 93 |
$requested_at_timestamp = strtotime( $requested_at ); |
| 94 |
if ( false !== $requested_at_timestamp ) { |
| 95 |
$requested_at_display = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $requested_at_timestamp ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$this->variables = array_merge( |
| 100 |
$this->variables, |
| 101 |
array( |
| 102 |
'{{refund_request_status}}' => $request_status, |
| 103 |
'{{refund_requested_by}}' => (string) $requested_by, |
| 104 |
'{{refund_requested_email}}' => $requested_email, |
| 105 |
'{{refund_requested_at}}' => $requested_at_display, |
| 106 |
'{{refund_reason}}' => $reason, |
| 107 |
'{{admin_order_edit_url}}' => $admin_order_edit_url, |
| 108 |
) |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return new LP_Email_Refund_Request_Order_Admin(); |
| 114 |
} |
| 115 |
|