| 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.4 |
| 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\Request_Service; |
| 21 |
use \Wpo\Services\Router_Service; |
| 22 |
|
| 23 |
// Prevent public access to this script |
| 24 |
defined('ABSPATH') or die(); |
| 25 |
|
| 26 |
if (!class_exists('\Wpo\MsGraphMailer')) { |
| 27 |
|
| 28 |
class MsGraphMailer |
| 29 |
{ |
| 30 |
|
| 31 |
private $dependencies; |
| 32 |
|
| 33 |
private $no_conflict = true; |
| 34 |
|
| 35 |
public function __construct() |
| 36 |
{ |
| 37 |
add_action('plugins_loaded', array($this, 'load'), 1); |
| 38 |
} |
| 39 |
|
| 40 |
public function load() |
| 41 |
{ |
| 42 |
|
| 43 |
// If WPO365 | LOGIN exists and is active then do not initialize this plugin. |
| 44 |
if (\file_exists(dirname(__DIR__) . '/wpo365-login')) { |
| 45 |
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 46 |
|
| 47 |
if (is_plugin_active('wpo365-login/wpo365-login.php')) { |
| 48 |
add_action('admin_notices', array($this, 'ensure_wpo365_login'), 10, 0); |
| 49 |
return; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
Globals::set_global_vars(__FILE__, __DIR__); |
| 54 |
$this->cache_dependencies(); |
| 55 |
$this->add_wp_hooks(); |
| 56 |
|
| 57 |
$has_route = Router_Service::has_route(); |
| 58 |
} |
| 59 |
|
| 60 |
public function ensure_wpo365_login() |
| 61 |
{ |
| 62 |
echo '<div class="notice notice-error" style="margin-left: 2px;"><p>' |
| 63 |
. 'The <strong>WPO365 | MS GRAPH MAILER</strong> plugin has detected that you have also installed the <strong>WPO365 | LOGIN</strong> plugin. ' |
| 64 |
. 'Since that plugin offers the same functionality <a href="https://www.wpo365.com/features/" target="_blank">plus a lot more</a> and uses the ' |
| 65 |
. 'same configuration-source, the <em>Microsoft Graph Mailer</em> of the <strong>WPO365 | LOGIN</strong> plugin will be used instead. ' |
| 66 |
. '<br><br>' |
| 67 |
. 'See the <a href="https://docs.wpo365.com/article/141-send-email-using-microsoft-graph-mailer" target="_blank">online documentation</a> for details.' |
| 68 |
. '</p></div>'; |
| 69 |
} |
| 70 |
|
| 71 |
private function cache_dependencies() |
| 72 |
{ |
| 73 |
$this->dependencies = Dependency_Service::get_instance(); |
| 74 |
$this->dependencies->add('Request_Service', Request_Service::get_instance(true)); |
| 75 |
} |
| 76 |
|
| 77 |
private function add_wp_hooks() |
| 78 |
{ |
| 79 |
// Do super admin stuff |
| 80 |
if ((is_admin() || is_network_admin()) && Permissions_Helpers::user_is_admin(\wp_get_current_user())) { |
| 81 |
|
| 82 |
// Add and hide wizard (page) |
| 83 |
add_action('admin_menu', '\Wpo\Pages\Wizard_Page::add_management_page'); |
| 84 |
add_action('network_admin_menu', '\Wpo\Pages\Wizard_Page::add_management_page'); |
| 85 |
|
| 86 |
new \Wpo\Pages\License_Page(); |
| 87 |
|
| 88 |
// Show admin notification when WPO365 not properly configured |
| 89 |
add_action('admin_notices', '\Wpo\Services\Notifications_Service::show_admin_notices', 10, 0); |
| 90 |
add_action('network_admin_notices', '\Wpo\Services\Notifications_Service::show_admin_notices', 10, 0); |
| 91 |
add_action('admin_init', '\Wpo\Services\Notifications_Service::dismiss_admin_notices', 10, 0); |
| 92 |
|
| 93 |
// Wire up AJAX backend services |
| 94 |
add_action('wp_ajax_wpo365_delete_settings', '\Wpo\Services\Ajax_Service::delete_settings'); |
| 95 |
add_action('wp_ajax_wpo365_delete_tokens', '\Wpo\Services\Ajax_Service::delete_tokens'); |
| 96 |
add_action('wp_ajax_wpo365_get_settings', '\Wpo\Services\Ajax_Service::get_settings'); |
| 97 |
add_action('wp_ajax_wpo365_update_settings', '\Wpo\Services\Ajax_Service::update_settings'); |
| 98 |
add_action('wp_ajax_wpo365_get_log', '\Wpo\Services\Ajax_Service::get_log'); |
| 99 |
|
| 100 |
// Graph mailer |
| 101 |
add_action('wp_ajax_wpo365_send_test_mail', '\Wpo\Services\Ajax_Service::send_test_mail'); |
| 102 |
add_action('wp_ajax_wpo365_get_mail_authorization_url', '\Wpo\Services\Ajax_Service::get_mail_authorization_url'); |
| 103 |
add_action('wp_ajax_wpo365_get_mail_auth_configuration', '\Wpo\Services\Ajax_Service::get_mail_auth_configuration'); |
| 104 |
|
| 105 |
// Graph mailer auditing |
| 106 |
if (class_exists('\Wpo\Mail\Mail_Db')) { |
| 107 |
// Wire up AJAX backend services |
| 108 |
add_action('wp_ajax_wpo365_get_mail_log', '\Wpo\Mail\Mail_Ajax_Service::get_mail_log'); |
| 109 |
add_action('wp_ajax_wpo365_send_mail_again', '\Wpo\Mail\Mail_Ajax_Service::send_mail_again'); |
| 110 |
add_action('wp_ajax_wpo365_truncate_mail_log', '\Wpo\Mail\Mail_Ajax_Service::truncate_mail_log'); |
| 111 |
} |
| 112 |
|
| 113 |
// Show settings link |
| 114 |
add_filter((is_network_admin() ? 'network_admin_' : '') . 'plugin_action_links_' . $GLOBALS['WPO_CONFIG']['plugin'], '\Wpo\Core\Plugin_Helpers::get_configuration_action_link', 10, 1); |
| 115 |
|
| 116 |
if (class_exists('\Wpo\Core\Plugin_Updater')) { |
| 117 |
add_action('admin_init', '\Wpo\Core\Plugin_Updater::check_for_updates'); |
| 118 |
} |
| 119 |
|
| 120 |
// Add license related messages to WP Admin |
| 121 |
\Wpo\Core\Plugin_Updater::show_license_notices(); |
| 122 |
} |
| 123 |
|
| 124 |
// Clean up on shutdown |
| 125 |
add_action('shutdown', '\Wpo\Services\Request_Service::shutdown'); |
| 126 |
|
| 127 |
add_action('admin_enqueue_scripts', '\Wpo\Core\Script_Helpers::enqueue_wizard', 10, 0); |
| 128 |
|
| 129 |
// Replace phpmailer with Graph Mailer (but only if configured) |
| 130 |
// According to https://developer.wordpress.org/reference/functions/wp_mail/ wp_mail is available after plugins_loaded |
| 131 |
add_action('phpmailer_init', '\Wpo\Mail\Mailer::init', 50); |
| 132 |
add_filter('wp_mail_from', '\Wpo\Mail\Mailer::mail_from', 10, 1); |
| 133 |
|
| 134 |
if (class_exists('\Wpo\Mail\Mail_Db')) { |
| 135 |
// Log each wp_mail in the wpo365_table |
| 136 |
add_filter('wp_mail', '\Wpo\Mail\Mail_Db::add_mail_log', 10, 1); |
| 137 |
} |
| 138 |
|
| 139 |
// Add safe style css |
| 140 |
add_filter('safe_style_css', '\Wpo\Core\WordPress_Helpers::safe_css', 10, 1); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$wpo365_msgraphmailer = new MsGraphMailer(); |
| 146 |
|