• Home
  • Pricing
  • Free Resources
  • About
  • Docs
  • Support
  • Account

Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Gravity Shop
  • Blog
    • Free Resources
    • About
  • Support
    • Docs
    • Support
    • Account

gpui_sequential_unique_id_pre_insert

  1. Description
  2. Usage
  3. Parameters
  4. Examples
    1. Fill Gaps in Sequential Unique IDs
    2. Add HyperDB Support for Sequential IDs
  5. Since

Description

Modify the sequential ID that will be used prior to adding the ID to the database. This is useful for changing the behavior of how sequential IDs are selected.

Usage

add_filter( 'gpui_sequential_unique_id_pre_insert', 'my_custom_function', 10, 4 );

Parameters

  • uid number|bool

    The unique ID to be inserted. If false, the next ID will be calculated using MySQL AUTO_INCREMENT.

  • form_id int

    The ID of the form for which the unique ID is being generated.

  • field_id int

    The ID of the field for which the unique ID is being generated.

  • starting_number int

    The number from which the sequence should start.

Examples

Fill Gaps in Sequential Unique IDs

<?php
/**
 * GP Unique ID // Gravity Perks // Fill Gaps in Sequential Unique IDs
 * https://gravitywiz.com/documentation/gp-unique-id/
 *
 * Use this snippet to automatically fill gaps in the sequence of unique IDs for Unique ID fields of the "Sequential"
 * type. Gaps will occur when entries containing a sequential unique ID are deleted.
 *
 * Update the filter name "gpui_sequential_unique_id_pre_insert_519_5", replacing "519" with your form ID and "5" with
 * your field ID.
 */
add_filter( 'gpui_sequential_unique_id_pre_insert_519_5', function ( $uid, $form_id, $field_id ) {
	global $wpdb;

	$result = $wpdb->get_results( $wpdb->prepare( "
		select value 
		from {$wpdb->prefix}rg_lead_detail 
		where form_id = %d and field_number = %d", $form_id, $field_id ) );

	if ( empty( $result ) ) {
		return $uid;
	}

	$_uids   = wp_list_pluck( $result, 'value' );
	$form    = GFAPI::get_form( $form_id );
	$field   = GFFormsModel::get_field( $form, $field_id );
	$numbers = array();

	foreach ( $_uids as $_uid ) {

		$clean_uid = str_replace( $field['gp-unique-id_prefix'], '', $_uid );
		$clean_uid = str_replace( $field['gp-unique-id_suffix'], '', $clean_uid );
		$clean_uid = intval( ltrim( $clean_uid, '0' ) );

		$numbers[] = $clean_uid;

	}

	sort( $numbers );

	$range = range( 1, max( $numbers ) );
	$diff  = array_diff( $range, $numbers );

	// looks like we've found a gap in the sequence; busted!
	if ( ! empty( $diff ) ) {
		$uid = array_shift( $diff );
	}

	return $uid;
}, 10, 3 );

Add HyperDB Support for Sequential IDs

<?php
/**
 * Gravity Perks // GP Unique ID // HyperDB Support for Sequential IDs
 * https://gravitywiz.com/documentation/gravity-forms-unique-id/
 */
add_filter( 'gpui_sequential_unique_id_pre_insert', function ( $uid, $form_id, $field_id, $starting_number ) {
	global $wpdb;

	$wpdb->query( 'START TRANSACTION' );

	$wpdb->query( $wpdb->prepare(
		'INSERT INTO ' . $wpdb->prefix . 'gpui_sequence ( form_id, field_id, current ) VALUES ( %d, %d, ( @next := 1 ) ) ON DUPLICATE KEY UPDATE current = ( @next := current + 1 )',
		$form_id,
		$field_id
	) );

	$uid = $wpdb->get_var( $wpdb->prepare( 'SELECT `current` from ' . $wpdb->prefix . 'gpui_sequence where form_id = %d and field_id = %d', $form_id, $field_id ) );

	$wpdb->query( 'COMMIT' );

	return $uid;
}, 10, 5 );

Since

This filter is available since Gravity Forms Unique ID 1.2.2.

Gravity Shop

  • GS Product Configurator

Gravity Perks

  • GF Address Autocomplete
  • GF Advanced Calculations
  • GF Advanced Phone Field
  • GF Advanced Save & Continue
  • GF Advanced Select
  • GF Auto List Field
  • GF Auto Login
  • GF Better User Activation
  • GF Blocklist
  • GF Conditional Logic Dates
  • GF Conditional Pricing
  • GF Copy Cat
  • GF Date Time Calculator
  • GF Disable Entry Creation
  • GF Easy Passthrough
  • GF eCommerce Fields
  • GF Email Users
  • GF Entry Blocks
  • GF Expand Textareas
  • GF File Renamer
  • GF File Upload Pro
  • GF Google Sheets
  • GF Inventory
  • GF Limit Checkboxes
  • GF Limit Choices
  • GF Limit Dates
  • GF Limit Submissions
  • GF Live Preview
  • GF Media Library
  • GF Multi-Page Navigation
  • GF Nested Forms
  • GF Notification Scheduler
  • GF Page Transitions
  • GF Pay Per Word
  • GF PayPal One-time Fee
  • GF Placeholder
  • GF Populate Anything
  • GF Post Content Merge Tags
  • GF Preview Submission
  • GF Price Range
  • GF QR Code
  • GF Randomizer
  • GF Read Only
  • GF Reload Form
  • GF Terms Of Service
  • GF Unique ID
  • GF Word Count

Copyright © 2023 · Powered by WordPress · Gravity Wiz LLC

  • Support
  • Affiliates
  • About
  • Sitemap
  • Terms & Conditions of Use
  • Privacy Policy
  • Cookies Policy