diff options
Diffstat (limited to 'plugins/jetpack/modules/likes/post-count.js')
-rw-r--r-- | plugins/jetpack/modules/likes/post-count.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/plugins/jetpack/modules/likes/post-count.js b/plugins/jetpack/modules/likes/post-count.js new file mode 100644 index 00000000..d93b8859 --- /dev/null +++ b/plugins/jetpack/modules/likes/post-count.js @@ -0,0 +1,69 @@ +var wpPostLikeCount; + +(function($) { + + wpPostLikeCount = { + + jsonAPIbase: 'https://public-api.wordpress.com/rest/v1', + APIqueue: [], + + wpPostLikeCount: function() { + $( '.post-like-count' ).each( function() { + var post_id = $(this).attr( 'data-post-id' ); + var blog_id = $(this).attr( 'data-blog-id' ); + wpPostLikeCount.APIqueue.push( '/sites/' + blog_id + '/posts/' + post_id + '/likes' ); + } ); + wpPostLikeCount.getCounts(); + }, + + showCount: function( post_id, count ) { + $( '#post-like-count-' + post_id ).find( '.comment-count' ).hide(); + $( '#post-like-count-' + post_id ).find( '.comment-count' ).text( count ); + $( '#post-like-count-' + post_id ).find( '.comment-count' ).fadeIn(); + }, + + getCounts: function() { + var batchRequest = { + path: '/batch', + data: '', + success: function( response ) { + for ( var path in response ) { + if ( ! response[path]['error_data'] ) { + var urlPieces = path.split( '/' ); // pieces[4] = post id; + var post_id = urlPieces[4]; + wpPostLikeCount.showCount( post_id, response[path].found ); + } + } + }, + error: function( response ) { + } + }; + + var amp = ''; + for( var i = 0; i < wpPostLikeCount.APIqueue.length; i++ ) { + if ( i > 0 ) + amp = '&'; + batchRequest.data += amp + 'urls[]=' + wpPostLikeCount.APIqueue[i]; + } + + wpPostLikeCount.request( batchRequest ); + }, + + request: function( options ) { + return $.ajax( { + type: 'GET', + url: wpPostLikeCount.jsonAPIbase + options.path, + dataType : "jsonp", + data: options.data, + success: function( response ) { options.success( response ) }, + error: function( response ) { options.error( response ) } + } ) + } + + }; + +})(jQuery); + +jQuery(document).ready(function($) { + wpPostLikeCount.wpPostLikeCount(); +});
\ No newline at end of file |