| @@ 331-380 (lines=50) @@ | ||
| 328 | * @param WP_REST_Request $request Full details about the request. |
|
| 329 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 330 | */ |
|
| 331 | public function create_item( $request ) { |
|
| 332 | ||
| 333 | if ( ! empty( $request['id'] ) ) { |
|
| 334 | return new WP_Error( 'rest_item_exists', __( 'Cannot create existing invoice item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 335 | } |
|
| 336 | ||
| 337 | $request->set_param( 'context', 'edit' ); |
|
| 338 | ||
| 339 | // Prepare the updated data. |
|
| 340 | $item_data = $this->prepare_item_for_database( $request ); |
|
| 341 | ||
| 342 | if ( is_wp_error( $item_data ) ) { |
|
| 343 | return $item_data; |
|
| 344 | } |
|
| 345 | ||
| 346 | // Try creating the item. |
|
| 347 | $item = wpinv_create_item( $item_data, true ); |
|
| 348 | ||
| 349 | if ( is_wp_error( $item ) ) { |
|
| 350 | return $item; |
|
| 351 | } |
|
| 352 | ||
| 353 | // Prepare the response |
|
| 354 | $response = $this->prepare_item_for_response( $item, $request ); |
|
| 355 | ||
| 356 | /** |
|
| 357 | * Fires after a single invoice item is created or updated via the REST API. |
|
| 358 | * |
|
| 359 | * @since 1.0.13 |
|
| 360 | * |
|
| 361 | * @param WPinv_Item $item Inserted or updated item object. |
|
| 362 | * @param WP_REST_Request $request Request object. |
|
| 363 | * @param bool $creating True when creating a post, false when updating. |
|
| 364 | */ |
|
| 365 | do_action( "wpinv_rest_insert_item", $item, $request, true ); |
|
| 366 | ||
| 367 | /** |
|
| 368 | * Filters the responses for creating single item requests. |
|
| 369 | * |
|
| 370 | * |
|
| 371 | * @since 1.0.13 |
|
| 372 | * |
|
| 373 | * |
|
| 374 | * @param array $item_data Invoice properties. |
|
| 375 | * @param WP_REST_Request $request The request used. |
|
| 376 | */ |
|
| 377 | $response = apply_filters( "wpinv_rest_create_item_response", $response, $request ); |
|
| 378 | ||
| 379 | return rest_ensure_response( $response ); |
|
| 380 | } |
|
| 381 | ||
| 382 | /** |
|
| 383 | * Checks if a given request has access to update an item. |
|
| @@ 295-344 (lines=50) @@ | ||
| 292 | * @param WP_REST_Request $request Full details about the request. |
|
| 293 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
| 294 | */ |
|
| 295 | public function create_item( $request ) { |
|
| 296 | ||
| 297 | if ( ! empty( $request['id'] ) ) { |
|
| 298 | return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
| 299 | } |
|
| 300 | ||
| 301 | $request->set_param( 'context', 'edit' ); |
|
| 302 | ||
| 303 | // Prepare the updated data. |
|
| 304 | $invoice_data = $this->prepare_item_for_database( $request ); |
|
| 305 | ||
| 306 | if ( is_wp_error( $invoice_data ) ) { |
|
| 307 | return $invoice_data; |
|
| 308 | } |
|
| 309 | ||
| 310 | // Try creating the invoice |
|
| 311 | $invoice = wpinv_insert_invoice( $invoice_data, true ); |
|
| 312 | ||
| 313 | if ( is_wp_error( $invoice ) ) { |
|
| 314 | return $invoice; |
|
| 315 | } |
|
| 316 | ||
| 317 | // Prepare the response |
|
| 318 | $response = $this->prepare_item_for_response( $invoice, $request ); |
|
| 319 | ||
| 320 | /** |
|
| 321 | * Fires after a single invoice is created or updated via the REST API. |
|
| 322 | * |
|
| 323 | * @since 1.0.13 |
|
| 324 | * |
|
| 325 | * @param WPinv_Invoice $invoice Inserted or updated invoice object. |
|
| 326 | * @param WP_REST_Request $request Request object. |
|
| 327 | * @param bool $creating True when creating a post, false when updating. |
|
| 328 | */ |
|
| 329 | do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
| 330 | ||
| 331 | /** |
|
| 332 | * Filters the responses for creating single invoice requests. |
|
| 333 | * |
|
| 334 | * |
|
| 335 | * @since 1.0.13 |
|
| 336 | * |
|
| 337 | * |
|
| 338 | * @param array $invoice_data Invoice properties. |
|
| 339 | * @param WP_REST_Request $request The request used. |
|
| 340 | */ |
|
| 341 | $response = apply_filters( "wpinv_rest_create_invoice_response", $response, $request ); |
|
| 342 | ||
| 343 | return rest_ensure_response( $response ); |
|
| 344 | } |
|
| 345 | ||
| 346 | /** |
|
| 347 | * Checks if a given request has access to update an invoice. |
|