Skip to content

Commit

Permalink
Update dashboard widget with new posts (#347)
Browse files Browse the repository at this point in the history
* Update dashboard widget with new posts

* Insert in the right order
  • Loading branch information
akirk committed Aug 29, 2024
1 parent 8fc080d commit 37923ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
47 changes: 30 additions & 17 deletions friends-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,28 +344,41 @@ jQuery( function ( $ ) {
}
} );

setTimeout( function () {
if ( $( '#friends-dashboard-widget' ).length ) {
var updateDashboardWidget = function() {
if ( 0 === $( '#friends-dashboard-widget' ).find( 'li.friends-post' ).length ) {
$( '#friends-dashboard-widget' ).append( ' <i class="friends-loading"></i>' );
$.post(
friends.ajax_url,
{
_ajax_nonce: $( '#friends-dashboard-widget' ).data( 'nonce' ),
action: 'friends_dashboard',
},
function ( response ) {
if ( response.success ) {
}
$.post(
friends.ajax_url,
{
_ajax_nonce: $( '#friends-dashboard-widget' ).data( 'nonce' ),
action: 'friends_dashboard',
},
function ( response ) {
if ( response.success ) {
if ( 0 === $( '#friends-dashboard-widget' ).find( 'li.friends-post' ).length ) {
$( '#friends-dashboard-widget' ).html( response.data );
} else {
$( '#friends-dashboard-widget i' )
.removeClass( 'friends-loading' )
.addClass( 'dashicons dashicons-warning' )
.prop( 'title', response.data );
$( response.data ).find( 'li.friends-post' ).reverse().each( function () {
if ( ! document.getElementById( $(this).attr('id') ) ) {
$( '#friends-dashboard-widget ul' ).prepend( this );
}
} );
$( '#friends-dashboard-widget li.friends-post:gt(49)' ).remove();
}
} else {
$( '#friends-dashboard-widget i' )
.removeClass( 'friends-loading' )
.addClass( 'dashicons dashicons-warning' )
.prop( 'title', response.data );
}
);
}
}, 500 );
}
);
};
if ( $( '#friends-dashboard-widget' ).length ) {
updateDashboardWidget();
setInterval( updateDashboardWidget, 60000 );
}

setTimeout( function () {
if ( $( '#fetch-feeds' ).length ) {
Expand Down
4 changes: 3 additions & 1 deletion templates/admin/dashboard-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
$avatar = apply_filters( 'friends_author_avatar_url', $avatar, $friend_user, $_post->ID );

?>
<li>
<li id="friends-dashboard-post-<?php echo esc_attr( $_post->ID ); ?>" class="friends-post">
<?php echo esc_html( get_the_date( __( 'H:i', 'friends' ), $_post ) ); ?>:

<a href="<?php echo esc_attr( $friend_user->get_local_friends_page_url() ); ?>" class="author-avatar">
<img src="<?php echo esc_url( $avatar ); ?>" width="16" height="16" />
</a>
Expand Down

0 comments on commit 37923ba

Please sign in to comment.