adczk

Forminator - limit chekbox selected at once

Jul 28th, 2022
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: [Forminator Pro] - Restrict select the multiple field(s).
  4.  * Description: [Forminator Pro] - Restrict select the multiple field(s) (select/checkbox).
  5.  * Author: Thobk @ WPMUDEV
  6.  * Jira: SLS-918
  7.  * Author URI: https://premium.wpmudev.org
  8.  * License: GPLv2 or later
  9.  */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11.     exit;
  12. } elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
  13.     return;
  14. }
  15. /**
  16.  * 1. Add custom class wpmudev-option-limit inside STYLING tab of a multiple field's settings: https://share.getcloudapp.com/KoulYdY9
  17.  * 2. Enter the form id(s) in the snippet code bellow that you want to apply this custom code: form_ids: [123, 456]
  18.  * 3. Modify the limit for each MU field:
  19.  * limit: {
  20.  * 'checkbox-2': 5,//[field-id]:[limit]
  21.  * 'select-1': 2
  22.  * }
  23.  */
  24. add_action( 'wp_footer', function(){
  25.  
  26.     global $post;
  27.  
  28.     if( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
  29.         return;
  30.     }
  31.  
  32.     ?>
  33.     <style>
  34.         .forminator-ui .wpmudev-option-limit .wpmudev-disabled{
  35.             color:#ddd!important;
  36.         }
  37.         .forminator-ui .wpmudev-option-limit .wpmudev-disabled span[aria-hidden]{
  38.             border-color: #ddd!important;
  39.         background-color: #ddd!important;
  40.         }
  41.     </style>
  42.     <script type="text/javascript">
  43.        
  44.         ($=>{
  45.  
  46.             const _forminator_restrict_multiple_fields = {
  47.                 form_ids: [791, 9034],
  48.                 limit: {
  49.                     'checkbox-2': 5,//[field-id]:[limit]
  50.                     'select-1': 1
  51.                 },
  52.                 run : function( e, form_id ) {
  53.                     if( _forminator_restrict_multiple_fields.form_ids.indexOf( form_id ) === -1 ){
  54.                         return;
  55.                     }
  56.                     let _form = $( "#forminator-module-" + form_id );
  57.  
  58.                     _form.find('.wpmudev-option-limit').each(function(){
  59.                         let _field = $(this),
  60.                                 checkbox_fields = _field.find( ":checkbox" );
  61.                         if( checkbox_fields.length ){
  62.                             checkbox_fields.on('change', function (e) {
  63.                                 let _parent = $(this).closest('.wpmudev-option-limit'),
  64.                                         _parent_id = _parent.attr('id'),
  65.                                         _selected = _parent.find(':checkbox:checked').length;
  66.                                 if( _parent_id in _forminator_restrict_multiple_fields.limit && _selected >= _forminator_restrict_multiple_fields.limit[ _parent_id ]){
  67.  
  68.                                     // save latest value.
  69.                                     _field.data('latest_value', $(this).val() );
  70.                                     // disable other options.
  71.                                     _parent.find(':checkbox:not(:checked)').each(function(){
  72.                                         $(this).prop('disabled', true).parent().addClass('wpmudev-disabled');
  73.                                     });
  74.                                 }else{
  75.                                     _parent.find(':checkbox:disabled').each(function(){
  76.                                         $(this).prop('disabled', false).parent().removeClass('wpmudev-disabled');
  77.                                     });
  78.  
  79.                                     _field.removeData('latest_value');
  80.                                 }
  81.                             });
  82.                         }
  83.  
  84.                         // auto remove previous value when riched the limit.
  85.                         $(this).on('click', '.wpmudev-disabled', function(){
  86.                             let _latest_value = _field.data('latest_value') ;
  87.                             if( _latest_value ){
  88.                                 let _previous_opt = $(this).closest('.wpmudev-option-limit').find('input[value="'+ _latest_value +'"');
  89.                                 if( _previous_opt.length ){
  90.                                     _previous_opt.trigger('click');
  91.                                     $(this).removeClass('wpmudev-disabled').find('input:disabled').removeAttr('disabled');
  92.                                 }
  93.                             }
  94.                         })
  95.                     });
  96.                 }
  97.             }
  98.  
  99.             $(document).ready(function(){
  100.                 $.each(_forminator_restrict_multiple_fields.form_ids, function(i, _form_id){
  101.                     _forminator_restrict_multiple_fields.run(this,_form_id);
  102.                 });
  103.  
  104.                 $(document).on('after.load.forminator', function(e, form_id) {
  105.                     $.each(_forminator_restrict_multiple_fields.form_ids, function(i, _form_id){
  106.                         _forminator_restrict_multiple_fields.run(this,_form_id);
  107.                     });
  108.                 });
  109.  
  110.                 $(document).on( 'response.success.load.forminator', _forminator_restrict_multiple_fields.run );
  111.             });
  112.         })(jQuery);
  113.  
  114.     </script>
  115.  
  116.     <?php
  117. }, 9999 );
Advertisement
Add Comment
Please, Sign In to add comment