<?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);
|
}
|