Test Failed
Pull Request — master (#2668)
by Devin
07:24 queued 30s
created

Give_Donation_Receipt_Email   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 295
Duplicated Lines 44.07 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 130
loc 295
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 18 1
B get_email_subject() 36 36 1
A get_email_message() 0 48 1
B get_email_attachments() 28 28 1
A setup_email_data() 55 55 1
A send_donation_receipt() 11 11 1
B resend_donation_receipt() 0 31 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Donation Receipt Email
4
 *
5
 *
6
 * @package     Give
7
 * @subpackage  Classes/Emails
8
 * @copyright   Copyright (c) 2016, WordImpress
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @since       2.0
11
 */
12
13
// Exit if access directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( 'Give_Donation_Receipt_Email' ) ) :
19
20
	/**
21
	 * Give_Donation_Receipt_Email
22
	 *
23
	 * @abstract
24
	 * @since       2.0
25
	 */
26
	class Give_Donation_Receipt_Email extends Give_Email_Notification {
27
		/* @var Give_Payment $payment */
28
		public $payment;
29
30
		/**
31
		 * Create a class instance.
32
		 *
33
		 * @access  public
34
		 * @since   2.0
35
		 */
36
		public function init() {
37
			// Initialize empty payment.
38
			$this->payment = new Give_Payment( 0 );
39
40
			$this->load( array(
41
				'id'                   => 'donation-receipt',
42
				'label'                => __( 'Donation Receipt', 'give' ),
43
				'description'          => __( 'Sent to the donor when their donation completes or a pending donation is marked as complete.', 'give' ),
44
				'notification_status'  => 'enabled',
45
				'form_metabox_setting' => true,
46
				'recipient_group_name' => __( 'Donor', 'give' ),
47
				'default_email_subject' => esc_attr__( 'Donation Receipt', 'give' ),
48
				'default_email_message' => give_get_default_donation_receipt_email()
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
		/**
57
		 * Get email subject.
58
		 *
59
		 * @since  2.0
60
		 * @access public
61
		 *
62
		 * @param int $form_id
63
		 * @return string
64
		 */
65 View Code Duplication
		public function get_email_subject( $form_id = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
			$subject = wp_strip_all_tags(
67
				Give_Email_Notification_Util::get_value(
68
					$this,
69
					Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject',
70
					$form_id,
71
					$this->config['default_email_subject']
72
				)
73
			);
74
75
			/**
76
			 * Filters the donation email receipt subject.
77
			 * Note: This filter will deprecate soon.
78
			 *
79
			 * @since 1.0
80
			 */
81
			$subject = apply_filters(
82
				'give_donation_subject',
83
				$subject,
84
				$this->payment->ID
85
			);
86
87
			/**
88
			 * Filters the donation email receipt subject.
89
			 *
90
			 * @since 2.0
91
			 */
92
			$subject = apply_filters(
93
				"give_{$this->config['id']}_get_email_subject",
94
				$subject,
95
				$this,
96
				$form_id
97
			);
98
99
			return $subject;
100
		}
101
102
103
		/**
104
		 * Get email message.
105
		 *
106
		 * @since  2.0
107
		 * @access public
108
		 *
109
		 * @param int $form_id
110
		 * @return string
111
		 */
112
		public function get_email_message( $form_id = null ) {
113
			$message = Give_Email_Notification_Util::get_value(
114
				$this,
115
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
116
				$form_id,
117
				$this->config['default_email_message']
118
			);
119
120
			/**
121
			 * Filter message on basis of email template
122
			 * Note: This filter will deprecate soon.
123
			 *
124
			 * @since 1.0
125
			 */
126
			$message = apply_filters(
127
				'give_donation_receipt_' . Give()->emails->get_template(),
128
				$message,
129
				$this->payment->ID,
130
				$this->payment->payment_meta
131
			);
132
133
			/**
134
			 * Filter the message
135
			 * Note: This filter will deprecate soon.
136
			 *
137
			 * @since 1.0
138
			 */
139
			$message = apply_filters(
140
				'give_donation_receipt',
141
				$message,
142
				$this->payment->ID,
143
				$this->payment->payment_meta
144
			);
145
146
			/**
147
			 * Filter the message
148
			 *
149
			 * @since 2.0
150
			 */
151
			$message = apply_filters(
152
				"give_{$this->config['id']}_get_email_message",
153
				$message,
154
				$this,
155
				$form_id
156
			);
157
158
			return $message;
159
		}
160
161
		/**
162
		 * Get the recipient attachments.
163
		 *
164
		 * @since  2.0
165
		 * @access public
166
		 *
167
		 * @param int $form_id
168
		 * @return array
169
		 */
170 View Code Duplication
		public function get_email_attachments( $form_id = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
			/**
172
			 * Filter the attachments.
173
			 * Note: this filter will deprecate soon.
174
			 *
175
			 * @since 1.0
176
			 */
177
			$attachments = apply_filters(
178
				'give_receipt_attachments',
179
				array(),
180
				$this->payment->ID,
181
				$this->payment->payment_meta
182
			);
183
184
			/**
185
			 * Filter the attachments.
186
			 *
187
			 * @since 2.0
188
			 */
189
			$attachments = apply_filters(
190
				"give_{$this->config['id']}_get_email_attachments",
191
				$attachments,
192
				$this,
193
				$form_id
194
			);
195
196
			return $attachments;
197
		}
198
199
200
		/**
201
		 * Set email data.
202
		 *
203
		 * @since 2.0
204
		 */
205 View Code Duplication
		public function setup_email_data() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
			// Set recipient email.
207
			$this->recipient_email = $this->payment->email;
208
209
			/**
210
			 * Filters the from name.
211
			 *
212
			 * @param int   $payment_id   Payment id.
213
			 * @param mixed $payment_data Payment meta data.
214
			 *
215
			 * @since 1.0
216
			 */
217
			$from_name = apply_filters(
218
				'give_donation_from_name',
219
				Give()->emails->get_from_name(),
220
				$this->payment->ID,
221
				$this->payment->payment_meta
222
			);
223
224
			/**
225
			 * Filters the from email.
226
			 *
227
			 * @param int   $payment_id   Payment id.
228
			 * @param mixed $payment_data Payment meta data.
229
			 *
230
			 * @since 1.0
231
			 */
232
			$from_email = apply_filters(
233
				'give_donation_from_address',
234
				Give()->emails->get_from_address(),
235
				$this->payment->ID,
236
				$this->payment->payment_meta
237
			);
238
239
			Give()->emails->__set( 'from_name', $from_name );
240
			Give()->emails->__set( 'from_email', $from_email );
241
			Give()->emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
242
243
			/**
244
			 * Filters the donation receipt's email headers.
245
			 *
246
			 * @param int   $payment_id   Payment id.
247
			 * @param mixed $payment_data Payment meta data.
248
			 *
249
			 * @since 1.0
250
			 */
251
			$headers = apply_filters(
252
				'give_receipt_headers',
253
				Give()->emails->get_headers(),
254
				$this->payment->ID,
255
				$this->payment->payment_meta
256
			);
257
258
			Give()->emails->__set( 'headers', $headers );
259
		}
260
261
		/**
262
		 * Send donation receipt
263
		 *
264
		 * @since  2.0
265
		 * @access public
266
		 *
267
		 * @param $payment_id
268
		 */
269 View Code Duplication
		public function send_donation_receipt( $payment_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
			$this->payment = new Give_Payment( $payment_id );
271
272
			// Setup email data.
273
			$this->setup_email_data();
274
275
			// Send email.
276
			$this->send_email_notification( array(
277
				'payment_id' => $this->payment->ID,
278
			) );
279
		}
280
281
		/**
282
		 * Resend payment receipt by row action.
283
		 *
284
		 * @since  2.0
285
		 * @access public
286
		 *
287
		 * @param array $data
288
		 */
289
		public function resend_donation_receipt( $data ) {
290
			$purchase_id = absint( $data['purchase_id'] );
291
292
			if ( empty( $purchase_id ) ) {
293
				return;
294
			}
295
296
			// Get donation payment information.
297
			$this->payment = new Give_Payment( $purchase_id );
298
299
			if ( ! current_user_can( 'edit_give_payments', $this->payment->ID ) ) {
300
				wp_die( esc_html__( 'You do not have permission to edit donations.', 'give' ), esc_html__( 'Error', 'give' ), array(
301
					'response' => 403,
302
				) );
303
			}
304
305
			// Setup email data.
306
			$this->setup_email_data();
307
308
			// Send email.
309
			$this->send_email_notification( array(
310
				'payment_id' => $this->payment->ID,
311
			) );
312
313
			wp_redirect( add_query_arg( array(
314
				'give-message' => 'email_sent',
315
				'give-action'  => false,
316
				'purchase_id'  => false,
317
			) ) );
318
			exit;
319
		}
320
	}
321
322
endif; // End class_exists check
323
324
return Give_Donation_Receipt_Email::get_instance();
325