UOG_CREATEUNCANNYGROUP::create_uncanny_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)


Source Source

File: src/integrations/uncanny-groups/actions/uog-createuncannygroup.php

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



		$uo_group_title               = Automator()->parse->text( $action_data['meta']['UOGROUPTITLE'], $recipe_id, $user_id, $args );
		$uo_group_num_seats           = absint( Automator()->parse->text( $action_data['meta']['UOGROUPNUMSEATS'], $recipe_id, $user_id, $args ) );
		$uo_group_courses             = Automator()->parse->text( $action_data['meta']['UOGROUPCOURSES'], $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 ) ) {
			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 ) {
			// bail early
			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;
		}

		update_post_meta( $group_id, '_ulgm_is_custom_group_created', 'yes' );

		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 );
			}
		}
		update_post_meta( $group_id, '_ulgm_total_seats', $uo_group_num_seats );
		$order_id      = \uncanny_learndash_groups\Database::get_random_order_number();
		$attr          = array(
			'user_id'    => $user_id,
			'order_id'   => $order_id,
			'group_id'   => $group_id,
			'group_name' => $group_title,
			'qty'        => $uo_group_num_seats,
		);
		$codes         = \uncanny_learndash_groups\SharedFunctions::generate_random_codes( $uo_group_num_seats );
		$code_group_id = \uncanny_learndash_groups\Database::add_codes( $attr, $codes );

		update_post_meta( $group_id, \uncanny_learndash_groups\SharedFunctions::$code_group_id_meta_key, $code_group_id );
		update_user_meta( $user_id, '_ulgm_custom_order_id', $order_id );

		if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {
			\uncanny_learndash_groups\RestApiEndPoints::add_existing_user( array( 'user_email' => $user->user_email ), true, $group_id, $order_id, 'redeemed', false );
		}


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

		return;
	}