LLMS_REST_Lessons_Controller::prepare_item_for_database( WP_REST_Request $request )

Prepares a single lesson for create or update.


Parameters Parameters

$request

(WP_REST_Request) (Required) Request object.


Top ↑

Return Return

(array|WP_Error) Array of lesson args or WP_Error.


Top ↑

Source Source

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

	public function get_parent_id() {
		return isset( $this->parent_id ) ? $this->parent_id : null;
	}

	/**
	 * Prepares a single lesson for create or update.
	 *
	 * @since 1.0.0-beta.7
	 * @since 1.0.0-beta.15 Fixed setting/updating parent section/course.
	 * @since 1.0.0-beta.23 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
	 * @since 1.0.0-beta.25 Remove now useless check on the existing parent course id when setting it automatically on parent section's id update.
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return array|WP_Error Array of lesson args or WP_Error.
	 */
	protected function prepare_item_for_database( $request ) {

		$prepared_item = parent::prepare_item_for_database( $request );
		$schema        = $this->get_item_schema();

		// Lesson's audio embed URL.
		if ( ! empty( $schema['properties']['audio_embed'] ) && isset( $request['audio_embed'] ) ) {
			$prepared_item['audio_embed'] = $request['audio_embed'];
		}

		// Lesson's video embed URL.
		if ( ! empty( $schema['properties']['video_embed'] ) && isset( $request['video_embed'] ) ) {
			$prepared_item['video_embed'] = $request['video_embed'];
		}

		// Parent (section) id.
		if ( ! empty( $schema['properties']['parent_id'] ) && isset( $request['parent_id'] ) ) {

			// Retrieve the parent section.
			$parent_section = llms_get_post( $request['parent_id'] );

			$prepared_item['parent_section'] = $parent_section && is_a( $parent_section, 'LLMS_Section' ) ? $request['parent_id'] : 0;

			// Retrieve the parent course id.
			if ( $prepared_item['parent_section'] ) {
				$parent_course = $parent_section->get_course();
			}

			$prepared_item['parent_course'] = ! empty( $parent_course ) && is_a( $parent_course, 'LLMS_Course' ) ? $parent_course->get( 'id' ) : 0;

		}

		// Course id.
		if ( ! empty( $schema['properties']['course_id'] ) && isset( $request['course_id'] ) ) {

			$parent_course = llms_get_post( $request['course_id'] );

			if ( ! $parent_course || ! is_a( $parent_course, 'LLMS_Course' ) ) {
				return llms_rest_bad_request_error( __( 'Invalid course_id param. It must be a valid Course ID.', 'lifterlms' ) );
			}

			$prepared_item['parent_course'] = $request['course_id'];
		}

		// Order.
		if ( ! empty( $schema['properties']['order'] ) && isset( $request['order'] ) ) {

			// order must be > 0. It's sanitized as absint so it cannot come as negative value.
			if ( 0 === $request['order'] ) {
				return llms_rest_bad_request_error( __( 'Invalid order param. It must be greater than 0.', 'lifterlms' ) );
			}

			$prepared_item['order'] = $request['order'];
		}

		// Public (free lesson).
		if ( ! empty( $schema['properties']['public'] ) && isset( $request['public'] ) ) {
			$prepared_item['free_lesson'] = empty( $request['public'] ) ? 'no' : 'yes';
		}

		// Points.
		if ( ! empty( $schema['properties']['points'] ) && isset( $request['points'] ) ) {
			$prepared_item['points'] = $request['points'];
		}

		// Drip days.
		if ( ! empty( $schema['properties']['drip_days'] ) && isset( $request['drip_days'] ) ) {

			// drip_days must be > 0. It's sanitized as absint so it cannot come as negative value.
			if ( 0 === $request['drip_days'] ) {
				return llms_rest_bad_request_error( __( 'Invalid drip_days param. It must be greater than 0.', 'lifterlms' ) );
			}

			$prepared_item['days_before_available'] = $request['drip_days'];
		}

		// Drip date.
		if ( ! empty( $schema['properties']['drip_date'] ) && isset( $request['drip_date'] ) ) {
			$drip_date = rest_parse_date( $request['drip_date'] );

			// Drip date is nullable.
			if ( empty( $drip_date ) ) {
				$prepared_item['date_available'] = '';
				$prepared_item['time_available'] = '';
			} else {
				$prepared_item['date_available'] = date_i18n( 'Y-m-d', $drip_date );
				$prepared_item['time_available'] = date_i18n( 'H:i:s', $drip_date );
			}
		}

		// Drip method.
		if ( ! empty( $schema['properties']['drip_method'] ) && isset( $request['drip_method'] ) ) {
			$prepared_item['drip_method'] = 'none' === $request['drip_method'] ? '' : $request['drip_method'];
		}

		// Quiz enabled.
		if ( ! empty( $schema['properties']['quiz']['properties']['enabled'] ) && isset( $request['quiz']['enabled'] ) ) {
			$prepared_item['quiz_enabled'] = empty( $request['quiz']['enabled'] ) ? 'no' : 'yes';
		}

		// Quiz id.
		if ( ! empty( $schema['properties']['quiz']['properties']['id'] ) && isset( $request['quiz']['id'] ) ) {

			// check if quiz exists.
			$quiz = llms_get_post( $request['quiz']['id'] );

			if ( is_a( $quiz, 'LLMS_Quiz' ) ) {
				$prepared_item['quiz'] = $request['quiz']['id'];
			}
		}

		// Quiz progression.
		if ( ! empty( $schema['properties']['quiz']['properties']['progression'] ) && isset( $request['quiz']['progression'] ) ) {


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.7
1.0.0-beta.25 Remove now useless check on the existing parent course id when setting it automatically on parent section's id update.
1.0.0-beta.23 Replaced the call to the deprecated LLMS_Lesson::get_parent_course() method with LLMS_Lesson::get( 'parent_course' ).
1.0.0-beta.15 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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