Changeset 3329587
- Timestamp:
- 07/17/2025 10:27:15 AM (8 months ago)
- Location:
- gforms-addon-for-country-and-state-selection
- Files:
-
- 17 added
- 8 edited
-
tags/1.2 (added)
-
tags/1.2/assets (added)
-
tags/1.2/assets/css (added)
-
tags/1.2/assets/css/admin.css (added)
-
tags/1.2/assets/data (added)
-
tags/1.2/assets/data/states.csv (added)
-
tags/1.2/assets/images (added)
-
tags/1.2/assets/images/ajax_loader.gif (added)
-
tags/1.2/assets/js (added)
-
tags/1.2/assets/js/admin.js (added)
-
tags/1.2/assets/js/fields.js (added)
-
tags/1.2/class-gf-cws-fields.php (added)
-
tags/1.2/includes (added)
-
tags/1.2/includes/class-gf-cws-admin-fields.php (added)
-
tags/1.2/includes/functions.php (added)
-
tags/1.2/index.php (added)
-
tags/1.2/readme.txt (added)
-
trunk/assets/css/admin.css (modified) (2 diffs)
-
trunk/assets/js/admin.js (modified) (1 diff)
-
trunk/assets/js/fields.js (modified) (1 diff)
-
trunk/class-gf-cws-fields.php (modified) (7 diffs)
-
trunk/includes/class-gf-cws-admin-fields.php (modified) (1 diff)
-
trunk/includes/functions.php (modified) (2 diffs)
-
trunk/index.php (modified) (1 diff)
-
trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
gforms-addon-for-country-and-state-selection/trunk/assets/css/admin.css
r2634709 r3329587 1 .gfcws_stateload .loader {1 .gfcws_stateload .loader { 2 2 position: absolute; 3 3 top: 10px; 4 4 left: 30px; 5 5 } 6 .gfcws_stateload{ 6 7 .gfcws_stateload { 7 8 position: relative; 8 9 } 9 10 10 11 11 .gfcws_stateload .stateselection {12 .gfcws_stateload .stateselection { 12 13 background-image: url('../images/ajax_loader.gif'); 13 14 background-repeat: no-repeat; … … 16 17 } 17 18 18 .gfield_error select {19 .gfield_error select { 19 20 border: 1px solid #c02b0a; 20 21 } 21 :focus-visible{ 22 23 :focus-visible { 22 24 outline: none; 23 25 } 24 26 25 div[name="country_state_fliter"] select,#field_state_value{ 26 width:100% 27 } 27 div[name="country_state_fliter"] select, 28 #field_state_value { 29 width: 100% 30 } 31 32 .gfcws-force-hide { 33 display: none !important; 34 } -
gforms-addon-for-country-and-state-selection/trunk/assets/js/admin.js
r2634709 r3329587 1 // filter state by country using ajax 2 jQuery(document).ready(function($) { 3 $('div[name="country_state_fliter"]').each(function() { 4 const field_id = $(this).attr("id"); 5 var formid = field_id.substr(6, 1); 6 $(document).on('change','#' + field_id + ' ' + 'select[id="' + field_id + '_1"]',function() { 7 $('select[id="' + field_id + '_2"]').addClass('stateselection'); 8 let country = $('select[id="' + field_id + '_1"] option:selected').val(); 9 $('select[id="' + field_id + '_2"] option').remove(); 10 // $('#' + field_id + ' ' + '.loader').fadeIn(); 11 $('#' + field_id + ' ' + 'select[id="' + field_id + '_2"]').attr('disabled','disabled'); 12 $.ajax({ 13 type: 'get', 14 url: ajax_object.ajaxurl, 15 data: { 16 action: 'Ajax_GFCWS_Filter_Record', 17 country: country, 18 }, 19 dataType: 'json', 20 cache: false, 21 success: function(data) { 22 $('#' + field_id + ' ' + 'select[id="' + field_id + '_2"]').html(data[0]); 23 // $('#' + field_id + ' ' + '.loader').fadeOut(); 24 $('#' + field_id + ' ' + 'select[id="' + field_id + '_2"]').removeAttr('disabled'); 25 $('select[id="' + field_id + '_2"]').removeClass('stateselection'); 26 } 27 }); 28 }) 29 }) 30 }); 1 // Namespace all code under window.gfcws 2 window.gfcws = window.gfcws || {}; 3 (function($, gfcws) { 4 // filter state by country using ajax 5 gfcws.initCountryStateFilter = function() { 6 $('div[name="country_state_fliter"]').each(function() { 7 const field_id = $(this).attr("id"); 8 $(document).on('change', '#' + field_id + ' select[id="' + field_id + '_1"]', function() { 9 var ajaxObj = window.ajax_object || {}; 10 $('select[id="' + field_id + '_2"]').addClass('stateselection'); 11 let country = $('select[id="' + field_id + '_1"] option:selected').val(); 12 $('select[id="' + field_id + '_2"] option').remove(); 13 $('#' + field_id + ' select[id="' + field_id + '_2"]').attr('disabled', 'disabled'); 14 $.ajax({ 15 type: 'get', 16 url: ajaxObj.ajaxurl, 17 data: { 18 action: 'Ajax_GFCWS_Filter_Record', 19 country: country, 20 }, 21 dataType: 'json', 22 cache: false, 23 success: function(data) { 24 var $dropdown = $('#' + field_id + ' select[id="' + field_id + '_2"]'); 25 var $textInput = $('#' + field_id + ' input#gfcws_state_text_input, #' + field_id + ' input[id="' + field_id + '_2_text"]'); 26 $dropdown.html(data[0]); 27 $dropdown.removeAttr('disabled'); 28 $dropdown.removeClass('stateselection'); 29 // Check if only the placeholder is present (no real states) 30 var optionCount = $dropdown.find('option').length; 31 if (optionCount <= 1) { 32 $dropdown.hide(); 33 $textInput.show(); 34 } else { 35 $dropdown.show(); 36 $textInput.hide(); 37 } 38 } 39 }); 40 }); 41 }); 42 }; 43 // Initialize on document ready 44 $(document).ready(function() { 45 gfcws.initCountryStateFilter(); 46 }); 47 })(jQuery, window.gfcws); -
gforms-addon-for-country-and-state-selection/trunk/assets/js/fields.js
r2634709 r3329587 1 // Namespace for frontend logic for the Country/State field 2 window.gfcws = window.gfcws || {}; 3 (function($, gfcws) { 4 // AJAX logic for frontend country/state selection 5 gfcws.initCountryStateFilterFrontend = function() { 6 $('div[name="country_state_fliter"]').each(function() { 7 const field_id = $(this).attr("id"); 8 function toggleStateField() { 9 var $dropdown = $('#' + field_id + ' select[id="' + field_id + '_2"]'); 10 var $textInput = $('#' + field_id + ' input.gfcws_state_text_input, #' + field_id + ' input[id="' + field_id + '_2_text"]'); 11 var optionCount = $dropdown.find('option').length; 12 var dropdownName = $dropdown.data('original-name') || $dropdown.attr('name'); 13 // Store original name for later use 14 if (!$dropdown.data('original-name')) { 15 $dropdown.data('original-name', dropdownName); 16 } 17 if (optionCount <= 1) { 18 $dropdown.addClass('gfcws-force-hide').hide(); 19 $dropdown.removeAttr('name'); 20 $textInput.show(); 21 $textInput.attr('name', dropdownName); 22 // Set value to country name if text input is shown and empty or matches previous country 23 var countryName = $('#' + field_id + ' select[id="' + field_id + '_1"] option:selected').text(); 24 if (!$textInput.data('user-typed') || !$textInput.val() || $textInput.val() === $textInput.data('last-country')) { 25 $textInput.val(countryName); 26 $textInput.data('last-country', countryName); 27 } 28 // Remove error styles from hidden dropdown 29 $dropdown.removeClass('gfield_error').removeAttr('aria-invalid').css('border', ''); 30 } else { 31 $dropdown.removeClass('gfcws-force-hide').show(); 32 $dropdown.attr('name', dropdownName); 33 $textInput.hide(); 34 $textInput.removeAttr('name'); 35 // Sync value from text input to dropdown (if any) 36 if ($textInput.val()) { 37 $dropdown.val($textInput.val()); 38 } 39 // Remove error styles from hidden text input 40 $textInput.removeClass('gfield_error').removeAttr('aria-invalid').css('border', ''); 41 } 42 } 43 // Track if user types in the text input (so we don't overwrite their input) 44 $(document).on('input', '#' + field_id + ' input.gfcws_state_text_input, #' + field_id + ' input[id="' + field_id + '_2_text"]', function() { 45 $(this).data('user-typed', true); 46 }); 47 // On country change 48 $(document).on('change', '#' + field_id + ' select[id="' + field_id + '_1"]', function() { 49 var ajaxObj = window.ajax_object || {}; 50 $('select[id="' + field_id + '_2"]').addClass('stateselection'); 51 let country = $('select[id="' + field_id + '_1"] option:selected').val(); 52 $('select[id="' + field_id + '_2"] option').remove(); 53 $('#' + field_id + ' select[id="' + field_id + '_2"]').attr('disabled', 'disabled'); 54 $.ajax({ 55 type: 'get', 56 url: ajaxObj.ajaxurl, 57 data: { 58 action: 'Ajax_GFCWS_Filter_Record', 59 country: country, 60 }, 61 dataType: 'html', 62 cache: false, 63 success: function(data) { 64 var $dropdown = $('#' + field_id + ' select[id="' + field_id + '_2"]'); 65 var $textInput = $('#' + field_id + ' input.gfcws_state_text_input, #' + field_id + ' input[id="' + field_id + '_2_text"]'); 66 $dropdown.html(data); 67 $dropdown.removeAttr('disabled'); 68 $dropdown.removeClass('stateselection'); 69 // Toggle after AJAX 70 var optionCount = $dropdown.find('option').length; 71 // Fix: define dropdownName here 72 var dropdownName = $dropdown.data('original-name') || $dropdown.attr('name'); 73 if (optionCount <= 1) { 74 $dropdown.addClass('gfcws-force-hide').hide(); 75 $dropdown.removeAttr('name'); 76 $textInput.show(); 77 $textInput.attr('name', dropdownName); 78 // Always set value to country name after AJAX 79 var countryName = $('#' + field_id + ' select[id="' + field_id + '_1"] option:selected').text(); 80 $textInput.val(countryName); 81 // console.log('Set state text field to country:', countryName); 82 $textInput.data('last-country', countryName); 83 // Remove error styles from hidden dropdown 84 $dropdown.removeClass('gfield_error').removeAttr('aria-invalid').css('border', ''); 85 } else { 86 $dropdown.removeClass('gfcws-force-hide').show(); 87 $textInput.hide(); 88 } 89 } 90 }); 91 }); 92 // On page load, if a country is pre-selected, trigger AJAX to load states 93 var $countrySelect = $('#' + field_id + ' select[id="' + field_id + '_1"]'); 94 var defaultCountry = $countrySelect.val(); 95 if (defaultCountry) { 96 var ajaxObj = window.ajax_object || {}; 97 var $dropdown = $('#' + field_id + ' select[id="' + field_id + '_2"]'); 98 $dropdown.addClass('stateselection'); 99 $dropdown.attr('disabled', 'disabled'); 100 $.ajax({ 101 type: 'get', 102 url: ajaxObj.ajaxurl, 103 data: { 104 action: 'Ajax_GFCWS_Filter_Record', 105 country: defaultCountry, 106 }, 107 dataType: 'html', 108 cache: false, 109 success: function(data) { 110 var $dropdown = $('#' + field_id + ' select[id="' + field_id + '_2"]'); 111 var $textInput = $('#' + field_id + ' input.gfcws_state_text_input, #' + field_id + ' input[id="' + field_id + '_2_text"]'); 112 $dropdown.html(data); 113 $dropdown.removeAttr('disabled'); 114 $dropdown.removeClass('stateselection'); 115 // Toggle after AJAX 116 var optionCount = $dropdown.find('option').length; 117 // console.log('Page Load AJAX:', {field_id, optionCount, $dropdown, $textInput}); 118 if (optionCount <= 1) { 119 $dropdown.addClass('gfcws-force-hide').hide(); 120 $textInput.show(); 121 $textInput.val(defaultCountry); 122 } else { 123 $dropdown.removeClass('gfcws-force-hide').show(); 124 $textInput.hide(); 125 } 126 } 127 }); 128 } else { 129 // If no country selected, just run the toggle 130 toggleStateField(); 131 } 132 133 // On form submit, ensure only the visible field has the name attribute 134 var $form = $(this).closest('form'); 135 $form.on('submit', function() { 136 var $dropdown = $('#' + field_id + ' select[id="' + field_id + '_2"]'); 137 var $textInput = $('#' + field_id + ' input.gfcws_state_text_input, #' + field_id + ' input[id="' + field_id + '_2_text"]'); 138 var dropdownName = $dropdown.data('original-name') || $dropdown.attr('name'); 139 if ($dropdown.is(':visible')) { 140 $dropdown.attr('name', dropdownName); 141 $textInput.removeAttr('name'); 142 } else if ($textInput.is(':visible')) { 143 $textInput.attr('name', dropdownName); 144 $dropdown.removeAttr('name'); 145 } 146 }); 147 }); 148 }; 149 // Initialize on document ready 150 $(document).ready(function() { 151 gfcws.initCountryStateFilterFrontend(); 152 }); 153 })(jQuery, window.gfcws); -
gforms-addon-for-country-and-state-selection/trunk/class-gf-cws-fields.php
r2634709 r3329587 1 1 <?php 2 // Ensure Gravity Forms and WordPress functions are available 3 if (!class_exists('GFForms')) require_once(ABSPATH . 'wp-content/plugins/gravityforms/gravityforms.php'); 4 if (!function_exists('add_action')) require_once(ABSPATH . 'wp-includes/plugin.php'); 2 5 GFForms::include_addon_framework(); 3 6 class GFCWSAddOn extends GFAddOn{ … … 20 23 require_once( GF_CWS_INC. 'class-gf-cws-admin-fields.php' ); 21 24 require_once( GF_CWS_INC. 'functions.php' ); 22 add_action( 'gform_enqueue_scripts', array($this,'gfcws_pre_init_enqueue_script'));23 25 add_action( 'gform_editor_js_set_default_values' , array( $this,'gfcws_countrywisestate_group_fields' )); 24 26 } … … 28 30 parent::init_admin(); 29 31 30 add_action( 'admin_enqueue_scripts', array($this,'gfcws_addon_admin'));31 32 add_action( 'gform_editor_js', array( $this,"gfcws_input_placeholders_editor_js")); 32 33 add_filter( 'gform_enable_field_label_visibility_settings', '__return_true' ); … … 43 44 'handle' => 'gfcws_addon_script_js', 44 45 'src' => $this->get_base_url() . '/assets/js/fields.js', 46 'version' => $this->_version, 47 'deps' => array( 'jquery' ), 48 'callback' => array( $this, 'localize_scripts' ), 49 'enqueue' => array( 50 array( 'field_types' => array( 'countrywisestate' ) ), 51 ) 52 ), 53 array( 54 'handle' => 'gfcws_addon_admin_js', 55 'src' => $this->get_base_url() . '/assets/js/admin.js', 45 56 'version' => $this->_version, 46 57 'deps' => array( 'jquery' ), … … 91 102 public function gfcws_field_advanced_settings( $position, $form_id ) { 92 103 if ( $position == 200 ) { 93 $get_countrys = NEWGF_ADMIN_FIELDS_MODULE;104 $get_countrys = new GF_ADMIN_FIELDS_MODULE; 94 105 ?> 95 106 <li class="input_class_setting field_setting"> … … 141 152 * 142 153 * Hook : gform_enqueue_scripts 143 *144 */145 public function gfcws_pre_init_enqueue_script() {146 wp_enqueue_script( GF_CWS_NAME.'-addon-script');147 wp_register_script( GF_CWS_NAME.'-addon-script', GF_CWS_JS . 'admin.js', array('jquery'),GF_CWS_VER, true );148 wp_localize_script( GF_CWS_NAME.'-addon-script','ajax_object',array(149 'ajaxurl' => admin_url('admin-ajax.php'),150 'GF_CWS_ASSETS' => plugin_dir_url(__FILE__).'assets/',151 ));152 }153 154 /**155 * Include css file in gravity form panel using hook156 *157 * Hook : admin_enqueue_scripts158 154 * 159 155 */ … … 324 320 <?php 325 321 } 322 323 public function localize_scripts() { 324 ?> 325 <script type="text/javascript"> 326 var ajax_object = { 327 ajaxurl: "<?php echo admin_url('admin-ajax.php'); ?>", 328 GF_CWS_ASSETS: "<?php echo plugin_dir_url(__FILE__); ?>assets/" 329 }; 330 </script> 331 <?php 332 } 326 333 } 327 334 GFAddOn::register( 'GFCWSAddOn' ); 328 ?> -
gforms-addon-for-country-and-state-selection/trunk/includes/class-gf-cws-admin-fields.php
r2634709 r3329587 1 1 <?php 2 if ( ! class_exists( 'GFForms' ) ) { 3 die(); 4 } 5 class GF_ADMIN_FIELDS_MODULE extends GF_Field{ 6 public $type = 'countrywisestate'; 7 8 public function get_form_editor_field_title() { 9 return esc_attr__( 'Lookup', 'gfcws' ); 10 } 11 public function get_form_editor_field_icon() { 12 return 'gform-icon gform-icon--place'; 13 } 14 15 public function get_form_editor_button() { 16 return array( 17 'group' => 'advanced_fields', 18 'text' => $this->get_form_editor_field_title(), 19 'icon' => $this->get_form_editor_field_icon(), 20 ); 21 } 22 23 function get_form_editor_field_settings() { 24 return array( 25 'label_setting', 26 'description_setting', 27 'input_placeholders_setting', 28 'admin_label_setting', 29 'label_placement_setting', 30 'sub_label_placement_setting', 31 'input_class_setting', 32 'countrywisestate_setting', 33 'css_class_setting', 34 'visibility_setting', 35 'conditional_logic_field_setting', 36 'description_setting', 37 'rules_setting', 38 'prepopulate_field_setting', 39 ); 40 } 41 42 public function is_conditional_logic_supported() { 43 return true; 44 } 45 46 47 public function get_required_inputs_ids() { 48 return array( '1', '2'); 49 } 50 51 public function get_field_container_tag( $form ) { 52 53 if ( GFCommon::is_legacy_markup_enabled( $form ) ) { 54 return parent::get_field_container_tag( $form ); 55 } 56 57 return 'fieldset'; 58 59 } 60 61 function validate( $value, $form ) { 62 $newval = array(); 63 foreach($value as $key => $data){ 64 if($data == 'No data'){ 65 $newval[$key] = ''; 66 }else{ 67 $newval[$key] = $data; 68 } 69 } 70 if ( $this->isRequired ) { 71 $message = $this->complex_validation_message( $newval, $this->get_required_inputs_ids() ); 72 73 if ( $message ) { 74 $this->failed_validation = true; 75 $message_intro = empty( $this->errorMessage ) ? __( 'This field is required.', 'gfcws' ) : $this->errorMessage; 76 $this->validation_message = $message_intro . ' ' . $message; 2 // Ensure Gravity Forms and WordPress functions are available 3 if (!class_exists('GF_Field')) require_once(ABSPATH . 'wp-content/plugins/gravityforms/includes/fields/class-gf-field.php'); 4 if (!function_exists('esc_attr__')) require_once(ABSPATH . 'wp-includes/l10n.php'); 5 if (!class_exists('GFCommon')) require_once(ABSPATH . 'wp-content/plugins/gravityforms/common.php'); 6 if (!function_exists('__')) require_once(ABSPATH . 'wp-includes/l10n.php'); 7 if (!class_exists('GFForms')) die(); 8 9 class GF_ADMIN_FIELDS_MODULE extends GF_Field { 10 public $type = 'countrywisestate'; 11 12 public function get_form_editor_field_title() { 13 return esc_attr__( 'Lookup', 'gfcws' ); 14 } 15 public function get_form_editor_field_icon() { 16 return 'gform-icon gform-icon--place'; 17 } 18 public function get_form_editor_button() { 19 return array( 20 'group' => 'advanced_fields', 21 'text' => $this->get_form_editor_field_title(), 22 'icon' => $this->get_form_editor_field_icon(), 23 ); 24 } 25 function get_form_editor_field_settings() { 26 return array( 27 'label_setting', 28 'description_setting', 29 'input_placeholders_setting', 30 'admin_label_setting', 31 'label_placement_setting', 32 'sub_label_placement_setting', 33 'input_class_setting', 34 'countrywisestate_setting', 35 'css_class_setting', 36 'visibility_setting', 37 'conditional_logic_field_setting', 38 'description_setting', 39 'rules_setting', 40 'prepopulate_field_setting', 41 ); 42 } 43 public function is_conditional_logic_supported() { return true; } 44 public function get_required_inputs_ids() { return array( '1', '2'); } 45 public function get_field_container_tag( $form ) { 46 if ( GFCommon::is_legacy_markup_enabled( $form ) ) { 47 return parent::get_field_container_tag( $form ); 48 } 49 return 'fieldset'; 50 } 51 function validate( $value, $form ) { 52 $newval = array(); 53 foreach($value as $key => $data){ 54 $newval[$key] = ($data == 'No data') ? '' : sanitize_text_field($data); 55 } 56 if ( $this->isRequired ) { 57 $message = $this->complex_validation_message( $newval, $this->get_required_inputs_ids() ); 58 if ( $message ) { 59 $this->failed_validation = true; 60 $message_intro = empty( $this->errorMessage ) ? __( 'This field is required.', 'gfcws' ) : $this->errorMessage; 61 $this->validation_message = $message_intro . ' ' . $message; 62 } 63 } 64 } 65 public function get_conditional_logic_event_custom(){ 66 return "onchange='gf_apply_rules_addon(" . esc_attr($this->formId) . "," . GFCommon::json_encode( $this->conditionalLogicFields ) . ");'"; 67 } 68 public function get_form_editor_inline_script_on_page_render() { 69 $script = sprintf( "function SetDefaultValues_countrywisestate(field) {field.label = '%s';}", esc_js($this->get_form_editor_field_title()) ) . PHP_EOL; 70 $script .= "jQuery(document).bind('gform_load_field_settings', function (event, field, form) {" . 71 "var inputClass = field.inputClass == undefined ? '' : field.inputClass;" . 72 "jQuery('#input_class_setting').val(inputClass);" . 73 "});" . PHP_EOL; 74 $script .= "function SetInputClassSetting(value) {SetFieldProperty('inputClass', value);}" . PHP_EOL; 75 return $script; 76 } 77 public function get_css_class() { 78 $state_field_input = GFFormsModel::get_input( $this, $this->id . '.1' ); 79 $country_field_input = GFFormsModel::get_input( $this, $this->id . '.2' ); 80 $css_class = ''; 81 if ( ! rgar( $state_field_input, 'isHidden' ) ) $css_class .= 'has_state '; 82 if ( ! rgar( $country_field_input, 'isHidden' ) ) $css_class .= 'has_country '; 83 $css_class .= 'ginput_container_address gform-grid-row'; 84 return trim( $css_class ); 85 } 86 /** 87 * Returns a country state fieldset. 88 */ 89 public function get_field_input( $form, $value = '', $entry = null ) { 90 $form_sub_label_placement = rgar( $form, 'subLabelPlacement' ); 91 $id = absint( $this->id ); 92 $form_id = absint( $form['id'] ); 93 $is_entry_detail = $this->is_entry_detail(); 94 $is_form_editor = $this->is_form_editor(); 95 $tabindex = $this->get_tabindex(); 96 $is_admin = $is_entry_detail || $is_form_editor; 97 $css_class = $this->get_css_class(); 98 $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; 99 $disabled_text = $is_form_editor ? "disabled='disabled'" : ''; 100 $class_suffix = $is_entry_detail ? '_admin' : ''; 101 $country = ''; 102 $states = ''; 103 if ( is_array( $value ) ) { 104 $country = esc_attr( RGForms::get( $this->id . '.1', $value ) ); 105 $states = esc_attr( RGForms::get( $this->id . '.2', $value ) ); 106 } 107 $country_input = GFFormsModel::get_input( $this, $this->id . '.1' ); 108 $states_input = GFFormsModel::get_input( $this, $this->id . '.2' ); 109 $country_sub_label = rgar( $country_input, 'customLabel' ) != '' ? esc_html($country_input['customLabel']) : apply_filters( "gform_name_country_{$form_id}", apply_filters( 'gform_name_country', __( 'Country', 'gforms-addon-for-country-and-state-selection' ), $form_id ), $form_id ); 110 $states_sub_label = rgar( $states_input, 'customLabel' ) != '' ? esc_html($states_input['customLabel']) : apply_filters( "gform_name_states_{$form_id}", apply_filters( 'gform_name_states', __( 'State', 'gforms-addon-for-country-and-state-selection' ), $form_id ), $form_id ); 111 $hide_country = isset($this->hideCountry) && $this->hideCountry || rgar( $country_input, 'isHidden' ); 112 $field = GFFormsModel::get_field( $form, $id ); 113 $logic_event = 'class="gfield_select"'; 114 $inputClass = isset($this->inputClass) ? esc_attr($this->inputClass) : ''; 115 $disabled_select = $is_form_editor ? 'disabled="disabled"' : ''; 116 $required_attribute = ""; 117 $field_sub_label_placement = isset($this->subLabelPlacement) ? $this->subLabelPlacement : ''; 118 $sub_label_class_attribute = $field_sub_label_placement == 'hidden_label' ? "class='hidden_sub_label'" : 'class="gform-field-label gform-field-label--type-sub"'; 119 $is_sub_label_above = $field_sub_label_placement == 'above' || ( empty( $field_sub_label_placement ) && $form_sub_label_placement == 'above' ); 120 // Load country/state data 121 $csvFile = file(plugin_dir_path( __DIR__ ) . 'assets/data/states.csv'); 122 $datas = []; 123 foreach ($csvFile as $line) { 124 $datas[] = str_getcsv($line); 125 } 126 $newcountry = array(); 127 foreach ($datas as $values) { 128 $state = $values[0]; 129 $key = $values[3]; 130 $newcountry[$key][] = $state; 131 } 132 array_shift($newcountry); // Remove header 133 ksort($newcountry); 134 $country_option = ""; 135 $states_option = ""; 136 $defaultValue = !empty($country) ? $country : (isset($field->defaultValue) ? $field->defaultValue : ''); 137 $defaultstate = !empty($states) ? $states : (isset($field->field_state_value) ? $field->field_state_value : ''); 138 // Build country and state dropdown options 139 $boolean_defaultValue = false; 140 foreach($newcountry as $countryname => $countrystate){ 141 if($countryname == $defaultValue){ 142 $boolean_defaultValue = true; 143 break; 144 } 145 } 146 if($boolean_defaultValue){ 147 foreach($newcountry as $countryname => $countrystate){ 148 if ($countryname == $defaultValue) { 149 $country_option .= "<option value='" . esc_attr($countryname) . "' selected>" . esc_html($countryname) . "</option>"; 150 foreach($countrystate as $val_states){ 151 if($val_states == $defaultstate){ 152 $states_option .= "<option value='" . esc_attr($val_states) . "' selected>" . esc_html($val_states) . "</option>"; 153 } else { 154 $states_option .= "<option value='" . esc_attr($val_states) . "'>" . esc_html($val_states) . "</option>"; 155 } 156 } 157 continue; 77 158 } 78 } 79 80 } 81 82 public function get_conditional_logic_event_custom(){ 83 return "onchange='gf_apply_rules_addon(" . $this->formId . ',' . GFCommon::json_encode( $this->conditionalLogicFields ) . ");'"; 84 } 85 86 public function get_form_editor_inline_script_on_page_render() { 87 $script = sprintf( "function SetDefaultValues_countrywisestate(field) {field.label = '%s';}", $this->get_form_editor_field_title() ) . PHP_EOL; 88 $script .= "jQuery(document).bind('gform_load_field_settings', function (event, field, form) {" . 89 "var inputClass = field.inputClass == undefined ? '' : field.inputClass;" . 90 "jQuery('#input_class_setting').val(inputClass);" . 91 "});" . PHP_EOL; 92 $script .= "function SetInputClassSetting(value) {SetFieldProperty('inputClass', value);}" . PHP_EOL; 93 return $script; 94 } 95 96 public function get_css_class() { 97 98 $state_field_input = GFFormsModel::get_input( $this, $this->id . '.1' ); 99 $country_field_input = GFFormsModel::get_input( $this, $this->id . '.2' ); 100 101 $css_class = ''; 102 if ( ! rgar( $state_field_input, 'isHidden' ) ) { 103 $css_class .= 'has_state '; 104 } 105 if ( ! rgar( $country_field_input, 'isHidden' ) ) { 106 $css_class .= 'has_country '; 107 } 108 109 $css_class .= 'ginput_container_address'; 110 111 return trim( $css_class ); 112 } 159 $country_option .= "<option value='" . esc_attr($countryname) . "'>" . esc_html($countryname) . "</option>"; 160 } 161 } else { 162 foreach($newcountry as $countryname => $countrystate){ 163 $country_option .= "<option value='" . esc_attr($countryname) . "'>" . esc_html($countryname) . "</option>"; 164 foreach($countrystate as $val_states){ 165 $states_option .= "<option value='" . esc_attr($val_states) . "'>" . esc_html($val_states) . "</option>"; 166 } 167 } 168 } 169 $country_field = self::gfcws_get_country_field( $country_input, $id, $field_id, $country, $disabled_select, $country_option, $logic_event, $tabindex, $required_attribute); 170 $states_field = self::gfcws_get_states_field( $states_input, $id, $form_id, $field_id, $states, $disabled_select, $states_option, $logic_event, $tabindex, $required_attribute); 171 // Country field. 172 if ( $is_admin || ! $hide_country ) { 173 $style = $hide_country ? "style='display:none;'" : ''; 174 if ( $is_sub_label_above ) { 175 $countryinput = "<span class='ginput_left{$class_suffix} address_country ginput_address_country gf_left_half gform-grid-col' id='{$field_id}_1_container' {$style}><label for='{$field_id}_1' id='{$field_id}_1_label' {$sub_label_class_attribute}>{$country_sub_label}</label>{$country_field}</span>"; 176 } else { 177 $countryinput = "<span class='ginput_left{$class_suffix} address_country ginput_address_country gf_left_half gform-grid-col' id='{$field_id}_1_container' {$style}>{$country_field}<label for='{$field_id}_1' id='{$field_id}_1_label' {$sub_label_class_attribute}>{$country_sub_label}</label></span>"; 178 } 179 } else { 180 $countryinput = sprintf( "<input type='hidden' class='gform_hidden' name='input_%d.1' id='%s_1' value='%s'/>", $id, $field_id, esc_attr($defaultValue) ); 181 } 182 // State field. 183 $style = ( $is_admin && ( isset($this->hideState) && $this->hideState || rgar( $states_input, 'isHidden' ) ) ) ? "style='display:none;'" : ''; 184 if ( $is_admin || ( !(isset($this->hideState) && $this->hideState) && ! rgar( $states_input, 'isHidden' ) ) ) { 185 if ( $is_sub_label_above ) { 186 $stateinput = "<span class='ginput_right{$class_suffix} gfcws_stateload address_state ginput_address_state gf_right_half gform-grid-col' id='{$field_id}_2_container' {$style}><label for='{$field_id}_2' id='{$field_id}_2_label' {$sub_label_class_attribute}>{$states_sub_label}</label>{$states_field}</span>"; 187 } else { 188 $stateinput = "<span class='ginput_right{$class_suffix} gfcws_stateload address_state ginput_address_state gf_right_half gform-grid-col' id='{$field_id}_2_container' {$style}>{$states_field}<label for='{$field_id}_2' id='{$field_id}_2_label' {$sub_label_class_attribute}>{$states_sub_label}</label></span>"; 189 } 190 } else { 191 $stateinput = sprintf( "<input type='hidden' class='gform_hidden' name='input_%d.2' id='%s_2' value='%s'/>", $id, $field_id, esc_attr($defaultValue) ); 192 } 193 $input = $countryinput . $stateinput; 194 $show = "<div class='ginput_complex{$class_suffix} ginput_container {$css_class}' name='country_state_fliter' id='$field_id'>{$input}<div class='gf_clear gf_clear_complex'></div></div>"; 195 return $show; 196 } 197 /** 198 * Returns a country select field. 199 */ 200 public function gfcws_get_country_field($input, $id, $field_id, $country, $disabled_select, $country_option, $logic_event, $tabindex, $required_attribute){ 201 $placeholder_value_country = GFCommon::get_input_placeholder_value( $input ); 202 $options_country = ""; 203 $style_width=''; 204 $autocomplete_country = 'large'; 205 if ($placeholder_value_country) { 206 $options_country .= "<option name='country_input_placeholders' value='{$placeholder_value_country}'>{$placeholder_value_country}</option>"; 207 }else{ 208 $options_country .= '<option value="">Select Country</option>'; 209 } 210 $options_country .= $country_option; 211 $markup = ''; 212 $markup .= "<select class='{$autocomplete_country}' data-show-subtext='true' data-live-search='true' name='input_{$id}.1' id='{$field_id}_1' {$logic_event} {$disabled_select} {$style_width} {$tabindex} {$required_attribute}>"; 213 $markup .= "{$options_country}"; 214 $markup .= "</select>"; 215 return $markup; 216 } 217 /** 218 * Returns a state select field. 219 */ 220 public function gfcws_get_states_field($input, $id, $form_id, $field_id, $states, $disabled_select, $states_option, $logic_event, $tabindex, $required_attribute){ 221 $placeholder_value_states = GFCommon::get_input_placeholder_value( $input ); 222 $field = GFFormsModel::get_field( $form_id, $id ); 223 $options_states = ""; 224 $customInput = ""; 225 $autocomplete_states = 'large'; 226 if ($placeholder_value_states) { 227 $options_states .= "<option name='states_input_placeholders' value='{$placeholder_value_states}'>{$placeholder_value_states}</option>"; 228 }else{ 229 $options_states .= '<option value="">Select State</option>'; 230 } 231 $options_states .= $states_option; 232 // Render both select and text input, hide text input by default 233 $markup = "<select class='{$autocomplete_states}' data-custom-input='{$customInput}' name='input_{$id}.2' id='{$field_id}_2' {$logic_event} {$disabled_select} {$tabindex} {$required_attribute}>{$options_states}</select>"; 234 $markup .= "<input type='text' class='gfcws_state_text_input' name='input_{$id}.2' id='{$field_id}_2_text' placeholder='Enter State/Region' value='" . esc_attr($states) . "' />"; 235 return $markup; 236 } 237 238 /** 239 * Returns a entry detail. 240 */ 241 public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) { 242 if ( is_array( $value ) ) { 243 $country_entry = trim( rgget( $this->id . '.1', $value ) ); 244 $states_entry = trim( rgget( $this->id . '.2', $value ) ); 245 246 $line_break = $format == 'html' ? '<br />' : "\n"; 247 248 $country_entry_ct = ''; 249 $states_entry_ct = ''; 250 if(!empty($states_entry ) ){ 251 $states_entry_ct = '<strong>State: </strong>'.$states_entry; 252 } 253 if(!empty($country_entry) ){ 254 $country_entry_ct = '<strong>Country: </strong>'.$country_entry .$line_break; 255 } 256 $address = $country_entry_ct . $states_entry_ct; 257 return $address; 258 } else { 259 return ''; 260 } 261 } 262 263 264 /** 265 * Returns a options of countries. 266 * 267 * @since Unknown 268 * 269 * @return array 270 */ 271 public function get_country_dropdown( $selected_country = '', $placeholder = '' ) { 272 $str = ''; 273 $selected_country = strtolower( $selected_country ); 274 $countries = array_merge( array( '' ), $this->get_countries() ); 275 foreach ( $countries as $code => $country ) { 276 if ( is_numeric( $code ) ) { 277 $code = $country; 278 } 279 if ( empty( $country ) ) { 280 $country = $placeholder; 281 } 282 283 284 $selected = strtolower( esc_attr( $code ) ) == $selected_country ? "selected='selected'" : ''; 285 $str .= "<option value='" . esc_attr( $code ) . "' $selected>" . esc_html( $country ) . '</option>'; 286 } 287 288 return $str; 289 } 290 291 292 /** 293 * Returns a list of countries. 294 * 295 * @since Unknown 296 * 297 * @return array 298 */ 299 300 public function get_countries() { 301 302 $countries = array_values( $this->get_default_countries() ); 303 sort( $countries ); 113 304 114 305 /** 115 * Returns a country state fieldset. 116 * 117 * get_field_input() 118 * 119 * @since Unknown 120 * 121 * @return array 122 */ 123 public function get_field_input( $form, $value = '', $entry = null ) { 124 125 $form_sub_label_placement = rgar( $form, 'subLabelPlacement' ); 126 $id = absint( $this->id ); 127 $form_id = absint( $form['id'] ); 128 $is_entry_detail = $this->is_entry_detail(); 129 $is_form_editor = $this->is_form_editor(); 130 $tabindex = $this->get_tabindex(); 131 $is_admin = $is_entry_detail || $is_form_editor; 132 $css_class = $this->get_css_class(); 133 $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; 134 135 136 137 $disabled_text = $is_form_editor ? "disabled='disabled'" : ''; 138 $class_suffix = $is_entry_detail ? '_admin' : ''; 139 140 $country = ''; 141 $states = ''; 142 143 if ( is_array( $value ) ) { 144 $country = esc_attr( RGForms::get( $this->id . '.1', $value ) ); 145 $states = esc_attr( RGForms::get( $this->id . '.2', $value ) ); 146 } 147 148 149 $country_input = GFFormsModel::get_input( $this, $this->id . '.1' ); 150 $states_input = GFFormsModel::get_input( $this, $this->id . '.2' ); 151 152 153 //lable 154 $country_sub_label = rgar( $country_input, 'customLabel' ) != '' ? $country_input['customLabel'] : apply_filters( "gform_name_country_{$form_id}", apply_filters( 'gform_name_country', __( 'Country', 'gforms-addon-for-country-and-state-selection' ), $form_id ), $form_id ); 155 $states_sub_label = rgar( $states_input, 'customLabel' ) != '' ? $states_input['customLabel'] : apply_filters( "gform_name_states_{$form_id}", apply_filters( 'gform_name_states', __( 'State', 'gforms-addon-for-country-and-state-selection' ), $form_id ), $form_id ); 156 $hide_country = $this->hideCountry || rgar( $country_input, 'isHidden' ); 157 158 $field = GFFormsModel::get_field( $form, $id ); 159 $logic_event = 'class="gfield_select"'; 160 $inputClass = $this->inputClass; 161 $custumClass = $this->customClass; 162 $disabled_select = $is_form_editor ? 'disabled="disabled"' : ''; 163 $required_attribute = ""; 164 $style_input = "custom"; 165 166 $field_sub_label_placement = $this->subLabelPlacement; 167 $sub_label_class_attribute = $field_sub_label_placement == 'hidden_label' ? "class='hidden_sub_label'" : ''; 168 $is_sub_label_above = $field_sub_label_placement == 'above' || ( empty( $field_sub_label_placement ) && $form_sub_label_placement == 'above' ); 169 170 $csvFile = file(plugin_dir_url( __DIR__ ). 'assets/data/states.csv'); 171 $datas = []; 172 foreach ($csvFile as $line) { 173 $datas[] = str_getcsv($line); 174 } 175 176 $newcountry = array(); 177 foreach ($datas as $values) { 178 $state = $values[0]; 179 $key = $values[3]; 180 $newcountry[$key][] = $state; 181 } 182 183 $newcountrys = array_shift($newcountry); 184 ksort($newcountry); 185 $country_option = ""; 186 $states_option = ""; 187 188 if(!empty($country)){ 189 $defaultValue = $country; 190 }else{ 191 $defaultValue = $field->defaultValue; 192 } 193 194 if(!empty($states)){ 195 $defaultstate = $states; 196 }else{ 197 $defaultstate = $field->field_state_value; 198 } 199 200 $boolean_defaultValue = false; 201 $stylecountry = ""; 202 203 204 foreach($newcountry as $countryname => $countrystate){ 205 if(empty($uniqueValue)){ 206 if($countryname == $defaultValue){ 207 $boolean_defaultValue = true; 208 break; 209 } 210 } 211 } 212 213 if($boolean_defaultValue == true){ 214 foreach($newcountry as $countryname => $countrystate){ 215 216 if ($countryname == $defaultValue) { 217 $country_option .= "<option value='{$countryname}' selected>{$countryname}</option>"; 218 foreach($countrystate as $val_states){ 219 if($val_states == $defaultstate){ 220 $states_option .= "<option value='{$val_states}' selected>{$val_states}</option>"; 221 } 222 $states_option .= "<option value='{$val_states}'>{$val_states}</option>"; 223 } 224 continue; 225 } 226 $country_option .= "<option value='{$countryname}'>{$countryname}</option>"; 227 } 228 }else{ 229 foreach($newcountry as $countryname => $countrystate){ 230 $country_option .= "<option value='{$countryname}'>{$countryname}</option>"; 231 foreach($countrystate as $val_states){ 232 $states_option .= "<option value='{$val_states}'>{$val_states}</option>"; 233 } 234 } 235 } 236 237 238 $dir = plugin_dir_url( __DIR__ ); 239 $show = ""; 240 $aboveinput = ''; 241 $input_style = ''; 242 $country_field = self::gfcws_get_country_field( $country_input, $id, $field_id, $country, $disabled_select, $country_option, $logic_event, $tabindex, $required_attribute); 243 $states_field = self::gfcws_get_states_field( $states_input, $id, $form_id, $field_id, $states, $disabled_select, $states_option, $logic_event, $tabindex, $required_attribute); 244 245 // Country field. 246 if ( $is_admin || ! $hide_country ) { 247 $style = $hide_country ? "style='display:none;'" : ''; 248 if ( $is_sub_label_above ) { 249 $countryinput = "<span class='ginput_left{$class_suffix} address_country ginput_address_country' id='{$field_id}_1_container' {$style}> 250 <label for='{$field_id}_1' id='{$field_id}_1_label' {$sub_label_class_attribute}>{$country_sub_label}</label>{$country_field}</span>"; 251 } else { 252 $countryinput = "<span class='ginput_left{$class_suffix} address_country ginput_address_country' id='{$field_id}_1_container' {$style}>{$country_field}<label for='{$field_id}_1' id='{$field_id}_1_label' {$sub_label_class_attribute}>{$country_sub_label}</label></span>"; 253 } 254 } else { 255 $countryinput = sprintf( "<input type='hidden' class='gform_hidden' name='input_%d.1' id='%s_1' value='%s'/>", $id, $field_id, $defaultValue ); 256 } 257 258 259 // State field. 260 $style = ( $is_admin && ( $this->hideState || rgar( $states_input, 'isHidden' ) ) ) ? "style='display:none;'" : ''; // support for $this->hideState legacy property 261 if ( $is_admin || ( ! $this->hideState && ! rgar( $states_input, 'isHidden' ) ) ) { 262 if ( $is_sub_label_above ) { 263 $stateinput = "<span class='ginput_right{$class_suffix} gfcws_stateload address_state ginput_address_state' id='{$field_id}_2_container' {$style}> 264 <label for='{$field_id}_2' id='{$field_id}_2_label' {$sub_label_class_attribute}>{$states_sub_label}</label>{$states_field}</span>"; 265 } else { 266 $stateinput = "<span class='ginput_right{$class_suffix} gfcws_stateload address_state ginput_address_state' id='{$field_id}_2_container' {$style}>{$states_field}<label for='{$field_id}_2' id='{$field_id}_2_label' {$sub_label_class_attribute}>{$states_sub_label}</label></span>"; 267 } 268 } else { 269 $stateinput = sprintf( "<input type='hidden' class='gform_hidden' name='input_%d.2' id='%s_2' value='%s'/>", $id, $field_id, $defaultValue ); 270 } 271 $input = $countryinput .$stateinput; 272 273 $show .= "<div class='ginput_complex{$class_suffix} ginput_container {$css_class}' name='country_state_fliter' id='$field_id' {$input_style}> 274 {$input}<div class='gf_clear gf_clear_complex'></div></div>"; 275 276 return $show; 277 } 278 279 /** 280 * Returns a country select field. 281 * 282 * gfcws_get_states_field() 283 * 284 * @since Unknown 285 * 286 * @return array 287 */ 288 public function gfcws_get_country_field($input, $id, $field_id, $country, $disabled_select, $country_option, $logic_event, $tabindex, $required_attribute){ 289 $placeholder_value_country = GFCommon::get_input_placeholder_value( $input ); 290 $options_country = ""; 291 $style_width=''; 292 $autocomplete_country = 'large'; 293 if ($placeholder_value_country) { 294 $options_country .= "<option name='country_input_placeholders' value='{$placeholder_value_country}'>{$placeholder_value_country}</option>"; 295 }else{ 296 $options_country .= '<option value="">Select Country</option>'; 297 } 298 $options_country .= $country_option; 299 $markup = ''; 300 $markup .= "<select class='{$autocomplete_country}' data-show-subtext='true' data-live-search='true' name='input_{$id}.1' id='{$field_id}_1' {$logic_event} {$disabled_select} {$style_width} {$tabindex} {$required_attribute}>"; 301 $markup .= "{$options_country}"; 302 $markup .= "</select>"; 303 return $markup; 304 } 305 306 /** 307 * Returns a state select field. 308 * 309 * gfcws_get_states_field() 310 * 311 * @since Unknown 312 * 313 * @return array 314 */ 315 public function gfcws_get_states_field($input, $id, $form_id, $field_id, $states, $disabled_select, $states_option, $logic_event, $tabindex, $required_attribute){ 316 $placeholder_value_states = GFCommon::get_input_placeholder_value( $input ); 317 $field = GFFormsModel::get_field( $form_id, $id ); 318 $options_states = ""; 319 $customInput = ""; 320 $autocomplete_states = 'large'; 321 if ($placeholder_value_states) { 322 $options_states .= "<option name='states_input_placeholders' value='{$placeholder_value_states}'>{$placeholder_value_states}</option>"; 323 }else{ 324 $options_states .= '<option value="">Select State</option>'; 325 } 326 $options_states .= $states_option; 327 $markup = "<select class='{$autocomplete_states}' data-custom-input='{$customInput}' name='input_{$id}.2' id='{$field_id}_2' {$logic_event} {$disabled_select} {$tabindex} {$required_attribute}>{$options_states}</select>"; 328 return $markup; 329 } 330 331 /** 332 * Returns a entry detail. 333 * 334 * get_value_entry_detail() 335 * 336 * @since Unknown 337 * 338 * @return array 339 */ 340 public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) { 341 if ( is_array( $value ) ) { 342 $country_entry = trim( rgget( $this->id . '.1', $value ) ); 343 $states_entry = trim( rgget( $this->id . '.2', $value ) ); 344 345 $line_break = $format == 'html' ? '<br />' : "\n"; 346 347 $country_entry_ct = ''; 348 $states_entry_ct = ''; 349 if(!empty($states_entry ) ){ 350 $states_entry_ct = '<strong>State: </strong>'.$states_entry; 351 } 352 if(!empty($country_entry) ){ 353 $country_entry_ct = '<strong>Country: </strong>'.$country_entry .$line_break; 354 } 355 $address = $country_entry_ct . $states_entry_ct; 356 return $address; 357 } else { 358 return ''; 359 } 360 } 361 362 363 /** 364 * Returns a options of countries. 306 * A list of countries displayed in the Lookup field country drop down. 365 307 * 366 308 * @since Unknown 367 309 * 368 * @ return array310 * @param array $countries ISO 3166-1 list of countries. 369 311 */ 370 public function get_country_dropdown( $selected_country = '', $placeholder = '' ) { 371 $str = ''; 372 $selected_country = strtolower( $selected_country ); 373 $countries = array_merge( array( '' ), $this->get_countries() ); 374 foreach ( $countries as $code => $country ) { 375 if ( is_numeric( $code ) ) { 376 $code = $country; 377 } 378 if ( empty( $country ) ) { 379 $country = $placeholder; 380 } 381 382 383 $selected = strtolower( esc_attr( $code ) ) == $selected_country ? "selected='selected'" : ''; 384 $str .= "<option value='" . esc_attr( $code ) . "' $selected>" . esc_html( $country ) . '</option>'; 385 } 386 387 return $str; 388 } 389 390 391 /** 392 * Returns a list of countries. 393 * 394 * @since Unknown 395 * 396 * @return array 397 */ 398 399 public function get_countries() { 400 401 $countries = array_values( $this->get_default_countries() ); 402 sort( $countries ); 403 404 /** 405 * A list of countries displayed in the Lookup field country drop down. 406 * 407 * @since Unknown 408 * 409 * @param array $countries ISO 3166-1 list of countries. 410 */ 411 return apply_filters( 'gform_countries', $countries ); 412 413 } 414 415 /** 416 * Returns the default array of countries using the ISO 3166-1 alpha-2 code as the key to the country name. 417 * 418 * @return array 419 * 420 */ 421 422 public function get_default_countries() { 423 424 $csvFile = file(plugin_dir_path( __DIR__ ). 'assets/data/states.csv'); 425 $datas = []; 426 foreach ($csvFile as $line) { 427 $datas[] = str_getcsv($line); 428 } 429 $newcountry = array(); 430 foreach ($datas as $values) { 431 $countrycode = $values[2]; 432 $country = $values[3]; 433 $newcountry[$countrycode] = $country; 434 } 435 436 $newcountrys = array_shift($newcountry); 437 ksort($newcountry); 438 return $newcountry; 439 } 440 } 441 GF_Fields::register( new GF_ADMIN_FIELDS_MODULE() ); 442 ?> 312 return apply_filters( 'gform_countries', $countries ); 313 314 } 315 316 /** 317 * Returns the default array of countries using the ISO 3166-1 alpha-2 code as the key to the country name. 318 * 319 * @return array 320 * 321 */ 322 323 public function get_default_countries() { 324 325 $csvFile = file(plugin_dir_path( __DIR__ ) . 'assets/data/states.csv'); 326 $datas = []; 327 foreach ($csvFile as $line) { 328 $datas[] = str_getcsv($line); 329 } 330 $newcountry = array(); 331 foreach ($datas as $values) { 332 $countrycode = $values[2]; 333 $country = $values[3]; 334 $newcountry[$countrycode] = $country; 335 } 336 337 $newcountrys = array_shift($newcountry); 338 ksort($newcountry); 339 return $newcountry; 340 } 341 } 342 GF_Fields::register( new GF_ADMIN_FIELDS_MODULE() ); -
gforms-addon-for-country-and-state-selection/trunk/includes/functions.php
r3275689 r3329587 1 1 <?php 2 class GFCWS_Countrywisestate_Field_Ajax{ 3 function __construct(){ 4 add_action( 'wp_ajax_Ajax_GFCWS_Filter_Record', array( $this, 'Ajax_GFCWS_Filter_Record')); 5 add_action('wp_ajax_nopriv_Ajax_GFCWS_Filter_Record', array( $this, 'Ajax_GFCWS_Filter_Record')); 6 add_action( 'wp_ajax_Ajax_GFCWS_Filter', array( $this, 'Ajax_GFCWS_Filter')); 7 add_action('wp_ajax_nopriv_Ajax_GFCWS_Filter', array( $this, 'Ajax_GFCWS_Filter')); 2 // Ensure WordPress functions are available 3 if (!function_exists('add_action')) require_once(ABSPATH . 'wp-includes/plugin.php'); 4 if (!function_exists('sanitize_text_field')) require_once(ABSPATH . 'wp-includes/formatting.php'); 5 if (!function_exists('plugin_dir_path')) require_once(ABSPATH . 'wp-admin/includes/plugin.php'); 6 if (!function_exists('wp_die')) require_once(ABSPATH . 'wp-includes/functions.php'); 7 class GFCWS_Countrywisestate_Field_Ajax 8 { 9 function __construct() 10 { 11 add_action('wp_ajax_Ajax_GFCWS_Filter_Record', array($this, 'Ajax_GFCWS_Filter_Record')); 12 add_action('wp_ajax_nopriv_Ajax_GFCWS_Filter_Record', array($this, 'Ajax_GFCWS_Filter_Record')); 13 add_action('wp_ajax_Ajax_GFCWS_Filter', array($this, 'Ajax_GFCWS_Filter')); 14 add_action('wp_ajax_nopriv_Ajax_GFCWS_Filter', array($this, 'Ajax_GFCWS_Filter')); 15 } 16 // AJAX handler for state dropdown based on selected country 17 function Ajax_GFCWS_Filter_Record() 18 { 19 $country_request = sanitize_text_field($_GET['country']); 20 $csvFile = file(plugin_dir_path(__DIR__) . 'assets/data/states.csv'); 21 $datas = []; 22 foreach ($csvFile as $line) { 23 $datas[] = str_getcsv($line); 8 24 } 9 function Ajax_GFCWS_Filter_Record(){ 10 $country_request = sanitize_text_field($_GET['country']); 11 $csvFile = file(plugin_dir_url( __DIR__ ). 'assets/data/states.csv'); 12 $datas = []; 13 foreach ($csvFile as $line) { 14 $datas[] = str_getcsv($line); 15 } 16 $newcountry = array(); 17 foreach ($datas as $values) { 18 $state = $values[0]; 19 $key = $values[3]; 25 $newcountry = array(); 26 foreach ($datas as $values) { 27 $state = $values[0]; 28 $key = $values[3]; 29 if (!empty($state)) { 20 30 $newcountry[$key][] = $state; 21 } 22 $newcountrys = array_shift($newcountry); 31 } 32 } 33 $states_option = "<option value=''>Select State</option>"; 34 $found = false; 35 foreach ($newcountry as $key_country => $val_country) { 36 if ($key_country === $country_request) { 37 foreach ($val_country as $val_states) { 38 if (!empty($val_states)) { 39 $states_option .= "<option value='" . esc_attr($val_states) . "'>" . esc_html($val_states) . "</option>"; 40 $found = true; 41 } 42 } 43 break; 44 } 45 } 46 // If no states found, only output the placeholder 47 if (!$found) { 48 $states_option = "<option value=''>Select State</option>"; 49 } 50 echo $states_option; 51 wp_die(); 52 } 23 53 24 $states_option = ""; 25 $states_option .= "<option value=''>Select State</option>"; 26 $bolean = false; 27 $arr = array(); 28 foreach($newcountry as $key_country => $val_country){ 29 if($key_country === $country_request){ 30 foreach($val_country as $key_states => $val_states){ 31 $states_option .= "<option value='{$val_states}'>{$val_states}</option>"; 32 $bolean = true; 33 } 34 break; 35 } 36 } 37 array_push($arr,$states_option); 38 array_push($arr,$bolean); 39 echo json_encode($arr); 40 wp_die(); 54 // AJAX handler for country dropdown and state selection 55 function Ajax_GFCWS_Filter() 56 { 57 $country_request = sanitize_text_field($_GET['country']); 58 $state_request = sanitize_text_field($_GET['state']); 59 $csvFile = file(plugin_dir_path(__DIR__) . 'assets/data/states.csv'); 60 $datas = []; 61 foreach ($csvFile as $line) { 62 $datas[] = str_getcsv($line); 41 63 } 42 43 44 function Ajax_GFCWS_Filter(){ 45 $country_request = sanitize_text_field($_GET['country']); 46 $state_request = sanitize_text_field($_GET['state']); 47 $csvFile = file(plugin_dir_url( __DIR__ ). 'assets/data/states.csv'); 48 $datas = []; 49 foreach ($csvFile as $line) { 50 $datas[] = str_getcsv($line); 51 } 52 $newcountry = array(); 53 foreach ($datas as $values) { 54 $state = $values[0]; 55 $key = $values[3]; 56 $newcountry[$key][] = $state; 57 } 58 59 $newcountrys = array_shift($newcountry); 60 // ksort($newcountry); 61 62 // $country_option = ""; 63 // $country_option .= "<option value=''>Empty (no choices selected)</option>"; 64 // $bolean = false; 65 // $arr = array(); 66 // if($country_request == 'country'){ 67 // foreach($newcountry as $countryname => $val_country){ 68 // $country_option .= "<option value='{$countryname}'>{$countryname}</option>"; 69 // } 70 // }else{ 71 // ksort($newcountry[$country_request]); 72 73 // foreach($newcountry[$country_request] as $countryname => $val_country){ 74 // if($val_country == $state_request){ 75 // $country_option .= "<option value='{$val_country}' selected>{$val_country}</option>"; 76 // } 77 // $country_option .= "<option value='{$val_country}'>{$val_country}</option>"; 78 // } 79 // } 80 ksort($newcountry); 81 64 $newcountry = array(); 65 foreach ($datas as $values) { 66 $state = $values[0]; 67 $key = $values[3]; 68 $newcountry[$key][] = $state; 69 } 70 ksort($newcountry); 82 71 $country_option = "<option value=''>Empty (no choices selected)</option>"; 83 $bolean = false;84 $arr = array();85 86 72 if ($country_request === 'country') { 87 73 foreach ($newcountry as $countryname => $val_country) { … … 95 81 } 96 82 } 97 98 array_push($arr,$country_option); 99 array_push($arr,$bolean); 100 echo json_encode($arr); 101 wp_die(); 102 } 83 echo json_encode([$country_option]); 84 wp_die(); 103 85 } 104 new GFCWS_Countrywisestate_Field_Ajax; 105 ?> 106 86 } 87 new GFCWS_Countrywisestate_Field_Ajax; -
gforms-addon-for-country-and-state-selection/trunk/index.php
r3275689 r3329587 1 1 <?php 2 2 /** 3 * Plugin Name: Country and State Selection Addon for Gravity Forms 4 * Plugin URI: https://plugins.hirewebxperts.com/ 5 * Description: Country and State Selection Addon for Gravity Forms is used to add country and state dropdown fields depending on your needs. By default, all the countries of the world appear on the country select dropdown on the form and once you select any specific country, its respective states appear in the state select dropdown. All the countries and states are already available in the addon by default. 6 * Version: 1.1 7 * Author: Coder426 8 * Author URI: https://hirewebxperts.com/ 9 * Donate link: https://hirewebxperts.com/donate/ 10 * Text Domain: gforms-addon-for-country-and-state-selection 11 * Domain Path: /languages 3 * Plugin Name: Country and State Selection Addon for Gravity Forms 4 * Plugin URI: https://plugins.hirewebxperts.com/ 5 * Description: Country and State Selection Addon for Gravity Forms is used to add country and state dropdown fields depending on your needs. By default, all the countries of the world appear on the country select dropdown on the form and once you select any specific country, its respective states appear in the state select dropdown. All the countries and states are already available in the addon by default. 6 * Version: 1.2 7 * Author: Coder426 8 * Author URI: https://hirewebxperts.com/ 9 * Text Domain: gforms-addon-for-country-and-state-selection 10 * Domain Path: /languages 12 11 * License: GPLv3 13 12 * License URI: https://www.gnu.org/licenses/gpl-2.0.txt 14 * License: GPL215 13 */ 16 14 17 if (!defined('ABSPATH')) { exit;}15 if (!defined('ABSPATH')) { exit; } 18 16 19 /* Plugin details */ 20 $version = '1.1'; 21 $name = 'gforms-addon-for-country-and-state-selection'; 22 $dir_name = 'gforms-addon-for-country-and-state-selection'; 23 $location = 'plugins'; 24 $plugin_dir = dirname(__FILE__); 25 $plugin_url = plugin_dir_url(__FILE__); 17 // Ensure WordPress functions are available 18 if (!function_exists('plugin_dir_url')) require_once(ABSPATH . 'wp-admin/includes/plugin.php'); 26 19 27 //Define plugin url path 28 define($plugin_url, plugin_dir_url(__FILE__)); 29 define($plugin_dir, dirname(__FILE__)); 30 define('GF_CWS_JS', $plugin_url . 'assets/js/'); 31 define('GF_CWS_CSS', $plugin_url . 'assets/css/'); 32 define('GF_CWS_IMG', $plugin_url . 'assets/images/'); 33 define('GF_CWS_INC', $plugin_dir . '/includes/'); 20 // Plugin constants 21 $version = rand(); 22 $name = 'gforms-addon-for-country-and-state-selection'; 23 24 define('GF_CWS_PLUGIN_URL', plugin_dir_url(__FILE__)); 25 define('GF_CWS_PLUGIN_DIR', dirname(__FILE__)); 26 define('GF_CWS_JS', GF_CWS_PLUGIN_URL . 'assets/js/'); 27 define('GF_CWS_CSS', GF_CWS_PLUGIN_URL . 'assets/css/'); 28 define('GF_CWS_IMG', GF_CWS_PLUGIN_URL . 'assets/images/'); 29 define('GF_CWS_INC', GF_CWS_PLUGIN_DIR . '/includes/'); 34 30 define('GF_CWS_VER', $version); 35 31 define('GF_CWS_NAME', $name); 36 32 37 38 add_action( 'gform_loaded', array( 'GF_CWS_Field_AddOn', 'load' ), 5);33 // Load the main Add-On class after Gravity Forms is loaded 34 add_action('gform_loaded', array('GF_CWS_Field_AddOn', 'load'), 5); 39 35 class GF_CWS_Field_AddOn { 40 36 public static function load() { 41 if ( ! method_exists( 'GFForms', 'include_addon_framework' )) {37 if (!method_exists('GFForms', 'include_addon_framework')) { 42 38 return; 43 39 } 44 require_once( 'class-gf-cws-fields.php');40 require_once('class-gf-cws-fields.php'); 45 41 } 46 42 } -
gforms-addon-for-country-and-state-selection/trunk/readme.txt
r3275689 r3329587 1 1 === Country and State Selection Addon for Gravity Forms === 2 2 Contributors: coder426 3 D onate link: https://hirewebxperts.com/donate/4 Tags: Gravity form, form, gf3 Description: Country and State Selection Addon for Gravity Forms 4 Tags: Gravity Forms, country dropdown, state dropdown, address fields, form builder, location, Australia, dynamic fields, conditional logic 5 5 Requires at least: 4.9.10 6 6 Tested up to: 6.8 7 7 Requires PHP: 8.1 8 Stable tag: 1. 18 Stable tag: 1.2 9 9 License: GPLv3 10 10 License URI: https://www.gnu.org/licenses/gpl-3.0.html … … 13 13 == Description == 14 14 15 Country and State Selection Addon for Gravity Forms is used to add country and state dropdown fields depending on your needs. By default, all the countries of the world appear on the country select dropdown on the form and once you select any specific country, its respective states appear in the state select dropdown. All the countries and states are already available in the addon by default.15 Country and State Selection Addon for Gravity Forms lets you easily add dynamic country and state dropdown fields to your Gravity Forms. Instantly populate all countries of the world in a country select dropdown, and display the relevant states or provinces when a country is selected. This plugin is perfect for address fields, shipping forms, user registration, and any form that needs accurate location data. All countries and states are included by default, including Australia and its states. 16 16 17 17 Watch this video to see how the plugin works: 18 19 18 https://www.youtube.com/watch?v=WtctxrU4zr4 20 19 20 = Key Features = 21 21 22 = Key features of this plugin include = 23 24 * Ability to add country and state fields dropdowns.25 * Ability to use either country or state dropdown.26 * Ability to set default country and state.27 * Ability to set country and state in Conditional Logic.28 * Ability to save country and state values in backend after the form submission.29 22 * Add country and state dropdown fields to any Gravity Form 23 * Use either country or state dropdown independently 24 * Set default country and state values 25 * Use country and state in Gravity Forms Conditional Logic 26 * Save country and state values in the backend after form submission 27 * Fully compatible with Gravity Forms add-ons (Signature, User Registration, etc.) 28 * Includes all countries and their states/provinces, including Australia 29 * Optimized for performance and security 30 30 31 31 == Installation == 32 32 33 The plugin is simple to install: 34 35 * Download gforms-addon-for-country-and-state-selection.1.1.zip 36 * Unzip 37 * Upload gforms-addon-for-country-and-state-selection directory to your /wp-content/plugins directory 38 * Go to the plugin menu page and activate the plugin. 39 33 1. Download gforms-addon-for-country-and-state-selection.1.2.zip 34 2. Unzip 35 3. Upload the gforms-addon-for-country-and-state-selection directory to your /wp-content/plugins directory 36 4. Go to the Plugins menu page and activate the plugin 40 37 41 38 == Screenshots == … … 49 46 7. Fields display on Frontend 50 47 48 == Changelog == 51 49 52 53 == Changelog == 50 = 1.2 - 2025-07-15 = 51 * Added Australia and its states to the country/state dropdown 52 * Optimized frontend and backend code for performance and security 53 * Improved compatibility with all Gravity Forms add-ons 54 54 55 55 = 1.1 - 17/Apr/2025 =
Note: See TracChangeset
for help on using the changeset viewer.