PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.0
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.0
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.0, at admin/views/sp-framework/classes/fields.class.php

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