Completed
Push — release/2.0 ( 9c875b...4d845f )
by Ravinder
19:03
created

Give_Email_Setting_Field   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 328
rs 10
c 0
b 0
f 0
wmc 26
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 29 3
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
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
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 );
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...
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...
137
				// Do not allow admin to disable notification on perform basis.
138
				unset( $settings[1]['options']['disabled'] );
139
			} else{
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 ) ) {
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
		return array(
186
			'name'          => esc_html__( 'Notification', 'give' ),
187
			'desc'          => esc_html__( 'Choose option if you want to send email notification or not.', 'give' ),
188
			'id'            => self::get_prefix( $email, $form_id ) . 'notification',
189
			'type'          => 'radio_inline',
190
			'default'       => $default_value,
191
			'options'       => $option,
192
			'wrapper_class' => 'give_email_api_notification_status_setting',
193
		);
194
	}
195
196
	/**
197
	 * Get email subject setting field.
198
	 *
199
	 * @since  2.0
200
	 * @access static
201
	 *
202
	 * @param Give_Email_Notification $email
203
	 * @param int                     $form_id
204
	 *
205
	 * @return array
206
	 */
207
	public static function get_email_subject_field( Give_Email_Notification $email, $form_id = null ) {
208
		return array(
209
			'id'      => self::get_prefix( $email, $form_id ) . 'email_subject',
210
			'name'    => esc_html__( 'Email Subject', 'give' ),
211
			'desc'    => esc_html__( 'Enter the subject line for email.', 'give' ),
212
			'default' => $email->config['default_email_subject'],
213
			'type'    => 'text',
214
		);
215
	}
216
217
	/**
218
	 * Get email message setting field.
219
	 *
220
	 * @since  2.0
221
	 * @access static
222
	 *
223
	 * @param Give_Email_Notification $email
224
	 * @param int                     $form_id
225
	 *
226
	 * @return array
227
	 */
228
	public static function get_email_message_field( Give_Email_Notification $email, $form_id = null ) {
229
		$desc = esc_html__( 'Enter the email message.', 'give' );
230
231
		if ( $email_tag_list = $email->get_allowed_email_tags( true ) ) {
232
			$desc = sprintf(
233
				esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
234
				$email_tag_list
235
			);
236
237
		}
238
239
		return array(
240
			'id'      => self::get_prefix( $email, $form_id ) . 'email_message',
241
			'name'    => esc_html__( 'Email message', 'give' ),
242
			'desc'    => $desc,
243
			'type'    => 'wysiwyg',
244
			'default' => $email->config['default_email_message'],
245
		);
246
	}
247
248
	/**
249
	 * Get email message setting field.
250
	 *
251
	 * @since  2.0
252
	 * @access static
253
	 *
254
	 * @param Give_Email_Notification $email
255
	 * @param int                     $form_id
256
	 *
257
	 * @return array
258
	 */
259
	public static function get_email_content_type_field( Give_Email_Notification $email, $form_id = null ) {
260
		return array(
261
			'id'      => self::get_prefix( $email, $form_id ) . 'email_content_type',
262
			'name'    => esc_html__( 'Email Content Type', 'give' ),
263
			'desc'    => __( 'Choose email content type.', 'give' ),
264
			'type'    => 'select',
265
			'options' => array(
266
				'text/html'  => Give_Email_Notification_Util::get_formatted_email_type( 'text/html' ),
267
				'text/plain' => Give_Email_Notification_Util::get_formatted_email_type( 'text/plain' ),
268
			),
269
			'default' => $email->config['content_type'],
270
		);
271
	}
272
273
274
	/**
275
	 * Get recipient setting field.
276
	 *
277
	 * @since  2.0
278
	 * @access static
279
	 * @todo check this field in form metabox setting after form api merge.
280
	 *
281
	 * @param Give_Email_Notification $email
282
	 * @param int                     $form_id
283
	 *
284
	 * @return array
285
	 */
286
	public static function get_recipient_setting_field( Give_Email_Notification $email, $form_id = null ) {
287
		return array(
288
			'id'               => self::get_prefix( $email, $form_id ) . 'recipient',
289
			'name'             => esc_html__( 'Email Recipients', 'give' ),
290
			'desc'             => __( 'Enter the email address(es) that should receive a notification anytime a donation is made.', 'give' ),
291
			'type'             => 'email',
292
			'default'          => get_bloginfo( 'admin_email' ),
293
			'repeat'           => true,
294
			'repeat_btn_title' => esc_html__( 'Add Recipient', 'give' ),
295
		);
296
	}
297
298
	/**
299
	 * Get preview setting field.
300
	 *
301
	 * @since  2.0
302
	 * @access static
303
	 *
304
	 * @param Give_Email_Notification $email
305
	 * @param int                     $form_id
306
	 *
307
	 * @return array
308
	 */
309
	public static function get_preview_setting_field( Give_Email_Notification $email, $form_id = null ) {
310
		return array(
311
			'name' => esc_html__( 'Preview Email', 'give' ),
312
			'desc' => esc_html__( 'Click the buttons to preview emails.', 'give' ),
313
			'id'   => self::get_prefix( $email, $form_id ) . 'preview_buttons',
314
			'type' => 'email_preview_buttons',
315
		);
316
	}
317
318
319
	/**
320
	 * Get form metabox setting field prefix.
321
	 *
322
	 * @since  2.0
323
	 * @access static
324
	 *
325
	 * @param Give_Email_Notification $email
326
	 * @param int                     $form_id
327
	 *
328
	 * @return string
329
	 */
330
	public static function get_prefix( Give_Email_Notification $email, $form_id = null  ) {
331
		$meta_key = "{$email->config['id']}_";
332
333
		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...
334
			$meta_key = "_give_{$email->config['id']}_";
335
		}
336
337
		return $meta_key;
338
	}
339
}
340
341
// @todo: add per email sender options
342