PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.2
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.2
6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / app / Resources / CollectionItem / CollectionItemResource.php
kirki / app / Resources / CollectionItem Last commit date
CollectionItemResource.php 4 days ago
CollectionItemResource.php
133 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 $default_val = $parent_field['default_value'] ?? '';
71 $fields[$parent_field['id']] = $default_val !== null ? $default_val : '';
72 }
73 }
74
75 return $fields;
76 }
77
78 /**
79 * Map the item's eager-loaded field meta as meta_key => meta_value.
80 *
81 * @return array<string, mixed>
82 */
83 protected function non_reference_fields()
84 {
85 $map = [];
86
87 foreach ($this->fields ?? [] as $meta) {
88 $map[$meta->meta_key] = $meta->meta_value;
89 }
90
91 return $map;
92 }
93
94 /**
95 * Group the item's reference rows by `field_meta_key`.
96 *
97 * @return array<string, array<int, array{value: int, title: string}>>
98 */
99 protected function grouped_references()
100 {
101 $grouped = [];
102
103 foreach ($this->references ?? [] as $reference) {
104 $grouped[$reference->field_meta_key][] = [
105 'value' => (int) $reference->ref_post_id,
106 'title' => get_the_title($reference->ref_post_id),
107 ];
108 }
109
110 return $grouped;
111 }
112
113 /**
114 * The collection's custom fields definitions.
115 *
116 * @return array
117 */
118 protected function custom_fields()
119 {
120 static $cache = [];
121
122 if (!isset($cache[$this->post_parent])) {
123 $item_with_relation = $this->load_missing('collection.fields');
124 $collection = $item_with_relation->collection ?? null;
125 $fields = !is_null($collection->fields) && !empty($collection->fields->meta_value) ? $collection->fields->meta_value : [];
126
127 $cache[$this->post_parent] = $fields;
128 }
129
130 return is_array($cache[$this->post_parent]) ? $cache[$this->post_parent] : [];
131 }
132 }
133