← All changes
|
includes/api/class-weforms-entries-controller.php
+327
-327
1.6.13
→
1.4.5
View file →
| @@ -1,327 +1,327 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Entry manager class | |
| 5 | - * | |
| 6 | - * @since 1.4.2 | |
| 7 | - */ | |
| 8 | -class Weforms_Entry_Controller extends Weforms_REST_Controller { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Endpoint namespace | |
| 12 | - * | |
| 13 | - * @var string | |
| 14 | - */ | |
| 15 | - protected $namespace = 'weforms/v1'; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Route name | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $rest_base = 'entries'; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Register all routes releated with forms | |
| 26 | - * | |
| 27 | - * @return void | |
| 28 | - */ | |
| 29 | - public function register_routes() { | |
| 30 | - register_rest_route( | |
| 31 | - $this->namespace, | |
| 32 | - '/' . $this->rest_base . '/(?P<entry_id>[\d]+)', | |
| 33 | - [ | |
| 34 | - [ | |
| 35 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 36 | - 'callback' => [ $this, 'delete_items' ], | |
| 37 | - 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 38 | - 'args' => [ | |
| 39 | - 'entry_id' => [ | |
| 40 | - 'required' => true, | |
| 41 | - 'type' => 'integer', | |
| 42 | - 'sanitize_callback' => 'absint', | |
| 43 | - 'description' => __( 'Entry id', 'weforms' ), | |
| 44 | - 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 45 | - ], | |
| 46 | - 'force' => [ | |
| 47 | - 'type' => 'boolean', | |
| 48 | - 'default' => false, | |
| 49 | - 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ), | |
| 50 | - ], | |
| 51 | - ], | |
| 52 | - ], | |
| 53 | - ] | |
| 54 | - ); | |
| 55 | - | |
| 56 | - register_rest_route( | |
| 57 | - $this->namespace, | |
| 58 | - '/' . $this->rest_base, | |
| 59 | - [ | |
| 60 | - [ | |
| 61 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 62 | - 'callback' => [ $this, 'bulk_delete_items' ], | |
| 63 | - 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 64 | - 'args' => [ | |
| 65 | - 'entry_id' => [ | |
| 66 | - 'required' => true, | |
| 67 | - 'type' => 'object', | |
| 68 | - 'description' => __( 'Entry id', 'weforms' ), | |
| 69 | - 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 70 | - ], | |
| 71 | - 'force' => [ | |
| 72 | - 'type' => 'boolean', | |
| 73 | - 'default' => false, | |
| 74 | - 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ), | |
| 75 | - ], | |
| 76 | - ], | |
| 77 | - ], | |
| 78 | - ] | |
| 79 | - ); | |
| 80 | - | |
| 81 | - register_rest_route( | |
| 82 | - $this->namespace, | |
| 83 | - '/' . $this->rest_base . '/(?P<entry_id>[\d]+)/restore', | |
| 84 | - [ | |
| 85 | - [ | |
| 86 | - 'methods' => WP_REST_Server::READABLE, | |
| 87 | - 'callback' => [ $this, 'restore_items' ], | |
| 88 | - 'permission_callback' => [ $this, 'update_item_permissions_check' ], | |
| 89 | - 'args' => [ | |
| 90 | - 'entry_id' => [ | |
| 91 | - 'required' => true, | |
| 92 | - 'type' => 'integer', | |
| 93 | - 'sanitize_callback' => 'absint', | |
| 94 | - 'description' => __( 'Entry id', 'weforms' ), | |
| 95 | - 'validate_callback' => [ $this, 'is_restore_exists' ], | |
| 96 | - ], | |
| 97 | - ], | |
| 98 | - ], | |
| 99 | - ] | |
| 100 | - ); | |
| 101 | - | |
| 102 | - register_rest_route( | |
| 103 | - $this->namespace, | |
| 104 | - '/' . $this->rest_base . '/restore', | |
| 105 | - [ | |
| 106 | - [ | |
| 107 | - 'methods' => WP_REST_Server::READABLE, | |
| 108 | - 'callback' => [ $this, 'bulk_restore_items' ], | |
| 109 | - 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 110 | - 'args' => [ | |
| 111 | - 'entry_id' => [ | |
| 112 | - 'required' => true, | |
| 113 | - 'type' => 'object', | |
| 114 | - 'description' => __( 'Entry id', 'weforms' ), | |
| 115 | - 'validate_callback' => [ $this, 'is_restore_exists' ], | |
| 116 | - ], | |
| 117 | - ], | |
| 118 | - ], | |
| 119 | - ] | |
| 120 | - ); | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * Restore Items From Trash | |
| 125 | - * | |
| 126 | - * @since 1.4.2 | |
| 127 | - * | |
| 128 | - * @param WP_REST_Request $request | |
| 129 | - * | |
| 130 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 131 | - **/ | |
| 132 | - public function bulk_restore_items( $request ) { | |
| 133 | - $entry_ids = isset( $request['entry_id'] ) ? array_map( 'absint', $request['entry_id'] ) : []; | |
| 134 | - | |
| 135 | - if ( !$entry_ids ) { | |
| 136 | - return new WP_Error( 'rest_weforms_invalid_entry', __( 'No entry ids provided!', 'weforms' ), [ 'status' => 404 ] ); | |
| 137 | - } | |
| 138 | - | |
| 139 | - foreach ( $entry_ids as $entry_id ) { | |
| 140 | - $status = weforms_change_entry_status( $entry_id, 'publish' ); | |
| 141 | - | |
| 142 | - if ( $status ) { | |
| 143 | - $response['message'][$entry_id] = __( 'Entry Restore successfully ', 'weforms' ); | |
| 144 | - } else { | |
| 145 | - $response['message'][$entry_id] = __( 'Entry Restore failed ', 'weforms' ); | |
| 146 | - } | |
| 147 | - } | |
| 148 | - | |
| 149 | - $response = $this->prepare_response_for_collection( $response ); | |
| 150 | - $response = rest_ensure_response( $response ); | |
| 151 | - | |
| 152 | - return $response; | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * Restore Item From Trash | |
| 157 | - * | |
| 158 | - * @since 1.4.2 | |
| 159 | - * | |
| 160 | - * @param WP_REST_Request $request | |
| 161 | - * | |
| 162 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 163 | - **/ | |
| 164 | - public function restore_items( $request ) { | |
| 165 | - $entry_id = isset( $request['entry_id'] ) ? intval( $request['entry_id'] ) : 0; | |
| 166 | - | |
| 167 | - weforms_change_entry_status( $entry_id, 'publish' ); | |
| 168 | - | |
| 169 | - $response['id'] = $entry_id; | |
| 170 | - $response['message'] = __( 'Entry Restore Successfully successfully ', 'weforms' ); | |
| 171 | - $response['data']['status'] = 200; | |
| 172 | - | |
| 173 | - $response = $this->prepare_response_for_collection( $response ); | |
| 174 | - $response = rest_ensure_response( $response ); | |
| 175 | - | |
| 176 | - return $response; | |
| 177 | - } | |
| 178 | - | |
| 179 | - /** | |
| 180 | - * Bulk Delele Item | |
| 181 | - * | |
| 182 | - * @since 1.4.2 | |
| 183 | - * | |
| 184 | - * @param WP_REST_Request $request | |
| 185 | - * | |
| 186 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 187 | - **/ | |
| 188 | - public function bulk_delete_items( $request ) { | |
| 189 | - $entry_ids = isset( $request['entry_id'] ) ? array_map( 'absint', $request['entry_id'] ) : []; | |
| 190 | - $force = (bool) $request['force']; | |
| 191 | - | |
| 192 | - if ( !$entry_ids ) { | |
| 193 | - return new WP_Error( 'rest_weforms_invalid_entry', __( 'No entry ids provided!', 'weforms' ) ); | |
| 194 | - } | |
| 195 | - | |
| 196 | - $response = []; | |
| 197 | - | |
| 198 | - foreach ( $entry_ids as $entry_id ) { | |
| 199 | - if ( $force ) { | |
| 200 | - $status = weforms_delete_entry( $entry_id ); | |
| 201 | - | |
| 202 | - if ( $status ) { | |
| 203 | - $response['message'][$entry_id] = __( 'Entry Deleted successfully ', 'weforms' ); | |
| 204 | - } else { | |
| 205 | - $response['message'][$entry_id] = __( 'Entry Not found ', 'weforms' ); | |
| 206 | - } | |
| 207 | - } else { | |
| 208 | - $status = weforms_change_entry_status( $entry_id, 'trash' ); | |
| 209 | - | |
| 210 | - if ( $status ) { | |
| 211 | - $response['message'][$entry_id] = __( 'Entry Move To Trash successfully ', 'weforms' ); | |
| 212 | - } else { | |
| 213 | - $response['message'][$entry_id] = __( 'Entry Not found ', 'weforms' ); | |
| 214 | - } | |
| 215 | - } | |
| 216 | - } | |
| 217 | - | |
| 218 | - $response = $this->prepare_response_for_collection( $response ); | |
| 219 | - $response = rest_ensure_response( $response ); | |
| 220 | - | |
| 221 | - return $response; | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * Delele Item | |
| 226 | - * | |
| 227 | - * @since 1.4.2 | |
| 228 | - * | |
| 229 | - * @param WP_REST_Request $request | |
| 230 | - * | |
| 231 | - * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 232 | - **/ | |
| 233 | - public function delete_items( $request ) { | |
| 234 | - $entry_id = $request['entry_id']; | |
| 235 | - $force = (bool) $request['force']; | |
| 236 | - | |
| 237 | - if ( $force ) { | |
| 238 | - weforms_delete_entry( $entry_id ); | |
| 239 | - | |
| 240 | - $entry_array['id'] = $entry_id; | |
| 241 | - $entry_array['message'] = __( 'Entry Deleted successfully', 'weforms' ); | |
| 242 | - $entry_array['data']['status'] = 200; | |
| 243 | - } else { | |
| 244 | - $status = weforms_change_entry_status( $entry_id, 'trash' ); | |
| 245 | - | |
| 246 | - $entry_array['id'] = $entry_id; | |
| 247 | - $entry_array['message'] = __( 'Entry Move To Trash successfully', 'weforms' ); | |
| 248 | - $entry_array['data']['status'] = 200; | |
| 249 | - } | |
| 250 | - | |
| 251 | - $response = $this->prepare_response_for_collection( $entry_array, $request ); | |
| 252 | - $response = rest_ensure_response( $response ); | |
| 253 | - | |
| 254 | - return $response; | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Check is entry in trash exist or not | |
| 259 | - * | |
| 260 | - * @since 1.4.2 | |
| 261 | - * | |
| 262 | - * @param string $param | |
| 263 | - * @param WP_REST_Request $request | |
| 264 | - * @param string $key | |
| 265 | - * | |
| 266 | - * @return bool | |
| 267 | - */ | |
| 268 | - public function is_restore_exists( $param, $request, $key ) { | |
| 269 | - global $wpdb; | |
| 270 | - | |
| 271 | - // if( is_array( $param ) ) { | |
| 272 | - if ( is_array( $request['entry_id'] ) ) { | |
| 273 | - $entry_id = implode( ',', $param ); | |
| 274 | - $querystr = " | |
| 275 | - SELECT $wpdb->weforms_entries.id | |
| 276 | - FROM $wpdb->weforms_entries | |
| 277 | - WHERE $wpdb->weforms_entries.ID IN ( $entry_id ) | |
| 278 | - AND $wpdb->weforms_entries.status = \"trash\" | |
| 279 | - "; | |
| 280 | - } else { | |
| 281 | - // $entry_id = (int) $param; | |
| 282 | - $entry_id = (int) $request['entry_id']; | |
| 283 | - $querystr = " | |
| 284 | - SELECT $wpdb->weforms_entries.id | |
| 285 | - FROM $wpdb->weforms_entries | |
| 286 | - WHERE $wpdb->weforms_entries.ID = $entry_id | |
| 287 | - AND $wpdb->weforms_entries.status = \"trash\" | |
| 288 | - "; | |
| 289 | - } | |
| 290 | - | |
| 291 | - $result = $wpdb->get_results( $querystr ); | |
| 292 | - | |
| 293 | - if ( empty( $result ) ) { | |
| 294 | - return false; | |
| 295 | - } else { | |
| 296 | - return true; | |
| 297 | - } | |
| 298 | - } | |
| 299 | - | |
| 300 | - /** | |
| 301 | - * Get the Form entries schema, conforming to JSON Schema | |
| 302 | - * | |
| 303 | - * @since 1.4.2 | |
| 304 | - * | |
| 305 | - * @return array | |
| 306 | - */ | |
| 307 | - public function get_item_schema() { | |
| 308 | - $schema = [ | |
| 309 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 310 | - 'title' => 'entries', | |
| 311 | - 'type' => 'object', | |
| 312 | - 'properties' => [ | |
| 313 | - 'entry_id' => [ | |
| 314 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 315 | - 'type' => 'integer', | |
| 316 | - 'sanitize_callback' => 'absint', | |
| 317 | - 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 318 | - 'context' => [ 'embed', 'view', 'edit' ], | |
| 319 | - 'required' => true, | |
| 320 | - 'readonly' => true, | |
| 321 | - ], | |
| 322 | - ], | |
| 323 | - ]; | |
| 324 | - | |
| 325 | - return $schema; | |
| 326 | - } | |
| 327 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Entry manager class | |
| 5 | + * | |
| 6 | + * @since 1.4.2 | |
| 7 | + */ | |
| 8 | +class Weforms_Entry_Controller extends Weforms_REST_Controller { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * Endpoint namespace | |
| 12 | + * | |
| 13 | + * @var string | |
| 14 | + */ | |
| 15 | + protected $namespace = 'weforms/v1'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Route name | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + protected $rest_base = 'entries'; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Register all routes releated with forms | |
| 26 | + * | |
| 27 | + * @return void | |
| 28 | + */ | |
| 29 | + public function register_routes() { | |
| 30 | + register_rest_route( | |
| 31 | + $this->namespace, | |
| 32 | + '/' . $this->rest_base . '/(?P<entry_id>[\d]+)', | |
| 33 | + [ | |
| 34 | + [ | |
| 35 | + 'methods' => WP_REST_Server::DELETABLE, | |
| 36 | + 'callback' => [ $this, 'delete_items' ], | |
| 37 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 38 | + 'args' => [ | |
| 39 | + 'entry_id' => [ | |
| 40 | + 'required' => true, | |
| 41 | + 'type' => 'integer', | |
| 42 | + 'sanitize_callback' => 'absint', | |
| 43 | + 'description' => __( 'Entry id', 'weforms' ), | |
| 44 | + 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 45 | + ], | |
| 46 | + 'force' => [ | |
| 47 | + 'type' => 'boolean', | |
| 48 | + 'default' => false, | |
| 49 | + 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ), | |
| 50 | + ], | |
| 51 | + ], | |
| 52 | + ], | |
| 53 | + ] | |
| 54 | + ); | |
| 55 | + | |
| 56 | + register_rest_route( | |
| 57 | + $this->namespace, | |
| 58 | + '/' . $this->rest_base, | |
| 59 | + [ | |
| 60 | + [ | |
| 61 | + 'methods' => WP_REST_Server::DELETABLE, | |
| 62 | + 'callback' => [ $this, 'bulk_delete_items' ], | |
| 63 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 64 | + 'args' => [ | |
| 65 | + 'entry_id' => [ | |
| 66 | + 'required' => true, | |
| 67 | + 'type' => 'object', | |
| 68 | + 'description' => __( 'Entry id', 'weforms' ), | |
| 69 | + 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 70 | + ], | |
| 71 | + 'force' => [ | |
| 72 | + 'type' => 'boolean', | |
| 73 | + 'default' => false, | |
| 74 | + 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ), | |
| 75 | + ], | |
| 76 | + ], | |
| 77 | + ], | |
| 78 | + ] | |
| 79 | + ); | |
| 80 | + | |
| 81 | + register_rest_route( | |
| 82 | + $this->namespace, | |
| 83 | + '/' . $this->rest_base . '/(?P<entry_id>[\d]+)/restore', | |
| 84 | + [ | |
| 85 | + [ | |
| 86 | + 'methods' => WP_REST_Server::READABLE, | |
| 87 | + 'callback' => [ $this, 'restore_items' ], | |
| 88 | + 'permission_callback' => [ $this, 'update_item_permissions_check' ], | |
| 89 | + 'args' => [ | |
| 90 | + 'entry_id' => [ | |
| 91 | + 'required' => true, | |
| 92 | + 'type' => 'integer', | |
| 93 | + 'sanitize_callback' => 'absint', | |
| 94 | + 'description' => __( 'Entry id', 'weforms' ), | |
| 95 | + 'validate_callback' => [ $this, 'is_restore_exists' ], | |
| 96 | + ], | |
| 97 | + ], | |
| 98 | + ], | |
| 99 | + ] | |
| 100 | + ); | |
| 101 | + | |
| 102 | + register_rest_route( | |
| 103 | + $this->namespace, | |
| 104 | + '/' . $this->rest_base . '/restore', | |
| 105 | + [ | |
| 106 | + [ | |
| 107 | + 'methods' => WP_REST_Server::READABLE, | |
| 108 | + 'callback' => [ $this, 'bulk_restore_items' ], | |
| 109 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], | |
| 110 | + 'args' => [ | |
| 111 | + 'entry_id' => [ | |
| 112 | + 'required' => true, | |
| 113 | + 'type' => 'object', | |
| 114 | + 'description' => __( 'Entry id', 'weforms' ), | |
| 115 | + 'validate_callback' => [ $this, 'is_restore_exists' ], | |
| 116 | + ], | |
| 117 | + ], | |
| 118 | + ], | |
| 119 | + ] | |
| 120 | + ); | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Restore Items From Trash | |
| 125 | + * | |
| 126 | + * @since 1.4.2 | |
| 127 | + * | |
| 128 | + * @param WP_REST_Request $request | |
| 129 | + * | |
| 130 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 131 | + **/ | |
| 132 | + public function bulk_restore_items( $request ) { | |
| 133 | + $entry_ids = isset( $request['entry_id'] ) ? array_map( 'absint', $request['entry_id'] ) : []; | |
| 134 | + | |
| 135 | + if ( !$entry_ids ) { | |
| 136 | + return new WP_Error( 'rest_weforms_invalid_entry', __( 'No entry ids provided!', 'weforms' ), [ 'status' => 404 ] ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + foreach ( $entry_ids as $entry_id ) { | |
| 140 | + $status = weforms_change_entry_status( $entry_id, 'publish' ); | |
| 141 | + | |
| 142 | + if ( $status ) { | |
| 143 | + $response['message'][$entry_id] = __( 'Entry Restore successfully ', 'weforms' ); | |
| 144 | + } else { | |
| 145 | + $response['message'][$entry_id] = __( 'Entry Restore failed ', 'weforms' ); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + $response = $this->prepare_response_for_collection( $response ); | |
| 150 | + $response = rest_ensure_response( $response ); | |
| 151 | + | |
| 152 | + return $response; | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Restore Item From Trash | |
| 157 | + * | |
| 158 | + * @since 1.4.2 | |
| 159 | + * | |
| 160 | + * @param WP_REST_Request $request | |
| 161 | + * | |
| 162 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 163 | + **/ | |
| 164 | + public function restore_items( $request ) { | |
| 165 | + $entry_id = isset( $request['entry_id'] ) ? intval( $request['entry_id'] ) : 0; | |
| 166 | + | |
| 167 | + weforms_change_entry_status( $entry_id, 'publish' ); | |
| 168 | + | |
| 169 | + $response['id'] = $entry_id; | |
| 170 | + $response['message'] = __( 'Entry Restore Successfully successfully ', 'weforms' ); | |
| 171 | + $response['data']['status'] = 200; | |
| 172 | + | |
| 173 | + $response = $this->prepare_response_for_collection( $response ); | |
| 174 | + $response = rest_ensure_response( $response ); | |
| 175 | + | |
| 176 | + return $response; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * Bulk Delele Item | |
| 181 | + * | |
| 182 | + * @since 1.4.2 | |
| 183 | + * | |
| 184 | + * @param WP_REST_Request $request | |
| 185 | + * | |
| 186 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 187 | + **/ | |
| 188 | + public function bulk_delete_items( $request ) { | |
| 189 | + $entry_ids = isset( $request['entry_id'] ) ? array_map( 'absint', $request['entry_id'] ) : []; | |
| 190 | + $force = (bool) $request['force']; | |
| 191 | + | |
| 192 | + if ( !$entry_ids ) { | |
| 193 | + return new WP_Error( 'rest_weforms_invalid_entry', __( 'No entry ids provided!', 'weforms' ) ); | |
| 194 | + } | |
| 195 | + | |
| 196 | + $response = []; | |
| 197 | + | |
| 198 | + foreach ( $entry_ids as $entry_id ) { | |
| 199 | + if ( $force ) { | |
| 200 | + $status = weforms_delete_entry( $entry_id ); | |
| 201 | + | |
| 202 | + if ( $status ) { | |
| 203 | + $response['message'][$entry_id] = __( 'Entry Deleted successfully ', 'weforms' ); | |
| 204 | + } else { | |
| 205 | + $response['message'][$entry_id] = __( 'Entry Not found ', 'weforms' ); | |
| 206 | + } | |
| 207 | + } else { | |
| 208 | + $status = weforms_change_entry_status( $entry_id, 'trash' ); | |
| 209 | + | |
| 210 | + if ( $status ) { | |
| 211 | + $response['message'][$entry_id] = __( 'Entry Move To Trash successfully ', 'weforms' ); | |
| 212 | + } else { | |
| 213 | + $response['message'][$entry_id] = __( 'Entry Not found ', 'weforms' ); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + } | |
| 217 | + | |
| 218 | + $response = $this->prepare_response_for_collection( $response ); | |
| 219 | + $response = rest_ensure_response( $response ); | |
| 220 | + | |
| 221 | + return $response; | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Delele Item | |
| 226 | + * | |
| 227 | + * @since 1.4.2 | |
| 228 | + * | |
| 229 | + * @param WP_REST_Request $request | |
| 230 | + * | |
| 231 | + * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure | |
| 232 | + **/ | |
| 233 | + public function delete_items( $request ) { | |
| 234 | + $entry_id = $request['entry_id']; | |
| 235 | + $force = (bool) $request['force']; | |
| 236 | + | |
| 237 | + if ( $force ) { | |
| 238 | + weforms_delete_entry( $entry_id ); | |
| 239 | + | |
| 240 | + $entry_array['id'] = $entry_id; | |
| 241 | + $entry_array['message'] = __( 'Entry Deleted successfully', 'weforms' ); | |
| 242 | + $entry_array['data']['status'] = 200; | |
| 243 | + } else { | |
| 244 | + $status = weforms_change_entry_status( $entry_id, 'trash' ); | |
| 245 | + | |
| 246 | + $entry_array['id'] = $entry_id; | |
| 247 | + $entry_array['message'] = __( 'Entry Move To Trash successfully', 'weforms' ); | |
| 248 | + $entry_array['data']['status'] = 200; | |
| 249 | + } | |
| 250 | + | |
| 251 | + $response = $this->prepare_response_for_collection( $entry_array, $request ); | |
| 252 | + $response = rest_ensure_response( $response ); | |
| 253 | + | |
| 254 | + return $response; | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Check is entry in trash exist or not | |
| 259 | + * | |
| 260 | + * @since 1.4.2 | |
| 261 | + * | |
| 262 | + * @param string $param | |
| 263 | + * @param WP_REST_Request $request | |
| 264 | + * @param string $key | |
| 265 | + * | |
| 266 | + * @return bool | |
| 267 | + */ | |
| 268 | + public function is_restore_exists( $param, $request, $key ) { | |
| 269 | + global $wpdb; | |
| 270 | + | |
| 271 | + // if( is_array( $param ) ) { | |
| 272 | + if ( is_array( $request['entry_id'] ) ) { | |
| 273 | + $entry_id = implode( ',', $param ); | |
| 274 | + $querystr = " | |
| 275 | + SELECT $wpdb->weforms_entries.id | |
| 276 | + FROM $wpdb->weforms_entries | |
| 277 | + WHERE $wpdb->weforms_entries.ID IN ( $entry_id ) | |
| 278 | + AND $wpdb->weforms_entries.status = \"trash\" | |
| 279 | + "; | |
| 280 | + } else { | |
| 281 | + // $entry_id = (int) $param; | |
| 282 | + $entry_id = (int) $request['entry_id']; | |
| 283 | + $querystr = " | |
| 284 | + SELECT $wpdb->weforms_entries.id | |
| 285 | + FROM $wpdb->weforms_entries | |
| 286 | + WHERE $wpdb->weforms_entries.ID = $entry_id | |
| 287 | + AND $wpdb->weforms_entries.status = \"trash\" | |
| 288 | + "; | |
| 289 | + } | |
| 290 | + | |
| 291 | + $result = $wpdb->get_results( $querystr ); | |
| 292 | + | |
| 293 | + if ( empty( $result ) ) { | |
| 294 | + return false; | |
| 295 | + } else { | |
| 296 | + return true; | |
| 297 | + } | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Get the Form entries schema, conforming to JSON Schema | |
| 302 | + * | |
| 303 | + * @since 1.4.2 | |
| 304 | + * | |
| 305 | + * @return array | |
| 306 | + */ | |
| 307 | + public function get_item_schema() { | |
| 308 | + $schema = [ | |
| 309 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 310 | + 'title' => 'entries', | |
| 311 | + 'type' => 'object', | |
| 312 | + 'properties' => [ | |
| 313 | + 'entry_id' => [ | |
| 314 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 315 | + 'type' => 'integer', | |
| 316 | + 'sanitize_callback' => 'absint', | |
| 317 | + 'validate_callback' => [ $this, 'is_entry_exists' ], | |
| 318 | + 'context' => [ 'embed', 'view', 'edit' ], | |
| 319 | + 'required' => true, | |
| 320 | + 'readonly' => true, | |
| 321 | + ], | |
| 322 | + ], | |
| 323 | + ]; | |
| 324 | + | |
| 325 | + return $schema; | |
| 326 | + } | |
| 327 | +} | |