-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrelire.php
96 lines (87 loc) · 3.6 KB
/
trelire.php
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
<?php
/**
* Plugin Name: TreLire
* Description: Nobody needs an email client when you have a website!
* Version: 1.0.1
* Author: Caffeina
* Author URI: https://caffeina.com/
* Plugin URI: https://github.com/caffeinalab/trelire
*/
defined('ABSPATH') or die('No script kiddies please!');
add_action('admin_post_trel_send', 'trel_check_admin_post');
function trel_check_admin_post()
{
$retrieved_nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($retrieved_nonce, 'trel_send_mail')) die('Failed security check');
if (!empty(sanitize_text_field($_POST['to'])) && !empty(sanitize_text_field($_POST['mail_content']))) {
$to = explode(',', sanitize_text_field($_POST['to']));
$to = trel_email_array_validator($to);
if (preg_match('/^(text\/plain|text\/html)$/', sanitize_text_field($_POST['content-type'])))
$content_type = sanitize_text_field($_POST['content-type']);
else
$content_type = 'text/plain';
$from = sanitize_email($_POST['from']);
$reply_to = sanitize_email($_POST['reply-to']);
$cc = explode(',', sanitize_text_field($_POST['cc']));
$cc = trel_email_array_validator($cc);
$bcc = explode(',', sanitize_text_field($_POST['bcc']));
$bcc = trel_email_array_validator($bcc);
$subject = sanitize_text_field($_POST['subject']);
$message = wp_kses_post($_POST['mail_content']);
$attachments = [];
$inserted_attachments = (!empty(sanitize_text_field($_POST['attachment']))) ? explode(',', sanitize_text_field($_POST['attachment'])) : '';
if (is_array($inserted_attachments)) {
foreach ($inserted_attachments as $inserted_attachment) {
$attachments[] = get_attached_file(intval($inserted_attachment));
}
}
$headers = [];
$headers[] = 'Content-type: '.$content_type;
$headers[] = 'From: '.$from;
if ($message == '') {
add_action(
'admin_notices',
function () {
?>
<div class="notice notice-error is-dismissible">
<p>Please write a message.</p>
</div>
<?php
}
);
}
if ($reply_to!= '')
$headers[] = 'Reply-to: '.$reply_to;
foreach ($cc as $cc_addr)
$headers[] = 'cc: '.$cc_addr;
foreach ($bcc as $bcc_addr)
$headers[] = 'bcc: '.$bcc_addr;
$success = wp_mail($to, $subject, $message, $headers, $attachments);
wp_redirect($_SERVER['HTTP_REFERER']."&success=".$success);
} else
wp_redirect($_SERVER['HTTP_REFERER']."&success=".false);
}
function trel_render_emailer_page()
{
function trel_enqueue_media_lib_uploader()
{
//Core media script
wp_enqueue_media();
// Your custom js file
wp_register_script('media-lib-uploader-js', plugins_url('media-lib-uploader.js' , __FILE__ ), array('jquery'));
wp_enqueue_script('media-lib-uploader-js');
}
add_action('admin_enqueue_scripts', 'trel_enqueue_media_lib_uploader');
wp_enqueue_style('style-trelire', plugin_dir_url(__FILE__).'Templates/assets/style.css');
include __DIR__.'/Templates/sending_page.php';
}
add_action('admin_menu', function () {
add_management_page( 'TreLire', 'TreLire', 'publish_posts', 'trelire', 'trel_render_emailer_page');
});
function trel_email_array_validator(array $emails)
{
foreach ($emails as $index => $email) {
$emails[$index] = sanitize_email($email);
}
return $emails;
}