- transfer User Tags conditional logic to extension, added JS hooks;

This commit is contained in:
nikitasinelnikov
2020-05-26 15:38:44 +03:00
parent 9da5cef5c5
commit 092b709d0f
5 changed files with 139 additions and 107 deletions
+122 -103
View File
@@ -6,11 +6,11 @@ var um_field_default_values = {};
* Get field default value
*
* @param {object} $dom
* @return {string}
* @return {object}
*/
function um_get_field_default_value( $dom ) {
var default_value = '';
var type = um_get_field_type($dom);
var type = um_get_field_type( $dom );
switch ( type ) {
case 'text':
@@ -18,35 +18,37 @@ function um_get_field_default_value( $dom ) {
case 'date':
case 'textarea':
case 'select':
default_value = $dom.find('input:text,input[type=number],textarea,select').val();
default_value = $dom.find('input:text,input[type="number"],textarea,select').val();
break;
case 'multiselect':
case 'user_tags':
default_value = $dom.find('select').val();
break;
case 'radio':
if ($dom.find('input[type=radio]:checked').length >= 1) {
default_value = $dom.find('input[type=radio]:checked').val();
if ( $dom.find('input[type="radio"]:checked').length >= 1 ) {
default_value = $dom.find('input[type="radio"]:checked').val();
}
break;
case 'checkbox':
if ($dom.find('input[type=checkbox]:checked').length >= 1) {
if ( $dom.find('input[type="checkbox"]:checked').length >= 1 ) {
if ($dom.find('input[type=checkbox]:checked').length > 1) {
if ( $dom.find('input[type="checkbox"]:checked').length > 1 ) {
var arr_values = [];
arr_values.push( default_value );
$dom.find('input[type=checkbox]:checked').each(function () {
$dom.find('input[type="checkbox"]:checked').each( function() {
arr_values.push( jQuery(this).val() );
});
default_value = arr_values;
} else {
default_value = $dom.find('input[type=checkbox]:checked').val();
default_value = $dom.find('input[type="checkbox"]:checked').val();
}
}
break;
default:
default_value = wp.hooks.applyFilters( 'um_conditional_logic_default_value', default_value, type, $dom );
break;
}
return {type: type, value: default_value};
@@ -60,7 +62,8 @@ function um_get_field_default_value( $dom ) {
*/
function um_get_field_element( $dom ) {
var type = um_get_field_type($dom);
var type = um_get_field_type( $dom );
var field_element = '';
switch ( type ) {
@@ -70,10 +73,12 @@ function um_get_field_element( $dom ) {
case 'textarea':
case 'select':
case 'multiselect':
case 'user_tags':
case 'radio':
case 'checkbox':
return $dom.find('input,textarea,select');
field_element = $dom.find( 'input,textarea,select' );
break;
default:
field_element = wp.hooks.applyFilters( 'um_conditional_logic_field_element', field_element, type, $dom );
break;
}
@@ -86,12 +91,13 @@ function um_get_field_element( $dom ) {
* @param {object} $dom
* @return {string}
*/
function um_get_field_type($dom) {
function um_get_field_type( $dom ) {
var type = '';
var classes = $dom.attr( 'class' ).split(' ');
jQuery.each( classes, function (i, d) {
if (/um-field-type_/.test(d)) {
type = d.replace('um-field-type_', '').trim();
jQuery.each( classes, function ( i, d ) {
if ( /um-field-type_/.test( d ) ) {
type = d.replace( 'um-field-type_', '' ).trim();
}
});
@@ -104,11 +110,11 @@ function um_get_field_type($dom) {
* @param {string} field_key
* @return {array}
*/
function um_get_field_children(field_key) {
function um_get_field_children( field_key ) {
var arr_conditions = [];
jQuery.each(arr_all_conditions, function (ii, condition) {
if (condition.field.parent === field_key) {
arr_conditions.push(condition.field.condition);
jQuery.each( arr_all_conditions, function ( ii, condition ) {
if ( condition.field.parent === field_key ) {
arr_conditions.push( condition.field.condition );
}
});
@@ -119,10 +125,10 @@ function um_get_field_children(field_key) {
* Split single array to multi-dimensional array
*
* @param {array} arr
* @param {nteger} n
* @param {integer} n
* @return {array}
*/
function um_splitup_array(arr, n) {
function um_splitup_array( arr, n ) {
var rest = arr.length % n,
restUsed = rest,
partLength = Math.floor(arr.length / n),
@@ -164,7 +170,7 @@ function um_splitup_array(arr, n) {
* @param {object} $dom
* @return {mixed}
*/
function um_get_field_data($dom) {
function um_get_field_data( $dom ) {
um_live_field = $dom.parents('.um-field').data('key');
um_live_value = $dom.val();
@@ -199,11 +205,11 @@ function um_get_field_data($dom) {
* @param {Boolean} strict
* @returns {Boolean}
*/
function um_in_array(needle, haystack, strict){
function um_in_array( needle, haystack, strict ) {
var found = false, key;
strict = !!strict;
for (key in haystack) {
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
for ( key in haystack ) {
if ( ( strict && haystack[ key ] === needle ) || ( ! strict && haystack[ key ] == needle ) ) {
found = true;
break;
}
@@ -235,113 +241,120 @@ function um_apply_conditions( $dom, is_single_update ) {
var $owners_values = {};
var $owner_conditions = {};
jQuery.each(conditions, function (index, condition) {
if (typeof $owners_values[condition.owner] == 'undefined') {
$owners_values[condition.owner] = [];
$owner_conditions[condition.owner] = {}
jQuery.each( conditions, function ( index, condition ) {
if ( typeof $owners_values[ condition.owner ] == 'undefined' ) {
$owners_values[ condition.owner ] = [];
$owner_conditions[ condition.owner ] = {}
}
$owners_values[condition.owner].push(condition.value);
$owner_conditions[condition.owner] = condition;
$owners_values[ condition.owner ].push( condition.value );
$owner_conditions[ condition.owner ] = condition;
});
jQuery.each(conditions, function (index, condition) {
if (typeof $owners[condition.owner] == 'undefined') {
$owners[condition.owner] = {};
jQuery.each( conditions, function ( index, condition ) {
if ( typeof $owners[ condition.owner ] == 'undefined' ) {
$owners[ condition.owner ] = {};
}
if (condition.operator === 'empty') {
var field_value = jQuery.isArray(live_field_value) ? live_field_value.join('') : live_field_value;
if (!field_value || field_value === '') {
$owners[condition.owner][index] = true;
if ( condition.operator === 'empty' ) {
var field_value = jQuery.isArray( live_field_value ) ? live_field_value.join('') : live_field_value;
if ( ! field_value || field_value === '' ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
}
if (condition.operator === 'not empty') {
var field_value = jQuery.isArray(live_field_value) ? live_field_value.join('') : live_field_value;
if (field_value && field_value !== '') {
$owners[condition.owner][index] = true;
if ( condition.operator === 'not empty' ) {
var field_value = jQuery.isArray( live_field_value ) ? live_field_value.join('') : live_field_value;
if ( field_value && field_value !== '' ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
}
if (condition.operator === 'equals to') {
var field_value = (jQuery.isArray(live_field_value) && live_field_value.length === 1) ? live_field_value[0] : live_field_value;
if (condition.value === field_value && um_in_array(field_value, $owners_values[condition.owner])) {
$owners[condition.owner][index] = true;
if ( condition.operator === 'equals to' ) {
var field_value = ( jQuery.isArray( live_field_value ) && live_field_value.length === 1 ) ? live_field_value[0] : live_field_value;
if ( condition.value === field_value && um_in_array( field_value, $owners_values[ condition.owner ] ) ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
}
if (condition.operator === 'not equals') {
var field_value = (jQuery.isArray(live_field_value) && live_field_value.length === 1) ? live_field_value[0] : live_field_value;
if (jQuery.isNumeric(condition.value) && parseInt(field_value) !== parseInt(condition.value) && field_value && !um_in_array(field_value, $owners_values[condition.owner])) {
$owners[condition.owner][index] = true;
} else if (condition.value != field_value && !um_in_array(field_value, $owners_values[condition.owner])) {
$owners[condition.owner][index] = true;
if ( condition.operator === 'not equals' ) {
var field_value = ( jQuery.isArray( live_field_value ) && live_field_value.length === 1 ) ? live_field_value[0] : live_field_value;
if ( jQuery.isNumeric(condition.value) && parseInt(field_value) !== parseInt( condition.value ) && field_value && ! um_in_array( field_value, $owners_values[ condition.owner ] ) ) {
$owners[ condition.owner ][ index ] = true;
} else if ( condition.value != field_value && ! um_in_array( field_value, $owners_values[ condition.owner ] ) ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
}
if (condition.operator === 'greater than') {
var field_value = (jQuery.isArray(live_field_value) && live_field_value.length === 1) ? live_field_value[0] : live_field_value;
if (jQuery.isNumeric(condition.value) && parseInt(field_value) > parseInt(condition.value)) {
$owners[condition.owner][index] = true;
if ( condition.operator === 'greater than' ) {
var field_value = ( jQuery.isArray( live_field_value ) && live_field_value.length === 1 ) ? live_field_value[0] : live_field_value;
if ( jQuery.isNumeric( condition.value ) && parseInt( field_value ) > parseInt( condition.value ) ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
}
if (condition.operator === 'less than') {
var field_value = (jQuery.isArray(live_field_value) && live_field_value.length === 1) ? live_field_value[0] : live_field_value;
if (jQuery.isNumeric(condition.value) && parseInt(field_value) < parseInt(condition.value)) {
$owners[condition.owner][index] = true;
if ( condition.operator === 'less than' ) {
var field_value = ( jQuery.isArray( live_field_value ) && live_field_value.length === 1 ) ? live_field_value[0] : live_field_value;
if ( jQuery.isNumeric( condition.value ) && parseInt( field_value ) < parseInt( condition.value ) ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
}
if ( condition.operator === 'contains' ) {
switch (field_type) {
switch ( field_type ) {
case 'multiselect':
case 'user_tags':
if ( live_field_value && live_field_value.indexOf( condition.value ) >= 0 && um_in_array( condition.value, live_field_value ) ) {
$owners[condition.owner][index] = true;
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
break;
case 'checkbox':
if ( live_field_value && live_field_value.indexOf( condition.value ) >= 0 ) {
$owners[condition.owner][index] = true;
$owners[ condition.owner ][ index ] = true;
} else {
$owners[condition.owner][index] = false;
$owners[ condition.owner ][ index ] = false;
}
break;
default:
if ( live_field_value && live_field_value.indexOf( condition.value ) >= 0 && um_in_array( live_field_value, $owners_values[ condition.owner ] ) ) {
$owners[condition.owner][index] = true;
} else {
$owners[condition.owner][index] = false;
$owners = wp.hooks.applyFilters( 'um_conditional_logic_contains_operator_owners', $owners, field_type, live_field_value, condition, index );
if ( typeof $owners[ condition.owner ][ index ] == 'undefined' ) {
if ( live_field_value && live_field_value.indexOf( condition.value ) >= 0 && um_in_array( live_field_value, $owners_values[ condition.owner ] ) ) {
$owners[ condition.owner ][ index ] = true;
} else {
$owners[ condition.owner ][ index ] = false;
}
}
break;
}
}
}); // end foreach `conditions`
jQuery.each($owners, function (index, field) {
if (um_in_array(true, field)) {
um_field_apply_action($dom, $owner_conditions[index], true);
jQuery.each( $owners, function ( index, field ) {
if ( um_in_array( true, field ) ) {
um_field_apply_action( $dom, $owner_conditions[ index ], true );
} else {
um_field_apply_action($dom, $owner_conditions[index], false);
um_field_apply_action( $dom, $owner_conditions[ index ], false );
}
});
$dom.trigger('um_fields_change');
$dom.trigger( 'um_fields_change' );
}
@@ -356,31 +369,32 @@ function um_apply_conditions( $dom, is_single_update ) {
function um_field_apply_action($dom, condition, is_true) {
var child_dom = jQuery('div.um-field[data-key="' + condition.owner + '"]');
if (condition.action === 'show' && is_true /*&& child_dom.is(':hidden')*/) {
if ( condition.action === 'show' && is_true /*&& child_dom.is(':hidden')*/ ) {
child_dom.show();
_show_in_ie( child_dom );
um_field_restore_default_value(child_dom);
}
if (condition.action === 'show' && !is_true /*&& child_dom.is(':visible') */) {
if ( condition.action === 'show' && ! is_true /*&& child_dom.is(':visible') */ ) {
child_dom.hide();
_hide_in_ie( child_dom );
}
if (condition.action === 'hide' && is_true /*&& child_dom.is(':visible')*/) {
if ( condition.action === 'hide' && is_true /*&& child_dom.is(':visible')*/ ) {
child_dom.hide();
_hide_in_ie( child_dom );
}
if (condition.action === 'hide' && !is_true /*&& child_dom.is(':hidden')*/) {
if ( condition.action === 'hide' && ! is_true /*&& child_dom.is(':hidden')*/ ) {
child_dom.show();
_show_in_ie( child_dom );
um_field_restore_default_value( child_dom );
}
return $dom.removeClass('um-field-has-changed');
return $dom.removeClass( 'um-field-has-changed' );
}
/**
* Restores default field value
*
@@ -390,14 +404,15 @@ function um_field_restore_default_value( $dom ) {
var type = um_get_field_type( $dom );
var key = $dom.data('key');
var field = um_field_default_values[key];
var field = um_field_default_values[ key ];
switch ( type ) {
case 'text':
case 'number':
case 'date':
case 'textarea':
$dom.find('input:text,input[type=number],textareas').val(field.value);
$dom.find('input:text,input[type="number"],textareas').val(field.value);
break;
case 'select':
@@ -407,7 +422,6 @@ function um_field_restore_default_value( $dom ) {
break;
case 'multiselect':
case 'user_tags':
$dom.find('select').find('option').prop('selected', false);
jQuery.each(field.value, function (i, value) {
$dom.find('select').find('option[value="' + value + '"]').attr('selected', true);
@@ -416,52 +430,57 @@ function um_field_restore_default_value( $dom ) {
break;
case 'checkbox':
if ( $dom.find('input[type=checkbox]:checked').length >= 1 ) {
if ( $dom.find('input[type="checkbox"]:checked').length >= 1 ) {
$dom.find('input[type=checkbox]:checked').removeAttr('checked');
$dom.find('input[type="checkbox"]:checked').removeAttr('checked');
$dom.find('span.um-field-checkbox-state i').removeClass('um-icon-android-checkbox-outline');
$dom.find('span.um-field-checkbox-state i').addClass('um-icon-android-checkbox-outline-blank');
$dom.find('.um-field-checkbox.active').removeClass('active');
if (jQuery.isArray(field.value)) {
jQuery.each(field.value, function (i, value) {
var cbox_elem = $dom.find('input[type=checkbox][value="' + value + '"]');
if ( jQuery.isArray( field.value ) ) {
jQuery.each( field.value, function ( i, value ) {
var cbox_elem = $dom.find('input[type="checkbox"][value="' + value + '"]');
cbox_elem.attr('checked', true);
cbox_elem.closest('.um-field-checkbox').find('i').removeClass('um-icon-android-checkbox-outline-blank');
cbox_elem.closest('.um-field-checkbox').find('i').addClass('um-icon-android-checkbox-outline');
cbox_elem.closest('.um-field-checkbox').addClass('active');
});
} else {
var cbox_elem = $dom.find('input[type=checkbox][value="' + field.value + '"]');
var cbox_elem = $dom.find('input[type="checkbox"][value="' + field.value + '"]');
cbox_elem.attr('checked', true);
cbox_elem.closest('.um-field-checkbox').find('i').removeClass('um-icon-android-checkbox-outline-blank');
cbox_elem.closest('.um-field-checkbox').find('i').addClass('um-icon-android-checkbox-outline');
cbox_elem.closest('.um-field-checkbox').addClass('active');
}
}
}
break;
case 'radio':
if ( $dom.find('input[type=radio]:checked').length >= 1 ) {
setTimeout(function () {
if ( $dom.find('input[type="radio"]:checked').length >= 1 ) {
$dom.find('input[type=radio]:checked').removeAttr('checked');
setTimeout( function() {
$dom.find('input[type="radio"]:checked').removeAttr('checked');
$dom.find('span.um-field-radio-state i').removeClass('um-icon-android-radio-button-on');
$dom.find('span.um-field-radio-state i').addClass('um-icon-android-radio-button-off');
$dom.find('.um-field-radio.active').removeClass('active');
var radio_elem = $dom.find("input[type=radio][value='" + field.value + "']");
var radio_elem = $dom.find('input[type="radio"][value="' + field.value + '"]');
radio_elem.attr('checked', true);
radio_elem.closest('.um-field-radio').find('i').removeClass('um-icon-android-radio-button-off');
radio_elem.closest('.um-field-radio').find('i').addClass('um-icon-android-radio-button-on');
radio_elem.closest('.um-field-radio').addClass('active');
}, 100);
}, 100 );
}
break;
default:
wp.hooks.doAction( 'um_conditional_logic_restore_default_value', type, $dom, field );
break;
} // end switch type
+1 -1
View File
File diff suppressed because one or more lines are too long
+13 -2
View File
@@ -1362,6 +1362,11 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
}
/**
* @param $field_data
*
* @return string
*/
function render_md_default_filters( $field_data ) {
if ( empty( $field_data['id'] ) ) {
return false;
@@ -1445,6 +1450,11 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
}
/**
* @param $field_data
*
* @return string
*/
function render_md_sorting_fields( $field_data ) {
if ( empty( $field_data['id'] ) ) {
return false;
@@ -1560,6 +1570,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
return $html;
}
/**
* Get field value
*
@@ -1576,8 +1587,8 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
$default = is_array( $field_data['default'] ) ? $field_data['default'] : array( $field_data['default'] );
}
}
if ( isset( $field_data['default' . $i] ) ) {
$default = $field_data['default' . $i];
if ( isset( $field_data[ 'default' . $i ] ) ) {
$default = $field_data[ 'default' . $i ];
}
if ( $field_data['type'] == 'checkbox' || $field_data['type'] == 'multi_checkbox' ) {
+1 -1
View File
@@ -127,7 +127,7 @@ if ( ! class_exists( 'um\core\Enqueue' ) ) {
wp_register_script( 'um_responsive', $this->js_baseurl . 'um-responsive' . $this->suffix . '.js', array( 'jquery', 'um_functions', 'um_crop' ), ultimatemember_version, true );
wp_register_script( 'um-gdpr', $this->js_baseurl . 'um-gdpr' . $this->suffix . '.js', array( 'jquery' ), ultimatemember_version, false );
wp_register_script('um_conditional', $this->js_baseurl . 'um-conditional' . $this->suffix . '.js', array( 'jquery' ), ultimatemember_version, true );
wp_register_script('um_conditional', $this->js_baseurl . 'um-conditional' . $this->suffix . '.js', array( 'jquery', 'wp-hooks' ), ultimatemember_version, true );
wp_register_script('um_scripts', $this->js_baseurl . 'um-scripts' . $this->suffix . '.js', array( 'jquery', 'wp-util', 'um_conditional', 'um_datetime', 'um_datetime_date', 'um_datetime_time', 'um_datetime_legacy', 'select2' ), ultimatemember_version, true );
/**
* UM hook
+2
View File
@@ -153,6 +153,7 @@ The plugin works with popular caching plugins by automatically excluding Ultimat
- Added layout changes to show email notification description in Settings > Email screen
- Added member directory option 'Exclude specific users'
- Added filter for the changing redirect after profile edited 'um_update_profile_redirect_after'
- Added JS filters for conditional logic
- Tweak: apply_shortcodes() function support
* Bugfixes:
@@ -165,6 +166,7 @@ The plugin works with popular caching plugins by automatically excluding Ultimat
- Fixed Erase User Data field on the Account page
- Fixed SkypeID field
- Fixed clickable links in the UM forms which are displayed in a modal window
- Fixed disabling select and textarea fields in inactive tabs on Account
- Fixed some translations
- Small PHP notices fixes