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

Give_Email_Access_Email   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 264
Duplicated Lines 19.7 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 52
loc 264
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 18 1
A get_email_attachments() 0 20 1
A get_default_email_message() 0 16 1
B setup_email_data() 0 30 1
A setup_email_notification() 0 15 1
A set_notification_status() 0 13 4
A email_preview_header() 0 7 2
B get_email_subject() 27 27 1
B get_email_message() 25 25 1

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
 * Email access notification
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_Email_Access_Email' ) ) :
19
20
	/**
21
	 * Give_Email_Access_Email
22
	 *
23
	 * @abstract
24
	 * @since       2.0
25
	 */
26
	class Give_Email_Access_Email extends Give_Email_Notification {
27
		/**
28
		 * Create a class instance.
29
		 *
30
		 * @access  public
31
		 * @since   2.0
32
		 */
33
		public function init() {
34
			$this->load( array(
35
				'id'                           => 'email-access',
36
				'label'                        => __( 'Email access', 'give' ),
37
				'description'                  => __( 'Email Access Notification will be sent to recipient(s) when want to access their donation history using only email.', 'give' ),
38
				'notification_status'          => give_get_option( 'email_access', 'disabled' ),
39
				'form_metabox_setting'         => false,
40
				'notification_status_editable' => false,
41
				'email_tag_context'            => 'donor',
42
				'recipient_group_name'         => __( 'Donor', 'give' ),
43
				'default_email_subject'        => sprintf( __( 'Your Access Link to %s', 'give' ), get_bloginfo( 'name' ) ),
44
				'default_email_message'        => $this->get_default_email_message(),
45
			) );
46
47
			add_action( "give_{$this->config['id']}_email_notification", array( $this, 'setup_email_notification' ), 10, 2 );
48
			add_action( 'give_save_settings_give_settings', array( $this, 'set_notification_status' ), 10, 2 );
49
			add_filter( 'give_email_preview_header', array( $this, 'email_preview_header' ), 10, 2 );
50
		}
51
52
53
		/**
54
		 * Get email subject.
55
		 *
56
		 * @since  2.0
57
		 * @access public
58
		 *
59
		 * @param int $form_id
60
		 *
61
		 * @return string
62
		 */
63 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...
64
			$subject = wp_strip_all_tags(
65
				Give_Email_Notification_Util::get_value(
66
					$this,
67
					Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject',
68
					$form_id,
69
					$this->config['default_email_subject']
70
				)
71
			);
72
73
			/**
74
			 * Filters the donation notification subject.
75
			 * Note: This filter will deprecate soon.
76
			 *
77
			 * @since 1.0
78
			 */
79
			$subject = apply_filters( 'give_email_access_token_subject', $subject );
80
81
			/**
82
			 * Filters the donation notification subject.
83
			 *
84
			 * @since 2.0
85
			 */
86
			$subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this, $form_id );
87
88
			return $subject;
89
		}
90
91
92
		/**
93
		 * Get email attachment.
94
		 *
95
		 * @since  2.0
96
		 * @access public
97
		 *
98
		 * @param int $form_id
99
		 *
100
		 * @return string
101
		 */
102 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...
103
			$message = Give_Email_Notification_Util::get_value(
104
				$this,
105
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
106
				$form_id,
107
				$this->config['default_email_message']
108
			);
109
110
			/**
111
			 * Filter the email message
112
			 * Note: This filter will deprecate soon.
113
			 *
114
			 * @since 1.0
115
			 */
116
			$message = apply_filters( 'give_email_access_token_message', $message );
117
118
			/**
119
			 * Filter the email message
120
			 *
121
			 * @since 2.0
122
			 */
123
			$message = apply_filters( "give_{$this->config['id']}_get_default_email_message", $message, $this, $form_id );
124
125
			return $message;
126
		}
127
128
129
		/**
130
		 * Get email attachment.
131
		 *
132
		 * @since  2.0
133
		 * @access public
134
		 *
135
		 * @param int $form_id
136
		 * @return array
137
		 */
138
		public function get_email_attachments( $form_id = null ) {
139
			/**
140
			 * Filters the donation notification email attachments.
141
			 * By default, there is no attachment but plugins can hook in to provide one more multiple.
142
			 * Note: This filter will deprecate soon.
143
			 *
144
			 * @since 1.0
145
			 */
146
			$attachments = apply_filters( 'give_admin_donation_notification_attachments', array() );
147
148
			/**
149
			 * Filters the donation notification email attachments.
150
			 * By default, there is no attachment but plugins can hook in to provide one more multiple.
151
			 *
152
			 * @since 2.0
153
			 */
154
			$attachments = apply_filters( "give_{$this->config['id']}_get_email_attachments", $attachments, $this, $form_id );
155
156
			return $attachments;
157
		}
158
159
160
		/**
161
		 * Get default email message.
162
		 *
163
		 * @since  2.0
164
		 * @access public
165
		 *
166
		 * @return string
167
		 */
168
		public function get_default_email_message() {
169
			$message = __( 'You or someone in your organization requested an access link be sent to this email address. This is a temporary access link for you to view your donation information. Click on the link below to view:', 'give' ) . "\n\n";
170
			$message .= '{email_access_link}' . "\n\n";
171
			$message .= "\n\n";
172
			$message .= __( 'Sincerely,', 'give' ) . "\n";
173
			$message .= get_bloginfo( 'name' ) . "\n";
174
175
			/**
176
			 * Filter the new donation email message
177
			 *
178
			 * @since 2.0
179
			 *
180
			 * @param string $message
181
			 */
182
			return apply_filters( "give_{$this->config['id']}_get_default_email_message", $message, $this );
183
		}
184
185
186
		/**
187
		 * Set email data
188
		 *
189
		 * @since 2.0
190
		 */
191
		public function setup_email_data() {
192
			/**
193
			 * Filters the from name.
194
			 * Note: This filter will deprecate soon.
195
			 *
196
			 * @since 1.0
197
			 */
198
			$from_name = apply_filters( 'give_donation_from_name', Give()->emails->get_from_name() );
199
200
			/**
201
			 * Filters the from email.
202
			 * Note: This filter will deprecate soon.
203
			 *
204
			 * @since 1.0
205
			 */
206
			$from_email = apply_filters( 'give_donation_from_address', Give()->emails->get_from_address() );
207
208
			Give()->emails->__set( 'from_name', $from_name );
209
			Give()->emails->__set( 'from_email', $from_email );
210
			Give()->emails->__set( 'heading', apply_filters( 'give_email_access_token_heading', __( 'Your Access Link', 'give' ) ) );
211
212
			/**
213
			 * Filters the donation notification email headers.
214
			 *
215
			 * @since 1.0
216
			 */
217
			$headers = apply_filters( 'give_admin_donation_notification_headers', Give()->emails->get_headers() );
218
219
			Give()->emails->__set( 'headers', $headers );
220
		}
221
222
		/**
223
		 * Setup email notification.
224
		 *
225
		 * @since  2.0
226
		 * @access public
227
		 *
228
		 * @param int    $donor_id
229
		 * @param string $email
230
		 */
231
		public function setup_email_notification( $donor_id, $email ) {
232
			$donor = Give()->customers->get_by( 'id', $donor_id );
233
			$this->recipient_email = $email;
234
235
			// Set email data.
236
			$this->setup_email_data();
237
238
			// Send email.
239
			$this->send_email_notification(
240
				array(
241
					'donor_id' => $donor_id,
242
					'user_id'  => $donor->user_id
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
243
				)
244
			);
245
		}
246
247
248
		/**
249
		 * Set notification status
250
		 *
251
		 * @since  2.0
252
		 * @access public
253
		 *
254
		 * @param $update_options
255
		 * @param $option_name
256
		 */
257
		public function set_notification_status( $update_options, $option_name ) {
258
			// Get updated settings.
259
			$update_options = give_get_settings();
260
261
			if (
262
				! empty( $update_options['email_access'] )
263
				&& ! empty( $update_options[ "{$this->config['id']}_notification" ] )
264
				&& $update_options['email_access'] !== $update_options[ "{$this->config['id']}_notification" ]
265
			) {
266
				$update_options[ "{$this->config['id']}_notification" ] = $update_options['email_access'];
267
				update_option( $option_name, $update_options );
268
			}
269
		}
270
271
272
		/**
273
		 * email preview header.
274
		 *
275
		 * @since  2.0
276
		 * @access public
277
		 *
278
		 * @param string                  $email_preview_header
279
		 * @param Give_Email_Access_Email $email
280
		 * @return string
281
		 */
282
		public function email_preview_header( $email_preview_header, $email ) {
283
			if( $this->config['id'] === $email->config['id'] ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
284
				$email_preview_header = '';
285
			}
286
287
			return $email_preview_header;
288
		}
289
	}
290
291
endif; // End class_exists check
292
293
return Give_Email_Access_Email::get_instance();
294