Нема описа

post-count.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var wpPostLikeCount = wpPostLikeCount || {};
  2. ( function ( $ ) {
  3. wpPostLikeCount = jQuery.extend( wpPostLikeCount, {
  4. jsonAPIbase: 'https://public-api.wordpress.com/rest/v1',
  5. APIqueue: [],
  6. wpPostLikeCount: function () {
  7. $( '.post-like-count' ).each( function () {
  8. var post_id = $( this ).attr( 'data-post-id' );
  9. var blog_id = $( this ).attr( 'data-blog-id' );
  10. wpPostLikeCount.APIqueue.push( '/sites/' + blog_id + '/posts/' + post_id + '/likes' );
  11. } );
  12. wpPostLikeCount.getCounts();
  13. },
  14. showCount: function ( post_id, count ) {
  15. if ( count > 0 ) {
  16. $( '#post-like-count-' + post_id )
  17. .find( '.comment-count' )
  18. .hide();
  19. $( '#post-like-count-' + post_id )
  20. .find( '.comment-count' )
  21. .text( count );
  22. $( '#post-like-count-' + post_id )
  23. .find( '.comment-count' )
  24. .fadeIn();
  25. }
  26. },
  27. getCounts: function () {
  28. var batchRequest = {
  29. path: '/batch',
  30. data: '',
  31. success: function ( response ) {
  32. for ( var path in response ) {
  33. if ( ! response[ path ].error_data ) {
  34. var urlPieces = path.split( '/' ); // pieces[4] = post id;
  35. var post_id = urlPieces[ 4 ];
  36. wpPostLikeCount.showCount( post_id, response[ path ].found );
  37. }
  38. }
  39. },
  40. error: function (/*response*/) {},
  41. };
  42. var amp = '';
  43. for ( var i = 0; i < wpPostLikeCount.APIqueue.length; i++ ) {
  44. if ( i > 0 ) {
  45. amp = '&';
  46. }
  47. batchRequest.data += amp + 'urls[]=' + wpPostLikeCount.APIqueue[ i ];
  48. }
  49. wpPostLikeCount.request( batchRequest );
  50. },
  51. } );
  52. } )( jQuery );
  53. jQuery( document ).ready( function (/*$*/) {
  54. wpPostLikeCount.wpPostLikeCount();
  55. } );