LLMS_REST_Posts_Controller::update_item( WP_REST_Request $request )
Updates a single llms post.
Description Description
Extending classes can add additional object fields by overriding the method update_additional_object_fields()
.
Parameters Parameters
- $request
-
(WP_REST_Request) (Required) Full details about the request.
Return Return
(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.
Source Source
File: libraries/lifterlms-rest/includes/abstracts/class-llms-rest-posts-controller.php
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | /** * Updates a single llms post. * * Extending classes can add additional object fields by overriding the method `update_additional_object_fields()`. * * @since 1.0.0-beta.1 * @since 1.0.0-beta.7 Don't execute `$object->set_bulk()` when there's no data to update: * this fixes an issue when updating only properties which are not handled in `prepare_item_for_database()`. * Added `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks: * fired after inserting/updating an llms post into the database. * @since 1.0.0-beta.11 Fixed `"llms_rest_insert_{$this->post_type}"` and `"llms_rest_insert_{$this->post_type}"` action hooks fourth param: * must be false when updating. * @since 1.0.0-beta.25 Allow updating meta with the same value as the stored one. * @since 1.0.0-beta.27 Handle custom meta registered via `register_meta()` and custom rest fields registered via `register_rest_field()`. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function update_item( $request ) { $object = $this ->get_object( (int) $request [ 'id' ] ); if ( is_wp_error( $object ) ) { return $object ; } $schema = $this ->get_item_schema(); $prepared_item = $this ->prepare_item_for_database( $request ); if ( is_wp_error( $prepared_item ) ) { return $prepared_item ; } $prepared_item = array_diff_key ( $prepared_item , $this ->get_additional_fields() ); $update_result = empty ( array_diff_key ( $prepared_item , array_flip ( array ( 'id' ) ) ) ) ? false : $object ->set_bulk( $prepared_item , true, true ); if ( is_wp_error( $update_result ) ) { if ( 'db_update_error' === $update_result ->get_error_code() ) { $update_result ->add_data( array ( 'status' => 500 ) ); } else { $update_result ->add_data( array ( 'status' => 400 ) ); } return $update_result ; } /** * Fires after a single llms post is created or updated via the REST API. * * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug. * * @since 1.0.0-beta.7 * * @param LLMS_Post $object Inserted or updated llms object. * @param WP_REST_Request $request Request object. * @param array $schema The item schema. * @param bool $creating True when creating a post, false when updating. */ do_action( "llms_rest_insert_{$this->post_type}" , $object , $request , $schema , false ); $additional_fields = $this ->update_additional_object_fields( $object , $request , $schema , $prepared_item , false ); if ( is_wp_error( $additional_fields ) ) { return $additional_fields ; } $object_id = $object ->get( 'id' ); if ( ! empty ( $schema [ 'properties' ][ 'featured_media' ] ) && isset( $request [ 'featured_media' ] ) ) { $this ->handle_featured_media( $request [ 'featured_media' ], $object_id ); } $terms_update = $this ->handle_terms( $object_id , $request ); if ( is_wp_error( $terms_update ) ) { return $terms_update ; } $meta_update = $this ->update_meta( $object , $request , $schema ); if ( is_wp_error( $meta_update ) ) { return $meta_update ; } // Fields registered via `register_rest_field()`. $fields_update = $this ->update_additional_fields_for_object( $object , $request ); |
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
1.0.0-beta.7 | Don't execute $object->set_bulk() when there's no data to update: this fixes an issue when updating only properties which are not handled in prepare_item_for_database() . Added "llms_rest_insert_{$this->post_type}" and "llms_rest_insert_{$this->post_type}" action hooks: fired after inserting/uodateing an llms post into the database. |
1.0.0-beta.25 | Allow updating meta with the same value as the stored one. |
1.0.0-beta.11 | Fixed "llms_rest_insert_{$this->post_type}" and "llms_rest_insert_{$this->post_type}" action hooks fourth param: must be false when updating. |
1.0.0-beta.1 | Introduced. |