Completed
Pull Request — dev/2.3.2 (#218)
by Sudar
27:34 queued 12:33
created

get_success_icon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
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(
71
			'id',
72
			'sent_date',
73
			'to_email',
74
			'subject',
75
			'from',
76
			'cc',
77
			'bcc',
78
			'reply-to',
79
			'attachment',
80
		);
81
	}
82
83
	return array( 'id', 'sent_date', 'to_email', 'subject' );
84
}
85
86
/**
87
 * Is it an admin request and not an ajax request.
88
 *
89
 * @since 2.1
90
 *
91
 * @return bool True if admin non ajax request, False otherwise.
92
 */
93
function is_admin_non_ajax_request() {
94
	if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) {
95
		return false;
96
	}
97
98
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
99
		return false;
100
	}
101
102
	return is_admin();
103
}
104
105
/**
106 2
 * Checks the Checkbox when values are present in a given array.
107 1
 *
108
 * Use this function in Checkbox fields.
109
 *
110 1
 * @since 2.1.0
111 1
 *
112 1
 * @param array  $values  List of all possible values.
113 1
 * @param string $current The current value to be checked.
114
 */
115
function checked_array( $values, $current ) {
116
	if ( ! is_array( $values ) ) {
117
		return;
118
	}
119
120
	if ( in_array( $current, $values ) ) {
121
		echo "checked='checked'";
122
	}
123
}
124
125
/**
126
 * Return failure icon.
127
 *
128
 * @since 2.3.2
129
 *
130 1
 * @return string Failure icon markup.
131
 */
132
function get_failure_icon() {
133
	return <<<EOT
134 1
<span class="dashicons dashicons-dismiss"></span>
135
EOT;
136
}
137
138
/**
139
 * Return success icon.
140
 *
141
 * @since 2.3.2
142
 *
143
 * @return string Success icon markup.
144
 */
145
function get_success_icon() {
146
	return <<<EOT
147
<span class="dashicons dashicons-yes-alt"></span>
148
EOT;
149
150
}
151
152
/**
153
 * Stringify arrays.
154
 *
155
 * If the parameter is an array, then return delimiter separated values of the array.
156
 * Otherwise return the parameter.
157
 *
158
 * @since 2.3.0
159
 * @since 2.3.2 Renamed name to `Stringify`.
160
 *
161
 * @param array|string $may_be_array The array whose values are to be converted to string.
162
 * @param string       $delimiter    Optional. Default is `,`.
163
 *
164
 * @return string Stringified value.
165
 */
166
function stringify( $may_be_array, $delimiter = ',' ) {
167
	if ( ! is_array( $may_be_array ) ) {
168
		return (string) $may_be_array;
169
	}
170
171
	return implode( $delimiter, $may_be_array );
172
}
173
174
/**
175
 * Gets the User defined Date time format.
176
 *
177
 * @used-by \EmailLog\Core\UI\Setting\CoreSetting
178
 * @used-by \EmailLog\Util\render_auto_delete_logs_next_run_schedule()
179
 *
180
 * @since   2.3.0
181
 *
182
 * @return string
183
 */
184
function get_user_defined_date_time_format() {
185
	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

185
	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...
186
}
187
188
/**
189
 * Renders the next run auto delete logs schedule in Date and time format set within WordPress.
190
 *
191
 * @used-by \EmailLog\Addon\UI\Setting\DashboardWidget
192
 * @used-by \EmailLog\Core\UI\Component\AutoDeleteLogsSetting
193
 *
194
 * @since   2.3.0
195
 */
