Completed
Push — issues/611 ( 75335d...7ca481 )
by Ravinder
18:34
created

Give_Email_Notification_Util::get_value()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 8
nop 4
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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
		return $email->config['notification_status_editable'];
97
	}
98
99
	/**
100
	 * Check if admin can edit notification status or not.
101
	 *
102
	 * @since  2.0
103
	 * @access public
104
	 *
105
	 * @param Give_Email_Notification $email
106
	 *
107
	 * @return bool
108
	 */
109
	public static function is_content_type_editable( Give_Email_Notification $email ) {
110
		return $email->config['content_type_editable'];
111
	}
112
113
	/**
114
	 * Check email preview header active or not.
115
	 *
116
	 * @since  2.0
117
	 * @access public
118
	 *
119
	 * @param Give_Email_Notification $email
120
	 *
121
	 * @return bool
122
	 */
123
	public static function is_email_preview_has_header( Give_Email_Notification $email ) {
124
		return $email->config['has_preview_header'];
125
	}
126
127
	/**
128
	 * Check email preview header active or not.
129
	 *
130
	 * @since  2.0
131
	 * @access public
132
	 *
133
	 * @param Give_Email_Notification $email
134
	 *
135
	 * @return bool
136
	 */
137
	public static function is_email_preview( Give_Email_Notification $email ) {
138
		return $email->config['has_preview'];
139
	}
140
141
142
	/**
143
	 * Check email active or not.
144
	 *
145
	 * @since  2.0
146
	 * @access public
147
	 *
148
	 * @param Give_Email_Notification $email
149
	 * @param int $form_id
150
	 *
151
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
152
	 */
153
	public static function is_email_notification_active( Give_Email_Notification $email, $form_id = null ) {
154
		$notification_status = $email->get_notification_status( $form_id );
155
156
157
		$notification_status = empty( $form_id )
158
			? give_is_setting_enabled( $notification_status )
159
			: give_is_setting_enabled( give_get_option( "{$email->config['id']}_notification", $email->config['notification_status'] ) ) && give_is_setting_enabled( $notification_status, array( 'enabled', 'global' ) );
160
			// To check if email notification active or not on per form basis, email notification must be globally active other it will consider as disable.
161
162
		return $notification_status;
163
	}
164
165
	/**
166
	 * Check if admin preview email or not
167
	 *
168
	 * @since  2.0
169
	 * @access public
170
	 * @return bool   $is_preview
171
	 */
172
	public static function can_preview_email() {
173
		$is_preview = false;
174
175
		if (
176
			current_user_can( 'manage_give_settings' )
177
			&& ! empty( $_GET['give_action'] )
178
			&& 'preview_email' === $_GET['give_action']
179
		) {
180
			$is_preview = true;
181
		}
182
183
		return $is_preview;
184
	}
185
186
	/**
187
	 * Check if admin preview email or not
188
	 *
189
	 * @since  2.0
190
	 * @access public
191
	 * @return bool   $is_preview
192
	 */
193
	public static function can_send_preview_email() {
194
		$is_preview = false;
195
196
		if (
197
			current_user_can( 'manage_give_settings' )
198
			&& ! empty( $_GET['give_action'] )
199
			&& 'send_preview_email' === $_GET['give_action']
200
		) {
201
			$is_preview = true;
202
		}
203
204
		return $is_preview;
205
	}
206
207
208
	/**
209
	 * Get formatted text for email content type.
210
	 *
211
	 * @since  2.0
212
	 * @access public
213
	 *
214
	 * @param string $content_type
215
	 *
216
	 * @return string
217
	 */
218
	public static function get_formatted_email_type( $content_type ) {
219
		$email_contents = array(
220
			'text/html'  => __( 'HTML', 'give' ),
221
			'text/plain' => __( 'Plain', 'give' ),
222
		);
223
224
		return $email_contents[ $content_type ];
225
	}
226
227
228
	/**
229
	 * Get email notification option value.
230
	 *
231
	 * @since  2.0
232
	 * @access public
233
	 *
234
	 * @param Give_Email_Notification $email
235
	 * @param string                   $option_name
236
	 * @param int                      $form_id
237
	 * @param mixed                    $default
238
	 *
239
	 * @return mixed
240
	 */
241
	public static function get_value( Give_Email_Notification $email, $option_name, $form_id = null, $default = false ) {
242
		$global_option_name = ( 0 === strpos( $option_name, '_give_' )
243
			? str_replace( '_give_', '', $option_name )
244
			: $option_name );
245
		$option_value = give_get_option( $global_option_name, $default );
246
247
		if (
248
			! empty( $form_id )
249
			&& give_is_setting_enabled( give_get_meta( $form_id, Give_Email_Setting_Field::get_prefix( $email, $form_id ) . 'notification', true, 'global' ) )
250
		) {
251
			$option_value = get_post_meta( $form_id, $option_name, true );
252
		}
253
254
		$option_value = empty( $option_value ) ? $default : $option_value;
255
256
		/**
257
		 * Filter the setting value
258
		 *
259
		 * @since 2.0
260
		 */
261
		return apply_filters( 'give_email_setting_value', $option_value, $option_name, $email, $form_id, $default );
262
	}
263
}
264