| | |
| | | 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; |
| | |
| | | continue; |
| | | } |
| | | |
| | | |
| | | |
| | | $this->applyMeta($attachmentId, $info); |
| | | $updatedCount++; |
| | | $progress->advance(1); |
| | |
| | | $errors[] = $e->getMessage(); |
| | | } |
| | | |
| | | if ($success) { |
| | | if ($success && $attachmentId) { |
| | | $postID = wp_get_post_parent_id($attachmentId); |
| | | if ($postID && !in_array($postID, $postsAttachedTo)){ |
| | | $postsAttachedTo[] = $postID; |
| | | //TODO: is there a better way? |
| | | } |
| | | } |
| | | |
| | |
| | | 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' |
| | | ]); |
| | | $children = jvbTimelinePoints($parentId, get_post_type($parentId), ['any']); |
| | | |
| | | // Count includes parent + children |
| | | $number = count($children) + 1; |
| | |
| | | |
| | | 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 = []; |
| | | 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-title', $metadata)) { |
| | | $postUpdates['post_title'] = sanitize_text_field($metadata['image-title']); |
| | | $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; |
| | | } |
| | | $v = match ($key) { |
| | | 'image-title' => sanitize_text_field($value), |
| | | default => sanitize_textarea_field($value) |
| | | }; |
| | | $postUpdates[$k] = $v; |
| | | |
| | | } |
| | | |
| | | if (array_key_exists('image-caption', $metadata)) { |
| | | $postUpdates['post_excerpt'] = sanitize_textarea_field($metadata['image-caption']); |
| | | } |
| | | if (array_key_exists('image-description', $metadata)) { |
| | | $postUpdates['post_excerpt']= sanitize_textarea_field($metadata['image-caption']); |
| | | } |
| | | 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); |
| | | } |
| | | } |