Jake Vanderwerf
4 hours ago 88d9e0e2b7997eb0c96dc737082c91b4e3f7ca6e
inc/managers/queue/executors/UploadExecutor.php
@@ -288,6 +288,7 @@
      error_log('Processing Meta Update with data: '.print_r($data, true));
      foreach ($data as $uploadId => $info) {
         if (!is_array($info)) {
            error_log('Not an array for processing meta update: '.print_r($info, true));
            continue;
         }
         $success = true;
@@ -313,8 +314,6 @@
               continue;
            }
            $this->applyMeta($attachmentId, $info);
            $updatedCount++;
            $progress->advance(1);
@@ -325,7 +324,7 @@
            $errors[] = $e->getMessage();
         }
         if ($success) {
         if ($success && $attachmentId) {
            $postID = wp_get_post_parent_id($attachmentId);
            if ($postID && !in_array($postID, $postsAttachedTo)){
               $postsAttachedTo[] = $postID;
@@ -698,24 +697,40 @@
   private function applyMeta(int $attachmentId, array $metadata): void
   {
      if (array_key_exists('image-alt-text', $metadata)) {
         update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text']));
      }
      $postUpdates = [];
      if (array_key_exists('image-title', $metadata)) {
         $postUpdates['post_title'] = sanitize_text_field($metadata['image-title']);
      foreach ($metadata as $key => $value) {
         //Remove the image_data| from the  meta's group formatting
         $base = 'image_data|';
         $key = str_replace($base, '', $key);
         if ($key === 'image-alt-text') {
            error_log('Updating Image Alt Text: '.print_r($value, true));
            update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($value));
            continue;
      }
      if (array_key_exists('image-caption', $metadata)) {
         $postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['image-caption']);
         $k = match ($key) {
            'image-title'  => 'post_title',
            'image-caption' => 'post_excerpt',
            'image-description' => 'post_content',
            default => false
         };
         if (!$k) {
            error_log('No matching key for '.$key.' with value: '.print_r($value, true));
            continue;
      }
      if (array_key_exists('image-description', $metadata)) {
         $postUpdates['post_excerpt']= sanitize_textarea_field($metadata['image-caption']);
         $v = match ($key) {
            'image-title'  => sanitize_text_field($value),
            default        => sanitize_textarea_field($value)
         };
         $postUpdates[$k] = $v;
      }
      if (!empty($postUpdates)){
         $postUpdates['ID'] = $attachmentId;
         $postUpdates['post_type'] = 'attachment';
         error_log('Updating Image post with data: '.print_r($postUpdates, true));
         wp_update_post($postUpdates);
      }
   }