Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active March 7, 2020 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/bb40edeb76f1d51f59debb1a01f4604a to your computer and use it in GitHub Desktop.
Save wpmudev-sls/bb40edeb76f1d51f59debb1a01f4604a to your computer and use it in GitHub Desktop.
Custom formula field (Square root)
<?php
/**
* Plugin Name: [Forminator Pro] - Custom formula field
* Description: [Forminator Pro] - Custom formula field (Square root) - 1156977754278478
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; }
add_action( 'after_setup_theme', 'wpmudev_forminator_custom_formula_field_func', 100 );
function wpmudev_forminator_custom_formula_field_func() {
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) {
class WPMUDEV_FM_Custom_Square_Field{
public function __construct(){
add_action( 'wp_footer', array( $this, 'custom_formula_field' ), 21 );
add_action( 'wp_head', array( $this, 'custom_css'), 10, 21 );
add_filter( 'forminator_field_create_input', array( $this, 'allow_float_number_field' ), 10, 2 );
// add_action( 'forminator_cform_post_message', array( $this, 'trigger_hidden_filter' ) );
}
public function custom_css(){
echo '<style>
.wpmudev-hidden{
display:none;
}
</style>';
}
public function trigger_hidden_filter(){
add_action( 'forminator_before_wrapper_markup', array( $this, 'add_custom_class_for_hidden_field' ), 10, 2 );
}
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 allow_float_number_field( $html, $attr ){
if( ! empty( $attr['pattern'] ) && '[0-9]*' === $attr['pattern'] ){
if( ( isset( $attr['min'] ) && is_float( $attr['min'] + 0 ) ) || ( isset( $attr['max'] ) && is_float( $attr['max'] + 0 ) ) ){
$attr['pattern'] = '[+-]?([0-9]*[.])?[0-9]+';
$html = str_replace('pattern="[0-9]*"', '', $html);
}
}
return $html;
}
public function custom_formula_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 && _form.hasClass('forminator-custom-form') ){
let _sqrt_field = _form.find('.wpmudev-sqrt-field');
if( _sqrt_field.length ){
_form.find('.wpmudev-hidden').parent().hide();
_sqrt_field.find('input,select').on('change', function(){
let _parent = $(this).closest('.wpmudev-sqrt-field'),
_number = _form.find('.wpmudev-'+ _parent.attr('id'));
if( _number.length ){
var _val = $(this).data('calculation');
if( undefined === _val ){
_val = $(this).val();
}
_val = Math.sqrt( _val );
if( _val % 1 !== 0 ){
_val = _val.toFixed(2);
}
_number.find('input').val( _val ).trigger('change');
}
});
}
}
});
}
});
}(window.jQuery, document, window))</script>";
}
}
$run = new WPMUDEV_FM_Custom_Square_Field;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment