| | |
| | | |
| | | use JVBase\managers\queue\{Executor, Operation, Progress, Result}; |
| | | use JVBase\managers\UploadManager; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\meta\Meta; |
| | | use Exception; |
| | | use JVBase\utility\Features; |
| | | |
| | |
| | | /** |
| | | * Executor for upload-related queue operations. |
| | | * Handles: image_upload, video_upload, document_upload, |
| | | * update_metadata, temporary_cleanup, attach_upload_to_content, process_upload_groups |
| | | * update_image_meta, temporary_cleanup, attach_upload_to_content, process_upload_groups |
| | | */ |
| | | final class UploadExecutor implements Executor |
| | | { |
| | |
| | | 'image_upload', |
| | | 'video_upload', |
| | | 'document_upload', |
| | | 'update_metadata', |
| | | 'update_image_meta', |
| | | 'temporary_cleanup', |
| | | 'attach_upload_to_content', |
| | | 'process_upload_groups' |
| | |
| | | 'image_upload' => $this->processFileUpload($operation, $data, 'image', $progress), |
| | | 'video_upload' => $this->processFileUpload($operation, $data, 'video', $progress), |
| | | 'document_upload' => $this->processFileUpload($operation, $data, 'document', $progress), |
| | | 'update_metadata' => $this->processMetadataUpdate($operation, $data, $progress), |
| | | 'update_image_meta' => $this->processMetaUpdate($operation, $data, $progress), |
| | | 'temporary_cleanup' => $this->processTemporaryCleanup($operation, $data, $progress), |
| | | 'attach_upload_to_content'=> $this->processAttachToContent($operation, $data, $progress), |
| | | 'process_upload_groups' => $this->processUploadGroups($operation, $data, $progress), |
| | |
| | | /** |
| | | * Process metadata updates for attachments |
| | | */ |
| | | private function processMetadataUpdate(Operation $operation, array $data, Progress $progress): Result |
| | | private function processMetaUpdate(Operation $operation, array $data, Progress $progress): Result |
| | | { |
| | | $updatedCount = 0; |
| | | $errors = []; |
| | | |
| | | foreach ($data as $uploadId => $info) { |
| | | if (!is_array($info) || empty($info['depends_on'])) { |
| | | if (!is_array($info)) { |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | // Get the dependency operation to find attachment ID |
| | | $depOp = JVB()->queue()->get($info['depends_on']); |
| | | if (!$depOp || !$depOp->result) { |
| | | $errors[] = "Dependency {$info['depends_on']} not found or has no result"; |
| | | if (array_key_exists('depends_on', $info)) { |
| | | // Get the dependency operation to find attachment ID |
| | | $depOp = JVB()->queue()->get($info['depends_on']); |
| | | if (!$depOp || !$depOp->result) { |
| | | $errors[] = "Dependency {$info['depends_on']} not found or has no result"; |
| | | continue; |
| | | } |
| | | $attachmentId = $this->findAttachmentByUploadId($uploadId, $depOp->result); |
| | | if (!$attachmentId) { |
| | | $errors[] = "No attachment found for upload ID: {$uploadId}"; |
| | | continue; |
| | | } |
| | | } else { |
| | | $attachmentId = $info['attachmentId']??false; |
| | | } |
| | | |
| | | if (!$attachmentId) { |
| | | $errors[] = "No attachment found for: ".print_r($info, true); |
| | | continue; |
| | | } |
| | | |
| | | $attachmentId = $this->findAttachmentByUploadId($uploadId, $depOp->result); |
| | | if (!$attachmentId) { |
| | | $errors[] = "No attachment found for upload ID: {$uploadId}"; |
| | | continue; |
| | | } |
| | | |
| | | |
| | | $this->applyMeta($attachmentId, $info); |
| | | $updatedCount++; |
| | |
| | | } |
| | | |
| | | if (!empty($gallery_attachment_ids)) { |
| | | $meta = new MetaManager($newPostID, 'post'); |
| | | $meta = Meta::forPost($newPostID); |
| | | $fields = jvbGetFields($content, 'post'); |
| | | foreach($fields as $name => $config) { |
| | | if ($config['type'] === 'gallery') { |
| | | $meta->updateValue($name, implode(',', $gallery_attachment_ids)); |
| | | $meta->set($name, implode(',', $gallery_attachment_ids)); |
| | | break; |
| | | } |
| | | } |
| | |
| | | |
| | | $defaultTitle = 'New '.$config['singular']. ' '; |
| | | foreach($data['posts'] as $index => $post) { |
| | | $progress->advance(); |
| | | $title = array_key_exists('post_title', $post['fields']) |
| | | ? sanitize_text_field($post['fields']['post_title']) |
| | | : $defaultTitle . ($index + 1); |
| | |
| | | 'post_author' => $user, |
| | | 'post_status' => 'draft', |
| | | 'post_title' => $title, |
| | | 'post_slug' => sanitize_title($title), |
| | | 'post_excerpt' => $excerpt |
| | | ]; |
| | | |
| | | $parent = wp_insert_post($args); |
| | | $progress->advance(); |
| | | if ($parent && !is_wp_error($parent)) { |
| | | |
| | | $childPosts = []; |
| | | $featured = $post['fields']['featured']??null; |
| | | $featuredID = null; |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | $this->updateTimelineMetadata($parent); |
| | | } |
| | | } |
| | | return new Result( |
| | |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * Update timeline parent post with count and latest date |
| | | * @param int $parentId Parent timeline post ID |
| | | */ |
| | | private function updateTimelineMetadata(int $parentId): void |
| | | { |
| | | // Get all child posts |
| | | $children = get_children([ |
| | | 'post_parent' => $parentId, |
| | | 'post_type' => get_post_type($parentId), |
| | | 'post_status' => ['publish', 'draft'], |
| | | 'orderby' => 'date', |
| | | 'order' => 'DESC', |
| | | 'fields' => 'ids' |
| | | ]); |
| | | |
| | | // Count includes parent + children |
| | | $number = count($children) + 1; |
| | | |
| | | // Update both meta fields |
| | | update_post_meta($parentId, BASE . 'number', $number); |
| | | update_post_meta($parentId, BASE . 'latest_date', time()); |
| | | } |
| | | |
| | | // ───────────────────────────────────────────────────────────── |
| | | // Helper methods |
| | | // ───────────────────────────────────────────────────────────── |
| | | |
| | | private function applyMeta(int $attachmentId, array $metadata): void |
| | | { |
| | | if (!empty($metadata['title'])) { |
| | | if (!empty($metadata['image-title'])) { |
| | | wp_update_post([ |
| | | 'ID' => $attachmentId, |
| | | 'post_title' => sanitize_text_field($metadata['title']), |
| | | 'post_title' => sanitize_text_field($metadata['image-title']), |
| | | ]); |
| | | } |
| | | |
| | | if (!empty($metadata['alt'])) { |
| | | update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['alt'])); |
| | | if (!empty($metadata['image-alt-text'])) { |
| | | update_post_meta($attachmentId, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text'])); |
| | | } |
| | | |
| | | if (!empty($metadata['caption'])) { |
| | | if (!empty($metadata['image-caption'])) { |
| | | wp_update_post([ |
| | | 'ID' => $attachmentId, |
| | | 'post_excerpt' => sanitize_textarea_field($metadata['caption']), |
| | | 'post_excerpt' => sanitize_textarea_field($metadata['image-caption']), |
| | | ]); |
| | | } |
| | | } |
| | |
| | | return; |
| | | } |
| | | |
| | | $existing = $meta->getValue($data['field_name']); |
| | | $existing = $meta->get($data['field_name']); |
| | | $existingIds = !empty($existing) ? explode(',', $existing) : []; |
| | | $allIds = array_unique(array_merge($existingIds, $attachmentIds)); |
| | | |
| | | $meta->updateValue($data['field_name'], implode(',', $allIds)); |
| | | $meta->set($data['field_name'], implode(',', $allIds)); |
| | | } |
| | | |
| | | private function updateFieldValue(array $data, array $results): void |
| | |
| | | return; |
| | | } |
| | | |
| | | $existing = $meta->getValue($data['field_name']); |
| | | $existing = $meta->get($data['field_name']); |
| | | $existingIds = !empty($existing) ? explode(',', $existing) : []; |
| | | $allIds = array_unique(array_merge($existingIds, $attachmentIds)); |
| | | |
| | | $meta->updateValue($data['field_name'], implode(',', $allIds)); |
| | | $meta->set($data['field_name'], implode(',', $allIds)); |
| | | } |
| | | |
| | | private function getMetaManager(array $data): ?MetaManager |
| | | private function getMetaManager(array $data): ?Meta |
| | | { |
| | | if (!empty($data['post_id'])) { |
| | | return new MetaManager($data['post_id'], 'post'); |
| | | return Meta::forPost($data['post_id']); |
| | | } |
| | | if (!empty($data['term_id'])) { |
| | | return new MetaManager($data['term_id'], 'term'); |
| | | return Meta::forTerm($data['term_id']); |
| | | } |
| | | if (!empty($data['user'])) { |
| | | $link = (int)get_user_meta($data['user'], BASE . 'link', true); |
| | | if ($link) { |
| | | return new MetaManager($link, 'post'); |
| | | return Meta::forPost($link); |
| | | } |
| | | } |
| | | return null; |
| | |
| | | if (str_starts_with($mimeType, 'image/')) { |
| | | set_post_thumbnail($postId, $attachmentId); |
| | | } elseif (str_starts_with($mimeType, 'video/')) { |
| | | $meta = new MetaManager($postId, 'post'); |
| | | $meta->updateValue('video', $attachmentId); |
| | | $meta = Meta::forPost($postId); |
| | | $meta->set('video', $attachmentId); |
| | | } else { |
| | | $meta = new MetaManager($postId, 'post'); |
| | | $existing = $meta->getValue('documents'); |
| | | $meta = Meta::forPost($postId); |
| | | $existing = $meta->get('documents'); |
| | | $existingIds = !empty($existing) ? explode(',', $existing) : []; |
| | | $existingIds[] = $attachmentId; |
| | | $meta->updateValue('documents', implode(',', $existingIds)); |
| | | $meta->set('documents', implode(',', $existingIds)); |
| | | } |
| | | } |
| | | |