Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JRGould/f051e7f74868bb5eb67ab30012b1451c to your computer and use it in GitHub Desktop.
Save JRGould/f051e7f74868bb5eb67ab30012b1451c to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: WPMDB Pro Migration Complete Notifications
Plugin URI: http://deliciousbrains.com
Description: Get notified when a migration completes
Author: Delicious Brains
Version: 0.0
Author URI: http://deliciousbrains.com
*/
add_action( 'admin_enqueue_scripts', 'jg_heartbeat_enqueue' );
function jg_heartbeat_enqueue( $hook ) {
// Make sure the JS part of the Heartbeat API is loaded.
wp_enqueue_script( 'heartbeat' );
add_action( 'admin_print_footer_scripts', 'jg_heartbeat_js', 20 );
}
function jg_heartbeat_js() {
// only run heartbeats on WP Migrate DB Pro page
if ( ! isset( $_GET['page'] ) || $_GET['page'] != 'wp-migrate-db-pro' ) {
return;
}
?>
<script>
(function($){
// Don't continue if browser not capable
if ( !("Notification" in window) ) {
return;
}
// Get permission if not already granted
if ( 'granted' !== Notification.permission ) {
Notification.requestPermission();
}
// Hook into the heartbeat-send
$(document).on('heartbeat-send', function(e, data) {
data['wpmdb_mig_complete_heartbeat'] = 'check';
});
// Listen for the custom event "heartbeat-tick" on $(document).
$(document).on( 'heartbeat-tick', function(e, data) {
//return if flag is not set
if ( ! data['wpmdb_mig_complete_heartbeat'] ) {
return;
}
//fire notification
if ( 'granted' === Notification.permission ) {
var notification = new Notification( "WP Migrate DB Pro Migration complete!", { body: 'Your migration has completed!', icon: 'https://example.com/logo.png' } );
}
});
}(jQuery));
</script>
<?php
}
add_filter( 'heartbeat_received', 'jg_heartbeat_received', 10, 2 );
function jg_heartbeat_received( $response, $data ) {
if( $data['wpmdb_mig_complete_heartbeat'] == 'check' ) {
if( get_site_transient( 'wpmdb_mig_is_complete' ) ) {
$response['wpmdb_mig_complete_heartbeat'] = "complete";
delete_site_transient( 'wpmdb_mig_is_complete' );
}
}
return $response;
}
add_action( 'wpmdb_migration_complete', 'jg_set_complete_transient', 2, 20 );
function jg_set_complete_transient( $action, $url ) {
global $wp_current_filter;
$is_remote = strstr($wp_current_filter[0], 'nopriv') ? true : false;
// only set transient on site running migration
if ( ! $is_remote ) {
set_site_transient( 'wpmdb_mig_is_complete', true, 500 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment