LD_TOPICDONE::define_trigger()

Define and register the trigger by pushing it into the Automator object


Source Source

File: src/integrations/learndash/triggers/ld-topicdone.php

	public function define_trigger() {



		$args = [
			'post_type'      => 'sfwd-courses',
			'posts_per_page' => 999,
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		];

		$course_options = Automator()->helpers->recipe->options->wp_query( $args, true, esc_attr__( 'Any course', 'uncanny-automator' ) );

		$args = [
			'post_type'      => 'sfwd-lessons',
			'posts_per_page' => 999,
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		];

		$lesson_options         = Automator()->helpers->recipe->options->wp_query( $args, true, esc_attr__( 'Any course', 'uncanny-automator' ) );
		$course_relevant_tokens = [
			'LDCOURSE'     => esc_attr__( 'Course title', 'uncanny-automator' ),
			'LDCOURSE_ID'  => esc_attr__( 'Course ID', 'uncanny-automator' ),
			'LDCOURSE_URL' => esc_attr__( 'Course URL', 'uncanny-automator' ),
		];
		$lesson_relevant_tokens = [
			'LDLESSON'     => esc_attr__( 'Lesson title', 'uncanny-automator' ),
			'LDLESSON_ID'  => esc_attr__( 'Lesson ID', 'uncanny-automator' ),
			'LDLESSON_URL' => esc_attr__( 'Lesson URL', 'uncanny-automator' ),
		];
		$relevant_tokens        = [
			$this->trigger_meta          => esc_attr__( 'Topic title', 'uncanny-automator' ),
			$this->trigger_meta . '_ID'  => esc_attr__( 'Topic ID', 'uncanny-automator' ),
			$this->trigger_meta . '_URL' => esc_attr__( 'Topic URL', 'uncanny-automator' ),
		];
		$trigger                = array(
			'author'              => Automator()->get_author_name( $this->trigger_code ),
			'support_link'        => Automator()->get_author_support_link( $this->trigger_code, 'integration/learndash/' ),
			'integration'         => self::$integration,
			'code'                => $this->trigger_code,
			/* translators: Logged-in trigger - LearnDash */
			'sentence'            => sprintf( esc_attr__( 'A user completes {{a topic:%1$s}} {{a number of:%2$s}} time(s)', 'uncanny-automator' ), $this->trigger_meta, 'NUMTIMES' ),
			/* translators: Logged-in trigger - LearnDash */
			'select_option_name'  => esc_attr__( 'A user completes {{a topic}}', 'uncanny-automator' ),
			'action'              => 'learndash_topic_completed',
			'priority'            => 10,
			'accepted_args'       => 1,
			'validation_function' => array( $this, 'topic_completed' ),
			'options'             => [
				Automator()->helpers->recipe->options->number_of_times(),
			],
			'options_group'       => [
				$this->trigger_meta => [
					Automator()->helpers->recipe->field->select_field_ajax(
						'LDCOURSE',
						esc_attr__( 'Course', 'uncanny-automator' ),
						$course_options,
						'',
						'',
						false,
						true,
						[
							'target_field' => 'LDLESSON',
							'endpoint'     => 'select_lesson_from_course_LD_TOPICDONE',
						],
						$course_relevant_tokens
					),
					Automator()->helpers->recipe->field->select_field_ajax(
						'LDLESSON',
						esc_attr__( 'Lesson', 'uncanny-automator' ),
						$lesson_options,
						'',
						'',
						false,
						true,
						[
							'target_field' => 'LDTOPIC',
							'endpoint'     => 'select_topic_from_lesson_LD_TOPICDONE',
						],
						$lesson_relevant_tokens
					),
					Automator()->helpers->recipe->field->select_field( 'LDTOPIC', esc_attr__( 'Topic', 'uncanny-automator' ), array(), false, false, false, $relevant_tokens ),
				],
			],
		);

		Automator()->register->trigger( $trigger );

		return;
	}