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