Completed
Push — issues/611 ( 85f494...0354ff )
by Ravinder
66:55 queued 46:51
created

Give_Email_Setting_Field   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 305
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
B get_setting_fields() 0 27 4
A has_section_end() 0 10 2
A get_section_start() 0 10 1
A add_section_end() 0 11 2
B get_default_setting_fields() 0 23 4
B get_notification_status_field() 0 29 2
A get_email_subject_field() 0 9 1
A get_email_message_field() 0 19 2
A get_email_content_type_field() 0 13 1
A get_recipient_setting_field() 0 11 1
A get_preview_setting_field() 0 8 1
1
<?php
2
3
/**
4
 * Email Notification Setting Fields
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
class Give_Email_Setting_Field {
13
	/**
14
	 * Get setting field.
15
	 *
16
	 * @since  2.0
17
	 * @access public
18
	 *
19
	 * @param Give_Email_Notification $email
20
	 * @param int                     $form_id
21
	 *
22
	 * @return array
23
	 */
24
	public static function get_setting_fields( Give_Email_Notification $email, $form_id = 0 ) {
25
		$setting_fields = self::get_default_setting_fields( $email, $form_id );
26
27
		// Recipient field.
28
		if ( Give_Email_Notification_Util::has_recipient_field( $email ) ) {
29
			$setting_fields[] = self::get_recipient_setting_field( $email, $form_id );
30
		}
31
32
		// Preview field.
33
		if ( Give_Email_Notification_Util::has_preview( $email ) ) {
34
			$setting_fields[] = self::get_preview_setting_field( $email, $form_id );
35
		}
36
37
		// Add extra setting field.
38
		if ( $extra_setting_field = $email->get_extra_setting_fields( $form_id ) ) {
39
			$setting_fields = array_merge( $setting_fields, $extra_setting_field );
40
		}
41
42
		$setting_fields = self::add_section_end( $email, $setting_fields );
43
44
		/**
45
		 * Filter the email notification settings.
46
		 *
47
		 * @since 2.0
48
		 */
49
		return apply_filters( 'give_email_notification_setting_fields', $setting_fields, $email, $form_id );
50
	}
51
52
53
	/**
54
	 * Check if email notification setting has section end or not.
55
	 *
56
	 * @since  2.0
57
	 * @access private
58
	 *
59
	 * @param $setting
60
	 *
61
	 * @return bool
62
	 */
63
	public static function has_section_end( $setting ) {
64
		$last_field      = end( $setting );
65
		$has_section_end = false;
66
67
		if ( 'sectionend' === $last_field['type'] ) {
68
			$has_section_end = true;
69
		}
70
71
		return $has_section_end;
72
	}
73
74
	/**
75
	 * Check if email notification setting has section end or not.
76
	 *
77
	 * @since  2.0
78
	 * @access private
79
	 *
80
	 * @param Give_Email_Notification $email
81
	 * @param int                     $form_id
82
	 *
83
	 * @return array
84
	 */
85
	public static function get_section_start( Give_Email_Notification $email, $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
		// Add section end field.
87
		$setting = array(
88
			'id'    => "give_title_email_settings_{$email->config['id']}",
89
			'type'  => 'title',
90
			'title' => $email->config['label'],
91
		);
92
93
		return $setting;
94
	}
95
96
	/**
97
	 * Check if email notification setting has section end or not.
98
	 *
99
	 * @since  2.0
100
	 * @access private
101
	 *
102
	 * @param array                   $setting
103
	 * @param Give_Email_Notification $email
104
	 *
105
	 * @return array
106
	 */
107
	public static function add_section_end( Give_Email_Notification $email, $setting ) {
108
		if ( ! self::has_section_end( $setting ) ) {
109
			// Add section end field.
110
			$setting[] = array(
111
				'id'   => "give_title_email_settings_{$email->config['id']}",
112
				'type' => 'sectionend',
113
			);
114
		}
115
116
		return $setting;
117
	}
118
119
	/**
120
	 * Get default setting field.
121
	 *
122
	 * @since  2.0
123
	 * @access static
124
	 *
125
	 * @param Give_Email_Notification $email
126
	 * @param int                     $form_id
127
	 *
128
	 * @return array
129
	 */
130
	public static function get_default_setting_fields( Give_Email_Notification $email, $form_id = 0 ) {
131
		$settings[] = self::get_section_start( $email, $form_id );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $settings = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
132
		$settings[] = self::get_notification_status_field( $email, $form_id );
133
134
		if ( ! Give_Email_Notification_Util::is_notification_status_editable( $email ) ) {
135
			if( $form_id ){
136
				// Do not allow admin to disable notification on perform basis.
137
				unset( $settings[1]['options']['disabled'] );
138
			} else{
139
				// Do not allow admin to edit notification status globally.
140
				unset( $settings[1] );
141
			}
142
		}
143
144
		$settings[] = self::get_email_subject_field( $email, $form_id );
145
		$settings[] = self::get_email_message_field( $email, $form_id );
146
147
		if( Give_Email_Notification_Util::is_content_type_editable( $email ) ) {
148
			$settings[] = self::get_email_content_type_field( $email, $form_id );
149
		}
150
151
		return $settings;
152
	}
153
154
	/**
155
	 * Get notification status setting field.
156
	 *
157
	 * @since  2.0
158
	 * @access static
159
	 *
160
	 * @param Give_Email_Notification $email
161
	 * @param int                     $form_id
162
	 *
163
	 * @return array
164
	 */
