Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions classes/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2781,23 +2781,21 @@ 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 );

$similar_keys = FrmDb::get_col(
$table_name,
array(
$column . ' like%' => $key,
'ID !' => $id,
),
$column
);
$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.
if ( ! in_array( $key, $similar_keys, true ) ) {
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 = self::get_similar_keys( $key, $table_name, $column, $id );
}

/**
* Allow for a custom separator between the attempted key and the generated suffix.
Expand All @@ -2818,6 +2816,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<string>
*/
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.
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/misc/test_FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmAppHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

$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.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmAppHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

}

private function add_field_to_form( $form_id, $field_key ) {
$type = 'text';
$this->factory->field->create( compact( 'type', 'form_id', 'field_key' ) );
Expand Down
Loading