Make WordPress Core

Ticket #23216: heartbeat-test-plugin.2.php

File heartbeat-test-plugin.2.php, 2.4 KB (added by strangerstudios, 13 years ago)

Fixed original test plugin.

Line 
1<?php
2/**
3 * Plugin Name: Heartbeat API test plugin
4 * Description: For testing the heartbeat API only, not suitable for production sites. Updates the comments count on the dashboard.
5 */
6
7
8add_filter( 'heartbeat_send', 'heartbeat_test_comments_count', 10, 2 );
9
10function heartbeat_test_comments_count($data, $screen_id) {
11 // Check if we are on the right screen
12 if ( $screen_id != 'dashboard' )
13 return $data;
14
15 $num_comments = wp_count_comments();
16
17 // Add data in an unique array key.
18 // The uniqueness requirement is the same like for PHP function names used in a plugin.
19 $data['my-comments-count'] = array(
20 'total-count' => number_format_i18n($num_comments->total_comments),
21 'approved-count' => number_format_i18n($num_comments->approved),
22 'pending-count' => number_format_i18n($num_comments->moderated),
23 'spam-count' => number_format_i18n($num_comments->spam),
24 );
25
26 return $data;
27}
28
29add_action( 'admin_enqueue_scripts', 'heartbeat_test_enqueue' );
30
31function heartbeat_test_enqueue($hook_suffix) {
32 // Load scripts only where needed.
33 if ( 'index.php' != $hook_suffix )
34 return;
35
36 // Make sure the JS part of the Heartbeat API is loaded.
37 wp_enqueue_script('heartbeat');
38
39 // Output the test JS.
40 add_action( 'admin_print_footer_scripts', 'heartbeat_test_js', 20 );
41}
42
43
44function heartbeat_test_js() {
45 ?>
46 <script>
47
48 jQuery(document).ready( function($) {
49 // Listen for the custom event "heartbeat-tick" on $(document).
50 // Note: the event may change!
51 // Always a good idea to add namespace.
52 $(document).on( 'heartbeat-tick.comments-count', function(e, data) {
53 //console.log(data); // Uncomment to debug. Added by Jason Coleman 08AUG13
54
55 if ( !data['my-comments-count'] )
56 return;
57
58 $.each( data['my-comments-count'], function(key, value) {
59 var node = $('#dashboard_right_now .' + key);
60 // Update only when changed
61 if ( node.html() != value )
62 node.html(value);
63 });
64
65 if ( typeof console != 'undefined' ) {
66 // Show debug info
67 wp.heartbeat.debug = true;
68 }
69
70 });
71
72 // You need to send some information or the heartbeat won't tick back
73 // Added by Jason Coleman 08AUG13
74 $(document).on('heartbeat-send', function(e, data) {
75 //console.log('Sending data to server.'); //uncomment to debug
76 data['anything'] = 'anything'; //need some data to kick off AJAX call
77 });
78 });
79
80 </script>
81 <?php
82}