PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / trunk
Kirki – Freeform Page Builder, Website Builder & Customizer vtrunk
6.3.0 6.2.5 6.2.4 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 / Http / Controllers / Api / CollectionItemController.php
kirki / app / Http / Controllers / Api Last commit date
AppsController.php 2 months ago CollaborationCommentController.php 1 day ago CollaborationController.php 1 month ago CollectionController.php 1 month ago CollectionItemController.php 1 day ago EditorController.php 1 month ago FormController.php 3 weeks ago GlobalDataController.php 1 month ago MediaController.php 2 months ago PageController.php 1 month ago PageSettingsController.php 1 month ago PostController.php 1 month ago UserController.php 1 month ago
CollectionItemController.php
145 lines
1 <?php
2
3 namespace Kirki\App\Http\Controllers\Api;
4
5 use Kirki\App\Constants\Collection\ActionTypes;
6 use Kirki\App\DTO\CollectionItemListFilterDTO;
7 use Kirki\App\DTO\UpsertCollectionItemDTO;
8 use Kirki\App\Http\Requests\CollectionItem\CollectionItemBulkActionRequest;
9 use Kirki\App\Http\Requests\CollectionItem\CollectionItemSingleActionRequest;
10 use Kirki\App\Http\Requests\CollectionItem\CollectionItemStoreRequest;
11 use Kirki\App\Resources\CollectionItem\CollectionItemResource;
12 use Kirki\App\Services\CollectionItemService;
13 use Kirki\Framework\Http\Request;
14
15 use Kirki\Framework\Http\Response;
16 use function Kirki\Framework\response;
17
18 class CollectionItemController
19 {
20 /**
21 * @var CollectionItemService
22 */
23 protected $service;
24
25 public function __construct(CollectionItemService $service)
26 {
27 $this->service = $service;
28 }
29
30 /**
31 * Paginated listing of a collection's items.
32 */
33 public function index(Request $request)
34 {
35 $filters = CollectionItemListFilterDTO::from_array([
36 'post_parent' => $request->int('post_parent'),
37 'page' => $request->int('page', 1),
38 'limit' => $request->int('limit', 20),
39 'query' => $request->string('query', ''),
40 'exclude_post_ids' => $request->array('exclude_post_ids', []),
41 ]);
42
43 $filter_data = $request->array('filter', []);
44
45 if (!empty($filter_data)) {
46 $filters->post_status = $filter_data['post_status'] ?? $filters->post_status;
47 $filters->post_date = $filter_data['post_date'] ?? $filters->post_date;
48 $filters->post_modified = $filter_data['post_modified'] ?? $filters->post_modified;
49 }
50
51 $paginated_data = $this->service->paginated($filters);
52
53 return response()->json([
54 'data' => CollectionItemResource::paginated($paginated_data),
55 'message' => __('Items retrieved successfully.', 'kirki'),
56 ]);
57 }
58
59 /**
60 * Single item.
61 */
62 public function show(Request $request)
63 {
64 $item = $this->service->get_single($request->int('item'));
65
66 return response()->json([
67 'data' => CollectionItemResource::make($item),
68 'message' => __('Item retrieved successfully.', 'kirki'),
69 ]);
70 }
71
72 /**
73 * Create or update an item.
74 */
75 public function store(CollectionItemStoreRequest $request)
76 {
77 $dto = UpsertCollectionItemDTO::from_array($request->sanitized());
78 $item = $this->service->create_or_update($dto);
79
80 return response()->json([
81 'data' => CollectionItemResource::make($item),
82 'message' => __('Item saved successfully.', 'kirki'),
83 ]);
84 }
85
86 /**
87 * Delete or duplicate a single item.
88 */
89 public function action(CollectionItemSingleActionRequest $request)
90 {
91 $post_id = $request->post_id;
92 $action = $request->action;
93
94 $data = false;
95
96 switch ($action) {
97 case ActionTypes::DELETE:
98 $data = $this->service->delete($post_id);
99 break;
100 case ActionTypes::DUPLICATE:
101 $item = $this->service->duplicate($post_id);
102 $data = $item ? CollectionItemResource::make($item) : null;
103 break;
104 default:
105 break;
106 }
107
108 return response()->json([
109 'data' => $data,
110 'message' => __('Action handled successfully.', 'kirki'),
111 ]);
112 }
113
114 /**
115 * Bulk delete or duplicate items.
116 */
117 public function bulk_action(CollectionItemBulkActionRequest $request)
118 {
119 $post_ids = $request->array('post_ids', []);
120 $parent_id = $request->int('post_parent');
121 $action = $request->string('action');
122
123 $data = false;
124
125 switch ($action) {
126 case ActionTypes::DELETE:
127 $data = $this->service->bulk_delete($post_ids, $parent_id);
128 break;
129 case ActionTypes::DUPLICATE:
130 $data = CollectionItemResource::collection($this->service->bulk_duplicate($post_ids, $parent_id));
131 break;
132 case ActionTypes::DRAFT:
133 $data = $this->service->bulk_draft($post_ids, $parent_id);
134 break;
135 default:
136 break;
137 }
138
139 return response()->json([
140 'data' => $data,
141 'message' => __('Bulk action handled successfully.', 'kirki'),
142 ]);
143 }
144 }
145