Facades
3 weeks ago
Canvas.php
3 weeks ago
CollectionItem.php
3 weeks ago
ContentManager.php
3 weeks ago
DateTime.php
3 weeks ago
EditorPreview.php
3 weeks ago
FileHandler.php
3 weeks ago
FilterHooks.php
3 weeks ago
PageUrl.php
3 weeks ago
Role.php
3 weeks ago
Template.php
3 weeks ago
CollectionItem.php
131 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 | 'number' => $limit, |
| 86 | 'orderby' => 'ID', |
| 87 | 'order' => 'DESC', |
| 88 | ]; |
| 89 | |
| 90 | if ($search) { |
| 91 | $arg['search'] = $search; |
| 92 | } |
| 93 | |
| 94 | $terms = get_terms($arg); |
| 95 | |
| 96 | if (!is_wp_error($terms) && is_array($terms)) { |
| 97 | foreach ($terms as $term) { |
| 98 | // Handle both object and array cases safely |
| 99 | $term_id = is_object($term) ? $term->term_id : ($term['term_id'] ?? null); |
| 100 | $term_name = is_object($term) ? $term->name : ($term['name'] ?? null); |
| 101 | |
| 102 | $data[] = [ |
| 103 | 'ID' => $term_id, |
| 104 | 'title' => $term_name, |
| 105 | ]; |
| 106 | } |
| 107 | } |
| 108 | break; |
| 109 | case CollectionTypes::USER: |
| 110 | $role === '*' ? '' : $role; |
| 111 | $users = UserModel::query() |
| 112 | ->role($role) |
| 113 | ->search($search) |
| 114 | ->order_by('ID', 'DESC') |
| 115 | ->limit($limit) |
| 116 | ->get(['ID', 'display_name']); |
| 117 | |
| 118 | foreach ($users as $user) { |
| 119 | $data[] = [ |
| 120 | 'ID' => $user->ID, |
| 121 | 'title' => $user->display_name, |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return ['data' => $data, 'type' => $type]; |
| 130 | } |
| 131 | } |