UOG_ADDSEATSTOGROUP::add_seats_to_a_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-addseatstogroup.php

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

		$uo_group_id = Automator()->parse->text( $action_data['meta']['UNCANNYGROUP'], $recipe_id, $user_id, $args );
		$check_group = learndash_validate_groups( array( $uo_group_id ) );
		if ( empty( $check_group ) || ! is_array( $check_group ) ) {
			$error_message                       = esc_html__( 'The selected group is not found.', 'uncanny-automator' );
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

		$uo_group_num_seats = absint( Automator()->parse->text( $action_data['meta']['NUMOFSEATS'], $recipe_id, $user_id, $args ) );
		$code_group_id      = ulgm()->group_management->seat->get_code_group_id( $uo_group_id );
		if ( empty( $code_group_id ) ) {
			$error_message                       = __( 'Group management is not enabled on the selected group.', 'uncanny-automator' );
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}
		$existing_seats = ulgm()->group_management->seat->total_seats( $uo_group_id );

		// Seats added
		if ( $uo_group_num_seats > 0 ) {
			$new_seats = $existing_seats + $uo_group_num_seats;
			$new_codes = ulgm()->group_management->generate_random_codes( $uo_group_num_seats );

			$attr = array(
				'qty'           => $uo_group_num_seats,
				'code_group_id' => $code_group_id,
			);
			ulgm()->group_management->add_additional_codes( $attr, $new_codes );
			update_post_meta( $uo_group_id, '_ulgm_total_seats', $new_seats );
		}

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