Last active
January 9, 2020 20:10
-
-
Save wpmudev-sls/79d0ce412a0d616ec5c43adf88766803 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Support conditional relative select fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Support conditional relative select fields | |
* Description: [Forminator Pro] - Support conditional relative select fields - the selected option on select field A will not available on select field B - 1155684028615381 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( 'after_setup_theme', 'wpmudev_forminator_support_conditional_relative_select_field_func', 100 ); | |
function wpmudev_forminator_support_conditional_relative_select_field_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
add_action( 'wp_footer', 'wpmudev_forminator_support_conditional_relative_select_field', 21 ); | |
function wpmudev_forminator_support_conditional_relative_select_field(){ | |
echo "<script type='text/javascript'>(function ($, document, window) { | |
'use strict'; | |
$(function () { | |
if (typeof ($.fn.forminatorLoader) === 'undefined') { | |
// nothing to do here | |
} else { | |
$(document).on('after.load.forminator', function(e, form_id){ | |
let _form = $('#forminator-module-'+ form_id); | |
if( _form.length ){ | |
let _relative_select_field_wrappers = _form.find('.wpmudev-relative-select-field'); | |
if( _relative_select_field_wrappers.length ){ | |
let _main_selects = {}; | |
_relative_select_field_wrappers.each( function(){ | |
let _id = $(this).attr('id') +'-field', | |
_that = $(this), | |
_stop_reverse = ! _that.hasClass('wpmudev-reversing'); //enable this to stop change on reverse field | |
if( _id ){ | |
let _oppo_select = _form.find('.wpmudev-relative-select-field.'+ _id).find('select'); | |
_form.find('#'+ _id).data('wpmudev-opposite', _oppo_select ); | |
if( _stop_reverse && ! ( _id in _main_selects ) ){ | |
_main_selects[ _oppo_select.attr('id') ] = 1; | |
} | |
} | |
_that.on('change', 'select', function(){ | |
if( _stop_reverse && $(this).attr('id') in _main_selects ){ | |
return; | |
} | |
let _oppo_select = $(this).data('wpmudev-opposite'); | |
if( _oppo_select.length ){ | |
var _disabled_option = _oppo_select.find('option[value='+ $(this).val() +']'); | |
if( _disabled_option.length ){ | |
let _select_list_wrapper = _oppo_select.next('.forminator-select-list'); | |
if( _select_list_wrapper.length ){ | |
if( _disabled_option.text() === _select_list_wrapper.children('.forminator-value').text() ){ | |
let _text = ''; | |
if( _disabled_option.next('option').length ){ | |
_text = _disabled_option.next('option').text(); | |
}else if( _disabled_option.prev('option').length ){ | |
_text = _disabled_option.prev('option').text(); | |
} | |
_select_list_wrapper.children('.forminator-value').text( _text ); | |
} | |
_select_list_wrapper.find('.forminator-dropdown-list').find('li').eq(_disabled_option.index()).removeClass('current').hide().addClass('disabled').siblings('.disabled').show(); | |
} | |
} | |
} | |
}); | |
}); | |
} | |
} | |
}); | |
} | |
}); | |
}(window.jQuery, document, window))</script>"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment