| | |
| | | try { |
| | | $data = $request->get_params(); |
| | | |
| | | // Validate user permissions |
| | | if (!$this->userCheck($data['user'])) { |
| | | return $this->sendResponse( |
| | | false, |
| | | ['error_code' => 'invalid_user'], |
| | | 'Invalid user specified' |
| | | ); |
| | | } |
| | | error_log('Received data for meta change: '.print_r($data, true)); |
| | | |
| | | |
| | | $items = $data['items']??false; |
| | | if (!$items) { |
| | |
| | | } |
| | | $pending = []; |
| | | $attachments = array_filter($items, function ($item) { |
| | | return is_numeric($item); |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | return array_key_exists('attachmentId', $item); |
| | | }); |
| | | if (count($attachments) !== count($items)) { |
| | | $pending = array_filter($items, function ($item) { |
| | | return !is_numeric($item); |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | return array_key_exists('uploadId',$item); |
| | | }); |
| | | } |
| | | |
| | | |
| | |
| | | { |
| | | $updated_count = 0; |
| | | $errors = []; |
| | | |
| | | foreach ($data as $attachment_id => $info) { |
| | | $ids = []; |
| | | foreach ($data as $info) { |
| | | try { |
| | | $attachment_id = $info['attachmentId']; |
| | | $ids[] = $attachment_id; |
| | | unset($info['attachmentId']); |
| | | // Verify attachment exists and user has permission |
| | | if (!$this->verifyAttachmentAccess($attachment_id, $user)) { |
| | | $errors[] = "No permission to edit attachment {$attachment_id}"; |
| | |
| | | [ |
| | | 'updated_count' => $updated_count, |
| | | 'errors' => $errors, |
| | | 'attachment_ids' => $data['attachment_ids'] |
| | | 'attachment_ids' => $ids |
| | | ], |
| | | $updated_count > 0 ? |
| | | "Updated metadata for {$updated_count} attachment(s)" : |
| | |
| | | protected function applyMeta(int $attachment_id, array $metadata): void |
| | | { |
| | | // Update alt text |
| | | if (!empty($metadata['alt'])) { |
| | | update_post_meta($attachment_id, '_wp_attachment_image_alt', sanitize_text_field($metadata['alt'])); |
| | | if (!empty($metadata['image-alt-text'])) { |
| | | update_post_meta($attachment_id, '_wp_attachment_image_alt', sanitize_text_field($metadata['image-alt-text'])); |
| | | } |
| | | $postUpdates = []; |
| | | // Update title |
| | | if (!empty($metadata['title'])) { |
| | | $postUpdates['post_title'] = $metadata['title']; |
| | | if (!empty($metadata['image-title'])) { |
| | | $postUpdates['post_title'] = $metadata['image-title']; |
| | | } |
| | | |
| | | // Update caption |
| | | if (!empty($metadata['caption'])) { |
| | | $postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['caption']); |
| | | if (!empty($metadata['image-caption'])) { |
| | | $postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['image-caption']); |
| | | } |
| | | |
| | | if (!empty($postUpdates)) { |