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
2 changes: 2 additions & 0 deletions classes/controllers/FrmFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
194 changes: 194 additions & 0 deletions classes/controllers/FrmOptionGroupsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}

/**
* Option groups let dropdown options be organized under labeled headings.
*
* The group name is stored on each option as a 'group' key next to 'label' and 'value',
* so anything that reads options without knowing about groups keeps working unchanged.
*
* @since x.x
*/
class FrmOptionGroupsController {

/**
* A line in the Bulk Edit Options text that starts with this marker begins a new group.
* The marker on its own ends the current group.
*
* @var string
*/
const BULK_EDIT_MARKER = '##';

/**
* The label of the optgroup that is currently open while a dropdown is printed.
*
* @var string
*/
private static $open_group = '';

/**
* @since x.x
*
* @param array|object $field The field settings.
*
* @return bool
*/
public static function field_supports_groups( $field ) {
$type = is_array( $field ) ? ( $field['type'] ?? '' ) : $field->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 '<optgroup label="' . esc_attr( $group ) . '">';
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 '</optgroup>';
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',
);
?>
<input <?php FrmAppHelper::array_to_html_params( $input_atts, true ); ?> />
<?php
}

/**
* Turns "## Group name" lines from the Bulk Edit Options text into groups
* for the options that follow them.
*
* @since x.x
*
* @param array $opts One item per line, either a string or an array with 'label' and 'value'.
* @param array $field The field the options belong to.
*
* @return array
*/
public static function bulk_edit_options( $opts, $field ) {
if ( ! is_array( $opts ) || ! self::field_supports_groups( $field ) ) {
return $opts;
}

$grouped_opts = array();
$group = '';

foreach ( $opts as $opt ) {
$label = is_array( $opt ) ? (string) ( $opt['label'] ?? '' ) : (string) $opt;

if ( str_starts_with( $label, self::BULK_EDIT_MARKER ) ) {
$group = trim( substr( $label, strlen( self::BULK_EDIT_MARKER ) ) );
continue;
}

if ( '' === $group ) {
$grouped_opts[] = $opt;
continue;
}

if ( ! is_array( $opt ) ) {
$opt = array(
'label' => $opt,
'value' => $opt,
);
}

$opt['group'] = $group;
$grouped_opts[] = $opt;
}//end foreach

return $grouped_opts;
}
}
4 changes: 4 additions & 0 deletions classes/views/frm-fields/front-end/dropdown-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
$option_params['disabled'] = 'disabled';
}

FrmOptionGroupsController::before_dropdown_option( $field, $opt_key );

echo '<option ';
FrmAppHelper::array_to_html_params( $option_params, true );
selected( (bool) $selected );
Expand All @@ -96,6 +98,8 @@

unset( $option_params );
}//end foreach

FrmOptionGroupsController::after_dropdown_options();
?>
</select>
<?php
Expand Down
5 changes: 4 additions & 1 deletion classes/views/frm-fields/single-option.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@
?>
</span>

<?php do_action( 'frm_admin_single_opt', compact( 'field', 'opt', 'opt_key' ) ); ?>
<?php
FrmOptionGroupsController::admin_single_opt( compact( 'field', 'opt_key' ) );
do_action( 'frm_admin_single_opt', compact( 'field', 'opt', 'opt_key' ) );
?>
</li>
2 changes: 1 addition & 1 deletion css/frm_admin.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { syncFieldOrder, confirmFieldOrderSaved } = require( './fieldOrder' );
const { allowContainerDrop } = require( './fieldPlacement' );
const { getRangeSettingsDefaults, validateNumberRangeSetting, validateStepSetting, validateRangeSettings } = require( './settings/validateRangeSettings' );
const { initFieldListHoverPill } = require( './fieldListHoverPill' );
const { setupOptionGroups, initOptionGroupLists, refreshOptionGroups, addGroupLineToBulkEdit } = require( './optionGroups' );
const { initShowBoxIconSwap } = require( './showBoxIconSwap' );
const { initFieldSizePreview } = require( './fieldSizePreview' );
const { hydrateBuilderSelect, hydrateBuilderSelectsIn } = require( './sharedSelectOptions' );
Expand Down Expand Up @@ -4866,6 +4867,7 @@ window.frmAdminBuildJS = function() {
if ( key !== '000' ) {
const label = document.getElementsByName( `field_options[options_${ fieldId }][${ key }][label]` )[ 0 ];
if ( label !== undefined ) {
content += addGroupLineToBulkEdit( '', { optionEl: opts[ i ] } );
content += label.value;
if ( separate ) {
content += `|${ document.getElementsByName( `field_options[options_${ fieldId }][${ key }][value]` )[ 0 ].value }`;
Expand Down Expand Up @@ -9513,6 +9515,7 @@ window.frmAdminBuildJS = function() {
);
}

initOptionGroupLists( singleField );
wp.hooks.doAction( 'frmShowedFieldSettings', obj, singleField );
maybeAddShortcodesModalTriggerIcon( fieldType, fieldId, singleField );

Expand Down Expand Up @@ -12734,6 +12737,7 @@ window.frmAdminBuildJS = function() {

document.querySelectorAll( '.field_type_list > li:not(.frm_show_upgrade):not(.frm_show_update)' ).forEach( makeDraggable );
initFieldListHoverPill();
setupOptionGroups();
initFieldSizePreview();

jQuery( 'ul.field_type_list, .field_type_list li, ul.frm_code_list, .frm_code_list li, .frm_code_list li a, #frm_adv_info #category-tabs li, #frm_adv_info #category-tabs li a' ).disableSelection();
Expand Down Expand Up @@ -13458,6 +13462,7 @@ window.frmAdminBuildJS = function() {
},
success( html ) {
document.getElementById( `frm_field_${ fieldId }_opts` ).innerHTML = html;
refreshOptionGroups( fieldId );
wp.hooks.doAction( 'frm_after_bulk_edit_opts', fieldId );
resetDisplayedOpts( fieldId );

Expand Down
Loading
Loading