Action uncanny-learndash-groups

ulgm_seat_key_deleted

Fires after a seat key is deleted, passing the deleted key code and the associated LearnDash group ID.

add_action( 'ulgm_seat_key_deleted', $callback, 10, 2 );

Description

Fires after a seat key is deleted. Developers can use this hook to perform custom actions when a sign-up code is removed, such as updating related user data or logging the deletion. It receives the deleted code and the associated LearnDash group ID.


Usage

add_action( 'ulgm_seat_key_deleted', 'your_function_name', 10, 2 );

Parameters

$code (mixed)
The code of the deleted sign-up code.
$ld_group_id (mixed)
The `$code` parameter contains the specific sign-up code that has been deleted from the system.

Examples

add_action( 'ulgm_seat_key_deleted', 'my_handle_seat_key_deleted', 10, 2 );

/**
 * Example function to handle the ulgm_seat_key_deleted action.
 * This function might log the deletion of a seat key or perform other cleanup tasks.
 *
 * @param mixed $code The deleted seat code.
 * @param mixed $ld_group_id The ID of the LearnDash group associated with the seat.
 */
function my_handle_seat_key_deleted( $code, $ld_group_id ) {
    // Check if the group ID is valid and if the code is not empty.
    if ( ! empty( $ld_group_id ) && ! empty( $code ) ) {
        // In a real-world scenario, you might want to log this event.
        error_log( sprintf( 'Seat key "%s" was deleted from group ID "%d".', $code, $ld_group_id ) );

        // You could also potentially trigger other related actions or update a custom meta field.
        // For instance, if you're tracking seat usage across multiple systems.

        // Example: Update a custom meta field on the group post if it exists.
        if ( get_post_type( $ld_group_id ) === 'groups' ) { // Assuming 'groups' is the post type for LearnDash groups
            $deleted_keys_count = get_post_meta( $ld_group_id, '_ulgm_deleted_seat_keys_count', true );
            if ( empty( $deleted_keys_count ) || ! is_numeric( $deleted_keys_count ) ) {
                $deleted_keys_count = 0;
            }
            update_post_meta( $ld_group_id, '_ulgm_deleted_seat_keys_count', (int) $deleted_keys_count + 1 );
        }
    }
}

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:723

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