|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Failed Donation Email |
| 4 | + * |
| 5 | + * Donation Notification will be sent to recipient(s) when a donation status is changed to failed. |
| 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 | + * @unreleased |
| 12 | + */ |
| 13 | + |
| 14 | +// Exit if access directly. |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { |
| 16 | + exit; |
| 17 | +} |
| 18 | + |
| 19 | +use Give\Donations\Models\Donation; |
| 20 | + |
| 21 | +if ( ! class_exists( 'Give_Failed_Donation_Email' ) ) : |
| 22 | + |
| 23 | + /** |
| 24 | + * @unreleased |
| 25 | + */ |
| 26 | + class Give_Failed_Donation_Email extends Give_Email_Notification { |
| 27 | + |
| 28 | + /* @var Donation|null $donation */ |
| 29 | + private $donation; |
| 30 | + |
| 31 | + /** |
| 32 | + * @unreleased |
| 33 | + */ |
| 34 | + public function init() { |
| 35 | + |
| 36 | + $this->load( |
| 37 | + array( |
| 38 | + 'id' => 'failed-donation', |
| 39 | + 'label' => __( 'Failed Donation', 'give' ), |
| 40 | + 'description' => __( 'Sent to designated recipient(s) when a donation status is changed to failed.', 'give' ), |
| 41 | + 'has_recipient_field' => true, |
| 42 | + 'notification_status' => 'enabled', |
| 43 | + 'form_metabox_setting' => true, |
| 44 | + 'default_email_subject' => esc_attr__( 'Failed Donation - #{payment_id}', 'give' ), |
| 45 | + 'default_email_message' => ( false !== give_get_option( 'failed-donation_email_message' ) ) ? give_get_option( 'failed-donation_email_message' ) : $this->get_default_email_message(), |
| 46 | + 'default_email_header' => __( 'Failed Donation!', 'give' ), |
| 47 | + ) |
| 48 | + ); |
| 49 | + |
| 50 | + add_action( "give_{$this->config['id']}_email_notification", array( $this, 'setup_email_notification' ) ); |
| 51 | + add_action( 'give_update_payment_status', array( $this, 'maybe_send_email_notification' ), 10, 3 ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @unreleased |
| 56 | + * |
| 57 | + * @return string |
| 58 | + */ |
| 59 | + private function get_default_email_message() { |
| 60 | + $default_email_body = __( 'Hi there,', 'give' ) . "\n\n"; |
| 61 | + $default_email_body .= __( 'This email is to inform you that a donation has failed on your website:', 'give' ) . ' {site_url}' . ".\n\n"; |
| 62 | + $default_email_body .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {name}' . "\n"; |
| 63 | + $default_email_body .= '<strong>' . __( 'Donation:', 'give' ) . '</strong> {donation}' . "\n"; |
| 64 | + $default_email_body .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n"; |
| 65 | + $default_email_body .= '<strong>' . __( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n"; |
| 66 | + $default_email_body .= '<strong>' . __( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n\n"; |
| 67 | + $default_email_body .= __( 'Thank you,', 'give' ) . "\n\n"; |
| 68 | + $default_email_body .= '{sitename}' . "\n"; |
| 69 | + |
| 70 | + /** |
| 71 | + * Filter the default failed donation notification email message. |
| 72 | + * |
| 73 | + * @unreleased |
| 74 | + * |
| 75 | + * @param string $default_email_body Default email message. |
| 76 | + */ |
| 77 | + return apply_filters( 'give_default_failed_donation_notification_email', $default_email_body ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @unreleased |
| 82 | + * |
| 83 | + * @param int $payment_id The ID number of the payment. |
| 84 | + * @param string $new_status The status of the payment. |
| 85 | + * @param string $old_status The status of the payment prior to being changed. |
| 86 | + * |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function maybe_send_email_notification( $payment_id, $new_status, $old_status ) { |
| 90 | + if ( $new_status === $old_status ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + $donation = Donation::find( (int) $payment_id ); |
| 95 | + if ( ! $donation || ! $donation->status->isFailed() ) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if ( ! give_is_setting_enabled( $this->get_notification_status() ) ) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Fires when a donation status is changed to failed. |
| 105 | + * |
| 106 | + * @param int $payment_id Payment ID. |
| 107 | + * |
| 108 | + * @unreleased |
| 109 | + */ |
| 110 | + do_action( "give_{$this->config['id']}_email_notification", $payment_id ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @unreleased |
| 115 | + * |
| 116 | + * @param int $form_id |
| 117 | + * |
| 118 | + * @return string |
| 119 | + */ |
| 120 | + public function get_email_subject( $form_id = null ) { |
| 121 | + $subject = wp_strip_all_tags( |
| 122 | + Give_Email_Notification_Util::get_value( |
| 123 | + $this, |
| 124 | + Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
| 125 | + $form_id, |
| 126 | + $this->config['default_email_subject'] |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + /** |
| 131 | + * Filters the failed donation notification subject. |
| 132 | + * |
| 133 | + * @unreleased |
| 134 | + */ |
| 135 | + $subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this, $form_id ); |
| 136 | + |
| 137 | + return $subject; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * @unreleased |
| 143 | + * |
| 144 | + * @param int $form_id |
| 145 | + * |
| 146 | + * @return string |
| 147 | + */ |
| 148 | + public function get_email_message( $form_id = null ) { |
| 149 | + $message = Give_Email_Notification_Util::get_value( |
| 150 | + $this, |
| 151 | + Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message', |
| 152 | + $form_id, |
| 153 | + $this->config['default_email_message'] |
| 154 | + ); |
| 155 | + |
| 156 | + /** |
| 157 | + * Filter the email message |
| 158 | + * |
| 159 | + * @unreleased |
| 160 | + */ |
| 161 | + $message = apply_filters( |
| 162 | + "give_{$this->config['id']}_get_default_email_message", |
| 163 | + $message, |
| 164 | + $this, |
| 165 | + $form_id |
| 166 | + ); |
| 167 | + |
| 168 | + return $message; |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + /** |
| 173 | + * @unreleased |
| 174 | + * |
| 175 | + * @param int $form_id |
| 176 | + * @return array |
| 177 | + */ |
| 178 | + public function get_email_attachments( $form_id = null ) { |
| 179 | + /** |
| 180 | + * Filters the failed donation notification email attachments. |
| 181 | + * By default, there is no attachment but plugins can hook in to provide one more multiple. |
| 182 | + * |
| 183 | + * @unreleased |
| 184 | + */ |
| 185 | + $attachments = apply_filters( |
| 186 | + "give_{$this->config['id']}_get_email_attachments", |
| 187 | + array(), |
| 188 | + $this, |
| 189 | + $form_id |
| 190 | + ); |
| 191 | + |
| 192 | + return $attachments; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @unreleased |
| 197 | + */ |
| 198 | + public function setup_email_data() { |
| 199 | + /** |
| 200 | + * Filters the from name. |
| 201 | + * |
| 202 | + * @unreleased |
| 203 | + */ |
| 204 | + $from_name = apply_filters( |
| 205 | + 'give_failed_donation_from_name', |
| 206 | + Give()->emails->get_from_name(), |
| 207 | + $this->donation->id, |
| 208 | + null |
| 209 | + ); |
| 210 | + |
| 211 | + /** |
| 212 | + * Filters the from email. |
| 213 | + * |
| 214 | + * @unreleased |
| 215 | + */ |
| 216 | + $from_email = apply_filters( |
| 217 | + 'give_failed_donation_from_address', |
| 218 | + Give()->emails->get_from_address(), |
| 219 | + $this->donation->id, |
| 220 | + null |
| 221 | + ); |
| 222 | + |
| 223 | + Give()->emails->__set( 'from_name', $from_name ); |
| 224 | + Give()->emails->__set( 'from_email', $from_email ); |
| 225 | + |
| 226 | + /** |
| 227 | + * Filters the failed donation notification email headers. |
| 228 | + * |
| 229 | + * @unreleased |
| 230 | + */ |
| 231 | + $headers = apply_filters( |
| 232 | + 'give_failed_donation_notification_headers', |
| 233 | + Give()->emails->get_headers(), |
| 234 | + $this->donation->id, |
| 235 | + null |
| 236 | + ); |
| 237 | + |
| 238 | + Give()->emails->__set( 'headers', $headers ); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @unreleased |
| 243 | + * |
| 244 | + * @param int $payment_id |
| 245 | + */ |
| 246 | + public function setup_email_notification( $payment_id ) { |
| 247 | + // Get the Donation model. |
| 248 | + $this->donation = Donation::find( $payment_id ); |
| 249 | + |
| 250 | + if ( ! $this->donation ) { |
| 251 | + wp_die( |
| 252 | + esc_html__( 'Cheatin’ uh?', 'give' ), |
| 253 | + esc_html__( 'Error', 'give' ), |
| 254 | + array( |
| 255 | + 'response' => 400, |
| 256 | + ) |
| 257 | + ); |
| 258 | + } |
| 259 | + |
| 260 | + // Set email data. |
| 261 | + $this->setup_email_data(); |
| 262 | + |
| 263 | + // Send email. |
| 264 | + $this->send_email_notification( |
| 265 | + array( |
| 266 | + 'payment_id' => $payment_id, |
| 267 | + ) |
| 268 | + ); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | +endif; |
| 273 | + |
| 274 | +return Give_Failed_Donation_Email::get_instance(); |
0 commit comments