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, LLMS_Section $section )
Update lesson from heartbeat data
Parameters Parameters
- $lessons
-
(array) (Required) Lesson data from heartbeat.
- $section
-
(LLMS_Section) (Required) instance of the parent LLMS_Section.
Return Return
(array)
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' ), ) ); $created = true; } else { $lesson = llms_get_post( $lesson_data['id'] ); $created = false; } if ( empty( $lesson ) || ! is_a( $lesson, 'LLMS_Lesson' ) ) { // Translators: %s = Lesson post id. $res['error'] = sprintf( esc_html__( 'Unable to update lesson "%s". Invalid lesson ID.', 'lifterlms' ), $lesson_data['id'] ); } else { /** * 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' ); } // 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, true ) ) { $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 ); } } // Allow 3rd parties to update custom data. $res = apply_filters( 'llms_builder_update_lesson', $res, $lesson_data, $lesson, $created ); array_push( $ret, $res ); } return $ret; }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
5.1.3 | Made sure a lesson moved in a just created section is correctly assigned to it. |
3.16.0 | Introduced. |