CollectionItemResource.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Resources\CollectionItem; |
| 4 | |
| 5 | use Kirki\App\Constants\Collection\CustomFieldTypes; |
| 6 | use Kirki\App\Supports\ContentManager; |
| 7 | use Kirki\Framework\Resource; |
| 8 | |
| 9 | /** |
| 10 | * Presentation for a content manager collection item (child post). |
| 11 | * |
| 12 | * Field values are formatted here from eager-loaded relations: reference |
| 13 | * values are grouped from the `references` relation by `field_meta_key`, and |
| 14 | * non-reference values are read from the item's post meta. |
| 15 | */ |
| 16 | class CollectionItemResource extends Resource |
| 17 | { |
| 18 | /** |
| 19 | * Convert the collection item resource to an array. |
| 20 | * |
| 21 | * @return array The collection item data as an associative array. |
| 22 | */ |
| 23 | public function to_array() |
| 24 | { |
| 25 | return [ |
| 26 | 'ID' => (int) $this->ID, |
| 27 | 'post_author' => (int) $this->post_author, |
| 28 | 'post_date' => $this->post_date, |
| 29 | 'post_content' => $this->post_content, |
| 30 | 'post_title' => $this->post_title, |
| 31 | 'post_excerpt' => $this->post_excerpt, |
| 32 | 'post_status' => $this->post_status, |
| 33 | 'post_name' => $this->post_name, |
| 34 | 'pinged' => $this->pinged, |
| 35 | 'post_modified' => $this->post_modified, |
| 36 | 'post_parent' => (int) $this->post_parent, |
| 37 | 'url' => home_url('?p=' . $this->ID), |
| 38 | 'menu_order' => (int) $this->menu_order, |
| 39 | 'post_type' => $this->post_type, |
| 40 | 'comment_count' => (int) $this->comment_count, |
| 41 | |
| 42 | 'fields' => $this->format_fields() ?? [], |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Build the item's field values keyed by field id. |
| 48 | * |
| 49 | * Reference / multi-reference fields are resolved from the grouped |
| 50 | * `references` relation; all other fields are read from the eager-loaded |
| 51 | * `fields` (post meta) relation, falling back to the field's default value. |
| 52 | * |
| 53 | * @return array |
| 54 | */ |
| 55 | protected function format_fields() |
| 56 | { |
| 57 | $grouped_references = $this->grouped_references(); |
| 58 | $non_reference_fields = $this->non_reference_fields(); |
| 59 | $fields = []; |
| 60 | |
| 61 | foreach ($this->custom_fields() as $parent_field) { |
| 62 | $meta_key = ContentManager::get_child_post_meta_key_using_field_id($this->post_parent, $parent_field['id']); |
| 63 | $type = $parent_field['type'] ?? ''; |
| 64 | |
| 65 | if ($type === CustomFieldTypes::REFERENCE || $type === CustomFieldTypes::MULTI_REFERENCE) { |
| 66 | $fields[$parent_field['id']] = $grouped_references[$meta_key] ?? []; |
| 67 | } elseif (array_key_exists($meta_key, $non_reference_fields)) { |
| 68 | $fields[$parent_field['id']] = $non_reference_fields[$meta_key]; |
| 69 | } else { |
| 70 | $fields[$parent_field['id']] = $parent_field['default_value'] ?? ''; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $fields; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Map the item's eager-loaded field meta as meta_key => meta_value. |
| 79 | * |
| 80 | * @return array<string, mixed> |
| 81 | */ |
| 82 | protected function non_reference_fields() |
| 83 | { |
| 84 | $map = []; |
| 85 | |
| 86 | foreach ($this->fields ?? [] as $meta) { |
| 87 | $map[$meta->meta_key] = $meta->meta_value; |
| 88 | } |
| 89 | |
| 90 | return $map; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Group the item's reference rows by `field_meta_key`. |
| 95 | * |
| 96 | * @return array<string, array<int, array{value: int, title: string}>> |
| 97 | */ |
| 98 | protected function grouped_references() |
| 99 | { |
| 100 | $grouped = []; |
| 101 | |
| 102 | foreach ($this->references ?? [] as $reference) { |
| 103 | $grouped[$reference->field_meta_key][] = [ |
| 104 | 'value' => (int) $reference->ref_post_id, |
| 105 | 'title' => get_the_title($reference->ref_post_id), |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | return $grouped; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * The collection's custom fields definitions. |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | protected function custom_fields() |
| 118 | { |
| 119 | static $cache = []; |
| 120 | |
| 121 | if (!isset($cache[$this->post_parent])) { |
| 122 | $item_with_relation = $this->load_missing('collection.fields'); |
| 123 | $collection = $item_with_relation->collection ?? null; |
| 124 | $fields = !is_null($collection->fields) && !empty($collection->fields->meta_value) ? $collection->fields->meta_value : []; |
| 125 | |
| 126 | $cache[$this->post_parent] = $fields; |
| 127 | } |
| 128 | |
| 129 | return is_array($cache[$this->post_parent]) ? $cache[$this->post_parent] : []; |
| 130 | } |
| 131 | } |
| 132 |