| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that sets up the emails. |
| 4 |
* |
| 5 |
* @version 1.5.0 |
| 6 |
* @package Charitable/Classes/Charitable_Emails |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2020, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Emails' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Emails |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Charitable_Emails { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @var Charitable_Emails|null |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* All available emails. |
| 35 |
* |
| 36 |
* @var string[] |
| 37 |
*/ |
| 38 |
private $emails; |
| 39 |
|
| 40 |
/** |
| 41 |
* Set up the class. |
| 42 |
* |
| 43 |
* Note that the only way to instantiate an object is with the charitable_start method, |
| 44 |
* which can only be called during the start phase. In other words, don't try |
| 45 |
* to instantiate this object. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
private function __construct() { |
| 50 |
/* 3rd party hook for overriding anything we've done so far. */ |
| 51 |
do_action( 'charitable_emails_start', $this ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns and/or create the single instance of this class. |
| 56 |
* |
| 57 |
* @since 1.2.0 |
| 58 |
* |
| 59 |
* @return Charitable_Emails |
| 60 |
*/ |
| 61 |
public static function get_instance() { |
| 62 |
if ( is_null( self::$instance ) ) { |
| 63 |
self::$instance = new self(); |
| 64 |
} |
| 65 |
|
| 66 |
return self::$instance; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register Charitable emails. |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
* |
| 74 |
* @return string[] |
| 75 |
*/ |
| 76 |
public function register_emails() { |
| 77 |
$this->emails = apply_filters( 'charitable_emails', array( |
| 78 |
'donation_receipt' => 'Charitable_Email_Donation_Receipt', |
| 79 |
'offline_donation_receipt' => 'Charitable_Email_Offline_Donation_Receipt', |
| 80 |
'new_donation' => 'Charitable_Email_New_Donation', |
| 81 |
'campaign_end' => 'Charitable_Email_Campaign_End', |
| 82 |
'offline_donation_notification' => 'Charitable_Email_Offline_Donation_Notification', |
| 83 |
'password_reset' => 'Charitable_Email_Password_Reset', |
| 84 |
'email_verification' => 'Charitable_Email_Email_Verification', |
| 85 |
) ); |
| 86 |
|
| 87 |
return $this->emails; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Register admin actions for default emails. |
| 92 |
* |
| 93 |
* @since 1.5.0 |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function register_admin_actions() { |
| 98 |
$donation_actions = charitable_get_donation_actions(); |
| 99 |
|
| 100 |
/** |
| 101 |
* Filter the list of resendable donation emails. |
| 102 |
* |
| 103 |
* @since 1.5.0 |
| 104 |
* |
| 105 |
* @param string[] $emails The list of emails and their label. |
| 106 |
*/ |
| 107 |
$emails = apply_filters( 'charitable_resendable_donation_emails', array( |
| 108 |
'donation_receipt', |
| 109 |
'new_donation', |
| 110 |
'offline_donation_receipt', |
| 111 |
'offline_donation_notification', |
| 112 |
) ); |
| 113 |
|
| 114 |
foreach ( $emails as $email ) { |
| 115 |
$class = $this->get_email( $email ); |
| 116 |
$object = new $class; |
| 117 |
|
| 118 |
$donation_actions->register( 'resend_' . $email, array( |
| 119 |
'label' => $object->get_name(), |
| 120 |
'callback' => array( $this, 'resend_email' ), |
| 121 |
'button_text' => __( 'Resend Email', 'charitable' ), |
| 122 |
'active_callback' => array( $class, 'can_be_resent' ), |
| 123 |
'success_message' => 11, |
| 124 |
'failed_message' => 12, |
| 125 |
), __( 'Resend Donation Emails', 'charitable' ) ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Resend an email. |
| 131 |
* |
| 132 |
* This is the callback for all of the resend email actions. |
| 133 |
* |
| 134 |
* @since 1.5.0 |
| 135 |
* |
| 136 |
* @param boolean $success Whether the action has been successfully completed. |
| 137 |
* @param int $object_id An object ID. |
| 138 |
* @param array $args Mixed set of arguments. |
| 139 |
* @param string $action The action we are executing. |
| 140 |
* @return boolean |
| 141 |
*/ |
| 142 |
public function resend_email( $success, $object_id, $args, $action ) { |
| 143 |
$email = str_replace( 'resend_', '', $action ); |
| 144 |
$class = $this->get_email( $email ); |
| 145 |
$object = new $class; |
| 146 |
|
| 147 |
return $object->resend( $object_id, $args ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Receives a request to enable or disable an email and validates it before passing it off. |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function handle_email_settings_request() { |
| 158 |
if ( ! wp_verify_nonce( $_REQUEST['_nonce'], 'email' ) ) { |
| 159 |
wp_die( __( 'Cheatin\' eh?!', 'charitable' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
$email = isset( $_REQUEST['email_id'] ) ? $_REQUEST['email_id'] : false; |
| 163 |
|
| 164 |
/* Gateway must be set */ |
| 165 |
if ( false === $email ) { |
| 166 |
wp_die( __( 'Missing email.', 'charitable' ) ); |
| 167 |
} |
| 168 |
|
| 169 |
/* Validate email. */ |
| 170 |
if ( ! isset( $this->emails[ $email ] ) ) { |
| 171 |
wp_die( __( 'Invalid email.', 'charitable' ) ); |
| 172 |
} |
| 173 |
|
| 174 |
/* All good, so disable or enable the email */ |
| 175 |
if ( 'charitable_disable_email' == current_filter() ) { |
| 176 |
$this->disable_email( $email ); |
| 177 |
} else { |
| 178 |
$this->enable_email( $email ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Returns all available emails. |
| 184 |
* |
| 185 |
* @since 1.0.0 |
| 186 |
* |
| 187 |
* @return string |
| 188 |
*/ |
| 189 |
public function get_available_emails() { |
| 190 |
return $this->emails; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns the currently enabled emails. |
| 195 |
* |
| 196 |
* @since 1.0.0 |
| 197 |
* |
| 198 |
* @return string[] |
| 199 |
*/ |
| 200 |
public function get_enabled_emails() { |
| 201 |
$enabled = charitable_get_option( 'enabled_emails', array() ); |
| 202 |
|
| 203 |
/* The Password Reset & Email Verification emails are always enabled. */ |
| 204 |
$enabled['password_reset'] = 'Charitable_Email_Password_Reset'; |
| 205 |
$enabled['email_verification'] = 'Charitable_Email_Email_Verification'; |
| 206 |
|
| 207 |
return $enabled; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Returns a list of the names of currently enabled emails. |
| 212 |
* |
| 213 |
* @since 1.3.0 |
| 214 |
* |
| 215 |
* @return string[] |
| 216 |
*/ |
| 217 |
public function get_enabled_emails_names() { |
| 218 |
$emails = array(); |
| 219 |
|
| 220 |
foreach ( $this->get_enabled_emails() as $class ) { |
| 221 |
if ( ! class_exists( $class ) ) { |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
$email = new $class; |
| 226 |
$emails[ $email->get_email_id() ] = $email->get_name(); |
| 227 |
} |
| 228 |
|
| 229 |
return $emails; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Return the email class name for a given email. |
| 234 |
* |
| 235 |
* @since 1.0.0 |
| 236 |
* |
| 237 |
* @param string $email The email to return. |
| 238 |
* @return string|false A string representing the email class name if the |
| 239 |
* email is registered; false if it is not registered. |
| 240 |
*/ |
| 241 |
public function get_email( $email ) { |
| 242 |
return isset( $this->emails[ $email ] ) ? $this->emails[ $email ] : false; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Returns whether the passed email is enabled. |
| 247 |
* |
| 248 |
* @since 1.0.0 |
| 249 |
* |
| 250 |
* @param string $email_id The email to check for. |
| 251 |
* @return boolean |
| 252 |
*/ |
| 253 |
public function is_enabled_email( $email_id ) { |
| 254 |
return array_key_exists( $email_id, $this->get_enabled_emails() ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Enable an email. |
| 259 |
* |
| 260 |
* @since 1.0.0 |
| 261 |
* |
| 262 |
* @param string $email The email to be enabled. |
| 263 |
* @return void |
| 264 |
*/ |
| 265 |
protected function enable_email( $email ) { |
| 266 |
$settings = get_option( 'charitable_settings' ); |
| 267 |
$enabled_emails = isset( $settings['enabled_emails'] ) ? $settings['enabled_emails'] : array(); |
| 268 |
$enabled_emails[ $email ] = $this->emails[ $email ]; |
| 269 |
$settings['enabled_emails'] = $enabled_emails; |
| 270 |
|
| 271 |
update_option( 'charitable_settings', $settings ); |
| 272 |
|
| 273 |
Charitable_Settings::get_instance()->add_update_message( __( 'Email enabled', 'charitable' ), 'success' ); |
| 274 |
|
| 275 |
do_action( 'charitable_email_enable', $email ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Disable an email. |
| 280 |
* |
| 281 |
* @since 1.0.0 |
| 282 |
* |
| 283 |
* @param string $email The email to be disabled. |
| 284 |
* @return void |
| 285 |
*/ |
| 286 |
protected function disable_email( $email ) { |
| 287 |
$settings = get_option( 'charitable_settings' ); |
| 288 |
|
| 289 |
if ( ! isset( $settings['enabled_emails'][ $email ] ) ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
unset( $settings['enabled_emails'][ $email ] ); |
| 294 |
|
| 295 |
update_option( 'charitable_settings', $settings ); |
| 296 |
|
| 297 |
Charitable_Settings::get_instance()->add_update_message( __( 'Email disabled', 'charitable' ), 'success' ); |
| 298 |
|
| 299 |
do_action( 'charitable_email_disable', $email ); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
endif; |
| 304 |
|