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