ListCountry.php
4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NF_Fields_CountryList
*/
class NF_Fields_ListCountry extends NF_Abstracts_List
{
protected $_name = 'listcountry';
protected $_type = 'listcountry';
protected $_nicename = 'Country';
protected $_section = 'userinfo';
protected $_templates = array( 'listcountry', 'listselect' );
public function __construct()
{
parent::__construct();
$this->_nicename = esc_html__( 'Country', 'ninja-forms' );
$this->_settings[ 'options' ][ 'group' ] = '';
// $this->_settings[ 'options' ][ 'value' ] = $this->get_options();
$this->_settings[ 'default' ] = array(
'name' => 'default',
'type' => 'select',
'label' => esc_html__( 'Default Value', 'ninja-forms' ),
'options' => $this->get_default_value_options(),
'width' => 'one-half',
'group' => 'primary',
'value' => 'US',
);
add_filter( 'ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 2 );
add_filter( 'ninja_forms_render_options_' . $this->_type, array( $this, 'filter_options' ), 10, 2 );
add_filter( 'ninja_forms_subs_export_field_value_' . $this->_name, array( $this, 'filter_csv_value' ), 10, 2 );
}
public function custom_columns( $value, $field )
{
if( $this->_name != $field->get_setting( 'type' ) ) return $value;
foreach( Ninja_Forms()->config( 'CountryList' ) as $country => $abbr ){
if( $value == $abbr ) return $country;
}
return $value;
}
public function filter_options( $options, $settings )
{
$default_value = ( isset( $settings[ 'default' ] ) ) ? $settings[ 'default' ] : '';
$options = $this->get_options(); // Overwrite the default list options.
foreach( $options as $key => $option ){
if( $default_value != $option[ 'value' ] ) continue;
$options[ $key ][ 'selected' ] = 1;
}
usort( $options, array($this,'sort_options_by_label') );
return $options;
}
private function sort_options_by_label( $option_a, $option_b )
{
return strcasecmp( $option_a['label'], $option_b['label'] );
}
public function filter_options_preview( $field_settings )
{
$field_settings[ 'settings' ][ 'options' ] = $this->get_options();
foreach( $field_settings[ 'settings' ][ 'options' ] as $key => $option ){
if( $field_settings[ 'settings' ][ 'default' ] != $option[ 'value' ] ) continue;
$field_settings[ 'settings' ][ 'options' ][ $key ][ 'selected' ] = 1;
}
return $field_settings;
}
public function admin_form_element( $id, $value )
{
$field = Ninja_Forms()->form()->get_field( $id );
$options = $this->get_options();
$options = apply_filters( 'ninja_forms_render_options', $options, $field->get_settings() );
$options = apply_filters( 'ninja_forms_render_options_' . $this->_type, $options, $field->get_settings() );
ob_start();
echo "<select name='fields[$id]'>";
foreach( $options as $option ){
$selected = ( $option['value'] == $value ) ? ' selected' : '';
echo "<option value='" . $option['value'] . "'" . $selected . ">" . $option['label'] . "</option>";
}
echo "</select>";
return ob_get_clean();
}
private function get_default_value_options()
{
$options = array();
// Option to have no default country
$options[] = array(
'label' => '- ' . esc_html__( 'Select Country', 'ninja-forms' ) . ' -',
'value' => ''
);
foreach( Ninja_Forms()->config( 'CountryList' ) as $label => $value ){
$options[] = array(
'label' => $label,
'value' => $value,
);
}
return $options;
}
private function get_options()
{
$order = 0;
$options = array();
// option to have no default country selected
$options[] = array(
'label' => '- ' . esc_html__( 'Select Country', 'ninja-forms' ) . ' -',
'value' => '',
'calc' => '',
'selected' => 0,
'order' => $order,
);
$order++;
foreach( Ninja_Forms()->config( 'CountryList' ) as $label => $value ){
$options[] = array(
'label' => $label,
'value' => $value,
'calc' => '',
'selected' => 0,
'order' => $order
);
$order++;
}
return $options;
}
public function filter_csv_value( $field_value, $field )
{
$lookup = array_flip( Ninja_Forms()->config( 'CountryList' ) );
if( isset( $lookup[ $field_value ] ) ) $field_value = $lookup[ $field_value ];
return $field_value;
}
}