Learndash_Helpers::topic_from_lesson_func()

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


Source Source

File: src/integrations/learndash/helpers/learndash-helpers.php

	public function topic_from_lesson_func() {



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

		$fields      = array();
		$include_any = $this->load_any_options;
		if ( $include_any ) {
			$fields[] = [
				'value' => - 1,
				'text'  => esc_attr__( 'Any topic', 'uncanny-automator' ),
			];
		}

		if ( ! isset( $_POST ) ) {
			echo wp_json_encode( $fields );
			die();
		}

		$trigger_id = absint( $_POST['item_id'] );
		if ( ! $trigger_id ) {
			echo wp_json_encode( $fields );
			die();
		}

		if ( ! isset( $_POST['values'] ) ) {
			echo wp_json_encode( $fields );
			die();
		}

		$post_value = sanitize_text_field( $_POST['value'] );

		if ( 'automator_custom_value' === $post_value ) {
			$course_id = isset( $_POST['values']['LDCOURSE_custom'] ) ? absint( $_POST['values']['LDCOURSE_custom'] ) : 0;
		} else {
			$course_id = absint( $_POST['values']['LDCOURSE'] );
		}

		if ( '-1' === sanitize_text_field( $_POST['value'] ) ) {
			$lesson = null;
			echo wp_json_encode( $fields );
			die();
		} else {
			if ( 'automator_custom_value' === $post_value ) {
				$lesson = isset( $_POST['values']['LDLESSON_custom'] ) ? absint( $_POST['values']['LDLESSON_custom'] ) : 0;
			} else {
				$lesson = absint( $_POST['value'] );
			}
		}

		$topics = learndash_get_topic_list( $lesson, absint( $course_id ) );

		foreach ( $topics as $topic ) {
			$fields[] = array(
				'value' => $topic->ID,
				'text'  => $topic->post_title,
			);
		}

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