Test Failed
Pull Request — master (#2668)
by Devin
07:48
created

Give_Email_Setting_Field   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 353
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
B get_setting_fields() 0 28 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 5
B get_notification_status_field() 0 31 5
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
B get_recipient_setting_field() 0 33 3
A get_preview_setting_field() 0 9 1
A get_prefix() 0 9 3
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 = null ) {
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
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
32
33
		// Add extra setting field.
34
		if ( $extra_setting_field = $email->get_extra_setting_fields( $form_id ) ) {
35
			$setting_fields = array_merge( $setting_fields, $extra_setting_field );
36
		}
37
38
		// Preview field.
39
		if ( Give_Email_Notification_Util::has_preview( $email ) ) {
40
			$setting_fields[] = self::get_preview_setting_field( $email, $form_id );
41
		}
42
43
		$setting_fields = self::add_section_end( $email, $setting_fields );
44
45
		/**
46
		 * Filter the email notification settings.
47
		 *
48
		 * @since 2.0
49
		 */
50
		return apply_filters( 'give_email_notification_setting_fields', $setting_fields, $email, $form_id );
51
	}
52
53
54
	/**
55
	 * Check if email notification setting has section end or not.
56
	 *
57
	 * @since  2.0
58
	 * @access private
59
	 *
60
	 * @param $setting
61
	 *
62
	 * @return bool
63
	 */
64
	public static function has_section_end( $setting ) {
65
		$last_field      = end( $setting );
66
		$has_section_end = false;
67
68
		if ( 'sectionend' === $last_field['type'] ) {
69
			$has_section_end = true;
70
		}
71
72
		return $has_section_end;
73
	}
74
75
	/**
76
	 * Check if email notification setting has section end or not.
77
	 *
78
	 * @since  2.0
79
	 * @access private
80
	 *
81
	 * @param Give_Email_Notification $email
82
	 * @param int                     $form_id
83
	 *
84
	 * @return array
85
	 */
86
	public static function get_section_start( Give_Email_Notification $email, $form_id = null ) {
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...
87
		// Add section end field.
88
		$setting = array(
89
			'id'    => "give_title_email_settings_{$email->config['id']}",
90
			'type'  => 'title',
91
			'title' => $email->config['label'],
92
		);
93
94
		return $setting;
95
	}
96
97
	/**
98
	 * Check if email notification setting has section end or not.
99
	 *
100
	 * @since  2.0
101
	 * @access private
102
	 *
103
	 * @param array                   $setting
104
	 * @param Give_Email_Notification $email
105
	 *
106
	 * @return array
107
	 */
108
	public static function add_section_end( Give_Email_Notification $email, $setting ) {
109
		if ( ! self::has_section_end( $setting ) ) {
110
			// Add section end field.
111
			$setting[] = array(
112
				'id'   => "give_title_email_settings_{$email->config['id']}",
113
				'type' => 'sectionend',
114
			);
115
		}
116
117
		return $setting;
118
	}
119
120
	/**
121
	 * Get default setting field.
122
	 *
123
	 * @since  2.0
124
	 * @access static
125
	 *
126
	 * @param Give_Email_Notification $email
127
	 * @param int                     $form_id
128
	 *
129
	 * @return array
130
	 */
131
	public static function get_default_setting_fields( Give_Email_Notification $email, $form_id = null ) {
132
		$settings[] = self::get_section_start( $email, $form_id );
133
		$settings[] = self::get_notification_status_field( $email, $form_id );
134
135
		if ( ! Give_Email_Notification_Util::is_notification_status_editable( $email ) ) {
136
			if( $form_id || give_is_add_new_form_page() ){
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
137
				// Do not allow admin to disable notification on perform basis.
138
				unset( $settings[1]['options']['disabled'] );
139
			} else{
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...
140
				// Do not allow admin to edit notification status globally.
141
				unset( $settings[1] );
142
			}
143
		}
144
145
		$settings[] = self::get_email_subject_field( $email, $form_id );
146
		$settings[] = self::get_email_message_field( $email, $form_id );
147
148
		if( Give_Email_Notification_Util::is_content_type_editable( $email ) ) {
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...
149
			$settings[] = self::get_email_content_type_field( $email, $form_id );
150
		}
151
152
		return $settings;
153
	}
