Jake Vanderwerf
3 days ago ba1e1ccf869b818f7a7a897264dfea05563a7796
inc/rest/routes/FeedRoutes.php
@@ -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['date_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_gallery'] = $subFields;
      $item['images'] = $item['images'] + $images;
      $item['number'] = $count;
      $item['fields']['before'] = $item['fields']['post_thumbnail'];
      return $item;
   }
@@ -520,7 +501,7 @@
               : explode(',', $args['post_type']);
            // Check if filtering global feed content
            if (in_array(jvbNoBase($context['type']), Registrar::getFeatured('is_content', 'term'))) {
            if (in_array(jvbNoBase($context['type']), Registrar::withFeature('is_content', 'term'))) {
               // Global: show posts from any content type with this taxonomy
               $for_content = Registrar::getInstance($context['type'])->registrar->for ?? [];
@@ -528,7 +509,7 @@
               $post_types = array_map(fn($type) => jvbCheckBase($type), $for_content);
               // Filter to only show_feed content types
               $show_feed_types = Registrar::getFeatured('show_feed', 'post');
               $show_feed_types = Registrar::withFeature('show_feed', 'post');
               $args['post_type'] = array_intersect(
                  $post_types,
                  array_map(fn($type) => jvbCheckBase($type), $show_feed_types)
@@ -546,7 +527,6 @@
            // Add term to tax query
            $args['tax_query'][] = [
               'taxonomy' => $registrar->getBased(),
//             'field' => 'term_id',
               'terms' => [(int)$context['id']],
            ];
            break;
@@ -630,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;
@@ -1167,7 +1147,7 @@
            $config = [];
            // Get content types with show_feed
            $contentTypes = Registrar::getFeatured('show_feed', 'post');
            $contentTypes = Registrar::withFeature('show_feed', 'post');
            foreach ($contentTypes as $slug) {
               $this->cache->tag('content:'.$slug);
               $registrar = Registrar::getInstance($slug);
@@ -1183,7 +1163,7 @@
            }
            // Get taxonomies with show_feed (content taxonomies)
            $taxonomies = Registrar::getFeatured('show_feed', 'term');
            $taxonomies = Registrar::withFeature('show_feed', 'term');
            foreach ($taxonomies as $slug) {
               $registrar = Registrar::getInstance($slug);
               if (!$registrar || !($registrar->hasFeature('is_content') ?? false)) {