Jake Vanderwerf
5 days ago a9b3b28d001941921aa70d37fdc87c758a163a44
inc/rest/routes/FeedRoutes.php
@@ -6,7 +6,7 @@
use JVBase\rest\Rest;
use JVBase\integrations\Umami;
use JVBase\rest\Route;
use JVBase\utility\Features;
use JVBase\base\Site;
use WP_Query;
use WP_Post;
use WP_Term;
@@ -44,7 +44,7 @@
   public function init():void
   {
      if (Features::hasIntegration('umami')) {
      if (Site::hasIntegration('umami')) {
         $this->tracker = JVB()->connect('umami');
      }
   }
@@ -68,7 +68,7 @@
            'dateFrom' => 'string',
            'dateTo' => 'string',
            'context' => 'string',
            'source' => 'string',
            'contextId' => 'string',
            'favourites' => 'boolean',
            'user' => 'integer',
            'highlight' => 'string',
@@ -87,7 +87,7 @@
            'dateFrom' => 'string',
            'dateTo' => 'string',
            'context' => 'string',
            'source' => 'string',
            'contextId' => 'string',
            'favourites' => 'boolean',
            'user' => 'integer',
            'highlight' => 'string',
@@ -149,21 +149,16 @@
                  $meta = Meta::forUser($postID);
                  $registrar = Registrar::getInstance($type);
                  break;
               default:
                  $meta = false;
            }
            if (!$registrar) {
            if (!$registrar || !$meta) {
               return [];
            }
            $fields = $registrar->getFields();
            [$images, $fields] = $registrar->getFeedFields();
            //Allow custom filtering for public fields
            if (!empty($registrar->getConfig('feed')['fields'])) {
               $fields = array_filter($fields, function($field) use ($registrar) {
                  return in_array($field, $registrar->getConfig('feed')['fields']);
               }, ARRAY_FILTER_USE_KEY);
            }
            $values = $meta->getAll(array_keys($fields));
            $all = array_merge($images, $fields);
            $values = $meta->getAll(array_map(function($f) { return $f['name']; }, $all));
            $out = [
               'fields' => $values,
            ];
@@ -173,23 +168,18 @@
            //Add images
            $imgIDs = [];
            $temp = array_filter($fields, function($field) {
               return in_array($field['type'], [ 'upload', 'image', 'gallery']);
            });
            foreach ($temp as $key => $config) {
               if (array_key_exists($key, $out['fields']) && $out['fields'][$key] !== '') {
                  $IDs = array_map('absint', explode(',',$out['fields'][$key]));
                  foreach ($IDs as $ID) {
                     $imgIDs[$ID] = jvbImageData($ID);
                  }
            $imgs = $meta->getAll(array_map(function($f) {return $f['name'];}, $images));
            foreach ($imgs as $img) {
               $IDs = array_map('absint', explode(',',$img));
               foreach ($IDs as $ID) {
                  $imgIDs[$ID] = jvbImageData($ID);
               }
            }
            $out['images'] = $imgIDs;
            $out['id'] = $postID;
            $out['content'] = $type;
            $out['icon'] = $registrar->getIcon()??jvbDefaultIcon();
            $out['icon'] = $registrar->getIcon();
            if ($out['icon'] === '') {
               $out['icon'] = jvbDefaultIcon();
            }
@@ -212,8 +202,6 @@
                  $out['title'] = html_entity_decode($post->name);
                  break;
               case 'post':
                  $out['date'] = $post->post_date;
                  $out['modified'] = $post->post_modified;
                  $out['user_id'] = (int)$post->post_author;
                  $out['url'] = get_the_permalink($postID);
                  $out['title']= get_the_title($postID);
@@ -253,37 +241,30 @@
   protected function formatTimeline(int $postID, WP_Post $post):array
   {
      if (!$this->timelineSharedFields || !$this->timelineUniqueFields){
         $this->initTimelineFields($post->post_type);
      }
      $item = $this->formatItem($postID, 'post', true);
      //Step 1: Get the fields that apply to all posts
      $mainMeta = Meta::forPost($post->ID);
      $item['fields'] = $mainMeta->getAll($this->timelineSharedFields);
      $item = $this->formatItem($postID, 'post', true);
      //Step 2: Get the fields for each individual posts
      $children = get_children(['post_parent' => $post->ID, 'orderby' => 'date', 'order' => 'ASC', 'post_status' => ['publish'], 'fields'=> 'ids']);
      array_unshift($children, $post->ID);
      $children = jvbTimelinePoints($postID, $post->post_type);
      $last = $children[array_key_last($children)];
         $lastMeta = Meta::forPost($last);
         $lastImg = $lastMeta->get('post_thumbnail');
      $item['taxonomies'] = $this->extractTaxonomies($item['fields'], $postID, jvbNoBase($post->post_type));
         $item['fields']['after'] = $lastImg;
         $item['images'][$lastImg] = jvbImageData((int) $lastImg);
         $item['fields']['post_modified'] = $lastMeta->get('post_date');
      $subFields = [];
      $images = [];
      foreach ($children as $child) {
         $meta = Meta::forPost($child);
         $f = $meta->getAll($this->timelineUniqueFields);
         $f =  ['id' => $child] + $f;
         $subFields[] = $f;
         $item['taxonomies'] = array_merge($item['taxonomies'], $this->extractTaxonomies($f, $postID, jvbNoBase($post->post_type)));
         $images[$f['post_thumbnail']] = jvbImageData((int) $f['post_thumbnail']);
         $count = count(array_filter($children, function ($child) {
            $tmp = Meta::forPost($child);
            return empty($tmp->get('is_update')) || $tmp->get('is_update') !==1;
         }));
      foreach($item['taxonomies'] as $tax => $items) {
         $item[$tax] = array_keys($items);
      }
      $item['number'] = (int)get_post_meta($post->ID,BASE.'number', true);
      $item['fields']['before'] = get_post_thumbnail_id($children[0]);
      $item['fields']['after'] = get_post_thumbnail_id($children[array_key_last($children)]);
      $item['fields']['timeline'] = $subFields;
      $item['images'] = $item['images'] + $images;
      $item['number'] = $count;
      $item['fields']['before'] = $item['fields']['post_thumbnail'];
      return $item;
   }
@@ -338,7 +319,7 @@
   protected function getAuthorData(WP_Post $post)
   {
      $author = $post->post_author;
      $userLink = get_user_meta($author, BASE.'link', true);
      $userLink = get_user_meta($author, BASE.'profile_link', true);
      return $this->cache->remember(
         $userLink,
         function () use ($userLink, $author) {
@@ -410,7 +391,7 @@
         $args = $this->applyContextFilters(
            $args,
            [
               'id' => $data['source']??'0',
               'id' => $data['contextId']??'0',
               'type' => $data['context']
            ]
         );
@@ -537,8 +518,15 @@
            // Add term to tax query
            $args['tax_query'][] = [
               'taxonomy' => jvbCheckBase($context['type']),
               'field' => 'term_id',
               'taxonomy' => $registrar->getBased(),
//             'field' => 'term_id',
               'terms' => [(int)$context['id']],
            ];
            break;
         case $registrar->getType() === 'term':
            // Add term to tax query
            $args['tax_query'][] = [
               'taxonomy' => $registrar->getBased(),
               'terms' => [(int)$context['id']],
            ];
            break;
@@ -622,7 +610,7 @@
   protected function handleContentTaxonomies(array $args): array
   {
      //TODO: This needs to be better. We have CustomTable now, maybe pass it along to that?
      $taxonomy = jvbNoBase($args['post_type']);
      global $wpdb;
      $table = $wpdb->prefix . BASE . 'content_' . $taxonomy;