This repository has been archived by the owner on Nov 25, 2017. It is now read-only.
/
Base.php
255 lines (228 loc) · 6.41 KB
/
Base.php
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
<?php
/**
* WPDLib\FieldTypes\Base class
*
* @package WPDLib
* @subpackage FieldTypes
* @author Felix Arntz <felix-arntz@leaves-and-love.net>
* @since 0.5.0
*/
namespace WPDLib\FieldTypes;
use WPDLib\FieldTypes\Manager as FieldManager;
if ( ! defined( 'ABSPATH' ) ) {
die();
}
if ( ! class_exists( 'WPDLib\FieldTypes\Base' ) ) {
/**
* The base class for all field types.
*
* Every field type that does not have its own class will be instantiated with this class.
*
* @since 0.5.0
*/
class Base {
/**
* @since 0.5.0
* @var string Holds the field type.
*/
protected $type = '';
/**
* @since 0.5.0
* @var array Holds the field type arguments.
*/
protected $args = array();
/**
* @since 0.5.3
* @var array Holds the field's data attributes (if any).
*/
protected $data_atts = array();
/**
* @since 0.5.0
* @var string Stores whether assets for this type have been enqueued yet.
*/
private static $enqueued = array();
/**
* Class constructor.
*
* For an overview of the supported arguments, please read the Field Types Reference.
*
* @since 0.5.0
* @param string $type the field type
* @param array $args array of field type arguments
*/
public function __construct( $type, $args ) {
$this->type = $type;
$this->args = wp_parse_args( $args, array(
'id' => '',
'name' => '',
'class' => '',
'placeholder' => '',
'required' => false,
'readonly' => false,
'disabled' => false,
) );
if ( isset( $this->args['label'] ) ) {
unset( $this->args['label'] );
}
if ( strpos( $this->args['class'], 'wpdlib-input' ) === false ) {
if ( ! empty( $this->args['class'] ) ) {
$this->args['class'] .= ' ';
}
$this->args['class'] .= 'wpdlib-input';
}
if ( strpos( $this->args['class'], 'wpdlib-input-' . $this->type ) === false ) {
$this->args['class'] .= ' wpdlib-input-' . $this->type;
}
// create data attribuets from arguments
foreach ( array_keys( $this->args ) as $key ) {
if ( 0 !== strpos( $key, 'data-' ) ) {
continue;
}
$this->data_atts[ $key ] = $this->args[ $key ];
unset( $this->args[ $key ] );
}
}
/**
* Magic set method.
*
* This function provides direct access to the field type arguments.
*
* Note that only 'id' and 'name' can be set.
* Other arguments are read-only.
*
* @since 0.5.0
* @param string $property name of the property to get
* @param mixed $value new value for the property
*/
public function __set( $property, $value ) {
if ( in_array( $property, array( 'id', 'name' ) ) ) {
$this->args[ $property ] = $value;
} elseif ( 0 === strpos( $property, 'data-' ) ) {
$this->data_atts[ $property ] = $value;
}
}
/**
* Magic get method.
*
* This function provides direct access to the field type arguments.
*
* @since 0.5.0
* @param string $property name of the property to get
* @return mixed value of the property or null if it does not exist
*/
public function __get( $property ) {
if ( property_exists( $this, $property ) ) {
return $this->$property;
} elseif ( isset( $this->args[ $property ] ) ) {
return $this->args[ $property ];
} elseif ( isset( $this->data_atts[ $property ] ) ) {
return $this->data_atts[ $property ];
}
return null;
}
/**
* Magic isset method.
*
* This function provides direct access to the field type arguments.
*
* @since 0.5.0
* @param string $property name of the property to check
* @return bool true if the property exists, otherwise false
*/
public function __isset( $property ) {
if ( property_exists( $this, $property ) ) {
return true;
} elseif ( isset( $this->args[ $property ] ) ) {
return true;
} elseif ( isset( $this->data_atts[ $property ] ) ) {
return true;
}
return false;
}
/**
* Displays the input control for the field.
*
* @since 0.5.0
* @param string $val the current value of the field
* @param bool $echo whether to echo the output (default is true)
* @return string the HTML output of the field control
*/
public function display( $val, $echo = true ) {
$args = $this->args;
$args['value'] = $val;
if ( isset( $args['rows'] ) ) {
unset( $args['rows'] );
}
$args = array_merge( $args, $this->data_atts );
$output = '<input type="' . $this->type . '"' . FieldManager::make_html_attributes( $args, false, false ) . ' />';
if ( $echo ) {
echo $output;
}
return $output;
}
/**
* Validates a value for the field.
*
* @since 0.5.0
* @param mixed $val the current value of the field
* @return string the validated field value
*/
public function validate( $val = null ) {
if ( ! $val ) {
return '';
}
return FieldManager::format( $val, 'string', 'input' );
}
/**
* Checks whether a value for the field is considered empty.
*
* This function is needed to check whether a required field has been properly filled.
*
* @since 0.5.0
* @param string $val the current value of the field
* @return bool whether the value is considered empty
*/
public function is_empty( $val ) {
return empty( $val );
}
/**
* Parses a value for the field.
*
* @since 0.5.0
* @param mixed $val the current value of the field
* @param bool|array $formatted whether to also format the value (default is false)
* @return string the correctly parsed value
*/
public function parse( $val, $formatted = false ) {
if ( $formatted ) {
return FieldManager::format( $val, 'string', 'output' );
}
return FieldManager::format( $val, 'string', 'input' );
}
/**
* Enqueues required assets for the field type.
*
* The function also generates script vars to be applied in `wp_localize_script()`.
*
* @since 0.5.0
* @return array array which can (possibly) contain a 'dependencies' array and a 'script_vars' array
*/
public function enqueue_assets() {
return array();
}
/**
* Checks whether the assets of a field type of a specific class have been enqueued yet.
*
* @since 0.5.0
* @param string $class the class name to check for its assets
* @return bool whether the assets for the class have been enqueued yet
*/
protected static function is_enqueued( $class ) {
if ( ! in_array( $class, self::$enqueued ) ) {
self::$enqueued[] = $class;
return false;
}
return true;
}
}
}