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

410 lines 11.5 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
174 case 'category':
175 case 'categories':
176 case 'tag':
177 case 'tags':
178 case 'menu':
179 case 'menus':
180 if ( ! empty( $term ) ) {
181 $query = new WP_Term_Query(
182 wp_parse_args(
183 $query_args,
184 array(
185 'search' => $term,
186 'taxonomy' => $option,
187 'hide_empty' => false,
188 'number' => 25,
189 )
190 )
191 );
192 } else {
193 $query = new WP_Term_Query(
194 wp_parse_args(
195 $query_args,
196 array(
197 'taxonomy' => $option,
198 'hide_empty' => false,
199 )
200 )
201 );
202 }
203 if ( ! is_wp_error( $query ) && ! empty( $query->terms ) ) {
204 foreach ( $query->terms as $item ) {
205 $options[ $item->term_id ] = $item->name;
206 }
207 }
208
209 break;
210
211 case 'user':
212 case 'users':
213 if ( ! empty( $term ) ) {
214 $query = new WP_User_Query(
215 array(
216 'search' => '*' . $term . '*',
217 'number' => 25,
218 'orderby' => 'title',
219 'order' => 'ASC',
220 'fields' => array( 'display_name', 'ID' ),
221 )
222 );
223 } else {
224 $query = new WP_User_Query( array( 'fields' => array( 'display_name', 'ID' ) ) );
225 }
226 if ( ! is_wp_error( $query ) && ! empty( $query->get_results() ) ) {
227 foreach ( $query->get_results() as $item ) {
228 $options[ $item->ID ] = $item->display_name;
229 }
230 }
231
232 break;
233
234 case 'role':
235 case 'roles':
236 global $wp_roles;
237 if ( ! empty( $wp_roles ) ) {
238 if ( ! empty( $wp_roles->roles ) ) {
239 foreach ( $wp_roles->roles as $role_key => $role_value ) {
240 $options[ $role_key ] = $role_value['name'];
241 }
242 }
243 }
244 $array_search = true;
245
246 break;
247
248 case 'post_type':
249 case 'post_types':
250 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
251 if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) ) {
252 foreach ( $post_types as $post_type ) {
253 $options[ $post_type->name ] = $post_type->labels->name;
254 }
255 }
256 $array_search = true;
257
258 break;
259
260 case 'taxonomies':
261 case 'taxonomy':
262 global $post;
263 $view_options = get_post_meta( $post->ID, 'sp_pcp_view_options', true );
264 $pcp_post_types = isset( $view_options['pcp_select_post_type'] ) && ! empty( $view_options['pcp_select_post_type'] ) ? $view_options['pcp_select_post_type'] : 'post';
265 $taxonomy_names = get_object_taxonomies( $pcp_post_types, 'names' );
266 if ( ! is_wp_error( $taxonomy_names ) && ! empty( $taxonomy_names ) ) {
267 $options[''] = __( 'Select Taxonomy', 'smart-post-show' );
268 foreach ( $taxonomy_names as $taxonomy => $label ) {
269 $options[ $label ] = $label;
270 }
271 }
272 break;
273
274 case 'terms':
275 case 'term':
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'] : get_post_types( array(), 'names' );
279
280 $field_index = preg_replace( '/[^0-9]/', '', $field_unique );
281 $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' );
282 if ( version_compare( get_bloginfo( 'version' ), '4.5', '>=' ) ) {
283 $terms = get_terms( array( 'taxonomy' => $pcp_taxonomy ) );
284 } else {
285 $terms = get_terms( array( $pcp_taxonomy ) );
286 }
287
288 if ( ! is_wp_error( $terms ) && ! empty( $terms ) && ! isset( $terms['errors'] ) ) {
289 foreach ( $terms as $key => $value ) {
290 $options[ $value->term_id ] = $value->name;
291 }
292 }
293
294 break;
295
296 case 'post_status':
297 case 'post_statuses':
298 $statuses = get_post_stati( null, 'objects' );
299 foreach ( $statuses as $status => $object ) {
300 $options[ $status ] = ucfirst( $object->label );
301 }
302
303 break;
304
305 default:
306 if ( function_exists( $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 break;
314 }
315 // Array search by "term".
316 if ( ! empty( $term ) && ! empty( $options ) && ! empty( $array_search ) ) {
317 $options = preg_grep( '/' . $term . '/i', $options );
318 }
319 // Make multidimensional array for ajax search.
320 if ( ! empty( $term ) && ! empty( $options ) ) {
321 $arr = array();
322 foreach ( $options as $option_key => $option_value ) {
323 $arr[] = array(
324 'value' => $option_key,
325 'text' => $option_value,
326 );
327 }
328 $options = $arr;
329 }
330 return $options;
331 }
332
333 /**
334 * WP Query data title
335 *
336 * @param string $type The field option type.
337 * @param array $values Values of the field.
338 * @since 2.1.1
339 * @return statement
340 */
341 public function field_wp_query_data_title( $type, $values ) {
342 $options = array();
343 if ( ! empty( $values ) && is_array( $values ) ) {
344 foreach ( $values as $value ) {
345 switch ( $type ) {
346 case 'post':
347 case 'posts':
348 case 'page':
349 case 'pages':
350 $title = get_the_title( $value );
351 if ( ! is_wp_error( $title ) && ! empty( $title ) ) {
352 $options[ $value ] = $title;
353 }
354 break;
355 case 'category':
356 case 'categories':
357 case 'tag':
358 case 'tags':
359 case 'menu':
360 case 'menus':
361 $term = get_term( $value );
362 if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
363 $options[ $value ] = $term->name;
364 }
365 break;
366 case 'user':
367 case 'users':
368 $user = get_user_by( 'id', $value );
369 if ( ! is_wp_error( $user ) && ! empty( $user ) ) {
370 $options[ $value ] = $user->display_name;
371 }
372 break;
373 case 'sidebar':
374 case 'sidebars':
375 global $wp_registered_sidebars;
376 if ( ! empty( $wp_registered_sidebars[ $value ] ) ) {
377 $options[ $value ] = $wp_registered_sidebars[ $value ]['name'];
378 }
379 break;
380 case 'role':
381 case 'roles':
382 global $wp_roles;
383 if ( ! empty( $wp_roles ) && ! empty( $wp_roles->roles ) && ! empty( $wp_roles->roles[ $value ] ) ) {
384 $options[ $value ] = $wp_roles->roles[ $value ]['name'];
385 }
386 break;
387 case 'post_type':
388 case 'post_types':
389 $post_types = get_post_types( array( 'show_in_nav_menus' => true ) );
390 if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) && ! empty( $post_types[ $value ] ) ) {
391 $options[ $value ] = ucfirst( $value );
392 }
393 break;
394
395 default:
396 if ( function_exists( $type . '_title' ) ) {
397 $options[ $value ] = call_user_func( $type . '_title', $value );
398 } else {
399 $options[ $value ] = ucfirst( $value );
400 }
401 break;
402 }
403 }
404 }
405 return $options;
406 }
407
408 }
409 }
410