forked from iiooana/wordpress-plugin-genpdf-queuemail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
139 lines (128 loc) · 5.11 KB
/
Copy pathcron.php
File metadata and controls
139 lines (128 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
use GenPDF\OrderGenPDF;
use GenPDF\GenPDF;
use GenPDF\OrderEmailGenPDF;
//add every minute wordpress cron
add_filter('cron_schedules', function ($schedules) {
$schedules['every_minute'] = [
'interval' => 60,
'display' => esc_html__('GenPDF every minute')
];
return $schedules;
});
add_action('wp_loaded', function () {
if (!wp_next_scheduled('genpdf_cron')) {
wp_schedule_event(time(), 'every_minute', 'genpdf_cron');
}
});
add_action('genpdf_cron', function () {
global $wpdb;
$genpdf = new GenPDF();
$array_settings = $genpdf->getArraySettings();
$wpdb->query("START TRANSACTION");
try {
$order_queue = OrderEmailGenPDF::getOrderToSendEmail();
if (!empty($order_queue)) {
$genpdf_order = new OrderGenPDF($order_queue['order_id']);
$customer_info = $genpdf_order->getCustomerInfo();
//region generate attachments
$attachments = $genpdf_order->getAttachmentsPDF($array_settings['temp_dir']);
if (empty($attachments)) {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "error",
"message" => "There aren't any attachments for the email."
]);
$wpdb->query("COMMIT");
die("There aren't any attachments for the email.");
}
//endregion
//region CHECKS
if (!empty($genpdf_order->order['status']) && in_array($genpdf_order->order['status'], $array_settings['ok_status_order'])) {
//1. STATUS ORDER OK
if (!empty($customer_info['email']) && filter_var($customer_info['email'], FILTER_VALIDATE_EMAIL)) {
//1a. check if the customer email is valid
$message = $array_settings['templates']['customer'];
$message = str_replace('[numero_ordine]', $genpdf_order->order_id, $message);
$message = str_replace('[nome_cliente]', $customer_info['first_name'], $message);
$message = str_replace('[cognome_nome]', $customer_info['last_name'], $message);
$headers = array('Content-Type: text/html; charset=UTF-8');
//region add CC
if(!empty($array_settings['cc']) && is_array($array_settings['cc'])){
foreach($array_settings['cc'] as $email){
$headers[] = "bcc: $email <$email>";
}
}
//endregion
if (wp_mail($customer_info['email'], "Ordine #" . $genpdf_order->order_id , $message, $headers, $attachments)) {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "success_email_customer",
"email_to" => $customer_info['email'],
], true);
} else {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "error",
"message" => "I can't send email to customer " . $customer_info['email'] . "."
]);
}
} else {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "error",
"message" => "The email " . $customer_info['email'] . " is not valid ."
]);
}
} else if ($genpdf_order->isBonifico()) {
//2. Check if the payment method "Bonifico"
if (empty($order_queue['has_sent_email_admin'])) {
// First email to admins
if (!empty($array_settings['cc'][0]) && filter_var($array_settings['cc'][0], FILTER_VALIDATE_EMAIL)) {
$message = $array_settings['templates']['admin'];
$message = str_replace('[numero_ordine]', $genpdf_order->order_id, $message);
$message = str_replace('[nome_cliente]', $customer_info['first_name'], $message);
$message = str_replace('[cognome_nome]', $customer_info['last_name'], $message);
$headers = array('Content-Type: text/html; charset=UTF-8');
//region add CC
if(!empty($array_settings['cc']) && is_array($array_settings['cc'])){
foreach($array_settings['cc'] as $j => $email){
if($j > 0){
$headers[] = "bcc: $email <$email>";
}
}
}
//endregion
if (wp_mail($array_settings['cc'][0], "Ordine #" . $genpdf_order->order_id . " - Bonifico", $message, $headers, $attachments)) {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "success_email_admin",
"email_to" => $array_settings['cc'],
], true);
//update has_sent_email admin
OrderEmailGenPDF::setHasSentEmailAdmin($genpdf_order->order_id);
//endregion
} else {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "error",
"message" => "I can't send email to admin " . $array_settings['cc'] . "."
]);
}
} else {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "error",
"message" => "There is not email for option _genpdf_email_cc or is not valid " . var_export($array_settings['cc'],true) . "."
]);
}
}
} else {
OrderEmailGenPDF::UpdateOrderEmail($order_queue['order_id'], [
"status" => "payment_pending",
"message" => "The status order is: " . $genpdf_order->order['status'] . " and the payment method is not bonifico."
]);
}
//endregion
$genpdf_order->deleteAttachments($attachments);
}
$wpdb->query("COMMIT");
} catch (\Exception $e) {
var_dump("[ERROR-GENPDF-CRON]".$e->getMessage());
//always commit
$wpdb->query("COMMIT");
}
});