Memberpress_Courses_Helpers::select_lesson_from_course_func()

Return all the specific fields of a form ID provided in ajax call


Parameters Parameters

$include_any

(Required)


Source Source

File: src/integrations/memberpress-courses/helpers/memberpress-courses-helpers.php

	public function select_lesson_from_course_func() {
		global $wpdb;

		// Nonce and post object validation
		Automator()->utilities->ajax_auth_check();

		$fields = array();
		if ( ! automator_filter_has_var( 'value', INPUT_POST ) ) {
			echo wp_json_encode( $fields );
			die();
		}

		$mpcs_post_value  = automator_filter_input( 'value', INPUT_POST );
		$mpcs_post_values = automator_filter_input_array( 'values', INPUT_POST );

		if ( 'automator_custom_value' === (string) $mpcs_post_value && '-1' !== absint( $mpcs_post_value ) ) {
			$mpcs_course_id = isset( $mpcs_post_values['MPCOURSE_custom'] ) ? absint( $mpcs_post_values['MPCOURSE_custom'] ) : 0;
		} else {
			$mpcs_course_id = absint( $mpcs_post_values['MPCOURSE'] );
		}

		if ( absint( '-1' ) !== absint( $mpcs_course_id ) ) {

			$course          = new models\Course( $mpcs_course_id );
			$course_sections = (array) $course->sections();

			foreach ( $course_sections as $section ) {

				$curriculum['sections'][ $section->uuid ] = array(
					'id'        => $section->uuid,
					'title'     => $section->title,
					'lessonIds' => array(),
				);

				$section_lessons = $section->lessons();
				foreach ( $section_lessons as $lesson ) {

					$fields[] = array(
						'value' => $lesson->ID,
						'text'  => ( $section->title ) ? $section->title . ' -> ' . $lesson->post_title : $lesson->post_title,
					);
				}
			}
		}

		echo wp_json_encode( $fields );
		die();
	}