mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-06-05 15:09:37 +09:00
- added users dropdown field. Closed #1034
This commit is contained in:
@@ -1,17 +1,48 @@
|
||||
function um_admin_init_users_select() {
|
||||
if ( jQuery('.um-user-select-field').length ) {
|
||||
function avatarformat( data ) {
|
||||
var option;
|
||||
if ( ! data.id ) {
|
||||
return data.text;
|
||||
}
|
||||
if ( 'undefined' !== typeof data.img ) {
|
||||
option = jQuery('<span><img style="vertical-align: sub; width: 20px; height: 20px;" src="' + data.img + '" /> ' + data.text + '</span>');
|
||||
} else {
|
||||
var img = data.element.attributes['data-img']['value'];
|
||||
if ( img ) {
|
||||
option = jQuery('<img style="vertical-align: sub; width: 20px; height: 20px;" src="' + img + '" /> ' + data.text + '</span>');
|
||||
} else {
|
||||
option = jQuery('<span>' + data.text + '</span>');
|
||||
}
|
||||
}
|
||||
return option;
|
||||
}
|
||||
|
||||
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
|
||||
var args = {
|
||||
action: 'um_get_users', // AJAX action for admin-ajax.php
|
||||
search: params.term, // search query
|
||||
page: params.page || 1, // infinite scroll pagination
|
||||
nonce: um_admin_scripts.nonce
|
||||
};
|
||||
|
||||
jQuery.each( jQuery(this)[0].attributes, function() {
|
||||
// this.attributes is not a plain object, but an array
|
||||
// of attribute nodes, which contain both the name and value
|
||||
if ( this.specified ) {
|
||||
if ( -1 !== this.name.indexOf( 'data-ajax-args-' ) ) {
|
||||
var arg_name = this.name.replace( 'data-ajax-args-', '' ).trim();
|
||||
args[ arg_name ] = this.value;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return args;
|
||||
},
|
||||
processResults: function( response, params ) {
|
||||
params.page = params.page || 1;
|
||||
@@ -19,7 +50,11 @@ function um_admin_init_users_select() {
|
||||
|
||||
if ( response.data.users ) {
|
||||
jQuery.each( response.data.users, function( index, text ) {
|
||||
options.push( { id: text.ID, text: text.user_login + ' (#' + text.ID + ')' } );
|
||||
if ( typeof text.img !== 'undefined' ) {
|
||||
options.push({ id: text.ID, text: text.user_login + ' (#' + text.ID + ')', img: text.img });
|
||||
} else {
|
||||
options.push( { id: text.ID, text: text.user_login + ' (#' + text.ID + ')' } );
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -38,7 +73,9 @@ function um_admin_init_users_select() {
|
||||
allowHtml: true,
|
||||
dropdownCssClass: 'um-select2-users-dropdown',
|
||||
containerCssClass : 'um-select2-users-container',
|
||||
placeholder: jQuery(this).data('placeholder')
|
||||
placeholder: jQuery(this).data('placeholder'),
|
||||
templateSelection: avatarformat,
|
||||
templateResult: avatarformat
|
||||
};
|
||||
|
||||
jQuery('.um-user-select-field').select2( select2_atts );
|
||||
|
||||
@@ -576,33 +576,39 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
function render_users_dropdown( $field_data ) {
|
||||
public 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 = ( ! 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 = ! 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'],
|
||||
'avatar' => ! empty( $field_data['avatar'] ) ? 1 : 0,
|
||||
);
|
||||
|
||||
if ( ! empty( $field_data['data'] ) && is_array( $field_data['data'] ) ) {
|
||||
$data = array_merge( $data, $field_data['data'] );
|
||||
}
|
||||
|
||||
$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;
|
||||
$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 = $name . ( ! empty( $field_data['multi'] ) ? '[]' : '' );
|
||||
$name_attr = ' name="' . $name . '" ';
|
||||
|
||||
$value = $this->get_field_value( $field_data );
|
||||
@@ -620,15 +626,22 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
|
||||
$options = '';
|
||||
if ( ! empty( $users ) ) {
|
||||
foreach ( $users as $user ) {
|
||||
$options .= '<option value="' . esc_attr( $user->ID ) . '" selected>' . esc_html( $user->user_login . ' (#' . $user->ID . ')' ) . '</option>';
|
||||
if ( ! empty( $field_data['avatar'] ) ) {
|
||||
$url = get_avatar_url( $user->ID, 'size=20' );
|
||||
$options .= '<option data-img="' . esc_url( $url ) . '" value="' . esc_attr( $user->ID ) . '" selected>' . esc_html( $user->user_login . ' (#' . $user->ID . ')' ) . '</option>';
|
||||
} else {
|
||||
$options .= '<option value="' . esc_attr( $user->ID ) . '" selected>' . esc_html( $user->user_login . ' (#' . $user->ID . ')' ) . '</option>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$placeholder = ! empty( $field_data['placeholder'] ) ? $field_data['placeholder'] : __( 'Select Users', 'ultimate-member' );
|
||||
|
||||
$hidden = '';
|
||||
if ( ! empty( $multiple ) ) {
|
||||
$hidden = "<input type=\"hidden\" $hidden_name_attr value=\"\" />";
|
||||
}
|
||||
$html = "$hidden<select $multiple $id_attr $name_attr $class_attr $data_attr data-placeholder=\"" . esc_attr__( 'Select Users', 'ultimate-member' ) . "\" placeholder=\"" . esc_attr__( 'Select Users', 'ultimate-member' ) . "\"><option>" . esc_html__( 'Select Users', 'ultimate-member' ) . "</option>$options</select>";
|
||||
$html = "$hidden<select $multiple $id_attr $name_attr $class_attr $data_attr data-placeholder=\"" . esc_attr( $placeholder ) . "\" placeholder=\"" . esc_attr( $placeholder ) . "\"><option value=\"\">" . esc_html( $placeholder ) . "</option>$options</select>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
<?php
|
||||
namespace um\admin\core;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
|
||||
|
||||
/**
|
||||
* Class Admin_Users
|
||||
* @package um\admin\core
|
||||
@@ -42,8 +41,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
add_action( 'um_admin_user_action_hook', array( &$this, 'user_action_hook' ), 10, 1 );
|
||||
}
|
||||
|
||||
|
||||
function get_users() {
|
||||
public function get_users() {
|
||||
UM()->admin()->check_ajax_nonce();
|
||||
|
||||
$search_request = ! empty( $_REQUEST['search'] ) ? sanitize_text_field( $_REQUEST['search'] ) : '';
|
||||
@@ -53,17 +51,26 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
$args = array(
|
||||
'fields' => array( 'ID', 'user_login' ),
|
||||
'paged' => $page,
|
||||
'number' => $per_page
|
||||
'number' => $per_page,
|
||||
);
|
||||
|
||||
if ( ! empty( $search_request ) ) {
|
||||
$args['search'] = $search_request;
|
||||
$args['search'] = '*' . $search_request . '*';
|
||||
}
|
||||
|
||||
$args = apply_filters( 'um_get_users_list_ajax_args', $args );
|
||||
|
||||
$users_query = new \WP_User_Query( $args );
|
||||
$users = $users_query->get_results();
|
||||
$total_count = $users_query->get_total();
|
||||
|
||||
if ( ! empty( $_REQUEST['avatar'] ) ) {
|
||||
foreach ( $users as $key => $user ) {
|
||||
$url = get_avatar_url( $user->ID );
|
||||
$users[ $key ]->img = $url;
|
||||
}
|
||||
}
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'users' => $users,
|
||||
@@ -72,7 +79,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Restrict the edit/delete users via wp-admin screen by the UM role capabilities
|
||||
*
|
||||
@@ -83,7 +89,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function map_caps_by_role( $allcaps, $cap, $args, $user ) {
|
||||
public function map_caps_by_role( $allcaps, $cap, $args, $user ) {
|
||||
if ( isset( $cap[0] ) && $cap[0] == 'edit_users' ) {
|
||||
if ( isset( $args[0] ) && isset( $args[1] ) && ! user_can( $args[1], 'administrator' ) && $args[0] == 'edit_user' ) {
|
||||
if ( isset( $args[2] ) && ! UM()->roles()->um_current_user_can( 'edit', $args[2] ) ) {
|
||||
@@ -107,13 +113,12 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $allcaps;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Does an action to user asap
|
||||
*
|
||||
* @param string $action
|
||||
*/
|
||||
function user_action_hook( $action ) {
|
||||
public function user_action_hook( $action ) {
|
||||
switch ( $action ) {
|
||||
default:
|
||||
/**
|
||||
@@ -176,12 +181,12 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add UM Bulk actions to Users List Table
|
||||
*
|
||||
*/
|
||||
function restrict_manage_users() { ?>
|
||||
public function restrict_manage_users() {
|
||||
?>
|
||||
<div style="float:right;margin:0 4px">
|
||||
|
||||
<label class="screen-reader-text" for="um_bulk_action"><?php _e( 'UM Action', 'ultimate-member' ); ?></label>
|
||||
@@ -197,16 +202,16 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
|
||||
<?php if ( ! empty( $_REQUEST['um_status'] ) ) { ?>
|
||||
<input type="hidden" name="um_status" id="um_status" value="<?php echo esc_attr( sanitize_key( $_REQUEST['um_status'] ) );?>"/>
|
||||
<?php }
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get UM bulk actions HTML
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function get_bulk_admin_actions() {
|
||||
public function get_bulk_admin_actions() {
|
||||
|
||||
/**
|
||||
* UM hook
|
||||
@@ -259,7 +264,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Custom row actions for users page
|
||||
*
|
||||
@@ -267,7 +271,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
* @param $user_object \WP_User
|
||||
* @return array
|
||||
*/
|
||||
function user_row_actions( $actions, $user_object ) {
|
||||
public function user_row_actions( $actions, $user_object ) {
|
||||
$user_id = $user_object->ID;
|
||||
|
||||
$actions['frontend_profile'] = '<a href="' . um_user_profile_url( $user_id ) . '">' . __( 'View profile', 'ultimate-member' ) . '</a>';
|
||||
@@ -312,14 +316,13 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change default sorting at WP Users list table
|
||||
*
|
||||
* @param array $args
|
||||
* @return array
|
||||
*/
|
||||
function hide_by_caps( $args ) {
|
||||
public function hide_by_caps( $args ) {
|
||||
if ( ! current_user_can( 'administrator' ) ) {
|
||||
$can_view_roles = um_user( 'can_view_roles' );
|
||||
if ( um_user( 'can_view_all' ) && ! empty( $can_view_roles ) ) {
|
||||
@@ -330,7 +333,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change default sorting at WP Users list table
|
||||
*
|
||||
@@ -350,7 +352,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filter WP users by UM Status
|
||||
*
|
||||
@@ -385,7 +386,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add status links to WP Users List Table
|
||||
*
|
||||
@@ -474,7 +474,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $views;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Bulk user editing actions
|
||||
*/
|
||||
@@ -561,14 +560,13 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets redirect URI after bulk action
|
||||
*
|
||||
* @param string $uri
|
||||
* @return string
|
||||
*/
|
||||
function set_redirect_uri( $uri ) {
|
||||
public function set_redirect_uri( $uri ) {
|
||||
|
||||
if ( ! empty( $_REQUEST['s'] ) ) {
|
||||
$uri = add_query_arg( 's', sanitize_text_field( $_REQUEST['s'] ), $uri );
|
||||
@@ -581,6 +579,5 @@ if ( ! class_exists( 'um\admin\core\Admin_Users' ) ) {
|
||||
return $uri;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user