PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / ajax / entry.php

entry.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/ajax/entry.php

287 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Ajax;
4
5 use ABlocks\Classes\Sanitizer;
6 use ABlocks\Blocks\FormBuilder\Query;
7 use ABlocks\Classes\AbstractAjaxHandler;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * @class Entry
14 * Ajax handler for entries
15 */
16 class Entry extends AbstractAjaxHandler {
17 public function __construct() {
18 $this->actions = [
19 /** Get entries with pagination */
20 'get_entries' => [
21 'callback' => [ $this, 'get_entries' ],
22 'fields' => array(
23 'is_in_trash' => 'string', // yes/no; default: no
24 'search' => 'string', // name of form
25 'form_type' => 'string', // name of form
26 'order' => 'string', // asc/desc
27 'order_by' => 'string', // created_at, id,
28 'date' => 'string', // date : format : y-m; eg: 2024-12, get entries by a month of year;
29 'from_date' => 'string', // date : y-m-d
30 'to_date' => 'string', // date : y-m-d
31 'status' => 'string', // all/read/unread
32 'per_page' => 'integer', // int: default = 15
33 'current_page' => 'integer', // int
34 'export_csv' => 'integer', // int
35 )
36 ],
37 /** Get single entry */
38 'get_entry' => [
39 'callback' => [ $this, 'get_entry' ],
40 'fields' => array(
41 'entry_id' => 'integer', // int : entry id
42 )
43 ],
44 /** Edit entry */
45 'edit_entry' => [
46 'callback' => [ $this, 'edit_entry' ],
47 'fields' => array(
48 'entry_id' => 'integer', // int : entry id
49 'data' => 'array',
50 )
51 ],
52 /** Delete entries */
53 'delete_entries' => [
54 'callback' => [ $this, 'delete_entries' ],
55 'fields' => array(
56 'entry_id_array' => 'array',
57 )
58 ],
59 /** Mark entries as read */
60 'mark_entries_as_read' => [
61 'callback' => [ $this, 'mark_entries_as_read' ],
62 'fields' => array(
63 'entry_id_array' => 'array',
64 )
65 ],
66 /** Mark entries as unread */
67 'mark_entries_as_unread' => [
68 'callback' => [ $this, 'mark_entries_as_unread' ],
69 'fields' => array(
70 'entry_id_array' => 'array',
71 )
72 ],
73 /** Send one or more entries to trash */
74 'send_entries_to_trash' => [
75 'callback' => [ $this, 'send_entries_to_trash' ],
76 'fields' => array(
77 'entry_id' => 'integer', // int : entry id
78 )
79 ],
80 /** Send one or more entries to trash */
81 'restore_entries_from_trash' => [
82 'callback' => [ $this, 'restore_entries_from_trash' ],
83 'fields' => array(
84 'entry_id' => 'integer', // int : entry id
85 )
86 ],
87 ];
88 }
89
90 /**
91 * Get entries with pagination & filter
92 * Available filter options:
93 * -> by a month of year, from a date, to a date, within two date
94 * -> by status: read, unread, all
95 * -> form_type : name of form
96 * Response like:
97 * Array
98 * (
99 * [total_entries] => Integer
100 * [current_page] => Integer
101 * [per_page] => Integer
102 * [available_dates] => Array
103 * (
104 * [0] => Array
105 * (
106 * [total_entries] => Integer: total number of entries that month
107 * [year] => Integer: eg: 2024
108 * [month] => Integer: range: 1-12
109 * )
110 *
111 * )
112 * [available_forms] => Array
113 * (
114 * [0] => Array
115 * (Form Nametotal number of entries that month
116 * )
117 *
118 * )
119 *
120 * [data] => Array: a associative array
121 * )
122 * Processes the form data.
123 *
124 * @param array $form_data The form data submitted by the user.
125 * @return void
126 */
127 public function get_entries( $payload ) {
128 wp_send_json_success(Query::get_entries(
129 $payload['is_in_trash'] ?? 'no',
130 $payload['search'] ?? null,
131 $payload['form_type'] ?? null,
132 $payload['date'] ?? null,
133 $payload['from_date'] ?? null,
134 $payload['to_date'] ?? null,
135 $payload['order_by'] ?? 'created_at',
136 $payload['order'] ?? 'DESC',
137 $payload['status'] ?? 'all',
138 $payload['per_page'] ?? 10,
139 $payload['current_page'] ?? 1,
140 $payload['export_csv'] ?? 0,
141 ));
142 }
143
144 public function get_entry( $payload ) {
145 if ( ! isset( $payload['entry_id'] ) ) {
146 wp_send_json_error( [ 'message' => __( 'Error', 'ablocks' ) ] );
147 }
148
149 $entry = Query::get_entry( $payload['entry_id'] );
150
151 if ( ! empty( $entry ) ) {
152 wp_send_json_success( $entry );
153 }
154 wp_send_json_error( [ 'message' => __( 'Entry not found', 'ablocks' ) ] );
155 }
156
157 /**
158 * Edit metadata of a single entry.
159 *
160 * @param array $form_data The form data containing metadata details.
161 * @return void
162 */
163 public function edit_entry( $payload ) {
164 if ( ! isset( $payload['entry_id'] ) || ! isset( $payload['data'] ) ) {
165 wp_send_json_error( [ 'message' => __( 'Error', 'ablocks' ) ] );
166 }
167
168 if ( Query::edit_entry( $payload['entry_id'], (array) $payload['data'] ) ) {
169 wp_send_json_success( [ 'message' => __( 'Success!', 'ablocks' ) ] );
170 }
171
172 wp_send_json_error( [ 'message' => __( 'Error', 'ablocks' ) ] );
173 }
174
175 /**
176 * Delete one or more entries by entry ID.
177 *
178 * @param array $form_data The form data containing entry IDs to delete.
179 * @return void
180 */
181 public function delete_entries( $payload ) {
182 $deleted_entries = Query::delete_entries( $payload['entry_id_array'] ?? [] );
183 $num_of_entries_deleted = count( $deleted_entries );
184 if ( $num_of_entries_deleted === 0 ) {
185 wp_send_json_error([
186 'message' => __( 'No entries deleted.', 'ablocks' ),
187 'num_of_deleted_entries' => 0,
188 'id_of_deleted_entries' => $deleted_entries,
189 ]);
190 }
191 wp_send_json_success([
192 // translators: %d is the number of entries deleted
193 'message' => sprintf( __( '%d entries deleted.', 'ablocks' ), $num_of_entries_deleted ),
194 'num_of_deleted_entries' => $num_of_entries_deleted,
195 'id_of_deleted_entries' => $deleted_entries,
196 ]);
197 }
198
199 /**
200 * Mark one or more entries as updated by entry ID.
201 *
202 * @param array $form_data The form data containing entry IDs to mark as updated.
203 * @return void
204 */
205 public function mark_entries_as_read( $payload ) {
206
207 $updated_entries = Query::update_status_of_entries( $payload['entry_id_array'] ?? [], 'read' );
208 $num_of_entries_updated = count( $updated_entries );
209 if ( $num_of_entries_updated === 0 ) {
210 wp_send_json_error([
211 'message' => __( 'No entries updated.', 'ablocks' ),
212 'num_of_updated_entries' => 0,
213 'id_of_updated_entries' => $updated_entries,
214 ]);
215 }
216 wp_send_json_success([
217 // translators: %d is the number of entries updated
218 'message' => sprintf( __( '%d entries updated.', 'ablocks' ), $num_of_entries_updated ),
219 'num_of_updated_entries' => $num_of_entries_updated,
220 'id_of_updated_entries' => $updated_entries,
221 ]);
222 }
223
224 /**
225 * Mark one or more entries as updated by entry ID.
226 *
227 * @param array $form_data The form data containing entry IDs to mark as updated.
228 * @return void
229 */
230 public function mark_entries_as_unread( $payload ) {
231 $updated_entries = Query::update_status_of_entries( $payload['entry_id_array'] ?? [], 'unread' );
232 $num_of_entries_updated = count( $updated_entries );
233 if ( $num_of_entries_updated === 0 ) {
234 wp_send_json_error([
235 'message' => __( 'No entries updated.', 'ablocks' ),
236 'num_of_updated_entries' => 0,
237 'id_of_updated_entries' => $updated_entries,
238 ]);
239 }
240 wp_send_json_success([
241 // translators: %d is the number of entries updated
242 'message' => sprintf( __( '%d entries updated.', 'ablocks' ), $num_of_entries_updated ),
243 'num_of_updated_entries' => $num_of_entries_updated,
244 'id_of_updated_entries' => $updated_entries,
245 ]);
246 }
247
248 /**
249 * Send one or more entries to trash.
250 *
251 * @param array $form_data The form data containing entry IDs to move to trash.
252 * @return void
253 */
254 public function send_entries_to_trash( $payload ) {
255 $updated = Query::update_trash_status_of_entries( $payload['entry_id'] ?? 0, 'yes' );
256 if ( empty( $updated ) ) {
257 wp_send_json_error([
258 'message' => __( 'No entries updated.', 'ablocks' ),
259 ]);
260 }
261 wp_send_json_success([
262 'message' => __( 'Updated.', 'ablocks' ),
263 'data' => $updated,
264 ]);
265 }
266
267 /**
268 * Restore one or more entries from trash.
269 *
270 * @param array $form_data The form data containing entry IDs to restore.
271 * @return void
272 */
273 public function restore_entries_from_trash( $payload ) {
274 $updated = Query::update_trash_status_of_entries( $payload['entry_id'] ?? 0, 'no' );
275
276 if ( empty( $updated ) ) {
277 wp_send_json_error([
278 'message' => __( 'No entries updated.', 'ablocks' ),
279 ]);
280 }
281 wp_send_json_success([
282 'message' => __( 'Entry updated.', 'ablocks' ),
283 'data' => $updated,
284 ]);
285 }
286 }
287