comment-like-count.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. jQuery( document ).ready( function( $ ) {
  2. var jsonAPIbase = 'https://public-api.wordpress.com/rest/v1',
  3. APIqueue = [];
  4. function getCommentLikeCounts() {
  5. $( '.comment-like-count' ).each( function() {
  6. var blogId = $( this ).attr( 'data-blog-id' ),
  7. commentId = $( this ).attr( 'data-comment-id' );
  8. APIqueue.push( '/sites/' + blogId + '/comments/' + commentId + '/likes' );
  9. } );
  10. return $.ajax( {
  11. type: 'GET',
  12. url: jsonAPIbase + '/batch',
  13. dataType: 'jsonp',
  14. data: 'urls[]=' + APIqueue.map( encodeURIComponent ).join( '&urls[]=' ),
  15. success: function( response ) {
  16. for ( var path in response ) {
  17. if ( ! response[ path ].error_data ) {
  18. var urlPieces = path.split( '/' ),
  19. commentId = urlPieces[ 4 ],
  20. likeCount = response[ path ].found;
  21. if ( likeCount < 1 ) {
  22. return;
  23. }
  24. $( '#comment-like-count-' + commentId ).find( '.like-count' ).hide().text( likeCount ).fadeIn();
  25. }
  26. }
  27. },
  28. error: function() {}
  29. } );
  30. }
  31. getCommentLikeCounts();
  32. } );