Completed
Push — issues/611 ( 66b6f3...efd7b9 )
by Ravinder
18:14
created

Give_Email_Setting_Field::get_section_start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 10
rs 9.4285
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, 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
		return $setting_fields;
45
	}
46
47
48
	/**
49
	 * Check if email notification setting has section end or not.
50
	 *
51
	 * @since  2.0
52
	 * @access private
53
	 *
54
	 * @param $setting
55
	 *
56
	 * @return bool
57
	 */
58
	public static function has_section_end( $setting ) {
59
		$last_field      = end( $setting );
60
		$has_section_end = false;
61
62
		if ( 'sectionend' === $last_field['type'] ) {
63
			$has_section_end = true;
64
		}
65
66
		return $has_section_end;
67
	}
68
69
	/**
70
	 * Check if email notification setting has section end or not.
71
	 *
72
	 * @since  2.0
73
	 * @access private
74
	 *
75
	 * @param Give_Email_Notification $email
76
	 * @param int                     $form_id
77
	 *
78
	 * @return array
79
	 */
80
	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...
81
		// Add section end field.
82
		$setting = array(
83
			'id'    => "give_title_email_settings_{$email->config['id']}",
84
			'type'  => 'title',
85
			'title' => $email->config['label'],
86
		);
87
88
		return $setting;
89
	}
90
91
	/**
92
	 * Check if email notification setting has section end or not.
93
	 *
94
	 * @since  2.0
95
	 * @access private
96
	 *
97
	 * @param array                   $setting
98
	 * @param Give_Email_Notification $email
99
	 *
100
	 * @return array
101
	 */
102
	public static function add_section_end( Give_Email_Notification $email, $setting ) {
103
		if ( ! self::has_section_end( $setting ) ) {
104
			// Add section end field.
105
			$setting[] = array(
106
				'id'   => "give_title_email_settings_{$email->config['id']}",
107
				'type' => 'sectionend',
108
			);
109
		}
110
111
		return $setting;
112
	}
113
114
	/**
115
	 * Get default setting field.
116
	 *
117
	 * @since  2.0
118
	 * @access static
119
	 *
120
	 * @param Give_Email_Notification $email
121
	 * @param int                     $form_id
122
	 *
123
	 * @return array
124
	 */
125
	public static function get_default_setting_fields( Give_Email_Notification $email, $form_id = 0 ) {
126
		$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...
127
128
		if ( Give_Email_Notification_Util::is_notification_status_editable( $email ) ) {
129
			$settings[] = self::get_notification_status_field( $email, $form_id );
130
		}
131
132
		$settings[] = self::get_email_subject_field( $email, $form_id );
133
		$settings[] = self::get_email_message_field( $email, $form_id );
134
		$settings[] = self::get_email_content_type_field( $email, $form_id );
135
136
		return $settings;
137
	}
138
139
	/**
140
	 * Get notification status setting field.
141
	 *
142
	 * @since  2.0
143
	 * @access static
144
	 *
145
	 * @param Give_Email_Notification $email
146
	 * @param int                     $form_id
147
	 *
148
	 * @return array
149
	 */
150
	public static function get_notification_status_field( Give_Email_Notification $email, $form_id = 0 ) {
151
		$option = array(
152
			'enabled'  => __( 'Enabled', 'give' ),
153
			'disabled' => __( 'Disabled', 'give' ),
154
		);
155
156
		$default_value = $email->get_notification_status();
157
158
		// Remove global options.
159
		if ( $form_id ) {
160
			$option = array(
161
				'global'   => __( 'Global Options' ),
162
				'enabled'  => __( 'Customize', 'give' ),
163
				'disabled' => __( 'Disabled', 'give' ),
164
			);
165
166
			$default_value = 'global';
167
		}
168
169
		return array(
170
			'name'          => esc_html__( 'Notification', 'give' ),
171
			'desc'          => esc_html__( 'Choose option if you want to send email notification or not.', 'give' ),
172
			'id'            => "{$email->config['id']}_notification",
173
			'type'          => 'radio_inline',
174
			'default'       => $default_value,
175
			'options'       => $option,
176
			'wrapper_class' => 'give_email_api_notification_status_setting',
177
		);
178
	}
179
180
	/**
181
	 * Get email subject setting field.
182
	 *
183
	 * @since  2.0
184
	 * @access static
185
	 *
186
	 * @param Give_Email_Notification $email
187
	 * @param int                     $form_id
188
	 *
189
	 * @return array
190
	 */
191
	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...
192
		return array(
193
			'id'      => "{$email->config['id']}_email_subject",
194
			'name'    => esc_html__( 'Email Subject', 'give' ),
195
			'desc'    => esc_html__( 'Enter the subject line for email.', 'give' ),
196
			'default' => $email->config['default_email_subject'],
197
			'type'    => 'text',
198
		);
199
	}
200
201
	/**
202
	 * Get email message setting field.
203
	 *
204
	 * @since  2.0
205
	 * @access static
206
	 *
207
	 * @param Give_Email_Notification $email
208
	 * @param int                     $form_id
209
	 *
210
	 * @return array
211
	 */
212
	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...
213
		$desc = esc_html__( 'Enter the email message.', 'give' );
214
215
		if ( $email_tag_list = $email->get_allowed_email_tags( true ) ) {
216
			$desc = sprintf(
217
				esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
218
				$email_tag_list
219
			);
220
221
		}
222
223
		return array(
224
			'id'      => "{$email->config['id']}_email_message",
225
			'name'    => esc_html__( 'Email message', 'give' ),
226
			'desc'    => $desc,
227
			'type'    => 'wysiwyg',
228
			'default' => $email->config['default_email_message'],
229
		);
230
	}
231
232
	/**
233
	 * Get email message setting field.
234
	 *
235
	 * @since  2.0
236
	 * @access static
237
	 *
238
	 * @param Give_Email_Notification $email
239
	 * @param int                     $form_id
240
	 *
241
	 * @return array
242
	 */
243
	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...
244
		return array(
245
			'id'      => "{$email->config['id']}_email_content_type",
246
			'name'    => esc_html__( 'Email Content Type', 'give' ),
247
			'desc'    => __( 'Choose email content type.', 'give' ),
248
			'type'    => 'select',
249
			'options' => array(
250
				'text/html'  => Give_Email_Notification_Util::get_formatted_email_type( 'text/html' ),
251
				'text/plain' => Give_Email_Notification_Util::get_formatted_email_type( 'text/plain' ),
252
			),
253
			'default' => $email->config['content_type'],
254
		);
255
	}
256
257
258
	/**
259
	 * Get recipient setting field.
260
	 *
261
	 * @since  2.0
262
	 * @access static
263
	 *
264
	 * @param Give_Email_Notification $email
265
	 * @param int                     $form_id
266
	 *
267
	 * @return array
268
	 */
269
	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...
270
		return array(
271
			'id'               => "{$email->config['id']}_recipient",
272
			'name'             => esc_html__( 'Email Recipients', 'give' ),
273
			'desc'             => __( 'Enter the email address(es) that should receive a notification anytime a donation is made.', 'give' ),
274
			'type'             => 'email',
275
			'default'          => get_bloginfo( 'admin_email' ),
276
			'repeat'           => true,
277
			'repeat_btn_title' => esc_html__( 'Add Recipient', 'give' ),
278
		);
279
	}
280
281
	/**
282
	 * Get preview setting field.
283
	 *
284
	 * @since  2.0
285
	 * @access static
286
	 *
287
	 * @param Give_Email_Notification $email
288
	 * @param int                     $form_id
289
	 *
290
	 * @return array
291
	 */
292
	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...
293
		return array(
294
			'name' => esc_html__( 'Preview Email', 'give' ),
295
			'desc' => esc_html__( 'Click the buttons to preview emails.', 'give' ),
296
			'id'   => "{$email->config['id']}_preview_buttons",
297
			'type' => 'email_preview_buttons',
298
		);
299
	}
300
}
301
302
// @todo: add per email sender options
303