-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
executable file
·155 lines (129 loc) · 4.54 KB
/
plugin.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
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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
/*
Plugin Name: Send new post by email
Plugin URI: https://drapergiggs.com
Description: Send new posts by email to a mailing list in mailjet.
Version: 0.0.24
Author: Carlos Draper Giggs
Author URI: https://drapergiggs.com
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if (!defined('WPINC')) {
die;
}
// SECTION: ADMIN PAGE
function admin_menu() {
add_menu_page(
__('Posts by Email', 'posts-by-email' ),
__('Posts by email', 'posts-by-email' ),
'manage_options',
'posts-by-email',
'admin_page_contents',
'dashicons-schedule',
3
);
}
add_action('admin_menu', 'admin_menu');
add_action('admin_init', 'settings_init');
function settings_init() {
add_settings_section(
'page_setting_section',
__('Posts by email settings', 'posts-by-email' ),
'section_callback_function',
'posts-by-email'
);
add_settings_field(
'account-sid',
__('Mailjet Account SID', 'posts-by-email' ),
'account_sid_settings_markup',
'posts-by-email',
'page_setting_section'
);
add_settings_field(
'account-secret',
__('Mailjet Account Secret', 'posts-by-email' ),
'account_token_settings_markup',
'posts-by-email',
'page_setting_section'
);
add_settings_field(
'mailing_list_id',
__('Mailing list Id', 'posts-by-email' ),
'mailing_list_id_settings_markup',
'posts-by-email',
'page_setting_section'
);
register_setting('posts-by-email', 'account-sid');
register_setting('posts-by-email', 'account-secret');
register_setting('posts-by-email', 'mailing_list_id');
}
function section_callback_function() {
echo '<p>Here you can setup everything for sending your new posts by email</p>';
}
function admin_page_contents() {
?>
<h1> <?php esc_html_e('Welcome to my custom admin page.', 'my-plugin-textdomain'); ?> </h1>
<form method="POST" action="options.php">
<?php
settings_fields('posts-by-email');
do_settings_sections('posts-by-email');
submit_button();
?>
</form>
<?php
}
function account_sid_settings_markup() {
?>
<label for="account-sid"><?php _e('Account SID', 'account-sid'); ?></label>
<input type="text" id="account-sid" name="account-sid" value="<?php echo get_option('account-sid'); ?>">
<?php
}
function account_token_settings_markup() {
?>
<label for="account-secret"><?php _e('Account secret', 'account-sid'); ?></label>
<input type="text" id="account-secret" name="account-secret" value="<?php echo get_option('account-secret'); ?>">
<?php
}
function mailing_list_id_settings_markup() {
?>
<label for="mailing_list_id"><?php _e('Account secret', 'mailing_list_id'); ?></label>
<input type="text" id="mailing_list_id" name="mailing_list_id" value="<?php echo get_option('mailing_list_id'); ?>">
<?php
}
// SECTION: HOOK FOR NEW POSTS
add_action('transition_post_status', 'send_new_post', 10, 3);
function send_new_post($new_status, $old_status, $post) {
// if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
send_post_by_email($post);
// }
}
function send_post_by_email($post) {
$mail = new PHPMailer();
$subject = $post->post_title;
$content = $post->post_content;
$mailingListId = get_option('mailing_list_id');
$username = get_option('account-sid');
$password = get_option('account-secret');
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'in-v3.mailjet.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $username; //SMTP username
$mail->Password = $password; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('[email protected]', 'Carlos Draper Giggs');
$mail->addAddress($mailingListId); //Add a recipient
$mail->addReplyTo('[email protected]', 'Carlos Draper Giggs');
//Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $content;
$mail->send();
}