Skip to content

Commit

Permalink
Suggest friends via the local blogroll
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Apr 29, 2022
1 parent 2fa9fea commit cbbba08
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 4 deletions.
5 changes: 5 additions & 0 deletions friends-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
31 changes: 31 additions & 0 deletions friends-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );

} );
49 changes: 47 additions & 2 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -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'] ) ) {
Expand Down
26 changes: 24 additions & 2 deletions templates/admin/add-friend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,34 @@
<tr>
<th scope="row"><label for="friend_url"><?php esc_html_e( 'Site', 'friends' ); ?></label></th>
<td>
<input type="text" autofocus id="friend_url" name="friend_url" value="<?php echo esc_attr( $args['friend_url'] ); ?>" required placeholder="<?php esc_attr_e( 'Enter URL', 'friends' ); ?>" class="regular-text" />
<input type="text" autofocus id="friend_url" name="friend_url" value="<?php echo esc_attr( $args['friend_url'] ); ?>" required placeholder="<?php esc_attr_e( 'Enter URL or search suggestions', 'friends' ); ?>" class="regular-text" />
<p class="description" id="friend_url-description">
<?php esc_html_e( "In the next step we'll give you a selection of available feeds.", 'friends' ); ?>
</p>
</td>
</tr>
<th scope="row"><?php esc_html_e( 'Suggestions', 'friends' ); ?></label></th>
<td>
<?php

Friends\Friends::template_loader()->get_template_part(
'admin/links',
null,
array(
'links' => get_bookmarks(
array(
'orderby' => 'updated',
'limit' => 15,
)
),
)
);

?>
<p class="description" id="friend-suggestions">

</p>
</td>
</tr>
<tr>
<th></th>
Expand All @@ -48,5 +71,4 @@
</tr>
</tbody>
</table>

</form>
36 changes: 36 additions & 0 deletions templates/admin/links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* This template contains the links results
*
* @version 1.0
* @package Friends
*/

if ( ! isset( $args['skip_ul'] ) ) {
?><ul class="friend-suggestions" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends-links' ) ); ?>">
<?php
}

if ( empty( $args['links'] ) ) {
?>
<li>Nothing found.</li>
<?php
}
foreach ( $args['links'] as $link ) {
?>
<li>
<a href="<?php echo esc_url( $link->link_url ); ?>"><?php echo esc_html( $link->link_name ); ?></a>
<?php

if ( ! empty( $link->link_description ) ) {
echo ' (' . esc_html( $link->link_description ) . ') ';
}
?>
</li>
<?php
}
if ( ! isset( $args['skip_ul'] ) ) {
?>
</ul>
<?php
}

0 comments on commit cbbba08

Please sign in to comment.