PluginProbe
WPO365 | MICROSOFT 365 GRAPH MAILER / 2.37
WPO365 | MICROSOFT 365 GRAPH MAILER v2.37
6.1 6.0 5.10 5.9 5.8 5.7 5.6 5.5 2.33 2.34 2.35 2.36 2.37 2.38 2.39 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 All 60 releases
wpo365-msgraphmailer / wpo365-msgraphmailer.php

wpo365-msgraphmailer.php in WPO365 | MICROSOFT 365 GRAPH MAILER 2.37, at wpo365-msgraphmailer.php

255 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: WPO365 | MICROSOFT 365 GRAPH MAILER
5 * Plugin URI: https://wordpress.org/plugins/wpo365-msgraphmailer
6 * Description: WPO365 | MS GRAPH MAILER re-configures your WordPress website to send transactional emails from one of your Microsoft 365 Exchange Online / Mail enabled accounts using Microsoft Graph instead of - for example - using SMTP.
7 * Version: 2.37
8 * Author: marco@wpo365.com
9 * Author URI: https://www.wpo365.com
10 * License: GPL2+
11 */
12
13 namespace Wpo;
14
15 require __DIR__ . '/vendor/autoload.php';
16
17 use \Wpo\Core\Globals;
18 use \Wpo\Core\Permissions_Helpers;
19 use \Wpo\Services\Dependency_Service;
20 use \Wpo\Services\Options_Service;
21 use \Wpo\Services\Request_Service;
22 use \Wpo\Services\Router_Service;
23
24 // Prevent public access to this script
25 defined('ABSPATH') or die();
26
27 if (!class_exists('\Wpo\MsGraphMailer')) {
28
29 class MsGraphMailer
30 {
31
32 private $dependencies;
33
34 private $no_conflict = true;
35
36 public function __construct()
37 {
38 $this->deactivation_hooks();
39 add_action('plugins_loaded', array($this, 'load'), 1);
40 add_filter('cron_schedules', '\Wpo\Core\Cron_Helpers::add_cron_schedules', 10, 1);
41 }
42
43 public function load()
44 {
45 // If WPO365 | LOGIN exists and is active then do not initialize this plugin.
46 if (\file_exists(dirname(__DIR__) . '/wpo365-login')) {
47 include_once(ABSPATH . 'wp-admin/includes/plugin.php');
48
49 if (is_plugin_active('wpo365-login/wpo365-login.php')) {
50 add_action('admin_notices', array($this, 'ensure_wpo365_login'), 10, 0);
51 return;
52 }
53 }
54
55 Globals::set_global_vars(__FILE__, __DIR__);
56 $this->cache_dependencies();
57 Options_Service::ensure_options_cache();
58 $this->update_request_log();
59 $this->add_wp_hooks();
60 Router_Service::has_route();
61 }
62
63 public function ensure_wpo365_login()
64 {
65 echo '<div class="notice notice-error" style="margin-left: 2px;"><p>'
66 . 'The <strong>WPO365 | MS GRAPH MAILER</strong> plugin has detected that you have also installed the <strong>WPO365 | LOGIN</strong> plugin. '
67 . 'Since that plugin offers the same functionality <a href="https://www.wpo365.com/features/" target="_blank">plus a lot more</a> and uses the '
68 . 'same configuration-source, the <em>Microsoft Graph Mailer</em> of the <strong>WPO365 | LOGIN</strong> plugin will be used instead. '
69 . '<br><br>'
70 . 'See the <a href="https://docs.wpo365.com/article/141-send-email-using-microsoft-graph-mailer" target="_blank">online documentation</a> for details.'
71 . '</p></div>';
72 }
73
74 private function cache_dependencies()
75 {
76 $this->dependencies = Dependency_Service::get_instance();
77 $this->dependencies->add('Request_Service', Request_Service::get_instance(true));
78 }
79
80 private function add_wp_hooks()
81 {
82 // Plugin updater and license checker
83 add_filter('pre_set_site_transient_update_plugins', '\Wpo\Core\Plugin_Helpers::check_for_updates', 10, 1);
84
85 // Do super admin stuff
86 if ((is_admin() || is_network_admin()) && Permissions_Helpers::user_is_admin(\wp_get_current_user())) {
87
88 // Add and hide wizard (page)
89 add_action('admin_menu', '\Wpo\Pages\Wizard_Page::add_management_page');
90 add_action('network_admin_menu', '\Wpo\Pages\Wizard_Page::add_management_page');
91
92 new \Wpo\Pages\License_Page();
93
94 // Show admin notification when WPO365 not properly configured
95 add_action('admin_notices', '\Wpo\Services\Notifications_Service::show_admin_notices', 10, 0);
96 add_action('network_admin_notices', '\Wpo\Services\Notifications_Service::show_admin_notices', 10, 0);
97 add_action('admin_init', '\Wpo\Services\Notifications_Service::dismiss_admin_notices', 10, 0);
98
99 // Wire up AJAX backend services
100 add_action('wp_ajax_wpo365_delete_settings', '\Wpo\Services\Ajax_Service::delete_settings');
101 add_action('wp_ajax_wpo365_delete_tokens', '\Wpo\Services\Ajax_Service::delete_tokens');
102 add_action('wp_ajax_wpo365_get_settings', '\Wpo\Services\Ajax_Service::get_settings');
103 add_action('wp_ajax_wpo365_update_settings', '\Wpo\Services\Ajax_Service::update_settings');
104 add_action('wp_ajax_wpo365_get_log', '\Wpo\Services\Ajax_Service::get_log');
105 add_action('wp_ajax_wpo365_dismiss_wpo_health_messages', '\Wpo\Services\Ajax_Service::dismiss_wpo_health_messages');
106 add_action('wp_ajax_wpo365_get_wpo_health_messages', '\Wpo\Services\Ajax_Service::get_wpo_health_messages');
107 add_action('wp_ajax_wpo365_get_parseable_options', '\Wpo\Services\Ajax_Service::get_parseable_options');
108 add_action('wp_ajax_wpo365_get_insights_summary', '\Wpo\Services\Ajax_Service::get_insights_summary');
109 add_action('wp_ajax_wpo365_get_insights', '\Wpo\Services\Ajax_Service::get_insights');
110 add_action('wp_ajax_wpo365_truncate_insights_data', '\Wpo\Services\Ajax_Service::truncate_insights_data');
111
112 // Graph mailer
113 add_action('wp_ajax_wpo365_send_test_mail', '\Wpo\Services\Ajax_Service::send_test_mail');
114 add_action('wp_ajax_wpo365_get_mail_authorization_url', '\Wpo\Services\Ajax_Service::get_mail_authorization_url');
115 add_action('wp_ajax_wpo365_get_mail_auth_configuration', '\Wpo\Services\Ajax_Service::get_mail_auth_configuration');
116 add_action('wp_ajax_wpo365_try_migrate_mail_app_principal_info', '\Wpo\Services\Ajax_Service::try_migrate_mail_app_principal_info');
117
118 // Graph mailer auditing
119 if (class_exists('\Wpo\Mail\Mail_Db')) {
120 // Wire up AJAX backend services
121 add_action('wp_ajax_wpo365_get_mail_log', '\Wpo\Mail\Mail_Ajax_Service::get_mail_log');
122 add_action('wp_ajax_wpo365_send_mail_again', '\Wpo\Mail\Mail_Ajax_Service::send_mail_again');
123 add_action('wp_ajax_wpo365_truncate_mail_log', '\Wpo\Mail\Mail_Ajax_Service::truncate_mail_log');
124
125 if (method_exists('\Wpo\Mail\Mail_Ajax_Service', 'mail_auto_retry')) {
126 add_action('wp_ajax_wpo365_mail_auto_retry', '\Wpo\Mail\Mail_Ajax_Service::mail_auto_retry');
127 }
128 }
129
130 // Show settings link
131 add_filter((is_network_admin() ? 'network_admin_' : '') . 'plugin_action_links_' . $GLOBALS['WPO_CONFIG']['plugin'], '\Wpo\Core\Plugin_Helpers::get_configuration_action_link', 10, 1);
132
133 // Add license related messages to WP Admin
134 \Wpo\Core\Plugin_Helpers::show_license_notices();
135
136 // Ensure WP Cron job to check for each registered application whether its secret will epxire soon is added.
137
138 if (class_exists('\Wpo\Services\Password_Credentials_Service')) {
139 \Wpo\Services\Password_Credentials_Service::ensure_check_password_credentials_expiration();
140 }
141
142 // To force WordPress to check for plugin updates if requested by an administrator
143 add_action('admin_post_wpo365_force_check_for_plugin_updates', '\Wpo\Core\Plugin_Helpers::force_check_for_plugin_updates');
144 add_filter('plugin_row_meta', '\Wpo\Core\Plugin_Helpers::show_old_version_warning', 10, 2);
145 add_filter('plugins_api', '\Wpo\Core\Plugin_Helpers::plugin_info', 20, 3);
146 } // End of admin stuff
147
148 // WP Cron job triggered action to check for each registered application whether its secret will epxire soon.
149 add_action('wpo_check_password_credentials_expiration', '\Wpo\Services\Password_Credentials_Service::check_password_credentials_expiration');
150
151 // Add custom cron schedule for user sync
152 add_filter('cron_schedules', '\Wpo\Core\Cron_Helpers::add_cron_schedules', 10, 1);
153
154 // Clean up on shutdown
155 add_action('shutdown', '\Wpo\Services\Request_Service::shutdown', PHP_INT_MAX);
156
157 add_action('admin_enqueue_scripts', '\Wpo\Core\Script_Helpers::enqueue_wizard', 10, 0);
158
159 // Replace phpmailer with Graph Mailer (but only if configured)
160 // According to https://developer.wordpress.org/reference/functions/wp_mail/ wp_mail is available after plugins_loaded
161 add_action('phpmailer_init', '\Wpo\Mail\Mailer::init', PHP_INT_MAX);
162 add_filter('wp_mail_from', '\Wpo\Mail\Mailer::mail_from', 10, 1);
163
164 if (Options_Service::get_global_boolean_var('mail_log', false) && method_exists('\Wpo\Mail\Mail_Db', 'add_mail_log')) {
165 // Log each wp_mail in the wpo365_table
166 add_filter('wp_mail', '\Wpo\Mail\Mail_Db::add_mail_log', 10, 1);
167 }
168
169 if (Options_Service::get_global_boolean_var('mail_throttling_enabled') && method_exists('\Wpo\Mail\Mail_Db', 'check_message_rate_limit')) {
170 add_filter('wpo365/mail/before', '\Wpo\Mail\Mail_Db::check_message_rate_limit');
171 }
172
173 if (Options_Service::get_global_boolean_var('mail_auto_retry') && method_exists('\Wpo\Mail\Mail_Db', 'process_unsent_messages')) {
174 add_action('wpo_process_unsent_messages', '\Wpo\Mail\Mail_Db::process_unsent_messages');
175 add_action('admin_init', function () {
176 \Wpo\Mail\Mail_Db::ensure_unsent_messages(false);
177 });
178 }
179
180 // Add safe style css
181 add_filter('safe_style_css', '\Wpo\Core\WordPress_Helpers::safe_css', 10, 1);
182
183 // Admin menu bar notifications
184 if (Options_Service::get_global_boolean_var('mail_staging_mode', false) && class_exists('\Wpo\Mail\Mail_Notifications')) {
185 add_action('admin_bar_menu', '\Wpo\Mail\Mail_Notifications::staging_mode_active', 100);
186 add_action('wp_enqueue_scripts', '\Wpo\Core\Script_Helpers::add_admin_bar_styles');
187 add_action('admin_enqueue_scripts', '\Wpo\Core\Script_Helpers::add_admin_bar_styles');
188 }
189
190 // Update WPO365 extensions cache whenever a plugin is added or deleted
191 add_action('activated_plugin', '\Wpo\Core\Extensions_Helpers::plugin_activated', 10, 2);
192 add_action('deactivated_plugin', '\Wpo\Core\Extensions_Helpers::plugin_deactivated', 10, 2);
193
194 // Check again for updates after upgrader process completes to fix "Update available notice"
195 add_action('upgrader_process_complete', '\Wpo\Core\Extensions_Helpers::plugin_updated', 10, 2);
196
197 // To collect Insights
198 if (Options_Service::get_global_boolean_var('insights_enabled', false)) {
199 add_action('wpo365/mail/sent', '\Wpo\Services\Event_Service::mail_sent__handler', 10, 1);
200 add_action('wpo365/mail/sent/fail', '\Wpo\Services\Event_Service::mail_sent_fail__handler', 10, 1);
201 }
202
203 // To collect cURL logging
204 if (Options_Service::get_global_boolean_var('curl_logging_enabled')) {
205 add_action('http_api_curl', '\Wpo\Services\Log_Service::enable_curl_logging', 10, 3);
206 }
207 }
208
209 private function update_request_log()
210 {
211 $request_service = Request_Service::get_instance();
212 $request = $request_service->get_request($GLOBALS['WPO_CONFIG']['request_id']);
213 $request_log = $request->get_item('request_log');
214 $request_log['debug_log'] = Options_Service::get_global_boolean_var('debug_log', false);
215 $request->set_item('request_log', $request_log);
216 }
217
218 private function deactivation_hooks()
219 {
220 if (\class_exists('\Wpo\Mail\Mail_Db')) {
221 // Delete possible cron jobs
222 register_deactivation_hook(__FILE__, function () {
223 wp_clear_scheduled_hook('wpo_process_unsent_messages');
224 Options_Service::add_update_option('mail_auto_retry', false);
225 });
226 }
227
228 register_deactivation_hook(__FILE__, function () {
229 global $wpdb;
230
231 $wpdb->query(
232 $wpdb->prepare(
233 "DELETE FROM $wpdb->options
234 WHERE `option_name` LIKE '%s'
235 AND `option_name` != '%s'
236 AND `option_name` != '%s'",
237 '%wpo365%',
238 'wpo365_options',
239 'wpo365_mail_authorization'
240 )
241 );
242
243 $wpdb->query(
244 $wpdb->prepare(
245 "DELETE FROM $wpdb->options WHERE `option_name` LIKE '%s'",
246 'wpo_app_only_access_tokens'
247 )
248 );
249 });
250 }
251 }
252 }
253
254 $wpo365_msgraphmailer = new MsGraphMailer();
255