PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.6
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.6
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / emails / class-charitable-emails.php

class-charitable-emails.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.6, at includes/emails/class-charitable-emails.php

302 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class that sets up the emails.
4 *
5 * @version 1.5.0
6 * @package Charitable/Classes/Charitable_Emails
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 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) { exit; }
14
15 if ( ! class_exists( 'Charitable_Emails' ) ) :
16
17 /**
18 * Charitable_Emails
19 *
20 * @since 1.0.0
21 */
22 class Charitable_Emails {
23
24 /**
25 * The single instance of this class.
26 *
27 * @var Charitable_Emails|null
28 */
29 private static $instance = null;
30
31 /**
32 * All available emails.
33 *
34 * @var string[]
35 */
36 private $emails;
37
38 /**
39 * Set up the class.
40 *
41 * Note that the only way to instantiate an object is with the charitable_start method,
42 * which can only be called during the start phase. In other words, don't try
43 * to instantiate this object.
44 *
45 * @since 1.0.0
46 */
47 private function __construct() {
48 /* 3rd party hook for overriding anything we've done so far. */
49 do_action( 'charitable_emails_start', $this );
50 }
51
52 /**
53 * Returns and/or create the single instance of this class.
54 *
55 * @since 1.2.0
56 *
57 * @return Charitable_Emails
58 */
59 public static function get_instance() {
60 if ( is_null( self::$instance ) ) {
61 self::$instance = new self();
62 }
63
64 return self::$instance;
65 }
66
67 /**
68 * Register Charitable emails.
69 *
70 * @since 1.0.0
71 *
72 * @return string[]
73 */
74 public function register_emails() {
75 $this->emails = apply_filters( 'charitable_emails', array(
76 'donation_receipt' => 'Charitable_Email_Donation_Receipt',
77 'offline_donation_receipt' => 'Charitable_Email_Offline_Donation_Receipt',
78 'new_donation' => 'Charitable_Email_New_Donation',
79 'campaign_end' => 'Charitable_Email_Campaign_End',
80 'offline_donation_notification' => 'Charitable_Email_Offline_Donation_Notification',
81 'password_reset' => 'Charitable_Email_Password_Reset',
82 'email_verification' => 'Charitable_Email_Email_Verification',
83 ) );
84
85 return $this->emails;
86 }
87
88 /**
89 * Register admin actions for default emails.
90 *
91 * @since 1.5.0
92 *
93 * @return void
94 */
95 public function register_admin_actions() {
96 $donation_actions = charitable_get_donation_actions();
97
98 /**
99 * Filter the list of resendable donation emails.
100 *
101 * @since 1.5.0
102 *
103 * @param string[] $emails The list of emails and their label.
104 */
105 $emails = apply_filters( 'charitable_resendable_donation_emails', array(
106 'donation_receipt',
107 'new_donation',
108 'offline_donation_receipt',
109 'offline_donation_notification',
110 ) );
111
112 foreach ( $emails as $email ) {
113 $class = $this->get_email( $email );
114 $object = new $class;
115
116 $donation_actions->register( 'resend_' . $email, array(
117 'label' => $object->get_name(),
118 'callback' => array( $this, 'resend_email' ),
119 'button_text' => __( 'Resend Email', 'charitable' ),
120 'active_callback' => array( $class, 'can_be_resent' ),
121 'success_message' => 11,
122 'failed_message' => 12,
123 ), __( 'Resend Donation Emails', 'charitable' ) );
124 }
125 }
126
127 /**
128 * Resend an email.
129 *
130 * This is the callback for all of the resend email actions.
131 *
132 * @since 1.5.0
133 *
134 * @param boolean $success Whether the action has been successfully completed.
135 * @param int $object_id An object ID.
136 * @param array $args Mixed set of arguments.
137 * @param string $action The action we are executing.
138 * @return boolean
139 */
140 public function resend_email( $success, $object_id, $args, $action ) {
141 $email = str_replace( 'resend_', '', $action );
142 $class = $this->get_email( $email );
143 $object = new $class;
144
145 return $object->resend( $object_id, $args );
146 }
147
148 /**
149 * Receives a request to enable or disable an email and validates it before passing it off.
150 *
151 * @since 1.0.0
152 *
153 * @return void
154 */
155 public function handle_email_settings_request() {
156 if ( ! wp_verify_nonce( $_REQUEST['_nonce'], 'email' ) ) {
157 wp_die( __( 'Cheatin\' eh?!', 'charitable' ) );
158 }
159
160 $email = isset( $_REQUEST['email_id'] ) ? $_REQUEST['email_id'] : false;
161
162 /* Gateway must be set */
163 if ( false === $email ) {
164 wp_die( __( 'Missing email.', 'charitable' ) );
165 }
166
167 /* Validate email. */
168 if ( ! isset( $this->emails[ $email ] ) ) {
169 wp_die( __( 'Invalid email.', 'charitable' ) );
170 }
171
172 /* All good, so disable or enable the email */
173 if ( 'charitable_disable_email' == current_filter() ) {
174 $this->disable_email( $email );
175 } else {
176 $this->enable_email( $email );
177 }
178 }
179
180 /**
181 * Returns all available emails.
182 *
183 * @since 1.0.0
184 *
185 * @return string
186 */
187 public function get_available_emails() {
188 return $this->emails;
189 }
190
191 /**
192 * Returns the currently enabled emails.
193 *
194 * @since 1.0.0
195 *
196 * @return string[]
197 */
198 public function get_enabled_emails() {
199 $enabled = charitable_get_option( 'enabled_emails', array() );
200
201 /* The Password Reset & Email Verification emails are always enabled. */
202 $enabled['password_reset'] = 'Charitable_Email_Password_Reset';
203 $enabled['email_verification'] = 'Charitable_Email_Email_Verification';
204
205 return $enabled;
206 }
207
208 /**
209 * Returns a list of the names of currently enabled emails.
210 *
211 * @since 1.3.0
212 *
213 * @return string[]
214 */
215 public function get_enabled_emails_names() {
216 $emails = array();
217
218 foreach ( $this->get_enabled_emails() as $class ) {
219 if ( ! class_exists( $class ) ) {
220 continue;
221 }
222
223 $email = new $class;
224 $emails[ $email->get_email_id() ] = $email->get_name();
225 }
226
227 return $emails;
228 }
229
230 /**
231 * Return the email class name for a given email.
232 *
233 * @since 1.0.0
234 *
235 * @param string $email The email to return.
236 * @return string|false A string representing the email class name if the
237 * email is registered; false if it is not registered.
238 */
239 public function get_email( $email ) {
240 return isset( $this->emails[ $email ] ) ? $this->emails[ $email ] : false;
241 }
242
243 /**
244 * Returns whether the passed email is enabled.
245 *
246 * @since 1.0.0
247 *
248 * @param string $email_id The email to check for.
249 * @return boolean
250 */
251 public function is_enabled_email( $email_id ) {
252 return array_key_exists( $email_id, $this->get_enabled_emails() );
253 }
254
255 /**
256 * Enable an email.
257 *
258 * @since 1.0.0
259 *
260 * @param string $email The email to be enabled.
261 * @return void
262 */
263 protected function enable_email( $email ) {
264 $settings = get_option( 'charitable_settings' );
265 $enabled_emails = isset( $settings['enabled_emails'] ) ? $settings['enabled_emails'] : array();
266 $enabled_emails[ $email ] = $this->emails[ $email ];
267 $settings['enabled_emails'] = $enabled_emails;
268
269 update_option( 'charitable_settings', $settings );
270
271 Charitable_Settings::get_instance()->add_update_message( __( 'Email enabled', 'charitable' ), 'success' );
272
273 do_action( 'charitable_email_enable', $email );
274 }
275
276 /**
277 * Disable an email.
278 *
279 * @since 1.0.0
280 *
281 * @param string $email The email to be disabled.
282 * @return void
283 */
284 protected function disable_email( $email ) {
285 $settings = get_option( 'charitable_settings' );
286
287 if ( ! isset( $settings['enabled_emails'][ $email ] ) ) {
288 return;
289 }
290
291 unset( $settings['enabled_emails'][ $email ] );
292
293 update_option( 'charitable_settings', $settings );
294
295 Charitable_Settings::get_instance()->add_update_message( __( 'Email disabled', 'charitable' ), 'success' );
296
297 do_action( 'charitable_email_disable', $email );
298 }
299 }
300
301 endif;
302