| | |
| | | namespace JVBase\meta; |
| | | |
| | | use DateTime; |
| | | use JVBase\registrar\Registrar; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | |
| | | // Helper Methods |
| | | // ───────────────────────────────────────────────────────────── |
| | | |
| | | protected static function fieldWrap(string $name, string $content, array $config): string |
| | | public static function fieldWrap(string $name, string $content, array $config): string |
| | | { |
| | | $classes = static::buildClasses($config); |
| | | $datasets = static::buildDatasets($config); |
| | |
| | | '<div class="%s" data-field="%s" data-field-type="%s"%s>', |
| | | $classes, |
| | | $name, |
| | | $config['type'], |
| | | str_replace('_', '-', $config['type']), |
| | | $datasets |
| | | ); |
| | | |
| | | $output .= static::buildCharacterLimit($config); |
| | | $output .= static::buildLabel($name, $config); |
| | | if (!array_key_exists('skipInput', $config)) { |
| | | $output .= static::buildInput($content); |
| | | } else { |
| | | $output .= $content; |
| | | } |
| | | |
| | | $output .= static::buildHint($config); |
| | |
| | | ); |
| | | } |
| | | |
| | | protected static function buildCharacterLimit(array $config): string |
| | | { |
| | | $type = $config['type'] ?? 'text'; |
| | | if (in_array($type, ['upload', 'gallery', 'selector'])) { |
| | | return ''; |
| | | } |
| | | if (empty($config['maxlength'])) { |
| | | return ''; |
| | | } |
| | | return sprintf( |
| | | '<span class="char-limit"><span class="current">0</span> / <span class="limit">%s</span></span>', |
| | | esc_html($config['maxlength']) |
| | | ); |
| | | } |
| | | |
| | | protected static function buildLabel(string $name, array $config):string |
| | | { |
| | | if (!empty($config['label'])) { |
| | |
| | | return ''; |
| | | } |
| | | |
| | | protected static function buildInput(string $content):string |
| | | public static function buildInput(string $content):string |
| | | { |
| | | return sprintf( |
| | | '<div class="field-input-wrapper"> |
| | |
| | | |
| | | protected static function renderText(string $name, mixed $value, array $config): string |
| | | { |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="%s"%s%s />', |
| | | $config['subtype']??'text', |
| | |
| | | { |
| | | $attrs = static::inputAttrs($name, $config); |
| | | |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="number"%s%s />', |
| | | $value, |
| | |
| | | |
| | | $attrs = static::inputAttrs($name, $config); |
| | | |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<div class="quantity"> |
| | | <button type="button" class="decrease" title="%s" aria-label="Decrease %s">%s</button> |
| | |
| | | protected static function renderEmail(string $name, mixed $value, array $config): string |
| | | { |
| | | $config['validate'] = 'email'; |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="email"%s%s />', |
| | | $value, |
| | |
| | | protected static function renderUrl(string $name, mixed $value, array $config): string |
| | | { |
| | | $config['validate'] = 'url'; |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="url"%s%s />', |
| | | $value, |
| | |
| | | protected static function renderTel(string $name, mixed $value, array $config): string |
| | | { |
| | | $config['validate'] = 'phone'; |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="tel"%s%s />', |
| | | $value, |
| | |
| | | } |
| | | } |
| | | |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="date"%s%s />', |
| | | $value, |
| | |
| | | |
| | | protected static function renderTime(string $name, mixed $value, array $config): string |
| | | { |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="time"%s%s />', |
| | | $value, |
| | |
| | | |
| | | protected static function renderDatetime(string $name, mixed $value, array $config): string |
| | | { |
| | | $value = ($value === '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<input type="datetime-local"%s%s />', |
| | | $value, |
| | |
| | | $optionsHtml = ''; |
| | | if (empty($config['required'])) { |
| | | $optionsHtml .= '<option value="">— Select —</option>'; |
| | | } else { |
| | | $optionsHtml .= '<option value="" disabled selected hidden>— Select —</option>'; |
| | | } |
| | | |
| | | foreach ($options as $optValue => $optLabel) { |
| | | $optionsHtml .= sprintf( |
| | | '<option value="%s"%s>%s</option>', |
| | | esc_attr($optValue), |
| | | selected($value, $optValue), |
| | | selected($value, $optValue, false), |
| | | esc_html($optLabel) |
| | | ); |
| | | } |
| | |
| | | $optValue, |
| | | esc_attr($optValue), |
| | | $checked, |
| | | $name, |
| | | esc_attr($name), |
| | | $optValue, |
| | | esc_html($optLabel) |
| | | ); |
| | |
| | | |
| | | protected static function renderRadio(string $name, mixed $value, array $config): string |
| | | { |
| | | $options = $config['options'] ?? []; |
| | | $options = $config['options'] ?? []; |
| | | $inputClass = !empty($config['inputClass']) ? ' class="' . esc_attr($config['inputClass']) . '"' : ''; |
| | | $idPrefix = $config['idPrefix'] ?? ''; |
| | | |
| | | $radios = sprintf( |
| | | '<fieldset> |
| | | <legend>%s%s</legend>', |
| | | <legend>%s%s</legend>', |
| | | array_key_exists('label', $config) ? esc_html($config['label']) : 'Select an option', |
| | | array_key_exists('required', $config) && $config['required']===true ? '<span class="required" aria-label="required">*</span>' : '' |
| | | array_key_exists('required', $config) && $config['required'] === true |
| | | ? '<span class="required" aria-label="required">*</span>' : '' |
| | | ); |
| | | |
| | | foreach ($options as $optValue => $optLabel) { |
| | | foreach ($options as $optValue => $optConfig) { |
| | | if (is_array($optConfig)) { |
| | | $optLabel = $optConfig['label'] ?? $optValue; |
| | | $optIcon = $optConfig['icon'] ?? null; |
| | | $optDisabled = !empty($optConfig['disabled']) ? ' disabled' : ''; |
| | | } else { |
| | | $optLabel = $optConfig; |
| | | $optIcon = null; |
| | | $optDisabled = ''; |
| | | } |
| | | |
| | | $labelContent = $optIcon |
| | | ? jvbDashIcon($optIcon) |
| | | : '<span>' . esc_html($optLabel) . '</span>'; |
| | | |
| | | $optId = esc_attr($idPrefix . $name . '-' . $optValue); |
| | | |
| | | $radios .= sprintf( |
| | | ' |
| | | <input type="radio" name="%s" value="%s"%s /> |
| | | <label class="radio-option" for="%s-%s"> |
| | | <span>%s</span> |
| | | </label>', |
| | | '<input type="radio" name="%s" id="%s" value="%s"%s%s%s /> |
| | | <label class="radio-option" for="%s" title="%s">%s</label>', |
| | | esc_attr($name), |
| | | $optId, |
| | | esc_attr($optValue), |
| | | checked($value, $optValue), |
| | | $name, |
| | | $optValue, |
| | | esc_html($optLabel) |
| | | checked($value, $optValue, false), |
| | | $optDisabled, |
| | | $inputClass, |
| | | $optId, |
| | | esc_html($optLabel), |
| | | $labelContent |
| | | ); |
| | | } |
| | | |
| | |
| | | $content = $config['content']; |
| | | |
| | | $config['data']['content'] = $content; |
| | | $plural = |
| | | JVB_CONTENT[$content]['plural'] |
| | | ?? JVB_TAXONOMY[$content]['plural'] |
| | | ?? JVB_USER[$content]['plural'] |
| | | ?? str_replace('_', ' ', $content) . 's'; |
| | | |
| | | $singular = JVB_CONTENT[$content]['singular'] |
| | | ?? JVB_TAXONOMY[$content]['singular'] |
| | | ?? JVB_USER[$content]['singular'] |
| | | ?? str_replace('_', ' ',$config['content']); |
| | | $registrar = Registrar::getInstance($content); |
| | | $plural = $registrar->getPlural()??str_replace('_', ' ', $content).'s'; |
| | | $singular = $registrar->getSingular()??str_replace('_', ' ', $content); |
| | | } |
| | | if ($config['limit'] > 0) { |
| | | $config['data']['limit'] = $config['limit']; |
| | | $config['data']['max-files'] = $config['limit']; |
| | | } |
| | | |
| | | $attachmentIds = static::parseIds($value); |
| | |
| | | |
| | | $input .= '<div class="file-error"></div>'; |
| | | $input .= jvbRenderProgressBar('', false, true, true); |
| | | $input .= '</div>'; |
| | | |
| | | $input .= '</div>'; // closes .file-upload-wrapper |
| | | |
| | | if ($config['destination'] === 'post_group') { |
| | | $input .= static::renderUploadGroupAreaStart($config, $plural, $singular); |
| | |
| | | $input .= static::renderUploadGroupAreaEnd($config, $plural, $singular); |
| | | } |
| | | |
| | | $input .= '</div>'; // closes .file-upload-container |
| | | |
| | | $input .= sprintf( |
| | | '<input type="hidden" name="%s" value="%s">', |
| | | esc_attr($name), |
| | | esc_attr(!empty($attachmentIds) ? implode(',', $attachmentIds) : '') |
| | | ); |
| | | |
| | | unset($config['description']); |
| | | unset($config['label']); |
| | | return static::fieldWrap($name, $input, $config); |
| | |
| | | 'type' => $config['subtype'], |
| | | ], $config); |
| | | |
| | | $icon = match ($config['subtype']) { |
| | | 'taxonomy' => JVB_TAXONOMY[$config['taxonomy']]['icon'] ?? jvbDefaultIcon(), |
| | | 'content' => JVB_CONTENT[$config['content']]['icon'] ?? jvbDefaultIcon(), |
| | | 'user' => JVB_USER[$config['role']]['icon'] ?? 'user', |
| | | default => jvbDefaultIcon(), |
| | | }; |
| | | $registrar = Registrar::getInstance($config[$config['subtype']]); |
| | | $icon = jvbDefaultIcon(); |
| | | if ($registrar){ |
| | | $icon = $registrar->getIcon()??jvbDefaultIcon(); |
| | | } |
| | | |
| | | $containerId = sprintf('%s-%s-selector', $name, $config['subtype']); |
| | | |
| | | $input = sprintf( |
| | | '<div class="row btw"> |
| | | <label for="%s-autocomplete">%s<span>%s</span></label>', |
| | | <input type="hidden" name="%s" value="%s"> |
| | | <label for="%s-autocomplete">%s<span>%s</span></label>', |
| | | esc_attr($name), |
| | | esc_attr(!empty($ids) ? implode(',', $ids) : ''), |
| | | esc_attr($name), |
| | | jvbIcon($icon), |
| | | esc_html($config['label']), |
| | |
| | | } |
| | | $plural = static::getPlural($config); |
| | | $input .= sprintf( |
| | | '<div class="selected-item row" role="region" aria-label="Selected %s"></div>', |
| | | '<div class="selected-items row" role="region" aria-label="Selected %s"></div>', |
| | | $plural[1]??'' |
| | | ); |
| | | |
| | | $input .= '</div>'; //Close the first div.row.btw |
| | | |
| | | $config['type'] = 'selector'; |
| | | unset($config['label']); |
| | | unset($config['description']); |
| | | unset($config['hint']); |
| | | $config['skipInput'] = true; |
| | | return static::fieldWrap($containerId, $input, $config); |
| | | return static::fieldWrap($name, $input, $config); |
| | | } |
| | | |
| | | protected static function getPlural(array $config):array |
| | | { |
| | | $single = $plural = ''; |
| | | switch ($config['subtype']) { |
| | | case 'taxonomy': |
| | | if (array_key_exists($config['taxonomy'], JVB_TAXONOMY)) { |
| | | $single = JVB_TAXONOMY[$config['taxonomy']]['singular']; |
| | | $plural = JVB_TAXONOMY[$config['taxonomy']]['plural']; |
| | | $registrar = Registrar::getInstance($config['taxonomy']); |
| | | if ($registrar) { |
| | | $single = $registrar->getSingular(); |
| | | $plural = $registrar->getPlural(); |
| | | } else { |
| | | $taxonomy = get_taxonomy($config['taxonomy']); |
| | | if (!$taxonomy) { |
| | | return []; |
| | | if ($taxonomy) { |
| | | $single = $taxonomy->labels->singular_name; |
| | | $plural = $taxonomy->labels->name; |
| | | } |
| | | $single = $taxonomy->labels->singular_name; |
| | | $plural = $taxonomy->labels->name; |
| | | } |
| | | break; |
| | | case 'content': |
| | | if (array_key_exists($config['content'], JVB_CONTENT)) { |
| | | $single = JVB_CONTENT[$config['content']]['singular']; |
| | | $plural = JVB_CONTENT[$config['content']]['plural']; |
| | | $registrar = Registrar::getInstance($config['content']); |
| | | if ($registrar) { |
| | | $single = $registrar->getSingular(); |
| | | $plural = $registrar->getPlural(); |
| | | } else { |
| | | $postType = get_post_type_object($config['content']); |
| | | if (!$postType) { |
| | | return ''; |
| | | if ($postType) { |
| | | $single = $postType->labels->singular_name; |
| | | $plural = $postType->labels->name; |
| | | } |
| | | $single = $postType->labels->singular_name; |
| | | $plural = $postType->labels->name; |
| | | |
| | | } |
| | | break; |
| | | |
| | | case 'user': |
| | | if (array_key_exists($config['user'], JVB_USER)) { |
| | | $single = JVB_USER[$config['user']]['singular']; |
| | | $plural = JVB_USER[$config['user']]['plural']; |
| | | $registrar = Registrar::getInstance($config['user']); |
| | | |
| | | if ($registrar) { |
| | | $single = $registrar->getSingular(); |
| | | $plural = $registrar->getPlural(); |
| | | } else { |
| | | $user = get_role($config['user']); |
| | | if (!$user) { |
| | | return ''; |
| | | if ($user) { |
| | | $single = 'User'; |
| | | $plural = 'Users'; |
| | | } |
| | | $single = 'User'; |
| | | $plural = 'Users'; |
| | | } |
| | | break; |
| | | } |
| | |
| | | { |
| | | switch ($config['subtype']) { |
| | | case 'taxonomy': |
| | | if (array_key_exists($config['taxonomy'], JVB_TAXONOMY)) { |
| | | $single = JVB_TAXONOMY[$config['taxonomy']]['singular']; |
| | | $plural = JVB_TAXONOMY[$config['taxonomy']]['plural']; |
| | | $registrar = Registrar::getInstance($config['taxonomy']); |
| | | if ($registrar) { |
| | | $single = $registrar->getSingular(); |
| | | $plural = $registrar->getPlural(); |
| | | } else { |
| | | $taxonomy = get_taxonomy($config['taxonomy']); |
| | | if (!$taxonomy) { |
| | |
| | | $plural = $taxonomy->labels->name; |
| | | } |
| | | $attr = sprintf( |
| | | ' data-taxonomy="%s" data-single="%s" data-plural="%s', |
| | | ' data-taxonomy="%s" data-single="%s" data-plural="%s"', |
| | | $config['taxonomy'], |
| | | $single, |
| | | $plural |
| | | ); |
| | | break; |
| | | case 'content': |
| | | if (array_key_exists($config['content'], JVB_CONTENT)) { |
| | | $single = JVB_CONTENT[$config['content']]['singular']; |
| | | $plural = JVB_CONTENT[$config['content']]['plural']; |
| | | $registrar = Registrar::getInstance($config['content']); |
| | | if ($registrar) { |
| | | $single = $registrar->getSingular(); |
| | | $plural = $registrar->getPlural(); |
| | | } else { |
| | | $postType = get_post_type_object($config['content']); |
| | | if (!$postType) { |
| | |
| | | $plural = $postType->labels->name; |
| | | } |
| | | $attr = sprintf( |
| | | ' data-content="%s" data-single="%s" data-plural="%s', |
| | | ' data-content="%s" data-single="%s" data-plural="%s"', |
| | | $config['content'], |
| | | $single, |
| | | $plural |
| | |
| | | break; |
| | | |
| | | case 'user': |
| | | if (array_key_exists($config['user'], JVB_USER)) { |
| | | $single = JVB_USER[$config['user']]['singular']; |
| | | $plural = JVB_USER[$config['user']]['plural']; |
| | | $registrar = Registrar::getInstance($config['user']); |
| | | if ($registrar){ |
| | | $single = $registrar->getSingular(); |
| | | $plural = $registrar->getPlural(); |
| | | } else { |
| | | $user = get_role($config['user']); |
| | | if (!$user) { |
| | |
| | | $plural = 'Users'; |
| | | } |
| | | $attr = sprintf( |
| | | ' data-user="%s" data-single="%s" data-plural="%s', |
| | | ' data-user="%s" data-single="%s" data-plural="%s"', |
| | | $config['user'], |
| | | $single, |
| | | $plural |
| | |
| | | $dataAttrs[] = 'data-selected="'.esc_attr(implode(',',$selected)).'"'; |
| | | } |
| | | if ($config['autocomplete']) { |
| | | $dataAttrs[] = 'autocomplete'; |
| | | $dataAttrs[] = 'data-autocomplete'; |
| | | } |
| | | if (array_key_exists('hidden', $config) && $config['hidden']) { |
| | | $dataAttrs[] = 'hidden'; |
| | |
| | | jvbIcon('plus-square') |
| | | ); |
| | | } |
| | | protected static function buildSelectorAutocomplete(string $name, array $config):string |
| | | { |
| | | return sprintf( |
| | | '<input type="hidden" id="%s-autocomplete" autocomplete="off" data-ignore data-autocomplete> |
| | | <p class="message" hidden aria-live="polite">{ <span>Loading items</span> }</p> |
| | | <div class="auto-wrapper" hidden><ul class="search-results"></ul><button class="submit-term" hidden data-ignore><strong>Create: </strong> "<span></span>"</button></div>', |
| | | $name |
| | | ); |
| | | } |
| | | protected static function buildSelectorAutocomplete(string $name, array $config): string |
| | | { |
| | | return sprintf( |
| | | '<input type="search" id="%s-autocomplete" autocomplete="off" data-ignore data-autocomplete> |
| | | <p class="message" hidden aria-live="polite">{ <span>Loading items</span> }</p> |
| | | <div class="auto-wrapper" hidden><ul class="search-results"></ul><button class="submit-term" hidden data-ignore><strong>Create: </strong> "<span></span>"</button></div>', |
| | | esc_attr($name) |
| | | ); |
| | | } |
| | | |
| | | protected static function renderTaxonomy(string $name, mixed $value, array $config): string |
| | | { |
| | |
| | | $input .= sprintf( |
| | | '<button type="button" class="button add-tag">%s<span>%s</span></button></div>', |
| | | jvbIcon('plus'), |
| | | $field['add_label']??'Add' |
| | | $config['add_label']??'Add' |
| | | ); |
| | | |
| | | //Tag Display |
| | |
| | | } |
| | | protected static function renderTagItems(array $fields, mixed $value, string $name, string $tagFormat):string |
| | | { |
| | | if ($value === '') { |
| | | if (!$value || $value === '') { |
| | | return ''; |
| | | } |
| | | if (is_string($value)) { |
| | |
| | | |
| | | $out = sprintf( |
| | | '<div class="tag-item"%s><span class="tag-label">%s</span>', |
| | | ($index) ? ' data-index="'.$index.'"' : '', |
| | | ($index !== null) ? ' data-index="'.$index.'"' : '', |
| | | $tagText |
| | | ); |
| | | |
| | | foreach ($fields as $fieldName => $fieldConfig) { |
| | | $value = $values[$fieldName]??''; |
| | | $fullName = (!$index) ? $fieldName : sprintf('%s:%s:%s', $name, $index, $fieldName); |
| | | $fullName = ($index === null) ? $fieldName : sprintf('%s:%s:%s', $name, $index, $fieldName); |
| | | |
| | | $out .= sprintf( |
| | | '<input type="hidden" |
| | | name="%s" |
| | | value="%s" |
| | | data-field="%s" |
| | | data-field-type="%s" />', |
| | | name="%s" |
| | | value="%s" |
| | | data-field="%s" |
| | | data-field-type="%s" |
| | | id="%s" />', |
| | | esc_attr($fullName), |
| | | esc_attr($value), |
| | | esc_attr($fieldName), |
| | | esc_attr($fieldConfig['type']) |
| | | ); |
| | | |
| | | $out .= sprintf( |
| | | '<button type="button" class="remove-tag" aria-label="Remove">%s</button>', |
| | | jvbIcon('x') |
| | | esc_attr($fieldConfig['type']), |
| | | esc_attr($fullName) |
| | | ); |
| | | } |
| | | |
| | | $out .= sprintf( |
| | | '<button type="button" class="remove-tag" aria-label="Remove">%s</button>', |
| | | jvbIcon('x') |
| | | ); |
| | | $out .='</div>'; |
| | | return $out; |
| | | } |
| | | protected static function getTagDisplayText(array $fields, mixed $values, string $tagFormat):string |
| | | { |
| | | if (empty($data)) { |
| | | if (empty($values)) { |
| | | return 'New Item'; |
| | | } |
| | | |
| | |
| | | $firstKey = array_key_first($fields); |
| | | return $values[$firstKey] ?? 'New Item'; |
| | | case 'all_fields': |
| | | $values = array_filter(array_values($data)); |
| | | $values = array_filter(array_values($values)); |
| | | return implode(', ', $values) ?: 'New Item'; |
| | | default: |
| | | if (strpos($tagFormat, '{') !== false) { |
| | |
| | | { |
| | | $fields = $config['fields'] ?? []; |
| | | $rows = is_array($value) ? $value : []; |
| | | if(array_key_exists('row_label', $config)) { |
| | | $config['data']['label'] = esc_attr($config['row_label']); |
| | | if(array_key_exists('add_label', $config)) { |
| | | $config['data']['label'] = esc_attr($config['add_label']); |
| | | } |
| | | |
| | | $input = sprintf( |
| | |
| | | array_key_exists('add_label', $config) ? $config['add_label'] : 'Add Item' |
| | | ); |
| | | |
| | | unset($config['label']); |
| | | return static::fieldWrap($name, $input, $config); |
| | | } |
| | | protected static function renderRepeaterRow(array $fields, array $values, int|string $index, string $name, string $rowTitle='New Item'):string |
| | |
| | | |
| | | foreach ($fields as $fieldName => $fieldConfig) { |
| | | $fieldValue = $values[$fieldName] ?? ''; |
| | | $fullName = "{$name}:{$fieldName}"; |
| | | $fullName = array_key_exists('wrap', $config) ? $fieldName : "{$name}:{$fieldName}"; |
| | | $output .= static::render($fullName, $fieldValue, $fieldConfig); |
| | | } |
| | | |
| | | $output .= sprintf('</%s>', esc_attr($wrapper)); |
| | | |
| | | unset($config['label']); |
| | | return static::fieldWrap($name, $output, $config); |
| | | } |
| | | |