diff --git a/includes/admin/assets/js/um-admin-forms.js b/includes/admin/assets/js/um-admin-forms.js index 5101f89e..2391c021 100644 --- a/includes/admin/assets/js/um-admin-forms.js +++ b/includes/admin/assets/js/um-admin-forms.js @@ -1,5 +1,52 @@ -jQuery(document).ready( function() { +function um_admin_init_users_select() { + if ( jQuery('.um-user-select-field').length ) { + var select2_atts = { + ajax: { + url: wp.ajax.settings.url, + dataType: 'json', + delay: 250, // delay in ms while typing when to perform a AJAX search + data: function( params ) { + return { + search: params.term, // search query + action: 'um_get_users', // AJAX action for admin-ajax.php + page: params.page || 1, // infinite scroll pagination + nonce: um_admin_scripts.nonce + }; + }, + processResults: function( response, params ) { + params.page = params.page || 1; + var options = []; + if ( response.data.users ) { + jQuery.each( response.data.users, function( index, text ) { + options.push( { id: text.ID, text: text.user_login + ' (#' + text.ID + ')' } ); + }); + } + + return { + results: options, + pagination: { + more: ( params.page * 20 ) < response.data.total_count + } + }; + }, + cache: true + }, + minimumInputLength: 0, // the minimum of symbols to input before perform a search + allowClear: true, + width: "100%", + allowHtml: true, + dropdownCssClass: 'um-select2-users-dropdown', + containerCssClass : 'um-select2-users-container' + }; + + jQuery('.um-user-select-field').select2( select2_atts ); + } +} + + +jQuery(document).ready( function() { + um_admin_init_users_select(); /** * Same page upgrade field diff --git a/includes/admin/core/class-admin-ajax-hooks.php b/includes/admin/core/class-admin-ajax-hooks.php index b3972317..23e27f06 100644 --- a/includes/admin/core/class-admin-ajax-hooks.php +++ b/includes/admin/core/class-admin-ajax-hooks.php @@ -30,7 +30,9 @@ if ( ! class_exists( 'um\admin\core\Admin_Ajax_Hooks' ) ) { add_action( 'wp_ajax_um_member_directory_default_filter_settings', array( UM()->member_directory(), 'default_filter_settings' ) ); add_action( 'wp_ajax_um_same_page_update', array( UM()->admin_settings(), 'same_page_update_ajax' ) ); + + add_action( 'wp_ajax_um_get_users', array( UM()->users(), 'get_users' ) ); } } -} \ No newline at end of file +} diff --git a/includes/admin/core/class-admin-forms.php b/includes/admin/core/class-admin-forms.php index fffb49a9..0bb6f9f3 100644 --- a/includes/admin/core/class-admin-forms.php +++ b/includes/admin/core/class-admin-forms.php @@ -548,6 +548,69 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) { } + /** + * @param $field_data + * + * @return bool|string + */ + function render_users_dropdown( $field_data ) { + if ( empty( $field_data['id'] ) ) { + return false; + } + + $multiple = ! empty( $field_data['multi'] ) ? 'multiple' : ''; + + $id = ( ! empty( $this->form_data['prefix_id'] ) ? $this->form_data['prefix_id'] : '' ) . '_' . $field_data['id']; + $id_attr = ' id="' . esc_attr( $id ) . '" '; + + $class = ! empty( $field_data['class'] ) ? $field_data['class'] . ' ' : ' '; + $class .= ! empty( $field_data['size'] ) ? 'um-' . $field_data['size'] . '-field' : 'um-long-field'; + $class_attr = ' class="um-forms-field um-user-select-field' . esc_attr( $class ) . '" '; + + $data = array( + 'field_id' => $field_data['id'], + ); + + $data_attr = ''; + foreach ( $data as $key => $value ) { + $data_attr .= ' data-' . $key . '="' . esc_attr( $value ) . '" '; + } + + $name = $field_data['id']; + $name = ! empty( $this->form_data['prefix_id'] ) ? $this->form_data['prefix_id'] . '[' . $name . ']' : $name; + $hidden_name_attr = ' name="' . $name . '" '; + $name = $name . ( ! empty( $field_data['multi'] ) ? '[]' : '' ); + $name_attr = ' name="' . $name . '" '; + + $value = $this->get_field_value( $field_data ); + + $users = array(); + if ( ! empty( $value ) ) { + $users = get_users( + array( + 'include' => $value, + 'fields' => array( 'ID', 'user_login' ), + ) + ); + } + + $options = ''; + if ( ! empty( $users ) ) { + foreach ( $users as $user ) { + $options .= ''; + } + } + + $hidden = ''; + if ( ! empty( $multiple ) ) { + $hidden = ""; + } + $html = "$hidden"; + + return $html; + } + + /** * @param $field_data * diff --git a/includes/admin/core/class-admin-users.php b/includes/admin/core/class-admin-users.php index c8f4c666..b7a0d2a1 100644 --- a/includes/admin/core/class-admin-users.php +++ b/includes/admin/core/class-admin-users.php @@ -42,6 +42,36 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) { } + function get_users() { + UM()->admin()->check_ajax_nonce(); + + $search_request = ! empty( $_REQUEST['search'] ) ? sanitize_text_field( $_REQUEST['search'] ) : ''; + $page = ! empty( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : 1; + $per_page = 20; + + $args = array( + 'fields' => array( 'ID', 'user_login' ), + 'paged' => $page, + 'number' => $per_page + ); + + if ( ! empty( $search_request ) ) { + $args['search'] = $search_request; + } + + $users_query = new \WP_User_Query( $args ); + $users = $users_query->get_results(); + $total_count = $users_query->get_total(); + + wp_send_json_success( + array( + 'users' => $users, + 'total_count' => $total_count, + ) + ); + } + + /** * Restrict the edit/delete users via wp-admin screen by the UM role capabilities *