| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Email Hooks. |
| 4 |
* |
| 5 |
* Action/filter hooks used for Charitable emails. |
| 6 |
* |
| 7 |
* @package Charitable/Functions/Emails |
| 8 |
* @version 1.5.0 |
| 9 |
* @author David Bisset |
| 10 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 11 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Register Charitable emails. |
| 21 |
* |
| 22 |
* @see Charitable_Emails::register_emails() |
| 23 |
*/ |
| 24 |
add_action( 'init', array( Charitable_Emails::get_instance(), 'register_emails' ) ); |
| 25 |
|
| 26 |
/** |
| 27 |
* Register admin actions for Charitable emails. |
| 28 |
* |
| 29 |
* @see Charitable_Emails::register_email_admin_actions() |
| 30 |
*/ |
| 31 |
add_action( 'admin_init', array( Charitable_Emails::get_instance(), 'register_admin_actions' ) ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Send the Donation Receipt and Donation Notification emails. |
| 35 |
* |
| 36 |
* Both of these emails are sent immediately a donation has been completed. |
| 37 |
* |
| 38 |
* @see Charitable_Email_Donation_Receipt::send_with_donation_id() |
| 39 |
* @see Charitable_Email_New_Donation::send_with_donation_id() |
| 40 |
* @see Charitable_Email_Offline_Donation_Receipt::send_with_donation_id() |
| 41 |
* @see Charitable_Email_Offline_Donation_Notification::send_with_donation_id() |
| 42 |
*/ |
| 43 |
|
| 44 |
add_action( 'charitable_after_save_donation', array( 'Charitable_Email_Donation_Receipt', 'send_with_donation_id' ) ); |
| 45 |
add_action( 'charitable_after_save_donation', array( 'Charitable_Email_New_Donation', 'send_with_donation_id' ) ); |
| 46 |
add_action( 'charitable_after_save_donation', array( 'Charitable_Email_Offline_Donation_Receipt', 'send_with_donation_id' ) ); |
| 47 |
add_action( 'charitable_after_save_donation', array( 'Charitable_Email_Offline_Donation_Notification', 'send_with_donation_id' ) ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Send the Campaign Ended email. |
| 51 |
* |
| 52 |
* This email can be sent to any recipients, within 24 hours after a campaign has reached its end date. |
| 53 |
* |
| 54 |
* @see Charitable_Email_Campaign_End::send_with_campaign_id() |
| 55 |
*/ |
| 56 |
add_action( 'charitable_campaign_end', array( 'Charitable_Email_Campaign_End', 'send_with_campaign_id' ) ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Enable & disable emails. |
| 60 |
* |
| 61 |
* @see Charitable_Emails::handle_email_settings_request() |
| 62 |
*/ |
| 63 |
add_action( 'charitable_enable_email', array( Charitable_Emails::get_instance(), 'handle_email_settings_request' ) ); |
| 64 |
add_action( 'charitable_disable_email', array( Charitable_Emails::get_instance(), 'handle_email_settings_request' ) ); |
| 65 |
|
| 66 |
/** |
| 67 |
* Process deferred emails scheduled during AJAX to avoid Oxygen output buffer conflicts. |
| 68 |
* |
| 69 |
* When Oxygen + Braintree + Recurring donations are active, email sending is deferred |
| 70 |
* via wp_cron to prevent email HTML from contaminating the AJAX JSON response. |
| 71 |
* |
| 72 |
* @since 1.8.9.6 |
| 73 |
* |
| 74 |
* @param array $email_data Contains 'class', 'args', and 'timestamp'. |
| 75 |
*/ |
| 76 |
add_action( 'charitable_send_deferred_email', function( $email_data ) { |
| 77 |
if ( empty( $email_data['class'] ) || ! class_exists( $email_data['class'] ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$args = isset( $email_data['args'] ) ? $email_data['args'] : array(); |
| 82 |
$class = $email_data['class']; |
| 83 |
|
| 84 |
// Reconstruct donation object from ID. |
| 85 |
$donation = null; |
| 86 |
if ( ! empty( $args['donation_id'] ) ) { |
| 87 |
$donation = charitable_get_donation( $args['donation_id'] ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! $donation ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Reconstruct campaign donations. |
| 95 |
$campaign_donations = $donation->get_campaign_donations(); |
| 96 |
|
| 97 |
// Clean output buffers to ensure no contamination. |
| 98 |
while ( ob_get_level() > 0 ) { |
| 99 |
ob_end_clean(); |
| 100 |
} |
| 101 |
|
| 102 |
// Instantiate and send the email. |
| 103 |
$email_object = new $class( array( |
| 104 |
'donation' => $donation, |
| 105 |
'campaign' => ! empty( $campaign_donations ) ? charitable_get_campaign( current( $campaign_donations )->campaign_id ) : null, |
| 106 |
) ); |
| 107 |
|
| 108 |
if ( method_exists( $email_object, 'send' ) ) { |
| 109 |
$email_object->send(); |
| 110 |
} |
| 111 |
} ); |
| 112 |
|