getClassesAndStyles($attrs, ['search-container', 'row', 'left', 'nowrap'])
), jvbSearch($placeholder, uniqid(), $label, $buttonText, $isInside, $hideInput));
}
//core_shortcode
public function prerender_core_social_link(array $block, ?string $content, ?WP_Block $parent):?string
{
// jvbDump($block, 'social link');
// jvbDump($parent, 'Parent');
$parentAttrs = false;
if ($parent) {
$parentAttrs = $parent->attributes;
}
$attrs = $block['attrs']??[];
$url = $attrs['url']??'';
$service = $attrs['service']?:'';
$iconName = ($service === 'bluesky') ? 'butterfly' : $service.'-logo';
$icon = jvbIcon($iconName);
if (!$icon) {
$icon = jvbIcon('link');
}
$serviceName = $this->getServiceName($service);
$label = $parentAttrs && (!array_key_exists('className', $parentAttrs) || !str_contains($parentAttrs['className'], 'logos-only'))
? sprintf(
'
%s',
$serviceName
)
: sprintf(
'
Find us on %s',
$serviceName
);
$pillShaped = $parentAttrs && (array_key_exists('className', $parentAttrs) && str_contains($parentAttrs['className'], 'pill-shape'))
? 'style="border-radius:var(--radius-outer);"'
: '';
return sprintf(
'
%s%s',
$url,
$serviceName,
$pillShaped,
$icon,
$label
);
}
private function getServiceName(string $service) {
return match($service){
'wordpress' => 'WordPress',
default => ucfirst($service)
};
}
public function prerender_core_social_links(array $block, ?string $content, ?WP_Block $parent):?string
{
// jvbDump($block['attrs']??[], 'social links');
// jvbDump($parent, 'Parent');
return sprintf(
'
',
$this->getClassesAndStyles($block['attrs']??[], ['socials']),
$this->innerBlocks($block, '','',$block)
);
}
//core_tag_cloud
public function prerender_core_tag_cloud(array $block, ?string $content, ?WP_Block $parent):?string
{
// jvbDump($block, 'tag cloud');
$attrs = $block['attrs']??[];
$taxonomy = (array_key_exists('taxonomy', $attrs) && !empty($attrs['taxonomy']))
? $attrs['taxonomy']
: 'post_tag';
$showCounts = $this->checkAttrs('showTagCounts', $attrs);
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => true,
]);
if (!$terms || is_wp_error($terms)) {
return '';
}
$inside = '';
foreach ($terms as $term) {
$url = get_term_link($term->term_id, $taxonomy);
$count = $showCounts ?
sprintf(
'
%d',
$term->count
) :
'';
$size = match(true) {
$term->count <= 2 => 'small',
$term->count <= 5 => 'x-small',
$term->count <= 10 => 'medium',
$term->count <= 15 => 'x-medium',
$term->count <= 20 => 'large',
$term->count <= 25 => 'x-large',
$term->count <= 30 => 'xx-large',
$term->count > 30 => 'xxx-large',
};
$fontSize = 'font-size: var(--txt-'.$size.');';
$inside .= sprintf(
'
%s%s',
$size,
// $fontSize,
$url,
$term->name,
$count
);
}
return sprintf(
'
',
$this->getClassesAndStyles($attrs, ['term-list','cloud', jvbNoBase($taxonomy)]),
$inside
);
}
/**
* Extra feed block localization
*/
protected function localize_feedblock():void
{
wp_localize_script('jvb-feed-view-script', 'feedSettings', [
'currentUser' => is_user_logged_in() ? [
'id' => get_current_user_id()
] : null,
'from' => get_the_ID(),
]);
}
/***********************************
* Helpers
**********************************/
public function stripTagContents(string $tag, ?string $content):string
{
$clean = preg_replace('/<'.$tag.'\b[^>]*>.*?<\/'.$tag.'>/is', '', $content);
$clean = preg_replace('/\s+/', ' ', $clean);
return trim($clean);
}
public function innerBlocks(array $block, string $before = '', string $after = '', ?array $parent = null):string
{
if ($parent) {
$parent = new WP_Block($parent);
}
$content = '';
foreach ($block['innerBlocks'] as $b) {
$rendered = $parent
? $this->checkMethods(null, $b, $parent, true)
: render_block($b);
$content .= sprintf('%s%s%s',
$before,
$rendered,
$after
);
}
return $content;
}
public function inside(array $block, mixed $tag = false, mixed $o = false): string
{
$html = $o ?: trim($block['innerHTML']);
if (empty($html)) {
return '';
}
if (preg_match('/^<(\w+)[^>]*>(.*)<\/\1>$/s', $html, $matches)) {
if ($tag && strtolower($matches[1]) !== strtolower($tag)) {
return $html;
}
return trim($matches[2]);
}
return $html;
}
/**
* Extract content from a specific nested element
* @param string $html The HTML to parse
* @param string $tag The tag name to extract
* @return string The content of the first matching element, or empty string
*/
protected function extractElement(string $html, string $tag): string
{
if (empty($html)) {
return '';
}
$dom = new DOMDocument();
// Suppress errors for malformed HTML
libxml_use_internal_errors(true);
$dom->loadHTML('' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors();
$elements = $dom->getElementsByTagName($tag);
if ($elements->length === 0) {
return '';
}
return trim($elements->item(0)->textContent);
}
public function imageID(int|string $ID, array $block = []):int|false
{
if ($ID === '' && !empty($block)) {
if (($block['blockName'] === 'core/post-featured-image' ||
(!array_key_exists('attrs', $block) && !array_key_exists('id', $block['attrs'])))) {
$ID = get_post_thumbnail_id();
} else {
if (array_key_exists('id', $block['attrs']??[])) {
$ID = $block['attrs']['id'];
} elseif (array_key_exists('mediaId', $block['attrs'])) {
$ID = $block['attrs']['mediaId'];
}
}
}
if (!is_int($ID)) {
return false;
}
return $ID;
}
public function imageLink(
bool $close = false,
int $ID = 0,
string $start = 'tiny',
string $replace = 'large',
?string $postSlug = null
):string {
$image = jvbFormatImage($ID, $start, $replace, true, $postSlug);
if ($close) {
return $image;
}
$len = strlen('');
return substr_replace($image, '', -$len, $len);
}
public function image(string $ID = '', string $start = 'tiny', string $replace = 'large'):string
{
if ($ID == '') {
$ID = $this->imageID($ID);
}
if ($ID === 0 || $ID === false) {
return '';
}
return jvbFormatImage($ID, $start, $replace, false);
}
public function sanitizeBlockName(array $block):string
{
if (!array_key_exists('blockName', $block) || is_null($block['blockName'])) {
return '';
}
return str_replace(
'/',
'_',
str_replace(
'-',
'_',
$block['blockName']
)
);
}
/**
* Replaces the outside tag of an HTML string
* @param string $content
* @param bool|string $tagName
* @param string $classes
* @param string $styles
*
* @return string
*/
public function replaceOutsideTag(
string $content,
bool|string $tagName = false,
string $classes = '',
string $styles = ''
):string {
if ($tagName && !in_array($tagName, ['div', 'section', 'article', 'aside', 'main'])) {
return $content;
}
$content = explode('>', $content);
if ($classes !== '') {
if (!str_contains($classes, 'class="')) {
$classes = ' class="'.$classes.'"';
}
}
if ($styles !== '') {
if (!str_contains($styles, 'style="')) {
$styles = ' style="'.$styles.'"';
}
}
if ($tagName) {
$content[0] = '<'.str_replace(
'<',
'',
str_replace(
'>',
'',
$tagName
)
).$classes.$styles;
} else {
unset($content[0]);
}
unset($content[array_key_last($content)]);
$out = '';
foreach ($content as $c) {
$out .= $c.'>';
}
return $out;
}
protected function getClassesAndStyles(
array $attrs,
array $classes = [],
array $styles = []
):string {
// Get styles and classes from attributes
$attr_styles = $this->getInlineStyles($attrs);
$attr_classes = $this->getClasses($attrs);
if(array_key_exists('slug', $attrs) && $attrs['slug'] === 'footer') {
$classes[] = 'col';
}
// Merge with passed classes and styles
$styles = array_merge($attr_styles, $styles);
$classes = array_merge($attr_classes, $classes);
if (!empty(static::$pendingClass)) {
$classes = array_merge($classes, static::$pendingClass);
static::$pendingClass = [];
}
$classes = array_unique($classes);
$data = $this->getDataset($attrs);
// Build attribute strings
$class_string = !empty($classes) ? ' class="' . implode(' ', $classes) . '"' : '';
$style_string = !empty($styles) ? ' style="' . implode(';', $styles) . '"' : '';
$data_string = '';
if (!empty($data)) {
foreach ($data as $d => $v) {
if ($d === 'bg-small') {
$data_string .= ' data-bg-img';
}
$data_string .= sprintf(
' data-%s="%s"',
$d,
$v
);
}
}
$return = trim($class_string . $style_string . $data_string);
return ($return=='')? '' : ' '.$return;
}
/**
* @param string $spacing
*
* @return string
*/
protected function getPresetSpacing(string $spacing):string
{
return match ($spacing) {
'var:preset|spacing|20' => 1,
'var:preset|spacing|30' => 2,
'var:preset|spacing|40' => 3,
'var:preset|spacing|50' => 4,
'var:preset|spacing|60' => 5,
'var:preset|spacing|70' => 6,
'var:preset|spacing|80' => 7,
default => $spacing,
};
}
protected function getClasses(array $attrs):array
{
if (empty($attrs)) {
return [];
}
$classes = [];
foreach ($attrs as $key => $value) {
$class = $this->getClass($key, $value, $attrs);
if (is_string($class)) {
$class = explode(' ', $class);
}
$classes = array_merge($classes, $class);
}
return array_unique(array_filter($classes, function ($class) {
return $class!=='' && !str_starts_with($class, 'wp');
}));
}
protected function getClass(string $key, string|bool|array|int $value, array $attrs):string|array
{
//TODO: gradient
switch ($key) {
//Any additional classes the user adds
case 'className':
return match ($value) {
'is-style-floating' => 'always mobile fixed',
'is-style-fixed' => 'fixed bottom',
default => str_replace('is-style-', '', $value),
};
case 'contentPosition':
return $this->getContentPosition($value);
case 'term':
case 'taxonomy':
return jvbNoBase($value);
//Layout attributes
case 'layout':
return $this->getLayout($value, $attrs);
case 'align':
return !empty($value) ? 'align-'.$value : '';
case 'verticalAlignment':
switch ($value) {
case 'bottom':
$value = 'btm';
break;
case 'center':
$value = 'y-mid';
default:
}
return !empty($value) ? $value : '';
case 'isStackedOnMobile':
return ($value === true) ? 'stack-small' : '';
case 'justifyContent':
return !empty($value) ? 'j-'.$value : '';
case 'orientation':
return $value==='column' ? 'column' : '';
case 'width':
return $this->getWidth($value);
case 'dimRatio':
return $this->getDimRatio($value);
case 'overlayColor':
return $value;
break;
//Typography
case 'textAlign':
return !empty($value) ? 'text-'.$value : '';
case 'dropCap':
return $value === true ? 'drop-cap' : '';
//Media
case 'hasParallax':
return $value === true ? 'bg-parallax' : '';
case 'isRepeated':
return $value === true ? 'bg-repeat' : '';
//Style base:
case 'style':
return $this->getPresetStyles($value);
case 'fontSize':
$classes[] = 'font-'.$value;
return implode(' ', $classes);
case 'postLayout':
$classes[] = 'item-grid';
if (isset($attrs['columns']) && $attrs['columns']!== 3){
$classes[] = sprintf(
'split-%d',
$attrs['columns']
);
}
return $classes;
default:
if (JVB_TESTING && !is_admin() &&!in_array($key, $this->ignore)) {
// TESTING
jvbDump($attrs, '[getClass] '.$key);
}
return '';
}
}
/*** CLASS HELPERS ***/
private function getContentPosition(string $value):string
{
$classes = [];
$pos = explode(' ', $value);
foreach($pos as $p) {
switch ($p) {
case 'top':
$classes[] = 'top';
break;
case 'right':
$classes[] = 'right';
break;
case 'bottom':
$classes[] = 'btm';
break;
case 'left':
$classes[] = 'left';
break;
}
}
return implode(' ', $classes);
}
private function getLayout(array $value, array $attrs):array
{
// jvbDump($value, 'getLayout');
$classes = [];
$type = 'row';
$isRow = true;
//Determine type
if ((array_key_exists('type', $value) && !in_array($value['type'], ['flex', 'grid'])) ||
(array_key_exists('orientation', $value) && $value['orientation'] === 'vertical')) {
$type = 'col';
$isRow = false;
} elseif (array_key_exists('type', $value) && $value['type'] === 'grid') {
$type = 'item-grid';
$isRow = false;
if (array_key_exists('columnCount', $value) && $value['columnCount']!== 3) {
$classes[] = sprintf(
'split-%s',
$value['columnCount']
);
}
}
if (array_key_exists('justifyContent', $value) && !array_key_exists('contentPosition', $attrs)) {
switch ($value['justifyContent']) {
case 'right':
$classes[] = 'right';
break;
case 'center':
$classes[] = 'x-mid';
break;
case 'space-between':
$classes[] = 'x-btw';
break;
case 'left':
$classes[] = 'left';
break;
case 'space-evenly':
$classes[] = 'x-even';
break;
case 'space-around':
$classes[] = 'x-around';
break;
case 'stretch':
$classes[] = 'stretch';
}
} else {
$classes[] = 'left';
}
if (array_key_exists('verticalAlignment', $value)) {
switch ($value['verticalAlignment']) {
case 'bottom':
$classes[] = 'btm';
break;
case 'top':
$classes[] = 'top';
break;
case 'center':
$classes[] = 'y-mid';
break;
case 'space-between':
$classes[] = 'y-btw';
break;
case 'space-around':
$classes[] = 'y-around';
break;
case 'space-even':
$classes[] = 'y-even';
}
}
if (array_key_exists('flexWrap', $value)) {
if ($value['flexWrap'] === 'nowrap') {
$classes[] = 'nowrap';
}
}
$classes[] = $type;
return $classes;
}
private function getWidth(string $value):string
{
$value = (int)str_replace('%', '', $value);
return sprintf(
'width-%d',
match (true) {
$value <= 25 => '25',
$value <= 33 => '33',
$value <= 50 => '50',
$value <= 66 => '66',
$value <= 75 => '75',
default => 'full',
}
);
}
private function getDimRatio(string $value):string
{
if (is_numeric($value)) {
return sprintf(
'op-%d',
match (true) {
$value <= 14 => '1',
$value <= 28 => '2',
$value <= 42 => '3',
$value <= 56 => '45',
$value <= 70 => '4',
$value <= 84 => '5',
default => '6',
}
);
}
return '';
}
private function getPresetStyles(array $value):string
{
$classes = [];
//Margin and Padding
if (array_key_exists('spacing', $value)) {
$classes = array_merge($classes, $this->buildSpacingClasses($value));
}
if (array_key_exists('color', $value)) {
if (array_key_exists('duotone', $value['color'])) {
$preset = explode('|', $value['color']['duotone']);
$preset = $preset[array_key_last($preset)];
if (str_contains($preset, '-')) {
$preset = explode('-', $preset);
} else {
$preset = [$preset];
}
$classes[] = 'duotone';
foreach ($preset as $p) {
$classes[] = $p;
}
}
}
if (array_key_exists('fontSize', $value)) {
if (in_array($value['fontSize'], ['small', 'large', 'extra-large', 'huge'])) {
$classes[] = 'font-'.$value['fontSize'];
}
if (in_array('fontWeight', $value)) {
$classes[] = 'text-'.$value['fontWeight'];
}
if (in_array('textTransform', $value)) {
if (in_array($value['textTransform'], ['uppercase', 'capitalize', 'lowercase'])) {
$classes[] = $value['textTransform'];
}
}
}
return implode(' ', $classes);
}
private function buildSpacingClasses(array $value):array
{
$classes = [];
foreach (['margin' => 'm', 'padding'=>'p'] as $search => $c) {
if (array_key_exists($search, $value['spacing'])) {
$directions = [];
// Collect ONLY preset spacing values for classes
foreach ($value['spacing'][$search] as $direction => $size) {
$presetSize = $this->getPresetSpacing($size);
if ($presetSize) {
$directions[$direction] = $presetSize;
}
// Non-preset values are skipped here and handled by inline styles below
}
if (empty($directions)) {
continue;
}
// Check what directions we have
$hasTop = isset($directions['top']);
$hasBottom = isset($directions['bottom']);
$hasLeft = isset($directions['left']);
$hasRight = isset($directions['right']);
// Check if axes match
$xMatch = $hasLeft && $hasRight && $directions['left'] === $directions['right'];
$yMatch = $hasTop && $hasBottom && $directions['top'] === $directions['bottom'];
// All 4 directions exist and match → p-3
if ($hasTop && $hasBottom && $hasLeft && $hasRight &&
count(array_unique($directions)) === 1) {
$classes[] = $c . '-' . reset($directions);
}
// Both axes match → px-3 py-2
elseif ($xMatch && $yMatch) {
$classes[] = $c . 'x-' . $directions['left'];
$classes[] = $c . 'y-' . $directions['top'];
}
// Only X axis matches → px-3 (+ individual for top/bottom)
elseif ($xMatch) {
$classes[] = $c . 'x-' . $directions['left'];
if ($hasTop) {
$classes[] = $c . 't-' . $directions['top'];
}
if ($hasBottom) {
$classes[] = $c . 'b-' . $directions['bottom'];
}
}
// Only Y axis matches → py-3 (+ individual for left/right)
elseif ($yMatch) {
$classes[] = $c . 'y-' . $directions['top'];
if ($hasLeft) {
$classes[] = $c . 'l-' . $directions['left'];
}
if ($hasRight) {
$classes[] = $c . 'r-' . $directions['right'];
}
}
// No matches - individual directions
else {
foreach ($directions as $direction => $size) {
$dir = match($direction) {
'top' => 't',
'bottom' => 'b',
'left' => 'l',
'right' => 'r',
default => $direction
};
$classes[] = $c . $dir . '-' . $size;
}
}
}
}
return $classes;
}
protected function getInlineStyles(array $attrs):array
{
if (empty($attrs)) {
return [];
}
$styles = [];
foreach ($attrs as $key => $value) {
$style = $this->getStyle($key, $value, $attrs);
if (is_string($style)) {
$style = [$style];
}
$styles = array_merge($styles, $style);
}
return $styles;
}
protected function getStyle(string $key, string|bool|array|int $value, array $attrs):array|string
{
$styles = [];
switch ($key) {
// Font family settings
case 'size':
return $this->getIconSizeStyle($value);
case 'fontFamily':
return $this->getFontFamilyStyle($value);
// Icon color (for icon blocks)
case 'iconColorValue':
return $this->getColorStyle($value);
// Minimum height settings
case 'minHeight':
return $this->getMinHeightStyle($value, $attrs);
// Background URL (for cover, media blocks)
case 'url':
if (!empty($value) && str_starts_with($value, 'http')) {
return 'background-image: url('.$value.')';
}
break;
// Focal point for background images
case 'focalPoint':
return $this->getFocalPointStyle($value);
// Complex style object
case 'style':
return $this->extractStyles($value, $attrs);
case 'dimRatio':
return $this->getDimRatioStyle($value, $attrs);
// Custom styles (any other attributes that need inline styling)
case 'backgroundType':
if ($value === 'video' && isset($attrs['backgroundUrl'])) {
// Don't set a background image for videos - it will be handled by the video element
} elseif (isset($attrs['backgroundUrl'])) {
return 'background-image: url('.$attrs['backgroundUrl'].')';
}
break;
case 'backgroundColor':
case 'borderColor':
case 'textColor':
$type = str_replace('Color', '-color', $key);
$type = str_replace('text-', '', $type);
if (isset($attrs['border']['width']) && $key === 'borderColor') {
break;
}
return $this->getColorStyle($value, $type);
// Any other attributes that need direct styling
default:
if (JVB_TESTING && !is_admin() && !in_array($key, $this->ignore)) {
//TESTING
jvbDump($attrs, '[getStyle] '.$key);
}
// No default inline styles
break;
}
return $styles;
}
private function getIconSizeStyle(string $value):array
{
$values = explode(' ', $value);
$styles = [];
foreach ($values as $v) {
switch ($value) {
case 'has-small-icon-size':
$styles[] = '--w:var(--txt-x-small)';
break;
case 'has-large-icon-size':
$styles[] = '--w:var(--txt-large)';
break;
case 'has-huge-icon-size':
$styles[] = '--w:var(--txt-xx-large)';
break;
default:
if (JVB_TESTING) {
jvbDump($value, 'No preset found for size: '.print_r($value, true));
}
}
}
return $styles;
}
private function getFontFamilyStyle(string $value):string
{
return match($value) {
'body' => 'font-family: var(--body)',
'heading' => 'font-family: var(--heading)',
default => sprintf('font-family: %s', $value)
};
}
private function getColorStyle(string $value, ?string $type = 'color'):string
{
if (!in_array($type, ['color', 'background','background-color','border-color'])) {
$type = null;
}
return sprintf(
'%s%s',
$type ? $type.': ' : '',
$this->getColor($value)
);
}
private function getMinHeightStyle(string $value, array $attrs):string
{
$out = '';
if (!empty($value)) {
if (isset($attrs['minHeightUnit'])) {
$out = sprintf(
'min-height: %s%s',
$value,
$attrs['minHeightUnit']
);
} else {
$out = sprintf(
'min-height: %spx',
$value
);
}
}
return $out;
}
private function getFocalPointStyle(array $value):string
{
jvbDump($value, 'Focal Point');
$x = array_key_exists('x', $value) ? $value['x'] * 100 : 'center';
$y = array_key_exists('y', $value) ? $value['y'] * 100 : 'center';
$y = $x === $y ? '' : ' '.$y;
return sprintf(
'background-position:%s%s',
$x,
$y
);
}
private function extractStyles(array $value, array $attrs):array
{
$styles = [];
foreach ($value as $k => $v) {
switch ($k) {
case 'border':
$styles = array_merge($styles, $this->getBorderStyle($v, $attrs));
break;
case 'color':
if (isset($v['background'])) {
$styles[] = $this->getColorStyle($v['background'], 'background-color');
}
if (isset($v['text'])) {
$styles[] = $this->getColorStyle($v['text']);
}
if (isset($v['gradient'])) {
jvbDump($v, 'Gradient');
}
break;
case 'layout':
$styles = array_merge($styles, $this->getLayoutStyle($v, $attrs));
break;
case 'typography':
$styles = array_merge($styles, $this->getTypographyStyle($v, $attrs));
break;
case 'spacing':
if (isset($v['blockGap'])) {
if (is_array($v['blockGap'])) {
$inner = [];
foreach ($v['blockGap'] as $gap) {
$inner[] = sprintf(
'var(--sp%s)',
$this->getPresetSpacing($gap)
);
}
if (!empty($inner)) {
$styles[] = sprintf(
'--gap: %s',
implode(' ', $inner)
);
}
} else {
$styles[] = '--gap: var(--sp'.$this->getPresetSpacing($v['blockGap']).')';
}
}
// Don't duplicate margin/padding that's handled by classes
// Only add specific CSS values here that wouldn't work well as classes
if (isset($v['margin'])) {
foreach ($v['margin'] as $direction => $size) {
if (!str_contains($size, 'var:preset')) {
$styles[] = 'margin-'.$direction.': '.$size;
}
}
}
if (isset($v['padding'])) {
foreach($v['padding'] as $dir => $size) {
if (!str_contains($size, 'var:preset')) {
$styles[] = 'padding-'.$dir.': '.$size;
}
}
}
break;
case 'background':
if (array_key_exists('backgroundImage', $v)) {
$data = Image::getData($v['backgroundImage']['id']);
if (!empty($data) && array_key_exists('tiny', $data)) {
$styles[] = sprintf(
'background-image: url(%s)',
$data['tiny']
);
}
}
break;
case 'dimensions':
foreach ($v as $sk => $sv) {
if ($sk === 'minHeight') {
$styles[] = sprintf(
'min-height: %s',
$sv
);
} else {
jvbDump('No config set for dimension '.$sk.': '.print_r($sv, true));
}
}
break;
case 'elements':
if (!empty($v)) {
// Generate a unique class tied to this block instance
$uid = 'b-'.substr(md5(serialize($attrs)), 0, 8);
$this->extractElementStyles($v, $uid);
// We need the uid added as a class — store it for getClassesAndStyles to pick up
static::$pendingClass[] = $uid;
}
break;
default:
jvbDump('No config set for '.$k.': '.print_r($v, true));
}
}
return $styles;
}
private function getBorderStyle(array $border, array $attrs):array
{
$styles = [];
if (isset($border['radius'])) {
$styles[] = 'border-radius: '.$border['radius'];
}
if (isset($border['width']) && isset($attrs['borderColor'])) {
$st = $border['style'] ?? 'solid';
$styles[] = sprintf(
'border: %s %s %s',
$border['width'],
$st,
$this->getColor($attrs['borderColor'])
);
} elseif (isset($border['width'])) {
$styles[] = 'border-width: '.$border['width'];
} elseif (isset($border['style'])) {
$styles[] = 'border-style: '.$border['style'];
}
return $styles;
}
private function getLayoutStyle(array $layout, array $attrs):array
{
$styles = [];
// jvbDump($layout);
foreach ($layout as $l => $option) {
switch ($l) {
case 'selfStretch':
if ($option === 'fixed' && isset($layout['selfStretchValue'])) {
$styles[] = 'width: '.$layout['selfStretchValue'];
} elseif ($option === 'fill') {
$styles[] = 'flex:1';
}
break;
default:
$ignore = [
'selfStretchValue',
'flexSize',
];
if (JVB_TESTING && !in_array($l, $ignore)) {
jvbDump($l, 'No layout style set for: ');
}
// case 'type':
// if ($option === 'grid' && $value['layout']['columnCount'] !== 3) {
// $styles[] = sprintf(
// 'grid-template-columns: repeat(1fr, %s)',
// $value['layout']['columnCount']
// );
// }
// break;
}
}
return $styles;
}
private function getTypographyStyle(array $typography, array $attrs):array
{
$styles = [];
if (isset($typography['fontSize'])) {
$styles[] = 'font-size: '.$typography['fontSize'];
}
if (isset($typography['fontWeight'])) {
$styles[] = 'font-weight: '.$typography['fontWeight'];
}
if (isset($typography['textDecoration'])) {
$styles[] = 'text-decoration: '.$typography['textDecoration'];
}
if (isset($typography['textTransform'])) {
$styles[] = 'text-transform: '.$typography['textTransform'];
}
if (isset($typography['letterSpacing'])) {
$styles[] = 'letter-spacing: '.$typography['letterSpacing'];
}
if (isset($typography['lineHeight'])) {
$styles[] = 'line-height: '.$typography['lineHeight'];
}
return $styles;
}
private function extractElementStyles(array $elements, string $uid):void
{
foreach ($elements as $element => $states) {
$selector = match($element) {
'link' => "a",
'heading' => "h1,h2,h3,h4,h5,h6",
'button' => "button,.button",
default => $element,
};
foreach ($states as $state => $rules) {
$css = [];
$fullSelector = str_starts_with($state, ':')
? ".{$uid} {$selector}{$state}"
: ".{$uid} {$selector}";
if (isset($rules['color']['text'])) {
$css[] = 'color: '.$this->getColor($rules['color']['text']);
}
if (isset($rules['color']['background'])) {
$css[] = 'background-color: '.$this->getColor($rules['color']['background']);
}
if (!empty($css)) {
static::$pendingStyles[] = $fullSelector.' { '.implode('; ', $css).' }';
}
}
}
}
public function maybeOutputCustomStyles(): string
{
if (empty(static::$pendingStyles)) return '';
$out = '';
static::$pendingStyles = [];
return $out;
}
private function getDimRatioStyle(int $value, array $attrs):string
{
//TODO: This likely isn't working correctly
jvbDump($value, 'dimRatio');
jvbDump($attrs, 'dimRatio attrs');
$s = '';
if (!array_key_exists('overlayColor', $attrs)) {
$s = 'background-color: rgba(var(--base-rgb), ';
if ($value <= 14) {
$s .= 'var(--op-1));';
} elseif ($value <= 28) {
$s .= 'var(--op-2));';
} elseif ($value <= 42) {
$s .= 'var(--op-3));';
} elseif ($value <= 56) {
$s .= 'var(--op-45));';
} elseif ($value <= 70) {
$s .= 'var(--op-4));';
} elseif ($value <= 84) {
$s .= 'var(--op-5));';
} else {
$s .= 'var(--op-6));';
}
}
return $s;
}
protected function getDataset(array $attrs):array
{
$dataset = [];
if (array_key_exists('style', $attrs)) {
if (array_key_exists('background', $attrs['style'])){
if (array_key_exists('backgroundImage', $attrs['style']['background'])) {
$id = $attrs['style']['background']['backgroundImage']['id']??false;
if ($id) {
$data = Image::getData($id);
$dataset['bg-small'] = $data['small'];
$dataset['bg-med'] = $data['medium'];
$dataset['bg-large'] = $data['large'];
}
}
}
}
return $dataset;
}
public function formatImage(int $ID = 0, string $start = 'tiny', string $replace = 'large'):string
{
if ($ID === 0) {
$ID = $this->imageID($ID);
}
if ($ID === 0) {
return '';
}
return jvbFormatImage($ID, $start, $replace);
}
protected function checkAttrs(string $test, array $attrs):bool
{
return array_key_exists($test, $attrs) && $attrs[$test]===true;
}
protected function getColor(string $value):string
{
$defaults = apply_filters('jvbColours', ['base', 'contrast', 'action', 'secondary']);
foreach ($defaults as $default) {
if (str_starts_with($value, $default)) {
return 'var(--'.$value.')';
}
}
return $value;
}
protected function counter(string $key):void
{
if (!array_key_exists($key, static::$counters)) {
static::$counters[$key] = 1;
} else {
static::$counters[$key]++;
}
}
}