Last active
January 9, 2020 20:10
-
-
Save wpmudev-sls/c2f86612eb779f014b94388aa37fa222 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Custom calculations and mix the checkboxes with radios
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] - Custom calculations and mix the checkboxes with radios | |
* Description: [Forminator Pro] - Custom calculations and mix the checkboxes with radios - 1155690115716017 | |
* 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_custom_calc_and_mix_checkboxes_radios_func', 100 ); | |
function wpmudev_forminator_custom_calc_and_mix_checkboxes_radios_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_FM_Calc_Mix_Checkboxes_Radios{ | |
public function __construct(){ | |
add_action( 'wp_head', array( $this, 'custom_css'), 10, 21 ); | |
add_action( 'forminator_before_wrapper_markup', array( $this, 'add_custom_class_for_hidden_field' ), 10, 2 ); | |
add_action( 'wp_footer', array( $this, 'custom_calc_and_mix_checkboxes_radios' ), 21 ); | |
} | |
public function custom_css(){ | |
echo '<style> | |
.wpmudev-hidden{ | |
display:none; | |
} | |
</style>'; | |
} | |
public function add_custom_class_for_hidden_field( $html, $wrapper ){ | |
// Check if the field type is hidden | |
if ( isset( $wrapper['fields'] ) && "hidden" === $wrapper['fields'][0]['type'] ) { | |
if( ! empty( $wrapper['fields'][0]['custom-class'] ) ){ | |
$html = str_replace( 'class="', 'class="'. $wrapper['fields'][0]['custom-class'] .' ', $html ); | |
} | |
} | |
return $html; | |
} | |
public function custom_calc_and_mix_checkboxes_radios(){ | |
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 && _form.hasClass('forminator-custom-form') ){ | |
let _checkbox_radios = _form.find('.wpmudev-checkbox-radio'); | |
if( _checkbox_radios.length ){ | |
_checkbox_radios.each( function(){ | |
let _that = $(this), | |
_id = _that.attr('id'), | |
_radios = _form.find('.radio-'+ _id), | |
_number = _form.find('.no-checked-'+ _id); | |
if( _radios.length && _number.length ){ | |
// hide these fields | |
_radios.parent().hide(); | |
_number.parent().hide(); | |
_that.find('.forminator-checkbox').each(function(i, v){ | |
if( _radios.find('.forminator-radio').first().length ){ | |
$(this).after(_radios.find('.forminator-radio').first().hide().detach()); | |
} | |
}); | |
} | |
} ).on('change',':checkbox', function(){ | |
let _parent = $(this).closest('.wpmudev-checkbox-radio'), | |
_number = _form.find('.no-checked-'+ _parent.attr('id')); | |
if( $(this).is(':checked') ){ | |
$(this).parent().next('label').slideDown(); | |
}else{ | |
$(this).parent().next('label').slideUp(); | |
} | |
if( _number.length ){ | |
_number.find('input').val( _parent.find(':checkbox:checked').length ).trigger('change'); | |
} | |
}); | |
} | |
} | |
}); | |
} | |
}); | |
}(window.jQuery, document, window))</script>"; | |
} | |
} | |
$run = new WPMUDEV_FM_Calc_Mix_Checkboxes_Radios; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment