Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active November 11, 2024 23:03
Show Gist options
  • Save Crocoblock/e99cd1f5983b6890b04590b58e3085de to your computer and use it in GitHub Desktop.
Save Crocoblock/e99cd1f5983b6890b04590b58e3085de to your computer and use it in GitHub Desktop.
JetEngine Dynamic Field add custom callback (for example, add callback that partially masks an email with * or other symbols)
<?php
//filter to add a callback to the list
add_filter( 'jet-engine/listings/allowed-callbacks', 'add_custom_dynamic_field_callbacks' );
//filter to specify which arguments will the callback have
add_filter( 'jet-engine/listing/dynamic-field/callback-args', 'add_custom_dynamic_field_callbacks_args', 0, 3 );
//filter to add controls
//controls arguments are set as in Elementor controls
//(do not use Elementor constants if you plan to use the callback in Gutenberg only)
// https://developers.elementor.com/docs/controls/regular-control/
add_filter ( 'jet-engine/listings/allowed-callbacks-args', 'add_custom_controls', 0, 1 );
function add_custom_dynamic_field_callbacks( $callbacks ) {
$callbacks['mask_email'] = __( 'Mask email', 'jet-engine' );
return $callbacks;
}
function add_custom_dynamic_field_callbacks_args( $result, $callback, $settings ) {
if ($callback === 'mask_email') {
//$result[0] is the value, that Dynamic Field originally receives - for example, the ID of the post, or the meta field value, etc.
$result = array( $result[0], $settings['from_start'], $settings['from_end'], $settings['mask_placeholder'], $settings['invalid_email_message'], $settings['show_at_symbol'], $settings['minimum_hidden'] );
}
return $result;
}
function add_custom_controls( $controls ) {
$controls['from_start'] = array(
'label' => esc_html__( 'Visible symbols from start', 'jet-engine' ),
'type' => 'number',
'min' => 1,
'default' => '3',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'mask_email' ),
),
);
$controls['from_end'] = array(
'label' => esc_html__( 'Visible symbols from end', 'jet-engine' ),
'type' => 'number',
'min' => 1,
'default' => '3',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'mask_email' ),
),
);
$controls['minimum_hidden'] = array(
'label' => __( 'Minimum hidden', 'jet-engine' ),
'type' => 'number',
'min' => 1,
'default' => 3,
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'mask_email' ),
),
);
$controls['mask_placeholder'] = array(
'label' => esc_html__( 'Mask placeholder', 'jet-engine' ),
'type' => 'text',
'default' => '*',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'mask_email' ),
),
);
$controls['invalid_email_message'] = array(
'label' => esc_html__( 'Invalid email message', 'jet-engine' ),
'type' => 'text',
'default' => 'Not a valid email.',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'mask_email' ),
),
);
$controls['show_at_symbol'] = array(
'label' => esc_html__( 'Show @ symbol', 'jet-engine' ),
'type' => 'switcher',
'default' => '',
'condition' => array(
'dynamic_field_filter' => 'yes',
'filter_callback' => array( 'mask_email' ),
),
);
return $controls;
}
function mask_email( $value, $from_start, $from_end, $placeholder, $invalid_email_message, $show_at, $minimum_hidden ) {
if ( !is_email( $value ) ){
return $invalid_email_message;
}
$at_pos = strpos( $value, '@' );
$length = strlen($value);
$placeholder = !empty( $placeholder ) ? $placeholder : '*';
if ( ( $at_pos - $from_start ) < $minimum_hidden ) {
$mask_start = max( ( $at_pos - $minimum_hidden ), 0) ;
} else {
$mask_start = $from_start;
}
if ( ( $at_pos + $minimum_hidden + $from_end ) >= $length ) {
$mask_end = min( ( $at_pos + $minimum_hidden ), ( $length - 1 ) );
} else {
$mask_end = $length - $from_end - 1;
}
$mask_length_start = $at_pos - $mask_start;
$mask_length_end = $mask_end - $at_pos;
$result = substr_replace( $value, str_repeat( $placeholder, $mask_length_start ), $mask_start, $mask_length_start );
$result = substr_replace( $result, str_repeat( $placeholder, $mask_length_end ), $at_pos+1, $mask_length_end );
if ( !$show_at ) {
$result[ $at_pos ] = $placeholder;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment