Created
February 2, 2026 09:08
-
-
Save saifsultanc/064788a73bde897a9190742a81c3ed41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'gform_update_status', function( $entry_id, $status, $previous_status ) { | |
| // When entry is marked as spam. | |
| if ( $status !== 'spam' || $previous_status === 'spam' ) { | |
| return; | |
| } | |
| $entry = GFAPI::get_entry( $entry_id ); | |
| $form = GFAPI::get_form( $entry['form_id'] ); | |
| // Find the Unique ID field in the form, and remove the UID from the entry | |
| foreach ( $form['fields'] as $field ) { | |
| if ( $field->type === 'uid' ) { | |
| $uid_field_id = $field->id; | |
| $uid_value = rgar( $entry, $uid_field_id ); | |
| if ( $uid_value ) { | |
| // Check if this is a sequential unique ID field | |
| $uid_type = $field->{gp_unique_id()->perk->key( 'type' )}; | |
| if ( $uid_type === 'sequential' ) { | |
| // Parse the sequential number from the UID | |
| $sequential_number = gpuid_extract_sequential_number( $uid_value, $field, $form, $entry ); | |
| if ( $sequential_number !== false ) { | |
| // Check if this was the highest sequential number for this field | |
| if ( gpuid_is_highest_sequential_number( $form['id'], $uid_field_id, $sequential_number ) ) { | |
| // Roll back the sequence counter so the next entry can reuse this number | |
| gp_unique_id()->set_sequential_starting_number( $form['id'], $uid_field_id, $sequential_number - 1 ); | |
| } | |
| } | |
| } | |
| // Clear the UID from the entry | |
| GFAPI::update_entry_field( $entry_id, $uid_field_id, '' ); | |
| } | |
| } | |
| } | |
| }, 10, 3 ); | |
| function gpuid_extract_sequential_number( $uid_value, $field, $form, $entry ) { | |
| $atts = gp_unique_id()->get_field_unique_id_attributes( $field, 5, array(), $entry ); | |
| $prefix = GFCommon::replace_variables( $atts['prefix'], $form, $entry, false, true, false, 'text' ); | |
| $suffix = GFCommon::replace_variables( $atts['suffix'], $form, $entry, false, true, false, 'text' ); | |
| $pattern = '/^' . preg_quote( $prefix, '/' ) . '(\d+)' . preg_quote( $suffix, '/' ) . '$/'; | |
| if ( preg_match( $pattern, $uid_value, $matches ) ) { | |
| return intval( $matches[1] ); | |
| } | |
| return false; | |
| } | |
| function gpuid_is_highest_sequential_number( $form_id, $field_id, $sequential_number ) { | |
| global $wpdb; | |
| $current_sequence = $wpdb->get_var( $wpdb->prepare( | |
| "SELECT current FROM {$wpdb->prefix}gpui_sequence WHERE form_id = %d AND field_id = %d AND slug = ''", | |
| $form_id, | |
| $field_id | |
| ) ); | |
| if ( $current_sequence === null ) { | |
| return false; | |
| } | |
| // Check if the sequential number matches the current sequence | |
| return intval( $sequential_number ) === intval( $current_sequence ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment