← All changes
|
Libs/adminify-framework/classes/fields.class.php
+302
-323
4.2.17
→
3.1.9
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { | |
| 2 | + die; } // Cannot access directly. | |
| 2 | 3 | /** |
| 3 | 4 | * |
| 4 | 5 | * Fields Class |
| 5 | 6 | * |
| @@ -4,402 +5,380 @@ | ||
| 4 | 5 | * Fields Class |
| 5 | 6 | * |
| 6 | 7 | * @since 1.0.0 |
| 7 | 8 | * @version 1.0.0 |
| 8 | - * | |
| 9 | 9 | */ |
| 10 | 10 | if ( ! class_exists( 'ADMINIFY_Fields' ) ) { |
| 11 | - abstract class ADMINIFY_Fields extends ADMINIFY_Abstract { | |
| 11 | + abstract class ADMINIFY_Fields extends ADMINIFY_Abstract { | |
| 12 | 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 | - } | |
| 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 | 20 | |
| 21 | - public function field_name( $nested_name = '' ) { | |
| 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'] : ''; | |
| 22 | 26 | |
| 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 | + if ( ! empty( $tag_prefix ) ) { | |
| 28 | + $nested_name = str_replace( '[', '[' . $tag_prefix, $nested_name ); | |
| 29 | + } | |
| 27 | 30 | |
| 28 | - if ( ! empty( $tag_prefix ) ) { | |
| 29 | - $nested_name = str_replace( '[', '['. $tag_prefix, $nested_name ); | |
| 30 | - } | |
| 31 | + return $field_name . $nested_name; | |
| 32 | + } | |
| 31 | 33 | |
| 32 | - return $field_name . $nested_name; | |
| 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'] : []; | |
| 33 | 37 | |
| 34 | - } | |
| 38 | + if ( ! empty( $field_id ) && empty( $attributes['data-depend-id'] ) ) { | |
| 39 | + $attributes['data-depend-id'] = $field_id; | |
| 40 | + } | |
| 35 | 41 | |
| 36 | - public function field_attributes( $custom_atts = array() ) { | |
| 42 | + if ( ! empty( $this->field['placeholder'] ) ) { | |
| 43 | + $attributes['placeholder'] = $this->field['placeholder']; | |
| 44 | + } | |
| 37 | 45 | |
| 38 | - $field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : ''; | |
| 39 | - $attributes = ( ! empty( $this->field['attributes'] ) ) ? $this->field['attributes'] : array(); | |
| 46 | + $attributes = wp_parse_args( $attributes, $custom_atts ); | |
| 40 | 47 | |
| 41 | - if ( ! empty( $field_id ) && empty( $attributes['data-depend-id'] ) ) { | |
| 42 | - $attributes['data-depend-id'] = $field_id; | |
| 43 | - } | |
| 48 | + $atts = ''; | |
| 44 | 49 | |
| 45 | - if ( ! empty( $this->field['placeholder'] ) ) { | |
| 46 | - $attributes['placeholder'] = $this->field['placeholder']; | |
| 47 | - } | |
| 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 | + } | |
| 48 | 59 | |
| 49 | - $attributes = wp_parse_args( $attributes, $custom_atts ); | |
| 60 | + return $atts; | |
| 61 | + } | |
| 50 | 62 | |
| 51 | - $atts = ''; | |
| 63 | + public function field_before() { | |
| 64 | + return ( ! empty( $this->field['before'] ) ) ? '<div class="adminify-before-text">' . $this->field['before'] . '</div>' : ''; | |
| 65 | + } | |
| 52 | 66 | |
| 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 | - } | |
| 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>' : ''; | |
| 62 | 72 | |
| 63 | - return $atts; | |
| 73 | + return $output; | |
| 74 | + } | |
| 64 | 75 | |
| 65 | - } | |
| 76 | + public static function field_data( $type = '', $term = false, $query_args = [] ) { | |
| 77 | + $options = []; | |
| 78 | + $array_search = false; | |
| 66 | 79 | |
| 67 | - public function field_before() { | |
| 68 | - return ( ! empty( $this->field['before'] ) ) ? '<div class="adminify-before-text">'. $this->field['before'] .'</div>' : ''; | |
| 69 | - } | |
| 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 | + } | |
| 70 | 94 | |
| 71 | - public function field_after() { | |
| 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 | + } | |
| 72 | 125 | |
| 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>' : ''; | |
| 126 | + if ( ! is_wp_error( $query ) && ! empty( $query->posts ) ) { | |
| 127 | + foreach ( $query->posts as $item ) { | |
| 128 | + $options[ $item->ID ] = $item->post_title; | |
| 129 | + } | |
| 130 | + } | |
| 77 | 131 | |
| 78 | - return $output; | |
| 132 | + break; | |
| 79 | 133 | |
| 80 | - } | |
| 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 | + } | |
| 81 | 163 | |
| 82 | - public static function field_data( $type = '', $term = false, $query_args = array() ) { | |
| 164 | + if ( ! is_wp_error( $query ) && ! empty( $query->terms ) ) { | |
| 165 | + foreach ( $query->terms as $item ) { | |
| 166 | + $options[ $item->term_id ] = $item->name; | |
| 167 | + } | |
| 168 | + } | |
| 83 | 169 | |
| 84 | - $options = array(); | |
| 85 | - $array_search = false; | |
| 170 | + break; | |
| 86 | 171 | |
| 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 | - } | |
| 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 | + } | |
| 101 | 187 | |
| 102 | - // switch type | |
| 103 | - switch( $type ) { | |
| 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 | + } | |
| 104 | 193 | |
| 105 | - case 'page': | |
| 106 | - case 'pages': | |
| 107 | - case 'post': | |
| 108 | - case 'posts': | |
| 194 | + break; | |
| 109 | 195 | |
| 110 | - // term query required for ajax select | |
| 111 | - if ( ! empty( $term ) ) { | |
| 196 | + case 'sidebar': | |
| 197 | + case 'sidebars': | |
| 198 | + global $wp_registered_sidebars; | |
| 112 | 199 | |
| 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 | - ) ) ); | |
| 200 | + if ( ! empty( $wp_registered_sidebars ) ) { | |
| 201 | + foreach ( $wp_registered_sidebars as $sidebar ) { | |
| 202 | + $options[ $sidebar['id'] ] = $sidebar['name']; | |
| 203 | + } | |
| 204 | + } | |
| 119 | 205 | |
| 120 | - } else { | |
| 206 | + $array_search = true; | |
| 121 | 207 | |
| 122 | - $query = new WP_Query( wp_parse_args( $query_args, array( | |
| 123 | - 'post_type' => $option, | |
| 124 | - 'post_status' => 'publish', | |
| 125 | - ) ) ); | |
| 208 | + break; | |
| 126 | 209 | |
| 127 | - } | |
| 210 | + case 'role': | |
| 211 | + case 'roles': | |
| 212 | + global $wp_roles; | |
| 128 | 213 | |
| 129 | - if ( ! is_wp_error( $query ) && ! empty( $query->posts ) ) { | |
| 130 | - foreach ( $query->posts as $item ) { | |
| 131 | - $options[$item->ID] = $item->post_title; | |
| 132 | - } | |
| 133 | - } | |
| 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 | + } | |
| 134 | 221 | |
| 135 | - break; | |
| 222 | + $array_search = true; | |
| 136 | 223 | |
| 137 | - case 'category': | |
| 138 | - case 'categories': | |
| 139 | - case 'tag': | |
| 140 | - case 'tags': | |
| 141 | - case 'menu': | |
| 142 | - case 'menus': | |
| 224 | + break; | |
| 143 | 225 | |
| 144 | - if ( ! empty( $term ) ) { | |
| 226 | + case 'post_type': | |
| 227 | + case 'post_types': | |
| 228 | + $post_types = get_post_types( [ 'show_in_nav_menus' => true ], 'objects' ); | |
| 145 | 229 | |
| 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 | - ) ) ); | |
| 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 | + } | |
| 152 | 235 | |
| 153 | - } else { | |
| 236 | + $array_search = true; | |
| 154 | 237 | |
| 155 | - $query = new WP_Term_Query( wp_parse_args( $query_args, array( | |
| 156 | - 'taxonomy' => $option, | |
| 157 | - 'hide_empty' => false, | |
| 158 | - ) ) ); | |
| 238 | + break; | |
| 159 | 239 | |
| 160 | - } | |
| 240 | + case 'location': | |
| 241 | + case 'locations': | |
| 242 | + $nav_menus = get_registered_nav_menus(); | |
| 161 | 243 | |
| 162 | - if ( ! is_wp_error( $query ) && ! empty( $query->terms ) ) { | |
| 163 | - foreach ( $query->terms as $item ) { | |
| 164 | - $options[$item->term_id] = $item->name; | |
| 165 | - } | |
| 166 | - } | |
| 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 | + } | |
| 167 | 249 | |
| 168 | - break; | |
| 250 | + $array_search = true; | |
| 169 | 251 | |
| 170 | - case 'user': | |
| 171 | - case 'users': | |
| 252 | + break; | |
| 172 | 253 | |
| 173 | - if ( ! empty( $term ) ) { | |
| 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 | + } | |
| 174 | 262 | |
| 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 | - ) ); | |
| 263 | + break; | |
| 264 | + } | |
| 182 | 265 | |
| 183 | - } else { | |
| 266 | + // Array search by "term" | |
| 267 | + if ( ! empty( $term ) && ! empty( $options ) && ! empty( $array_search ) ) { | |
| 268 | + $options = preg_grep( '/' . $term . '/i', $options ); | |
| 269 | + } | |
| 184 | 270 | |
| 185 | - $query = new WP_User_Query( array( 'fields' => array( 'display_name', 'ID' ) ) ); | |
| 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 | + } | |
| 186 | 282 | |
| 187 | - } | |
| 283 | + return $options; | |
| 284 | + } | |
| 188 | 285 | |
| 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 | - } | |
| 286 | + public function field_wp_query_data_title( $type, $values ) { | |
| 287 | + $options = []; | |
| 194 | 288 | |
| 195 | - break; | |
| 289 | + if ( ! empty( $values ) && is_array( $values ) ) { | |
| 290 | + foreach ( $values as $value ) { | |
| 291 | + $options[ $value ] = ucfirst( $value ); | |
| 196 | 292 | |
| 197 | - case 'sidebar': | |
| 198 | - case 'sidebars': | |
| 293 | + switch ( $type ) { | |
| 294 | + case 'post': | |
| 295 | + case 'posts': | |
| 296 | + case 'page': | |
| 297 | + case 'pages': | |
| 298 | + $title = get_the_title( $value ); | |
| 199 | 299 | |
| 200 | - global $wp_registered_sidebars; | |
| 300 | + if ( ! is_wp_error( $title ) && ! empty( $title ) ) { | |
| 301 | + $options[ $value ] = $title; | |
| 302 | + } | |
| 201 | 303 | |
| 202 | - if ( ! empty( $wp_registered_sidebars ) ) { | |
| 203 | - foreach ( $wp_registered_sidebars as $sidebar ) { | |
| 204 | - $options[$sidebar['id']] = $sidebar['name']; | |
| 205 | - } | |
| 206 | - } | |
| 304 | + break; | |
| 207 | 305 | |
| 208 | - $array_search = true; | |
| 306 | + case 'category': | |
| 307 | + case 'categories': | |
| 308 | + case 'tag': | |
| 309 | + case 'tags': | |
| 310 | + case 'menu': | |
| 311 | + case 'menus': | |
| 312 | + $term = get_term( $value ); | |
| 209 | 313 | |
| 210 | - break; | |
| 314 | + if ( ! is_wp_error( $term ) && ! empty( $term ) ) { | |
| 315 | + $options[ $value ] = $term->name; | |
| 316 | + } | |
| 211 | 317 | |
| 212 | - case 'role': | |
| 213 | - case 'roles': | |
| 318 | + break; | |
| 214 | 319 | |
| 215 | - global $wp_roles; | |
| 320 | + case 'user': | |
| 321 | + case 'users': | |
| 322 | + $user = get_user_by( 'id', $value ); | |
| 216 | 323 | |
| 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 | - } | |
| 324 | + if ( ! is_wp_error( $user ) && ! empty( $user ) ) { | |
| 325 | + $options[ $value ] = $user->display_name; | |
| 326 | + } | |
| 224 | 327 | |
| 225 | - $array_search = true; | |
| 328 | + break; | |
| 226 | 329 | |
| 227 | - break; | |
| 330 | + case 'sidebar': | |
| 331 | + case 'sidebars': | |
| 332 | + global $wp_registered_sidebars; | |
| 228 | 333 | |
| 229 | - case 'post_type': | |
| 230 | - case 'post_types': | |
| 334 | + if ( ! empty( $wp_registered_sidebars[ $value ] ) ) { | |
| 335 | + $options[ $value ] = $wp_registered_sidebars[ $value ]['name']; | |
| 336 | + } | |
| 231 | 337 | |
| 232 | - $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); | |
| 338 | + break; | |
| 233 | 339 | |
| 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 | - } | |
| 340 | + case 'role': | |
| 341 | + case 'roles': | |
| 342 | + global $wp_roles; | |
| 239 | 343 | |
| 240 | - $array_search = true; | |
| 344 | + if ( ! empty( $wp_roles ) && ! empty( $wp_roles->roles ) && ! empty( $wp_roles->roles[ $value ] ) ) { | |
| 345 | + $options[ $value ] = $wp_roles->roles[ $value ]['name']; | |
| 346 | + } | |
| 241 | 347 | |
| 242 | - break; | |
| 348 | + break; | |
| 243 | 349 | |
| 244 | - case 'location': | |
| 245 | - case 'locations': | |
| 350 | + case 'post_type': | |
| 351 | + case 'post_types': | |
| 352 | + $post_types = get_post_types( [ 'show_in_nav_menus' => true ] ); | |
| 246 | 353 | |
| 247 | - $nav_menus = get_registered_nav_menus(); | |
| 354 | + if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) && ! empty( $post_types[ $value ] ) ) { | |
| 355 | + $options[ $value ] = ucfirst( $value ); | |
| 356 | + } | |
| 248 | 357 | |
| 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 | - } | |
| 358 | + break; | |
| 254 | 359 | |
| 255 | - $array_search = true; | |
| 360 | + case 'location': | |
| 361 | + case 'locations': | |
| 362 | + $nav_menus = get_registered_nav_menus(); | |
| 256 | 363 | |
| 257 | - break; | |
| 364 | + if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) && ! empty( $nav_menus[ $value ] ) ) { | |
| 365 | + $options[ $value ] = $nav_menus[ $value ]; | |
| 366 | + } | |
| 258 | 367 | |
| 259 | - default: | |
| 368 | + break; | |
| 260 | 369 | |
| 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 | - } | |
| 370 | + default: | |
| 371 | + if ( is_callable( $type . '_title' ) ) { | |
| 372 | + $options[ $value ] = call_user_func( $type . '_title', $value ); | |
| 373 | + } | |
| 268 | 374 | |
| 269 | - break; | |
| 375 | + break; | |
| 376 | + } | |
| 377 | + } | |
| 378 | + } | |
| 270 | 379 | |
| 271 | - } | |
| 380 | + return $options; | |
| 381 | + } | |
| 272 | 382 | |
| 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 | - } | |
| 383 | + } | |
| 405 | 384 | } |