Jake Vanderwerf
2026-02-10 2ba4657ad4ba8630319b6ce126ddf0b3d4be0778
inc/meta/Form.php
@@ -87,7 +87,7 @@
   // 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);
@@ -99,13 +99,16 @@
         '<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);
@@ -117,7 +120,7 @@
   }
      protected static function buildClasses(array $config): string
      {
         $classes = ['field field-' . ($config['type'] ?? 'text')];
         $classes = ['field '. (str_replace('_','-',sanitize_text_field($config['type'] ?? 'text')))];
         if (!empty($config['required'])) {
            $classes[] = 'required';
         }
@@ -141,10 +144,10 @@
         protected static function handleCustomDatasets($config):string
         {
            if (array_key_exists('data', $config) && !empty($config['data'])) {
               $datasets = array_map(function ($key) use ($config) {
               $datasets = array_map(function ($value, $key) use ($config) {
                  $name = str_replace('_', '-', sanitize_title($key));
                  return ' data-'.$name.'="'.$config['data'][$key].'"';
               }, $config['data']);
                  return ' data-'.$name.'="'.$value.'"';
               }, array_values($config['data']), array_keys($config['data']));
               return implode($datasets);
            }
@@ -179,6 +182,21 @@
            );
         }
   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'])) {
@@ -192,7 +210,7 @@
         return '';
      }
      protected static function buildInput(string $content):string
      public static function buildInput(string $content):string
      {
         return sprintf(
            '<div class="field-input-wrapper">
@@ -276,7 +294,7 @@
   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',
@@ -312,7 +330,7 @@
   {
      $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,
@@ -331,7 +349,7 @@
      $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>
@@ -354,7 +372,7 @@
   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,
@@ -367,7 +385,7 @@
   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,
@@ -380,7 +398,7 @@
   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,
@@ -401,7 +419,7 @@
         }
      }
      $value = ($value === '') ? ' value="'.esc_attr($value).'"' : '';
      $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : '';
      $input = sprintf(
         '<input type="date"%s%s />',
         $value,
@@ -413,7 +431,7 @@
   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,
@@ -425,7 +443,7 @@
   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,
@@ -492,13 +510,15 @@
      $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)
         );
      }
@@ -538,7 +558,7 @@
            $optValue,
            esc_attr($optValue),
            $checked,
            $name,
            esc_attr($name),
            $optValue,
            esc_html($optLabel)
         );
@@ -552,28 +572,47 @@
   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
         );
      }
@@ -629,11 +668,25 @@
      }
      $config['data']['upload-field'] = '';
      $config['data']['type'] = $config['multiple'] ? 'gallery' : 'single';
      $plural = 'images';
      $singular = 'image';
      if (!empty($config['content'])) {
         $config['data']['content'] = $config['content'];
         $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']);
      }
      if ($config['limit'] > 0) {
         $config['data']['limit'] = $config['limit'];
         $config['data']['max-files'] = $config['limit'];
      }
      $attachmentIds = static::parseIds($value);
@@ -644,7 +697,8 @@
            <input type="file"
               %s
               accept="%s"
               data-max-size="%s">
               data-max-size="%s"
               data-ignore>
            <h2>%s</h2>
            %s
            <p class="file-upload-text">
@@ -660,8 +714,8 @@
         esc_html(static::getAcceptedTypesLabel($config)),
         esc_html(static::formatFileSize(static::getMaxFileSize($config)))
      );
      $plural = (array_key_exists($config['content'], JVB_CONTENT)) ? JVB_CONTENT[$config['content']]['plural'] : (array_key_exists($config['content'], JVB_TAXONOMY) ? JVB_TAXONOMY[$config['content']]['plural'] : str_replace('_', ' ',$config['content']).'s');
      $singular = (array_key_exists($config['content'], JVB_CONTENT)) ? JVB_CONTENT[$config['content']]['singular'] : (array_key_exists($config['content'], JVB_TAXONOMY) ? JVB_TAXONOMY[$config['content']]['singular'] : str_replace('_', ' ',$config['content']));
      if ($config['destination'] === 'post_group') {
         $input .= sprintf(
            '<p class="hint">You can group images to create separate %s.</p>
@@ -678,7 +732,8 @@
      $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);
@@ -689,6 +744,7 @@
         $input .= static::renderUploadGroupAreaEnd($config, $plural, $singular);
      }
      $input .= '</div>'; // closes .file-upload-container
      unset($config['description']);
      unset($config['label']);
      return static::fieldWrap($name, $input, $config);
@@ -852,7 +908,7 @@
                  ]
               ];
               $out .= static::render('image_data', $fields);
               $out .= static::render('image_data', '', $fields);
               $out .= static::renderUploadItemMetaEnd();
               if ($additionalFields) {
@@ -920,7 +976,7 @@
                  ]
               ];
               $out .= static::render('image_data', $fields);
               $out .= static::render('image_data', '', $fields);
               $out .= static::renderUploadItemMetaEnd();
               if ($additionalFields) {
@@ -982,7 +1038,7 @@
                  ]
               ];
               $out .= static::render('image_data', $fields);
               $out .= static::render('image_data', '', $fields);
               $out .= static::renderUploadItemMetaEnd();
               if ($additionalFields) {
@@ -1150,6 +1206,7 @@
         $plural[1]??''
      );
      $config['type'] = 'selector';
      unset($config['label']);
      unset($config['description']);
      unset($config['hint']);
@@ -1220,7 +1277,7 @@
                  $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
@@ -1239,7 +1296,7 @@
                  $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
@@ -1259,7 +1316,7 @@
                  $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
@@ -1287,7 +1344,7 @@
            $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';
@@ -1304,15 +1361,18 @@
            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
   {
      $containerId = sprintf('%s-%s-selector', $name, $config['subtype'] ?? $config['type']);
      return sprintf(
         '<input type="hidden" name="%s" value="">
        <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($containerId),
         esc_attr($name)
      );
   }
   protected static function renderTaxonomy(string $name, mixed $value, array $config): string
   {
@@ -1426,59 +1486,83 @@
         $input .= static::render($newName, '', $fieldConfig);
      }
      $input .= sprintf(
         '<button type="button" class="button add-tag-item">%s<span>%s</span></button></div>',
         '<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
      $input .= '<div class="tag-items">'.static::renderTagItem($config['fields'], $value, $name, null, $tagFormat).'</div>';
      $input .= '<div class="tag-items">'.static::renderTagItems($config['fields'], $value, $name, $tagFormat).'</div>';
      //Template for tags
      $input .= sprintf(
         '<template class="%s">%s</template>',
         uniqid('tagListItem'),
         static::renderTagItem($config['fields'], [], null, $name, $tagFormat)
         static::renderTagItem($config['fields'], [], $name, null, $tagFormat)
      );
      $input .= '</div>';
      unset($config['label']);
      return static::fieldWrap($name, $input, $config);
   }
      protected static function renderTagItems(array $fields, mixed $value, string $name, string $tagFormat):string
      {
         if (!$value || $value === '') {
            return '';
         }
         if (is_string($value)) {
            $value = explode(',', $value);
         }
         if (empty($value)) {
            return '';
         }
         $out = '';
         foreach ($value as $index => $v) {
            $out .= static::renderTagItem($fields, $v, $name, $index, $tagFormat);
         }
         return $out;
      }
      protected static function renderTagItem(array $fields, mixed $values, string $name, ?int $index, string $tagFormat):string
      {
         $tagText = static::getTagDisplayText($fields, $values, $tagFormat);
         $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></div>',
               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';
            }
@@ -1487,7 +1571,7 @@
                  $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) {
@@ -1577,7 +1661,6 @@
   protected static function renderGroup(string $name, mixed $value, array $config): string
   {
      $fields = $config['fields'] ?? [];
      error_log('Group fields: '.print_r($fields, true));
      $values = is_array($value) ? $value : [];
      $wrapper = (array_key_exists('wrap', $config)) ? 'details' : 'fieldset';
@@ -1594,12 +1677,13 @@
      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);
   }