-
-
Save tommcfarlin/f1d8785e16d8e4780d92 to your computer and use it in GitHub Desktop.
[WordPress] A practical example of the Heartbeat API.
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
(function( $ ) { | |
"use strict"; | |
$(function() { | |
$( document ).on( 'heartbeat-send', function( e, data ) { | |
data['client'] = 'acme_request_embed_type'; | |
}); | |
$( document ).on( 'heartbeat-tick', function( e, data ) { | |
switch ( data['server'] ) { | |
case 'hide_video': | |
if( false === $('#bumpers').is(':visible') ) { | |
location.reload(); | |
} // end if | |
break; | |
case 'display_video': | |
if( false === $('#video').is(':visible') ) { | |
location.reload(); | |
} // end if | |
break; | |
} // end switch/case | |
}); | |
}); | |
}( jQuery )); |
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 | |
function acme_heartbeat_receive( $response, $data ) { | |
if ( 'acme_request_embed_type' == $data['client'] ) { | |
switch ( get_theme_mod( 'acme_video_embed_display' ) ) { | |
case 'video': | |
$response['server'] = 'display_video'; | |
break; | |
case 'bumpers': | |
default: | |
$response['server'] = 'hide_video'; | |
break; | |
} // end switch/case | |
} // end if | |
return $response; | |
} // end live_heartbeat_receive | |
add_filter( 'heartbeat_received', 'acme_heartbeat_receive', 10, 2 ); | |
add_filter( 'heartbeat_nopriv_received', 'acme_heartbeat_receive', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment