@@ -160,18 +160,26 @@ function wp_ajax_autocomplete_user() {
160160
161161 $ return = array ();
162162
163+ // Check the type of request
164+ if ( isset ( $ _REQUEST ['autocomplete_type ' ] ) )
165+ $ type = $ _REQUEST ['autocomplete_type ' ];
166+ else
167+ $ type = 'add ' ;
168+
163169 // Exclude current users of this blog
164170 if ( isset ( $ _REQUEST ['site_id ' ] ) )
165171 $ id = absint ( $ _REQUEST ['site_id ' ] );
166172 else
167173 $ id = get_current_blog_id ();
168174
169- $ this_blog_users = get_users ( array ( 'blog_id ' => $ id , 'fields ' => 'ID ' ) );
175+ $ include_blog_users = ( $ type == 'search ' ? get_users ( array ( 'blog_id ' => $ id , 'fields ' => 'ID ' ) ) : array () );
176+ $ exclude_blog_users = ( $ type == 'add ' ? get_users ( array ( 'blog_id ' => $ id , 'fields ' => 'ID ' ) ) : array () );
170177
171178 $ users = get_users ( array (
172179 'blog_id ' => false ,
173180 'search ' => '* ' . $ _REQUEST ['term ' ] . '* ' ,
174- 'exclude ' => $ this_blog_users ,
181+ 'include ' => $ include_blog_users ,
182+ 'exclude ' => $ exclude_blog_users ,
175183 'search_columns ' => array ( 'user_login ' , 'user_nicename ' , 'user_email ' ),
176184 ) );
177185
@@ -206,6 +214,35 @@ function wp_ajax_dashboard_widgets() {
206214 wp_die ();
207215}
208216
217+ function wp_ajax_autocomplete_site () {
218+ if ( ! is_multisite ()
219+ || ! current_user_can ( 'manage_sites ' )
220+ || wp_is_large_network ( 'sites ' )
221+ || ! is_super_admin ()
222+ ) {
223+ wp_die ( -1 );
224+ }
225+
226+ $ return = array ();
227+
228+ global $ wpdb ;
229+ $ like_escaped_term = '% ' . like_escape ( stripslashes ( $ _REQUEST ['term ' ] ) ) . '% ' ;
230+ $ sites = $ wpdb ->get_results ( $ wpdb ->prepare ( "SELECT blog_id, domain, path FROM $ wpdb ->blogs WHERE ( domain LIKE %s OR path LIKE %s ) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC " , $ like_escaped_term , $ like_escaped_term ), ARRAY_A );
231+
232+ if ( empty ( $ sites ) )
233+ wp_die ( -1 );
234+
235+ foreach ( (array ) $ sites as $ details ) {
236+ $ blogname = get_blog_option ( $ details ['blog_id ' ], 'blogname ' );
237+ $ return [] = array (
238+ 'label ' => sprintf ( '%1$s (%2$s) ' , $ blogname , $ details ['domain ' ] . $ details ['path ' ] ),
239+ 'value ' => $ details ['domain ' ]
240+ );
241+ }
242+
243+ wp_die ( json_encode ( $ return ) );
244+ }
245+
209246/*
210247 * Ajax helper.
211248 */
0 commit comments