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