Jake Vanderwerf
7 days ago 46d681c6b825d21b3f698d793c4e630c687d90ad
inc/helpers/renderFields.php
@@ -4,9 +4,10 @@
   exit;
}
use JVBase\managers\CacheManager;
use JVBase\meta\MetaForm;
use JVBase\meta\MetaManager;
use JVBase\forms\TaxonomySelector;
use JVBase\managers\Cache;
use JVBase\meta\Form;
use JVBase\meta\Meta;
/**
 * Outputs a toggle text that visually changes based on selection, like a switch
@@ -25,12 +26,16 @@
    string $labelAfter,
    string $on,
    string $off,
    bool $hidden = false
    bool $hidden = false,
   array $data = []
):string {
    $hidden = ($hidden) ? ' hidden' : '';
   $dataset = (!empty($data)) ? implode('', array_map(function ($key, $value) {
      return ' data-'.$key.'="'.$value.'"';
   }, array_keys($data), array_values($data))) : '';
    return sprintf(
        '<div class="toggle-text" %s>
            <input value="all" name="%s" id="%s" type="checkbox">
        '<div class="toggle-text"%s>
            <input value="all" name="%s" id="%s" type="checkbox"%s>
            <label for="%s" class="row">
                       %s
                <span class="text row">
@@ -43,6 +48,7 @@
        $hidden,
        $name,
        $name,
      $dataset,
        $name,
        $labelBefore,
        $off,
@@ -53,24 +59,31 @@
/**
 * @param int $ID
 * @param MetaManager|null $meta
 *
 * @param string $type
 * @return string
 */
function jvbRenderLinks(int $ID, MetaManager|null $meta = null):string
function jvbRenderLinks(int $ID, string $type =''):string
{
    $cache = CacheManager::for('bio-'.$ID, WEEK_IN_SECONDS);
    $key = 'links';
    $cached = $cache->get($key);
    $cache = Cache::for('user_links', WEEK_IN_SECONDS)->connect('post')->connect('taxonomy')->connect('user');
    $cached = $cache->get($ID);
    if ($cached) {
        return $cached;
    }
    if (!$meta) {
        $meta = jvbGetMeta($ID);
    }
   $meta = match($type){
      'post'   => Meta::forPost($ID),
      'term'   => Meta::forTerm($ID),
      'user'   => Meta::forUser($ID),
      default  => false
   };
   if (!$meta) {
      $meta = jvbGetMeta($ID);
   }
   if (!$meta) {
      return '';
   }
    $links = $meta->getValue('links');
    $links = $meta->get('links');
    $out = '';
    if (!empty($links)) {
@@ -79,41 +92,41 @@
            $url = $link['url'];
            switch (true) {
                case strpos($url, 'facebook'):
                    $i = jvbIcon('facebook');
                    $i = jvbIcon('facebook-logo');
                    break;
                case strpos($url, 'instagram'):
                    $i = jvbIcon('instagram');
                    $i = jvbIcon('instagram-logo');
                    break;
                case strpos($url, 'tiktok'):
                    $i = jvbIcon('tiktok');
                    $i = jvbIcon('tiktok-logo');
                    break;
                case strpos($url, 'x.com'):
                    $i = jvbIcon('x');
                    break;
                case strpos($url, 'youtube'):
                    $i = jvbIcon('youtube');
                    $i = jvbIcon('youtube-logo');
                    break;
                case strpos($url, 'mastadon'):
                    $i = jvbIcon('mastadon');
                    $i = jvbIcon('mastadon-logo');
                    break;
                case strpos($url, 'fediverse'):
                case strpos($url, 'bluesky'):
                    $i = jvbIcon('fediverse');
                    $i = jvbIcon('fediverse-logo');
                    break;
                case strpos($url, 'linktree'):
                    $i = jvbIcon('linktree');
                    $i = jvbIcon('linktree-logo');
                    break;
                case strpos($url, 'snapchat'):
                    $i = jvbIcon('snapchat');
                    $i = jvbIcon('snapchat-logo');
                    break;
                case strpos($url, 'twitch'):
                    $i = jvbIcon('twitch');
                    $i = jvbIcon('twitch-logo');
                    break;
                case strpos($url, 'threads'):
                    $i = jvbIcon('threads');
                    $i = jvbIcon('threads-logo');
                    break;
                case strpos($url, 'twitter'):
                    $i = jvbIcon('twitter');
                    $i = jvbIcon('twitter-logo');
                    break;
                default:
                    $i = jvbIcon('link');
@@ -124,50 +137,59 @@
        }
        $out .= '</ul>';
    }
    $cache->set($key, $out);
    $cache->set($ID, $out);
    return $out;
}
/**
 * @param int $ID
 * @param MetaManager|null $meta
 * @param string $type
 *
 * @return string
 */
