Test Failed
Push — issues/370 ( 90279e )
by Ravinder
05:35
created

Give_New_Offline_Donation_Email   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 257
Duplicated Lines 24.12 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 62
loc 257
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 6

8 Methods

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

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'                  => __( 'Donation Notification will be sent to admin when new offline donation received.', '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
			) );
54
55
			add_action( 'give_insert_payment', array( $this, 'setup_email_notification' ) );
56
			add_action( 'give_save_settings_give_settings', array( $this, 'set_notification_status' ), 10, 2 );
57
		}
58
59
		/**
60
		 * Get default email subject.
61
		 *
62
		 * @since  2.0
63
		 * @access public
64
		 * @return string
65
		 */
66
		public function get_default_email_subject() {
67
			/**
68
			 * Filter the default subject.
69
			 * Note: This filter will deprecate soon.
70
			 *
71
			 * @since 1.0
72
			 */
73
			$subject = apply_filters(
74
				'give_offline_admin_donation_notification_subject',
75
				__( 'New Pending Donation', 'give' )
76
			);
77
78
			/**
79
			 * Filter the default subject
80
			 *
81
			 * @since 2.0
82
			 */
83
			return apply_filters(
84
				"give_{$this->config['id']}_get_default_email_subject",
85
				$subject,
86
				$this
87
			);
88
		}
89
90
91
		/**
92
		 * Get default email message.
93
		 *
94
		 * @since  2.0
95
		 * @access public
96
		 *
97
		 * @return string
98
		 */
99
		public function get_default_email_message() {
100
			$message = __( 'Dear Admin,', 'give' ) . "\n\n";
101
			$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...
102
			$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";
103
104
			$message .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
105
			$message .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n\n";
106
107
			$message .= sprintf(
108
				'<a href="%1$s">%2$s</a>',
109
				admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&id=' . $this->payment->ID ),
110
				__( 'Click Here to View and/or Update Donation Details', 'give' )
111
			) . "\n\n";
112
113
			/**
114
			 * Filter the donation receipt email message
115
			 * Note: This filter will deprecate soon.
116
			 *
117
			 * @since 1.0
118
			 *
119
			 * @param string $message
120
			 */
121
			$message = apply_filters(
122
				'give_default_new_offline_donation_email',
123
				$message,
124
				$this->payment->ID
125
			);
126
127
			/**
128
			 * Filter the default message
129
			 *
130
			 * @since 2.0
131
			 */
132
			return apply_filters(
133
				"give_{$this->config['id']}_get_default_email_message",
134
				$message,
135
				$this
136
			);
137
		}
138
139
140
		/**
141
		 * Get message
142
		 *
143
		 * @since 2.0
144
		 *
145
		 * @param int $form_id
146
		 *
147
		 * @return string
148
		 */
149 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...
150
			$message = Give_Email_Notification_Util::get_value(
151
				$this,
152
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
153
				$form_id,
154
				$this->config['default_email_message']
155
			);
156
157
			/**
158
			 * Filter the email message.
159
			 * Note: This filter will deprecate soon.
160
			 *
161
			 * @since 1.0
162
			 */
163
			$message = apply_filters(
164
				'give_offline_admin_donation_notification',
165
				$message,
166
				$this->payment->ID
167
			);
168
169
			/**
170
			 * Filter the email message
171
			 *
172
			 * @since 2.0
173
			 */
174
			return apply_filters(
175
				"give_{$this->config['id']}_get_email_message",
176
				$message,
177
				$this,
178
				$form_id
179
			);
180
		}
181
182
183
		/**
184
		 * Get attachments.
185
		 *
186
		 * @since 2.0
187
		 *
188
		 * @param int $form_id
189
		 *
190
		 * @return array
191
		 */
192
		public function get_email_attachments( $form_id = null ) {
193
			/**
194
			 * Filter the attachments.
195
			 * Note: This filter will deprecate soon.
196
			 *
197
			 * @since 1.0
198
			 */
199
			$attachment = apply_filters(
200
				'give_offline_admin_donation_notification_attachments',
201
				array(),
202
				$this->payment->ID
203
			);
204
205
			/**
206
			 * Filter the attachments.
207
			 *
208
			 * @since 2.0
209
			 */
210
			return apply_filters(
211
				"give_{$this->config['id']}_get_email_attachments",
212
				$attachment,
213
				$this
214
			);
215
		}
216
217
218
		/**
219
		 * Set email data.
220
		 *
221
		 * @since 2.0
222
		 */
223
		public function setup_email_data() {
224
			// Set header.
225
			Give()->emails->__set(
226
				'headers',
227
				apply_filters(
228
					'give_offline_admin_donation_notification_headers',
229
					Give()->emails->get_headers(),
230
					$this->payment->ID
231
				)
232
			);
233
		}
234
235
		/**
236
		 * Setup email notification.
237
		 *
238
		 * @since  2.0
239
		 * @access public
240
		 *
241
		 * @param int $payment_id
242
		 */
243 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...
244
			$this->payment = new Give_Payment( $payment_id );
245
246
			// Exit if not donation was not with offline donation.
247
			if ( 'offline' !== $this->payment->gateway ) {
248
				return;
249
			}
250
251
			// Set email data.
252
			$this->setup_email_data();
253
254
			// Send email.
255
			$this->send_email_notification( array(
256
				'payment_id' => $this->payment->ID,
257
			) );
258
		}
259
260
		/**
261
		 * Set notification status
262
		 *
263
		 * @since  2.0
264
		 * @access public
265
		 *
266
		 * @param $update_options
267
		 * @param $option_name
268
		 */
269 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...
270
			// Get updated settings.
271
			$update_options = give_get_settings();
272
273
			$notification_status = isset( $update_options['gateways']['offline'] ) ? 'enabled' : 'disabled';
274
275
			if (
276
				empty( $update_options[ "{$this->config['id']}_notification" ] )
277
				|| $notification_status !== $update_options[ "{$this->config['id']}_notification" ]
278
			) {
279
				$update_options[ "{$this->config['id']}_notification" ] = $notification_status;
280
				update_option( $option_name, $update_options );
281
			}
282
		}
283
	}
284
285
endif; // End class_exists check
286
287
return Give_New_Offline_Donation_Email::get_instance();
288