Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Admin_Builder::update_lessons( array $lessons, obj $section )

Update lesson from heartbeat data


Description Description


Parameters Parameters

$lessons

(array) (Required) lesson data from heartbeat

$section

(obj) (Required) instance of the parent LLMS_Section


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/admin/class.llms.admin.builder.php

	private static function update_lessons( $lessons, $section ) {

		$ret = array();

		foreach ( $lessons as $lesson_data ) {

			if ( ! isset( $lesson_data['id'] ) ) {
				continue;
			}

			$res = array_merge( $lesson_data, array(
				'orig_id' => $lesson_data['id'],
			) );

			// create a new lesson
			if ( self::is_temp_id( $lesson_data['id'] ) ) {

				$lesson = new LLMS_Lesson( 'new', array(
					'post_title' => isset( $lesson_data['title'] ) ? $lesson_data['title'] : __( 'New Lesson', 'lifterlms' ),
				) );

				// if the parent section was just created the lesson will have a temp id
				// replace it with the newly created section's real ID
				if ( ! isset( $lesson_data['parent_section'] ) || self::is_temp_id( $lesson_data['parent_section'] ) ) {
					$lesson_data['parent_section'] = $section->get( 'id' );
				}

				$created = true;

			} else {

				$lesson = llms_get_post( $lesson_data['id'] );
				$created = false;

			}

			if ( empty( $lesson ) || ! is_a( $lesson, 'LLMS_Lesson' ) ) {

				$res['error'] = sprintf( esc_html__( 'Unable to update lesson "%s". Invalid lesson ID.', 'lifterlms' ), $lesson_data['id'] );

			} else {

				// return the real ID (important when creating a new lesson)
				$res['id'] = $lesson->get( 'id' );

				$properties = array_merge( array_keys( $lesson->get_properties() ), array(
					'content',
					'title',
				) );

				$skip_props = apply_filters( 'llms_builder_update_lesson_skip_props', array( 'quiz' ) );

				// update all updatable properties
				foreach ( $properties as $prop ) {
					if ( isset( $lesson_data[ $prop ] ) && ! in_array( $prop, $skip_props ) ) {
						$lesson->set( $prop, $lesson_data[ $prop ] );
					}
				}

				// update all custom fields
				self::update_custom_schemas( 'lesson', $lesson, $lesson_data );

				// during clone's we want to ensure custom field data comes with the lesson
				if ( $created && isset( $lesson_data['custom'] ) ) {
					foreach ( $lesson_data['custom'] as $custom_key => $custom_vals ) {
						foreach ( $custom_vals as $val ) {
							add_post_meta( $lesson->get( 'id' ), $custom_key, maybe_unserialize( $val ) );
						}
					}
				}

				// ensure slug gets updated when changing title from default "New Lesson"
				if ( isset( $lesson_data['title'] ) && ! $lesson->has_modified_slug() ) {
					$lesson->set( 'name', sanitize_title( $lesson_data['title'] ) );
				}

				if ( ! empty( $lesson_data['quiz'] ) && is_array( $lesson_data['quiz'] ) ) {
					$res['quiz'] = self::update_quiz( $lesson_data['quiz'], $lesson );
				}
			}// End if().

			// allow 3rd parties to update custom data
			$res = apply_filters( 'llms_builder_update_lesson', $res, $lesson_data, $lesson, $created );

			array_push( $ret, $res );

		}// End foreach().

		return $ret;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.16.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: