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