PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / trunk
Forumax – AI Powered Advanced Community Forum Plugin vtrunk
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / lib / csf / classes / fields.class.php

fields.class.php in Forumax – AI Powered Advanced Community Forum Plugin trunk, at lib/csf/classes/fields.class.php

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