| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donation Receipt Email |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Classes/Emails |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 2.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if access directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Give_Donation_Receipt_Email' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Give_Donation_Receipt_Email |
| 21 |
* |
| 22 |
* @abstract |
| 23 |
* @since 2.0 |
| 24 |
*/ |
| 25 |
class Give_Donation_Receipt_Email extends Give_Email_Notification { |
| 26 |
/* @var Give_Payment $payment */ |
| 27 |
public $payment; |
| 28 |
|
| 29 |
/** |
| 30 |
* Create a class instance. |
| 31 |
* |
| 32 |
* @access public |
| 33 |
* @since 2.0 |
| 34 |
*/ |
| 35 |
public function init() { |
| 36 |
// Initialize empty payment. |
| 37 |
$this->payment = new Give_Payment( 0 ); |
| 38 |
|
| 39 |
$this->load( |
| 40 |
array( |
| 41 |
'id' => 'donation-receipt', |
| 42 |
'label' => __( 'Donation Receipt', 'give' ), |
| 43 |
'description' => __( 'Sent to the donor when their donation completes or a pending donation is marked as complete.', 'give' ), |
| 44 |
'notification_status' => 'enabled', |
| 45 |
'form_metabox_setting' => true, |
| 46 |
'recipient_group_name' => __( 'Donor', 'give' ), |
| 47 |
'default_email_subject' => esc_attr__( 'Donation Receipt', 'give' ), |
| 48 |
'default_email_message' => give_get_default_donation_receipt_email(), |
| 49 |
'default_email_header' => __( 'Donation Receipt', 'give' ), |
| 50 |
) |
| 51 |
); |
| 52 |
|
| 53 |
add_action( "give_{$this->config['id']}_email_notification", array( $this, 'send_donation_receipt' ) ); |
| 54 |
add_action( 'give_email_links', array( $this, 'resend_donation_receipt' ) ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get email subject. |
| 59 |
* |
| 60 |
* @since 2.0 |
| 61 |
* @access public |
| 62 |
* |
| 63 |
* @param int $form_id |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function get_email_subject( $form_id = null ) { |
| 67 |
$subject = wp_strip_all_tags( |
| 68 |
Give_Email_Notification_Util::get_value( |
| 69 |
$this, |
| 70 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
| 71 |
$form_id, |
| 72 |
$this->config['default_email_subject'] |
| 73 |
) |
| 74 |
); |
| 75 |
|
| 76 |
/** |
| 77 |
* Filters the donation email receipt subject. |
| 78 |
* Note: This filter will deprecate soon. |
| 79 |
* |
| 80 |
* @since 1.0 |
| 81 |
*/ |
| 82 |
$subject = apply_filters( |
| 83 |
'give_donation_subject', |
| 84 |
$subject, |
| 85 |
$this->payment->ID |
| 86 |
); |
| 87 |
|
| 88 |
/** |
| 89 |
* Filters the donation email receipt subject. |
| 90 |
* |
| 91 |
* @since 2.0 |
| 92 |
*/ |
| 93 |
$subject = apply_filters( |
| 94 |
"give_{$this->config['id']}_get_email_subject", |
| 95 |
$subject, |
| 96 |
$this, |
| 97 |
$form_id |
| 98 |
); |
| 99 |
|
| 100 |
return $subject; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Get email message. |
| 106 |
* |
| 107 |
* @since 2.0 |
| 108 |
* @access public |
| 109 |
* |
| 110 |
* @param int $form_id |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function get_email_message( $form_id = null ) { |
| 114 |
$message = Give_Email_Notification_Util::get_value( |
| 115 |
$this, |
| 116 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message', |
| 117 |
$form_id, |
| 118 |
$this->config['default_email_message'] |
| 119 |
); |
| 120 |
|
| 121 |
/** |
| 122 |
* Filter message on basis of email template |
| 123 |
* Note: This filter will deprecate soon. |
| 124 |
* |
| 125 |
* @since 1.0 |
| 126 |
*/ |
| 127 |
$message = apply_filters( |
| 128 |
'give_donation_receipt_' . Give()->emails->get_template(), |
| 129 |
$message, |
| 130 |
$this->payment->ID, |
| 131 |
$this->payment->payment_meta |
| 132 |
); |
| 133 |
|
| 134 |
/** |
| 135 |
* Filter the message |
| 136 |
* Note: This filter will deprecate soon. |
| 137 |
* |
| 138 |
* @since 1.0 |
| 139 |
*/ |
| 140 |
$message = apply_filters( |
| 141 |
'give_donation_receipt', |
| 142 |
$message, |
| 143 |
$this->payment->ID, |
| 144 |
$this->payment->payment_meta |
| 145 |
); |
| 146 |
|
| 147 |
/** |
| 148 |
* Filter the message |
| 149 |
* |
| 150 |
* @since 2.0 |
| 151 |
*/ |
| 152 |
$message = apply_filters( |
| 153 |
"give_{$this->config['id']}_get_email_message", |
| 154 |
$message, |
| 155 |
$this, |
| 156 |
$form_id |
| 157 |
); |
| 158 |
|
| 159 |
return $message; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get the recipient attachments. |
| 164 |
* |
| 165 |
* @since 2.0 |
| 166 |
* @access public |
| 167 |
* |
| 168 |
* @param int $form_id |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
public function get_email_attachments( $form_id = null ) { |
| 172 |
/** |
| 173 |
* Filter the attachments. |
| 174 |
* Note: this filter will deprecate soon. |
| 175 |
* |
| 176 |
* @since 1.0 |
| 177 |
*/ |
| 178 |
$attachments = apply_filters( |
| 179 |
'give_receipt_attachments', |
| 180 |
array(), |
| 181 |
$this->payment->ID, |
| 182 |
$this->payment->payment_meta |
| 183 |
); |
| 184 |
|
| 185 |
/** |
| 186 |
* Filter the attachments. |
| 187 |
* |
| 188 |
* @since 2.0 |
| 189 |
*/ |
| 190 |
$attachments = apply_filters( |
| 191 |
"give_{$this->config['id']}_get_email_attachments", |
| 192 |
$attachments, |
| 193 |
$this, |
| 194 |
$form_id |
| 195 |
); |
| 196 |
|
| 197 |
return $attachments; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* Set email data. |
| 203 |
* |
| 204 |
* @since 2.0 |
| 205 |
*/ |
| 206 |
public function setup_email_data() { |
| 207 |
// Set recipient email. |
| 208 |
$this->recipient_email = $this->payment->email; |
| 209 |
|
| 210 |
/** |
| 211 |
* Filters the from name. |
| 212 |
* |
| 213 |
* @param int $payment_id Payment id. |
| 214 |
* @param mixed $payment_data Payment meta data. |
| 215 |
* |
| 216 |
* @since 1.0 |
| 217 |
*/ |
| 218 |
$from_name = apply_filters( |
| 219 |
'give_donation_from_name', |
| 220 |
Give()->emails->get_from_name(), |
| 221 |
$this->payment->ID, |
| 222 |
$this->payment->payment_meta |
| 223 |
); |
| 224 |
|
| 225 |
/** |
| 226 |
* Filters the from email. |
| 227 |
* |
| 228 |
* @param int $payment_id Payment id. |
| 229 |
* @param mixed $payment_data Payment meta data. |
| 230 |
* |
| 231 |
* @since 1.0 |
| 232 |
*/ |
| 233 |
$from_email = apply_filters( |
| 234 |
'give_donation_from_address', |
| 235 |
Give()->emails->get_from_address(), |
| 236 |
$this->payment->ID, |
| 237 |
$this->payment->payment_meta |
| 238 |
); |
| 239 |
|
| 240 |
Give()->emails->__set( 'from_name', $from_name ); |
| 241 |
Give()->emails->__set( 'from_email', $from_email ); |
| 242 |
|
| 243 |
/** |
| 244 |
* Filters the donation receipt's email headers. |
| 245 |
* |
| 246 |
* @param int $payment_id Payment id. |
| 247 |
* @param mixed $payment_data Payment meta data. |
| 248 |
* |
| 249 |
* @since 1.0 |
| 250 |
*/ |
| 251 |
$headers = apply_filters( |
| 252 |
'give_receipt_headers', |
| 253 |
Give()->emails->get_headers(), |
| 254 |
$this->payment->ID, |
| 255 |
$this->payment->payment_meta |
| 256 |
); |
| 257 |
|
| 258 |
Give()->emails->__set( 'headers', $headers ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Send donation receipt |
| 263 |
* |
| 264 |
* @since 2.0 |
| 265 |
* @access public |
| 266 |
* |
| 267 |
* @param $payment_id |
| 268 |
*/ |
| 269 |
public function send_donation_receipt( $payment_id ) { |
| 270 |
$this->payment = new Give_Payment( $payment_id ); |
| 271 |
|
| 272 |
if ( ! $this->payment->ID ) { |
| 273 |
wp_die( |
| 274 |
esc_html__( 'Cheatin’ uh?', 'give' ), |
| 275 |
esc_html__( 'Error', 'give' ), |
| 276 |
array( |
| 277 |
'response' => 400, |
| 278 |
) |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
// Setup email data. |
| 283 |
$this->setup_email_data(); |
| 284 |
|
| 285 |
// Send email. |
| 286 |
$this->send_email_notification( |
| 287 |
array( |
| 288 |
'payment_id' => $this->payment->ID, |
| 289 |
) |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Resend payment receipt by row action. |
| 295 |
* |
| 296 |
* @since 2.0 |
| 297 |
* @access public |
| 298 |
* |
| 299 |
* @param array $data Donation details. |
| 300 |
*/ |
| 301 |
public function resend_donation_receipt( $data ) { |
| 302 |
$purchase_id = absint( $data['purchase_id'] ); |
| 303 |
|
| 304 |
if ( empty( $purchase_id ) ) { |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
// Get donation payment information. |
| 309 |
$this->payment = new Give_Payment( $purchase_id ); |
| 310 |
|
| 311 |
if ( ! current_user_can( 'edit_give_payments', $this->payment->ID ) ) { |
| 312 |
wp_die( |
| 313 |
esc_html__( 'Cheatin’ uh?', 'give' ), |
| 314 |
esc_html__( 'Error', 'give' ), |
| 315 |
array( |
| 316 |
'response' => 400, |
| 317 |
) |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
// Setup email data. |
| 322 |
$this->setup_email_data(); |
| 323 |
|
| 324 |
// Send email. |
| 325 |
$this->send_email_notification( |
| 326 |
array( |
| 327 |
'payment_id' => $this->payment->ID, |
| 328 |
) |
| 329 |
); |
| 330 |
|
| 331 |
wp_redirect( |
| 332 |
esc_url_raw( |
| 333 |
add_query_arg( |
| 334 |
array( |
| 335 |
'give-messages[]' => 'email-sent', |
| 336 |
'give-action' => false, |
| 337 |
'purchase_id' => false, |
| 338 |
) |
| 339 |
) |
| 340 |
) |
| 341 |
); |
| 342 |
exit; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
endif; // End class_exists check |
| 347 |
|
| 348 |
return Give_Donation_Receipt_Email::get_instance(); |
| 349 |
|