Field.php
6.87 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NF_Abstracts_Field
*/
abstract class NF_Abstracts_Field extends NF_Abstracts_Element
{
/**
* @var string
*/
protected $_name = '';
/**
* @var string
*/
protected $_nicename = '';
/**
* @var string
*/
protected $_section = '';
/**
* @var string
*/
protected $_icon = 'square-o';
/**
* @var array
*/
protected $_aliases = array();
/**
* @var array
*/
protected $_settings = array();
/**
* @var array
*/
protected $_settings_all_fields = array();
/**
* @var array
*/
protected $_settings_exclude = array();
/**
* @var array
*/
protected $_settings_only = array();
/**
* @var array
*/
protected $_use_merge_tags = array( 'user', 'post', 'system', 'fields' );
/**
* @var array
*/
protected $_use_merge_tags_include = array();
/**
* @var array
*/
protected $_use_merge_tags_exclude = array();
/**
* @var string
*/
protected $_test_value = 'test';
/**
* @var string
*/
protected $_attr = '';
/**
* @var string
*/
protected $_type = '';
/**
* @var string
*/
protected $_parent_type = '';
/**
* @var string
*/
public static $_base_template = 'input';
/**
* @var array
*/
protected $_templates = array();
/**
* @var string
*/
protected $_wrap_template = 'wrap';
/**
* @var array
*/
protected $_old_classname = '';
/**
* @var bool
*/
protected $_show_in_builder = true;
//-----------------------------------------------------
// Public Methods
//-----------------------------------------------------
/**
* Constructor
*/
public function __construct()
{
if( ! empty( $this->_settings_only ) ){
$this->_settings = array_merge( $this->_settings, $this->_settings_only );
} else {
$this->_settings = array_merge( $this->_settings_all_fields, $this->_settings );
$this->_settings = array_diff( $this->_settings, $this->_settings_exclude );
}
$this->_settings = $this->load_settings( $this->_settings );
$this->_test_value = apply_filters( 'ninja_forms_field_' . $this->_name . '_test_value', $this->_test_value );
add_filter( 'ninja_forms_localize_field_settings_' . $this->_type, array( $this, 'localize_settings' ), 10, 2 );
}
/**
* Validate
*
* @param $field
* @param $data
* @return array $errors
*/
public function validate( $field, $data )
{
$errors = array();
// Required check.
if( is_array( $field[ 'value' ] ) && "repeater" !== $field[ 'type' ] ){
$field[ 'value' ] = implode( '', $field[ 'value' ] );
}
if( isset( $field['required'] ) && 1 == intval( $field['required'] ) ) {
$val = trim( $field['value'] );
if( empty( $val ) && '0' !== $val ){
$errors['slug'] = 'required-error';
$errors['message'] = esc_html__('This field is required.', 'ninja-forms');
}
}
return $errors;
}
public function process( $field, $data )
{
return $data;
}
/**
* Admin Form Element
*
* Returns the output for editing fields in a submission.
*
* @param $id
* @param $value
* @return string
*/
public function admin_form_element( $id, $value )
{
return '<input class="widefat" name="fields[' . absint( $id ) . ']" value="' . esc_attr( $value ) . '" type="text" />';
}
public function get_name()
{
return $this->_name;
}
public function get_nicename()
{
return $this->_nicename;
}
public function get_section()
{
return $this->_section;
}
public function get_icon()
{
return $this->_icon;
}
public function get_aliases()
{
return $this->_aliases;
}
public function get_type()
{
return $this->_type;
}
public function get_parent_type()
{
if( $this->_parent_type ){
return $this->_parent_type;
}
// If a type is not set, return 'textbox'
return ( get_parent_class() && isset ( parent::$_type ) ) ? parent::$_type : 'textbox';
}
public function get_settings()
{
return $this->_settings;
}
public function use_merge_tags()
{
$use_merge_tags = array_merge( $this->_use_merge_tags, $this->_use_merge_tags_include );
$use_merge_tags = array_diff( $use_merge_tags, $this->_use_merge_tags_exclude );
return $use_merge_tags;
}
public function get_test_value()
{
return $this->_test_value;
}
public function get_templates()
{
$templates = (array) $this->_templates;
// Create a reflection for examining the parent
$reflection = new ReflectionClass( $this );
$parent_class = $reflection->getParentClass();
if ( $parent_class->isAbstract() ) {
$parent_class_name = $parent_class->getName();
$parent_templates = call_user_func( array( $parent_class_name, 'get_base_template' ) ); // Parent Class' Static Property
return array_merge( $templates, (array) $parent_templates );
}
$parent_class_name = strtolower( str_replace('NF_Fields_', '', $parent_class->getName() ) );
if( ! isset( Ninja_Forms()->fields[ $parent_class_name ] ) ) return $templates;
$parent = Ninja_Forms()->fields[ $parent_class_name ];
return array_merge($templates, $parent->get_templates());
}
public function get_wrap_template()
{
return $this->_wrap_template;
}
public function get_old_classname()
{
return $this->_old_classname;
}
public function show_in_builder() {
return $this->_show_in_builder;
}
protected function load_settings( $only_settings = array() )
{
$settings = array();
// Loads a settings array from the FieldSettings configuration file.
$all_settings = Ninja_Forms::config( 'FieldSettings' );
foreach( $only_settings as $setting ){
if( isset( $all_settings[ $setting ]) ){
$settings[ $setting ] = $all_settings[ $setting ];
}
}
return $settings = apply_filters( 'ninja_forms_field_load_settings', $settings, $this->_name, $this->get_parent_type() );
}
public static function get_base_template()
{
return self::$_base_template;
}
public static function sort_by_order( $a, $b )
{
return $a->get_setting( 'order' ) - $b->get_setting( 'order' );
}
public function localize_settings( $settings, $form_id ) {
return $settings;
}
}