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