HyperDB Support for Sequential IDs

Code

Filename: gpuid-hyperdb.php

<?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 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.