From 81db2d8d9e0b552c728b49cda7ed6ce07559fea8 Mon Sep 17 00:00:00 2001 From: "vivi-the-going-merry[bot]" <308115520+vivi-the-going-merry[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:41:15 -0600 Subject: [PATCH 1/4] Add failing test: unique key suffix for truncated long form key Co-Authored-By: Claude Sonnet 5.5 --- tests/phpunit/misc/test_FrmAppHelper.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/phpunit/misc/test_FrmAppHelper.php b/tests/phpunit/misc/test_FrmAppHelper.php index 84d0864862..c9f77110ec 100644 --- a/tests/phpunit/misc/test_FrmAppHelper.php +++ b/tests/phpunit/misc/test_FrmAppHelper.php @@ -522,6 +522,22 @@ public function test_get_unique_key() { $this->assertNotSame( $super_long_form_key, $unique_key ); } + public function test_get_unique_key_increments_for_truncated_long_key() { + $table_name = 'frm_forms'; + $column = 'form_key'; + $long_key = str_repeat( 'a', 75 ); + $truncated = substr( $long_key, 0, 60 ); + + $this->factory->form->create( array( 'form_key' => $long_key ) ); + + $first = FrmAppHelper::get_unique_key( $long_key, $table_name, $column ); + $this->assertSame( $truncated . '2', $first ); + $this->factory->form->create( array( 'form_key' => $first ) ); + + $second = FrmAppHelper::get_unique_key( $long_key, $table_name, $column ); + $this->assertSame( $truncated . '3', $second, 'The suffix should skip keys already created from the truncated key.' ); + } + private function add_field_to_form( $form_id, $field_key ) { $type = 'text'; $this->factory->field->create( compact( 'type', 'form_id', 'field_key' ) ); From 4e213d8d01281df752ef684675db993b412e01e0 Mon Sep 17 00:00:00 2001 From: "vivi-the-going-merry[bot]" <308115520+vivi-the-going-merry[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:41:58 -0600 Subject: [PATCH 2/4] Reload similar keys after truncating a long form/field key Co-Authored-By: Claude Sonnet 5.5 --- classes/helpers/FrmAppHelper.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index fa9234fec0..a13cceeb40 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -2797,7 +2797,20 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu return $key; } - $key = self::maybe_truncate_key_before_appending( $column, $key ); + $truncated_key = self::maybe_truncate_key_before_appending( $column, $key ); + + if ( $truncated_key !== $key ) { + // Keys already created from the truncated key aren't in the list loaded for the full key. + $key = $truncated_key; + $similar_keys = FrmDb::get_col( + $table_name, + array( + $column . ' like%' => $key, + 'ID !' => $id, + ), + $column + ); + } /** * Allow for a custom separator between the attempted key and the generated suffix. From c4bf9c9be284dee0d9e1c6fd776e0b6c84689c40 Mon Sep 17 00:00:00 2001 From: "vivi-the-going-merry[bot]" <308115520+vivi-the-going-merry[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:43:12 -0600 Subject: [PATCH 3/4] Extract similar-keys query into a helper Co-Authored-By: Claude Sonnet 5.5 --- classes/helpers/FrmAppHelper.php | 37 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index a13cceeb40..beadd1f3b4 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -2783,14 +2783,7 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu $key = self::prevent_numeric_and_reserved_keys( $key ); - $similar_keys = FrmDb::get_col( - $table_name, - array( - $column . ' like%' => $key, - 'ID !' => $id, - ), - $column - ); + $similar_keys = self::get_similar_keys( $key, $table_name, $column, $id ); // Create a unique field id if it has already been used. if ( ! in_array( $key, $similar_keys, true ) ) { @@ -2802,14 +2795,7 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu if ( $truncated_key !== $key ) { // Keys already created from the truncated key aren't in the list loaded for the full key. $key = $truncated_key; - $similar_keys = FrmDb::get_col( - $table_name, - array( - $column . ' like%' => $key, - 'ID !' => $id, - ), - $column - ); + $similar_keys = self::get_similar_keys( $key, $table_name, $column, $id ); } /** @@ -2831,6 +2817,25 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu return $key_check; } + /** + * @param string $key + * @param string $table_name + * @param string $column + * @param int $id + * + * @return array + */ + private static function get_similar_keys( $key, $table_name, $column, $id ) { + return FrmDb::get_col( + $table_name, + array( + $column . ' like%' => $key, + 'ID !' => $id, + ), + $column + ); + } + /** * Avoid trying to append to a really long key, * The database limit is 100 for form and field keys so we want to avoid getting too close. From 125f4593b454667157bf84da9d4b95b0df50f705 Mon Sep 17 00:00:00 2001 From: "vivi-the-going-merry[bot]" <308115520+vivi-the-going-merry[bot]@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:46:18 -0600 Subject: [PATCH 4/4] Group consecutive assignments for PHPCS Co-Authored-By: Claude Sonnet 5.5 --- classes/helpers/FrmAppHelper.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index beadd1f3b4..b090246702 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -2781,8 +2781,7 @@ public static function get_unique_key( $name, $table_name, $column, $id = 0, $nu $key = self::generate_new_key( $num_chars ); } - $key = self::prevent_numeric_and_reserved_keys( $key ); - + $key = self::prevent_numeric_and_reserved_keys( $key ); $similar_keys = self::get_similar_keys( $key, $table_name, $column, $id ); // Create a unique field id if it has already been used.