LLMS_REST_Enrollments_Controller::create_item( WP_REST_Request $request )

Creates a single enrollment.


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/server/class-llms-rest-enrollments-controller.php

	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function create_item( $request ) {

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

		// The default trigger for the `LLMS_Student::enroll()` method is 'unspecified'.
		$trigger = $request['trigger'] && 'any' !== $request['trigger'] ? $request['trigger'] : 'unspecified';

		// Check both students and product exist.
		$student = new LLMS_Student( $user_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();
		}

		// Enroll.
		$enroll = $student->enroll( $post_id, $trigger );

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

		$request->set_param( 'context', 'edit' );
		$enrollment = $this->get_object( $user_id, $post_id );

		// Fields registered via `register_rest_field()`.
		$fields_update = $this->update_additional_fields_for_object( $enrollment, $request );
		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

		$response = $this->prepare_item_for_response( $enrollment, $request );

		$response->set_status( 201 );



Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.10 Handle the 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.