Completed
Push — issues/611 ( cbcc9a...382813 )
by Ravinder
18:21
created

get_email_attachments()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 27
rs 8.8571
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
			$this->recipient_group_name = __( 'Donor', 'give' );
39
40
			// Initialize empty payment.
41
			$this->payment = new Give_Payment( 0 );
42
43
			$this->load( array(
44
				'id'                   => 'donation-receipt',
45
				'label'                => __( 'Donation Receipt', 'give' ),
46
				'description'          => __( 'Donation Receipt Notification will be sent to donor when new donation received.', 'give' ),
47
				'notification_status'  => 'enabled',
48
				'form_metabox_setting' => true,
49
			) );
50
51
			add_action( "give_{$this->config['id']}_email_notification", array( $this, 'send_donation_receipt' ) );
52
			add_action( 'give_email_links', array( $this, 'resend_donation_receipt' ) );
53
		}
54
55
		/**
56
		 * Get default email subject.
57
		 *
58
		 * @since  2.0
59
		 * @access public
60
		 * @return string
61
		 */
62
		public function get_default_email_subject() {
63
			/**
64
			 * Filter the default subject.
65
			 *
66
			 * @since 2.0
67
			 */
68
			return apply_filters( "give_{$this->config['id']}_get_default_email_subject", esc_attr__( 'Donation Receipt', 'give' ), $this );
69
		}
70
71
72
		/**
73
		 * Get default email message.
74
		 *
75
		 * @since  2.0
76
		 * @access public
77
		 *
78
		 * @return string
79
		 */
80
		public function get_default_email_message() {
81
			/**
82
			 * Filter the donation receipt email message
83
			 *
84
			 * @since 2.0
85
			 *
86
			 * @param string $message
87
			 */
88
			return apply_filters( "give_{$this->config['id']}_get_default_email_message", give_get_default_donation_receipt_email(), $this );
89
		}
90
91
92
		/**
93
		 * Get email subject.
94
		 *
95
		 * @since  2.0
96
		 * @access public
97
		 * @return string
98
		 */
99
		public function get_email_subject() {
100
			$subject = wp_strip_all_tags( give_get_option( "{$this->config['id']}_email_subject", $this->get_default_email_subject() ) );
101
102
			/**
103
			 * Filters the donation email receipt subject.
104
			 * Note: This filter will deprecate soon.
105
			 *
106
			 * @since 1.0
107
			 */
108
			$subject = apply_filters( 'give_donation_subject', $subject, $this->payment->ID );
109
110
			/**
111
			 * Filters the donation email receipt subject.
112
			 *
113
			 * @since 2.0
114
			 */
115
			$subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this );
116
117
			return $subject;
118
		}
119
120
121
		/**
122
		 * Get email message.
123
		 *
124
		 * @since  2.0
125
		 * @access public
126
		 * @return string
127
		 */
128
		public function get_email_message() {
129
			$message = give_get_option( "{$this->config['id']}_email_message", $this->get_default_email_message() );
130
131
			/**
132
			 * Filter message on basis of email template
133
			 * Note: This filter will deprecate soon.
134
			 *
135
			 * @since 1.0
136
			 */
137
			$message = apply_filters(
138
				'give_donation_receipt_' . Give()->emails->get_template(),
139
				$message,
140
				$this->payment->ID,
141
				$this->payment->payment_meta
142
			);
143
144
			/**
145
			 * Filter the message
146
			 * Note: This filter will deprecate soon.
147
			 *
148
			 * @since 1.0
149
			 */
150
			$message = apply_filters(
151
				'give_donation_receipt',
152
				$message,
153
				$this->payment->ID,
154
				$this->payment->payment_meta
155
			);
156
157
			/**
158
			 * Filter the message
159
			 *
160
			 * @since 2.0
161
			 */
162
			$message = apply_filters(
163
				"give_{$this->config['id']}_get_email_message",
164
				$message,
165
				$this
166
			);
167
168
			return $message;
169
		}
170
171
		/**
172
		 * Get the recipient attachments.
173
		 *
174
		 * @since  2.0
175
		 * @access public
176
		 * @return array
177
		 */
