Completed
Push — issues/611 ( 7d2e61...f3b0b1 )
by Ravinder
16:40
created

Give_Donation_Receipt_Email::get_email_message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 1
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Donation Receipt Email
4
 *
5
 * This class handles all email notification settings.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.0
12
 */
13
14
// Exit if access directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
if ( ! class_exists( 'Give_Donation_Receipt_Email' ) ) :
20
21
	/**
22
	 * Give_Donation_Receipt_Email
23
	 *
24
	 * @abstract
25
	 * @since       2.0
26
	 */
27
	class Give_Donation_Receipt_Email extends Give_Email_Notification {
28
		/* @var Give_Payment $payment */
29
		public $payment;
30
31
		/**
32
		 * Create a class instance.
33
		 *
34
		 * @access  public
35
		 * @since   2.0
36
		 */
37
		public function init() {
38
			// Initialize empty payment.
39
			$this->payment = new Give_Payment( 0 );
40
41
			$this->load( array(
42
				'id'                   => 'donation-receipt',
43
				'label'                => __( 'Donation Receipt', 'give' ),
44
				'description'          => __( 'Donation Receipt Notification will be sent to donor when new donation received.', 'give' ),
45
				'notification_status'  => 'enabled',
46
				'form_metabox_setting' => true,
47
				'recipient_group_name' => __( 'Donor', 'give' ),
48
				'default_email_subject' => esc_attr__( 'Donation Receipt', 'give' ),
49
				'default_email_message' => give_get_default_donation_receipt_email()
50
			) );
51
52
			add_action( "give_{$this->config['id']}_email_notification", array( $this, 'send_donation_receipt' ) );
53
			add_action( 'give_email_links', array( $this, 'resend_donation_receipt' ) );
54
		}
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
					"{$this->config['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( 'give_donation_subject', $subject, $this->payment->ID );
83
84
			/**
85
			 * Filters the donation email receipt subject.
86
			 *
87
			 * @since 2.0
88
			 */
89
			$subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this, $form_id );
90
91
			return $subject;
92
		}
93
94
95
		/**
96
		 * Get email message.
97
		 *
98
		 * @since  2.0
99
		 * @access public
100
		 *
101
		 * @param int $form_id
102
		 * @return string
103
		 */
104
		public function get_email_message( $form_id = null ) {
105
			$message = Give_Email_Notification_Util::get_value(
106
				$this,
107
				"{$this->config['id']}_email_message",
108
				$form_id,
109
				$this->config['default_email_message']
110
			);
111
112
			/**
113
			 * Filter message on basis of email template
114
			 * Note: This filter will deprecate soon.
115
			 *
116
			 * @since 1.0
117
			 */
118
			$message = apply_filters(
119
				'give_donation_receipt_' . Give()->emails->get_template(),
120
				$message,
121
				$this->payment->ID,
122
				$this->payment->payment_meta
123
			);
124
125
			/**
126
			 * Filter the message
127
			 * Note: This filter will deprecate soon.
128
			 *
129
			 * @since 1.0
130
			 */
131
			$message = apply_filters(
132
				'give_donation_receipt',
133
				$message,
134
				$this->payment->ID,
135
				$this->payment->payment_meta
136
			);
137
138
			/**
139
			 * Filter the message
140
			 *
141
			 * @since 2.0
142
			 */
143
			$message = apply_filters(
144
				"give_{$this->config['id']}_get_email_message",
145
				$message,
146
				$this,
147
				$form_id
148
			);
149
150
			return $message;
151
		}
152
153
		/**
154
		 * Get the recipient attachments.
155
		 *
156
		 * @since  2.0
157
		 * @access public
158
		 *
159
		 * @param int $form_id
160
		 * @return array
161
		 */