165
	public static function get_notification_status_field( Give_Email_Notification $email, $form_id = 0 ) {
166
		$option = array(
167
			'enabled'  => __( 'Enabled', 'give' ),
168
			'disabled' => __( 'Disabled', 'give' ),
169
		);
170
171
		$default_value = $email->get_notification_status();
172
173
		// Remove global options.
174
		if ( $form_id ) {
175
			$option = array(
176
				'global'   => __( 'Global Options' ),
177
				'enabled'  => __( 'Customize', 'give' ),
178
				'disabled' => __( 'Disabled', 'give' ),
179
			);
180
181
			$default_value = 'global';
182
		}
183
184
		return array(
185
			'name'          => esc_html__( 'Notification', 'give' ),
186
			'desc'          => esc_html__( 'Choose option if you want to send email notification or not.', 'give' ),
187
			'id'            => "{$email->config['id']}_notification",
188
			'type'          => 'radio_inline',
189
			'default'       => $default_value,
190
			'options'       => $option,
191
			'wrapper_class' => 'give_email_api_notification_status_setting',
192
		);
193
	}
194
195
	/**
196
	 * Get email subject setting field.
197
	 *
198
	 * @since  2.0
199
	 * @access static
200
	 *
201
	 * @param Give_Email_Notification $email
202
	 * @param int                     $form_id
203
	 *
204
	 * @return array
205
	 */
206
	public static function get_email_subject_field( Give_Email_Notification $email, $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
		return array(
208
			'id'      => "{$email->config['id']}_email_subject",
209
			'name'    => esc_html__( 'Email Subject', 'give' ),
210
			'desc'    => esc_html__( 'Enter the subject line for email.', 'give' ),
211
			'default' => $email->config['default_email_subject'],
212
			'type'    => 'text',
213
		);
214
	}
215
216
	/**
217
	 * Get email message setting field.
218
	 *
219
	 * @since  2.0
220
	 * @access static
221
	 *
222
	 * @param Give_Email_Notification $email
223
	 * @param int                     $form_id
224
	 *
225
	 * @return array
226
	 */
227
	public static function get_email_message_field( Give_Email_Notification $email, $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
		$desc = esc_html__( 'Enter the email message.', 'give' );
229
230
		if ( $email_tag_list = $email->get_allowed_email_tags( true ) ) {
231
			$desc = sprintf(
232
				esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
233
				$email_tag_list
234
			);
235
236
		}
237
238
		return array(
239
			'id'      => "{$email->config['id']}_email_message",
240
			'name'    => esc_html__( 'Email message', 'give' ),
241
			'desc'    => $desc,
242
			'type'    => 'wysiwyg',
243
			'default' => $email->config['default_email_message'],
244
		);
245
	}
246
247
	/**
248
	 * Get email message setting field.
249
	 *
250
	 * @since  2.0
251
	 * @access static
252
	 *
253
	 * @param Give_Email_Notification $email
254
	 * @param int                     $form_id
255
	 *
256
	 * @return array
257
	 */
258
	public static function get_email_content_type_field( Give_Email_Notification $email, $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
259
		return array(
260
			'id'      => "{$email->config['id']}_email_content_type",
261
			'name'    => esc_html__( 'Email Content Type', 'give' ),
262
			'desc'    => __( 'Choose email content type.', 'give' ),
263
			'type'    => 'select',
264
			'options' => array(
265
				'text/html'  => Give_Email_Notification_Util::get_formatted_email_type( 'text/html' ),
266
				'text/plain' => Give_Email_Notification_Util::get_formatted_email_type( 'text/plain' ),
267
			),
268
			'default' => $email->config['content_type'],
269
		);
270
	}
271
272
273
	/**
274
	 * Get recipient setting field.
275
	 *
276
	 * @since  2.0
277
	 * @access static
278
	 * @todo check this field in form metabox setting after form api merge.
279
	 *
280
	 * @param Give_Email_Notification $email
281
	 * @param int                     $form_id
282
	 *
283
	 * @return array
284
	 */
285
	public static function get_recipient_setting_field( Give_Email_Notification $email, $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
286
		return array(
287
			'id'               => "{$email->config['id']}_recipient",
288
			'name'             => esc_html__( 'Email Recipients', 'give' ),
289
			'desc'             => __( 'Enter the email address(es) that should receive a notification anytime a donation is made.', 'give' ),
290
			'type'             => 'email',
291
			'default'          => get_bloginfo( 'admin_email' ),
292
			'repeat'           => true,
293
			'repeat_btn_title' => esc_html__( 'Add Recipient', 'give' ),
294
		);
295
	}
296
297
	/**
298
	 * Get preview setting field.
299
	 *
300
	 * @since  2.0
301
	 * @access static
302
	 *
303
	 * @param Give_Email_Notification $email
304
	 * @param int                     $form_id
305
	 *
306
	 * @return array
307
	 */
308
	public static function get_preview_setting_field( Give_Email_Notification $email, $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
309
		return array(
310
			'name' => esc_html__( 'Preview Email', 'give' ),
311
			'desc' => esc_html__( 'Click the buttons to preview emails.', 'give' ),
312
			'id'   => "{$email->config['id']}_preview_buttons",
313
			'type' => 'email_preview_buttons',
314
		);
315
	}
316
}
317
318
// @todo: add per email sender options
319