| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that models the new donation email. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Classes/Charitable_Email_New_Donation |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2018, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 13 |
|
| 14 |
if ( ! class_exists( 'Charitable_Email_New_Donation' ) ) : |
| 15 |
|
| 16 |
/** |
| 17 |
* New Donation Email |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
class Charitable_Email_New_Donation extends Charitable_Email { |
| 22 |
|
| 23 |
/* @var string */ |
| 24 |
const ID = 'new_donation'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Whether the email allows you to define the email recipients. |
| 28 |
* |
| 29 |
* @since 1.1.0 |
| 30 |
* |
| 31 |
* @var boolean |
| 32 |
*/ |
| 33 |
protected $has_recipient_field = true; |
| 34 |
|
| 35 |
/** |
| 36 |
* Object types supported by this email. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @var string[] Array of supported object types (campaigns, donations, donors, etc). |
| 41 |
*/ |
| 42 |
protected $object_types = array( 'donation' ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Instantiate the email class, defining its key values. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* |
| 49 |
* @param mixed[] $objects Array containing a Charitable_Donation object. |
| 50 |
*/ |
| 51 |
public function __construct( $objects = array() ) { |
| 52 |
parent::__construct( $objects ); |
| 53 |
|
| 54 |
/** |
| 55 |
* Customize the name of the donation notification. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @param string $name |
| 60 |
*/ |
| 61 |
$this->name = apply_filters( 'charitable_email_new_donation_name', __( 'Admin: New Donation Notification', 'charitable' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns the current email's ID. |
| 66 |
* |
| 67 |
* @since 1.0.3 |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public static function get_email_id() { |
| 72 |
return self::ID; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Static method that triggers the sending of this email. Sends the receipt with the donation is marked complete. |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param int $donation_id The ID of the donation that we're sending an email about. |
| 81 |
* @return boolean |
| 82 |
*/ |
| 83 |
public static function send_with_donation_id( $donation_id ) { |
| 84 |
if ( ! charitable_get_helper( 'emails' )->is_enabled_email( self::get_email_id() ) ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! charitable_is_approved_status( get_post_status( $donation_id ) ) ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
$donation = charitable_get_donation( $donation_id ); |
| 93 |
|
| 94 |
if ( ! is_object( $donation ) || 0 == count( $donation->get_campaign_donations() ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! apply_filters( 'charitable_send_' . self::get_email_id(), true, $donation ) ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
$email = new self( array( |
| 103 |
'donation' => $donation, |
| 104 |
) ); |
| 105 |
|
| 106 |
/** |
| 107 |
* Don't resend the email. |
| 108 |
*/ |
| 109 |
if ( $email->is_sent_already( $donation_id ) ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
$sent = $email->send(); |
| 114 |
|
| 115 |
/** |
| 116 |
* Log that the email was sent. |
| 117 |
*/ |
| 118 |
if ( apply_filters( 'charitable_log_email_send', true, self::get_email_id(), $email ) ) { |
| 119 |
$email->log( $donation_id, $sent ); |
| 120 |
} |
| 121 |
|
| 122 |
return $sent; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Resend the email. |
| 127 |
* |
| 128 |
* @since 1.5.0 |
| 129 |
* |
| 130 |
* @param int $object_id An object ID. |
| 131 |
* @param array $args Mixed set of arguments. |
| 132 |
* @return boolean |
| 133 |
*/ |
| 134 |
public static function resend( $object_id, $args = array() ) { |
| 135 |
$donation = charitable_get_donation( $object_id ); |
| 136 |
|
| 137 |
if ( ! is_object( $donation ) || 0 == count( $donation->get_campaign_donations() ) ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
$email = new Charitable_Email_New_Donation( array( |
| 142 |
'donation' => $donation, |
| 143 |
) ); |
| 144 |
|
| 145 |
$success = $email->send(); |
| 146 |
|
| 147 |
/** |
| 148 |
* Log that the email was sent. |
| 149 |
*/ |
| 150 |
if ( apply_filters( 'charitable_log_email_send', true, self::get_email_id(), $email ) ) { |
| 151 |
$email->log( $object_id, $success ); |
| 152 |
} |
| 153 |
|
| 154 |
return $success; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Checks whether an email can be resent. |
| 159 |
* |
| 160 |
* @since 1.5.0 |
| 161 |
* |
| 162 |
* @param int $object_id An object ID. |
| 163 |
* @param array $args Mixed set of arguments. |
| 164 |
* @return boolean |
| 165 |
*/ |
| 166 |
public static function can_be_resent( $object_id, $args = array() ) { |
| 167 |
$resendable = charitable_is_approved_status( get_post_status( $object_id ) ); |
| 168 |
|
| 169 |
/** |
| 170 |
* Filter whether the email can be resent. |
| 171 |
* |
| 172 |
* @since 1.6.0 |
| 173 |
* |
| 174 |
* @param boolean $resendable Whether the email can be resent. |
| 175 |
* @param int $object_id The donation ID. |
| 176 |
* @param array $args Mixed set of arguments. |
| 177 |
*/ |
| 178 |
return apply_filters( 'charitable_can_resend_new_donation_email', $resendable, $object_id, $args ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Return the default recipient for the email. |
| 183 |
* |
| 184 |
* @since 1.0.0 |
| 185 |
* |
| 186 |
* @return string |
| 187 |
*/ |
| 188 |
protected function get_default_recipient() { |
| 189 |
return get_option( 'admin_email' ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Return the default subject line for the email. |
| 194 |
* |
| 195 |
* @since 1.0.0 |
| 196 |
* |
| 197 |
* @return string |
| 198 |
*/ |
| 199 |
protected function get_default_subject() { |
| 200 |
return __( 'You have received a new donation', 'charitable' ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Return the default headline for the email. |
| 205 |
* |
| 206 |
* @since 1.0.0 |
| 207 |
* |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
protected function get_default_headline() { |
| 211 |
/** |
| 212 |
* Filter the default email headline. |
| 213 |
* |
| 214 |
* @since 1.0.0 |
| 215 |
* |
| 216 |
* @param string $headline Default headline. |
| 217 |
* @param Charitable_Email_New_Donation $email The email object. |
| 218 |
*/ |
| 219 |
return apply_filters( 'charitable_email_donation_receipt_default_headline', __( 'New Donation', 'charitable' ), $this ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Return the default body for the email. |
| 224 |
* |
| 225 |
* @since 1.0.0 |
| 226 |
* |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
protected function get_default_body() { |
| 230 |
ob_start(); |
| 231 |
?> |
| 232 |
<p><?php _e( '[charitable_email show=donor] has just made a donation!', 'charitable' ) ?></p> |
| 233 |
<p><strong>Summary</strong><br /> |
| 234 |
[charitable_email show=donation_summary]</p> |
| 235 |
<?php |
| 236 |
/** |
| 237 |
* Filter the default body content. |
| 238 |
* |
| 239 |
* @since 1.0.0 |
| 240 |
* |
| 241 |
* @param string $body Default email body content. |
| 242 |
* @param Charitable_Email_New_Donation $email The email object. |
| 243 |
*/ |
| 244 |
return apply_filters( 'charitable_email_new_donation_default_body', ob_get_clean(), $this ); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
endif; |
| 249 |
|