diff --git a/classes/controllers/FrmFieldsController.php b/classes/controllers/FrmFieldsController.php index 7d7281028f..b321468f54 100644 --- a/classes/controllers/FrmFieldsController.php +++ b/classes/controllers/FrmFieldsController.php @@ -570,6 +570,8 @@ public static function import_options() { } } + $opts = FrmOptionGroupsController::bulk_edit_options( $opts, $field ); + // Keep other options after bulk update. if ( ! empty( $field['field_options']['other'] ) ) { $other_array = array(); diff --git a/classes/controllers/FrmOptionGroupsController.php b/classes/controllers/FrmOptionGroupsController.php new file mode 100644 index 0000000000..09d43a3289 --- /dev/null +++ b/classes/controllers/FrmOptionGroupsController.php @@ -0,0 +1,194 @@ +type; + + if ( 'select' !== $type ) { + return false; + } + + $post_field = is_array( $field ) ? ( $field['post_field'] ?? '' ) : FrmField::get_option( $field, 'post_field' ); + return 'post_category' !== $post_field; + } + + /** + * @since x.x + * + * @param array $field The field settings. + * @param int|string $opt_key The option key. + * + * @return string + */ + public static function get_option_group( $field, $opt_key ) { + $option = $field['options'][ $opt_key ] ?? ''; + + if ( ! is_array( $option ) || ! isset( $option['group'] ) || ! is_string( $option['group'] ) ) { + return ''; + } + + return trim( $option['group'] ); + } + + /** + * Opens or closes an optgroup when the group changes between two options. + * + * @since x.x + * + * @param array $field The field settings. + * @param int|string $opt_key The option key. + * + * @return void + */ + public static function before_dropdown_option( $field, $opt_key ) { + if ( ! self::field_supports_groups( $field ) ) { + return; + } + + $group = self::get_option_group( $field, $opt_key ); + + if ( $group === self::$open_group ) { + return; + } + + self::close_open_group(); + + if ( '' === $group ) { + return; + } + + echo ''; + self::$open_group = $group; + } + + /** + * @since x.x + * + * @return void + */ + public static function after_dropdown_options() { + self::close_open_group(); + } + + /** + * @since x.x + * + * @return void + */ + private static function close_open_group() { + if ( '' === self::$open_group ) { + return; + } + + echo ''; + self::$open_group = ''; + } + + /** + * Adds the hidden group input to each option row in the builder. + * + * @since x.x + * + * @param array $args Includes the field array and the option key. + * + * @return void + */ + public static function admin_single_opt( $args ) { + $field = $args['field']; + + if ( ! self::field_supports_groups( $field ) || FrmFieldsHelper::is_other_opt( $args['opt_key'] ) ) { + return; + } + + $input_atts = array( + 'type' => 'hidden', + 'name' => 'field_options[options_' . $field['id'] . '][' . $args['opt_key'] . '][group]', + 'value' => self::get_option_group( $field, $args['opt_key'] ), + 'class' => 'frm-option-group-input', + ); + ?> + /> + $opt, + 'value' => $opt, + ); + } + + $opt['group'] = $group; + $grouped_opts[] = $opt; + }//end foreach + + return $grouped_opts; + } +} diff --git a/classes/views/frm-fields/front-end/dropdown-field.php b/classes/views/frm-fields/front-end/dropdown-field.php index e89396707b..f2c5ea58d7 100644 --- a/classes/views/frm-fields/front-end/dropdown-field.php +++ b/classes/views/frm-fields/front-end/dropdown-field.php @@ -85,6 +85,8 @@ $option_params['disabled'] = 'disabled'; } + FrmOptionGroupsController::before_dropdown_option( $field, $opt_key ); + echo '