CollectionResource.php
47 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Resources\Collection; |
| 4 | |
| 5 | use Kirki\Framework\Resource; |
| 6 | |
| 7 | class CollectionResource extends Resource |
| 8 | { |
| 9 | /** |
| 10 | * Convert the collection resource to an array. |
| 11 | * |
| 12 | * @return array The collection data as an associative array. |
| 13 | */ |
| 14 | public function to_array() |
| 15 | { |
| 16 | $basic_fields = $this->basic_fields ? ($this->basic_fields->meta_value ?? null) : null; |
| 17 | |
| 18 | if (empty($basic_fields)) { |
| 19 | $basic_fields = [ |
| 20 | 'post_title' => ['title' => 'Name', 'help_text' => ''], |
| 21 | 'post_name' => ['title' => 'Slug', 'help_text' => ''], |
| 22 | ]; |
| 23 | } |
| 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 | 'post_modified' => $this->post_modified, |
| 35 | 'post_parent' => (int) $this->post_parent, |
| 36 | 'url' => home_url('?p=' . $this->ID), |
| 37 | 'menu_order' => (int) $this->menu_order, |
| 38 | 'post_type' => $this->post_type, |
| 39 | 'comment_count' => (int) $this->comment_count, |
| 40 | |
| 41 | 'fields' => !empty($this->fields->meta_value) ? $this->fields->meta_value : [], |
| 42 | 'item_count' => $this->items_count ?? 0, |
| 43 | 'basic_fields' => $basic_fields, |
| 44 | ]; |
| 45 | } |
| 46 | } |
| 47 |