-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper-functions.php
65 lines (58 loc) · 2.08 KB
/
helper-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Returns a string of combined CSS class names.
*
* @param string ...$classes Multiple string arguments representing CSS class names.
* @return string A space-separated string of unique CSS class names.
*/
function get_css_classes(...$classes) {
// Remove empty values and ensure unique class names
$classes = array_unique(array_filter($classes));
// Sanitize class names
$sanitized_classes = array_map('esc_attr', $classes);
// Combine class names into a space-separated string
return implode(' ', $sanitized_classes);
}
/**
* Returns a link from an ACF link field.
*
* @param array $link The ACF link field array containing URL, title, and target.
* @param string $class Optional. Additional class(es) to add to the link. Default empty.
*
* @return string
*/
function get_acf_link($link, $class = '') {
if (!is_array($link) || empty($link['url'])) {
return '';
}
// Extract and sanitize link attributes
$url = esc_url($link['url']);
$title = esc_html($link['title'] ?? '');
$target = esc_attr($link['target'] ?? '_self');
$rel = ($target === '_blank') ? 'noopener noreferrer' : '';
// Format the anchor tag
return sprintf(
'<a href="%s" %s target="%s" rel="%s">%s</a>',
$url,
$class ? 'class="' . esc_attr($class) . '"' : '',
$target,
esc_attr($rel),
$title
);
}
/**
* Filters out empty strings, empty arrays, and false values from an array, except for specified keys.
*
* @param array $array The array to be filtered.
* @param array $retainFalse Keys for which false values should be kept.
* @return array The filtered array.
*/
function filter_empty_values($array, $retainFalse = []) {
return array_filter($array, function($value, $key) use ($retainFalse) {
if (in_array($key, $retainFalse)) {
return true; // Retain all values for these keys
}
// For other keys, filter out empty strings, empty arrays, and false values
return $value !== '' && $value !== array() && $value !== false;
}, ARRAY_FILTER_USE_BOTH);
}