Test Failed
Push — master ( 966cf3...fe1ced )
by Devin
13:57 queued 06:53
created

Give_New_Offline_Donation_Email   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 289
Duplicated Lines 25.61 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 74
loc 289
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 29 2
A get_default_email_subject() 0 23 1
B get_default_email_message() 0 39 1
B get_email_message() 32 32 1
B get_email_attachments() 0 24 1
A setup_email_data() 0 11 1
A setup_email_notification() 16 16 2
A set_notification_status() 14 14 4
A add_metabox_setting_field() 12 12 2

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
 * New Offline Donation 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_New_Offline_Donation_Email' ) ) :
20
21
	/**
22
	 * Give_New_Offline_Donation_Email
23
	 *
24
	 * @abstract
25
	 * @since       2.0
26
	 */
27
	class Give_New_Offline_Donation_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'                           => 'new-offline-donation',
43
				'label'                        => __( 'New Offline Donation', 'give' ),
44
				'description'                  => __( 'Sent to designated recipient(s) for a new (pending) offline donation.', 'give' ),
45
				'has_recipient_field'          => true,
46
				'notification_status'          => give_is_gateway_active( 'offline' ) ? 'enabled' : 'disabled',
47
				'notification_status_editable' => false,
48
				'preview_email_tags_values'    => array(
49
					'payment_method' => esc_html__( 'Offline', 'give' ),
50
				),
51
				'default_email_subject'        => $this->get_default_email_subject(),
52
				'default_email_message'        => $this->get_default_email_message(),
53
				'notices' => array(
54
					'non-notification-status-editable' => sprintf(
55
						'%1$s <a href="%2$s">%3$s &raquo;</a>',
56
						__( 'This notification is automatically toggled based on whether the gateway is enabled or not.', 'give' ),
57
						esc_url( admin_url('edit.php?post_type=give_forms&page=give-settings&tab=gateways&section=offline-donations') ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
58
						__( 'Edit Setting', 'give' )
59
					)
60
				),
61
			) );
62
63
			add_action( 'give_insert_payment', array( $this, 'setup_email_notification' ) );
64
			add_action( 'give_save_settings_give_settings', array( $this, 'set_notification_status' ), 10, 2 );
65
		}
66
67
		/**
68
		 * Get default email subject.
69
		 *
70
		 * @since  2.0
71
		 * @access public
72
		 * @return string
73
		 */
74
		public function get_default_email_subject() {
75
			/**
76
			 * Filter the default subject.
77
			 * Note: This filter will deprecate soon.
78
			 *
79
			 * @since 1.0
80
			 */
81
			$subject = apply_filters(
82
				'give_offline_admin_donation_notification_subject',
83
				__( 'New Pending Donation', 'give' )
84
			);
85
86
			/**
87
			 * Filter the default subject
88
			 *
89
			 * @since 2.0
90
			 */
91
			return apply_filters(
92
				"give_{$this->config['id']}_get_default_email_subject",
93
				$subject,
94
				$this
95
			);
96
		}
97
98
99
		/**
100
		 * Get default email message.
101
		 *
102
		 * @since  2.0
103
		 * @access public
104
		 *
105
		 * @return string
106
		 */
107
		public function get_default_email_message() {
108
			$message = __( 'Dear Admin,', 'give' ) . "\n\n";
109
			$message .= __( 'An offline donation has been made on your website:', 'give' ) . ' ' . get_bloginfo( 'name' ) . ' ';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'get_bloginfo'
Loading history...
110
			$message .= __( 'Hooray! The donation is in a pending status and is awaiting payment. Donation instructions have been emailed to the donor. Once you receive payment, be sure to mark the donation as complete using the link below.', 'give' ) . "\n\n";
111
112
			$message .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
113
			$message .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n\n";
114
115
			$message .= sprintf(
116
				'<a href="%1$s">%2$s</a>',
117
				admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&id=' . $this->payment->ID ),
118
				__( 'Click Here to View and/or Update Donation Details', 'give' )
119
			) . "\n\n";
120
121
			/**
122
			 * Filter the donation receipt email message
123
			 * Note: This filter will deprecate soon.
124
			 *
125
			 * @since 1.0
126
			 *
127
			 * @param string $message
128
			 */
129
			$message = apply_filters(
130
				'give_default_new_offline_donation_email',
131
				$message,
132
				$this->payment->ID
133
			);
134
135
			/**
136
			 * Filter the default message
137
			 *
138
			 * @since 2.0
139
			 */
140
			return apply_filters(
141
				"give_{$this->config['id']}_get_default_email_message",
142
				$message,
143
				$this
144
			);
145
		}
