Skip to content

Commit d36fc48

Browse files
committed
Settings API: support live_update_field_options for repeater titles
Render the mapped option label (instead of the raw value) for select and TomSelect-enhanced repeater fields, both server-side and via JS. Adds a new optional repeater arg live_update_field_options and listens for input+change events so selects update the title live.
1 parent 9359fc2 commit d36fc48

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

includes/admin/settings/class-settings-form.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,10 @@ public function callback_repeater( $args ) {
999999
$class = $this->get_field_class( $args );
10001000
$attributes = $this->get_boolean_attributes( $args ) . $this->build_field_attributes( $args );
10011001

1002-
$data_index = (string) count( $value );
1003-
$live_update_field = ! empty( $args['live_update_field'] ) ? $args['live_update_field'] : 'name';
1004-
$fallback_title = ! empty( $args['new_item_text'] ) ? $args['new_item_text'] : $this->translation_strings['repeater_new_item'];
1002+
$data_index = (string) count( $value );
1003+
$live_update_field = ! empty( $args['live_update_field'] ) ? $args['live_update_field'] : 'name';
1004+
$fallback_title = ! empty( $args['new_item_text'] ) ? $args['new_item_text'] : $this->translation_strings['repeater_new_item'];
1005+
$live_update_options = ! empty( $args['live_update_field_options'] ) && is_array( $args['live_update_field_options'] ) ? $args['live_update_field_options'] : array();
10051006

10061007
ob_start();
10071008
?>
@@ -1010,6 +1011,9 @@ public function callback_repeater( $args ) {
10101011
data-index="<?php echo esc_attr( $data_index ); ?>"
10111012
data-live-update-field="<?php echo esc_attr( $live_update_field ); ?>"
10121013
data-fallback-title="<?php echo esc_attr( $fallback_title ); ?>"
1014+
<?php if ( ! empty( $live_update_options ) ) : ?>
1015+
data-live-update-field-options="<?php echo esc_attr( wp_json_encode( $live_update_options ) ); ?>"
1016+
<?php endif; ?>
10131017
<?php echo $attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
10141018

10151019
<div class="<?php echo esc_attr( $args['id'] ); ?>-items wz-repeater-items">
@@ -1069,9 +1073,14 @@ public function render_repeater_item( $args, $index, $item = null ) {
10691073
<input type="hidden" name="<?php echo esc_attr( $this->settings_key ); ?>[<?php echo esc_attr( $args['id'] ); ?>][<?php echo esc_attr( $index ); ?>][row_id]" value="<?php echo esc_attr( $item_id ); ?>" />
10701074
<div class="repeater-item-header">
10711075
<?php
1072-
$display_field = ! empty( $args['live_update_field'] ) ? $args['live_update_field'] : 'name';
1076+
$display_field = ! empty( $args['live_update_field'] ) ? $args['live_update_field'] : 'name';
1077+
$live_options = ! empty( $args['live_update_field_options'] ) && is_array( $args['live_update_field_options'] ) ? $args['live_update_field_options'] : array();
1078+
$raw_live_value = ! empty( $item['fields'][ $display_field ] ) ? (string) $item['fields'][ $display_field ] : '';
1079+
$display_value = '' !== $raw_live_value
1080+
? ( isset( $live_options[ $raw_live_value ] ) ? $live_options[ $raw_live_value ] : $raw_live_value )
1081+
: $fallback_title;
10731082
?>
1074-
<span class="repeater-title"><?php echo esc_html( ! empty( $item['fields'][ $display_field ] ) ? $item['fields'][ $display_field ] : $fallback_title ); ?></span>
1083+
<span class="repeater-title"><?php echo esc_html( $display_value ); ?></span>
10751084
<span class="toggle-icon">▼</span>
10761085
</div>
10771086
<div class="repeater-item-content" style="display: none;">

includes/admin/settings/js/settings-admin-scripts.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ jQuery(document).ready(function ($) {
6464
var index = parseInt(wrapper.data('index'), 10) || 0;
6565
var liveUpdateField = wrapper.data('live-update-field') || 'name';
6666
var fallbackTitle = wrapper.data('fallback-title') || '';
67+
var liveUpdateOptions = wrapper.data('live-update-field-options') || {};
6768

6869
function reindexItems() {
6970
itemsContainer.find('.wz-repeater-item').each(function (idx) {
@@ -139,11 +140,22 @@ jQuery(document).ready(function ($) {
139140
});
140141

141142
// Live update repeater title when the specified field changes.
142-
wrapper.on('input', '.wz-repeater-item :input[name$="[fields][' + liveUpdateField + ']"]', function () {
143-
var $this = $(this);
144-
var newName = $this.val();
145-
var $repeaterTitle = $this.closest('.wz-repeater-item').find('.repeater-title');
146-
$repeaterTitle.text(newName || fallbackTitle);
143+
// Handles text inputs (input event), selects (change event, uses option text),
144+
// and TomSelect-enhanced inputs (change event, uses displayed text or value).
145+
function updateRepeaterTitle($field) {
146+
var newName;
147+
var val = $field.val();
148+
if ($field.is('select')) {
149+
// Ignore placeholder options (empty value).
150+
newName = val ? $field.find('option:selected').text().trim() : '';
151+
} else {
152+
newName = val ? (liveUpdateOptions[val] || val) : '';
153+
}
154+
$field.closest('.wz-repeater-item').find('.repeater-title').text(newName || fallbackTitle);
155+
}
156+
157+
wrapper.on('input change', '.wz-repeater-item :input[name$="[fields][' + liveUpdateField + ']"]', function () {
158+
updateRepeaterTitle($(this));
147159
});
148160
});
149161

includes/admin/settings/js/settings-admin-scripts.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)