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