get_default_setting_fields()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Email Notification Setting Fields
5
 *
6
 * @package     Give
7
 * @subpackage  Classes/Emails
8
 * @copyright   Copyright (c) 2016, GiveWP
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
		$setting_fields[] = self::get_recipient_setting_field( $email, $form_id, Give_Email_Notification_Util::has_recipient_field( $email ) );
29
30
		// Add extra setting field.
31
		if ( $extra_setting_field = $email->get_extra_setting_fields( $form_id ) ) {
32
			$setting_fields = array_merge( $setting_fields, $extra_setting_field );
33
		}
34
35
		// Preview field.
36
		if ( Give_Email_Notification_Util::has_preview( $email ) ) {
37
			$setting_fields[] = self::get_preview_setting_field( $email, $form_id );
38
		}
39
40
		$setting_fields = self::add_section_end( $email, $setting_fields );
41
42
		/**
43
		 * Filter the email notification settings.
44
		 *
45
		 * @since 2.0
46
		 */
47
		return apply_filters( 'give_email_notification_setting_fields', $setting_fields, $email, $form_id );
48
	}
49
50
51
	/**
52
	 * Check if email notification setting has section end or not.
53
	 *
54
	 * @since  2.0
55
	 * @access private
56
	 *
57
	 * @param $setting
58
	 *
59
	 * @return bool
60
	 */
61
	public static function has_section_end( $setting ) {
62
		$last_field      = end( $setting );
63
		$has_section_end = false;
64
65
		if ( 'sectionend' === $last_field['type'] ) {
66
			$has_section_end = true;
67
		}
68
69
		return $has_section_end;
70
	}
71
72
	/**
73
	 * Check if email notification setting has section end or not.
74
	 *
75
	 * @since  2.0
76
	 * @access private
77
	 *
78
	 * @param Give_Email_Notification $email
79
	 * @param int                     $form_id
80
	 *
81
	 * @return array
82
	 */
83
	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...
84
		// Add section end field.
85
		$setting = array(
86
			'id'    => "give_title_email_settings_{$email->config['id']}",
87
			'type'  => 'title',
88
			'title' => $email->config['label'],
89
		);
90
91
		return $setting;
92
	}
93
94
	/**
95
	 * Check if email notification setting has section end or not.
96
	 *
97
	 * @since  2.0
98
	 * @access private
99
	 *
100
	 * @param array                   $setting
101
	 * @param Give_Email_Notification $email
102
	 *
103
	 * @return array
104
	 */
105
	public static function add_section_end( Give_Email_Notification $email, $setting ) {
106
		if ( ! self::has_section_end( $setting ) ) {
107
			// Add section end field.
108
			$setting[] = array(
109
				'id'   => "give_title_email_settings_{$email->config['id']}",
110
				'type' => 'sectionend',
111
			);
112
		}
113
114
		return $setting;
115
	}
116
117
	/**
118
	 * Get default setting field.
119
	 *
120
	 * @since  2.0
121
	 * @access static
122
	 *
123
	 * @param Give_Email_Notification $email
124
	 * @param int                     $form_id
125
	 *
126
	 * @return array
127
	 */
128
	public static function get_default_setting_fields( Give_Email_Notification $email, $form_id = null ) {
129
		$settings[] = self::get_section_start( $email, $form_id );
130
		$settings[] = self::get_notification_status_field( $email, $form_id );
131
132
		if ( ! Give_Email_Notification_Util::is_notification_status_editable( $email ) ) {
133
			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...
134
				// Do not allow admin to disable notification on perform basis.
135
				unset( $settings[1]['options']['disabled'] );
136
			} else {
137
				// Do not allow admin to edit notification status globally.
138
				unset( $settings[1] );
139
			}
140
		}
141
142
		$settings[] = self::get_email_subject_field( $email, $form_id );
143
		$settings[] = self::get_email_header_field( $email, $form_id );
144
		$settings[] = self::get_email_message_field( $email, $form_id );
145
146
		if ( Give_Email_Notification_Util::is_content_type_editable( $email ) ) {
147
			$settings[] = self::get_email_content_type_field( $email, $form_id );
148
		}
149
150
		return $settings;
151
	}
152
153
	/**
154
	 * Get notification status setting field.
155
	 *
156
	 * @since  2.0
157
	 * @access static
158
	 *
159
	 * @param Give_Email_Notification $email
160
	 * @param int                     $form_id
161
	 *
162
	 * @return array
163
	 */
164
	public static function get_notification_status_field( Give_Email_Notification $email, $form_id = null ) {
165
		$option = array(
166
			'enabled'  => __( 'Enabled', 'give' ),
167
			'disabled' => __( 'Disabled', 'give' ),
168
		);
169
170
		$default_value = $email->get_notification_status();
171
172
		// Add global options.
173
		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...
174
			$option = array(
175
				'global'   => __( 'Global Options' ),
176
				'enabled'  => __( 'Customize', 'give' ),
177
				'disabled' => __( 'Disabled', 'give' ),
178
			);
179
180
			$default_value = 'global';
181
		}
182
183
		$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
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...
184
185
		return array(
186
			'name'          => esc_html__( 'Notification', 'give' ),
187
			'desc'          => $description,
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 View Code Duplication
	public static function get_email_subject_field( Give_Email_Notification $email, $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...
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 email subject line.', 'give' ),
212
			'default' => $email->config['default_email_subject'],
213
			'type'    => 'text',
214
		);
215
	}
216
217
	/**
218
	 * Get email header setting field.
219
	 *
220
	 * @since  2.1.3
221
	 *
222
	 * @param Give_Email_Notification $email   The email object.
223
	 * @param int                     $form_id The Form ID.
224
	 *
225
	 * @return array
226
	 */
227 View Code Duplication
	public static function get_email_header_field( Give_Email_Notification $email, $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...
228
		return array(
229
			'id'      => self::get_prefix( $email, $form_id ) . 'email_header',
230
			'name'    => esc_html__( 'Email Header', 'give' ),
231
			'desc'    => esc_html__( 'Enter the email header that appears at the top of the email.', 'give' ),
232
			'default' => $email->config['default_email_header'],
233
			'type'    => 'text',
234
		);
235
	}
236
237
	/**
238
	 * Get email message setting field.
239
	 *
240
	 * @since  2.0
241
	 * @access static
242
	 *
243
	 * @param Give_Email_Notification $email
244
	 * @param int                     $form_id
245
	 *
246
	 * @return array
247
	 */
248
	public static function get_email_message_field( Give_Email_Notification $email, $form_id = null ) {
249
		$desc = esc_html__( 'Enter the email message.', 'give' );
250
251
		if ( $email_tag_list = $email->get_allowed_email_tags( true ) ) {
252
			$desc = sprintf(
253
				'%1$s <br> %2$s: %3$s %4$s',
254
				__( 'The email that is sent to users after completing a successful donation. HTML is accepted.', 'give' ),
255
				__( 'Available template tags', 'give' ),
256
				$email_tag_list,
257
				sprintf(
258
					'<br><a href="%1$s" target="_blank">%2$s</a> %3$s',
259
					esc_url( 'http://docs.givewp.com/meta-email-tags' ),
260
					__( 'See our documentation', 'give' ),
261
					__( 'for examples of how to use custom meta email tags to output additional donor or donation information in your Give emails.', 'give' )
262
				)
263
			);
264
265
		}
266
267
		return array(
268
			'id'      => self::get_prefix( $email, $form_id ) . 'email_message',
269
			'name'    => esc_html__( 'Email message', 'give' ),
270
			'desc'    => $desc,
271
			'type'    => 'wysiwyg',
272
			'default' => $email->config['default_email_message'],
273
		);
274
	}
275
276
	/**
277
	 * Get email message setting field.
278
	 *
279
	 * @since  2.0
280
	 * @access static
281
	 *
282
	 * @param Give_Email_Notification $email
283
	 * @param int                     $form_id
284
	 *
285
	 * @return array
286
	 */
287
	public static function get_email_content_type_field( Give_Email_Notification $email, $form_id = null ) {
288
		return array(
289
			'id'      => self::get_prefix( $email, $form_id ) . 'email_content_type',
290
			'name'    => esc_html__( 'Email Content Type', 'give' ),
291
			'desc'    => __( 'Choose email type.', 'give' ),
292
			'type'    => 'select',
293
			'options' => array(
294
				'text/html'  => Give_Email_Notification_Util::get_formatted_email_type( 'text/html' ),
295
				'text/plain' => Give_Email_Notification_Util::get_formatted_email_type( 'text/plain' ),
296
			),
297
			'default' => $email->config['content_type'],
298
		);
299
	}
300
301
302
	/**
303
	 * Get recipient setting field.
304
	 *
305
	 * @since  2.0
306
	 * @access public
307
	 * @todo   check this field in form metabox setting after form api merge.
308
	 *
309
	 * @param Give_Email_Notification $email
310
	 * @param int                     $form_id
311
	 * @param bool                    $edit_recipient
312
	 *
313
	 * @return array
314
	 */
315
	public static function get_recipient_setting_field( Give_Email_Notification $email, $form_id = null, $edit_recipient = true ) {
316
		$recipient = array(
317
			'id'               => self::get_prefix( $email, $form_id ) . 'recipient',
318
			'name'             => esc_html__( 'Email Recipients', 'give' ),
319
			'desc'             => __( 'Enter the email address(es) that should receive a notification.', 'give' ),
320
			'type'             => 'email',
321
			'default'          => get_bloginfo( 'admin_email' ),
322
			'repeat'           => true,
323
			'repeat_btn_title' => esc_html__( 'Add Recipient', 'give' ),
324
		);
325
326
		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...
327
			$recipient['name']    = __( 'Email', 'give' );
328
			$recipient['default'] = '';
329
			$recipient['id']      = 'email';
330
			$recipient['desc']    = __( 'Enter the email address that should receive a notification.', 'give' );
331
332
			$recipient = array(
333
				'id'      => self::get_prefix( $email, $form_id ) . 'recipient',
334
				'type'    => 'group',
335
				'options' => array(
336
					'add_button'    => __( 'Add Email', 'give' ),
337
					'header_title'  => __( 'Email Recipient', 'give' ),
338
					'remove_button' => '<span class="dashicons dashicons-no"></span>',
339
				),
340
				'fields'  => array(
341
					$recipient,
342
				),
343
			);
344
		}
345
346
		// Disable field if email donor has recipient field.
347
		// @see https://github.com/impress-org/give/issues/2657
348
		if ( ! $edit_recipient ) {
349
			if ( 'group' == $recipient['type'] ) {
350
				$recipient         = current( $recipient['fields'] );
351
				$recipient['type'] = 'text';
352
			}
353
354
			$recipient['attributes']['disabled'] = 'disabled';
355
			$recipient['value']                  = $recipient['default'] = '{donor_email}';
356
			$recipient['repeat']                 = false;
357
			$recipient['desc']                   = __( 'This email is automatically sent to the donor and the recipient cannot be customized.', 'give' );
358
		}
359
360
		return $recipient;
361
	}
362
363
	/**
364
	 * Get preview setting field.
365
	 *
366
	 * @param Give_Email_Notification $email   Email Type.
367
	 * @param int                     $form_id Form ID.
368
	 *
369
	 * @since  2.0
370
	 * @access static
371
	 *
372
	 * @return array
373
	 */
374
	public static function get_preview_setting_field( Give_Email_Notification $email, $form_id = null ) {
375
		return array(
376
			'name' => __( 'Preview Email', 'give' ),
377
			'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.',
378
				'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...
379
			'id'   => self::get_prefix( $email, $form_id ) . 'preview_buttons',
380
			'type' => 'email_preview_buttons',
381
		);
382
	}
383
384
385
	/**
386
	 * Get form metabox setting field prefix.
387
	 *
388
	 * @since  2.0
389
	 * @access static
390
	 *
391
	 * @param Give_Email_Notification $email
392
	 * @param int                     $form_id
393
	 *
394
	 * @return string
395
	 */
396
	public static function get_prefix( Give_Email_Notification $email, $form_id = null ) {
397
		$meta_key = "{$email->config['id']}_";
398
399
		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...
400
			$meta_key = "_give_{$email->config['id']}_";
401
		}
402
403
		return $meta_key;
404
	}
405
}
406
407
// @todo: add per email sender options
408