PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
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 / Supports / CollectionItem.php
kirki / app / Supports Last commit date
Facades 5 days ago Form 5 days ago ActionHooks.php 5 days ago Canvas.php 1 month ago CollectionItem.php 5 days ago ContentManager.php 1 month ago DateTime.php 1 month ago EditorPreview.php 2 weeks ago FileHandler.php 2 weeks ago FilterHooks.php 2 weeks ago PageUrl.php 5 days ago Recaptcha.php 5 days ago Role.php 1 month ago Session.php 5 days ago Template.php 5 days ago
CollectionItem.php
132 lines
1 <?php
2
3 namespace Kirki\App\Supports;
4
5 defined('ABSPATH') || exit;
6
7 use Kirki\App\Constants\CollectionTypes;
8 use Kirki\App\Constants\PostTypes;
9 use Kirki\App\Models\Post as PostModel;
10 use Kirki\App\Models\User as UserModel;
11
12 use function Kirki\App\is_not_empty_array;
13 use function Kirki\Framework\user;
14
15 class CollectionItem
16 {
17 /**
18 * Get items from conditions
19 *
20 * @param array $conditions
21 * @param string $search
22 * @return array
23 */
24 public static function get_items_from_condition($conditions, $search = '')
25 {
26 $data = [];
27 $type = CollectionTypes::POST;
28 $post_type = PostTypes::WP_POST;
29 $role = '*';
30
31 if (is_not_empty_array($conditions)) {
32 if (isset($conditions[0]['type'])) {
33 $type = $conditions[0]['type'];
34
35 if ($type === CollectionTypes::POST) {
36 $post_type = $conditions[0]['post_type'];
37
38 // if the conditions has from key then it is term. then it will be term type.
39 if (isset($conditions[0]['from']) && $conditions[0]['from'] === CollectionTypes::TERM) {
40 $type = CollectionTypes::TERM;
41 }
42 }
43
44 if ($type === CollectionTypes::USER) {
45 $role = $conditions[0]['to'];
46 }
47
48 } else {
49 //legacy support
50 $post_type = $conditions[0]['category'];
51 }
52 }
53
54 if (user()->has_edit_access()) {
55 $limit = 20;
56
57 switch ($type) {
58 case CollectionTypes::POST:
59 $posts = PostModel::query()
60 ->where('post_type', $post_type)
61 ->where_in('post_status', ['publish', 'draft', 'future'])
62 ->search($search)
63 ->order_by('ID', 'DESC')
64 ->limit($limit)
65 ->get(['ID', 'post_title']);
66
67 foreach ($posts as $post) {
68 $data[] = [
69 'ID' => $post->ID,
70 'title' => $post->post_title,
71 ];
72 }
73 break;
74 case CollectionTypes::TERM:
75 $taxonomy = [];
76
77 foreach ($conditions as $key => $condition) {
78 if ($condition['from'] === CollectionTypes::TERM) {
79 $taxonomy[] = $condition['where'];
80 }
81 }
82
83 $arg = [
84 'taxonomy' => $taxonomy,
85 'hide_empty' => false,
86 'number' => $limit,
87 'orderby' => 'ID',
88 'order' => 'DESC',
89 ];
90
91 if ($search) {
92 $arg['search'] = $search;
93 }
94
95 $terms = get_terms($arg);
96
97 if (!is_wp_error($terms) && is_array($terms)) {
98 foreach ($terms as $term) {
99 // Handle both object and array cases safely
100 $term_id = is_object($term) ? $term->term_id : ($term['term_id'] ?? null);
101 $term_name = is_object($term) ? $term->name : ($term['name'] ?? null);
102
103 $data[] = [
104 'ID' => $term_id,
105 'title' => $term_name,
106 ];
107 }
108 }
109 break;
110 case CollectionTypes::USER:
111 $role = $role === '*' ? '' : $role;
112 $users = UserModel::query()
113 ->role($role)
114 ->search($search)
115 ->order_by('ID', 'DESC')
116 ->limit($limit)
117 ->get(['ID', 'display_name']);
118
119 foreach ($users as $user) {
120 $data[] = [
121 'ID' => $user->ID,
122 'title' => $user->display_name,
123 ];
124 }
125
126 break;
127 }
128 }
129
130 return ['data' => $data, 'type' => $type];
131 }
132 }