PluginProbe
GoSMTP – SMTP for WordPress / trunk
GoSMTP – SMTP for WordPress vtrunk
trunk 1.0.1 1.0.5 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1
gosmtp / init.php

init.php in GoSMTP – SMTP for WordPress trunk, at init.php

340 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * GoSMTP
4 * https://gosmtp.net
5 * (c) Softaculous Team
6 */
7
8 // We need the ABSPATH
9 if (!defined('ABSPATH')) exit;
10
11 define('GOSMTP_BASE', plugin_basename(GOSMTP_FILE));
12 define('GOSMTP_VERSION', '1.2.1');
13 define('GOSMTP_DIR', dirname(GOSMTP_FILE));
14 define('GOSMTP_SLUG', 'gosmtp');
15 define('GOSMTP_URL', plugins_url('', GOSMTP_FILE));
16 define('GOSMTP_CSS', GOSMTP_URL.'/css');
17 define('GOSMTP_JS', GOSMTP_URL.'/js');
18 define('GOSMTP_PRO_URL', 'https://gosmtp.net/pricing?from=plugin');
19 define('GOSMTP_WWW_URL', 'https://gosmtp.net/');
20 define('GOSMTP_DOCS', 'https://gosmtp.net/docs/');
21 define('GOSMTP_DB_PREFIX', 'gosmtp_');
22
23 include_once(GOSMTP_DIR.'/main/functions.php');
24
25 spl_autoload_register('gosmtp_autoload_register');
26 function gosmtp_autoload_register($class){
27
28 if(!preg_match('/GOSMTP\\\\/', $class)){
29 return;
30 }
31
32 $file = strtolower(str_replace( array('GOSMTP', '\\'), array('', DIRECTORY_SEPARATOR), $class));
33 $file = trim(strtolower($file), '/').'.php';
34
35 // For Free
36 if(file_exists(GOSMTP_DIR.'/main/'.$file)){
37 include_once(GOSMTP_DIR.'/main/'.$file);
38 }
39
40 // For Pro
41 if(defined('GOSMTP_PRO_DIR') && file_exists(GOSMTP_PRO_DIR.'/main/'.$file)){
42 include_once(GOSMTP_PRO_DIR.'/main/'.$file);
43 }
44
45 }
46
47 function gosmtp_died(){
48 print_r(error_get_last());
49 }
50 //register_shutdown_function('gosmtp_died');
51
52 // Ok so we are now ready to go
53 register_activation_hook(GOSMTP_FILE, 'gosmtp_activation');
54
55 // Is called when the ADMIN enables the plugin
56 function gosmtp_activation(){
57 global $wpdb;
58
59 add_option('gosmtp_version', GOSMTP_VERSION);
60
61 }
62
63 // Checks if we are to update ?
64 function gosmtp_update_check(){
65 global $wpdb;
66
67 $current_version = get_option('gosmtp_version');
68 $version = (int) str_replace('.', '', $current_version);
69
70 // No update required
71 if($current_version == GOSMTP_VERSION){
72 return true;
73 }
74
75 // Is it first run ?
76 if(empty($current_version)){
77
78 // Reinstall
79 gosmtp_activation();
80
81 // Trick the following if conditions to not run
82 $version = (int) str_replace('.', '', GOSMTP_VERSION);
83
84 }
85
86 // Save the new Version
87 update_option('gosmtp_version', GOSMTP_VERSION);
88
89 }
90
91 // Add action to load GoSMTP
92 add_action('plugins_loaded', 'gosmtp_load_plugin');
93 function gosmtp_load_plugin(){
94 global $gosmtp;
95
96 if(empty($gosmtp)){
97 $gosmtp = new stdClass();
98 }
99
100 // Check if the installed version is outdated
101 gosmtp_update_check();
102
103 $options = get_option('gosmtp_options', array());
104 $gosmtp->options = empty($options) ? array() : $options;
105
106 // Register GoSMTP FREE abilities with the WordPress 6.9+ Abilities API.
107 // Pro abilities are registered separately by the GoSMTP Pro plugin.
108 if(function_exists('wp_register_ability') && !empty($options['ai_abilities']) && !empty($options['ai_abilities']['enabled']) && class_exists('\GOSMTP\AbilitiesRegister')){
109 add_action('wp_abilities_api_categories_init', '\GOSMTP\AbilitiesRegister::register_categories');
110 add_action('wp_abilities_api_init', '\GOSMTP\AbilitiesRegister::register_abilities');
111 }
112 }
113
114 // The function that will be called when the plugin is loaded
115 add_action('wp_mail', 'gosmtp_load_phpmailer');
116 function gosmtp_load_phpmailer($atts){
117 global $gosmtp, $phpmailer;
118
119 if(!class_exists('GOSMTP_PHPMailer')){
120 // Load PHPMailer class, so we can subclass it.
121 require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
122 require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
123 require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
124
125 class GOSMTP_PHPMailer extends \PHPMailer\PHPMailer\PHPMailer {
126
127 // Modify the default send() behaviour of PHPMailer.
128 public function send(){
129
130 global $gosmtp, $phpmailer;
131
132 // Define a custom header, that will be used to identify the plugin and the mailer.
133 $this->XMailer = 'GOSMTP/Mailer/' . $gosmtp->_mailer . ' ' . GOSMTP_VERSION;
134
135 do_action( 'gosmtp_mailer_mail_pre_send' );
136
137 // If mailer not exists or send function not exists
138 if(!method_exists($gosmtp->mailer, 'send')){
139 return parent::send();
140 }
141
142 do_action( 'gosmtp_mailer_mail_send_before' );
143
144 // Are we to enforce from ?
145 $gosmtp->mailer->set_from();
146
147 $exception = false;
148
149 /*
150 * Send the actual email.
151 * We reuse everything, that was preprocessed for usage in \PHPMailer.
152 */
153 try{
154 $is_sent = $gosmtp->mailer->send();
155 }catch(PHPMailer\PHPMailer\Exception $e){
156 $is_sent = false;
157 $exception = $e;
158 }
159
160 // Get backup connection
161 $backup_connection = $gosmtp->mailer->get_backup_connection();
162 $backup_sent = false;
163
164 // Store current values
165 $current_data = array(
166 '_mailer' => $gosmtp->_mailer,
167 'mailer' => $gosmtp->mailer,
168 'conn_id' => $gosmtp->mailer->conn_id
169 );
170
171 // Try to send email with secondary email
172 if(empty($is_sent) && !empty($backup_connection) ){
173
174 $mailer = sanitize_key( $gosmtp->options['mailer'][$backup_connection]['mail_type'] );
175 $class = $gosmtp->mailer_list[$mailer]['class'];
176 $parent_log = $gosmtp->mailer->last_log;
177
178 if(class_exists($class)){
179 $gosmtp->_mailer = $mailer;
180 $gosmtp->mailer = new $class();
181 $gosmtp->mailer->conn_id = $backup_connection;
182 $gosmtp->mailer->parent_log = $parent_log;
183
184 try{
185 $backup_sent = $phpmailer->send();
186 }catch(Exception $e){}
187 }
188
189 }
190
191 // Reset connection
192 $gosmtp->_mailer = $current_data['_mailer'];
193 $gosmtp->mailer = $current_data['mailer'];
194 $gosmtp->mailer->conn_id = $current_data['conn_id'];
195
196 do_action( 'gosmtp_mailer_mail_send_after', $is_sent, $exception, $backup_sent );
197
198 if($exception && !$backup_sent){
199 throw $exception;
200 }
201
202 return $is_sent || $backup_sent;
203 }
204
205 }
206 }
207
208 if($phpmailer instanceof GOSMTP_PHPMailer){
209 return $atts;
210 }
211
212 // Load all mailer
213 $gosmtp->mailer_list = gosmtp_get_mailer_list();
214
215 $connection_key = apply_filters('gosmtp_connection_key', 0);
216
217 // Making sure the filter returns expected type
218 if(!is_string($connection_key) && !is_int($connection_key)){
219 $connection_key = 0;
220 }
221
222 // For PHP Email dont do anything
223 if(empty($gosmtp->options['mailer'][$connection_key]['mail_type'])){
224 return $atts;
225 }
226
227 $mailer = sanitize_key( $gosmtp->options['mailer'][$connection_key]['mail_type'] );
228 $class = $gosmtp->mailer_list[$mailer]['class'];
229
230 if(!class_exists($class)){
231 return $atts;
232 }
233
234 $gosmtp->_mailer = $mailer;
235 $gosmtp->mailer = new $class();
236 $gosmtp->mailer->conn_id = $connection_key;
237
238 // Handle the from email name
239 add_filter('wp_mail_from', [$gosmtp->mailer, 'get_from'], 100, 1);
240
241 $phpmailer = new GOSMTP_PHPMailer(true);
242
243 return $atts;
244 }
245
246 // This adds the left menu in WordPress Admin page
247 add_action('admin_menu', 'gosmtp_admin_menu', 5);
248 function gosmtp_admin_menu() {
249
250 global $wp_version;
251
252 $capability = 'activate_plugins';// TODO : Capability for accessing this page
253
254 // Add the menu page
255 add_menu_page(__('GoSMTP', 'gosmtp'), __('GoSMTP', 'gosmtp'), $capability, 'gosmtp', 'gosmtp_page_handler', 'dashicons-email-alt');
256
257 // Settings Page
258 add_submenu_page( 'gosmtp', __('Settings', 'gosmtp'), __('Settings', 'gosmtp'), $capability, 'gosmtp', 'gosmtp_page_handler');
259
260 // Test Mail Page
261 add_submenu_page( 'gosmtp', 'Test Mail', 'Test Mail', $capability, 'gosmtp#test-mail', 'gosmtp_page_handler');
262
263 // AI Abilities
264 add_submenu_page( 'gosmtp', __('AI Abilities', 'gosmtp'), __('AI Abilities', 'gosmtp') . ' <span style="vertical-align:middle;background:#d63638;font-size:9px;padding:0 6px;border-radius:10px;line-height:18px;">New!</span>' , $capability, 'gosmtp-ai-abilities', 'gosmtp_ai_abilities');
265
266 if(defined('GOSMTP_PREMIUM')){
267
268 // Logs Page
269 add_submenu_page( 'gosmtp', __('Email Logs', 'gosmtp'), __('Email Logs', 'gosmtp'), $capability, 'gosmtp-logs', 'gosmtp_logs_handler');
270
271 // Email reports
272 add_submenu_page( 'gosmtp', __('Email Reports', 'gosmtp'), __('Email Reports', 'gosmtp'), $capability, 'email_reports', 'gosmtp_email_reports_handler');
273
274 // Export Page
275 add_submenu_page( 'gosmtp', __('Export', 'gosmtp'), __('Export', 'gosmtp'), $capability, 'export', 'gosmtp_export_handler');
276
277 // Email reports
278 add_submenu_page( '', __('Email Reports', 'gosmtp'), __('Weekly Email', 'gosmtp'), $capability, 'weekly_email_reports', 'gosmtp_weekly_email_handler');
279
280 // License Page
281 if(!defined('SITEPAD')){
282 add_submenu_page( 'gosmtp', __('License', 'gosmtp'), __('License', 'gosmtp'), $capability, 'gosmtp-license', 'gosmtp_license_handler');
283 }
284 }
285
286 // Support
287 if(!defined('SITEPAD')){
288 add_submenu_page( 'gosmtp', __('Support', 'gosmtp'), __('Support', 'gosmtp'), $capability, 'gosmtp#support', 'gosmtp_page_handler');
289 }
290 }
291
292 // SMTP page Handler
293 function gosmtp_page_handler(){
294 include_once GOSMTP_DIR .'/main/settings.php';
295 gosmtp_settings_page();
296 }
297
298 function gosmtp_ai_abilities(){
299 include_once GOSMTP_DIR .'/main/abilities.php';
300 \GOSMTP\Abilities::ui_abilities();
301 }
302
303
304 function gosmtp_logs_handler(){
305 include_once GOSMTP_PRO_DIR .'/main/smtp-logs.php';
306 }
307
308 function gosmtp_email_reports_handler(){
309 include_once GOSMTP_PRO_DIR .'/main/email-reports.php';
310 gosmtp_reports_table();
311 }
312
313 function gosmtp_export_handler(){
314 include_once GOSMTP_PRO_DIR .'/main/export.php';
315 gosmtp_export_page();
316 }
317
318 function gosmtp_weekly_email_handler(){
319 include_once GOSMTP_PRO_DIR .'/main/weekly_email_reports.php';
320 gosmtp_send_email_reports();
321 }
322
323 function gosmtp_license_handler(){
324 global $gosmtp;
325
326 include_once GOSMTP_PRO_DIR .'/main/license.php';
327 }
328
329 if(wp_doing_ajax()){
330 include_once GOSMTP_DIR.'/main/ajax.php';
331 }
332
333 add_action( 'admin_init', 'gosmtp_admin_init');
334
335 function gosmtp_admin_init(){
336 include_once GOSMTP_DIR .'/main/admin.php';
337
338 gosmtp_admin_hooks();
339
340 }