Jake Vanderwerf
2026-01-25 b38f03c0e7218762d90fa5092696b127f24f36db
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
 
use JVBase\managers\Cache;
use JVBase\utility\Image;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Ensures a string starts with the BASE constant
 * @param string $content
 *
 * @return string
 */
function jvbCheckBase(string $content):string
{
    return (str_starts_with($content, BASE)) ? $content : BASE.$content;
}
 
/**
 * Strips the BASE from the beginning of a string, for easier reference
 * @param string $content
 *
 * @return string
 */
function jvbNoBase(string $content):string
{
    return str_replace(BASE,'',$content);
}
 
/**
 * Outputs a comma separated list with a final 'and' or 'or'
 * @param array $arr
 * @param bool $and
 *
 * @return string
 */
function jvbCommaList(array $arr, bool $and = true):string
{
    $count = count($arr);
    $and = ($and) ? ' and ' : ' or ';
    switch ($count) {
        case '1':
            return implode(',', $arr);
        case '2':
            return implode($and, $arr);
        default:
            $last = array_pop($arr);
            return implode(', ', $arr).$and.$last;
    }
}
 
/**
 * Ensures every id is an int
 * @param string|array $list
 *
 * @return array
 */
function jvbSanitizeIDList(string|array $list):array
{
    if(!is_array($list)){
        $list = explode(',', $list);
    }
    return array_filter(array_map('intval', $list));
}
 
 
/**
 * @param int $ID
 * @param JVBase\meta\MetaManager|null $meta
 *
 * @return string
 */
function jvbFormatRating(int $ID, JVBase\meta\MetaManager|null $meta = null):string
{
    $cache = Cache::for('rating', WEEK_IN_SECONDS)->connect('post')->connect('taxonomy')->connect('user');
 
    $cached = $cache->get($ID);
    if ($cached) {
        return $cached;
    }
 
    if (!$meta) {
        if (term_exists((int)$ID)) {
            $type = 'term';
        } elseif (get_post_status((int)$ID)) {
            $type = 'post';
        } else {
            $type = 'user';
        }
        $meta = new JVBase\meta\MetaManager($ID, $type);
    }
 
    $out = '';
    $avg = $meta->getValue('average_rating');
 
    $total = $meta->getValue('total_ratings');
    if ($avg !== 'none') {
        $out .= jvbFormatStarRating($avg, (int)$total);
    }
 
    $cache->set($ID, $out);
    return $out;
}
 
function jvbFormatStarRating(string $rating, int $total = 0):string
{
    $out = '<p class="ratings" id ="rating">
            <span class="stars" aria-label="Rated '.$rating.' out of 5 Stars">';
    $temp = str_contains($rating, '.5');
    if ($temp) {
        $rating = (int)$rating;
    }
    $remaining = ($temp) ? $rating + 1 - 5 : $rating - 5;
    for ($i = 1; $i <= $rating; $i++) {
        $out .= jvbIcon('star', ['style' => 'fill']);
    }
    if ($temp) {
        $out .= jvbIcon('star-half');
    }
    for ($i = 1; $i <= $remaining; $i++) {
        $out .= jvbIcon('star');
    }
    $out .= '</span>';
    $out .= ($total == 0) ? '' : '( '.$total.' Ratings )';
    $out .= '</p>';
 
    return $out;
}
 
/**
 * gets all the image links together and stores them
 * @param int $imgID
 *
 * @return array
 */
function jvbImageData(int $imgID):array
{
    $image = new Image();
    return $image->getImageData($imgID);
}
 
 
function formatJSONField(mixed $value): string|bool|null
{
    if (empty($value)) {
        return null;
    }
 
    // If already a JSON string, validate it
    if (is_string($value)) {
        json_decode($value);
        if (json_last_error() === JSON_ERROR_NONE) {
            return $value;
        }
    }
 
    // If array or object, encode it
    if (is_array($value) || is_object($value)) {
        return json_encode($value);
    }
 
    // If not valid for JSON, return null
    return null;
}
 
 
function jvbCurrency(string $number) {
    if ($number === '') {
        $number = 0;
    }
    if (!is_numeric($number)) {
        return $number;
    }
    $number = number_format($number, 2);
    list($dollars, $cents) = explode('.', $number);
    return '$'.$dollars.'.<span>'.$cents.'</span>';
}
 
function jvbMailToLink(string $emailTo,
                       string $subject = 'Contact from Website',
                       string $message = '',
                       bool $icon = true,
                       ?string $linkText = null
):string
{
    if (!is_email($emailTo)) {
        return '';
    }
    $link = 'mailto:'.$emailTo.'?subject='.rawurlencode($subject);
    if ($message !== '') {
        $link .= '&body='.rawurlencode($message);
    }
    return $link;
}
function jvbTextLink(int $phoneNumber, string $message=''):string
{
    $length =strlen((string)$phoneNumber);
    if ($length < 10 || $length > 10) {
        return '';
    }
    $link = 'sms:+1'.$phoneNumber;
    if ($message !== '') {
        $link .= '?body='.rawurlencode($message);
    }
    return $link;
}
 
function jvbPhoneLink(int $phoneNumber):string
{
    $length =strlen((string)$phoneNumber);
    if ($length < 10 || $length > 10) {
        return '';
    }
    return 'tel:+1'.$phoneNumber;
}
 
function jvbFormatString(string $string):string
{
    return html_entity_decode($string);
}