154
155
	/**
156
	 * Get notification status setting field.
157
	 *
158
	 * @since  2.0
159
	 * @access static
160
	 *
161
	 * @param Give_Email_Notification $email
162
	 * @param int                     $form_id
163
	 *
164
	 * @return array
165
	 */
166
	public static function get_notification_status_field( Give_Email_Notification $email, $form_id = null ) {
167
		$option = array(
168
			'enabled'  => __( 'Enabled', 'give' ),
169
			'disabled' => __( 'Disabled', 'give' ),
170
		);
171
172
		$default_value = $email->get_notification_status();
173
174
		// Add global options.
175
		if ( $form_id || give_is_add_new_form_page() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
176
			$option = array(
177
				'global'   => __( 'Global Options' ),
178
				'enabled'  => __( 'Customize', 'give' ),
179
				'disabled' => __( 'Disabled', 'give' ),
180
			);
181
182
			$default_value = 'global';
183
		}
184
185
		$description = isset($_GET['page']) && 'give-settings' === $_GET['page'] ? __('Choose whether you want this email enabled or not.', 'give') : sprintf( __( 'Global Options are set <a href="%s">in Give settings</a>. You may override them for this form here.', 'give' ), admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails' ) );
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...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
186
187
		return array(
188
			'name'          => esc_html__( 'Notification', 'give' ),
189
			'desc'          => $description,
190
			'id'            => self::get_prefix( $email, $form_id ) . 'notification',
191
			'type'          => 'radio_inline',
192
			'default'       => $default_value,
193
			'options'       => $option,
194
			'wrapper_class' => 'give_email_api_notification_status_setting',
195
		);
196
	}
197
198
	/**
199
	 * Get email subject setting field.
200
	 *
201
	 * @since  2.0
202
	 * @access static
203
	 *
204
	 * @param Give_Email_Notification $email
205
	 * @param int                     $form_id
206
	 *
207
	 * @return array
208
	 */
209
	public static function get_email_subject_field( Give_Email_Notification $email, $form_id = null ) {
210
		return array(
211
			'id'      => self::get_prefix( $email, $form_id ) . 'email_subject',
212
			'name'    => esc_html__( 'Email Subject', 'give' ),
213
			'desc'    => esc_html__( 'Enter the email subject line.', 'give' ),
214
			'default' => $email->config['default_email_subject'],
215
			'type'    => 'text',
216
		);
217
	}
218
219
	/**
220
	 * Get email message setting field.
221
	 *
222
	 * @since  2.0
223
	 * @access static
224
	 *
225
	 * @param Give_Email_Notification $email
226
	 * @param int                     $form_id
227
	 *
228
	 * @return array
229
	 */
230
	public static function get_email_message_field( Give_Email_Notification $email, $form_id = null ) {
231
		$desc = esc_html__( 'Enter the email message.', 'give' );
232
233
		if ( $email_tag_list = $email->get_allowed_email_tags( true ) ) {
234
			$desc = sprintf(
235
				esc_html__( 'The email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
236
				$email_tag_list
237
			);
238
239
		}
240
241
		return array(
242
			'id'      => self::get_prefix( $email, $form_id ) . 'email_message',
243
			'name'    => esc_html__( 'Email message', 'give' ),
244
			'desc'    => $desc,
245
			'type'    => 'wysiwyg',
246
			'default' => $email->config['default_email_message'],
247
		);
248
	}
249
250
	/**
251
	 * Get email message setting field.
252
	 *
253
	 * @since  2.0
254
	 * @access static
255
	 *
256
	 * @param Give_Email_Notification $email
257
	 * @param int                     $form_id
258
	 *
259
	 * @return array
260
	 */
261
	public static function get_email_content_type_field( Give_Email_Notification $email, $form_id = null ) {
262
		return array(
263
			'id'      => self::get_prefix( $email, $form_id ) . 'email_content_type',
264
			'name'    => esc_html__( 'Email Content Type', 'give' ),
265
			'desc'    => __( 'Choose email type.', 'give' ),
266
			'type'    => 'select',
267
			'options' => array(
268
				'text/html'  => Give_Email_Notification_Util::get_formatted_email_type( 'text/html' ),
269
				'text/plain' => Give_Email_Notification_Util::get_formatted_email_type( 'text/plain' ),
270
			),
271
			'default' => $email->config['content_type'],
272
		);
273
	}
274
275
276
	/**
277
	 * Get recipient setting field.
278
	 *
279
	 * @since  2.0
280
	 * @access static
281
	 * @todo check this field in form metabox setting after form api merge.
282
	 *
283
	 * @param Give_Email_Notification $email
284
	 * @param int                     $form_id
285
	 *
286
	 * @return array
287
	 */
288
	public static function get_recipient_setting_field( Give_Email_Notification $email, $form_id = null ) {
289
		$recipient =  array(
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
290
			'id'               => self::get_prefix( $email, $form_id ) . 'recipient',
291
			'name'             => esc_html__( 'Email Recipients', 'give' ),
292
			'desc'             => __( 'Enter the email address(es) that should receive a notification.', 'give' ),
293
			'type'             => 'email',
294
			'default'          => get_bloginfo( 'admin_email' ),
295
			'repeat'           => true,
296
			'repeat_btn_title' => esc_html__( 'Add Recipient', 'give' ),
297
		);
298
299
		if ( $form_id || give_is_add_new_form_page() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
300
			$recipient['name']    = __( 'Email', 'give' );
301
			$recipient['default'] = '';
302
			$recipient['id']      = 'email';
303
			$recipient['desc']    = __( 'Enter the email address that should receive a notification.', 'give' );
304
305
			$recipient = array(
306
				'id'      => self::get_prefix( $email, $form_id ) . 'recipient',
307
				'type'    => 'group',
308
				'options' => array(
309
					'add_button'    => __( 'Add Email', 'give' ),
310
					'header_title'  => __( 'Email Recipient', 'give' ),
311
					'remove_button' => '<span class="dashicons dashicons-no"></span>',
312
				),
313
				'fields'  => array(
314
					$recipient,
315
				),
316
			);
317
		}
318
319
		return $recipient;
320
	}
321
322
	/**
323
	 * Get preview setting field.
324
	 *
325
	 * @param Give_Email_Notification $email   Email Type.
326
	 * @param int                     $form_id Form ID.
327
	 *
328
	 * @since  2.0
329
	 * @access static
330
	 *
331
	 * @return array
332
	 */
333
	public static function get_preview_setting_field( Give_Email_Notification $email, $form_id = null ) {
334
		return array(
335
			'name' => __( 'Preview Email', 'give' ),
336
			'desc' => __( 'Click the "Preview Email" button to preview the email in your browser. Click the "Send Test Email" button to send a test email directly to your inbox.',
337
				'give' ),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
338
			'id'   => self::get_prefix( $email, $form_id ) . 'preview_buttons',
339
			'type' => 'email_preview_buttons',
340
		);
341
	}
342
343
344
	/**
345
	 * Get form metabox setting field prefix.
346
	 *
347
	 * @since  2.0
348
	 * @access static
349
	 *
350
	 * @param Give_Email_Notification $email
351
	 * @param int                     $form_id
352
	 *
353
	 * @return string
354
	 */
355
	public static function get_prefix( Give_Email_Notification $email, $form_id = null  ) {
356
		$meta_key = "{$email->config['id']}_";
357
358
		if( $form_id || give_is_add_new_form_page() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
359
			$meta_key = "_give_{$email->config['id']}_";
360
		}
361
362
		return $meta_key;
363
	}
364
}
365
366
// @todo: add per email sender options
367