LD_CREATEGROUP::create_group( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the trigger action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)

$args

(Required)


Source Source

File: src/integrations/learndash/actions/ld-creategroup.php

	public function create_group( $user_id, $action_data, $recipe_id, $args ) {



		$uo_group_title               = Automator()->parse->text( $action_data['meta']['LDGROUPTITLE'], $recipe_id, $user_id, $args );
		$uo_group_courses             = Automator()->parse->text( $action_data['meta']['LDGROUPCOURSES'], $recipe_id, $user_id, $args );
		$group_leader_role_assignment = Automator()->parse->text( $action_data['meta']['GROUP_LEADER_ROLE_ASSIGNMENT'], $recipe_id, $user_id, $args );

		$create_group = false;
		$user         = get_user_by( 'ID', $user_id );
		if ( is_wp_error( $user_id ) ) {
			return;
		}

		if ( user_can( $user, 'group_leader' ) ) {
			$create_group = true;
		} else {
			switch ( trim( $group_leader_role_assignment ) ) {
				case 'add':
					$user->add_role( 'group_leader' );
					$create_group = true;
					break;
				case 'replace':
					$user->set_role( 'group_leader' );
					$create_group = true;
					break;
			}
		}
		if ( false === $create_group ) {
			return;
		}

		$group_title = $uo_group_title;

		$ld_group_args = array(
			'post_type'    => 'groups',
			'post_status'  => apply_filters( 'uo_create_new_group_status', 'publish' ),
			'post_title'   => $group_title,
			'post_content' => '',
			'post_author'  => $user_id,
		);

		$group_id = wp_insert_post( $ld_group_args );

		if ( is_wp_error( $group_id ) ) {
			return;
		}

		ld_update_leader_group_access( $user_id, $group_id );

		$group_courses = json_decode( $uo_group_courses );

		if ( ! empty( $group_courses ) ) {
			foreach ( $group_courses as $course_id ) {
				ld_update_course_group_access( (int) $course_id, (int) $group_id, false );
				$transient_key = 'learndash_course_groups_' . $course_id;
				delete_transient( $transient_key );
			}
		}


		Automator()->complete_action( $user_id, $action_data, $recipe_id );

		return;
	}