-
Notifications
You must be signed in to change notification settings - Fork 4
/
postino.php
142 lines (125 loc) · 3.95 KB
/
postino.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
<?php
/**
* Plugin Name: Postino
* Description: A new breath of life to wp_mail.
* Version: 2.0.1
* Author: Caffeina
* Author URI: https://caffeina.com/
* Plugin URI: https://github.com/caffeinalab/postino
*/
require 'updater.php';
use Postino\Updater;
defined('ABSPATH') or die('No script kiddies please!');
function post_caff_setup_mailer()
{
add_filter('wp_mail', 'post_caff_set_from');
add_action('phpmailer_init', 'post_caff_send_smtp_email', 10, 1);
}
function post_caff_set_from($mail)
{
add_filter('wp_mail_from', function($from) {
return (get_option('mail_sender')==false)
? get_bloginfo('admin_email')
: get_option('mail_sender');
});
add_filter('wp_mail_from_name', function($from_name) {
return (get_option('mail_sender_name')==false)
? get_bloginfo('name')
: get_option('mail_sender_name');
});
}
function post_caff_send_smtp_email($mail)
{
if ( ! is_object( $mail ) ) {
$mail = (object) $phpmailer;
}
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = get_option('smtp_server');
$mail->Username = get_option('smtp_user');
$mail->Password = get_option('smtp_password');
$mail->Port = (get_option('smtp_port')==false)
? 25
: get_option('smtp_port');
$mail->SMTPSecure = (get_option('smtp_secure')=="none" || get_option('smtp_secure')=="")
? null
: get_option('smtp_secure');
}
function post_caff_register_settings()
{
register_setting('postino_options', 'smtp_secure', ['type' => 'string']);
register_setting('postino_options', 'smtp_port', ['type' => 'number']);
register_setting('postino_options', 'smtp_server', ['type' => 'string']);
register_setting('postino_options', 'smtp_user', ['type' => 'string']);
register_setting('postino_options', 'smtp_password', ['type' => 'string']);
register_setting('postino_options', 'mail_sender', ['type' => 'string']);
register_setting('postino_options', 'mail_sender_name', ['type' => 'string']);
}
function post_caff_register_menu_entry()
{
add_options_page(
'Postino settings',
'Postino',
'manage_options',
'postino',
'post_caff_render_option_page'
);
}
function post_caff_render_option_page()
{
include 'Templates/OptionPage.php';
}
function post_caff_check_if_options_exist()
{
$loaded = false;
$options = [
'smtp_secure',
'smtp_port',
'smtp_server',
'smtp_user',
'smtp_password',
'mail_sender',
'mail_sender_name'
];
foreach ($options as $option) {
if (false == get_option($option) && defined(strtoupper('POSTINO_CAFF_'.$option))) {
update_option($option, constant('POSTINO_CAFF_'.strtoupper($option)));
$loaded = true;
}
}
if ($loaded) {
post_caff_render_admin_notice('notice-info', 'I have loaded the config from postino.json. You can now delete it.');
}
}
function post_caff_render_admin_notice($type, $message)
{
add_action(
'admin_notices',
function () use ($type, $message) {
include 'Templates/AdminNotice.php';
}
);
}
function post_caff_setting_button_adder($links)
{
array_splice(
$links,
0,
0,
'<a href="' .admin_url('options-general.php?page=postino') .
'">' . __('Settings') . '</a>'
);
return $links;
}
post_caff_setup_mailer();
// Boot of Postino
if (is_admin()) {
add_action('admin_menu', 'post_caff_register_menu_entry');
add_action('admin_init', 'post_caff_register_settings');
add_action('admin_init', 'post_caff_check_if_options_exist');
add_filter(
'plugin_action_links_'.plugin_basename(__FILE__),
'post_caff_setting_button_adder'
);
(new Updater())->bootUpdateService();
}