| | |
| | | return $this->formatTimeline($post); |
| | | } |
| | | $this->meta = Meta::forPost($post->ID); |
| | | $fields = ($fields) ? $this->meta->getAll() : []; |
| | | $data = [ |
| | | 'id' => $post->ID, |
| | | 'title' => $post->post_title, |
| | |
| | | 'alt' => get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true), |
| | | 'icon' => $this->post_type, |
| | | 'taxonomies' => [], |
| | | 'fields' => ($fields) ? $this->meta->getAll() : [], |
| | | 'fields' => $fields, |
| | | 'images' => [], |
| | | ]; |
| | | |
| | |
| | | $images = []; |
| | | $get = []; |
| | | $fields = (empty($fields)) ? $this->fields : $fields; |
| | | foreach ($fields as $field => $config) { |
| | | if ($config['type'] === 'gallery' || $config['type'] === 'image' || $field === 'post_thumbnail') { |
| | | $get[] = $field; |
| | | } |
| | | } |
| | | $get = $this->getUploadFields($fields); |
| | | |
| | | |
| | | if (!empty($get)) { |
| | | $allImages = $this->meta->getAll($get); |
| | | $actualGet = array_map(function($fieldName) { |
| | | return (str_contains($fieldName, ':')) ? strtok($fieldName, ':') : $fieldName; |
| | | }, $get); |
| | | $complex = array_map(function($item) { |
| | | return explode(':', $item); |
| | | },array_filter($get, function($fieldName) { |
| | | return str_contains($fieldName, ':'); |
| | | })); |
| | | |
| | | $allImages = $this->meta->getAll($actualGet); |
| | | foreach ($allImages as $k => $imgs) { |
| | | $temp = explode(',', $imgs); |
| | | foreach ($temp as $img) { |
| | | if (is_numeric($img) && !array_key_exists($img, $images)) { |
| | | $images[$img] = jvbImageData((int)$img); |
| | | //It's a complex field |
| | | if (is_array($imgs)) { |
| | | foreach ($imgs as $row) { |
| | | foreach ($row as $fName => $fValue) { |
| | | foreach ($complex as $c) { |
| | | if (in_array($k, $c)) { |
| | | foreach ($c as $complexField) { |
| | | if ($complexField === $fName) { |
| | | $images = $this->addImages($fValue, $images); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | $images = $this->addImages($imgs, $images); |
| | | } |
| | | } |
| | | } |
| | | return $images; |
| | | } |
| | | public function addImages(string $imgs, array $images):array |
| | | { |
| | | $temp = explode(',', $imgs); |
| | | foreach ($temp as $img) { |
| | | if (is_numeric($img) && !array_key_exists($img, $images)) { |
| | | $images[$img] = jvbImageData((int)$img); |
| | | } |
| | | } |
| | | return $images; |
| | | } |
| | | public function getUploadFields($fields):array |
| | | { |
| | | $get = []; |
| | | foreach ($fields as $field => $config) { |
| | | if (in_array($config['type'], ['group', 'repeater'])) { |
| | | $nestedUploads = $this->getUploadFields($config['fields']); |
| | | foreach ($nestedUploads as $nested) { |
| | | $get[] = $field.':'.$nested; |
| | | } |
| | | } elseif ($config['type'] === 'upload' || $config['type'] === 'gallery' || $config['type'] === 'image' ) { |
| | | $get[] = $field; |
| | | } |
| | | } |
| | | |
| | | return $get; |
| | | } |
| | | |
| | | public function formatTimeline(WP_Post $post): array |
| | | { |