Skip to content

Commit

Permalink
Add AJAX refreshing of feeds (#382)
Browse files Browse the repository at this point in the history
* Add AJAX refreshing of feeds

* Refresh the page content

* Also count fails

* Return some more info

* Update wp_feed_options action

* errorlog

* Fetch all feeds in sequence

* Force Poll

* typo
  • Loading branch information
akirk authored Oct 22, 2024
1 parent 956d3df commit 0e4d760
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 20 deletions.
93 changes: 86 additions & 7 deletions friends.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
searchDialog.append( $( '.navbar-section.search' ).children() );

// create a new dialog
if ( searchDialog.is(':visible') ) {
if ( searchDialog.is( ':visible' ) ) {
searchDialog.hide();
$( '.navbar-section.search' ).append( searchDialog.children() );
} else {
Expand All @@ -116,6 +116,85 @@
}
} );

const refresh_feeds_now = function() {
let $this = $( this );
function set_status( t ) {
if ( ! $this.length ) {
$this = $( 'a.friends-refresh' );
}
if ( ! $this.find( 'span' ).length ) {
$this.html( '<span></span> <i class="loading"></i> ' );
$this.find( 'i' ).css( 'margin-left', '1em' );
$this.find( 'i' ).css( 'margin-right', '1em' );
}
$this.find( 'span' ).text( t );
}
set_status( friends.text_refreshing );

$.ajax( {
url: friends.rest_base + 'get-feeds',
beforeSend: function(xhr){
xhr.setRequestHeader( 'X-WP-Nonce', friends.rest_nonce );
},
}
).done( function( feeds ) {
const feed_count = feeds.length;
let alreadyLoading = false;
function update_page() {
if ( alreadyLoading ) {
return;
}
wp.ajax.send( 'friends-load-next-page', {
data: {
query_vars: friends.query_vars,
page: friends.current_page - 1,
qv_sign: friends.qv_sign,
},
beforeSend() {
alreadyLoading = true;
},
success( newPosts ) {
alreadyLoading = false;
if ( newPosts ) {
$( 'section.posts' )
.html( newPosts );
}
},
} );
}
function fetch_next() {
if ( ! feeds.length ) {
$this.html( friends.text_refreshed + '<i class="dashicons dashicons-yes">' );
alreadyLoading = false;
update_page();
return;
}
const feed = feeds.shift();
$.ajax( {
url: friends.rest_base + 'refresh-feed',
method: 'POST',
data: { id: feed.id },
beforeSend: function(xhr){
xhr.setRequestHeader( 'X-WP-Nonce', friends.rest_nonce );
},
} ).always( function( data ) {
set_status( ( feed_count - feeds.length ) + ' / ' + feed_count );
if ( data.new_posts ) {
update_page();
}
setTimeout( fetch_next, 1 );
} );
}
fetch_next();
} );
return false;
};
$document.on( 'click', 'a.friends-refresh', refresh_feeds_now );

if ( 'true' === friends.refresh_now ) {
refresh_feeds_now();
}

$document.on(
'click',
'a.friends-auth-link, button.comments.friends-auth-link',
Expand Down Expand Up @@ -610,16 +689,16 @@
data: {
_ajax_nonce: $this.data( 'nonce' ),
url: $this.data( 'id' ),
followers: $this.data('followers' ),
following: $this.data('following' ),
followers: $this.data( 'followers' ),
following: $this.data( 'following' ),
},
success( result ) {
$this.find('.loading-posts').hide().after( result.posts );
$this.find('.their-followers').text( result.followers );
$this.find('.their-following').text( result.following );
$this.find( '.loading-posts' ).hide().after( result.posts );
$this.find( '.their-followers' ).text( result.followers );
$this.find( '.their-following' ).text( result.following );
},
error( result ) {
$this.find('.loading-posts').text( result.map(function( error ) { return error.message || error.code; } ).join( ',' ) );
$this.find( '.loading-posts' ).text( result.map(function( error ) { return error.message || error.code; } ).join( ',' ) );
}
} );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public function process_incoming_feed_items( array $items, User_Feed $user_feed
$new_posts = array();
$modified_posts = array();
foreach ( $items as $item ) {
if ( ! $item->permalink ) {
if ( ! isset( $item->permalink ) || ! $item->permalink ) {
continue;
}
$item->permalink = str_replace( array( '&#38;', '&#038;' ), '&', ent2ncr( wp_kses_normalize_entities( $item->permalink ) ) );
Expand Down
16 changes: 5 additions & 11 deletions includes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,17 @@ public function enqueue_scripts() {
$variables = array(
'emojis_json' => plugins_url( 'emojis.json', FRIENDS_PLUGIN_FILE ),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'rest_base' => rest_url( 'friends/v1/' ),
'rest_nonce' => wp_create_nonce( 'wp_rest' ),
'text_link_expired' => __( 'The link has expired. A new link has been generated, please click it again.', 'friends' ),
'text_undo' => __( 'Undo' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
'text_trash_post' => __( 'Trash this post', 'friends' ),
'text_del_convers' => __( 'Do you really want to delete this conversation?', 'friends' ),
'text_no_more_posts' => __( 'No more posts available.', 'friends' ),
'text_checking_url' => __( 'Checking URL.', 'friends' ),
'text_refreshed' => __( 'Refreshed', 'friends' ),
'text_refreshing' => __( 'Refreshing', 'friends' ),
'refresh_now' => isset( $_GET['refresh'] ) ? 'true' : 'false', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'query_vars' => $query_vars,
'qv_sign' => sha1( wp_salt( 'nonce' ) . $query_vars ),
'current_page' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
Expand Down Expand Up @@ -808,17 +813,6 @@ public function template_override( $template ) {
return Friends::template_loader()->get_template_part( $this->template, null, $args, false );
}

if ( isset( $_GET['refresh'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
add_filter( 'notify_about_new_friend_post', '__return_false', 999 );
add_filter(
'wp_feed_options',
function ( $feed ) {
$feed->enable_cache( false );
}
);
$this->friends->feed->retrieve_friend_posts( true );
}

$args['frontend_default_view'] = get_option( 'friends_frontend_default_view', 'expanded' );
$args['blocks-everywhere'] = false;

Expand Down
83 changes: 83 additions & 0 deletions includes/class-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,36 @@ public function add_rest_routes() {
'permission_callback' => '__return_true',
)
);

register_rest_route(
self::PREFIX,
'get-feeds',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_feeds' ),
'permission_callback' => function () {
return current_user_can( Friends::REQUIRED_ROLE );
},
)
);

register_rest_route(
self::PREFIX,
'refresh-feed',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_refresh_feed' ),
'params' => array(
'id' => array(
'type' => 'integer',
'required' => true,
),
),
'permission_callback' => function () {
return current_user_can( Friends::REQUIRED_ROLE );
},
)
);
}

/**
Expand Down Expand Up @@ -334,6 +364,59 @@ public function rest_embed_friend_post( $request ) { // phpcs:ignore Generic.Cod
exit;
}

public function rest_get_feeds( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
$feeds = User_Feed::get_all_due( true );
$feeds = array_map(
function ( $feed ) {
return array(
'id' => $feed->get_id(),
'url' => $feed->get_url(),
'parser' => $feed->get_parser(),
'last_log' => $feed->get_last_log(),
'next_poll' => $feed->get_next_poll(),
);
},
$feeds
);

return $feeds;
}

public function rest_refresh_feed( $request ) {
$feed_id = $request->get_param( 'id' );
$feed = new User_Feed( get_term( intval( $feed_id ) ) );
add_filter( 'notify_about_new_friend_post', '__return_false', 999 );
add_action(
'wp_feed_options',
function ( &$feed ) {
$feed->enable_cache( false );
}
);
$new_posts = array();

$friend_user = $feed->get_friend_user();
$was_polled = false;
if ( $friend_user && $feed->can_be_polled_now() ) {
$feed->set_polling_now();
$new_posts = $this->friends->feed->retrieve_feed( $feed );
$feed->was_polled();
if ( is_wp_error( $new_posts ) ) {
return $new_posts;
}
$was_polled = true;
$friend_user->delete_outdated_posts();
}

return array(
'new_posts' => count( $new_posts ),
'url' => $feed->get_url(),
'was_polled' => $was_polled,
);
}




/**
* Discover the REST URL for a friend site
*
Expand Down
2 changes: 1 addition & 1 deletion widgets/class-widget-refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function widget( $args, $instance ) {
echo $args['before_title'] . $title . $args['after_title'];
}

echo '<a href="' . esc_url( home_url( '/friends/?refresh' ) ) . '">' . esc_html__( 'Refresh', 'friends' ) . '</a>';
echo '<a href="' . esc_url( home_url( '/friends/?refresh' ) ) . '" data-nonce="' . esc_attr( wp_create_nonce( 'friends-refresh' ) ) . '" class="friends-refresh">' . esc_html__( 'Refresh', 'friends' ) . '</a>';

echo $args['after_widget'];
}
Expand Down

0 comments on commit 0e4d760

Please sign in to comment.