Skip to content

Commit e1bb758

Browse files
committed
Conditional logic for fields moved to core plugin to support both Custom Fields and ACF addons
1 parent d47c0bf commit e1bb758

File tree

4 files changed

+238
-5
lines changed

4 files changed

+238
-5
lines changed

assets/js/src/wp-user-manager.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,107 @@ jQuery( function( $ ) {
158158
}
159159
}
160160

161+
$.wpumConditionalFields = function( element, options ){
162+
163+
var form = $(element),
164+
self = this;
165+
166+
this.init = function(){
167+
168+
this.validateFields();
169+
170+
form.find(':input').on( 'input change', function(){
171+
self.validateFields( $(this).parents('fieldset') );
172+
});
173+
}
174+
175+
this.validateField = function(element){
176+
var rules = element.data('condition');
177+
element.toggle( this.validateRules(rules) );
178+
}
179+
180+
this.validateFields = function(){
181+
form.find('fieldset[data-condition]').each(function(){
182+
var rules = $(this).data('condition');
183+
$(this).toggle( self.validateRules(rules) );
184+
});
185+
}
186+
187+
this.validateRules = function(rules){
188+
return rules.some(function(andRules){
189+
return andRules.every(self.validateRule);
190+
})
191+
}
192+
193+
this.validateRule = function(rule){
194+
return self.hasOwnProperty(self.ruleMethodName(rule.condition)) ? self[self.ruleMethodName(rule.condition)](rule) : false;
195+
}
196+
197+
this.ruleMethodName = function(rule){
198+
return rule.replace(/([-_][a-z])/ig, function($1){
199+
return $1.toUpperCase()
200+
.replace('-', '')
201+
.replace('_', '');
202+
});
203+
}
204+
205+
this.getValue = function(rule){
206+
var el = $('[name^="'+rule.field+'"]');
207+
if( el.length ){
208+
if( el.is('[type="radio"]') ){
209+
return el.filter(':checked').val();
210+
}else if( el.is('[type="checkbox"]') ){
211+
return el.filter(':checked').map(function(){
212+
return $(this).val();
213+
}).toArray();
214+
}else{
215+
return el.first().val();
216+
}
217+
}
218+
}
219+
220+
this.hasValue = function(rule){
221+
var value = this.getValue(rule);
222+
return $.isArray(value) ? value.length : value && $.trim(value) !== '';
223+
}
224+
225+
this.hasNoValue = function(rule){
226+
var value = this.getValue(rule);
227+
return $.isArray(value) ? !value.length : !value || value === '';
228+
}
229+
230+
this.valueContains = function(rule){
231+
var value = this.getValue(rule);
232+
return $.isArray(value) ? value.includes(rule.value) : value && value.toLowerCase().indexOf(rule.value.toLowerCase()) > -1;
233+
}
234+
235+
this.valueEquals = function(rule){
236+
var value = this.getValue(rule);
237+
return $.isArray(value) ? value.includes(rule.value) : value && value.toLowerCase() === rule.value.toLowerCase();
238+
}
239+
240+
this.valueNotEquals = function(rule){
241+
var value = this.getValue(rule);
242+
return $.isArray(value) ? !value.includes(rule.value) : value && value.toLowerCase() !== rule.value.toLowerCase();
243+
}
244+
245+
this.valueGreater = function(rule){
246+
var value = this.getValue(rule);
247+
return parseFloat(value) > parseFloat(rule.value);
248+
}
249+
250+
this.valueLess = function(rule){
251+
var value = this.getValue(rule);
252+
return parseFloat(value) < parseFloat(rule.value);
253+
}
254+
255+
this.init();
256+
}
257+
258+
$.fn.wpumConditionalFields = function( options ) {
259+
new $.wpumConditionalFields(this, options);
260+
};
261+
161262