196
function render_auto_delete_logs_next_run_schedule() {
197
	?>
198
	<?php if ( wp_next_scheduled( 'el_scheduled_delete_logs' ) ) : ?>
199
		<p>
200
			<?php _e( 'Auto delete logs cron will be triggered next at', 'email-log' ); ?>:
201
			<?php $date_time_format = get_user_defined_date_time_format(); ?>
202
			<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

202
			<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...
203
		</p>
204
	<?php endif; ?>
205
	<?php
206
}
207
208
/**
209
 * Gets the value by key from the array.
210
 *
211
 * If the key isn't found, then null is returned.
212
 *
213
 * @since 2.3.0
214
 *
215
 * @param array  $array   The actual array.
216
 * @param string $key     The key whose value is to be retrieved.
217
 * @param string $default Optional.
218
 *
219
 * @return mixed|null
220
 */
221
function el_array_get( $array, $key, $default = null ) {
222
	return isset( $array[ $key ] ) ? $array[ $key ] : $default;
223
}
224
225
/**
226
 * Returns TRUE if the given search term is Advanced Search Term.
227
 *
228
 * @param string $term Search Term.
229
 *
230
 * @return bool
231
 */
232
function is_advanced_search_term( $term ) {
233
	if ( ! is_string( $term ) ) {
0 ignored issues
show
introduced by
The condition is_string($term) is always true.
Loading history...
234
		return false;
235
	}
236
237
	$predicates = get_advanced_search_term_predicates( $term );
238
239
	return ! empty( $predicates );
240
}
241
242
/**
243
 * Gets the Search Term Predicates.
244
 *
245
 * Example:
246
 *
247
 * If $term = to:[email protected] then,
248
 *
249
 * the output would be
250
 *
251
 * $output = array(
252
 *      'to' => [email protected]
253
 * )
254
 *
255
 * @since 2.3.0
256
 *
257
 * @param string $term Search Term.
258
 *
259
 * @return array
260
 */
261
function get_advanced_search_term_predicates( $term ) {
262
	if ( ! is_string( $term ) ) {
0 ignored issues
show
introduced by
The condition is_string($term) is always true.
Loading history...
263
		return array();
264
	}
265
266
	$predicates           = explode( ' ', $term );
267
	$predicates_organized = array();
268
269 1
	foreach ( $predicates as $predicate ) {
270 1
		$is_match = preg_match( '/(id|email|to|cc|bcc|reply-to):(.*)$/', $predicate, $matches );
271 1
		if ( 1 === $is_match ) {
272 1
			$predicates_organized[ $matches[1] ] = $matches[2];
273
		}
274 1
	}
275
276
	return $predicates_organized;
277
}
278
279
/**
280
 * Gets the Advanced Search URL.
281
 *
282
 * @since 2.3.0
283
 *
284
 * @return string
285
 */
286
function get_advanced_search_url() {
287
	$admin_url = get_admin_url( null, 'admin.php?page=email-log' );
288
289
	return add_query_arg( 'el_as', 1, $admin_url );
290 1
}
291
292 1
/**
293 1
 * Gets the Column labels to be used in LogList table.
294
 *
295 1
 * @since 2.3.2
296
 * @since 2.3.0
297
 *
298 1
 * @param string $db_column
299
 *
300
 * @return string
301
 */
302
function get_column_label_by_db_column( $db_column ) {
303
	// Standard column labels are on the right.
304
	// $mapping[ $non_standard_key ] => $standard_key
305
	$mapping = array(
306
		'to'          => 'to_email', // EmailLog\Core\UI\ListTable::get_columns() uses `to`.
307
		'reply-to'    => 'reply_to',
308
		'attachment'  => 'attachments',
309
		'sent_status' => 'result',
310
	);
311
312
	$labels = get_email_log_columns();
313 1
314 1
	/**
315 1
	 * Filters the Labels used through out the Email Log plugin.
316 1
	 *
317 1
	 * @since 2.3.0
318 1
	 *
319 1
	 * @param array $labels {
320 1
	 *                      List of DB Columns and its respective labels.
321 1
	 *
322 1
	 *                      Example:
323 1
	 *                      'id'          => __( 'ID', 'email-log' ),
324 1
	 *
325
	 * @type string $key    DB Column or any key for which a Label would be required. Accepts a internationalized string as Label.
326
	 *              }
327
	 */
328
	$labels = apply_filters( 'el_db_column_labels', $labels );
329
330
	if ( array_key_exists( $db_column, $labels ) ) {
331
		return $labels[ $db_column ];
332
	} elseif ( array_key_exists( $db_column, $mapping ) ) {
333
		$label_key = $mapping[ $db_column ];
334
335
		return $labels[ $label_key ];
336
	}
337
338
	return $db_column;
339
}
340
341
/**
342
 * Returns an array of Email Log columns.
343
 *
344
 * Keys are the column names in the DB.
345
 * This holds true except for CC, BCC & Reply To as they are put under one column `headers`.
346
 *
347
 * @since 2.3.2
348
 *
349
 * @return array Key value pair of Email Log columns.
350
 */
