diff --git a/friends-admin.css b/friends-admin.css index 80f82949..7b2bc170 100644 --- a/friends-admin.css +++ b/friends-admin.css @@ -116,3 +116,8 @@ span#friends_enable_retention_days_line, span#friends_enable_retention_number_li #wpadminbar li#wp-admin-bar-friends { display: block; } + +ul.friend-suggestions li { + display: inline-block; + margin-right: 1em; +} diff --git a/friends-admin.js b/friends-admin.js index 52774c2f..c2635b5a 100644 --- a/friends-admin.js +++ b/friends-admin.js @@ -213,4 +213,35 @@ jQuery( function( $ ) { } } ); + $( document ).on( 'click', 'ul.friend-suggestions li a', function() { + event.preventDefault(); + $( '#friend_url' ).val( this.href ); + } ); + + var searchTimeout = null; + $( document ).on( 'keydown', '#friend_url', function() { + var search = this.value; + if ( ! search ) { + return; + } + + if ( searchTimeout ) { + clearTimeout( searchTimeout ); + } + + searchTimeout = setTimeout( function() { + wp.ajax.post( 'friends_search_links', { + _ajax_nonce: $( 'ul.friend-suggestions' ).data( 'nonce' ), + search: search + } ).done( function( response ) { + if ( response.data ) { + $( 'ul.friend-suggestions' ).html( response.data.content ); + $( 'ul.friend-suggestions' ).closest( 'tr' ).show(); + } else { + $( 'ul.friend-suggestions' ).closest( 'tr' ).hide(); + } + } ); + }, 50 ); + } ); + } ); diff --git a/includes/class-admin.php b/includes/class-admin.php index 82b78845..25b2ca6c 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -58,6 +58,7 @@ private function register_hooks() { add_action( 'wp_ajax_friends_preview_rules', array( $this, 'ajax_preview_friend_rules' ) ); add_action( 'wp_ajax_friends_update_welcome_panel', array( $this, 'ajax_update_welcome_panel' ) ); add_action( 'wp_ajax_friends_refresh_link_token', array( $this, 'ajax_refresh_link_token' ) ); + add_action( 'wp_ajax_friends_search_links', array( $this, 'ajax_search_links' ) ); add_action( 'delete_user_form', array( $this, 'delete_user_form' ), 10, 2 ); add_action( 'delete_user', array( $this, 'delete_user' ) ); add_action( 'tool_box', array( $this, 'toolbox_bookmarklets' ) ); @@ -283,7 +284,7 @@ public function register_help( $screen ) { * Reference our script for the /friends page */ public function admin_enqueue_scripts() { - wp_enqueue_script( 'friends-admin', plugins_url( 'friends-admin.js', FRIENDS_PLUGIN_FILE ), array( 'jquery' ), Friends::VERSION ); + wp_enqueue_script( 'friends-admin', plugins_url( 'friends-admin.js', FRIENDS_PLUGIN_FILE ), array( 'jquery', 'wp-util' ), Friends::VERSION ); $variables = array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'add_friend_url' => self_admin_url( 'admin.php?page=add-friend' ), @@ -833,8 +834,52 @@ public function ajax_update_welcome_panel() { wp_die( 1 ); } + + /** - * Respond to the Ajax request to the Friend Welcome Panel + * Respond to the Ajax request to search links. + */ + public function ajax_search_links() { + check_ajax_referer( 'friends-links' ); + + if ( ! current_user_can( Friends::REQUIRED_ROLE ) ) { + wp_die( -1 ); + } + + $search = $_POST['search']; + + $links = get_bookmarks( + array( + 'search' => $search, + 'limit' => 15, + ) + ); + ob_start(); + Friends::template_loader()->get_template_part( + 'admin/links', + null, + array( + 'skip_ul' => true, + 'links' => $links, + ) + ); + $content = ob_get_contents(); + ob_end_clean(); + + wp_send_json_success( + array( + 'success' => true, + 'data' => array( + 'content' => $content, + 'search' => $_POST['search'], + ), + ) + ); + + } + + /** + * Respond to the Ajax request to refresh the link token. */ public function ajax_refresh_link_token() { if ( ! isset( $_POST['url'] ) || ! isset( $_POST['friend'] ) ) { diff --git a/templates/admin/add-friend.php b/templates/admin/add-friend.php index ff71a49f..61b64dc3 100644 --- a/templates/admin/add-friend.php +++ b/templates/admin/add-friend.php @@ -24,11 +24,34 @@
+ +
+