mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-11 18:56:10 +09:00
- added select2 dropdown with users list;
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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' ) );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 .= '<option value="' . esc_attr( $user->ID ) . '" selected>' . esc_html( $user->user_login ) . '</option>';
|
||||
}
|
||||
}
|
||||
|
||||
$hidden = '';
|
||||
if ( ! empty( $multiple ) ) {
|
||||
$hidden = "<input type=\"hidden\" $hidden_name_attr value=\"\" />";
|
||||
}
|
||||
$html = "$hidden<select $multiple $id_attr $name_attr $class_attr $data_attr><option value=\"\">" . esc_html__( 'Select Users', 'ultimate-member' ) . "</option>$options</select>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $field_data
|
||||
*
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user