351
function get_email_log_columns() {
352
	return array(
353
		'id'          => __( 'ID', 'email-log' ),
354
		'sent_date'   => __( 'Sent at', 'email-log' ),
355
		'to_email'    => __( 'To', 'email-log' ),
356
		'subject'     => __( 'Subject', 'email-log' ),
357
		'message'     => __( 'Message', 'email-log' ),
358
		'from'        => __( 'From', 'email-log' ),
359
		'cc'          => __( 'CC', 'email-log' ),
360
		'bcc'         => __( 'BCC', 'email-log' ),
361
		'attachments' => __( 'Attachment', 'email-log' ),
362
		'ip_address'  => __( 'IP Address', 'email-log' ),
363
		'reply_to'    => __( 'Reply To', 'email-log' ),
364
		'result'      => __( 'Sent Status', 'email-log' ),
365
	);
366
}
367
368
/**
369
 * Abstract of the core logic behind masking.
370
 *
371
 * @since 2.3.2
372
 *
373
 * @param string $value     Content.
374
 * @param string $mask_char Mask character.
375
 * @param int    $percent   The higher the percent, the more masking character on the email.
376
 *
377
 * @return string
378
 */
379
function get_masked_value( $value, $mask_char, $percent ) {
380
	$len        = strlen( $value );
381
	$mask_count = (int) floor( $len * $percent / 100 );
382
	$offset     = (int) floor( ( $len - $mask_count ) / 2 );
383
384
	return substr( $value, 0, $offset ) . str_repeat( $mask_char, $mask_count ) . substr( $value, $mask_count + $offset );
385
}
386
387
/**
388
 * Masks Email address.
389
 *
390
 * @see   http://www.webhostingtalk.com/showthread.php?t=1014672
391
 * @since 2.3.2
392
 *
393
 * @uses  get_masked_value()
394
 *
395
 * @param string $email     Email to be masked.
396
 * @param string $mask_char Mask character.
397
 * @param int    $percent   The higher the percent, the more masking character on the email.
398
 *
399
 * @return string
400
 */
401
function mask_email( $email, $mask_char = '*', $percent = 50 ) {
402
	if ( ! is_email( $email ) ) {
403
		return $email;
404
	}
405
406
	list( $user, $domain ) = preg_split( '/@/', $email );
407
408
	return sprintf(
409
		'%1$s@%2$s',
410
		get_masked_value( $user, $mask_char, $percent ),
411
		get_masked_value( $domain, $mask_char, $percent )
412
	);
413
}
414
415
/**
416
 * Mask Content fields.
417
 *
418
 * Content fields can be Subject or Email message.
419
 *
420
 * @since 2.3.2
421
 *
422
 * @uses  get_masked_value()
423
 *
424
 * @param string $content   The actual content.
425
 * @param string $mask_char Mask character.
426
 * @param int    $percent   The higher the percent, the more masking character on the email.
427
 *
428
 * @return string
429
 */
430
function mask_content( $content, $mask_char = '*', $percent = 80 ) {
431
	$content = wp_strip_all_tags( $content );
432
433
	return get_masked_value( $content, $mask_char, $percent );
434
}
435