178
		public function get_email_attachments() {
179
			/**
180
			 * Filter the attachments.
181
			 * Note: this filter will deprecate soon.
182
			 *
183
			 * @since 1.0
184
			 */
185
			$attachments = apply_filters(
186
				'give_receipt_attachments',
187
				array(),
188
				$this->payment->ID,
189
				$this->payment->payment_meta
190
			);
191
192
			/**
193
			 * Filter the attachments.
194
			 *
195
			 * @since 2.0
196
			 */
197
			$attachments = apply_filters(
198
				"give_{$this->config['id']}_get_email_attachments",
199
				$attachments,
200
				$this
201
			);
202
203
			return $attachments;
204
		}
205
206
207
		/**
208
		 * Set email data.
209
		 *
210
		 * @since 2.0
211
		 */
212
		public function setup_email_data() {
213
			// Set recipient email.
214
			$this->recipient_email = $this->payment->email;
215
216
			/**
217
			 * Filters the from name.
218
			 *
219
			 * @param int   $payment_id   Payment id.
220
			 * @param mixed $payment_data Payment meta data.
221
			 *
222
			 * @since 1.0
223
			 */
224
			$from_name = apply_filters(
225
				'give_donation_from_name',
226
				Give()->emails->get_from_name(),
227
				$this->payment->ID,
228
				$this->payment->payment_meta
229
			);
230
231
			/**
232
			 * Filters the from email.
233
			 *
234
			 * @param int   $payment_id   Payment id.
235
			 * @param mixed $payment_data Payment meta data.
236
			 *
237
			 * @since 1.0
238
			 */
239
			$from_email = apply_filters(
240
				'give_donation_from_address',
241
				Give()->emails->get_from_address(),
242
				$this->payment->ID,
243
				$this->payment->payment_meta
244
			);
245
246
			Give()->emails->__set( 'from_name', $from_name );
247
			Give()->emails->__set( 'from_email', $from_email );
248
			Give()->emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
249
250
			/**
251
			 * Filters the donation receipt's email headers.
252
			 *
253
			 * @param int   $payment_id   Payment id.
254
			 * @param mixed $payment_data Payment meta data.
255
			 *
256
			 * @since 1.0
257
			 */
258
			$headers = apply_filters(
259
				'give_receipt_headers',
260
				Give()->emails->get_headers(),
261
				$this->payment->ID,
262
				$this->payment->payment_meta
263
			);
264
265
			Give()->emails->__set( 'headers', $headers );
266
		}
267
268
		/**
269
		 * Send donation receipt
270
		 *
271
		 * @since  2.0
272
		 * @access public
273
		 *
274
		 * @param $payment_id
275
		 */
276
		public function send_donation_receipt( $payment_id ) {
277
			$this->payment = new Give_Payment( $payment_id );
278
279
			// Setup email data.
280
			$this->setup_email_data();
281
282
			// Send email.
283
			$this->send_email_notification( array(
284
				'payment_id' => $this->payment->ID,
285
			) );
286
		}
287
288
		/**
289
		 * Resend payment receipt by row action.
290
		 *
291
		 * @since  2.0
292
		 * @access public
293
		 *
294
		 * @param array $data
295
		 */
296
		public function resend_donation_receipt( $data ) {
297
			$purchase_id = absint( $data['purchase_id'] );
298
299
			if ( empty( $purchase_id ) ) {
300
				return;
301
			}
302
303
			// Get donation payment information.
304
			$this->payment = new Give_Payment( $purchase_id );
305
306
			if ( ! current_user_can( 'edit_give_payments', $this->payment->ID ) ) {
307
				wp_die( esc_html__( 'You do not have permission to edit payments.', 'give' ), esc_html__( 'Error', 'give' ), array(
308
					'response' => 403,
309
				) );
310
			}
311
312
			// Setup email data.
313
			$this->setup_email_data();
314
315
			// Send email.
316
			$this->send_email_notification( array(
317
				'payment_id' => $this->payment->ID,
318
			) );
319
320
			wp_redirect( add_query_arg( array(
321
				'give-message' => 'email_sent',
322
				'give-action'  => false,
323
				'purchase_id'  => false,
324
			) ) );
325
			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...
326
		}
327
	}
328
329
endif; // End class_exists check
330
331
return Give_Donation_Receipt_Email::get_instance();
332