| @@ 420-474 (lines=55) @@ | ||
| 417 | * @param WP_REST_Request $request Full details about the request. |
|
| 418 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 419 | */ |
|
| 420 | public function update_item( $request ) { |
|
| 421 | ||
| 422 | // Ensure the item exists. |
|
| 423 | $valid_check = $this->get_post( $request['id'] ); |
|
| 424 | ||
| 425 | // Abort early if it does not exist |
|
| 426 | if ( is_wp_error( $valid_check ) ) { |
|
| 427 | return $valid_check; |
|
| 428 | } |
|
| 429 | ||
| 430 | $request->set_param( 'context', 'edit' ); |
|
| 431 | ||
| 432 | // Prepare the updated data. |
|
| 433 | $data_to_update = $this->prepare_item_for_database( $request ); |
|
| 434 | ||
| 435 | if ( is_wp_error( $data_to_update ) ) { |
|
| 436 | return $data_to_update; |
|
| 437 | } |
|
| 438 | ||
| 439 | // Abort if no item data is provided |
|
| 440 | if( empty( $data_to_update ) ) { |
|
| 441 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
|
| 442 | } |
|
| 443 | ||
| 444 | // Include the item ID |
|
| 445 | $data_to_update['ID'] = $request['id']; |
|
| 446 | ||
| 447 | // Update the item |
|
| 448 | $updated_item = wpinv_update_item( $data_to_update, true ); |
|
| 449 | ||
| 450 | // Incase the update operation failed... |
|
| 451 | if ( is_wp_error( $updated_item ) ) { |
|
| 452 | return $updated_item; |
|
| 453 | } |
|
| 454 | ||
| 455 | // Prepare the response |
|
| 456 | $response = $this->prepare_item_for_response( $updated_item, $request ); |
|
| 457 | ||
| 458 | /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
| 459 | do_action( "wpinv_rest_insert_item", $updated_item, $request, false ); |
|
| 460 | ||
| 461 | /** |
|
| 462 | * Filters the responses for updating single item requests. |
|
| 463 | * |
|
| 464 | * |
|
| 465 | * @since 1.0.13 |
|
| 466 | * |
|
| 467 | * |
|
| 468 | * @param array $item_data Item properties. |
|
| 469 | * @param WP_REST_Request $request The request used. |
|
| 470 | */ |
|
| 471 | $response = apply_filters( "wpinv_rest_update_item_response", $response, $data_to_update, $request ); |
|
| 472 | ||
| 473 | return rest_ensure_response( $response ); |
|
| 474 | } |
|
| 475 | ||
| 476 | /** |
|
| 477 | * Checks if a given request has access to delete an item. |
|
| @@ 385-439 (lines=55) @@ | ||
| 382 | * @param WP_REST_Request $request Full details about the request. |
|
| 383 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 384 | */ |
|
| 385 | public function update_item( $request ) { |
|
| 386 | ||
| 387 | // Ensure the invoice exists. |
|
| 388 | $valid_check = $this->get_post( $request['id'] ); |
|
| 389 | ||
| 390 | // Abort early if it does not exist |
|
| 391 | if ( is_wp_error( $valid_check ) ) { |
|
| 392 | return $valid_check; |
|
| 393 | } |
|
| 394 | ||
| 395 | $request->set_param( 'context', 'edit' ); |
|
| 396 | ||
| 397 | // Prepare the updated data. |
|
| 398 | $data_to_update = $this->prepare_item_for_database( $request ); |
|
| 399 | ||
| 400 | if ( is_wp_error( $data_to_update ) ) { |
|
| 401 | return $data_to_update; |
|
| 402 | } |
|
| 403 | ||
| 404 | // Abort if no invoice data is provided |
|
| 405 | if( empty( $data_to_update ) ) { |
|
| 406 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
|
| 407 | } |
|
| 408 | ||
| 409 | // Include the invoice ID |
|
| 410 | $data_to_update['ID'] = $request['id']; |
|
| 411 | ||
| 412 | // Update the invoice |
|
| 413 | $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
| 414 | ||
| 415 | // Incase the update operation failed... |
|
| 416 | if ( is_wp_error( $updated_invoice ) ) { |
|
| 417 | return $updated_invoice; |
|
| 418 | } |
|
| 419 | ||
| 420 | // Prepare the response |
|
| 421 | $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
| 422 | ||
| 423 | /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
|
| 424 | do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
| 425 | ||
| 426 | /** |
|
| 427 | * Filters the responses for updating single invoice requests. |
|
| 428 | * |
|
| 429 | * |
|
| 430 | * @since 1.0.13 |
|
| 431 | * |
|
| 432 | * |
|
| 433 | * @param array $invoice_data Invoice properties. |
|
| 434 | * @param WP_REST_Request $request The request used. |
|
| 435 | */ |
|
| 436 | $response = apply_filters( "wpinv_rest_update_invoice_response", $response, $request ); |
|
| 437 | ||
| 438 | return rest_ensure_response( $response ); |
|
| 439 | } |
|
| 440 | ||
| 441 | /** |
|
| 442 | * Checks if a given request has access to delete an invoice. |
|