b6247d5b by Jeff Balicki

ff

1 parent 85909766
...@@ -364,535 +364,6 @@ function create_notification() ...@@ -364,535 +364,6 @@ function create_notification()
364 // email 364 // email
365 $subject = $_POST[$lang.'_subject']; 365 $subject = $_POST[$lang.'_subject'];
366 $text = $_POST[$lang.'_text']; 366 $text = $_POST[$lang.'_text'];
367 $html = $_POST[$lang.'_html'];
368 $attachments = [];
369 $uploadDir = wp_upload_dir()['basedir'].'/notifications/';
370
371 fixFilesArray($_FILES[$lang.'_attachment']);
372
373 foreach ($_FILES[$lang.'_attachment'] as $position => $file) {
374 // should output array with indices name, type, tmp_name, error, size
375 if ($file['name'] != '') {
376 move_uploaded_file($file['tmp_name'], $uploadDir.$file['name']);
377 $attachments[] = $file['name'];
378 }
379 }
380
381 if($style == 'newsletter'){
382 $newletter_id = -1;
383 $author_id = 1;
384 $slug = strtolower($subject);
385
386 if( null == get_page_by_title( $title ) ) {
387
388 // Set the page ID so that we know the page was created successfully
389 $post_id = wp_insert_post(
390 array(
391 'comment_status' => 'closed',
392 'ping_status' => 'closed',
393 'post_author' => '1',
394 'post_name' => $slug,
395 'post_title' => $subject,
396 'post_status' => 'publish',
397 'post_type' => 'newsLetter',
398 'post_content' => '<div id="newsletter">'.$html.'</div>' ,
399
400 )
401 );
402
403 if ($post_id) {
404 // https://wpml.org/wpml-hook/wpml_element_type/
405 $wpml_element_type = apply_filters( 'wpml_element_type', 'newsletter' );
406
407 // get the language info of the original post
408 // https://wpml.org/wpml-hook/wpml_element_language_details/
409 $get_language_args = array('element_id' => $post_id, 'element_type' => 'post_product' );
410 $original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );
411
412 $set_language_args = array(
413 'element_id' => $post_id,
414 'element_type' => $wpml_element_type,
415 'trid' => $original_post_language_info->trid,
416 'language_code' => $lang,
417 'source_language_code' => $original_post_language_info->language_code
418 );
419
420 do_action( 'wpml_set_element_language_details', $set_language_args );
421 }
422
423 } else {
424
425 $post_id = -2;
426
427 }
428
429 }
430
431 // system
432 $systemMessageType = $_POST['system_message_type'];
433 $system = $_POST[$lang.'_system'];
434
435 $pushMessageTime = $_POST[$lang.'_push_notifications_time'];
436 $title = $_POST[$lang.'_push_notifications_title'];
437 $message = $_POST[$lang.'_push_notifications_message'];
438 $link = $_POST[$lang.'_push_notifications_link'];
439 $status = $_POST[$lang.'_push_notifications_status'];
440
441
442
443 $emailData = array_merge(
444 $emailData,
445 [
446 $lang.'_subject' => $subject,
447 $lang.'_text' => $text,
448 $lang.'_html' => $html,
449 $lang.'_attachments' => $attachments,
450
451 ]
452 );
453 $systemMessageData = array_merge(
454 $systemMessageData,
455 [
456 'system_message_type' => $systemMessageType,
457 $lang.'_message' => $system
458 ]
459 );
460 $pushMessageData = array_merge(
461 $pushMessageData,
462 [
463 'push_message_time' => $pushMessageTime,
464 $lang.'_title' => $title,
465 $lang.'_message' => $message,
466 $lang.'_link' => $link,
467 $lang.'_status' => $status
468 ]
469 );
470
471
472 $links[$lang] = get_permalink($post_id);
473
474 }
475
476
477
478 // make post...
479 $notification = new PostTemplate();
480 $notification->post_title = $title;
481 $notification->post_content = 'Notification created '.date(
482 'Y-m-d H:i:s'
483 )." --- to be sent on $executeDate";
484 $notification->post_date_gmt = date('Y-m-d H:i:s', time());
485
486 $id = wp_insert_post((array)$notification);
487
488 if($style == 'newsletter'){
489 update_post_meta($id, 'links', $links);
490 }
491
492 update_post_meta(
493 $id,
494 'details',
495 [
496 'type' => $type,
497 'sendto' => $sendto,
498 'status' => 'pending',
499 'trigger' => $trigger,
500 'execute_date' => $executeDate
501
502 ]
503 );
504 update_post_meta($id, 'send_status', 'pending');
505 update_post_meta($id, 'notif_type', $type);
506 // update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($executeDate));
507 update_post_meta($id, 'execute_date', strtotime($executeDate));
508
509 update_post_meta($id, 'trigger', $trigger);
510
511
512 update_post_meta($id, 'email', $emailData);
513 update_post_meta($id, 'style', $style);
514 update_post_meta($id, 'system', $systemMessageData);
515 update_post_meta($id, 'push', $pushMessageData);
516
517 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
518 require_once(__DIR__.'/views/create.php');
519
520
521
522
523
524
525 } else {
526 require_once(__DIR__.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'create.php');
527 }
528 }
529 }
530 <?php
531 namespace Tz\WordPress\Tools\Notifications\Settings;
532
533 use Tz\Common;
534 use Tz\WordPress\Tools;
535 use Tz\WordPress\Tools\Notifications;
536
537 const CAPABILITY = 'manage_notifications';
538 const MANAGE_SYSTEM_NOTIFICATIONS = 'create_system_notifications';
539 const SETTING_NS = 'canspam_settings';
540 const ADMIN_PAGE = 'canspam_admin';
541
542 call_user_func(
543 function () {
544 $role = get_role('administrator');
545 $role->add_cap(CAPABILITY);
546 $role->add_cap(MANAGE_SYSTEM_NOTIFICATIONS);
547
548 Vars::$settings = new Tools\WP_Option(SETTING_NS);
549
550 Tools\add_actions(__NAMESPACE__.'\Actions');
551 }
552 );
553
554 function display_page()
555 {
556 if (isset($_GET['action']) && $_GET['action'] == "edit") {
557 /** @var \StdClass $entry */
558 $entry = get_post($_GET['page_id']);
559
560 $id = $entry->ID;
561
562 $details = get_post_meta($id, 'details', true);
563 $email = get_post_meta($id, 'email', true);
564 $system = get_post_meta($id, 'system', true);
565 $push = get_post_meta($id, 'push', true);
566
567 $entry->details = $details;
568 $entry->email = $email;
569 $entry->system = $system;
570 $entry->push = $push;
571
572 $validation = new Notifications\Validation;
573
574 $validation->set_rules('type', 'Notification Type', 'required');
575 $validation->set_rules('title', 'Description', 'trim|required|min_length[4]');
576 $validation->set_rules('sendto', 'Send To', 'required');
577
578 $type_val = ($_POST && $_POST['type'] == 'scheduled') ? 'required' : '';
579 $validation->set_rules('execute_date', 'Execute Date', $type_val);
580
581 $trigger_val = ($_POST && $_POST['type'] == 'triggered') ? 'required' : '';
582 $validation->set_rules('trigger', 'Trigger', $trigger_val);
583
584 $validation->set_rules('system_message_type', 'System Message Type', 'trim');
585
586 foreach (['en', 'fr'] as $lang) {
587 $validation->set_rules($lang.'_subject', 'Subject', 'trim');
588 $validation->set_rules($lang.'_text', 'Text Version', 'trim|min_length[16]');
589 $validation->set_rules($lang.'_html', 'HTML Version', 'trim|min_length[16]');
590 $validation->set_rules($lang.'_system', 'System Message', 'trim|min_length[16]');
591 }
592
593 //details
594 if ($validation->run() == true) {
595
596 // Clean up data before saving
597 Tools\tzClean($_POST);
598
599 $type = $_POST['type'];
600 $title = $_POST['title'];
601 $sendto = $_POST['sendto'];
602 $executeDate = ($type == 'scheduled' || $type == 'newsletter' ) ? $_POST['execute_date'] : '0000-00-00 00:00:00';
603 $trigger = ($type == 'scheduled' || $type == 'newsletter' ) ? 'scheduled-cron-job' : $_POST['trigger'];
604 $style = $_POST['email_style'];
605 $emailData = [];
606 $systemMessageData = [];
607 $pushMessageData = [];
608
609 foreach (['en', 'fr'] as $lang) {
610 // email
611 $subject = $_POST[$lang.'_subject'];
612 $text = $_POST[$lang.'_text'];
613 $html = $_POST[$lang.'_html'];
614 $attachments = [];
615 $upload_dir = wp_upload_dir();
616 $uploadDir = $upload_dir['basedir'].'/notifications/';
617
618 fixFilesArray($_FILES[$lang.'_attachment']);
619
620 foreach ($_FILES[$lang.'_attachment'] as $position => $file) {
621 // should output array with indices name, type, tmp_name, error, size
622 if ($file['name'] != '') {
623 move_uploaded_file($file['tmp_name'], $uploadDir.$file['name']);
624 $attachments[] = $file['name'];
625 }
626 }
627
628 // system
629 $systemMessageType = $_POST['system_message_type'];
630 $system = $_POST[$lang.'_system'];
631
632 $pushMessageTime = $_POST[$lang.'_push_notifications_time'];
633 $title = $_POST[$lang.'_push_notifications_title'];
634 $message = $_POST[$lang.'_push_notifications_message'];
635 $link = $_POST[$lang.'_push_notifications_link'];
636 $status = $_POST[$lang.'_push_notifications_status'];
637
638
639 if (count($entry->email[$lang.'_attachments']) > 0) {
640 $attachments = array_merge($entry->email[$lang.'_attachments'], $attachments);
641 }
642
643 $emailData = array_merge(
644 $emailData,
645 [
646 $lang.'_subject' => $subject,
647 $lang.'_text' => $text,
648 $lang.'_html' => $html,
649 $lang.'_attachments' => $attachments
650 ]
651 );
652 $systemMessageData = array_merge(
653 $systemMessageData,
654 [
655 'system_message_type' => $systemMessageType,
656 $lang.'_message' => $system
657 ]
658 );
659 $pushMessageData = array_merge(
660 $pushMessageData,
661 [
662 'push_message_time' => $pushMessageTime,
663 $lang.'_title' => $title,
664 $lang.'_message' => $message,
665 $lang.'_link' => $link,
666 $lang.'_status' => $status
667 ]
668 );
669 }
670
671 update_post_meta(
672 $id,
673 'details',
674 [
675 'type' => $type,
676 'sendto' => $sendto,
677 'status' => $entry->details['status'],
678 'trigger' => $trigger,
679 'execute_date' => $executeDate
680
681
682 ]
683 );
684 update_post_meta($id, 'send_status', $entry->details['status']);
685 update_post_meta($id, 'notif_type', $type);
686 // update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($executeDate));
687 update_post_meta($id, 'execute_date', strtotime($executeDate));
688 update_post_meta($id, 'trigger', $trigger);
689
690 update_post_meta($id, 'email', $emailData);
691 update_post_meta($id, 'system', $systemMessageData);
692 update_post_meta($id, 'style', $style);
693 update_post_meta($id, 'push', $pushMessageData);
694
695 $update = [];
696 $update['ID'] = $id;
697 $update['post_title'] = $title;
698 wp_update_post($update);
699
700 $id = $entry->ID;
701
702 $details = get_post_meta($id, 'details', true);
703 $email = get_post_meta($id, 'email', true);
704 $system = get_post_meta($id, 'system', true);
705 $push = get_post_meta($id, 'push', true);
706
707 $entry->details = $details;
708 $entry->email = $email;
709 $entry->system = $system;
710 $entry->push = $push;
711
712 $flash = '<strong>Notification Saved Successfully!</strong><br /><a href=""/wp-admin/admin.php?page=notifications">Click here</a> to view all notifications.</a>';
713 require_once(__DIR__.'/views/form.php');
714 } else {
715 require_once(__DIR__.'/views/form.php');
716 }
717 } else {
718 if (isset($_GET['action']) && $_GET['action'] == 'delete') {
719 wp_delete_post($_GET['page_id'], true);
720 } elseif (isset($_GET['action']) && $_GET['action'] == 'archive') {
721 $id = $_GET['page_id'];
722
723 $postdata = get_post_meta($id, 'details', true);
724 $postdata['status'] = "archived";
725
726 update_post_meta($id, 'details', $postdata);
727 update_post_meta($id, 'send_status', $postdata['status']);
728 }
729
730 // get all the notifications that status != "archived";
731 $notifications = [];
732 $notifications['triggered'] = [];
733 $notifications['scheduled'] = [];
734
735 $args = [
736 'post_type' => 'notifications',
737 'numberposts' => -1,
738 'orderby' => 'modified',
739 'order' => 'desc',
740 'meta_key' => 'send_status',
741 'meta_compare' => '=',
742 'meta_value' => 'pending'
743 ];
744
745 $entries = get_posts($args);
746
747 foreach ($entries as $entry) {
748 $id = $entry->ID;
749
750 $details = get_post_meta($id, 'details', true);
751 $email = get_post_meta($id, 'email', true);
752 $system = get_post_meta($id, 'system', true);
753 $push = get_post_meta($id, 'push', true);
754
755 $entry->trigger = $details['trigger'];
756 $entry->status = isset($details['status']) ? $details['status'] : 'active';
757 $entry->type = $details['type'];
758 $entry->sendto = $details['sendto'];
759
760 $entry->is_email = (($email['en_text'] != '' || $email['en_html'] != '')) || (($email['fr_text'] != '' || $email['fr_html'] != '')) ? true : false;
761 $entry->is_system = (isset($system['en_message']) && $system['en_message'] != '') || (isset($system['fr_message']) && $system['fr_message'] != '') ? true : false;
762 $entry->push = (isset($push['en_message']) && $push['en_title'] != '') || (isset($push['fr_message']) && $push['en_title'] != '') ? true : false;
763 $entry->execute_date = $details['execute_date'];
764
765 if ($entry->status != 'archived') {
766 $notifications[$entry->type][] = $entry;
767 }
768 }
769
770 require_once(__DIR__.'/views/admin.php');
771 }
772 }
773
774 /**
775 * @param string $datetime
776 *
777 * @return int
778 */
779 function mysqldatetime_to_timestamp($datetime = '')
780 {
781 // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
782 $l = strlen($datetime);
783 if (!($l == 10 || $l == 19)) {
784 return 0;
785 }
786
787 //
788 $date = $datetime;
789 $hours = 0;
790 $minutes = 0;
791 $seconds = 0;
792
793 // DATETIME only
794 if ($l == 19) {
795 list($date, $time) = explode(" ", $datetime);
796 list($hours, $minutes, $seconds) = explode(":", $time);
797 }
798
799 list($year, $month, $day) = explode("-", $date);
800
801 return mktime($hours, $minutes, $seconds, $month, $day, $year);
802 }
803
804 /**
805 * @param $files
806 */
807 function fixFilesArray(&$files)
808 {
809 $names = ['name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1];
810
811 foreach ($files as $key => $part) {
812 // only deal with valid keys and multiple files
813 $key = (string)$key;
814 if (isset($names[$key]) && is_array($part)) {
815 foreach ($part as $position => $value) {
816 $files[$position][$key] = $value;
817 }
818 // remove old key reference
819 unset($files[$key]);
820 }
821 }
822 }
823
824 function notification_settings()
825 {
826
827 require_once(__DIR__.'/views/settings.php');
828 }
829
830 function create_notification()
831 {
832 $validation = new Notifications\Validation;
833
834 $validation->set_rules('type', 'Notification Type', 'required');
835 $validation->set_rules('title', 'Description', 'trim|required|min_length[4]');
836 $validation->set_rules('sendto', 'Send To', 'required');
837
838 $type_val = ($_POST && $_POST['type'] == 'scheduled') ? 'required' : '';
839 $validation->set_rules('execute_date', 'Execute Date', $type_val);
840
841 $trigger_val = ($_POST && $_POST['type'] == 'triggered') ? 'required' : '';
842 $validation->set_rules('trigger', 'Trigger', $trigger_val);
843
844 $validation->set_rules('system_message_type', 'System Message Type', 'trim');
845
846 foreach (['en', 'fr'] as $lang) {
847 $validation->set_rules($lang.'_subject', 'Subject', 'trim');
848 // $validation->set_rules($lang.'_text', 'Text Version', 'trim|min_length[16]');
849 // $validation->set_rules($lang.'_html', 'HTML Version', 'trim|min_length[16]');
850 $validation->set_rules($lang.'_text', 'Text Version', 'trim');
851 $validation->set_rules($lang.'_html', 'HTML Version', 'trim');
852 $validation->set_rules($lang.'_system', 'System Message', 'trim|min_length[16]');
853 }
854
855 if ($_POST && ($_POST['en_subject'] == '' && $_POST['fr_subject'] == '' && $_POST['en_system'] == '' && $_POST['fr_system'] == '')) {
856 $form_error = true;
857 require_once(__DIR__.'/views/create.php');
858 } else {
859 if ($validation->run() == true) {
860
861 // Clean up the data before saving
862 Tools\tzClean($_POST);
863
864 // ok, so now we need to create the notification.
865 class PostTemplate
866 {
867 var $post_title = '';
868 var $post_content = '';
869 var $post_status = 'publish';
870 var $post_type = 'notifications';
871 var $comment_status = 'closed';
872 }
873
874 // details
875 $type = $_POST['type'];
876 $title = $_POST['title'];
877 $sendto = $_POST['sendto'];
878 $executeDate = ($type == 'scheduled' || $type == 'newsletter') ? $_POST['execute_date'] : '0000-00-00 00:00:00';
879 $trigger = ($type == 'scheduled' || $type == 'newsletter') ? 'scheduled-cron-job' : $_POST['trigger'];
880 $style = $_POST['email_style'];
881 $emailData = [];
882 $systemMessageData = [];
883 $pushMessageData = [];
884
885
886
887
888
889
890
891
892 foreach (['en', 'fr'] as $lang) {
893 // email
894 $subject = $_POST[$lang.'_subject'];
895 $text = $_POST[$lang.'_text'];
896 $html = strip_word_html($_POST[$lang.'_html']); 367 $html = strip_word_html($_POST[$lang.'_html']);
897 $attachments = []; 368 $attachments = [];
898 $uploadDir = wp_upload_dir()['basedir'].'/notifications/'; 369 $uploadDir = wp_upload_dir()['basedir'].'/notifications/';
......