-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-upon-signup.php
More file actions
514 lines (432 loc) · 14.9 KB
/
email-upon-signup.php
File metadata and controls
514 lines (432 loc) · 14.9 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/**
* Handle logic to send Emails to customers
*
* @package gemeindetage-anmeldung
*/
namespace gemeindetag\anmeldung;
add_action( 'save_post_anmeldung', __NAMESPACE__ . '\send_anmelde_emails', 10, 3 );
/**
* send anmelde emails
*
* @param Integer $post_id Post ID
* @param Array $post Post Object
* @param Bolean $is_update Weather or not it is an Update
*/
function send_anmelde_emails( $post_id, $post, $is_update ) {
if ( ! $is_update ) {
do_action( 'send_signup_mail', $post_id );
}
}
add_action( 'updated_post_meta', __NAMESPACE__ . '\send_payment_success_emails', 10, 4 );
/**
* send payment success email
*
* @param Integer $meta_id Meta ID
* @param Integer $post_id Post Post ID
* @param String $meta_key meta key
* @param String $meta_value meta value
*/
function send_payment_success_emails( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( 'status' === $meta_key ) {
$zahlungsbestaetigung_versand = get_post_meta( $post_id, 'zahlungsbestaetigung_versand', true );
if ( 'bezahlt' === $meta_value && ! $zahlungsbestaetigung_versand ) {
do_action( 'send_payment_success_email', $post_id );
}
}
}
add_action( 'send_signup_mail', __NAMESPACE__ . '\set_invoice_sent', 0, 1 );
/**
* set invoice send meta field
*
* @param Integer $post_id ID of the post to update
*/
function set_invoice_sent( $post_id ) {
update_post_meta( $post_id, 'rechnung_versand', true );
};
add_action( 'send_signup_mail', __NAMESPACE__ . '\send_signup_mail', 10, 1 );
/**
* sending the signup email
*
* @param Integer $post_id ID of the post
*/
function send_signup_mail( $post_id ) {
$invoice = get_invoice( $post_id );
$email = get_post_meta( $post_id, 'email', true );
$invoice_id = $post_id;
$vorname = get_post_meta( $post_id, 'vorname', true );
$nachname = get_post_meta( $post_id, 'nachname', true );
$to = $email;
$subject = "Rechnung $invoice_id - Mennonitischer Gemeindetag 2023";
$attachments = [ $invoice ];
$body = "<p>Hallo $vorname,\n \n</p>
<p>herzlichen Dank für deine Anmeldung zum Gemeindetag 2023 in Neuwied. Im Anhang findest du die Rechnung mit der Auflistung aller von dir ausgewählten Veranstaltungen, an denen du teilnehmen möchtest. Bitte überweise den Betrag auf das in der Rechnung angegebene Konto. Deine Anmeldung wird mit Eingang des Rechnungsbetrages abgeschlossen. Hierüber erhältst du erneut eine Information.\n \n</p>
<p>Bitte wende dich bei Rückfragen an folgende E.Mail-Adresse: <a href='mailto:gemeindetag@mennoniten.de'>gemeindetag@mennoniten.de</a> oder an 0 152 29388940.</p>
\n \n
<p>Das AMG Gemeindetag 2023-Team wünscht viel Spaß.</p>";
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'Bcc: anmeldung.gemeindetag@mennoniten.de';
$headers[] = 'From: Gemeindetag 2023 <gemeindetag@mennoniten.de>';
return wp_mail( $to, $subject, $body, $headers, $attachments );
}
/**
* get registration object
*
* @param int $registration_id registration post id
*/
function get_registration( $registration_id ) {
$day_ids = get_post_meta( $registration_id, 'teilnahmetage', true );
$workshops_ids = get_post_meta( $registration_id, 'workshops', true );
$trip_ids = get_post_meta( $registration_id, 'ausfluege', true );
$food_ids = get_post_meta( $registration_id, 'verpflegung', true );
$geb_datum = get_post_meta( $registration_id, 'geb_datum', true );
$ermaessigt_adult = get_post_meta( $registration_id, 'ermaessigt_adult', true );
$vorname = get_post_meta( $registration_id, 'vorname', true );
$nachname = get_post_meta( $registration_id, 'nachname', true );
$street = get_post_meta( $registration_id, 'adresse_straße', true );
$adresse_ort = get_post_meta( $registration_id, 'adresse_ort', true );
$adresse_plz = get_post_meta( $registration_id, 'adresse_plz', true );
$uebernachtung_and_breakfast = get_post_meta( $registration_id, 'uebernachtung_and_breakfast', true );
$ermaessigt_mitarbeiter = get_post_meta( $registration_id, 'ermaessigt_mitarbeiter', true );
$age = intval( get_age( $geb_datum ) );
$days = get_days_by_ids( $day_ids, $age, $ermaessigt_adult, $ermaessigt_mitarbeiter );
$workshops = get_workshops_by_ids( $workshops_ids );
$ausfluege = get_trips_by_ids( $trip_ids );
$food = get_food_by_ids( $food_ids, $age );
return [
'id' => $registration_id,
'day_ids' => $day_ids,
'workshops_ids' => $workshops_ids,
'trip_ids' => $trip_ids,
'food_ids' => $food_ids,
'geb_datum' => $geb_datum,
'ermaessigt_adult' => $ermaessigt_adult,
'vorname' => $vorname,
'nachname' => $nachname,
'street' => $street,
'adresse_ort' => $adresse_ort,
'adresse_plz' => $adresse_plz,
'uebernachtung_and_breakfast' => $uebernachtung_and_breakfast,
'age' => $age,
'days' => $days,
'workshops' => $workshops,
'ausfluege' => $ausfluege,
'food' => $food,
];
}
/**
* get days by ids
*
* @param array $ids array of ids
* @param int $age age of the person
* @param boolean $ermaessigt_adult whether the person gets a reduced price
*/
function get_days_by_ids( $ids, $age, $ermaessigt_adult, $ermaessigt_mitarbeiter ) {
return $ids ? array_map(
function( $id ) use ( $age, $ermaessigt_adult, $ermaessigt_mitarbeiter ) {
return [
'id' => $id,
'title' => get_the_title( $id ),
'price' => get_price(
$id,
$age,
$ermaessigt_adult,
$ermaessigt_mitarbeiter
),
];
},
$ids
) : [];
}
/**
* get workshops by ids
*
* @param array $ids ids
*/
function get_workshops_by_ids( $ids ) {
return $ids ? array_map(
function( $id ) {
$title = get_the_title( $id );
$meta_char = get_post_meta( $id, 'character', true );
$character = $meta_char ? $meta_char : 'W';
$number = get_post_meta( $id, 'nr', true );
return [
'id' => $id,
'title' => "$character$number - $title",
'price' => get_post_meta( $id, 'preis', true ),
];
},
$ids
) : [];
}
/**
* get trips by ids
*
* @param array $ids ids
*/
function get_trips_by_ids( $ids ) {
return $ids ? array_map(
function( $id ) {
$title = get_the_title( $id );
$meta_char = get_post_meta( $id, 'character', true );
$character = $meta_char ? $meta_char : 'A';
$number = get_post_meta( $id, 'nr', true );
return [
'id' => $id,
'title' => "$character$number - $title",
'price' => get_post_meta( $id, 'preis', true ),
];
},
$ids
) : [];
}
function get_food_price( $id, $age = 99 ) {
$price = get_post_meta( $id, 'price', true );
if ( $age < 12) {
return intval( $price ) / 2;
}
return $price;
}
/**
* get food by ids
*
* @param array $ids ids
*/
function get_food_by_ids( $ids, $age = 0 ) {
return $ids ? array_map(
function( $id ) use ( $age ) {
return [
'id' => $id,
'nr' => get_post_meta( $id, 'nr', true ),
'title' => get_the_title( $id ),
'price' => get_food_price( $id, $age ),
];
},
$ids
) : [];
}
/**
* add prices together
*
* @param Integer $carry carry
* @param Integer $item item
*/
function sum( $carry, $item ) {
$carry += \floatval( $item['price'] );
return $carry;
}
/**
* get total price of registration
*
* @param array $registration registration object
*/
function get_total_price( $registration ) {
$workshops_total = array_reduce( $registration['workshops'] ?? [], __NAMESPACE__ . '\sum', 0 );
$trips_total = array_reduce( $registration['ausfluege'] ?? [], __NAMESPACE__ . '\sum', 0 );
$food_total = array_reduce( $registration['food'] ?? [], __NAMESPACE__ . '\sum', 0 );
$days_total = array_reduce( $registration['days'] ?? [], __NAMESPACE__ . '\sum', 0 );
$is_sleeping_on_site = $registration['uebernachtung_and_breakfast'] ?? false;
if ( $is_sleeping_on_site ) {
$days_total += 15;
}
return $workshops_total + $trips_total + $food_total + $days_total;
}
/**
* get json data to send to pdf generator api
*
* @param array $registration registration object
*/
function get_registration_data_for_api( $registration ) {
$data = [
'id' => $registration['id'],
'vorname' => $registration['vorname'],
'nachname' => $registration['nachname'],
'adresse_straße' => $registration['street'],
'adresse_ort' => $registration['adresse_ort'],
'adresse_plz' => $registration['adresse_plz'],
'teilnahmetage' => $registration['days'],
'workshops' => $registration['workshops'],
'ausfluege' => $registration['ausfluege'],
'verpflegung' => $registration['food'],
'uebernachtung_and_breakfast' => $registration['uebernachtung_and_breakfast'] ? [
'title' => 'Massenlager inkl. Frühstück von Freitag (28.04.2023) bis
Montag (01.05.2023)',
'price' => 15,
] : null,
'betrag' => get_total_price( $registration ),
];
return wp_json_encode( $data );
}
/**
* get invoice pdf from api
*
* @param array $registration registration object
*/
function get_invoice_pdf_from_api( $registration ) {
$url = 'http://invoice-generator.fabian-kaegy.com/invoice';
$post_request_options = [
'method' => 'POST',
'timeout' => 100,
'blocking' => true,
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => get_registration_data_for_api( $registration ),
];
$response = wp_remote_post( $url, $post_request_options );
if ( is_wp_error( $response ) ) {
// log to sentry
if ( function_exists( 'wp_sentry_safe' ) ) {
wp_sentry_safe(
function ( \Sentry\State\HubInterface $client ) use ( $response ) {
$client->captureException( $response );
}
);
}
// try again once if it doesn't work
$response = wp_remote_post( $url, $post_request_options );
}
$pdf_data = wp_remote_retrieve_body( $response );
header( 'Content-type: application/pdf' );
$registration_id = $registration['id'];
$file_path = ABSPATH . "/invoices/rechnung-$registration_id.pdf";
$file = \fopen( $file_path, 'w' );
$bites_written = file_put_contents( $file_path, $pdf_data );
return $file_path;
}
/**
* adds total price to registration post meta
*
* @param array $registration registration
*/
function add_total_price_to_registration( $registration ) {
$total_price = get_total_price( $registration );
add_post_meta( $registration['id'], 'betrag', $total_price, true );
}
/**
* get the invoice from API
*
* @param Integer $registration_id ID of Post
*/
function get_invoice( $registration_id ) {
$registration = get_registration( $registration_id );
add_total_price_to_registration( $registration );
return get_invoice_pdf_from_api( $registration );
}
/**
* is date
*
* @param String $date date string
*/
function is_date( $date ) {
return 1 === preg_match(
'~^(((0[1-9]|[12]\\d|3[01])\\.(0[13578]|1[02])\\.((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\.(0[13456789]|1[012])\\.((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\.02\\.((19|[2-9]\\d)\\d{2}))|(29\\.02\\.((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$~',
$date
);
}
/**
* calculates the Age
*
* @param String $geb_date dd.mm.yyyy
* @throws WP_Error Error when not a Date
*/
function get_age( $geb_date ) {
$is_valid_date = is_date( $geb_date );
if ( ! $is_valid_date ) {
error_log('Fehler im Geburtsdatum');
return 'Fehler im Geburtsdatum';
}
$d1 = new \DateTime( $geb_date );
$d2 = new \DateTime( '28.02.2023' );
$diff = $d2->diff( $d1 );
return $diff->y;
}
/**
* gets the price of a given anmeldung
*
* @param Integer $post_id ID of Post
* @param Integer $age Age
* @param Boolean $ermaessigt_adult lower rate
*/
function get_price( $post_id, $age, $ermaessigt_adult = false, $ermaessigt_mitarbeiter = false ) {
$key = 'price_adult';
$meta_values = get_post_meta( $post_id );
if ( $ermaessigt_mitarbeiter ) {
return 0;
}
if ( $age <= 18 || $ermaessigt_adult ) {
$key = 'price_teen';
}
if ( $age < 12 && $age >= 3 ) {
$key = 'price_kid';
}
if ( $age < 3 ) {
return 0;
}
return get_post_meta( $post_id, $key, true );
}
/**
* calculate amount of late payment
*/
function get_late_payment_fee_amount() {
// 2023 there is no late payment fee
return 0;
$first_stage = new \DateTime( '01-03-2022' );
$second_stage = new \DateTime( '15-04-2022' );
$now = new \DateTime();
$fee = 0;
if ( $first_stage < $now ) {
$fee = 10;
}
if ( $second_stage < $now ) {
$fee = 20;
}
return $fee;
}
add_action( 'send_payment_success_email', __NAMESPACE__ . '\send_payment_success_email', 10, 1 );
/**
* send payment success email
*
* @param Integer $post_id ID of Post
*/
function send_payment_success_email( $post_id ) {
$email = get_post_meta( $post_id, 'email', true );
$invoice_id = $post_id;
$vorname = get_post_meta( $post_id, 'vorname', true );
$to = $email;
$headers[] = 'Bcc: anmeldung.gemeindetag@mennoniten.de';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Gemeindetag 2023 <gemeindetag@mennoniten.de>';
$subject = 'Zahlungsbestätigung - Mennonitischer Gemeindetag 2023';
$body = "<p>Hallo $vorname,\n \n</p>
<p>vielen Dank für die Überweisung von Rechnung $invoice_id\n \n</p>
<p>Hiermit ist die Anmeldung zum Gemeindetag 2023 bestätigt. Wir freuen uns, Dich zum Gemeindetag in Neuwied begrüßen zu dürfen.</p>";
return wp_mail( $to, $subject, $body, $headers );
}
add_action( 'send_payment_success_email', __NAMESPACE__ . '\set_payment_success', 15, 1 );
/**
* set payment success meta field
*
* @param Integer $post_id ID of the post to update
*/
function set_payment_success( $post_id ) {
$updated = update_post_meta( $post_id, 'zahlungsbestaetigung_versand', 'true' );
return $updated;
};
add_action( 'send_email', __NAMESPACE__ . '\send_email', 15, 3 );
/**
* send email
*
* @param Number $post_id ID of the anmeldung that the email should be send to
* @param String $subject Subject of the mail
* @param String $message Content of the mail
*/
function send_email( $post_id, $subject, $message ) {
$email = get_post_meta( $post_id, 'email', true );
$vorname = get_post_meta( $post_id, 'vorname', true );
$to = $email;
$headers[] = 'Bcc: gemeindetag@mennoniten.de';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Gemeindetag 2023 <gemeindetag@mennoniten.de>';
$mail_subject = "$subject - Mennonitischer Gemeindetag 2023";
$body = "<p>Hallo $vorname,\n \n</p> <p>$message</p>";
return wp_mail( $to, $subject, $body, $headers );
};