| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP Offload SES Lite |
| 4 |
Description: Automatically send WordPress mail through Amazon SES (Simple Email Service). |
| 5 |
Author: Delicious Brains |
| 6 |
Version: 1.8.1 |
| 7 |
Author URI: https://deliciousbrains.com/ |
| 8 |
Plugin URI: https://deliciousbrains.com/ |
| 9 |
Network: True |
| 10 |
Text Domain: wp-offload-ses |
| 11 |
Domain Path: /languages/ |
| 12 |
License: GPLv2 or later |
| 13 |
License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 14 |
|
| 15 |
// Copyright (c) 2018 Delicious Brains. All rights reserved. |
| 16 |
// |
| 17 |
// Released under the GPL license |
| 18 |
// http://www.opensource.org/licenses/gpl-license.php |
| 19 |
// |
| 20 |
// ********************************************************************** |
| 21 |
// This program is distributed in the hope that it will be useful, but |
| 22 |
// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 24 |
// ********************************************************************** |
| 25 |
*/ |
| 26 |
|
| 27 |
use DeliciousBrains\WP_Offload_SES\Utils; |
| 28 |
use DeliciousBrains\WP_Offload_SES\WP_Offload_SES; |
| 29 |
use DeliciousBrains\WP_Offload_SES\Compatibility_Check; |
| 30 |
|
| 31 |
// Exit if accessed directly. |
| 32 |
if ( ! defined( 'ABSPATH' ) ) { |
| 33 |
exit; |
| 34 |
} |
| 35 |
|
| 36 |
// Avoid clash with WP Offload SES (Pro) if both installed as must-use plugins. |
| 37 |
if ( defined( 'WPOSES_FILE' ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- wposes is the plugin's established prefix. |
| 42 |
$GLOBALS['wposes_meta']['wp-ses']['version'] = '1.8.1'; |
| 43 |
|
| 44 |
if ( ! defined( 'WPOSESLITE_FILE' ) ) { |
| 45 |
// Defines the path to the main plugin file. |
| 46 |
define( 'WPOSESLITE_FILE', __FILE__ ); |
| 47 |
|
| 48 |
// Defines the path to be used for includes. |
| 49 |
define( 'WPOSESLITE_PATH', wposes_lite_get_plugin_dir_path() ); |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! class_exists( 'DeliciousBrains\WP_Offload_SES\Compatibility_Check' ) ) { |
| 53 |
require_once WPOSESLITE_PATH . 'classes/Compatibility-Check.php'; |
| 54 |
} |
| 55 |
|
| 56 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- wposes is the plugin's established prefix. |
| 57 |
global $wposes_compat_check; |
| 58 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound |
| 59 |
$wposes_compat_check = new DeliciousBrains\WP_Offload_SES\Compatibility_Check( |
| 60 |
'WP Offload SES Lite', |
| 61 |
'wp-ses', |
| 62 |
WPOSESLITE_FILE |
| 63 |
); |
| 64 |
|
| 65 |
add_action( 'activated_plugin', array( $wposes_compat_check, 'deactivate_other_instances' ) ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Initiate the WP Offload SES plugin. |
| 69 |
*/ |
| 70 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 71 |
function wp_offload_ses_lite_init() { |
| 72 |
if ( class_exists( 'DeliciousBrains\WP_Offload_SES\WP_Offload_SES' ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
/** @var WP_Offload_SES $wp_offload_ses */ |
| 77 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound |
| 78 |
global $wp_offload_ses; |
| 79 |
|
| 80 |
/** @var Compatibility_Check $wposes_compat_check */ |
| 81 |
global $wposes_compat_check; |
| 82 |
|
| 83 |
if ( $wposes_compat_check->is_plugin_active( 'wp-offload-ses/wp-offload-ses.php' ) ) { |
| 84 |
// Don't load if the pro version is installed. |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! $wposes_compat_check->is_compatible() ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Prevent error in Guzzle when PHP doesn't have the intl extension. |
| 93 |
if ( ! function_exists( 'idn_to_ascii' ) && ! defined( 'IDNA_DEFAULT' ) ) { |
| 94 |
define( 'IDNA_DEFAULT', 0 ); |
| 95 |
} |
| 96 |
|
| 97 |
// Load autoloaders. |
| 98 |
require_once WPOSESLITE_PATH . 'vendor/Aws3/aws-autoloader.php'; |
| 99 |
require_once WPOSESLITE_PATH . 'classes/Autoloader.php'; |
| 100 |
new DeliciousBrains\WP_Offload_SES\Autoloader( 'WP_Offload_SES', WPOSESLITE_PATH ); |
| 101 |
|
| 102 |
// Load compatibility functions for older PHP (< 8.0) and WordPress (< 5.9). |
| 103 |
require_once WPOSESLITE_PATH . 'includes/compat.php'; |
| 104 |
|
| 105 |
// Kick off the plugin. |
| 106 |
$wp_offload_ses = new DeliciousBrains\WP_Offload_SES\WP_Offload_SES( WPOSESLITE_FILE ); |
| 107 |
|
| 108 |
return $wp_offload_ses; |
| 109 |
} |
| 110 |
|
| 111 |
add_action( 'init', 'wp_offload_ses_lite_init', 1 ); |
| 112 |
|
| 113 |
/** |
| 114 |
* Gets the path to the plugin files. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- wposes is the plugin's established prefix. |
| 119 |
function wposes_lite_get_plugin_dir_path() { |
| 120 |
$abspath = wp_normalize_path( dirname( WPOSESLITE_FILE ) ); |
| 121 |
$mu_path = $abspath . '/wp-ses'; |
| 122 |
$core_class = '/classes/WP-Offload-SES.php'; |
| 123 |
|
| 124 |
// Is entry point file in directory above where plugin's files are? |
| 125 |
if ( file_exists( $mu_path . $core_class ) ) { |
| 126 |
$abspath = $mu_path; |
| 127 |
} |
| 128 |
|
| 129 |
return trailingslashit( $abspath ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check whether we should send mail via SES. |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- wposes is the plugin's established prefix. |
| 138 |
function wposes_lite_sending_enabled() { |
| 139 |
if ( defined( 'WPOSES_SETTINGS' ) ) { |
| 140 |
require_once WPOSESLITE_PATH . 'classes/Utils.php'; |
| 141 |
$defined_settings = Utils::maybe_unserialize( constant( 'WPOSES_SETTINGS' ) ); |
| 142 |
|
| 143 |
if ( isset( $defined_settings['send-via-ses'] ) ) { |
| 144 |
return (bool) $defined_settings['send-via-ses']; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$settings = get_option( 'wposes_settings' ); |
| 149 |
|
| 150 |
// Single sites & multisite subsites. |
| 151 |
if ( isset( $settings['send-via-ses'] ) ) { |
| 152 |
return (bool) $settings['send-via-ses']; |
| 153 |
} |
| 154 |
|
| 155 |
// If subsite isn't configured with an override, go with network setting. |
| 156 |
if ( is_multisite() ) { |
| 157 |
$network_settings = get_site_option( 'wposes_settings' ); |
| 158 |
|
| 159 |
if ( isset( $network_settings['send-via-ses'] ) ) { |
| 160 |
return (bool) $network_settings['send-via-ses']; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
// Override `wp_mail()` if sending via SES is enabled. |
| 168 |
if ( ! function_exists( 'wp_mail' ) ) { |
| 169 |
if ( wposes_lite_sending_enabled() ) { |
| 170 |
/** |
| 171 |
* Send mail via Amazon SES. |
| 172 |
* |
| 173 |
* @param string|array $to Array or comma-separated list of email addresses. |
| 174 |
* @param string $subject Email subject. |
| 175 |
* @param string $message Email message. |
| 176 |
* @param string|array $headers Optional. Additional headers. |
| 177 |
* @param string|array $attachments Optional. Files to attach. |
| 178 |
* |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Intentional wp_mail() override for SES integration. |
| 182 |
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { |
| 183 |
/** @var WP_Offload_SES $wp_offload_ses */ |
| 184 |
global $wp_offload_ses; |
| 185 |
|
| 186 |
if ( is_null( $wp_offload_ses ) ) { |
| 187 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound |
| 188 |
$wp_offload_ses = wp_offload_ses_lite_init(); |
| 189 |
} |
| 190 |
|
| 191 |
// Could not initialize plugin. |
| 192 |
if ( is_null( $wp_offload_ses ) ) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
return $wp_offload_ses->mail_handler( $to, $subject, $message, $headers, $attachments ); |
| 197 |
} |
| 198 |
} |
| 199 |
} else { |
| 200 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WordPress core global. |
| 201 |
global $pagenow; |
| 202 |
|
| 203 |
if ( ! in_array( $pagenow, array( 'plugins.php', 'update-core.php' ), true ) ) { |
| 204 |
require_once WPOSESLITE_PATH . 'classes/Error.php'; |
| 205 |
new DeliciousBrains\WP_Offload_SES\Error( |
| 206 |
DeliciousBrains\WP_Offload_SES\Error::$mail_function_exists, |
| 207 |
'Mail function already overridden.' |
| 208 |
); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if ( file_exists( WPOSESLITE_PATH . 'ext/wposes-ext-functions.php' ) ) { |
| 213 |
require_once WPOSESLITE_PATH . 'ext/wposes-ext-functions.php'; |
| 214 |
} |
| 215 |
|