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
163 changes: 102 additions & 61 deletions assets/js/src/wp-user-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ jQuery( function( $ ) {
init: function() {
var self = this;

$( '.add-repeater-row' ).each( function() {
var parent = $( this ).parents( 'fieldset' );
var repeater = parent.find( '.fieldset-wpum_field_group' ).not('.fieldset-wpum_field_group-clone' );

if ( repeater.length ) {
var name = parent.get( 0 ).classList[ 0 ];
self.increaseInstance( name );
self.validateMaxRows( name );
// Setup instances to first level repeaters
$('form > fieldset > .add-repeater-row').each(function () {
var fieldSet = $(this).closest( 'fieldset' );
var fieldGroup = $(fieldSet).find( ' > .fieldset-wpum_field_group' ).not('.fieldset-wpum_field_group-clone' );

if ( fieldGroup.length ) {
self.setupInstances(fieldSet, null);

var repeaterKey = self.getRepeaterKey(fieldSet);

self.increaseInstance( repeaterKey );
self.validateMaxRows( fieldSet );
}
} );

Expand All @@ -51,17 +55,21 @@ jQuery( function( $ ) {
} );

self.form.on( 'click', '.add-repeater-row', function() {
var parent = $( this ).parents( 'fieldset' );
self.addNewInstance( parent.get( 0 ).classList[ 0 ] );
var fieldSet = $(this).parent('fieldset');
// Setup new instance based on the parent fieldset
self.addNewInstance( fieldSet );
self.form.wpumConditionalFields({});
} );

self.form.on( 'click', '.remove-repeater-row', function(e) {
e.preventDefault();
var parent = $( this ).parents( 'fieldset' );
var $row = $( this ).parents( '.fieldset-wpum_field_group' );
var fieldSet = $(this).closest('fieldset');
var parentBase = $(fieldSet).attr('data-parent-base');
var $row = $( this ).parent( '.fieldset-wpum_field_group' );
$row.remove();
self.setupInstances( parent.get( 0 ).classList[ 0 ] );

self.setupInstances( fieldSet, parentBase );
self.validateMaxRows( fieldSet );
} );
},

Expand All @@ -73,23 +81,27 @@ jQuery( function( $ ) {
this.repeaters[ name ]++;
},

addNewInstance: function( name ) {
this.addNewRepeaterRow( name );
this.setupInstances( name );
addNewInstance: function( fieldSet ) {
this.addNewRepeaterRow(fieldSet);

var parentBase = $(fieldSet).attr('data-parent-base');
this.setupInstances(fieldSet, parentBase);

initFields();
},

resetInstance: function( name ) {
this.repeaters[ name ] = 0;
},

addNewRepeaterRow: function( name ) {
var repeater = $( '.' + name ).find( '.fieldset-wpum_field_group-clone' ).last();
addNewRepeaterRow: function (fieldSet) {
// Get repeater from the immediate child
var repeater = $(fieldSet).find(' > .fieldset-wpum_field_group-clone').last();
if ( !repeater.length ) {
return;
}

if ( !this.validateMaxRows( name ) ) {
if ( !this.validateMaxRows( fieldSet ) ) {
return;
}

Expand All @@ -99,58 +111,87 @@ jQuery( function( $ ) {
newRepeater.insertBefore( repeater );
},

setupInstances: function( name ) {
var repeaterRow = $( '.' + name ).find( '.fieldset-wpum_field_group' ).not( '.fieldset-wpum_field_group-clone' );
getRepeaterKey: function(fieldSet) {
var parentBase = $(fieldSet).attr('data-parent-base');
var repeaterKey = $(fieldSet).get(0).classList[0];
repeaterKey = repeaterKey.replace('fieldset-', '');

if (parentBase) {
repeaterKey = parentBase + '[' + repeaterKey + ']';
}

return repeaterKey;
},

setupInstances: function (fieldSet, parentBase) {
if (typeof parentBase === 'undefined' || parentBase === null) {
parentBase = null;
}

var repeaterRow = $( fieldSet ).find( ' > .fieldset-wpum_field_group' ).not( '.fieldset-wpum_field_group-clone' );
var self = this;

if ( !repeaterRow.length ) {
return;
}

self.resetInstance( name );

repeaterRow.each( function( i ) {
$( this ).find('fieldset').attr('data-index', i);
$( this ).find( ':input' ).each( function() {
var name = '';
if ( $( this ).attr( 'data-name' ) ) {
name = $( this ).attr( 'data-name' );
} else {
name = $( this ).prop( 'name' );
}

$( this ).attr(
'name',
name.replace(
new RegExp( /\[(.*?)\]/ ),
function() {
return '[' + i + ']';
}
)
);

if ( i > 0 ) {
var clone_id = '';
if ( $( this ).attr( 'data-clone' ) ) {
clone_id = $( this ).attr( 'data-clone' );
} else {
clone_id = $(this).prop( 'id' );
// Apply parentBase to the fieldset to make it available later
if (parentBase) {
$(fieldSet).attr('data-parent-base', parentBase);
}

var repeaterKey = self.getRepeaterKey(fieldSet);
self.resetInstance(repeaterKey);

repeaterRow.each(function (i) {
$(fieldSet).attr('data-index', i);
$(this)
.find('> fieldset > .field :input')
// Exclude sub repeater fields
.not($(this).find('> fieldset > .fieldset-wpum_field_group :input'))
Comment on lines +149 to +151

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

The selector $(this).find('> fieldset > .field :input') excludes inputs from nested repeaters using .not($(this).find('> fieldset > .fieldset-wpum_field_group :input')). However, this creates a new jQuery object inside .not() which could have performance implications if there are many nested repeaters. Consider caching the nested inputs selector or using a more efficient approach like filtering within a single find operation.

Copilot uses AI. Check for mistakes.
.each(function() {
var fieldName = $(this).attr('data-name') || $(this).prop('name');
fieldName = fieldName.replace(/\[(.*?)\]/, '[' + i + ']');

// Prepend parentBase if available and does not already have a parentBase
if (parentBase && !fieldName.includes(parentBase)) {
Comment on lines +156 to +157

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

The check !fieldName.includes(parentBase) on line 157 could produce incorrect results if the field name naturally contains the parentBase string as a substring in an unrelated context. For example, if parentBase is "user" and fieldName is "username[0][field]", this would incorrectly match. Consider using a more precise check, such as verifying if fieldName starts with parentBase followed by a bracket.

Suggested change
// Prepend parentBase if available and does not already have a parentBase
if (parentBase && !fieldName.includes(parentBase)) {
// Prepend parentBase if available and the fieldName is not already prefixed with it
if (parentBase && !fieldName.startsWith(parentBase + '[')) {

Copilot uses AI. Check for mistakes.
// Wrap the base key (before the first "[") in brackets
fieldName = fieldName.replace(/^([^[]+)/, '[$1]');
fieldName = parentBase + fieldName;
}

var id = clone_id + '_' + i;
$( this ).attr( 'id', id );
$( this ).closest( 'fieldset' ).find( 'label' ).attr( 'for', id );
}
} );
self.increaseInstance( name );
} );
$(this).attr(
'name',
fieldName
);

if (i > 0) {
var clone_id = $(this).attr('data-clone') || $(this).prop('id');
var id = clone_id + '_' + i;
$(this).attr('id', id);
$(this).closest('fieldset').find('label').attr('for', id);
}
});

self.increaseInstance(repeaterKey);

// Recurse into subrepeaters
$(this)
.find('> fieldset > .add-repeater-row')
.each(function () {
var fieldSet = $(this).closest('fieldset');
var currentParentBase = repeaterKey + '[' + i + ']';

self.setupInstances(fieldSet, currentParentBase);

Copilot AI Feb 26, 2026

Copy link

Choose a reason for hiding this comment

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

The recursive call to setupInstances on line 185 doesn't have any depth limit or circular reference check. While unlikely in normal usage, if there's a bug in the field configuration that creates circular parent-child relationships, this could cause infinite recursion and stack overflow. Consider adding a depth parameter or tracking visited fieldsets to prevent potential infinite loops.

Copilot uses AI. Check for mistakes.
self.validateMaxRows(fieldSet);
});
});
},
validateMaxRows: function( fieldSet ) {
var repeater = $(fieldSet).find( ' > .fieldset-wpum_field_group' ).not( '.fieldset-wpum_field_group-clone' );
var addBtn = $(fieldSet).find( ' > .add-repeater-row' );
var maxRows = addBtn.data('max-row');

validateMaxRows: function( name ) {
var parent = $( '.' + name );
var repeater = parent.find( '.fieldset-wpum_field_group' ).not( '.fieldset-wpum_field_group-clone' );
var addBtn = parent.find( '.add-repeater-row' );
var maxRows = addBtn.data( 'max-row' );
if ( !maxRows || parseInt( maxRows ) < 1 ) {
return true;
}
Expand Down
8 changes: 0 additions & 8 deletions src/fields-editor/settings/field-type-repeater.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,9 @@ export default {
this.labels.fields_create = this.labels.repeater_fields_create;
},
created(){
this.repeater = wpumFieldsEditor.fields_types.advanced.fields.find((field) => field.type === 'repeater')
if(this.repeater){
wpumFieldsEditor.fields_types.advanced.fields = wpumFieldsEditor.fields_types.advanced.fields.filter((field) => field.type !== 'repeater')
}

this.getFields()
},
destroyed(){
if(this.repeater){
wpumFieldsEditor.fields_types.advanced.fields.push(this.repeater)
}
this.labels.fields_add_new = this.clonedLabels.fields_add_new;
this.labels.fields_create = this.clonedLabels.fields_create;
}
Expand Down
13 changes: 13 additions & 0 deletions templates/forms/form-registration-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@

$field = $data->field;
$key = $data->key;

// Parent field should handle the child field rendering
if ( in_array( $field['type'], wpum_get_registered_parent_field_types(), true ) ) {

$field['key'] = $key;
$template = isset( $field['template'] ) ? $field['template'] : $field['type'];

WPUM()->templates
->set_template_data( $field )
->get_template_part( 'form-fields/' . $template, 'field' );
return;
}

if ( ! empty( $field['default_value'] ) ) {
$field['value'] = $field['default_value'];

Expand Down