Skip to content
Merged
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
27 changes: 26 additions & 1 deletion classes/models/FrmFieldValueSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ class FrmFieldValueSelector {
*/
protected $db_row;

/**
* Field dropdowns will truncate at 25 characters by default.
*
* @var int|null
*
* @since x.x
*/
protected $truncate;

/**
* FrmFieldValueSelector constructor
*
Expand All @@ -83,6 +92,7 @@ public function __construct( $field_id, $args ) {
$this->set_html_name( $args );
$this->set_value( $args );
$this->set_source( $args );
$this->set_truncate( $args );

$this->field_id = (int) $field_id;
if ( $this->field_id === 0 ) {
Expand Down Expand Up @@ -196,6 +206,19 @@ protected function set_source( $args ) {
}
}

/**
* @since x.x
*
* @param array $args
*
* @return void
*/
protected function set_truncate( $args ) {
if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) {
$this->truncate = (int) $args['truncate'];
}
}
Comment on lines +209 to +220
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation and improve method documentation.

The method needs the following improvements:

  1. Add validation for negative values
  2. Enhance the documentation with parameter details and validation rules
 /**
+ * Sets the truncation length for field dropdown options.
+ *
+ * @param array $args {
+ *     @type int|null $truncate Optional. The number of characters at which to truncate options.
+ *                              Must be a positive integer or null. Default null.
+ * }
  *
- * @since x.x
+ * @since {ACTUAL_VERSION}
  *
  * @param array $args
  *
  * @return void
  */
 protected function set_truncate( $args ) {
-    if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) {
+    if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) && $args['truncate'] > 0 ) {
         $this->truncate = (int) $args['truncate'];
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @since x.x
*
* @param array $args
*
* @return void
*/
protected function set_truncate( $args ) {
if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) {
$this->truncate = (int) $args['truncate'];
}
}
/**
* Sets the truncation length for field dropdown options.
*
* @param array $args {
* @type int|null $truncate Optional. The number of characters at which to truncate options.
* Must be a positive integer or null. Default null.
* }
*
* @since {ACTUAL_VERSION}
*
* @param array $args
*
* @return void
*/
protected function set_truncate( $args ) {
if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) && $args['truncate'] > 0 ) {
$this->truncate = (int) $args['truncate'];
}
}


/**
* Check if object has any options
*
Expand Down Expand Up @@ -256,13 +279,15 @@ protected function display_dropdown() {
echo '<option value="">' . esc_html( $this->blank_option_label ) . '</option>';

if ( ! empty( $this->options ) ) {
$truncate = isset( $this->truncate ) ? $this->truncate : 25;

foreach ( $this->options as $key => $value ) {
if ( $value == '' ) {
continue;
}

$option = $this->get_single_field_option( $key, $value );
$option->print_single_option( $this->value, 25 );
$option->print_single_option( $this->value, $truncate );
}
}

Expand Down