| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that models the donation receipt email. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Email_Donation_Receipt |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Email_Donation_Receipt' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Donation Receipt Email |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Charitable_Email_Donation_Receipt extends Charitable_Email { |
| 25 |
|
| 26 |
/** Email ID */ |
| 27 |
const ID = 'donation_receipt'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Array of supported object types (campaigns, donations, donors, etc). |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* |
| 34 |
* @var string[] |
| 35 |
*/ |
| 36 |
protected $object_types = array( 'donation' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Instantiate the email class, defining its key values. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* |
| 43 |
* @param array $objects an array containing a donation object. |
| 44 |
*/ |
| 45 |
public function __construct( $objects = array() ) { |
| 46 |
parent::__construct( $objects ); |
| 47 |
|
| 48 |
$this->name = apply_filters( 'charitable_email_donation_receipt_name', __( 'Donor: Donation Receipt', 'charitable' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the current email's ID. |
| 53 |
* |
| 54 |
* @since 1.0.3 |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public static function get_email_id() { |
| 59 |
return self::ID; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Static method that is fired right after a donation is completed, sending the donation receipt. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* |
| 67 |
* @param int $donation_id The donation ID. |
| 68 |
* @return boolean |
| 69 |
*/ |
| 70 |
public static function send_with_donation_id( $donation_id ) { |
| 71 |
if ( ! charitable_get_helper( 'emails' )->is_enabled_email( self::get_email_id() ) ) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! charitable_is_approved_status( get_post_status( $donation_id ) ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
$donation = charitable_get_donation( $donation_id ); |
| 80 |
|
| 81 |
if ( ! is_object( $donation ) || 0 == count( $donation->get_campaign_donations() ) ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! $donation->has_valid_email() ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! apply_filters( 'charitable_send_' . self::get_email_id(), true, $donation ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$email = new self( |
| 94 |
array( |
| 95 |
'donation' => $donation, |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
/** |
| 100 |
* Don't resend the email. |
| 101 |
*/ |
| 102 |
if ( $email->is_sent_already( $donation_id ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
$sent = $email->send(); |
| 107 |
|
| 108 |
/** |
| 109 |
* Log that the email was sent. |
| 110 |
*/ |
| 111 |
if ( apply_filters( 'charitable_log_email_send', true, self::get_email_id(), $email ) ) { |
| 112 |
$email->log( $donation_id, $sent ); |
| 113 |
} |
| 114 |
|
| 115 |
return $sent; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Resend an email. |
| 120 |
* |
| 121 |
* @since 1.5.0 |
| 122 |
* |
| 123 |
* @param int $object_id An object ID. |
| 124 |
* @param array $args Mixed set of arguments. |
| 125 |
* @return boolean |
| 126 |
*/ |
| 127 |
public static function resend( $object_id, $args = array() ) { |
| 128 |
$donation = charitable_get_donation( $object_id ); |
| 129 |
|
| 130 |
if ( ! is_object( $donation ) || 0 == count( $donation->get_campaign_donations() ) ) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
$email = new Charitable_Email_Donation_Receipt( |
| 135 |
array( |
| 136 |
'donation' => $donation, |
| 137 |
) |
| 138 |
); |
| 139 |
|
| 140 |
$success = $email->send(); |
| 141 |
|
| 142 |
/** |
| 143 |
* Log that the email was sent. |
| 144 |
*/ |
| 145 |
if ( apply_filters( 'charitable_log_email_send', true, self::get_email_id(), $email ) ) { |
| 146 |
$email->log( $object_id, $success ); |
| 147 |
} |
| 148 |
|
| 149 |
return $success; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Checks whether an email can be resent. |
| 154 |
* |
| 155 |
* @since 1.5.0 |
| 156 |
* |
| 157 |
* @param int $object_id An object ID. |
| 158 |
* @param array $args Mixed set of arguments. |
| 159 |
* @return boolean |
| 160 |
*/ |
| 161 |
public static function can_be_resent( $object_id, $args = array() ) { |
| 162 |
$donation = charitable_get_donation( $object_id ); |
| 163 |
$resendable = is_object( $donation ) |
| 164 |
&& $donation->has_valid_email() |
| 165 |
&& charitable_is_approved_status( $donation->get_status() ); |
| 166 |
|
| 167 |
/** |
| 168 |
* Filter whether the email can be resent. |
| 169 |
* |
| 170 |
* @since 1.6.0 |
| 171 |
* |
| 172 |
* @param boolean $resendable Whether the email can be resent. |
| 173 |
* @param int $object_id The donation ID. |
| 174 |
* @param array $args Mixed set of arguments. |
| 175 |
*/ |
| 176 |
return apply_filters( 'charitable_can_resend_donation_receipt_email', $resendable, $object_id, $args ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Return the recipient for the email. |
| 181 |
* |
| 182 |
* @since 1.0.0 |
| 183 |
* @since 1.8.9.8 Normalize array return values to prevent TypeError in safe_get_recipient(). |
| 184 |
* |
| 185 |
* @return string |
| 186 |
*/ |
| 187 |
public function get_recipient() { |
| 188 |
if ( ! is_a( $this->donation, 'Charitable_Donation' ) ) { |
| 189 |
return ''; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Deprecated hook. Use charitable_email_donation_receipt_recipient instead. |
| 194 |
* |
| 195 |
* @deprecated 1.9.0 |
| 196 |
* |
| 197 |
* @since 1.0.0 |
| 198 |
* @since 1.6.0 Deprecated |
| 199 |
* |
| 200 |
* @param string $email_address Recipient email address. |
| 201 |
* @param Charitable_Email_Donation_Receipt $email Instance of `Charitable_Email_Donation_Receipt`. |
| 202 |
*/ |
| 203 |
$email_address = apply_filters( 'charitable_email_donation_receipt_receipient', $this->donation->get_donor()->get_email(), $this ); |
| 204 |
|
| 205 |
/** |
| 206 |
* Filter the recipient for the donation receipt email. |
| 207 |
* |
| 208 |
* @since 1.6.0 |
| 209 |
* |
| 210 |
* @param string $email_address Recipient email address. |
| 211 |
* @param Charitable_Email_Donation_Receipt $email Instance of `Charitable_Email_Donation_Receipt`. |
| 212 |
*/ |
| 213 |
$recipient = apply_filters( 'charitable_email_donation_receipt_recipient', $email_address, $this ); |
| 214 |
return is_array( $recipient ) ? implode( ', ', array_filter( array_map( 'trim', $recipient ) ) ) : $recipient; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Return the default subject line for the email. |
| 219 |
* |
| 220 |
* @since 1.0.0 |
| 221 |
* |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
protected function get_default_subject() { |
| 225 |
return apply_filters( 'charitable_email_donation_receipt_default_subject', __( 'Thank you for your donation', 'charitable' ), $this ); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Return the default headline for the email. |
| 230 |
* |
| 231 |
* @since 1.0.0 |
| 232 |
* |
| 233 |
* @return string |
| 234 |
*/ |
| 235 |
protected function get_default_headline() { |
| 236 |
return apply_filters( 'charitable_email_donation_receipt_default_headline', __( 'Your Donation Receipt', 'charitable' ), $this ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Return the default body for the email. |
| 241 |
* |
| 242 |
* @since 1.0.0 |
| 243 |
* |
| 244 |
* @return string |
| 245 |
*/ |
| 246 |
protected function get_default_body() { |
| 247 |
ob_start(); |
| 248 |
?> |
| 249 |
<p><?php esc_html_e( 'Dear [charitable_email show=donor_first_name],', 'charitable' ); ?></p> |
| 250 |
<p><?php esc_html_e( 'Thank you so much for your generous donation.', 'charitable' ); ?></p> |
| 251 |
<p><strong><?php esc_html_e( 'Your Receipt', 'charitable' ); ?></strong><br /> |
| 252 |
[charitable_email show=donation_summary]</p> |
| 253 |
<p><?php esc_html_e( 'With thanks, [charitable_email show=site_name]', 'charitable' ); ?></p> |
| 254 |
<?php |
| 255 |
$body = ob_get_clean(); |
| 256 |
|
| 257 |
return apply_filters( 'charitable_email_donation_receipt_default_body', $body, $this ); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
endif; |
| 262 |
|