af91c49d by Jeff Balicki

dd

1 parent 3546c9e4
<?php
namespace Tz\WordPress\Tools\Notifications\Settings;
use Tz\Common;
......@@ -18,7 +19,7 @@ call_user_func(
Vars::$settings = new Tools\WP_Option(SETTING_NS);
Tools\add_actions(__NAMESPACE__.'\Actions');
Tools\add_actions(__NAMESPACE__ . '\Actions');
}
);
......@@ -55,10 +56,10 @@ function display_page()
$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]');
$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
......@@ -70,71 +71,71 @@ function display_page()
$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'];
$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'];
$subject = $_POST[$lang . '_subject'];
$text = $_POST[$lang . '_text'];
$html = $_POST[$lang . '_html'];
$attachments = [];
$upload_dir = wp_upload_dir();
$uploadDir = $upload_dir['basedir'].'/notifications/';
$uploadDir = $upload_dir['basedir'] . '/notifications/';
fixFilesArray($_FILES[$lang.'_attachment']);
fixFilesArray($_FILES[$lang . '_attachment']);
foreach ($_FILES[$lang.'_attachment'] as $position => $file) {
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']);
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'];
$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);
$system = $_POST[$lang . '_system'];
$pushMessageTime = $_POST[$lang . '_push_notifications_time'];
$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
$lang . '_subject' => $subject,
$lang . '_text' => $text,
$lang . '_html' => $html,
$lang . '_attachments' => $attachments
]
);
$systemMessageData = array_merge(
$systemMessageData,
[
'system_message_type' => $systemMessageType,
$lang.'_message' => $system
$lang . '_message' => $system
]
);
$pushMessageData = array_merge(
$pushMessageData,
[
'push_message_time' => $pushMessageTime,
$lang.'_title' => $title,
$lang.'_message' => $message,
$lang.'_link' => $link,
$lang.'_status' => $status
$lang . '_title' => $title,
$lang . '_message' => $message,
$lang . '_link' => $link,
$lang . '_status' => $status
]
);
}
......@@ -148,13 +149,13 @@ function display_page()
'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', mysqldatetime_to_timestamp($executeDate));
update_post_meta($id, 'execute_date', strtotime($executeDate));
update_post_meta($id, 'trigger', $trigger);
......@@ -181,9 +182,9 @@ function display_page()
$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');
require_once(__DIR__ . '/views/form.php');
} else {
require_once(__DIR__.'/views/form.php');
require_once(__DIR__ . '/views/form.php');
}
} else {
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
......@@ -238,7 +239,7 @@ function display_page()
}
}
require_once(__DIR__.'/views/admin.php');
require_once(__DIR__ . '/views/admin.php');
}
}
......@@ -281,7 +282,7 @@ function fixFilesArray(&$files)
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string)$key;
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
......@@ -295,7 +296,7 @@ function fixFilesArray(&$files)
function notification_settings()
{
require_once(__DIR__.'/views/settings.php');
require_once(__DIR__ . '/views/settings.php');
}
function create_notification()
......@@ -315,17 +316,17 @@ function create_notification()
$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]');
$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');
require_once(__DIR__ . '/views/create.php');
} else {
if ($validation->run() == true) {
......@@ -348,146 +349,142 @@ function create_notification()
$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'];
$style = $_POST['email_style'];
$emailData = [];
$systemMessageData = [];
$pushMessageData = [];
foreach (['en', 'fr'] as $lang) {
// email
$subject = $_POST[$lang.'_subject'];
$text = $_POST[$lang.'_text'];
$html = strip_word_html($_POST[$lang.'_html']);
$subject = $_POST[$lang . '_subject'];
$text = $_POST[$lang . '_text'];
$html = strip_word_html($_POST[$lang . '_html']);
$attachments = [];
$uploadDir = wp_upload_dir()['basedir'].'/notifications/';
$uploadDir = wp_upload_dir()['basedir'] . '/notifications/';
fixFilesArray($_FILES[$lang.'_attachment']);
fixFilesArray($_FILES[$lang . '_attachment']);
foreach ($_FILES[$lang.'_attachment'] as $position => $file) {
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']);
move_uploaded_file($file['tmp_name'], $uploadDir . $file['name']);
$attachments[] = $file['name'];
}
}
if($style == 'newsletter'){
$newletter_id = -1;
$author_id = 1;
$slug = strtolower($subject);
if( null == get_page_by_title( $title ) ) {
// 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' => '<div id="newsletter">'.$html.'</div>' ,
)
);
if ($post_id) {
// https://wpml.org/wpml-hook/wpml_element_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 );
}
} else {
$post_id = -2;
}
}
if ($style == 'newsletter') {
$newletter_id = -1;
$author_id = 1;
$slug = strtolower($subject);
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' => '<div id="newsletter">' . $html . '</div>',
)
);
if ($post_id) {
// https://wpml.org/wpml-hook/wpml_element_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);
}
} else {
$post_id = -2;
}
}
// system
$systemMessageType = $_POST['system_message_type'];
$system = $_POST[$lang.'_system'];
$system = $_POST[$lang . '_system'];
$pushMessageTime = $_POST[$lang . '_push_notifications_time'];
$title = $_POST[$lang . '_push_notifications_title'];
$message = $_POST[$lang . '_push_notifications_message'];
$link = $_POST[$lang . '_push_notifications_link'];
$status = $_POST[$lang . '_push_notifications_status'];
$pushMessageTime = $_POST[$lang.'_push_notifications_time'];
$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,
$lang . '_subject' => $subject,
$lang . '_text' => $text,
$lang . '_html' => $html,
$lang . '_attachments' => $attachments,
]
);
$systemMessageData = array_merge(
$systemMessageData,
[
'system_message_type' => $systemMessageType,
$lang.'_message' => $system
$lang . '_message' => $system
]
);
$pushMessageData = array_merge(
$pushMessageData,
[
'push_message_time' => $pushMessageTime,
$lang.'_title' => $title,
$lang.'_message' => $message,
$lang.'_link' => $link,
$lang.'_status' => $status
$lang . '_title' => $title,
$lang . '_message' => $message,
$lang . '_link' => $link,
$lang . '_status' => $status
]
);
$links[$lang] = get_permalink($post_id);
$links[$lang] = get_permalink($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_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);
}
$id = wp_insert_post((array) $notification);
if ($style == 'newsletter') {
update_post_meta($id, 'links', $links);
}
update_post_meta(
$id,
......@@ -498,32 +495,26 @@ function create_notification()
'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', 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, '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');
require_once(__DIR__ . '/views/create.php');
} else {
require_once(__DIR__.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'create.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php');
}
}
}
......@@ -533,7 +524,7 @@ function create_notification()
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('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u');
......@@ -542,16 +533,16 @@ function strip_word_html($text, $allowed_tags = '<b><i><sup><sub><em><strong><u>
//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);
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;
......