Action uncanny-learndash-groups

ulgm_seat_key_replaced

Fires when a user's seat key is replaced, providing the new and old codes, user ID, and LearnDash group ID.

add_action( 'ulgm_seat_key_replaced', $callback, 10, 4 );

Description

Fires after a sign-up code has been successfully replaced within a LearnDash group. Developers can use this hook to perform custom actions like logging the replacement, updating related data, or triggering notifications when a seat code is updated for a specific group and user.


Usage

add_action( 'ulgm_seat_key_replaced', 'your_function_name', 10, 4 );

Parameters

$new_code (mixed)
The `$new_code` parameter holds the new seat key code that has replaced the old one.
$code (mixed)
This parameter holds the newly generated or assigned seat code after a replacement has occurred.
$user_id (mixed)
This parameter contains the code that was replaced.
$ld_group_id (mixed)
The user ID of the WordPress user whose seat key was replaced.

Examples

add_action( 'ulgm_seat_key_replaced', 'my_ulgm_handle_seat_key_replaced', 10, 4 );

/**
 * Example callback function for the ulgm_seat_key_replaced action hook.
 * This function demonstrates how to react when a seat key is replaced.
 *
 * @param mixed $new_code The new code that replaced the old one.
 * @param mixed $code     The original code that was replaced.
 * @param mixed $user_id  The user ID associated with the replaced code.
 * @param mixed $ld_group_id The LearnDash Group ID.
 */
function my_ulgm_handle_seat_key_replaced( $new_code, $code, $user_id, $ld_group_id ) {
	// In a real scenario, you might want to log this event,
	// send a notification, or perform other administrative tasks.

	// Example: Log the replacement event to the WordPress debug log.
	if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
		error_log( sprintf(
			'ULGM Seat Key Replaced: Old code "%s" was replaced with new code "%s" for user ID "%s" in group ID "%s".',
			esc_html( $code ),
			esc_html( $new_code ),
			esc_html( $user_id ),
			esc_html( $ld_group_id )
		) );
	}

	// Example: If you needed to update another meta field in the group
	// based on this replacement, you could do it here.
	// update_post_meta( $ld_group_id, 'last_seat_key_replacement', time() );
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/handlers/class-group-management-db-handler.php:749

public function remove_sign_up_code( $code, $ld_group_id = null, $replace = false ) {
		global $wpdb;

		if ( empty( $code ) ) {
			return null;
		}
		// To avoid any warnings.
		if ( is_array( $code ) ) {
			foreach ( $code as $cc ) {
				$this->remove_sign_up_code( $cc, $ld_group_id, $replace );
			}

			//all codes dealt above; return;
			return true;
		}

		/**
		 * Decrease seat count of the group is a "Completed" status user is removed from a
		 * group and the setting is turned on
		 *
		 * @since 4.0.6
		 */
		$user_id = $this->get_user_id_from_code( $code );
		$status  = SharedFunctions::has_user_completed_all_courses_in_group( $user_id, $ld_group_id );
		if ( 'yes' === get_option( 'do_not_restore_seat_if_user_is_removed', 'no' ) && 'yes' === get_option( 'allow_to_remove_users_anytime', 'no' ) && true === $status ) {
			$replace = false;
			add_post_meta( $ld_group_id, 'user-id-removed-completed-status-' . $user_id, time() );
		}

		if ( false === $replace ) {
			$wpdb->delete(
				$wpdb->prefix . ulgm()->db->tbl_group_codes,
				array( 'code' => $code ),
				array( '%s' )
			);
			do_action( 'ulgm_seat_key_deleted', $code, $ld_group_id );

			return true;
		}
		$new_code = $this->generate_random_codes( 1 );
		if ( $new_code ) {
			$new_code = array_shift( $new_code );

			//Update to change from php null to mysql NULL
			$wpdb->query(
				$wpdb->prepare(
					'UPDATE ' . $wpdb->prefix . ulgm()->db->tbl_group_codes . '
									SET code = %s,
										code_status = %s,
										used_date = NULL,
										student_id = NULL,
										user_email = NULL,
										first_name = NULL,
										last_name = NULL,
										ld_group_id = NULL
										WHERE code = %s',
					$new_code,
					'available',
					trim( $code )
				)
			);
			do_action( 'ulgm_seat_key_replaced', $new_code, trim( $code ), $user_id, $ld_group_id );

			return true;
		}

		return null;
	}


Scroll to Top