Skip to content

Instantly share code, notes, and snippets.

@soderlind
Last active November 11, 2016 23:32
Show Gist options
  • Select an option

  • Save soderlind/94629e4ae933407d823c to your computer and use it in GitHub Desktop.

Select an option

Save soderlind/94629e4ae933407d823c to your computer and use it in GitHub Desktop.
Testing WP Gears and the Heartbeat API, WP Gears is at https://github.com/10up/WP-Gears (NOTE: this plugin is run continuously, triggered by 'plugins_loaded', you should use an event triggered by the user (onclick, onhover etc))
<?php
/*
Plugin Name: WP Gears & Heartbeat
Version: 0.0.6
Description: Testing WP Gears and the Heartbeat API
Author: Per Soderlind
Author URI: https://soderlind.no
Plugin URI: https://gist.github.com/soderlind/94629e4ae933407d823c
License: GPL
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
* WP Gears
*/
// Exit if WP Gears isn't installed (WP Gears is at https://github.com/10up/WP-Gears)
if ( ! function_exists( 'wp_async_task_add' ) ) {
exit;
}
function add_wp_gears_async_task() {
wp_async_task_add( 'wp_gears_heartbeat_worker', array( 'action' => 'gettime' ), 'high' );
}
/*
* NOTE, this plugin was written to test if I could use WP Gears with the Heartbeat API and not
* bothering to write a UI I used plugins_load.
* You should call wp_async_task_add using an user triggerd action like a button click.
*/
add_action( 'plugins_loaded', 'add_wp_gears_async_task' );
if ( ! class_exists( 'WP_Gears_Get_Time_Worker' ) ) {
class WP_Gears_Get_Time_Worker {
static function wp_gears_heartbeat_worker_callback( $args ) {
//if ( 'gettime' == $args['action']) { // doesn't work, $args is a string with value = 'gettime'
sleep( 1 ); // without, you'll get ,in supervisor, "INFO gave up: nn_worker-00 entered FATAL state, too many start retries too quickly"
update_option( 'wp-gears-heartbeat-time_' . $_GET['doing_wp_cron'], time() ); // $_GET['doing_wp_cron'] is unique
//}
}
}
}
add_action( 'wp_gears_heartbeat_worker', array( 'WP_Gears_Get_Time_Worker', 'wp_gears_heartbeat_worker_callback' ) );
/*
* Heartbeat
*/
function wp_gears_heartbeat_init() {
//add_filter( 'heartbeat_nopriv_received', 'wp_gears_heartbeat', 10, 3 );
add_filter( 'heartbeat_received', 'wp_gears_heartbeat', 10, 2 );
add_action( 'admin_enqueue_scripts', 'wp_gears_heartbeat_script_enqueue' );
}
add_action( 'plugins_loaded', 'wp_gears_heartbeat_init' );
function wp_gears_heartbeat( $response, $data ) {
// Get all options since last heartbeat
$all_options = wp_load_alloptions(); // will this cause a heavy load?
$my_options = array();
// Find my options
foreach ( $all_options as $name => $value ) {
if ( stristr( $name, 'wp-gears-heartbeat-time_' ) ) {
$my_options[] = $value;
delete_option( $name );
}
}
if ( count( $my_options ) ) {
$response['wp-gears-heartbeat-time'] = json_encode( $my_options );
}
// If the above conditions aren't met, we still pass along the existing $response.
return $response;
}
function wp_gears_heartbeat_script_enqueue( $hook_suffix ) {
// 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', 'wp_gears_heartbeat_js', 20 );
}
function wp_gears_heartbeat_js() {
?>
<script>
(function($){
// Hook into the heartbeat-send
$(document).on( 'heartbeat-send.wp-gears-heartbeat-time', function( e, data ) {
data['wp-gears-heartbeat-time'] = 'bootstrap heartbeat';
window.wp.heartbeat.interval( 15 );
});
// Listen for the tick custom event.
$(document).on( 'heartbeat-tick.wp-gears-heartbeat-time', function( e, data ) {
// Double-check we have the data we're listening for
if ( ! data['wp-gears-heartbeat-time'] ) {
return;
}
console.group('Gearman:');
var jsonObj = $.parseJSON( data['wp-gears-heartbeat-time'] );
$.each( jsonObj, function( k, v ) {
//convert the epoch time to a readable format
var time_from_gearman = new Date( v * 1000 );
console.log( time_from_gearman.toLocaleString() );
});
console.groupEnd();
});
// Initial connection to cement our new interval timing
window.wp.heartbeat.connectNow();
}(jQuery));
</script>
<?php
}
@soderlind
Copy link
Copy Markdown
Author

I'm testing WP Gears together with the Heartbeat API. The idea is that heartbeat should update status when a gearman job is done.

I had a race condition, it is fixed now: 10up/WP-Minions#5 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment