-
Notifications
You must be signed in to change notification settings - Fork 5
/
wp-simple-smtp.php
173 lines (150 loc) · 4.52 KB
/
wp-simple-smtp.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Adds mail configuration to WordPress in a simple, standardised plugin.
*
* @package sb-simple-smtp
* @author soup-bowl <code@soupbowl.io>
* @license MIT
*
* @wordpress-plugin
* Plugin Name: Simple SMTP
* Description: Adds mail configuration to WordPress in a simple, standardised plugin.
* Plugin URI: https://github.com/soup-bowl/wp-simple-smtp
* Version: development-build
* Author: soup-bowl & Contributors
* Author URI: https://github.com/soup-bowl/wp-simple-smtp
* License: MIT
*/
use wpsimplesmtp\LogService;
use wpsimplesmtp\Singular as Settings;
use wpsimplesmtp\Multisite as SettingsMultisite;
use wpsimplesmtp\QuickConfig;
use wpsimplesmtp\Privacy;
use wpsimplesmtp\Mail;
use wpsimplesmtp\MailDisable;
use wpsimplesmtp\Mailtest;
use wpsimplesmtp\Options;
use wpsimplesmtp\Glance;
/**
* Autoloader.
*/
require_once __DIR__ . '/vendor/autoload.php';
// Override and disable emails if the user has disabled them.
$disabled = ( new Options() )->get( 'disable' );
if ( ! empty( $disabled ) && true === filter_var( $disabled->value, FILTER_VALIDATE_BOOLEAN ) ) {
add_action(
'plugins_loaded',
function () {
global $phpmailer;
$phpmailer = new MailDisable();
}
);
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'email-test', [ new wpsimplesmtp\cli\EmailTest(), 'test_email' ] );
WP_CLI::add_command( 'email-log', [ new wpsimplesmtp\cli\EmailLog(), 'load_log' ] );
WP_CLI::add_command( 'email-view', [ new wpsimplesmtp\cli\EmailLog(), 'view_email' ] );
}
if ( is_admin() ) {
new Settings();
( new Privacy() )->hooks();
( new Glance() )->hooks();
if ( is_multisite() ) {
new SettingsMultisite();
}
}
new Mail();
add_action(
'wpss_clear_resent',
function () {
delete_option( 'wpss_resent' );
}
);
add_action(
'wpss_clear_logs',
function () {
$is_disabled = apply_filters( 'simple_smtp_disable_log_prune', false );
// 2629800 = 1 Month.
if ( ! $is_disabled ) {
( new LogService() )->prune_logs( apply_filters( 'simple_smtp_log_expiry', 2629800 ) );
}
}
);
add_action(
'admin_enqueue_scripts',
function ( $page ) {
$assets_pages = array(
'settings_page_wpsimplesmtp',
'settings_page_wpsimplesmtpms',
'index.php',
);
if ( in_array( $page, $assets_pages, true ) ) {
wp_enqueue_style( 'wpss_admin_css', plugin_dir_url( __FILE__ ) . 'assets/simple-smtp.css', [], '1.4' );
if ( 'index.php' !== $page ) {
wp_enqueue_script( 'wpss_config', plugin_dir_url( __FILE__ ) . 'assets/smtp-config.js', [ 'jquery', 'wp-i18n' ], '1.4', true );
wp_set_script_translations( 'wpss_config', 'simple-smtp' );
$smtp_settings = QuickConfig::settings();
wp_localize_script( 'wpss_config', 'wpss_qc_settings', $smtp_settings );
}
}
}
);
( new Mailtest() )->hooks();
/**
* Actions to be executed on plugin activation.
*/
function wpsmtp_activation() {
if ( ! wp_next_scheduled( 'wpss_clear_resent' ) ) {
wp_schedule_event( time(), 'hourly', 'wpss_clear_resent' );
}
if ( ! wp_next_scheduled( 'wpss_clear_logs' ) ) {
wp_schedule_event( time(), 'hourly', 'wpss_clear_logs' );
}
}
/**
* Actions to be executed on deactivation.
*/
function wpsmtp_deactivation() {
wp_unschedule_event(
wp_next_scheduled( 'wpss_clear_resent' ),
'wpss_clear_resent'
);
// Clear out remaining log files upon deactivation.
( new LogService() )->delete_all_logs();
}
/**
* Create CPT for storing logs.
*/
add_action(
'init',
function () {
( new LogService() )->register_log_storage();
}
);
/**
* Displays plugin errors on admin screen if error criteria is met.
*/
function wpsmtp_has_error() {
$kses_standard = [
'div' => [
'class' => [],
],
'p' => [],
];
if ( ! empty( get_option( 'wpssmtp_keycheck_fail' ) ) ) {
$notice = '<div class="error fade"><p>';
$notice .= __( 'Encryption keys have changed - Please update the SMTP password to avoid email disruption.', 'simple-smtp' );
$notice .= '</p></div>';
echo wp_kses( $notice, $kses_standard );
}
$disabled = ( new Options() )->get( 'disable' );
if ( ! empty( $disabled ) && true === filter_var( $disabled->value, FILTER_VALIDATE_BOOLEAN ) ) {
$notice = '<div class="error fade"><p>';
$notice .= __( 'Emails have been disabled. Please visit the Mail settings if you wish to re-enable them.', 'simple-smtp' );
$notice .= '</p></div>';
echo wp_kses( $notice, $kses_standard );
}
}
add_action( 'admin_notices', 'wpsmtp_has_error' );
register_activation_hook( __FILE__, 'wpsmtp_activation' );
register_deactivation_hook( __FILE__, 'wpsmtp_deactivation' );