Admin.php
21.4 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
namespace Tz\WordPress\Tools\Notifications\Settings;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Notifications;
const CAPABILITY = 'manage_notifications';
const MANAGE_SYSTEM_NOTIFICATIONS = 'create_system_notifications';
const SETTING_NS = 'canspam_settings';
const ADMIN_PAGE = 'canspam_admin';
call_user_func(
function () {
$role = get_role('administrator');
$role->add_cap(CAPABILITY);
$role->add_cap(MANAGE_SYSTEM_NOTIFICATIONS);
Vars::$settings = new Tools\WP_Option(SETTING_NS);
Tools\add_actions(__NAMESPACE__ . '\Actions');
}
);
function display_page()
{
if (isset($_GET['action']) && $_GET['action'] == "edit") {
/** @var \StdClass $entry */
$entry = get_post($_GET['page_id']);
$id = $entry->ID;
$details = get_post_meta($id, 'details', true);
$email = get_post_meta($id, 'email', true);
$system = get_post_meta($id, 'system', true);
$push = get_post_meta($id, 'push', true);
$entry->details = $details;
$entry->email = $email;
$entry->system = $system;
$entry->push = $push;
$validation = new Notifications\Validation;
$validation->set_rules('type', 'Notification Type', 'required');
$validation->set_rules('title', 'Description', 'trim|required|min_length[4]');
$validation->set_rules('sendto', 'Send To', 'required');
$type_val = ($_POST && $_POST['type'] == 'scheduled') ? 'required' : '';
$validation->set_rules('execute_date', 'Execute Date', $type_val);
$trigger_val = ($_POST && $_POST['type'] == 'triggered') ? 'required' : '';
$validation->set_rules('trigger', 'Trigger', $trigger_val);
$validation->set_rules('system_message_type', 'System Message Type', 'trim');
foreach (['en', 'fr'] as $lang) {
$validation->set_rules($lang . '_subject', 'Subject', 'trim');
$validation->set_rules($lang . '_text', 'Text Version', 'trim|min_length[16]');
$validation->set_rules($lang . '_html', 'HTML Version', 'trim|min_length[16]');
$validation->set_rules($lang . '_system', 'System Message', 'trim|min_length[16]');
}
//details
if ($validation->run() == true) {
// Clean up data before saving
Tools\tzClean($_POST);
$type = $_POST['type'];
$title = $_POST['title'];
$sendto = $_POST['sendto'];
$executeDate = ($type == 'scheduled' || $type == 'newsletter') ? $_POST['execute_date'] : '0000-00-00 00:00:00';
$trigger = ($type == 'scheduled' || $type == 'newsletter') ? 'scheduled-cron-job' : $_POST['trigger'];
$style = $_POST['email_style'];
$emailData = [];
$systemMessageData = [];
$pushMessageData = [];
foreach (['en', 'fr'] as $lang) {
// email
$subject = $_POST[$lang . '_subject'];
$text = $_POST[$lang . '_text'];
$html = $_POST[$lang . '_html'];
$attachments = [];
$upload_dir = wp_upload_dir();
$uploadDir = $upload_dir['basedir'] . '/notifications/';
fixFilesArray($_FILES[$lang . '_attachment']);
foreach ($_FILES[$lang . '_attachment'] as $position => $file) {
// should output array with indices name, type, tmp_name, error, size
if ($file['name'] != '') {
move_uploaded_file($file['tmp_name'], $uploadDir . $file['name']);
$attachments[] = $file['name'];
}
}
// system
$systemMessageType = $_POST['system_message_type'];
$system = $_POST[$lang . '_system'];
$pushMessageTime = $_POST[$lang . '_push_notifications_time'];
$push_title = $_POST[$lang . '_push_notifications_title'];
$message = $_POST[$lang . '_push_notifications_message'];
$link = $_POST[$lang . '_push_notifications_link'];
$status = $_POST[$lang . '_push_notifications_status'];
if (count($entry->email[$lang . '_attachments']) > 0) {
$attachments = array_merge($entry->email[$lang . '_attachments'], $attachments);
}
$emailData = array_merge(
$emailData,
[
$lang . '_subject' => $subject,
$lang . '_text' => $text,
$lang . '_html' => $html,
$lang . '_attachments' => $attachments,
]
);
$systemMessageData = array_merge(
$systemMessageData,
[
'system_message_type' => $systemMessageType,
$lang . '_message' => $system,
]
);
$pushMessageData = array_merge(
$pushMessageData,
[
'push_message_time' => $pushMessageTime,
$lang . '_title' => $push_title,
$lang . '_message' => $message,
$lang . '_link' => $link,
$lang . '_status' => $status,
]
);
}
update_post_meta(
$id,
'details',
[
'type' => $type,
'sendto' => $sendto,
'status' => $entry->details['status'],
'trigger' => $trigger,
'execute_date' => $executeDate,
]
);
update_post_meta($id, 'send_status', $entry->details['status']);
update_post_meta($id, 'notif_type', $type);
// update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($executeDate));
update_post_meta($id, 'execute_date', strtotime($executeDate));
update_post_meta($id, 'trigger', $trigger);
update_post_meta($id, 'email', $emailData);
update_post_meta($id, 'system', $systemMessageData);
update_post_meta($id, 'style', $style);
update_post_meta($id, 'push', $pushMessageData);
$update = [];
$update['ID'] = $id;
$update['post_title'] = $title;
wp_update_post($update);
$id = $entry->ID;
$details = get_post_meta($id, 'details', true);
$email = get_post_meta($id, 'email', true);
$system = get_post_meta($id, 'system', true);
$push = get_post_meta($id, 'push', true);
$entry->details = $details;
$entry->email = $email;
$entry->system = $system;
$entry->push = $push;
$flash = '<strong>Notification Saved Successfully!</strong><br /><a href=""/wp-admin/admin.php?page=notifications">Click here</a> to view all notifications.</a>';
require_once __DIR__ . '/views/form.php';
} else {
require_once __DIR__ . '/views/form.php';
}
} else {
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
wp_delete_post($_GET['page_id'], true);
} elseif (isset($_GET['action']) && $_GET['action'] == 'archive') {
$id = $_GET['page_id'];
$postdata = get_post_meta($id, 'details', true);
$postdata['status'] = "archived";
update_post_meta($id, 'details', $postdata);
update_post_meta($id, 'send_status', $postdata['status']);
}
// get all the notifications that status != "archived";
$notifications = [];
$notifications['triggered'] = [];
$notifications['scheduled'] = [];
$args = [
'post_type' => 'notifications',
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'desc',
'meta_key' => 'send_status',
'meta_compare' => '=',
'meta_value' => 'pending',
];
$entries = get_posts($args);
foreach ($entries as $entry) {
$id = $entry->ID;
$details = get_post_meta($id, 'details', true);
$email = get_post_meta($id, 'email', true);
$system = get_post_meta($id, 'system', true);
$push = get_post_meta($id, 'push', true);
$entry->trigger = $details['trigger'];
$entry->status = isset($details['status']) ? $details['status'] : 'active';
$entry->type = $details['type'];
$entry->sendto = $details['sendto'];
$entry->is_email = (($email['en_text'] != '' || $email['en_html'] != '')) || (($email['fr_text'] != '' || $email['fr_html'] != '')) ? true : false;
$entry->is_system = (isset($system['en_message']) && $system['en_message'] != '') || (isset($system['fr_message']) && $system['fr_message'] != '') ? true : false;
$entry->push = (isset($push['en_message']) && $push['en_title'] != '') || (isset($push['fr_message']) && $push['en_title'] != '') ? true : false;
$entry->execute_date = $details['execute_date'];
if ($entry->status != 'archived') {
$notifications[$entry->type][] = $entry;
}
}
require_once __DIR__ . '/views/admin.php';
}
}
/**
* @param string $datetime
*
* @return int
*/
function mysqldatetime_to_timestamp($datetime = '')
{
// function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
$l = strlen($datetime);
if (!($l == 10 || $l == 19)) {
return 0;
}
//
$date = $datetime;
$hours = 0;
$minutes = 0;
$seconds = 0;
// DATETIME only
if ($l == 19) {
list($date, $time) = explode(" ", $datetime);
list($hours, $minutes, $seconds) = explode(":", $time);
}
list($year, $month, $day) = explode("-", $date);
return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
/**
* @param $files
*/
function fixFilesArray(&$files)
{
$names = ['name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1];
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
function notification_settings()
{
require_once __DIR__ . '/views/settings.php';
}
function create_notification()
{
$validation = new Notifications\Validation;
$validation->set_rules('type', 'Notification Type', 'required');
$validation->set_rules('title', 'Description', 'trim|required|min_length[4]');
$validation->set_rules('sendto', 'Send To', 'required');
$type_val = ($_POST && $_POST['type'] == 'scheduled') ? 'required' : '';
$validation->set_rules('execute_date', 'Execute Date', $type_val);
$trigger_val = ($_POST && $_POST['type'] == 'triggered') ? 'required' : '';
$validation->set_rules('trigger', 'Trigger', $trigger_val);
$validation->set_rules('system_message_type', 'System Message Type', 'trim');
foreach (['en', 'fr'] as $lang) {
$validation->set_rules($lang . '_subject', 'Subject', 'trim');
// $validation->set_rules($lang.'_text', 'Text Version', 'trim|min_length[16]');
// $validation->set_rules($lang.'_html', 'HTML Version', 'trim|min_length[16]');
$validation->set_rules($lang . '_text', 'Text Version', 'trim');
$validation->set_rules($lang . '_html', 'HTML Version', 'trim');
$validation->set_rules($lang . '_system', 'System Message', 'trim|min_length[16]');
}
if ($_POST && ($_POST['en_subject'] == '' && $_POST['fr_subject'] == '' && $_POST['en_system'] == '' && $_POST['fr_system'] == '')) {
$form_error = true;
require_once __DIR__ . '/views/create.php';
} else {
if ($validation->run() == true) {
// Clean up the data before saving
Tools\tzClean($_POST);
// ok, so now we need to create the notification.
class PostTemplate
{
var $post_title = '';
var $post_content = '';
var $post_status = 'publish';
var $post_type = 'notifications';
var $comment_status = 'closed';
}
// details
$type = $_POST['type'];
$title = $_POST['title'];
$sendto = $_POST['sendto'];
$executeDate = ($type == 'scheduled' || $type == 'newsletter') ? $_POST['execute_date'] : '0000-00-00 00:00:00';
$trigger = ($type == 'scheduled' || $type == 'newsletter') ? 'scheduled-cron-job' : $_POST['trigger'];
$style = $_POST['email_style'];
$emailData = [];
$systemMessageData = [];
$pushMessageData = [];
$def_trid = "";
foreach (['en', 'fr'] as $lang) {
// email
$subject = $_POST[$lang . '_subject'];
$text = $_POST[$lang . '_text'];
$html = strip_word_html($_POST[$lang . '_html']);
$attachments = [];
$uploadDir = wp_upload_dir()['basedir'] . '/notifications/';
fixFilesArray($_FILES[$lang . '_attachment']);
foreach ($_FILES[$lang . '_attachment'] as $position => $file) {
// should output array with indices name, type, tmp_name, error, size
if ($file['name'] != '') {
move_uploaded_file($file['tmp_name'], $uploadDir . $file['name']);
$attachments[] = $file['name'];
}
}
if ($style == 'newsletter') {
$newletter_id = -1;
$author_id = 1;
$slug = strtolower($subject);
$newsletter_type = $_POST['newsletter_type'];
if (null == get_page_by_title($subject)) {
// Set the page ID so that we know the page was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => '1',
'post_name' => $slug,
'post_title' => $subject,
'post_status' => 'publish',
'post_type' => 'newsLetter',
'post_content' => $html,
)
);
if ($post_id) {
// https://wpml.org/wpml-hook/wpml_element_type/
wp_set_object_terms( $post_id, $newsletter_type, 'newsletter_type' );
$wpml_element_type = apply_filters('wpml_element_type', 'newsletter');
// get the language info of the original post
// https://wpml.org/wpml-hook/wpml_element_language_details/
$get_language_args = array('element_id' => $post_id, 'element_type' => 'post_product');
$original_post_language_info = apply_filters('wpml_element_language_details', null, $get_language_args);
$set_language_args = array(
'element_id' => $post_id,
'element_type' => $wpml_element_type,
'trid' => $original_post_language_info->trid,
'language_code' => $lang,
'source_language_code' => $original_post_language_info->language_code,
);
do_action('wpml_set_element_language_details', $set_language_args);
}
if ($post_id && $lang == 'en') {
$postIdEn = $post_id;
}
if ($post_id && $lang == 'fr') {
$postIdFr = $post_id;
}
} else {
$post_id = -2;
}
}
// system
$systemMessageType = $_POST['system_message_type'];
$system = $_POST[$lang . '_system'];
$pushMessageTime = $_POST[$lang . '_push_notifications_time'];
$push_title = $_POST[$lang . '_push_notifications_title'];
$message = $_POST[$lang . '_push_notifications_message'];
$link = $_POST[$lang . '_push_notifications_link'];
$status = $_POST[$lang . '_push_notifications_status'];
$emailData = array_merge(
$emailData,
[
$lang . '_subject' => $subject,
$lang . '_text' => $text,
$lang . '_html' => $html,
$lang . '_attachments' => $attachments,
]
);
$systemMessageData = array_merge(
$systemMessageData,
[
'system_message_type' => $systemMessageType,
$lang . '_message' => $system,
]
);
$pushMessageData = array_merge(
$pushMessageData,
[
'push_message_time' => $pushMessageTime,
$lang . '_title' => $push_title,
$lang . '_message' => $message,
$lang . '_link' => $link,
$lang . '_status' => $status,
]
);
$links[$lang] = get_post_field( 'post_name', $post_id );
}
// make post...
$notification = new PostTemplate();
$notification->post_title = $title;
$notification->post_content = 'Notification created ' . date(
'Y-m-d H:i:s'
) . " --- to be sent on $executeDate";
$notification->post_date_gmt = date('Y-m-d H:i:s', time());
$id = wp_insert_post((array) $notification);
if ($style == 'newsletter') {
update_post_meta($id, 'links', $links);
global $sitepress;
$def_trid = $sitepress->get_element_trid($postIdEn, 'post_newsletter');
$sitepress->set_element_language_details($postIdFr, 'post_newsletter', $def_trid, 'fr');
}
update_post_meta(
$id,
'details',
[
'type' => $type,
'sendto' => $sendto,
'status' => 'pending',
'trigger' => $trigger,
'execute_date' => $executeDate,
]
);
update_post_meta($id, 'send_status', 'pending');
update_post_meta($id, 'notif_type', $type);
// update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($executeDate));
update_post_meta($id, 'execute_date', strtotime($executeDate));
update_post_meta($id, 'trigger', $trigger);
update_post_meta($id, 'email', $emailData);
update_post_meta($id, 'style', $style);
update_post_meta($id, 'system', $systemMessageData);
update_post_meta($id, 'push', $pushMessageData);
$flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
require_once __DIR__ . '/views/create.php';
} else {
require_once __DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php';
}
}
}
function strip_word_html($text, $allowed_tags = '<b><i><sup><sub><em><strong><u><br>')
{
mb_regex_encoding('UTF-8');
//replace MS special characters first
$search = array('/‘/u', '/’/u', '/“/u', '/”/u', '/—/u');
$replace = array('\'', '\'', '"', '"', '-');
$text = preg_replace($search, $replace, $text);
//make sure _all_ html entities are converted to the plain ascii equivalents - it appears
//in some MS headers, some html entities are encoded and some aren't
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
//on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears
//that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
//some MS Style Definitions - this last bit gets rid of any leftover comments */
$num_matches = preg_match_all("/\<!--/u", $text, $matches);
if ($num_matches) {
$text = preg_replace('/\<!--(.)*--\>/isu', '', $text);
}
$text = preg_replace('/(?:width\=\"\d*\")\S/mxi', '>', $text);
$text = preg_replace('/(?:width\=\"\d*%\")\S/mxi', '>', $text);
return $text;
}