Last active
July 12, 2024 15:09
-
-
Save Crocoblock/2e9b8c7c86a627b60127251abbf0a951 to your computer and use it in GitHub Desktop.
JetFormBuilder currency mask
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 | |
class JFB_Currency_Mask { | |
private $processed = false; | |
public function __construct() { | |
add_filter( 'jet-form-builder/render/text-field/attributes', array( $this, 'process_fields' ), 10, 2 ); | |
} | |
public function process_fields( $attrs, $field ) { | |
if ( $this->processed ) { | |
return $attrs; | |
} | |
if ( false !== strpos( $field->block_type->block_attrs['class_name'], 'currency-mask' ) ) { | |
wp_enqueue_script( | |
'jet-form-builder-inputmask', | |
\Jet_Form_Builder\Plugin::instance()->plugin_url( 'assets/lib/inputmask/jquery.inputmask.min.js' ), | |
array( 'jquery' ), | |
\Jet_Form_Builder\Plugin::instance()->get_version(), | |
true | |
); | |
$this->add_inline_script(); | |
$this->processed = true; | |
} | |
return $attrs; | |
} | |
private function add_inline_script() { | |
ob_start(); | |
?> | |
jQuery( document ).on( 'jet-form-builder/after-init', function( $ ) { | |
jQuery( "input.currency-mask" ).inputmask( { | |
prefix: "$ ", | |
groupSeparator: ",", | |
autoUnmask: true, | |
alias: "numeric", | |
digits: 2, | |
digitsOptional: 1, | |
max: 9999999 | |
} ); | |
} ); | |
<?php | |
$script = ob_get_clean(); | |
wp_add_inline_script( | |
'jet-form-builder-inputmask', | |
$script, | |
'after' | |
); | |
} | |
} | |
new JFB_Currency_Mask(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment