Passed
Push — 193-feature/search-permalink ( 77bed7...5759fa )
by Maria Daniel Deepak
03:17
created

is_advanced_search_term()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php namespace EmailLog\Util;
2
3
/**
4
 * Email Log Helper functions.
5
 * Some of these functions would be used the addons.
6
 */
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Perform additional sanitation of emails.
11
 *
12
 * @since 1.9
13
 *
14
 * @param string $email    Email string to be sanitized.
15
 * @param bool   $multiple (Optional) Should multiple emails be allowed. True by default.
16
 *
17
 * @return string Sanitized email.
18
 */
19
function sanitize_email( $email, $multiple = true ) {
20 13
	$emails = explode( ',', $email );
21 13
	if ( ! $multiple ) {
22 3
		$emails = array_slice( $emails, 0, 1 );
23 3
	}
24
25 13
	$cleaned_emails = array_map( __NAMESPACE__ . '\\sanitize_email_with_name', $emails );
26
27 13
	return implode( ', ', $cleaned_emails );
28
}
29
30
/**
31
 * Sanitize email with name.
32
 *
33
 * @since 1.9
34
 *
35
 * @param string $string Email string to be sanitized.
36
 *
37
 * @return string Sanitized email.
38
 */
39
function sanitize_email_with_name( $string ) {
40 13
	$string = trim( $string );
41
42 13
	$bracket_pos = strpos( $string, '<' );
43 13
	if ( false !== $bracket_pos ) {
44 5
		if ( $bracket_pos > 0 ) {
45 5
			$name = substr( $string, 0, $bracket_pos );
46 5
			$name = trim( $name );
47
48 5
			$email = substr( $string, $bracket_pos + 1 );
49 5
			$email = str_replace( '>', '', $email );
50
51 5
			return sanitize_text_field( $name ) . ' <' . \sanitize_email( $email ) . '>';
52
		}
53
	}
54
55 8
	return \sanitize_email( $string );
56
}
57
58
/**
59
 * Gets the columns to export logs.
60
 *
61
 * If the More Fields add-on is active, additional columns are returned.
62
 *
63
 * @since 2.0.0
64
 *
65
 * @return string[] List of Columns to export.
66
 */
67
function get_log_columns_to_export() {
68
69
	if ( is_plugin_active( 'email-log-more-fields/email-log-more-fields.php' ) ) {
70
		return array( 'id', 'sent_date', 'to_email', 'subject', 'from', 'cc', 'bcc', 'reply-to', 'attachment' );
71
	}
72
73
	return array( 'id', 'sent_date', 'to_email', 'subject' );
74
}
75
76
/**
77
 * Is it an admin request and not an ajax request.
78
 *
79
 * @since 2.1
80
 *
81
 * @return bool True if admin non ajax request, False otherwise.
82
 */
83
function is_admin_non_ajax_request() {
84
	if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) {
85
		return false;
86
	}
87
88
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
89
		return false;
90
	}
91
92
	return is_admin();
93
}
94
95
/**
96
 * Checks the Checkbox when values are present in a given array.
97
 *
98
 * Use this function in Checkbox fields.
99
 *
100
 * @since 2.1.0
101
 *
102
 * @param array  $values  List of all possible values.
103
 * @param string $current The current value to be checked.
104
 */
105
function checked_array( $values, $current ) {
106 2
	if ( ! is_array( $values ) ) {
1 ignored issue
show
introduced by
The condition is_array($values) is always true.
Loading history...
107 1
		return;
108
	}
109
110 1
	if ( in_array( $current, $values ) ) {
111 1
		echo "checked='checked'";
112 1
	}
113 1
}
114
115
/**
116
 * Returns Comma separated values of the given array elements.
117
 *
118
 * Use $delimiter param to join elements other than `,`.
119
 *
120
 * @since 2.3.0
121
 *
122
 * @param array|string $value     The array whose values are to be joined.
123
 * @param string       $delimiter Optional. Default is `,`.
124
 *
125
 * @return string
126
 */
127
function join_array_elements_with_delimiter( $value, $delimiter = ',' ) {
128
	if ( is_array( $value ) ) {
129
		return implode( $delimiter, $value );
130
	}
131
132
	return is_string( $value ) ? $value : '';
1 ignored issue
show
introduced by
The condition is_string($value) is always true.
Loading history...
133
}
134
135
/**
136
 * Gets the User defined Date time format.
137
 *
138
 * @used-by \EmailLog\Core\UI\Setting\CoreSetting
139
 * @used-by \EmailLog\Util\render_auto_delete_logs_next_run_schedule()
140
 *
141
 * @since   2.3.0
142
 *
143
 * @return string
144
 */
145
function get_user_defined_date_time_format() {
146
	return sprintf( '%1$s %2$s', get_option( 'date_format', 'Y-m-d' ), get_option( 'time_format', 'g:i a' ) );
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format', 'Y-m-d') can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

146
	return sprintf( '%1$s %2$s', /** @scrutinizer ignore-type */ get_option( 'date_format', 'Y-m-d' ), get_option( 'time_format', 'g:i a' ) );
Loading history...
147
}
148
149
/**
150
 * Renders the next run auto delete logs schedule in Date and time format set within WordPress.
151
 *
152
 * @used-by \EmailLog\Addon\UI\Setting\DashboardWidget
153
 * @used-by \EmailLog\Core\UI\Component\AutoDeleteLogsSetting
154
 *
155
 * @since 2.3.0
156
 */
157
function render_auto_delete_logs_next_run_schedule() {
158
	?>
159
	<?php if ( wp_next_scheduled( 'el_scheduled_delete_logs' ) ) : ?>
160
		<p>
161
			<?php _e( 'Auto delete logs cron will be triggered next at', 'email-log' ); ?>:
162
			<?php $date_time_format = get_user_defined_date_time_format(); ?>
163
			<strong><?php echo get_date_from_gmt( date( 'Y-m-d H:i:s', wp_next_scheduled( 'el_scheduled_delete_logs' ) ), $date_time_format ); ?></strong>
0 ignored issues
show
Bug introduced by
It seems like wp_next_scheduled('el_scheduled_delete_logs') can also be of type false; however, parameter $timestamp of date() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
			<strong><?php echo get_date_from_gmt( date( 'Y-m-d H:i:s', /** @scrutinizer ignore-type */ wp_next_scheduled( 'el_scheduled_delete_logs' ) ), $date_time_format ); ?></strong>
Loading history...
164
		</p>
165
	<?php endif; ?>
166
	<?php
167
}
168
169
/**
170
 * Gets the Advanced search URL that the Email Log search Icon uses.
171
 *
172
 * @since 2.3.0
173
 *
174
 * @return string The URL.
175
 */
176
function get_advanced_search_url() {
177
	return get_admin_url( null, 'admin.php?page=email-log' );
178
}
179
180
/**
181
 * Gets the value by key from the array.
182
 *
183
 * If the key isn't found, then null is returned.
184
 *
185
 * @since 2.3.0
186
 *
187
 * @param array  $array
188
 * @param string $key
189
 * @param string $default Optional.
190
 *
191
 * @return null|mixed
192
 */
193
function el_array_get( $array, $key, $default = null ) {
194
	return isset( $array[ $key ] ) ? $array[ $key ] : $default;
195
}
196
197
/**
198
 * Returns TRUE if the given search term is Advanced Search Term
199
 *
200
 * @param $term
201
 *
202
 * @return bool
203
 */
204
function is_advanced_search_term( $term ) {
205
	if ( ! is_string( $term ) ) {
206
		return false;
207
	}
208
209
	$predicates = get_advanced_search_term_predicates( $term );
210
211
	return ! empty( $predicates );
212
}
213
214
/**
215
 * Gets the Search Term Predicates.
216
 *
217
 * Example:
218
 *
219
 * If $term = to:[email protected] then,
220
 *
221
 * the output would be
222
 *
223
 * $output = array(
224
 *      'to' => [email protected]
225
 * )
226
 *
227
 * @since 2.3.0
228
 *
229
 * @param string $term
230
 *
231
 * @return array
232
 */
233
function get_advanced_search_term_predicates( $term ) {
234
	if ( ! is_string( $term ) ) {
0 ignored issues
show
introduced by
The condition is_string($term) is always true.
Loading history...
235
		return array();
236
	}
237
238
	$predicates           = explode( ' ', $term );
239
	$predicates_organized = array();
240
241
	foreach ( $predicates as $predicate ) {
242
		$is_match = preg_match( '/(email|to):(.*)$/', $predicate, $matches );
243
		if ( 1 === $is_match ) {
244
			$predicates_organized[ $matches[1] ] = $matches[2];
245
		}
246
	}
247
248
	return $predicates_organized;
249
}
250