PluginProbe
Cool Timeline (Horizontal & Vertical Timeline) / trunk
Cool Timeline (Horizontal & Vertical Timeline) vtrunk
3.4.0 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 2.2.3 2.3.3 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.6.1 2.7.1 2.8.3 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 All 79 releases
cool-timeline / admin / codestar-framework / classes / fields.class.php

fields.class.php in Cool Timeline (Horizontal & Vertical Timeline) trunk, at admin/codestar-framework/classes/fields.class.php

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