Last active
December 21, 2015 05:08
-
-
Save getsource/6254495 to your computer and use it in GitHub Desktop.
Heartbeat Sample plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Heartbeat Comment Update | |
Description: Updates Comment Count on Dashboard with Heartbeat. | |
Author: Andrew Ozz, Mike Schroder | |
Version: 0.2 | |
Author URI: http://www.getsource.net | |
*/ | |
add_filter( 'heartbeat_received', 'wc_heartbeat_comment_count', 10, 3 ); | |
function wc_heartbeat_comment_count( $response, $data, $screen_id ) { | |
// Check if we are on the right screen and data is requested. | |
if ( $screen_id == 'dashboard' && | |
isset( $data['wc-comment-heartbeat'] ) ) { | |
// Grab our data | |
$num_comments = wp_count_comments(); | |
// Add data in an unique array key -- prefix! | |
$response['wc-comment-count'] = array( | |
'comment-count' => number_format_i18n($num_comments->total_comments), | |
'pending-count' => number_format_i18n($num_comments->moderated), | |
'comment-mod-count' => number_format_i18n($num_comments->moderated), | |
'spam-count' => number_format_i18n($num_comments->spam), | |
'trash-count' => number_format_i18n($num_comments->trash), | |
); | |
} | |
// If the above conditions aren't met, we pass along the existing $response. | |
return $response; | |
} | |
add_action( 'admin_enqueue_scripts', 'wc_heartbeat_comment_script_enqueue' ); | |
function wc_heartbeat_comment_script_enqueue( $hook_suffix ) { | |
// Load this script only in the dashboard. | |
if ( 'index.php' != $hook_suffix ) | |
return; | |
// Make sure the JS part of the Heartbeat API is loaded. | |
wp_enqueue_script( 'heartbeat' ); | |
// Output the JavaScript we need. | |
add_action( 'admin_print_footer_scripts', 'wc_heartbeat_comment_js', 20 ); | |
} | |
function wc_heartbeat_comment_js() { | |
?> | |
<script> | |
jQuery(document).ready( function($) { | |
// Request comment count, using prefixed custom event. | |
$(document).on( 'heartbeat-send.wp-comment-count', function( e, data ) { | |
// For one-time send, use wp.heartbeat.enqueue( 'item', 'value' ); | |
data['wc-comment-heartbeat'] = 'count'; | |
// Speed up heartbeat for faster results. | |
// Only has to be called every 2.5 minutes. | |
window.wp.heartbeat.interval(15); | |
// Listen for the tick custom event. | |
}).on( 'heartbeat-tick.wc-comment-count', function( e, data ) { | |
// Double-check we have the data we're listening for | |
if ( ! data['wc-comment-count'] ) | |
return; | |
// Replace all of the comment counts | |
$.each( data['wc-comment-count'], function( key, value ) { | |
var replaced = $( '.' + key ).html().replace(/(.*)\d+(.*)/g, "$1" + value + "$2"); | |
$( '.' + key ).html( replaced ); | |
}); | |
}); | |
// Initial connection to cement our new interval timing | |
window.wp.heartbeat.connectNow(); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment