| | |
| | | namespace JVBase\meta; |
| | | |
| | | use DateTime; |
| | | use JVBase\base\Site; |
| | | use JVBase\integrations\services\GoogleMaps; |
| | | use JVBase\registrar\Registrar; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | |
| | | $value = ''; |
| | | } |
| | | if (!empty($config['hidden'])) { |
| | | return ''; |
| | | if ($config['type'] !== 'upload') { |
| | | return static::renderHidden($name, $value, $config); |
| | | } |
| | | return ''; |
| | | } |
| | | |
| | | $type = $config['type'] ?? 'text'; |
| | |
| | | /** |
| | | * Render complete form from Meta instance |
| | | */ |
| | | public static function renderFormFrom(Meta $meta, string $endpoint, array $options = []): string |
| | | public static function renderFormFrom(Meta $meta, string $endpoint, array $options = [], array $fields = []): string |
| | | { |
| | | $id = $options['form-id'] ?? $endpoint; |
| | | $classes = isset($options['classes']) ? ' class="' . implode(' ', $options['classes']) . '"' : ''; |
| | | $formID = isset($options['showFormID']) && $options['showFormID']!== false; |
| | | |
| | | $output = '<form id="' . esc_attr($endpoint) . '"' . $classes . ' data-save="' . esc_attr($endpoint) . '" data-form-id="' . esc_attr($id) . '">'; |
| | | $output = sprintf( |
| | | '<form id="%s"%s data-save="%s"%s>', |
| | | esc_attr($endpoint), |
| | | $classes, |
| | | esc_attr($endpoint), |
| | | $formID ? ' data-form-id="'.esc_attr($id).'"' : '', |
| | | ); |
| | | |
| | | if (!empty($options['heading'])) { |
| | | $output .= '<h2>' . esc_html($options['heading']) . '</h2>'; |
| | |
| | | } |
| | | } |
| | | |
| | | foreach ($meta->configs() as $name => $config) { |
| | | $output .= static::render($name, $meta->get($name), $config); |
| | | |
| | | $allFields = $meta->getAll($fields); |
| | | |
| | | foreach ($allFields as $name => $value) { |
| | | $config = $meta->config($name); |
| | | $output .= static::render($name, $value, $config); |
| | | } |
| | | |
| | | if (!empty($options['submit'])) { |
| | |
| | | return static::fieldWrap($name, $input, $config); |
| | | } |
| | | |
| | | protected static function renderHidden(string $name, mixed $value, array $config):string |
| | | { |
| | | return sprintf( |
| | | '<input type="hidden" name="%s" value="%s">', |
| | | esc_attr($name), |
| | | esc_attr($value) |
| | | ); |
| | | } |
| | | |
| | | protected static function renderTextarea(string $name, mixed $value, array $config): string |
| | | { |
| | | $rows = $config['rows'] ?? 5; |
| | |
| | | if (!array_key_exists('class', $config)) { |
| | | $config['class']=[]; |
| | | } |
| | | $config['class'][] ='quantity-input'; |
| | | $config['class'][] = 'quantity-input'; |
| | | $config['class'][] = 'row'; |
| | | $config['class'][] = 'nowrap'; |
| | | |
| | | if (array_key_exists('isCart', $config) && $config['isCart'] === true) { |
| | | |
| | | } |
| | | |
| | | $attrs = static::inputAttrs($name, $config); |
| | | |
| | | $value = ($value !== '') ? ' value="'.esc_attr($value).'"' : ''; |
| | | $input = sprintf( |
| | | '<div class="quantity"> |
| | | '<div class="row nowrap"> |
| | | <button type="button" class="decrease" title="%s" aria-label="Decrease %s">%s</button> |
| | | <input type="number"%s%s /> |
| | | <button type="button" class="increase" title="%s" aria-label="Increase %s">%s</button> |
| | |
| | | if (!array_key_exists('class', $config)) { |
| | | $config['class'] = []; |
| | | } |
| | | $config['class'][] ='row btw'; |
| | | $config['class'][] ='row x-btw'; |
| | | |
| | | $checked = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
| | | |
| | |
| | | $checked = in_array($optValue, $values) ? ' checked' : ''; |
| | | $checkboxes .= sprintf( |
| | | ' |
| | | <input type="checkbox" name="%s[]" id="%s-%s" value="%s"%s /> |
| | | <input type="checkbox" name="%s" id="%s-%s" value="%s"%s/> |
| | | <label class="checkbox-option" for="%s-%s"> |
| | | <span>%s</span> |
| | | </label>', |
| | | esc_attr($name), |
| | | esc_attr($name), |
| | | $optValue, |
| | | esc_attr($optValue), |
| | | esc_attr($optValue), |
| | | $checked, |
| | | esc_attr($name), |
| | | $optValue, |
| | | esc_attr($optValue), |
| | | esc_html($optLabel) |
| | | ); |
| | | } |
| | |
| | | </div> |
| | | </div> |
| | | |
| | | <div class="selection-actions row btw" hidden> |
| | | <div class="selection-actions row x-btw" hidden> |
| | | <button type="button" data-action="add-to-group"> |
| | | %sGroup |
| | | </button> |
| | |
| | | |
| | | public static function renderExistingAttachment(int $attachmentId, string $subtype):string |
| | | { |
| | | switch ($subtype){ |
| | | case 'video': |
| | | return static::renderVideoPreview($attachmentId); |
| | | case 'document': |
| | | case 'file': |
| | | return static::renderFilePreview($attachmentId); |
| | | default: |
| | | return static::renderImagePreview($attachmentId); |
| | | } |
| | | return match ($subtype) { |
| | | 'video' => static::renderVideoPreview($attachmentId), |
| | | 'document', 'file' => static::renderFilePreview($attachmentId), |
| | | default => static::renderImagePreview($attachmentId), |
| | | }; |
| | | } |
| | | protected static function renderUploadItemStart(?int $attachmentId = null):string |
| | | { |
| | |
| | | protected static function renderUploadItemActions(?int $attachmentId = null):string |
| | | { |
| | | return sprintf( |
| | | '<div class="item-actions row btw"> |
| | | '<div class="item-actions row x-btw"> |
| | | <div class="btn"> |
| | | <input type="radio" class="featured btn" name="featured" id="featured%s" hidden> |
| | | <label for="featured"> |
| | |
| | | } |
| | | public static function renderImagePreview(?int $ID = null, ?array $additionalFields = null, bool $isTemplate = false):string |
| | | { |
| | | $out = static::renderUploadItemStart($ID, $isTemplate); |
| | | $out = static::renderUploadItemStart($ID); |
| | | //add image preview |
| | | if ($ID) { |
| | | $out .= jvbFormatImage($ID, 'tiny', 'medium'); |
| | |
| | | $containerId = sprintf('%s-%s-selector', $name, $config['subtype']); |
| | | |
| | | $input = sprintf( |
| | | '<div class="row btw"> |
| | | '<div class="row x-btw"> |
| | | <input type="hidden" name="%s" value="%s"> |
| | | <label for="%s-autocomplete">%s<span>%s</span></label>', |
| | | esc_attr($name), |
| | |
| | | } |
| | | $plural = static::getPlural($config); |
| | | $input .= sprintf( |
| | | '<div class="selected-items row start" role="region" aria-label="Selected %s"></div>', |
| | | '<div class="selected-items row left" role="region" aria-label="Selected %s"></div>', |
| | | $plural[1]??'' |
| | | ); |
| | | |
| | |
| | | $plural |
| | | ); |
| | | break; |
| | | default: |
| | | $attr = false; |
| | | $single = false; |
| | | $plural = false; |
| | | } |
| | | |
| | | if (!$attr || !$single || !$plural) { |
| | | return ''; |
| | | } |
| | | $dataAttrs = []; |
| | | if ($config['update']) { |
| | | $dataAttrs[] = 'data-update="false"'; |
| | |
| | | |
| | | protected static function renderLocation(string $name, mixed $value, array $config): string |
| | | { |
| | | $googleMaps = JVB()->connect('maps'); |
| | | if (!$googleMaps->isSetUp()) { |
| | | return '<div class="notice notice-warning"><p>Google Maps not configured. Please configure in Integrations settings.</p></div>'; |
| | | } |
| | | $location = is_array($value) ? $value : []; |
| | | $fields = ['address', 'street', 'city', 'province', 'postal_code', 'country', 'lat', 'lng']; |
| | | |
| | | $field_id = esc_attr($name); |
| | | $map_id = sprintf('%s_map', $field_id); |
| | | $components = ['address', 'lat', 'lng', 'street', 'city', 'province', 'postal_code', 'country']; |
| | | if (!empty($value)) { |
| | | $lat = (float)$value['lat']??''; |
| | | $lng = (float)$value['lng']??''; |
| | | |
| | | $coords = [ |
| | | 'lat' => $lat, |
| | | 'ng' => $lng |
| | | ]; |
| | | } else { |
| | | $coords = null; |
| | | } |
| | | if (!array_key_exists('data', $config)) { |
| | | $config['data'] =[]; |
| | | } |
| | | $js_config = [ |
| | | 'fieldId' => $field_id, |
| | | 'initialCoords' => $coords |
| | | ]; |
| | | |
| | | $json_config = htmlspecialchars(json_encode($js_config), ENT_QUOTES, 'UTF-8'); |
| | | $config['data']['location-field-init'] = $json_config; |
| | | |
| | | $input = ''; |
| | | if (!empty($value) && array_key_exists('address', $value)) { |
| | | $input = sprintf( |
| | | '<p><b>Current location:</b> %s</p><p class="hint">Search below to change:</p>', |
| | | esc_html($value['address']) |
| | | ); |
| | | } |
| | | $links = (!empty($value)) ? jvbLocationLinks($value) : ''; |
| | | $input .= sprintf( |
| | | '<div class="location-search-wrapper"> |
| | | <div class="autocomplete-wrapper"></div> |
| | | <div class="location-preview"> |
| | | <div id="%s" class="location-map"></div> |
| | | %s |
| | | </div>', |
| | | esc_attr($map_id), |
| | | $links |
| | | $input = sprintf( |
| | | '<h3>%s</h3><input type="search" id="%s-search" placeholder="%s" autocomplete="off" data-ignore data-location-search /> |
| | | <p class="message" hidden aria-live="polite">{ <span>Searching locations</span> }</p> |
| | | <ul class="search-results"></ul><p><small><i>{ All Results © OpenStreetMap contributors, ODbL 1.0 }</i></small></p>', |
| | | $config['label'], |
| | | esc_attr($name), |
| | | esc_attr($config['placeholder'] ?? 'Search for an address…'), |
| | | ); |
| | | |
| | | if (!empty($value)) { |
| | | foreach($components as $el) { |
| | | $input .= sprintf( |
| | | '<input type="hidden" |
| | | name="%s[%s]" |
| | | value="%s" |
| | | data-location-field="%s">', |
| | | esc_attr($name), |
| | | $el, |
| | | $value[$el]??'', |
| | | $el |
| | | ); |
| | | } |
| | | $input .= '<fieldset class="location"><legend>Current Location</legend>'; |
| | | foreach ($fields as $field) { |
| | | $title = match($field) { |
| | | 'address' => 'House Number', |
| | | 'lat' => 'Latitude', |
| | | 'lng' => 'Longitude', |
| | | 'postal_code' => 'Postal Code', |
| | | default => ucfirst($field) |
| | | }; |
| | | $open = match($field) { |
| | | 'address','postal_code','lat' => '<div class="row">', |
| | | default => '', |
| | | }; |
| | | $close = match ($field) { |
| | | 'street','country','lng' => '</div>', |
| | | default => '', |
| | | }; |
| | | $input .= sprintf( |
| | | '%s<div><label for="%s|%s">%s</label> |
| | | <input type="text" name="%s" data-field-name="%s" id="%s|%s" value="%s" /></div>%s', |
| | | $open, |
| | | esc_attr($name), |
| | | esc_attr($field), |
| | | $title, |
| | | esc_attr("{$name}|{$field}"), |
| | | esc_attr($field), |
| | | esc_attr($name), |
| | | esc_attr($field), |
| | | esc_attr($location[$field] ?? ''), |
| | | $close |
| | | ); |
| | | } |
| | | $input .= '</fieldset>'; |
| | | |
| | | $input .= '</div>'; |
| | | // if (!empty($location['lat']) && !empty($location['lng'])) { |
| | | // $input .= sprintf( |
| | | // '<div class="location-preview" data-lat="%s" data-lng="%s"></div>', |
| | | // esc_attr($location['lat']), |
| | | // esc_attr($location['lng']) |
| | | // ); |
| | | // } |
| | | |
| | | $config['skipInput'] = true; |
| | | $config['label'] = false; |
| | | return static::fieldWrap($name, $input, $config); |
| | | } |
| | | |
| | |
| | | $config['data']['tag-format'] = esc_attr($tagFormat); |
| | | |
| | | $input = sprintf( |
| | | '<h3>%s</h3><div class="row start wrap">', |
| | | '<h3>%s</h3><div class="row left wrap">', |
| | | esc_html($config['label']??'') |
| | | ); |
| | | |
| | |
| | | $values = array_filter(array_values($values)); |
| | | return implode(', ', $values) ?: 'New Item'; |
| | | default: |
| | | if (strpos($tagFormat, '{') !== false) { |
| | | if (str_contains($tagFormat, '{')) { |
| | | $text = $tagFormat; |
| | | foreach ($values as $key => $value) { |
| | | $text = str_replace('{'.$key.'}', $value, $text); |
| | |
| | | ); |
| | | |
| | | $input .= '<div class="repeater-items">'; |
| | | $rowTitle = array_key_exists('new_row', $config) ? $config['new_row'] : 'New Item'; |
| | | |
| | | $hasTitle = array_key_exists('row_label', $config); |
| | | if(!empty($rows)) { |
| | | foreach ($rows as $index=>$row) { |
| | | $input .= static::renderRepeaterRow($config['fields'], $row, $index, $name, $rowTitle); |
| | | $titleReference = $hasTitle ? $config['row_label'] : null; |
| | | $rowTitle = !is_null($titleReference) ? $row[$titleReference] : 'New Item'; |
| | | |
| | | $input .= static::renderRepeaterRow($config['fields'], $row, $index, $name, $rowTitle, $titleReference); |
| | | } |
| | | } |
| | | $input .= '</div>'; |
| | | |
| | | $titleReference = $hasTitle ? $config['row_label'] : null; |
| | | $input .= '<template class="'.uniqid('repeaterRow').'">'; |
| | | $input .= static::renderRepeaterRow($config['fields'], [], '','',$rowTitle); |
| | | $input .= static::renderRepeaterRow($config['fields'], [], '','','New Item', $titleReference); |
| | | $input .= '</template>'; |
| | | |
| | | $input .= sprintf( |
| | |
| | | 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 |
| | | protected static function renderRepeaterRow(array $fields, array $values, int|string $index, string $name, string $rowTitle='New Item', ?string $rowTitleField = null):string |
| | | { |
| | | $displayNumber = (is_string($index)) ? $index : ($index +1); |
| | | |
| | | |
| | | $output = sprintf( |
| | | '<div class="repeater-row" data-index="%s"> |
| | | <button type="button" class="remove-row" title="Remove"> |
| | | %s |
| | | </button> |
| | | '<div class="repeater-row row nowrap" data-index="%s"> |
| | | <span class="drag-handle row">%s</span> |
| | | <details%s> |
| | | <summary class="row btw repeater-row-header"> |
| | | <span class="drag-handle">%s</span> |
| | | <summary class="row x-btw repeater-row-header"> |
| | | <span class="row-number">#%s</span> |
| | | <span class="row-title">%s</span> |
| | | <span class="row-title"%s>%s</span> |
| | | </summary> |
| | | <div class="repeater-row-content">', |
| | | esc_attr($index), |
| | | jvbIcon('trash'), |
| | | jvbIcon('dots-six-vertical'), |
| | | is_string($index) ? ' open' : '', |
| | | jvbIcon('dots-six-vertical'), |
| | | $displayNumber, |
| | | is_null($rowTitleField) ? '' : 'data-label="'.$rowTitleField.'"', |
| | | $rowTitle |
| | | ); |
| | | foreach ($fields as $fieldName => $fieldConfig) { |
| | |
| | | $output .= static::render($fullName, $fieldValue, $fieldConfig); |
| | | } |
| | | |
| | | return $output.'</div></details></div>'; |
| | | return $output.sprintf('</div></details><button type="button" class="remove-row" title="Remove"> |
| | | %s |
| | | </button></div>', |
| | | jvbIcon('trash'), |
| | | ); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | foreach ($fields as $fieldName => $fieldConfig) { |
| | | $fieldValue = $values[$fieldName] ?? ''; |
| | | $fullName = array_key_exists('wrap', $config) ? $fieldName : "{$name}:{$fieldName}"; |
| | | $fullName = "{$name}|{$fieldName}"; |
| | | $output .= static::render($fullName, $fieldValue, $fieldConfig); |
| | | } |
| | | |
| | |
| | | { |
| | | return sprintf('<dialog id="jvb-selector" aria-labelledby="modal-title" aria-modal="true"> |
| | | <div class="wrap col"> |
| | | <header class="modal-header"> |
| | | <header class="row"> |
| | | <h3 id="modal-title">Select Taxonomy</h3> |
| | | </header> |
| | | |
| | |
| | | <div class="items-wrap"> |
| | | <!-- Common/Favorite terms section --> |
| | | <details class="favourite-terms" hidden> |
| | | <summary class="title row btw">Your Go Tos:</summary> |
| | | <ul class="favourite-list row btw"></ul> |
| | | <summary class="title row x-btw">Your Go Tos:</summary> |
| | | <ul class="favourite-list row x-btw"></ul> |
| | | </details> |
| | | |
| | | <!-- Pagination info --> |
| | |
| | | { <span>loading items</span> } |
| | | </p> |
| | | <!-- Terms list --> |
| | | <ul class="items-container col start" role="listbox" aria-label="Available terms"> |
| | | <ul class="items-container col top" role="listbox" aria-label="Available terms"> |
| | | <!-- Terms will be populated here --> |
| | | </ul> |
| | | |
| | |
| | | |
| | | <!-- Create new term section --> |
| | | <details class="create-term" hidden> |
| | | <summary class="row btw">Add New Term</summary> |
| | | <summary class="row x-btw">Add New Term</summary> |
| | | <div class="create-new-term-section"> |
| | | <form class="create-term" data-nocache data-form-id="create-term" data-save="terms"> |
| | | <div class="form-group"> |
| | |
| | | <template class="selectedTerm"> |
| | | <div class="selected-item row"> |
| | | <span class="item-name"></span> |
| | | <button type="button" class="remove-term row">%s</button> |
| | | <button type="button" class="remove-term">%s</button> |
| | | </div> |
| | | </template> |
| | | <template class="termBreadcrumb"> |
| | | <button type="button" class="path-level"></button> |
| | | </template>', |
| | | static::search('Search terms', 'search-terms'), |
| | | str_replace('class="search-container', 'class="open search-container', static::search('Search terms', 'search-terms')), |
| | | jvbModalActions(), |
| | | jvbIcon('plus-square'), |
| | | jvbIcon('x') |
| | | ); |
| | | } |
| | | |
| | | public static function search(string $placeholder = 'Search...', string $id = 'search'):string |
| | | public static function search(string $placeholder = 'Search...', string $id = 'search', string $label = '', string $buttonText = '',bool $buttonInside = false, bool $hideSearch = false):string |
| | | { |
| | | $id = sanitize_title($id); |
| | | $label = empty($label) ? '' : sprintf( |
| | | '<h3>%s</h3>', |
| | | $label |
| | | ); |
| | | $buttonText = empty($buttonText) ? '' : sprintf( |
| | | '<span>%s</span>', |
| | | $buttonText |
| | | ); |
| | | $hideSearch = $hideSearch ? ' hidden' : ''; |
| | | return sprintf( |
| | | '<div class="search-container row start nowrap"> |
| | | <input type="search" id="%s" placeholder="%s"> |
| | | '%s<div class="search-container row left nowrap%s"> |
| | | <input type="search" id="%s" placeholder="%s"%s> |
| | | <button title="Clear Search" type="button" class="clear-search" aria-label="Clear search" |
| | | onclick="this.previousElementSibling.value = \'\'; this.previousElementSibling.focus();">%s</button> |
| | | <button type="button" title="Search" class="toggle search" aria-label="Toggles search input visually" onclick="this.parentNode.classList.toggle(\'open\');this.previousElementSibling.previousElementSibling.focus();">%s</button> |
| | | <button type="button" title="Search" class="toggle search" aria-label="Toggles search input visually" onclick="this.parentNode.classList.toggle(\'open\');this.previousElementSibling.previousElementSibling.focus();">%s%s</button> |
| | | </div>', |
| | | $label, |
| | | $buttonInside ? ' insideButton' : '', |
| | | $id, |
| | | $placeholder, |
| | | $hideSearch, |
| | | jvbIcon('x', ['title' => 'Clear Search']), |
| | | jvbIcon('magnifying-glass') |
| | | jvbIcon('magnifying-glass'), |
| | | $buttonText |
| | | ); |
| | | } |
| | | } |