function jvbRenderContactInfo(int $ID, MetaManager|null $meta = null):string
function jvbRenderContactInfo(int $ID, string $type = ''):string
{
    $cache = CacheManager::for('bio-'.$ID, WEEK_IN_SECONDS);
    $key = 'contact';
    $cached = $cache->get($key);
    $cache = Cache::for('contact', WEEK_IN_SECONDS)->connect('post')->connect('taxonomy');
    $cached = $cache->get($ID);
    if($cached){
        return $cached;
    }
    if (!$meta) {
        $meta = jvbGetMeta($ID);
    }
   $meta = match($type){
      'post'   => Meta::forPost($ID),
      'term'   => Meta::forTerm($ID),
      'user'   => Meta::forUser($ID),
      default  => false
   };
   if (!$meta) {
      $meta = jvbGetMeta($ID);
   }
   if (!$meta) {
      return '';
   }
    $preference = $meta->getValue('public_contact');
    $preference = $meta->get('public_contact');
    $preference = (is_array($preference)) ? $preference : explode(',', $preference);
    $out = '';
    if (!empty($preference)) {
        $out = '<ul class="contact">';
        $phone = $meta->getValue('phone');
        $phone = $meta->get('phone');
        foreach ($preference as $p) {
            $link = $label = false;
            switch ($p) {
                case 'text':
                    $link = 'sms:+1'.$phone.';?body='.rawurlencode('Hey! I found you on edmonton.ink, and I wanted to reach out.');
                    $label = jvbIcon('text').'<span>Text</span>';
                    $label = jvbIcon('chat').'<span>Text</span>';
                    break;
                case 'call':
                    $link = 'tel:+1'.$phone;
                    $label = jvbIcon('phone').'<span>Call</span>';
                    break;
                case 'email':
                    $link = 'mailto:'.$meta->getValue('email').'?subject='.rawurlencode('Contact from edmonton.ink').'&body='.rawurlencode('Hey,
                    $link = 'mailto:'.$meta->get('email').'?subject='.rawurlencode('Contact from edmonton.ink').'&body='.rawurlencode('Hey,
        I found you on edmonton.ink, and I wanted to reach out!');
                    $label = jvbIcon('email').'<span>Email</span>';
                    $label = jvbIcon('envelope').'<span>Email</span>';
                    break;
            }
            if ($link) {
@@ -178,29 +200,38 @@
        $out .= '</ul>';
    }
    $cache->set($key, $out);
    $cache->set($ID, $out);
    return $out;
}
/**
 * @param int $ID
 * @param MetaManager|null $meta
 * @param string $type
 * @return string
 */
function jvbRenderSpecialtyField(int $ID,  MetaManager|null $meta = null):string
function jvbRenderSpecialtyField(int $ID,  string $type = ''):string
{
    if (!$meta) {
        $meta = jvbGetMeta($ID);
    }
   $meta = match($type){
      'post'   => Meta::forPost($ID),
      'term'   => Meta::forTerm($ID),
      'user'   => Meta::forUser($ID),
      default  => false
   };
   if (!$meta) {
      $meta = jvbGetMeta($ID);
   }
   if (!$meta) {
      return '';
   }
    $out = '';
    $specialties = $meta->getValue('specialties');
    $specialties = $meta->get('specialties');
    if (!empty($specialties)) {
        foreach ($specialties as $specialty) {
            $out .= '<li><b>'.$specialty['specialty'].'</b>';
            if ($specialty['description'] !== '') {
                $out .= apply_filters('the_content', $specialty['description']);
                $out .= jvb_filter_content( $specialty['description']);
            }
            $out .= '</li>';
        }
@@ -215,17 +246,26 @@
/**
 * @param int $ID
 * @param MetaManager|null $meta
 * @param string $type = ''
 * @return string
 */
function jvbRenderAwardsField(int $ID, MetaManager|null $meta = null):string
function jvbRenderAwardsField(int $ID, string $type = ''):string
{
    if (!$meta) {
        $meta = jvbGetMeta($ID);
    }
   $meta = match($type){
      'post'   => Meta::forPost($ID),
      'term'   => Meta::forTerm($ID),
      'user'   => Meta::forUser($ID),
      default  => false
   };
   if (!$meta) {
      $meta = jvbGetMeta($ID);
   }
   if (!$meta) {
      return '';
   }
    $out = '';
    $awards = $meta->getValue('awards');
    $awards = $meta->get('awards');
    if (!empty($awards)) {
        foreach ($awards as $award) {
            $out .= '<li><b>'.$award['name'].'</b>';
@@ -243,23 +283,32 @@
/**
 * @param int $ID
 * @param MetaManager|null $meta
 * @param string $type
 * @return string
 */
function jvbRenderReviewsField(int $ID, MetaManager|null $meta = null):string
function jvbRenderReviewsField(int $ID, string $type = ''):string
{
    if (!$meta) {
        $meta = jvbGetMeta($ID);
    }
   $meta = match($type){
      'post'   => Meta::forPost($ID),
      'term'   => Meta::forTerm($ID),
      'user'   => Meta::forUser($ID),
      default  => false
   };
   if (!$meta) {
      $meta = jvbGetMeta($ID);
   }
   if (!$meta) {
      return '';
   }
    $out = '';
    $reviews = $meta->getValue('reviews');
    $reviews = $meta->get('reviews');
    if (!empty($reviews)) {
        foreach ($reviews as $review) {
            if ($review['review'] === '') {
                continue;
            }
            $out .= '<li><blockquote>'.apply_filters('the_content', $review['review']);
            $out .= '<li><blockquote>'.jvb_filter_content( $review['review']);
            if ($review['name'] !== '' || $review['rating'] !== 'none') {
                $date = $aOpen = $aClose = '';
                if ($review['url']) {
@@ -270,7 +319,7 @@
                    $date = new Date('M j, Y', strtotime($review['date']));
                }
                $out .= '<cite class="row btw">';
                $out .= '<cite class="row x-btw">';
                if ($review['rating'] !== 'none') {
                    $out .= jvbFormatStarRating($review['rating']);
                }
@@ -293,36 +342,53 @@
    return $out;
}
function jvbGetMeta(int $ID) {
    if (is_tax()) {
        $type = 'term';
    } elseif (is_singular()) {
        $type = 'post';
function jvbGetMeta(int $ID):Meta|false {
    if (term_exists($ID)) {
        return Meta::forTerm($ID);
    } elseif (get_post_status($ID)) {
        return Meta::forPost($ID);
    } elseif (get_userdata($ID)) {
        return Meta::forUser($ID);
    } else {
        $type = 'user';
    }
    return new JVBase\meta\MetaManager($ID, $type);
      return false;
   }
}
function jvbRenderTermList(array|bool|WP_Error $terms, string $label = '') {
function jvbRenderTermList(array|bool|WP_Error $terms, string $label = ''):string {
    if (!$terms || is_wp_error($terms) || empty($terms)) {
        return '';
    }
    $out = ($label === '') ? '' : '<h2 class="inline">'.$label.'</h2>';
    $out .= '<ul class="term-list '.jvbNoBase($terms[array_key_first($terms)]->taxonomy).'">';
    foreach ($terms as $term) {
        $out .= '<li>
            <a href="'.get_term_link($term->term_id, $term->taxonomy).'" title="'.$term->name.'">'.
                $term->name.
            '</a>
        </li>';
        $out .= '<li>'.jvbGetTermLink($term).'</li>';
    }
    $out .= '</ul>';
    return $out;
}
function jvbGetTermLink(int|WP_Term $term, string $taxonomy = ''):string
{
   if (is_int($term)){
      $term = get_term($term, jvbCheckBase($taxonomy));
      if (is_wp_error($term)) {
         return '';
      }
   }
   $cache = Cache::for($term->taxonomy.'_link')->connect('taxonomy');
   $key = $term->term_id;
   return $cache->remember(
      $key,
      function() use ($term) {
         return '<a href="'.get_term_link($term->term_id, $term->taxonomy).'" title="'.html_entity_decode($term->name).'">'.
            html_entity_decode($term->name).
         '</a>';
      }
   );
}
add_action('wp_footer', 'jvbOutputImageTemplates');
function jvbOutputImageTemplates() {
@@ -340,7 +406,7 @@
            </div>
            <div class="summary">
               <div class="result">
                  <h4></h4>
                  <h3></h3>
                  <p></p>
               </div>
            </div>
@@ -356,48 +422,48 @@
         </div>
      </template>
      <template class="uploadMeta">
         <?= jvbImageMeta() ?>
         <?= Form::renderImagePreview() ?>
      </template>
      <template class="imageGroup">
         <div class="upload-group">
            <div class="group-header">
               <div class="selected">
                  <div class="field">
                     <input type="checkbox" id="select-all-group" name="select-all-group">
                     <label for="select-all-group">
                        Select All
                  <div class="field checkbox">
                     <input type="checkbox" id="select-all" name="select-all" data-selects="item-grid" data-select-all>
                     <label for="select-all">
                        Select All In Group
                     </label>
                  </div>
                  <div class="info" hidden>
                  </div>
               </div>
               <div class="group-actions">
               <div class="selection-actions">
                  <button type="button" data-action="add-to-group" title="Add selected uploads to this group">
                     <?= jvbIcon('add') ?>
                     <?= jvbIcon('plus-square') ?>
                     Add Here
                  </button>
                  <button type="button" data-action="delete-group" title="Delete group">
                     <?= jvbIcon('delete') ?>
                     <?= jvbIcon('trash') ?>
                     Delete Group
                  </button>
               </div>
            </div>
            <details>
               <summary class="row btw">
               <summary class="row x-btw">
                  Extra Fields
               </summary>
               <div class="fields"></div>
            </details>
            <div class="group-content col">
               <p class="hint count"></p>
               <div class="item-grid group"></div>
               <p class="hint group-count"></p>
            </div>
         </div>
      </template>
      <template class="groupActions">
         <div class="item-actions">
            <div class="radio-button">
            <div class="btn">
               <input type="radio" class="featured btn" name="featured" id="featured">
               <label for="featured">
                  <?=jvbIcon('star')?>
@@ -407,7 +473,7 @@
            </div>
            <button type="button" data-action="remove-from-group" title="Remove from Group">
               <?=jvbIcon('delete')?>
               <?=jvbIcon('trash')?>
            </button>
         </div>
      </template>
@@ -418,7 +484,7 @@
            </div>
            <div class="selection-controls">
               <button type="button" data-action="add-to-group">
                  <?= jvbIcon('add') ?>
                  <?= jvbIcon('plus-square') ?>
                  New Group
               </button>
            </div>
@@ -446,22 +512,19 @@
         </div>
      </template>
      <template class="uploadItem">
         <?php
         $form = new MetaForm();
         $form->renderImagePreview();
         ?>
         <?= Form::renderImagePreview() ?>
      </template>
      <template class="restoreNotification">
         <dialog class="restore-uploads">
            <div class="wrap">
               <div class="restore-message">
                  <h4>Looks like we left things hanging</h4>
                  <p class="restore-details"></p>
                  <p class="details"></p>
                  <p class="hint">If you'd rather start over, you can clear this information.</p>
               </div>
               <div class="restore-actions">
               <div class="selection-actions">
                  <div class="selected">
                     <div class="field">
                     <div class="field checkbox">
                        <input type="checkbox" id="select-all-restore" name="select-all-restore">
                        <label for="select-all-restore">
                           Select All
@@ -471,12 +534,16 @@
                     </div>
                  </div>
                  <div class="m-actions row nowrap">
                     <button type="button" data-action="restore-all">
                        <?= jvbIcon('infinity') ?>
                        Restore All
                     </button>
                     <button type="button" data-action="restore">
                        <?= jvbIcon('restore') ?>
                        <?= jvbIcon('arrow-counter-clockwise') ?>
                        Restore Selected
                     </button>
                     <button type="button" data-action="clear-cache" title="Clear cache and close window">
                        <?= jvbIcon('close') ?>
                        <?= jvbIcon('x') ?>
                        Clear Cache
                     </button>
                  </div>
@@ -485,10 +552,10 @@
         </dialog>
      </template>
      <template class="restoreField">
         <div class="restore-field">
            <h3></h3>
         <details class="restore-field">
            <summary><h3><a></a></h3></summary>
            <div class="item-grid restore"></div>
         </div>
         </details>
      </template>
      <template class="startOverConfirmation">
@@ -522,58 +589,17 @@
      <?php
   }
   if (wp_script_is('jvb-selector')) {
      \JVBase\forms\TaxonomySelector::class::outputSelectorModal();
      TaxonomySelector::class::outputSelectorModal();
   }
}
function jvbImageMeta(int|null $ID = null, string $title = '', string $alt = '', string $caption = '', array $fields = []):string
{
   $form = new MetaForm();
   $dataID = ($ID) ? ['image-id' => $ID] : false;
   $addID = ($ID) ? '-'.$ID : '';
   $fields = array_merge([
      'image_data'   => [
         'type'   => 'group',
         'wrap'   => 'details',
         'label'  => 'Image Info',
         'hint'   => 'These will be automatically generated if left blank.',
         'fields' => [
            'image-title'.$addID => [
               'type'   => 'text',
               'label'  => 'Image Title',
               'value'  => $title,
               'data'   => $dataID
            ],
            'image-alt-text'.$addID => [
               'type'   => 'text',
               'label'  => 'Alt Text',
               'value'  => $alt,
               'hint'   => 'Alt text helps the visually impaired, as well as some benefits for SEO.',
               'data'   => $dataID
            ],
            'image-caption'.$addID => [
               'type'   => 'textarea',
               'value'  => $caption,
               'label'  => 'Image Caption',
               'data'   => $dataID
            ]
         ]
      ]
   ], $fields);
   return $form->render('image_data',null, $fields,false, true);
}
function jvbLocationLinks(array $location): string {
   if (empty($location['address'])) {
      return '';
   }
   $cache = CacheManager::for('locations');
   $cache = Cache::for('locations')->connect('taxonomy');
   $key = $cache->generateKey($location);
   $cached = false;