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.


Top ↑

Return Return

(array)


Top ↑

Source Source

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

994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
                        update_post_meta( $post_data['id'], $attr, $val );
 
                    }
                }
            }
        }
    }
 
}
 
/**
 * Update lesson from heartbeat data.
 *
 * @since 3.16.0
 * @since 5.1.3 Made sure a lesson moved in a just created section is correctly assigned to it.
 * @since 7.3.0 Skip revision creation when creating a brand new lesson.
 *
 * @param array        $lessons Lesson data from heartbeat.
 * @param LLMS_Section $section instance of the parent LLMS_Section.
 * @return array
 */
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 {
 
            // Don't create useless revision on "creating".
            add_filter( 'wp_revisions_to_keep', '__return_zero', 999 );
 
            /**
             * 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 ) );


Top ↑

Changelog 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.

Top ↑

User Contributed Notes User Contributed Notes

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