| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email Notification |
| 4 |
* |
| 5 |
* This class handles all email notification settings. |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @subpackage Classes/Emails |
| 9 |
* @copyright Copyright (c) 2016, GiveWP |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
* @since 2.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Give_Email_Notifications |
| 16 |
* |
| 17 |
* @since 2.17.1 Remove validate_settings function. |
| 18 |
* Email recipient option value validation logic move to src/EmailGlobalSettingValidator. |
| 19 |
*/ |
| 20 |
class Give_Email_Notifications { |
| 21 |
/** |
| 22 |
* Instance. |
| 23 |
* |
| 24 |
* @since 2.0 |
| 25 |
* @access static |
| 26 |
* @var |
| 27 |
*/ |
| 28 |
private static $instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* Array of email notifications. |
| 32 |
* |
| 33 |
* @since 2.0 |
| 34 |
* @access private |
| 35 |
* @var Give_Email_Notification[] |
| 36 |
*/ |
| 37 |
private $emails = array(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Singleton pattern. |
| 41 |
* |
| 42 |
* @since 2.0 |
| 43 |
* @access private |
| 44 |
* Give_Email_Notifications constructor. |
| 45 |
*/ |
| 46 |
private function __construct() { |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Get instance. |
| 52 |
* |
| 53 |
* @since 2.0 |
| 54 |
* @access static |
| 55 |
* @return static |
| 56 |
*/ |
| 57 |
static function get_instance() { |
| 58 |
if ( null === static::$instance ) { |
| 59 |
self::$instance = new static(); |
| 60 |
} |
| 61 |
|
| 62 |
return self::$instance; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Setup dependencies |
| 67 |
* |
| 68 |
* @since 2.0 |
| 69 |
*/ |
| 70 |
public function init() { |
| 71 |
// Load files. |
| 72 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/ajax-handler.php'; |
| 73 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-setting-field.php'; |
| 74 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/filters.php'; |
| 75 |
|
| 76 |
// Load email notifications. |
| 77 |
$this->add_emails_notifications(); |
| 78 |
|
| 79 |
add_filter( 'give_metabox_form_data_settings', array( $this, 'add_metabox_setting_fields' ), 10, 2 ); |
| 80 |
add_action( 'init', array( $this, 'preview_email' ) ); |
| 81 |
add_action( 'init', array( $this, 'send_preview_email' ) ); |
| 82 |
|
| 83 |
/* @var Give_Email_Notification $email */ |
| 84 |
foreach ( $this->get_email_notifications() as $email ) { |
| 85 |
// Setup email section. |
| 86 |
if ( Give_Email_Notification_Util::is_show_on_emails_setting_page( $email ) ) { |
| 87 |
add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) ); |
| 88 |
add_filter( "give_hide_section_{$email->config['id']}_on_emails_page", array( $email, 'hide_section' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
// Setup email preview. |
| 92 |
if ( Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) { |
| 93 |
add_action( "give_{$email->config['id']}_email_preview", array( $this, 'email_preview_header' ) ); |
| 94 |
add_filter( "give_{$email->config['id']}_email_preview_data", array( $this, 'email_preview_data' ) ); |
| 95 |
add_filter( "give_{$email->config['id']}_email_preview_message", array( $this, 'email_preview_message' ), 1, 2 ); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Add setting to metabox. |
| 103 |
* |
| 104 |
* @since 2.0 |
| 105 |
* @access public |
| 106 |
* |
| 107 |
* @param array $settings |
| 108 |
* @param int $post_id |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function add_metabox_setting_fields( $settings, $post_id ) { |
| 113 |
$emails = $this->get_email_notifications(); |
| 114 |
|
| 115 |
// Bailout. |
| 116 |
if ( empty( $emails ) ) { |
| 117 |
return $settings; |
| 118 |
} |
| 119 |
|
| 120 |
// Email notification setting. |
| 121 |
$settings['email_notification_options'] = array( |
| 122 |
'id' => 'email_notification_options', |
| 123 |
'title' => __( 'Email Notifications', 'give' ), |
| 124 |
'icon-html' => '<i class="fas fa-envelope"></i>', |
| 125 |
'fields' => array( |
| 126 |
array( |
| 127 |
'name' => __( 'Email Options', 'give' ), |
| 128 |
'id' => '_give_email_options', |
| 129 |
'type' => 'radio_inline', |
| 130 |
'default' => 'global', |
| 131 |
'options' => array( |
| 132 |
'global' => __( 'Global Options', 'give' ), |
| 133 |
'enabled' => __( 'Customize', 'give' ), |
| 134 |
), |
| 135 |
), |
| 136 |
array( |
| 137 |
'id' => '_give_email_template', |
| 138 |
'name' => esc_html__( 'Email Template', 'give' ), |
| 139 |
'desc' => esc_html__( 'Choose your template from the available registered template types.', 'give' ), |
| 140 |
'type' => 'select', |
| 141 |
'default' => 'default', |
| 142 |
'options' => give_get_email_templates(), |
| 143 |
), |
| 144 |
array( |
| 145 |
'id' => '_give_email_logo', |
| 146 |
'name' => esc_html__( 'Logo', 'give' ), |
| 147 |
'desc' => esc_html__( 'Upload or choose a logo to be displayed at the top of the donation receipt emails. Displayed on HTML emails only.', 'give' ), |
| 148 |
'type' => 'file', |
| 149 |
), |
| 150 |
array( |
| 151 |
'id' => '_give_from_name', |
| 152 |
'name' => esc_html__( 'From Name', 'give' ), |
| 153 |
'desc' => esc_html__( 'The name which appears in the "From" field in all GiveWP donation emails.', 'give' ), |
| 154 |
'default' => get_bloginfo( 'name' ), |
| 155 |
'type' => 'text', |
| 156 |
), |
| 157 |
array( |
| 158 |
'id' => '_give_from_email', |
| 159 |
'name' => esc_html__( 'From Email', 'give' ), |
| 160 |
'desc' => esc_html__( 'Email address from which all GiveWP emails are sent from. This will act as the "from" and "reply-to" email address.', 'give' ), |
| 161 |
'default' => get_bloginfo( 'admin_email' ), |
| 162 |
'type' => 'text', |
| 163 |
), |
| 164 |
array( |
| 165 |
'name' => 'email_notification_docs', |
| 166 |
'type' => 'docs_link', |
| 167 |
'url' => 'http://docs.givewp.com/email-notification', |
| 168 |
'title' => __( 'Email Notification', 'give' ), |
| 169 |
), |
| 170 |
), |
| 171 |
|
| 172 |
/** |
| 173 |
* Filter the email notification settings. |
| 174 |
* |
| 175 |
* @since 2.0 |
| 176 |
*/ |
| 177 |
'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ), |
| 178 |
); |
| 179 |
|
| 180 |
return $settings; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Add email notifications |
| 185 |
* |
| 186 |
* @since 4.14.0 Add failed donation email. |
| 187 |
* @since 2.0 |
| 188 |
* @access private |
| 189 |
*/ |
| 190 |
private function add_emails_notifications() { |
| 191 |
$this->emails = array( |
| 192 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php', |
| 193 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-failed-donation-email.php', |
| 194 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php', |
| 195 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php', |
| 196 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php', |
| 197 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php', |
| 198 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php', |
| 199 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-note-email.php', |
| 200 |
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php', |
| 201 |
); |
| 202 |
|
| 203 |
/** |
| 204 |
* Filter the email notifications. |
| 205 |
* |
| 206 |
* @since 2.0 |
| 207 |
*/ |
| 208 |
$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this ); |
| 209 |
|
| 210 |
// Bailout. |
| 211 |
if ( empty( $this->emails ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
// Initiate email notifications. |
| 216 |
foreach ( $this->emails as $email ) { |
| 217 |
$email->init(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Get list of email notifications. |
| 224 |
* |
| 225 |
* @since 2.0 |
| 226 |
* @access public |
| 227 |
* @return array |
| 228 |
*/ |
| 229 |
public function get_email_notifications() { |
| 230 |
return $this->emails; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Displays the email preview |
| 236 |
* |
| 237 |
* @since 2.0 |
| 238 |
* @access public |
| 239 |
* @return bool|null |
| 240 |
*/ |
| 241 |
public function preview_email() { |
| 242 |
// Bailout. |
| 243 |
if ( ! Give_Email_Notification_Util::can_preview_email() ) { |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
// Security check. |
| 248 |
give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' ); |
| 249 |
|
| 250 |
// Get email type. |
| 251 |
$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : ''; |
| 252 |
|
| 253 |
/* @var Give_Email_Notification $email */ |
| 254 |
foreach ( $this->get_email_notifications() as $email ) { |
| 255 |
if ( $email_type !== $email->config['id'] ) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
// Set form id. |
| 260 |
$form_id = empty( $_GET['form_id'] ) ? null : absint( $_GET['form_id'] ); |
| 261 |
|
| 262 |
// Call setup email data to apply filter and other thing to email. |
| 263 |
$email->send_preview_email( false ); |
| 264 |
|
| 265 |
// Decode message. |
| 266 |
$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) ); |
| 267 |
|
| 268 |
// Show formatted text in browser even text/plain content type set for an email. |
| 269 |
Give()->emails->html = true; |
| 270 |
|
| 271 |
Give()->emails->form_id = $form_id; |
| 272 |
|
| 273 |
if ( 'text/plain' === $email->config['content_type'] ) { |
| 274 |
// Give()->emails->__set( 'html', false ); |
| 275 |
Give()->emails->__set( 'template', 'none' ); |
| 276 |
} |
| 277 |
|
| 278 |
if ( $email_message = Give()->emails->build_email( $email_message ) ) { |
| 279 |
|
| 280 |
/** |
| 281 |
* Filter the email preview data |
| 282 |
* |
| 283 |
* @since 2.0 |
| 284 |
* |
| 285 |
* @param array |
| 286 |
*/ |
| 287 |
$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() ); |
| 288 |
|
| 289 |
/** |
| 290 |
* Fire the give_{$email_type}_email_preview action |
| 291 |
* |
| 292 |
* @since 2.0 |
| 293 |
*/ |
| 294 |
do_action( "give_{$email_type}_email_preview", $email ); |
| 295 |
|
| 296 |
/** |
| 297 |
* Filter the email message |
| 298 |
* |
| 299 |
* @since 2.0 |
| 300 |
* |
| 301 |
* @param string $email_message |
| 302 |
* @param array $email_preview_data |
| 303 |
* @param Give_Email_Notification $email |
| 304 |
*/ |
| 305 |
echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email ); |
| 306 |
|
| 307 |
exit(); |
| 308 |
} |
| 309 |
}// End foreach(). |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Add header to donation receipt email preview |
| 315 |
* |
| 316 |
* @since 2.0 |
| 317 |
* @since 4.16.6 Re-check capability/nonce here, since this callback is also reachable via a direct action dispatch that bypasses the check normally done in preview_email(). |
| 318 |
* @access public |
| 319 |
* |
| 320 |
* @param Give_Email_Notification $email |
| 321 |
*/ |
| 322 |
public function email_preview_header( $email ) { |
| 323 |
if ( ! Give_Email_Notification_Util::can_preview_email() ) { |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Filter the all email preview headers. |
| 329 |
* |
| 330 |
* @since 2.0 |
| 331 |
* |
| 332 |
* @param Give_Email_Notification $email |
| 333 |
*/ |
| 334 |
$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email ); |
| 335 |
|
| 336 |
echo $email_preview_header; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Add email preview data |
| 341 |
* |
| 342 |
* @since 2.0 |
| 343 |
* @access public |
| 344 |
* |
| 345 |
* @param array $email_preview_data |
| 346 |
* |
| 347 |
* @return array |
| 348 |
*/ |
| 349 |
public function email_preview_data( $email_preview_data ) { |
| 350 |
$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) ); |
| 351 |
$email_preview_data['user_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) ); |
| 352 |
|
| 353 |
return $email_preview_data; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Replace email template tags. |
| 358 |
* |
| 359 |
* @since 2.0 |
| 360 |
* @access public |
| 361 |
* |
| 362 |
* @param string $email_message |
| 363 |
* @param array $email_preview_data |
| 364 |
* |
| 365 |
* @return string |
| 366 |
*/ |
| 367 |
public function email_preview_message( $email_message, $email_preview_data ) { |
| 368 |
if ( |
| 369 |
! empty( $email_preview_data['payment_id'] ) |
| 370 |
|| ! empty( $email_preview_data['user_id'] ) |
| 371 |
) { |
| 372 |
$email_message = give_do_email_tags( $email_message, $email_preview_data ); |
| 373 |
} |
| 374 |
|
| 375 |
return $email_message; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Displays the email preview |
| 380 |
* |
| 381 |
* @since 2.0 |
| 382 |
* @access public |
| 383 |
* @return bool|null |
| 384 |
*/ |
| 385 |
public function send_preview_email() { |
| 386 |
// Bailout. |
| 387 |
if ( ! Give_Email_Notification_Util::can_send_preview_email() ) { |
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
// Security check. |
| 392 |
give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' ); |
| 393 |
|
| 394 |
// Get email type. |
| 395 |
$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' ); |
| 396 |
|
| 397 |
/* @var Give_Email_Notification $email */ |
| 398 |
foreach ( $this->get_email_notifications() as $email ) { |
| 399 |
if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) { |
| 400 |
$email->send_preview_email(); |
| 401 |
break; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
// Remove the test email query arg. |
| 406 |
wp_redirect( esc_url_raw( remove_query_arg( 'give_action' ) ) ); |
| 407 |
exit; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/** |
| 412 |
* Load Give_Email_Notifications |
| 413 |
* |
| 414 |
* @since 2.0 |
| 415 |
* @access public |
| 416 |
*/ |
| 417 |
public function load() { |
| 418 |
add_action( 'init', array( $this, 'init' ), -1 ); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
// Helper class. |
| 423 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php'; |
| 424 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php'; |
| 425 |
|
| 426 |
// Add backward compatibility. |
| 427 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php'; |
| 428 |
|
| 429 |
/** |
| 430 |
* Initialize functionality. |
| 431 |
*/ |
| 432 |
Give_Email_Notifications::get_instance()->load(); |
| 433 |
|