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().


Top ↑

Parameters Parameters

$request

(WP_REST_Request) (Required) Full details about the request.


Top ↑

Return Return

(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/abstracts/class-llms-rest-posts-controller.php

	public function update_item( $request ) {

		$object = $this->get_object( (int) $request['id'] );
		if ( is_wp_error( $object ) ) {
			return $object;
		}

		$prepared_item = $this->prepare_item_for_database( $request );
		if ( is_wp_error( $prepared_item ) ) {
			return $prepared_item;
		}

		$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;
		}

		$schema = $this->get_item_schema();

		/**
		 * 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 );

		$object_id = $object->get( 'id' );

		$additional_fields = $this->update_additional_object_fields( $object, $request, $schema, $prepared_item, false );
		if ( is_wp_error( $additional_fields ) ) {
			return $additional_fields;
		}

		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;
		}

		/**
		 * TODO: understand how to treat possible conflicting properties => instructors are registered as additional rest field by llms_blocks
		 */
		// $fields_update = $this->update_additional_fields_for_object( $object, $request );
		// if ( is_wp_error( $fields_update ) ) {
		// return $fields_update;
		// }
		$request->set_param( 'context', 'edit' );

		/**
		 * Fires after a single llms post is completely 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_after_insert_{$this->post_type}", $object, $request, $schema, false );

		return $this->prepare_item_for_response( $object, $request );

	}


Top ↑

Changelog 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.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.