Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active January 6, 2017 20:50
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 billerickson/12127b69fd93d0c8eeb956e7273a140e to your computer and use it in GitHub Desktop.
Save billerickson/12127b69fd93d0c8eeb956e7273a140e to your computer and use it in GitHub Desktop.
<?php
/**
* Load WPForms Customizations
*
* @since 1.0.0
*/
function be_wpforms_customizations() {
// WPForms Pro is required
if ( !class_exists( 'WPForms_Pro' ) ) {
return;
}
require_once( EA_DIR . '/inc/field-zip-checker.php' );
}
add_action( 'wpforms_loaded', 'be_wpforms_customizations' );
<?php
/**
* Zip Code Check field.
*
* @package WPForms
* @author WPForms
* @since 1.0.0
* @license GPL-2.0+
* @copyright Copyright (c) 2016, WPForms LLC
*/
class BE_Zip_Checker extends WPForms_Field {
/**
* Primary class constructor.
*
* @since 1.0.0
*/
public function init() {
// Define field type information
$this->name = __( 'Zipcode Check', 'ea' );
$this->type = 'zip_check';
$this->icon = 'fa-hashtag';
$this->order = 13;
}
/**
* Field options panel inside the builder.
*
* @since 1.0.0
* @param array $field
*/
public function field_options( $field ) {
//--------------------------------------------------------------------//
// Basic field options
//--------------------------------------------------------------------//
//$this->field_option( 'meta', $field );
$this->field_option( 'basic-options', $field, array( 'markup' => 'open' ) );
$this->field_option( 'placeholder', $field );
$this->field_option( 'required', $field );
$this->field_option( 'css', $field );
$this->field_option( 'basic-options', $field, array( 'markup' => 'close' ) );
}
/**
* Field preview inside the builder.
*
* @since 1.0.0
* @param array $field
*/
public function field_preview( $field ) {
$placeholder = !empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : '';
$this->field_preview_option( 'label', $field );
printf( '<input type="text" placeholder="%s" class="primary-input" disabled>', $placeholder );
$this->field_preview_option( 'description', $field );
}
/**
* Field display on the form front-end.
*
* @since 1.0.0
* @param array $field
* @param array $form_data
*/
public function field_display( $field, $field_atts, $form_data ) {
// Setup and sanitize the necessary data
$field = apply_filters( 'wpforms_number_field_display', $field, $field_atts, $form_data );
$field_placeholder = !empty( $field['placeholder']) ? esc_attr( $field['placeholder'] ) : '';
$field_required = !empty( $field['required'] ) ? ' required' : '';
$field_class = implode( ' ', array_map( 'sanitize_html_class', $field_atts['input_class'] ) );
$field_id = implode( ' ', array_map( 'sanitize_html_class', $field_atts['input_id'] ) );
$field_value = !empty( $field['default_value'] ) ? esc_attr( apply_filters( 'wpforms_process_smart_tags', $field['default_value'], $form_data ) ) : '';
$field_data = '';
// Add available zips as data
$zipcodes = explode( PHP_EOL, $form_data['settings']['mtc_zip_codes'] );
$zipcodes = array_map( 'intval', $zipcodes );
$field_data .= ' data-zipcodes="' . implode( ',', $zipcodes ) . '"';
if ( !empty( $field_atts['input_data'] ) ) {
foreach ( $field_atts['input_data'] as $key => $val ) {
$field_data .= ' data-' . $key . '="' . $val . '"';
}
}
$button = '<button class="zip-check">GO</button>';
$popup = '<div id="mtc-unavailable-popup" class="mct-popup"><p>We are not currently servicing this zip code. If you have further questions, please call us at 703.299.0101.</p></div>';
// Primary text field
printf(
'<input type="text" name="wpforms[fields][%d]" id="%s" class="%s" value="%s" placeholder="%s" %s %s>%s %s',
$field['id'],
$field_id,
$field_class,
$field_value,
$field_placeholder,
$field_required,
$field_data,
$button,
$popup
);
}
}
new BE_Zip_Checker;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment