Test Failed
Push — master ( 966cf3...fe1ced )
by Devin
13:57 queued 06:53
created

Give_Email_Notification_Util::get_value()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 12
nop 4
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Email Notification Util
4
 *
5
 * This class contains helper functions for  email notification.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.0
12
 */
13
14
// Exit if access directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
20
class Give_Email_Notification_Util {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
21
	/**
22
	 * Instance.
23
	 *
24
	 * @since  2.0
25
	 * @access static
26
	 * @var
27
	 */
28
	static private $instance;
29
30
	/**
31
	 * Singleton pattern.
32
	 *
33
	 * @since  2.0
34
	 * @access private
35
	 * Give_Email_Notification_Util constructor.
36
	 */
37
	private function __construct() {
38
	}
39
40
41
	/**
42
	 * Get instance.
43
	 *
44
	 * @since  2.0
45
	 * @access static
46
	 * @return static
47
	 */
48
	static function get_instance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
		if ( null === static::$instance ) {
50
			self::$instance = new static();
51
		}
52
53
		return self::$instance;
54
	}
55
56
57
	/**
58
	 * Check if notification has preview field or not.
59
	 *
60
	 * @since  2.0
61
	 * @access public
62
	 *
63
	 * @param Give_Email_Notification $email
64
	 *
65
	 * @return bool
66
	 */
67
	public static function has_preview( Give_Email_Notification $email ) {
68
		return $email->config['has_preview'];
69
	}
70
71
	/**
72
	 * Check if notification has recipient field or not.
73
	 *
74
	 * @since  2.0
75
	 * @access public
76
	 *
77
	 * @param Give_Email_Notification $email
78
	 *
79
	 * @return bool
80
	 */
81
	public static function has_recipient_field( Give_Email_Notification $email ) {
82
		return $email->config['has_recipient_field'];
83
	}
84
85
	/**
86
	 * Check if admin can edit notification status or not.
87
	 *
88
	 * @since  2.0
89
	 * @access public
90
	 *
91
	 * @param Give_Email_Notification $email
92
	 *
93
	 * @return bool
94
	 */
95
	public static function is_notification_status_editable( Give_Email_Notification $email ) {
96
		$user_can_edit = $email->config['notification_status_editable'];
97
98
		return (bool) $user_can_edit;
99
	}
100
101
	/**
102
	 * Check if admin can edit notification status or not.
103
	 *
104
	 * @since  2.0
105
	 * @access public
106
	 *
107
	 * @param Give_Email_Notification $email
108
	 *
109
	 * @return bool
110
	 */
111
	public static function is_content_type_editable( Give_Email_Notification $email ) {
112
		return $email->config['content_type_editable'];
113
	}
114
115
	/**
116
	 * Check email preview header active or not.
117
	 *
118
	 * @since  2.0
119
	 * @access public
120
	 *
121
	 * @param Give_Email_Notification $email
122
	 *
123
	 * @return bool
124
	 */
125
	public static function is_email_preview_has_header( Give_Email_Notification $email ) {
126
		return $email->config['has_preview_header'];
127
	}
128
129
	/**
130
	 * Check email preview header active or not.
131
	 *
132
	 * @since  2.0
133
	 * @access public
134
	 *
135
	 * @param Give_Email_Notification $email
136
	 *
137
	 * @return bool
138
	 */
139
	public static function is_email_preview( Give_Email_Notification $email ) {
140
		return $email->config['has_preview'];
141
	}
142
143
	/**
144
	 * Check if email notification setting appear on emails setting page or not.
145
	 *
146
	 * @since  2.0
147
	 * @access public
148
	 *
149
	 * @param Give_Email_Notification $email
150
	 *
151
	 * @return bool
152
	 */
153
	public static function is_show_on_emails_setting_page( Give_Email_Notification $email ){
154
		return $email->config['show_on_emails_setting_page'];
155
	}
156
157
	/**
158
	 * Check if we can use form email options.
159
	 *
160
	 * @since  2.0
161
	 * @access public
162
	 *
163
	 * @param Give_Email_Notification $email
164
	 * @param int $form_id
165
	 *
166
	 * @return bool
167
	 */
168
	public static function can_use_form_email_options( Give_Email_Notification $email, $form_id = null ){
0 ignored issues
show
Unused Code introduced by
The parameter $email 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...
169
		return give_is_setting_enabled( give_get_meta( $form_id, '_give_email_options', true ) );
170
	}
171
172
	/**
173
	 * Check email active or not.
174
	 *
175
	 * @since  2.0
176
	 * @access public
177
	 *
178
	 * @param Give_Email_Notification $email
179
	 * @param int $form_id
180
	 *
181
	 * @return string
182
	 */
183
	public static function is_email_notification_active( Give_Email_Notification $email, $form_id = null ) {
184
		$notification_status = $email->get_notification_status( $form_id );
185
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
186
187
		$notification_status = empty( $form_id )
188
			? give_is_setting_enabled( $notification_status )
0 ignored issues
show
Documentation introduced by
$notification_status is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
			: give_is_setting_enabled( give_get_option( "{$email->config['id']}_notification", $email->config['notification_status'] ) ) && give_is_setting_enabled( $notification_status, array( 'enabled', 'global' ) );
0 ignored issues
show
Documentation introduced by
$notification_status is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array('enabled', 'global') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
190
			// To check if email notification is active or not on a per-form basis, email notification must be globally active—otherwise it will be considered disabled.
191
192
		return $notification_status;
193
	}
194
195
	/**
196
	 * Check if admin preview email or not
197
	 *
198
	 * @since  2.0
199
	 * @access public
200
	 * @return bool   $is_preview
201
	 */
202 View Code Duplication
	public static function can_preview_email() {
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...
203
		$is_preview = false;
204
205
		if (
206
			current_user_can( 'manage_give_settings' )
207
			&& ! empty( $_GET['give_action'] )
208
			&& 'preview_email' === $_GET['give_action']
209
		) {
210
			$is_preview = true;
211
		}
212
213
		return $is_preview;
214
	}
215
216
	/**
217
	 * Check if admin preview email or not
218
	 *
219
	 * @since  2.0
220
	 * @access public
221
	 * @return bool   $is_preview
222
	 */
223 View Code Duplication
	public static function can_send_preview_email() {
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...
224
		$is_preview = false;
225
226
		if (
227
			current_user_can( 'manage_give_settings' )
228
			&& ! empty( $_GET['give_action'] )
229
			&& 'send_preview_email' === $_GET['give_action']
230
		) {
231
			$is_preview = true;
232
		}
233
234
		return $is_preview;
235
	}
236
237
238
	/**
239
	 * Get formatted text for email content type.
240
	 *
241
	 * @since  2.0
242
	 * @access public
243
	 *
244
	 * @param string $content_type
245
	 *
246
	 * @return string
247
	 */
248
	public static function get_formatted_email_type( $content_type ) {
249
		$email_contents = array(
250
			'text/html'  => __( 'HTML', 'give' ),
251
			'text/plain' => __( 'Plain', 'give' ),
252
		);
253
254
		return $email_contents[ $content_type ];
255
	}
256
257
258
	/**
259
	 * Get email notification option value.
260
	 *
261
	 * @since  2.0
262
	 * @access public
263
	 *
264
	 * @param Give_Email_Notification $email
265
	 * @param string                   $option_name
266
	 * @param int                      $form_id
267
	 * @param mixed                    $default
268
	 *
269
	 * @return mixed
270
	 */
271
	public static function get_value( Give_Email_Notification $email, $option_name, $form_id = null, $default = false ) {
272
		// If form id set then option name can be contain _give_ prefix which is only used for meta key,
273
		// So make sure you are using correct option name.
274
		$global_option_name = ( 0 === strpos( $option_name, '_give_' )
275
			? str_replace( '_give_', '', $option_name )
276
			: $option_name );
277
		$option_value = give_get_option( $global_option_name, $default );
278
279
		if (
280
			! empty( $form_id )
281
			&& give_is_setting_enabled(
282
				give_get_meta(
283
					$form_id,
284
					Give_Email_Setting_Field::get_prefix( $email, $form_id ) . 'notification',
285
					true,
286
					'global'
0 ignored issues
show
Documentation introduced by
'global' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
287
				)
288
			)
289
		) {
290
			$option_value = get_post_meta( $form_id, $option_name, true );
291
292
			// Get only email field value from recipients setting.
293
			if( Give_Email_Setting_Field::get_prefix( $email, $form_id ) . 'recipient' === $option_name ) {
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...
294
				$option_value = wp_list_pluck( $option_value, 'email' );
295
			}
296
		}
297
298
		$option_value = empty( $option_value ) ? $default : $option_value;
299
300
		/**
301
		 * Filter the setting value
302
		 *
303
		 * @since 2.0
304
		 */
305
		return apply_filters( 'give_email_setting_value', $option_value, $option_name, $email, $form_id, $default );
306
	}
307
}
308