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