Jake Vanderwerf
2025-11-23 d7dbe7fee362d587dfc334135d9581b6216a4295
inc/rest/routes/UploadRoutes.php
@@ -277,7 +277,7 @@
      unset($context['upload_ids']);
      $config = $this->getFieldConfig($args);
      error_log('secureFiles: '.print_r($files, true));
      $file_array = $files['files'] ?? $files;
      $tmp_names = isset($file_array['tmp_name'][0]) && is_array($file_array['tmp_name'][0])
         ? $file_array['tmp_name'][0]
@@ -1092,6 +1092,11 @@
         $args = $this->buildUploadArgs($request);
         $data = $request->get_params();
         error_log('[UploadRoutes]:handleGroupingRequest: data'.print_r($data, true));
         error_log('[UploadRoutes]:handleGroupingRequest: args'.print_r($args, true));
         if (!$args['content'] || !$args['user'] || !$args['posts']) {
            $this->logError('Missing required data');
@@ -1215,6 +1220,9 @@
         }
         $content = jvbCheckBase($data['content']);
         if (Features::forContent($data['content'])->has('is_timeline')) {
            return $this->processTimelineUploads($data, $uploads, $all_uploaded_images, $operation);
         }
         $user = (int)$data['user'];
         $created_posts = [];
         $used_upload_ids = [];
@@ -1229,13 +1237,15 @@
               ? sanitize_textarea_field($post['fields']['post_excerpt'])
               : '';
            $new_post_id = wp_insert_post([
            $args =[
               'post_type' => $content,
               'post_author' => $user,
               'post_status' => 'draft',
               'post_title' => $post_title,
               'post_excerpt' => $post_excerpt,
            ]);
            ];
            $new_post_id = wp_insert_post($args);
            if ($new_post_id && !is_wp_error($new_post_id)) {
               $created_posts[] = $new_post_id;
@@ -1312,6 +1322,105 @@
      }
   }
   protected function processTimelineUploads(array $data, array $uploads, array $uploadMap, object $operation):array
   {
      try {
         $user = (int)$data['user'];
         $created_posts = [];
         $used_upload_ids = [];
         $content = jvbCheckBase($data['content']);
         error_log('[processTimelineUploads]Got content: '.print_r($content, true));
         $config = Features::getConfig($content);
         error_log('[processTimelineUploads]Got config: '.print_r($config, true));
         $defaultTitle = 'New '.$config['singular']. ' ';
         foreach ($data['posts'] as $index=> $post) {
            $title = !empty($post['fields']['post_title'])
               ? sanitize_text_field($post['fields']['post_title'])
               : $defaultTitle.($index + 1);
            $excerpt = !empty($post['fields']['post_excerpt'])
               ? sanitize_textarea_field($post['fields']['post_excerpt'])
               : '';
            $args =[
               'post_type'    => $content,
               'post_author'  => $user,
               'post_status'  => 'draft',
               'post_title'   => $title,
               'post_excerpt' => $excerpt
            ];
            $parent = wp_insert_post($args);
            if ($parent && !is_wp_error($parent)) {
               //Get the attachment IDs first
               $childPosts = [];
               $featured = $post['fields']['featured']??null;
               $featuredID = null;
               foreach ($post['images'] as $key => $img) {
                  $upload_id = $img['upload_id'];
                  $used_upload_ids[] = $upload_id;
                  if (isset($uploadMap[$upload_id])) {
                     $attachment_id = (int)$uploadMap[$upload_id]['attachment_id'];
                     if ($upload_id === $featured) {
                        $featuredID = $attachment_id;
                     } else {
                        $childPosts[] = $attachment_id;
                     }
                  }
               }
               // Set the featured image for the parent
               if ($featuredID) {
                  set_post_thumbnail($parent, $featuredID);
               } elseif (!empty($childPosts)) {
                  //use first image if no set featured
                  set_post_thumbnail($parent, (int)$childPosts[0]);
                  array_shift($childPosts);
               }
               //Create Child Posts
               if (!empty($childPosts)) {
                  $args['post_parent'] = $parent;
                  $created_posts[$parent] = [];
                  foreach ($childPosts as $i => $imgID) {
                     $treatment = $i + 1;
                     $childTitle = $title.' - Treatment '.$treatment;
                     $childDesc = '';
                     $args['post_title'] = $childTitle;
                     $args['post_excerpt'] = $childDesc;
                     $child = wp_insert_post($args);
                     if ($child && !is_wp_error($child)) {
                        $created_posts[$parent][] = $child;
                        set_post_thumbnail($child, $imgID);
                     }
                  }
               }
            }
         }
         return [
            'success'   => true,
            'result' => [
               'created_posts'   => $created_posts,
               'used_images'  => $used_upload_ids
            ]
         ];
      } catch (Exception $e) {
         JVB()->error()->log(
            '[UploadRoutes]:processTimelineUploads',
            $e->getMessage(),
            [
               'operation_id' => $operation->id,
               'user_id' => $operation->user_id
            ]
         );
         return [
            'success' => false,
            'result' => $e->getMessage()
         ];
      }
   }
   protected function cleanupUnusedImages(array $unused_images): array
   {
      $cleaned_count = 0;