LLMS_REST_Lessons_Controller::prepare_links( LLMS_Lesson $lesson, WP_REST_Request $request )

Prepare links for the request.


Parameters Parameters

$lesson

(LLMS_Lesson) (Required) LLMS Section.

$request

(WP_REST_Request) (Required) Request object.


Top ↑

Return Return

(array) Links for the given object.


Top ↑

Source Source

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

	 * @since 1.0.0-beta.14 Added `$request` parameter.
	 * @since 1.0.0-beta.23 Replaced the call to the deprecated `LLMS_Lesson::get_parent_course()` method with `LLMS_Lesson::get( 'parent_course' )`.
	 *
	 * @param LLMS_Lesson     $lesson  LLMS Section.
	 * @param WP_REST_Request $request Request object.
	 * @return array Links for the given object.
	 */
	protected function prepare_links( $lesson, $request ) {

		$links = parent::prepare_links( $lesson, $request );

		unset( $links['content'] );

		$lesson_id         = $lesson->get( 'id' );
		$parent_course_id  = $lesson->get( 'parent_course' );
		$parent_section_id = $lesson->get_parent_section();

		$lesson_links = array();

		// Parent course.
		if ( $parent_course_id ) {
			$lesson_links['course'] = array(
				'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'courses', $parent_course_id ) ),
			);
		}

		// Parent section.
		if ( $parent_section_id ) {
			$lesson_links['parent'] = array(
				'type' => 'section',
				'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'sections', $parent_section_id ) ),
			);
		}

		// Siblings.
		$lesson_links['siblings'] = array(
			'href' => add_query_arg(
				'parent',
				$parent_section_id,
				$links['collection']['href']
			),
		);

		// Next.
		$next_lesson = $lesson->get_next_lesson();
		if ( $next_lesson ) {
			$lesson_links['next'] = array(
				'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $next_lesson ) ),
			);
		}

		// Previous.
		$previous_lesson = $lesson->get_previous_lesson();
		if ( $previous_lesson ) {
			$lesson_links['previous'] = array(
				'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $previous_lesson ) ),
			);
		}

		// Prerequisite.
		$prerequisite = $lesson->get_prerequisite();

		if ( ! empty( $prerequisite ) ) {
			$lesson_links['prerequisite'] = array(
				'type' => $this->post_type,
				'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $prerequisite ) ),
			);
		}

		// Quiz.
		if ( $lesson->is_quiz_enabled() ) {
			$quiz                 = $lesson->get_quiz();
			$lesson_links['quiz'] = array(
				'href' => rest_url( sprintf( '/%s/%s/%d', 'llms/v1', 'quizzes', $quiz->get( 'id' ) ) ),
			);
		}

		$links = array_merge( $links, $lesson_links );

		/**
		 * Filters the lesson's links.
		 *
		 * @since 1.0.0-beta.7


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.7 Fixed siblings link that was using the parent course's id instead of the parent section's id. Fixed parent link href, replacing 'section' with 'sections'. Following links added: prerequisite, quiz. Added llms_rest_lesson_links filter hook.
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.14 Added $request parameter.
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.