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