LLMS_REST_Enrollments_Controller::update_item( WP_REST_Request $request )

Update item.


Parameters Parameters

$request

(WP_REST_Request) (Required) Request object.


Top ↑

Return Return

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


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/server/class-llms-rest-enrollments-controller.php

	 *
	 * @since 1.0.0-beta.1
	 * @since 1.0.0-beta.4 Return a bad request error when supplying an invalid date_created param.
	 * @since 1.0.0-beta.10 Handle `trigger` param.
	 * @since 1.0.0-beta.26 By default don't load the current user if a falsy student ID is supplied.
	 * @since 1.0.0-beta.27 Handle custom rest fields registered via `register_rest_field()`.
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or WP_Error on failure.
	 */
	public function update_item( $request ) {

		$student_id = (int) $request['id'];
		$post_id    = (int) $request['post_id'];

		// Check both students and product exist.
		$student = new LLMS_Student( $student_id, false );

		if ( ! $student->exists() ) {
			return llms_rest_not_found_error();
		}

		// Can only be enrolled in the following post types.
		$product_type = get_post_type( $post_id );
		if ( ! $product_type ) {
			return llms_rest_not_found_error();
		}
		if ( ! in_array( $product_type, array( 'course', 'llms_membership' ), true ) ) {
			return llms_rest_bad_request_error();
		}

		if ( 'any' !== $request['trigger'] && $request['trigger'] !== $student->get_enrollment_trigger( $post_id ) ) {
			return llms_rest_not_found_error();
		}

		$schema = $this->get_item_schema();

		if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {

			$updated_status = $this->handle_status_update( $student, $post_id, $request['status'], $request['trigger'] );

			// Something went wrong internally.
			if ( ! $updated_status ) {
				return llms_rest_server_error( __( 'The enrollment status could not be updated', 'lifterlms' ) );
			}
		}

		if ( ! empty( $schema['properties']['date_created'] ) && isset( $request['date_created'] ) ) {

			$updated_date_created = $this->handle_creation_date_update( $student_id, $post_id, $request['date_created'] );

			if ( is_wp_error( $updated_date_created ) ) {
				return $updated_date_created;
			}

			// Something went wrong internally.
			if ( ! $updated_date_created ) {
				return llms_rest_server_error( __( 'The enrollment creation date could not be updated', 'lifterlms' ) );
			}
		}


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.4 Return a bad request error when supplying an invalid date_created param.
1.0.0-beta.10 Handle trigger param.
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.