get($key); $cached = false; 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($key, $out); return $out; } function jvbFormatStarRating(string $rating, int $total = 0):string { $out = '

'; $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 .= ''; $out .= ($total == 0) ? '' : '( '.$total.' Ratings )'; $out .= '

'; return $out; } /** * gets all the image links together and stores them * @param int $imgID * * @return array */ function jvbImageData(int $imgID):array { $cache = CacheManager::for('imageData', WEEK_IN_SECONDS); $cached = $cache->get($imgID); if ($cached) { return $cached; } if (!wp_get_attachment_image($imgID, 'tiny')) { return []; } $image = [ 'tiny' => wp_get_attachment_image_src($imgID, 'tiny')[0], 'small' => wp_get_attachment_image_src($imgID, 'medium')[0], 'medium' => wp_get_attachment_image_src($imgID, 'large')[0], 'large' => wp_get_attachment_image_src($imgID, 'full')[0], 'image-alt-text'=> get_post_meta($imgID, '_wp_attachment_image_alt', true), 'image-title' => get_the_title($imgID), 'image-caption' => get_the_excerpt($imgID), ]; $cache->set($imgID, $image); return $image; } 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.'.'.$cents.''; } 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; }