PluginProbe
Easy Hotel – Powerful Hotel Booking / trunk
Easy Hotel – Powerful Hotel Booking vtrunk
2.0.8 2.0.7 2.0.6 2.0.5 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.9.9 1.9.8 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 All 110 releases
easy-hotel / admin / includes / framework / classes / fields.class.php

fields.class.php in Easy Hotel – Powerful Hotel Booking trunk, at admin/includes/framework/classes/fields.class.php

451 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Fields Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'ESHB_Fields' ) ) {
11 abstract class ESHB_Fields extends ESHB_Abstract {
12
13 public function __construct( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
14 $this->field = $field;
15 $this->value = $value;
16 $this->unique = $unique;
17 $this->where = $where;
18 $this->parent = $parent;
19 }
20
21 public function field_name( $nested_name = '' ) {
22
23 $field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
24 $unique_id = ( ! empty( $this->unique ) ) ? $this->unique .'['. $field_id .']' : $field_id;
25 $field_name = ( ! empty( $this->field['name'] ) ) ? $this->field['name'] : $unique_id;
26 $tag_prefix = ( ! empty( $this->field['tag_prefix'] ) ) ? $this->field['tag_prefix'] : '';
27
28 if ( ! empty( $tag_prefix ) ) {
29 $nested_name = str_replace( '[', '['. $tag_prefix, $nested_name );
30 }
31
32 return $field_name . $nested_name;
33
34 }
35
36 public function field_attributes( $custom_atts = array() ) {
37
38 $field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
39 $attributes = ( ! empty( $this->field['attributes'] ) ) ? $this->field['attributes'] : array();
40
41 if ( ! empty( $field_id ) && empty( $attributes['data-depend-id'] ) ) {
42 $attributes['data-depend-id'] = $field_id;
43 }
44
45 if ( ! empty( $this->field['placeholder'] ) ) {
46 $attributes['placeholder'] = $this->field['placeholder'];
47 }
48
49 $attributes = wp_parse_args( $attributes, $custom_atts );
50
51 $atts = '';
52
53 if ( ! empty( $attributes ) ) {
54 foreach ( $attributes as $key => $value ) {
55 if ( $value === 'only-key' ) {
56 $atts .= ' '. esc_attr( $key );
57 } else {
58 $atts .= ' '. esc_attr( $key ) . '='. esc_attr( $value );
59 }
60 }
61 }
62
63 return $atts;
64
65 }
66
67 /**
68 * Same attribute list as field_attributes(), but with the values quoted.
69 *
70 * field_attributes() leaves them bare so that the callers which run its result
71 * through esc_attr() a second time still emit something a browser accepts. That
72 * only holds for single-token values: an empty value makes the parser read the
73 * NEXT attribute as this one's value (so ` data-x= class=y` loses `class`
74 * entirely), and a value with a space is truncated at that space. Callers that
75 * pass user data — an address, a state name — must use this method instead and
76 * echo it without re-escaping, since every key and value is escaped here.
77 *
78 * @param array $custom_atts Defaults merged under the field's own attributes.
79 * @return string Attribute string, each value wrapped in double quotes.
80 */
81 public function field_attributes_html( $custom_atts = array() ) {
82
83 $field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
84 $attributes = ( ! empty( $this->field['attributes'] ) ) ? $this->field['attributes'] : array();
85
86 if ( ! empty( $field_id ) && empty( $attributes['data-depend-id'] ) ) {
87 $attributes['data-depend-id'] = $field_id;
88 }
89
90 if ( ! empty( $this->field['placeholder'] ) ) {
91 $attributes['placeholder'] = $this->field['placeholder'];
92 }
93
94 $attributes = wp_parse_args( $attributes, $custom_atts );
95
96 $atts = '';
97
98 if ( ! empty( $attributes ) ) {
99 foreach ( $attributes as $key => $value ) {
100 if ( $value === 'only-key' ) {
101 $atts .= ' '. esc_attr( $key );
102 } else {
103 $atts .= ' '. esc_attr( $key ) . '="'. esc_attr( $value ) .'"';
104 }
105 }
106 }
107
108 return $atts;
109
110 }
111
112 public function field_before() {
113 return ( ! empty( $this->field['before'] ) ) ? '<div class="csf-before-text">'. $this->field['before'] .'</div>' : '';
114 }
115
116 public function field_after() {
117
118 $output = ( ! empty( $this->field['after'] ) ) ? '<div class="csf-after-text">'. $this->field['after'] .'</div>' : '';
119 $output .= ( ! empty( $this->field['desc'] ) ) ? '<div class="clear"></div><div class="csf-desc-text">'. $this->field['desc'] .'</div>' : '';
120 $output .= ( ! empty( $this->field['help'] ) ) ? '<div class="csf-help"><span class="csf-help-text">'. $this->field['help'] .'</span><i class="fas fa-question-circle"></i></div>' : '';
121 $output .= ( ! empty( $this->field['_error'] ) ) ? '<div class="csf-error-text">'. $this->field['_error'] .'</div>' : '';
122
123 return $output;
124
125 }
126
127 public static function field_data( $type = '', $term = false, $query_args = array() ) {
128
129 $options = array();
130 $array_search = false;
131
132 // sanitize type name
133 if ( in_array( $type, array( 'page', 'pages' ) ) ) {
134 $option = 'page';
135 } else if ( in_array( $type, array( 'post', 'posts' ) ) ) {
136 $option = 'post';
137 } else if ( in_array( $type, array( 'category', 'categories' ) ) ) {
138 $option = 'category';
139 } else if ( in_array( $type, array( 'tag', 'tags' ) ) ) {
140 $option = 'post_tag';
141 } else if ( in_array( $type, array( 'menu', 'menus' ) ) ) {
142 $option = 'nav_menu';
143 } else {
144 $option = '';
145 }
146
147 // switch type
148 switch( $type ) {
149
150 case 'page':
151 case 'pages':
152 case 'post':
153 case 'posts':
154
155 // term query required for ajax select
156 if ( ! empty( $term ) ) {
157
158 $query = new WP_Query( wp_parse_args( $query_args, array(
159 's' => $term,
160 'post_type' => $option,
161 'post_status' => 'publish',
162 'posts_per_page' => 25,
163 ) ) );
164
165 } else {
166
167 $query = new WP_Query( wp_parse_args( $query_args, array(
168 'post_type' => $option,
169 'post_status' => 'publish',
170 ) ) );
171
172 }
173
174 if ( ! is_wp_error( $query ) && ! empty( $query->posts ) ) {
175 foreach ( $query->posts as $item ) {
176 $options[$item->ID] = $item->post_title;
177 }
178 }
179
180 break;
181
182 case 'category':
183 case 'categories':
184 case 'tag':
185 case 'tags':
186 case 'menu':
187 case 'menus':
188
189 if ( ! empty( $term ) ) {
190
191 $query = new WP_Term_Query( wp_parse_args( $query_args, array(
192 'search' => $term,
193 'taxonomy' => $option,
194 'hide_empty' => false,
195 'number' => 25,
196 ) ) );
197
198 } else {
199
200 $query = new WP_Term_Query( wp_parse_args( $query_args, array(
201 'taxonomy' => $option,
202 'hide_empty' => false,
203 ) ) );
204
205 }
206
207 if ( ! is_wp_error( $query ) && ! empty( $query->terms ) ) {
208 foreach ( $query->terms as $item ) {
209 $options[$item->term_id] = $item->name;
210 }
211 }
212
213 break;
214
215 case 'user':
216 case 'users':
217
218 if ( ! empty( $term ) ) {
219
220 $query = new WP_User_Query( array(
221 'search' => '*'. $term .'*',
222 'number' => 25,
223 'orderby' => 'title',
224 'order' => 'ASC',
225 'fields' => array( 'display_name', 'ID' )
226 ) );
227
228 } else {
229
230 $query = new WP_User_Query( array( 'fields' => array( 'display_name', 'ID' ) ) );
231
232 }
233
234 if ( ! is_wp_error( $query ) && ! empty( $query->get_results() ) ) {
235 foreach ( $query->get_results() as $item ) {
236 $options[$item->ID] = $item->display_name;
237 }
238 }
239
240 break;
241
242 case 'sidebar':
243 case 'sidebars':
244
245 global $wp_registered_sidebars;
246
247 if ( ! empty( $wp_registered_sidebars ) ) {
248 foreach ( $wp_registered_sidebars as $sidebar ) {
249 $options[$sidebar['id']] = $sidebar['name'];
250 }
251 }
252
253 $array_search = true;
254
255 break;
256
257 case 'role':
258 case 'roles':
259
260 global $wp_roles;
261
262 if ( ! empty( $wp_roles ) ) {
263 if ( ! empty( $wp_roles->roles ) ) {
264 foreach ( $wp_roles->roles as $role_key => $role_value ) {
265 $options[$role_key] = $role_value['name'];
266 }
267 }
268 }
269
270 $array_search = true;
271
272 break;
273
274 case 'post_type':
275 case 'post_types':
276
277 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
278
279 if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) ) {
280 foreach ( $post_types as $post_type ) {
281 $options[$post_type->name] = $post_type->labels->name;
282 }
283 }
284
285 $array_search = true;
286
287 break;
288
289 case 'location':
290 case 'locations':
291
292 $nav_menus = get_registered_nav_menus();
293
294 if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) ) {
295 foreach ( $nav_menus as $nav_menu_key => $nav_menu_name ) {
296 $options[$nav_menu_key] = $nav_menu_name;
297 }
298 }
299
300 $array_search = true;
301
302 break;
303
304 default:
305
306 if ( is_callable( $type ) ) {
307 if ( ! empty( $term ) ) {
308 $options = call_user_func( $type, $query_args );
309 } else {
310 $options = call_user_func( $type, $term, $query_args );
311 }
312 }
313
314 break;
315
316 }
317
318 // Array search by "term"
319 if ( ! empty( $term ) && ! empty( $options ) && ! empty( $array_search ) ) {
320 $options = preg_grep( '/'. $term .'/i', $options );
321 }
322
323 // Make multidimensional array for ajax search
324 if ( ! empty( $term ) && ! empty( $options ) ) {
325 $arr = array();
326 foreach ( $options as $option_key => $option_value ) {
327 $arr[] = array( 'value' => $option_key, 'text' => $option_value );
328 }
329 $options = $arr;
330 }
331
332 return $options;
333
334 }
335
336 public function field_wp_query_data_title( $type, $values ) {
337
338 $options = array();
339
340 if ( ! empty( $values ) && is_array( $values ) ) {
341
342 foreach ( $values as $value ) {
343
344 $options[$value] = ucfirst( $value );
345
346 switch( $type ) {
347
348 case 'post':
349 case 'posts':
350 case 'page':
351 case 'pages':
352
353 $title = get_the_title( $value );
354
355 if ( ! is_wp_error( $title ) && ! empty( $title ) ) {
356 $options[$value] = $title;
357 }
358
359 break;
360
361 case 'category':
362 case 'categories':
363 case 'tag':
364 case 'tags':
365 case 'menu':
366 case 'menus':
367
368 $term = get_term( $value );
369
370 if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
371 $options[$value] = $term->name;
372 }
373
374 break;
375
376 case 'user':
377 case 'users':
378
379 $user = get_user_by( 'id', $value );
380
381 if ( ! is_wp_error( $user ) && ! empty( $user ) ) {
382 $options[$value] = $user->display_name;
383 }
384
385 break;
386
387 case 'sidebar':
388 case 'sidebars':
389
390 global $wp_registered_sidebars;
391
392 if ( ! empty( $wp_registered_sidebars[$value] ) ) {
393 $options[$value] = $wp_registered_sidebars[$value]['name'];
394 }
395
396 break;
397
398 case 'role':
399 case 'roles':
400
401 global $wp_roles;
402
403 if ( ! empty( $wp_roles ) && ! empty( $wp_roles->roles ) && ! empty( $wp_roles->roles[$value] ) ) {
404 $options[$value] = $wp_roles->roles[$value]['name'];
405 }
406
407 break;
408
409 case 'post_type':
410 case 'post_types':
411
412 $post_types = get_post_types( array( 'show_in_nav_menus' => true ) );
413
414 if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) && ! empty( $post_types[$value] ) ) {
415 $options[$value] = ucfirst( $value );
416 }
417
418 break;
419
420 case 'location':
421 case 'locations':
422
423 $nav_menus = get_registered_nav_menus();
424
425 if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) && ! empty( $nav_menus[$value] ) ) {
426 $options[$value] = $nav_menus[$value];
427 }
428
429 break;
430
431 default:
432
433 if ( is_callable( $type .'_title' ) ) {
434 $options[$value] = call_user_func( $type .'_title', $value );
435 }
436
437 break;
438
439 }
440
441 }
442
443 }
444
445 return $options;
446
447 }
448
449 }
450 }
451