PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.21
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.21
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / views / sp-framework / classes / fields.class.php

fields.class.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 2.4.21, at admin/views/sp-framework/classes/fields.class.php

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