diff --git a/includes/form.php b/includes/form.php
index bf98980d1..d82aeb4af 100644
--- a/includes/form.php
+++ b/includes/form.php
@@ -115,8 +115,10 @@ public function form_ajax_ep() {
} else {
switch ( $type ) {
case 'author':
+ add_action( 'pre_user_query', array( $this, 'fix_user_query_display_name' ) );
$users = get_users( array( 'search' => '*' . $query . '*' ) );
- $data = $this->format_json_for_select2( $users, 'ID', 'display_name' );
+ remove_action( 'pre_user_query', array( $this, 'fix_user_query_display_name' ) );
+ $data = $this->format_json_for_select2( $users, 'ID', 'display_name' );
break;
case 'action':
$actions = WP_Stream_Connectors::$term_labels['stream_action'];
@@ -125,7 +127,18 @@ public function form_ajax_ep() {
break;
}
}
- if ( isset( $data ) ) {
+
+ // Add gravatar for authors
+ if ( $type == 'author' && get_option( 'show_avatars' ) ) {
+ foreach ( $data as $i => $item ) {
+ if ( $avatar = get_avatar( $item['id'], 20 ) ) {
+ $item['avatar'] = $avatar;
+ }
+ $data[$i] = $item;
+ }
+ }
+
+ if ( $data ) {
wp_send_json_success( $data );
} else {
wp_send_json_error();
@@ -157,6 +170,16 @@ public function format_json_for_select2( $data, $key = null, $val = null ) {
return $return;
}
+ public function fix_user_query_display_name( $query ) {
+ global $wpdb;
+ $search = $query->query_vars['search'];
+ if ( empty( $search ) ) {
+ return;
+ }
+ $search = str_replace( '*', '', $search );
+ $query->query_where .= $wpdb->prepare( " OR $wpdb->users.display_name LIKE %s", '%' . like_escape( $search ) . '%' );
+ }
+
/**
* Format JS options for the form, to be used with wp_localize_script
*
@@ -314,14 +337,14 @@ public function metabox_save( $rule ) {
);
?>
-
+
-
+
diff --git a/ui/js/form.js b/ui/js/form.js
index 64d2d1223..e862477c2 100644
--- a/ui/js/form.js
+++ b/ui/js/form.js
@@ -23,10 +23,23 @@ jQuery(function($){
tmpl_alert = _.template( $('script#alert-template-row').html() ),
tmpl_alert_options = _.template( $('script#alert-template-options').html() ),
+ select2_format = function( item ) {
+ var text = item.text;
+ if ( typeof item.avatar != 'undefined' ) {
+ text = item.avatar + item.text;
+ } else{
+ console.log('no avatar', item)
+ }
+ return text;
+ },
+
select2_args = {
allowClear: true,
minimumResultsForSearch: 8,
- width: '160px'
+ width: '160px',
+ format: select2_format,
+ formatSelection: select2_format,
+ formatResult: select2_format
},
selectify = function( elements, args ) {
@@ -63,9 +76,6 @@ jQuery(function($){
results: function (data) {
var r = data.data || [];
return {results: r};
- },
- formatSelection: function(item) {
- return item.title;
}
};
elementArgs.initSelection = function(element, callback) {
@@ -104,7 +114,6 @@ jQuery(function($){
},
dataType: "json",
success: function(j){
- console.log(j.data)
$this.select2( 'data', j.data );
}
})
@@ -205,7 +214,7 @@ jQuery(function($){
var $this = $(this),
options = stream_notifications.adapters[ $this.val() ],
index = $this.parents('.alert').first().attr('rel');
- $this.next('.alert-options').remove();
+ $this.parent().next('.alert-options').remove();
if ( ! options ) { return; }
diff --git a/views/rule-form.php b/views/rule-form.php
index 10334ca65..047521eb3 100644
--- a/views/rule-form.php
+++ b/views/rule-form.php
@@ -292,6 +292,11 @@
.select2-container.trigger-operator {
width: 140px !important;
}
+ .select2-choices img.avatar,
+ .select2-results img.avatar {
+ vertical-align: middle;
+ padding-right: 5px;
+ }
#alerts .add-alert {
margin: 0 0 12px 12px;
}