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