162
		public function get_email_attachments( $form_id = null) {
163
			/**
164
			 * Filter the attachments.
165
			 * Note: this filter will deprecate soon.
166
			 *
167
			 * @since 1.0
168
			 */
169
			$attachments = apply_filters(
170
				'give_receipt_attachments',
171
				array(),
172
				$this->payment->ID,
173
				$this->payment->payment_meta
174
			);
175
176
			/**
177
			 * Filter the attachments.
178
			 *
179
			 * @since 2.0
180
			 */
181
			$attachments = apply_filters(
182
				"give_{$this->config['id']}_get_email_attachments",
183
				$attachments,
184
				$this,
185
				$form_id
186
			);
187
188
			return $attachments;
189
		}
190
191
192
		/**
193
		 * Set email data.
194
		 *
195
		 * @since 2.0
196
		 */
197
		public function setup_email_data() {
198
			// Set recipient email.
199
			$this->recipient_email = $this->payment->email;
200
201
			/**
202
			 * Filters the from name.
203
			 *
204
			 * @param int   $payment_id   Payment id.
205
			 * @param mixed $payment_data Payment meta data.
206
			 *
207
			 * @since 1.0
208
			 */
209
			$from_name = apply_filters(
210
				'give_donation_from_name',
211
				Give()->emails->get_from_name(),
212
				$this->payment->ID,
213
				$this->payment->payment_meta
214
			);
215
216
			/**
217
			 * Filters the from email.
218
			 *
219
			 * @param int   $payment_id   Payment id.
220
			 * @param mixed $payment_data Payment meta data.
221
			 *
222
			 * @since 1.0
223
			 */
224
			$from_email = apply_filters(
225
				'give_donation_from_address',
226
				Give()->emails->get_from_address(),
227
				$this->payment->ID,
228
				$this->payment->payment_meta
229
			);
230
231
			Give()->emails->__set( 'from_name', $from_name );
232
			Give()->emails->__set( 'from_email', $from_email );
233
			Give()->emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
234
235
			/**
236
			 * Filters the donation receipt's email headers.
237
			 *
238
			 * @param int   $payment_id   Payment id.
239
			 * @param mixed $payment_data Payment meta data.
240
			 *
241
			 * @since 1.0
242
			 */
243
			$headers = apply_filters(
244
				'give_receipt_headers',
245
				Give()->emails->get_headers(),
246
				$this->payment->ID,
247
				$this->payment->payment_meta
248
			);
249
250
			Give()->emails->__set( 'headers', $headers );
251
		}
252
253
		/**
254
		 * Send donation receipt
255
		 *
256
		 * @since  2.0
257
		 * @access public
258
		 *
259
		 * @param $payment_id
260
		 */
261
		public function send_donation_receipt( $payment_id ) {
262
			$this->payment = new Give_Payment( $payment_id );
263
264
			// Setup email data.
265
			$this->setup_email_data();
266
267
			// Send email.
268
			$this->send_email_notification( array(
269
				'payment_id' => $this->payment->ID,
270
			) );
271
		}
272
273
		/**
274
		 * Resend payment receipt by row action.
275
		 *
276
		 * @since  2.0
277
		 * @access public
278
		 *
279
		 * @param array $data
280
		 */
281
		public function resend_donation_receipt( $data ) {
282
			$purchase_id = absint( $data['purchase_id'] );
283
284
			if ( empty( $purchase_id ) ) {
285
				return;
286
			}
287
288
			// Get donation payment information.
289
			$this->payment = new Give_Payment( $purchase_id );
290
291
			if ( ! current_user_can( 'edit_give_payments', $this->payment->ID ) ) {
292
				wp_die( esc_html__( 'You do not have permission to edit payments.', 'give' ), esc_html__( 'Error', 'give' ), array(
293
					'response' => 403,
294
				) );
295
			}
296
297
			// Setup email data.
298
			$this->setup_email_data();
299
300
			// Send email.
301
			$this->send_email_notification( array(
302
				'payment_id' => $this->payment->ID,
303
			) );
304
305
			wp_redirect( add_query_arg( array(
306
				'give-message' => 'email_sent',
307
				'give-action'  => false,
308
				'purchase_id'  => false,
309
			) ) );
310
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method resend_donation_receipt() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
311
		}
312
	}
313
314
endif; // End class_exists check
315
316
return Give_Donation_Receipt_Email::get_instance();
317