146
147
148
		/**
149
		 * Get message
150
		 *
151
		 * @since 2.0
152
		 *
153
		 * @param int $form_id
154
		 *
155
		 * @return string
156
		 */
157 View Code Duplication
		public function get_email_message( $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...
158
			$message = Give_Email_Notification_Util::get_value(
159
				$this,
160
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
161
				$form_id,
162
				$this->config['default_email_message']
163
			);
164
165
			/**
166
			 * Filter the email message.
167
			 * Note: This filter will deprecate soon.
168
			 *
169
			 * @since 1.0
170
			 */
171
			$message = apply_filters(
172
				'give_offline_admin_donation_notification',
173
				$message,
174
				$this->payment->ID
175
			);
176
177
			/**
178
			 * Filter the email message
179
			 *
180
			 * @since 2.0
181
			 */
182
			return apply_filters(
183
				"give_{$this->config['id']}_get_email_message",
184
				$message,
185
				$this,
186
				$form_id
187
			);
188
		}
189
190
191
		/**
192
		 * Get attachments.
193
		 *
194
		 * @since 2.0
195
		 *
196
		 * @param int $form_id
197
		 *
198
		 * @return array
199
		 */
200
		public function get_email_attachments( $form_id = null ) {
201
			/**
202
			 * Filter the attachments.
203
			 * Note: This filter will deprecate soon.
204
			 *
205
			 * @since 1.0
206
			 */
207
			$attachment = apply_filters(
208
				'give_offline_admin_donation_notification_attachments',
209
				array(),
210
				$this->payment->ID
211
			);
212
213
			/**
214
			 * Filter the attachments.
215
			 *
216
			 * @since 2.0
217
			 */
218
			return apply_filters(
219
				"give_{$this->config['id']}_get_email_attachments",
220
				$attachment,
221
				$this
222
			);
223
		}
224
225
226
		/**
227
		 * Set email data.
228
		 *
229
		 * @since 2.0
230
		 */
231
		public function setup_email_data() {
232
			// Set header.
233
			Give()->emails->__set(
234
				'headers',
235
				apply_filters(
236
					'give_offline_admin_donation_notification_headers',
237
					Give()->emails->get_headers(),
238
					$this->payment->ID
239
				)
240
			);
241
		}
242
243
		/**
244
		 * Setup email notification.
245
		 *
246
		 * @since  2.0
247
		 * @access public
248
		 *
249
		 * @param int $payment_id
250
		 */
251 View Code Duplication
		public function setup_email_notification( $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...
252
			$this->payment = new Give_Payment( $payment_id );
253
254
			// Exit if not donation was not with offline donation.
255
			if ( 'offline' !== $this->payment->gateway ) {
256
				return;
257
			}
258
259
			// Set email data.
260
			$this->setup_email_data();
261
262
			// Send email.
263
			$this->send_email_notification( array(
264
				'payment_id' => $this->payment->ID,
265
			) );
266
		}
267
268
		/**
269
		 * Set notification status
270
		 *
271
		 * @since  2.0
272
		 * @access public
273
		 *
274
		 * @param $update_options
275
		 * @param $option_name
276
		 */
277 View Code Duplication
		public function set_notification_status( $update_options, $option_name ) {
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...
278
			// Get updated settings.
279
			$update_options = give_get_settings();
280
281
			$notification_status = isset( $update_options['gateways']['offline'] ) ? 'enabled' : 'disabled';
282
283
			if (
284
				empty( $update_options[ "{$this->config['id']}_notification" ] )
285
				|| $notification_status !== $update_options[ "{$this->config['id']}_notification" ]
286
			) {
287
				$update_options[ "{$this->config['id']}_notification" ] = $notification_status;
288
				update_option( $option_name, $update_options );
289
			}
290
		}
291
292
		/**
293
		 * Register email settings to form metabox.
294
		 *
295
		 * @since  2.0
296
		 * @access public
297
		 *
298
		 * @param array $settings
299
		 * @param int   $form_id
300
		 *
301
		 * @return array
302
		 */
303 View Code Duplication
		public function add_metabox_setting_field( $settings, $form_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...
304
305
			if ( in_array( 'offline', array_keys( give_get_enabled_payment_gateways($form_id) ) ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
306
				$settings[] = array(
307
					'id'     => $this->config['id'],
308
					'title'  => $this->config['label'],
309
					'fields' => $this->get_setting_fields( $form_id ),
310
				);
311
			}
312
313
			return $settings;
314
		}
315
	}
316
317
endif; // End class_exists check
318
319
return Give_New_Offline_Donation_Email::get_instance();
320