Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest friends via the local blogroll #110

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions friends-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ span#friends_enable_retention_days_line, span#friends_enable_retention_number_li
#wpadminbar li#wp-admin-bar-friends {
display: block;
}

ul.friend-suggestions {
margin: 0;
}
ul.friend-suggestions li {
display: inline-block;
margin-right: 1em;
}
30 changes: 30 additions & 0 deletions friends-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,34 @@ 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: $( 'tr.friend-suggestions' ).data( 'nonce' ),
search: search
} ).done( function( response ) {
if ( response.data ) {
$( 'tr.friend-suggestions' ).show().find( 'div' ).html( response.data.content ).show();
} else {
$( 'tr.friend-suggestions' ).hide();
}
} );
}, 50 );
} );

} );
48 changes: 46 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 @@ -837,8 +838,51 @@ 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(
'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
33 changes: 31 additions & 2 deletions templates/admin/add-friend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

$quick_subscribe = _x( 'Quick Subscribe', 'button', 'friends' );

$links = get_bookmarks(
array(
'orderby' => 'updated',
'limit' => 15,
)
);

?><div class="wrap"><form method="post">
<?php wp_nonce_field( 'add-friend' ); ?>
<p>
Expand All @@ -24,12 +31,35 @@
<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>
<tr class="friend-suggestions" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends-links' ) ); ?>">
<th scope="row"><?php esc_html_e( 'Suggestions', 'friends' ); ?></label></th>
<td>
<div>
<?php
if ( empty( $links ) ) {
esc_html_e( 'No suggestions available. You can import an OPML.', 'friends' );
} else {
Friends\Friends::template_loader()->get_template_part( 'admin/links', null, array( 'links' => $links ) );
}
?>
</div>
<p class="description" id="friend-suggestions">
<?php
printf(
// translators: %s is a URL.
__( 'You can manage the available suggestions in the <a href="%s">Link Manager</a>.', 'friends' ),
esc_url( self_admin_url( 'link-manager.php' ) )
);
?>
</p>
</td>
</tr>
<tr>
<th></th>
<td>
Expand All @@ -48,5 +78,4 @@
</tr>
</tbody>
</table>

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

?>
<ul class="friend-suggestions"">
<?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
}

?>
</ul>