162263
$( document ).ready( function() {
163264
$( document.body ).on( 'click', '.wpum-remove-uploaded-file', function() {
@@ -166,6 +267,8 @@ jQuery( function( $ ) {
166267
} );
167268

168269

270+
$('.wpum-registration-form, .wpum-account-form, .wpum-custom-account-form').wpumConditionalFields({});
271+
169272
repeater.init();
170273
initFields();
171274
} );

assets/js/wp-user-manager.js

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*! WP User Manager - v2.5
1+
/*! WP User Manager - v2.8.5
22
* https://wpusermanager.com
3-
* Copyright (c) 2021; * Licensed GPLv2+ */
3+
* Copyright (c) 2022; * Licensed GPLv2+ */
44
jQuery( function( $ ) {
55
function initFields() {
66
$( '.wpum-multiselect:not(.wpum-clone-field)' ).each( function() {
@@ -161,6 +161,107 @@ jQuery( function( $ ) {
161161
}
162162
}
163163

164+
$.wpumConditionalFields = function( element, options ){
165+
166+
var form = $(element),
167+
self = this;
168+
169+
this.init = function(){
170+
171+
this.validateFields();
172+
173+
form.find(':input').on( 'input change', function(){
174+
self.validateFields( $(this).parents('fieldset') );
175+
});
176+
}
177+
178+
this.validateField = function(element){
179+
var rules = element.data('condition');
180+
element.toggle( this.validateRules(rules) );
181+
}
182+
183+
this.validateFields = function(){
184+
form.find('fieldset[data-condition]').each(function(){
185+
var rules = $(this).data('condition');
186+
$(this).toggle( self.validateRules(rules) );
187+
});
188+
}
189+
190+
this.validateRules = function(rules){
191+
return rules.some(function(andRules){
192+
return andRules.every(self.validateRule);
193+
})
194+
}
195+
196+
this.validateRule = function(rule){
197+
return self.hasOwnProperty(self.ruleMethodName(rule.condition)) ? self[self.ruleMethodName(rule.condition)](rule) : false;
198+
}
199+
200+
this.ruleMethodName = function(rule){
201+
return rule.replace(/([-_][a-z])/ig, function($1){
202+
return $1.toUpperCase()
203+
.replace('-', '')
204+
.replace('_', '');
205+
});
206+
}
207+
208+
this.getValue = function(rule){
209+
var el = $('[name^="'+rule.field+'"]');
210+
if( el.length ){
211+
if( el.is('[type="radio"]') ){
212+
return el.filter(':checked').val();
213+
}else if( el.is('[type="checkbox"]') ){
214+
return el.filter(':checked').map(function(){
215+
return $(this).val();
216+
}).toArray();
217+
}else{
218+
return el.first().val();
219+
}
220+
}
221+
}
222+
223+
this.hasValue = function(rule){
224+
var value = this.getValue(rule);
225+
return $.isArray(value) ? value.length : value && $.trim(value) !== '';
226+
}
227+
228+
this.hasNoValue = function(rule){
229+
var value = this.getValue(rule);
230+
return $.isArray(value) ? !value.length : !value || value === '';
231+
}
232+
233+
this.valueContains = function(rule){
234+
var value = this.getValue(rule);
235+
return $.isArray(value) ? value.includes(rule.value) : value && value.toLowerCase().indexOf(rule.value.toLowerCase()) > -1;
236+
}
237+
238+
this.valueEquals = function(rule){
239+
var value = this.getValue(rule);
240+
return $.isArray(value) ? value.includes(rule.value) : value && value.toLowerCase() === rule.value.toLowerCase();
241+
}
242+
243+
this.valueNotEquals = function(rule){
244+
var value = this.getValue(rule);
245+
return $.isArray(value) ? !value.includes(rule.value) : value && value.toLowerCase() !== rule.value.toLowerCase();
246+
}
247+
248+
this.valueGreater = function(rule){
249+
var value = this.getValue(rule);
250+
return parseFloat(value) > parseFloat(rule.value);
251+
}
252+
253+
this.valueLess = function(rule){
254+
var value = this.getValue(rule);
255+
return parseFloat(value) < parseFloat(rule.value);
256+
}
257+
258+
this.init();
259+
}
260+
261+
$.fn.wpumConditionalFields = function( options ) {
262+
new $.wpumConditionalFields(this, options);
263+
};
264+
164265

165266
$( document ).ready( function() {
166267
$( document.body ).on( 'click', '.wpum-remove-uploaded-file', function() {
@@ -169,6 +270,8 @@ jQuery( function( $ ) {
169270
} );
170271

171272

273+
$('.wpum-registration-form, .wpum-account-form, .wpum-custom-account-form').wpumConditionalFields({});
274+
172275
repeater.init();
173276
initFields();
174277
} );

assets/js/wp-user-manager.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/actions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,30 @@ function wpum_flush_user_object_cache( $form, $values, $updated_user_id ) {
576576
add_action( 'wpum_after_custom_user_update', 'wpum_flush_user_object_cache', 100, 3 );
577577
add_action( 'wpum_after_user_update', 'wpum_flush_user_object_cache', 100, 3 );
578578

579+
580+
function wpum_field_conditional_logic_rules( $data ) {
581+
$rulesets = apply_filters( 'wpum_field_conditional_logic_rules', array(), $data );
582+
583+
if ( empty( $rulesets ) ) {
584+
return;
585+
}
586+
?>
587+
<script type="text/javascript">
588+
(function() {
589+
var ruleset = <?php echo json_encode( $rulesets ) ?>;
590+
Object.keys( ruleset ).forEach( function( fieldName ) {
591+
var field = document.querySelector( '.fieldset-' + fieldName );
592+
if ( field ) {
593+
field.style.display = 'none';
594+
field.dataset.condition = JSON.stringify( ruleset[ fieldName ] );
595+
}
596+
} );
597+
})();
598+
</script>
599+
<?php
600+
}
601+
602+
add_action( 'wpum_after_registration_form', 'wpum_field_conditional_logic_rules', 1 );
603+
add_action( 'wpum_after_account_form', 'wpum_field_conditional_logic_rules', 1 );
604+
add_action( 'wpum_after_custom_account_form', 'wpum_field_conditional_logic_rules', 1 );
605+

0 commit comments

Comments
 (0)