LLMS_Lesson::get_sibling_section_query( string $direction )

Performs a query to retrieve sibling lessons from the lesson’s adjacent section


Description Description

This will retrieve either the first lesson from the course’s next section or the last lesson from the course’s previous section.


Top ↑

Parameters Parameters

$direction

(string) (Required) Direction of navigation. Accepts either "prev" or "next".


Top ↑

Return Return

(false|int) WP_Post ID of the sibling lesson or false if one doesn't exist.


Top ↑

Source Source

File: includes/models/model.llms.lesson.php

	protected function get_sibling_section_query( $direction ) {

		$sibling_lesson = false;
		$curr_section   = $this->get_section();

		// Ensure we're not working with an orphan.
		if ( $curr_section ) {

			$curr_position = $curr_section->get( 'order' );

			// First cannot have a previous.
			if ( 1 === $curr_position && 'prev' === $direction ) {
				return false;
			}

			if ( 'next' === $direction ) {
				$sibling_position = $curr_position + 1;
				$order            = 'ASC';
			} elseif ( 'prev' === $direction ) {
				$sibling_position = $curr_position - 1;
				$order            = 'DESC';
			}

			$args = array(
				'post_type'      => 'section',
				'posts_per_page' => 1,
				'nopaging'       => true,
				'meta_key'       => '_llms_order', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
				'orderby'        => 'meta_value_num',
				'order'          => $order,
				'meta_query'     => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					'relation' => 'AND',
					array(
						'key'     => '_llms_parent_course',
						'value'   => $this->get( 'parent_course' ),
						'compare' => '=',
					),
					array(
						'key'     => '_llms_order',
						'value'   => $sibling_position,
						'compare' => '=',
					),
				),
			);

			/**
			 * Filter the WP_Query arguments used to locate a sibling lesson from a sibling section for the specified lesson.
			 *
			 * @since 4.10.2
			 *
			 * @param array       $args      WP_Query arguments array.
			 * @param string      $direction Navigation direction. Either "prev" or "next".
			 * @param LLMS_Lesson $lesson    Current lesson object.
			 */
			$args = apply_filters( 'llms_lesson_get_sibling_section_query_args', $args, $direction, $this );

			$sections = get_posts( $args );

			if ( ! empty( $sections ) ) {
				$sibling_section = llms_get_post( $sections[0]->ID );
				$lessons         = $sibling_section ? $sibling_section->get_lessons( 'posts' ) : array( false );
				$sibling_lesson  = 'next' === $direction ? reset( $lessons ) : end( $lessons );
			}
		}

		return $sibling_lesson instanceof WP_Post ? $sibling_lesson->ID : $sibling_lesson;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.7.0 Replaced the call to the deprecated LLMS_Section::get_order() method with LLMS_Section::get( 'order' ).
4.11.0 Fix PHP Notice when trying to retrieve next lesson from an empty section.
4.10.2 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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