Permalink
Browse files

Fire manual backups using ajax, completely removes the reliance on wp…

…-cron for manual backups.
  • Loading branch information...
1 parent 28f7fa9 commit 7d4b53611417d11ddb3c0669779e9b7669e66bf5 @willmot willmot committed Jan 2, 2012
Showing with 70 additions and 40 deletions.
  1. +31 −19 admin.actions.php
  2. +9 −0 admin.backup-button.php
  3. +4 −10 admin.page.php
  4. +2 −1 assets/hmbkp.css
  5. +24 −10 assets/hmbkp.js
View
@@ -117,7 +117,7 @@ function hmbkp_request_delete_backup() {
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_delete_backup' );
/**
- * Schedule a one time backup and then
+ * Perform a manual backup and then
* redirect back to the backups page
*/
function hmbkp_request_do_backup() {
@@ -126,31 +126,30 @@ function hmbkp_request_do_backup() {
if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'hmbkp_backup_now' || hmbkp_is_in_progress() || ! hmbkp_possible() )
return false;
- // If cron is disabled for manual backups
- if ( ( defined( 'HMBKP_DISABLE_MANUAL_BACKUP_CRON' ) && HMBKP_DISABLE_MANUAL_BACKUP_CRON ) || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
+ hmbkp_do_backup();
- hmbkp_do_backup();
-
- // If not fire the cron
- } else {
+ // Redirect back
+ wp_redirect( remove_query_arg( 'action' ), 303 );
+ exit;
- // Schedule a single backup
- wp_schedule_single_event( time(), 'hmbkp_schedule_single_backup_hook' );
+}
+add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_do_backup' );
- // Remove the once every 60 seconds limitation
- delete_transient( 'doing_cron' );
+/**
+ * Perform a manual backup via ajax
+ */
+function hmbkp_ajax_request_do_backup() {
- // Fire the cron now
- spawn_cron();
+ ignore_user_abort( true );
- }
+ // Are we sure
+ if ( hmbkp_is_in_progress() || ! hmbkp_possible() )
+ return false;
- // Redirect back
- wp_redirect( remove_query_arg( 'action' ), 303 );
- exit;
+ hmbkp_do_backup();
}
-add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_do_backup' );
+add_action( 'wp_ajax_hmbkp_backup', 'hmbkp_ajax_request_do_backup' );
/**
* Send the download file to the browser and
@@ -172,6 +171,19 @@ function hmbkp_request_download_backup() {
}
add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_download_backup' );
+function hmbkp_request_cancel_backup() {
+
+ if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'hmbkp_cancel' )
+ return false;
+
+ if ( file_exists( hmbkp_path() . '/.backup_running' ) )
+ unlink( hmbkp_path() . '/.backup_running' );
+
+ wp_redirect( remove_query_arg( 'action' ), 303 );
+
+}
+add_action( 'load-tools_page_' . HMBKP_PLUGIN_SLUG, 'hmbkp_request_cancel_backup' );
+
/**
* Display the running status via ajax
*
@@ -213,7 +225,7 @@ function hmbkp_ajax_cron_test() {
$response = wp_remote_get( site_url( 'wp-cron.php' ) );
if ( ! is_wp_error( $response ) && $response['response']['code'] != '200' )
- echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has detected a problem.', 'hmbkp' ) . '</strong> ' . sprintf( __( '%s is returning a %s response which could mean cron jobs aren\'t getting fired properly. BackUpWordPress relies on wp-cron to run back ups in a separate process.', 'hmbkp' ), '<code>wp-cron.php</code>', '<code>' . $response['response']['code'] . '</code>' ) . '</p></div>';
+ echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __( 'BackUpWordPress has detected a problem.', 'hmbkp' ) . '</strong> ' . sprintf( __( '%s is returning a %s response which could mean cron jobs aren\'t getting fired properly. BackUpWordPress relies on wp-cron to run scheduled back ups. See the %s for more details.', 'hmbkp' ), '<code>wp-cron.php</code>', '<code>' . $response['response']['code'] . '</code>', '<a href="http://wordpress.org/extend/plugins/backupwordpress/faq/">FAQ</a>' ) . '</p></div>';
else
echo 1;
@@ -0,0 +1,9 @@
+<?php if ( hmbkp_is_in_progress() ) : ?>
+
+ <a id="hmbkp_backup" class="add-new-h2 hmbkp_running" href="tools.php?page=<?php echo $_GET['page']; ?>&amp;action=hmbkp_cancel"><?php echo hmbkp_get_status(); ?></a>
+
+<?php elseif ( hmbkp_possible() ) : ?>
+
+ <a id="hmbkp_backup" class="add-new-h2" href="tools.php?page=<?php echo $_GET['page']; ?>&amp;action=hmbkp_backup_now"><?php _e( 'Back Up Now', 'hmbkp' ); ?></a>
+
+<?php endif; ?>
View
@@ -5,16 +5,10 @@
<h2>
<?php _e( 'Manage Backups', 'hmbkp' ); ?>
+
+ <?php include_once( HMBKP_PLUGIN_PATH . '/admin.backup-button.php' ); ?>
-<?php if ( hmbkp_is_in_progress() ) : ?>
- <a class="add-new-h2" <?php disabled( true ); ?>><img src="<?php echo site_url( 'wp-admin/images/wpspin_light.gif' ); ?>" width="16" height="16" /><?php echo hmbkp_get_status(); ?></a>
-
-<?php elseif ( hmbkp_possible() ) : ?>
- <a class="add-new-h2" href="tools.php?page=<?php echo $_GET['page']; ?>&amp;action=hmbkp_backup_now"><?php _e( 'Back Up Now', 'hmbkp' ); ?></a>
-
-<?php endif; ?>
-
- <a href="#hmbkp-settings" class="add-new-h2 hmbkp-settings-toggle"><?php _e( 'Settings', 'hmbkp' ); ?></a>
+ <a class="add-new-h2 hmbkp-settings-toggle" href="#hmbkp-settings"><?php _e( 'Settings', 'hmbkp' ); ?></a>
</h2>
@@ -31,7 +25,7 @@
<?php endif; ?>
<?php include_once( HMBKP_PLUGIN_PATH . '/admin.settings.php' ); ?>
-
+
<p class="howto"><?php printf( __( 'If you need help getting things working you are more than welcome to email us at %s and we\'ll do what we can to help.', 'hmbkp' ), '<a href="mailto:support@humanmade.co.uk">support@humanmade.co.uk</a>' ); ?></p>
</div>
View
@@ -19,4 +19,5 @@ tfoot p { margin: 0; font-weight: normal; }
.completed th, .completed td { background-color: #FFFFE0; }
.hmbkp_active:before { content: "\00a0 \2713 "; font-size: 11px; }
.hmbkp_active, .hmbkp_active code, #hmbkp-constants .hmbkp_active + dd { background: #E5F7E8; }
-.contextual-help-tabs-wrap .updated { margin: 15px 0 0; }
+.contextual-help-tabs-wrap .updated { margin: 15px 0 0; }
+#hmbkp_backup.hmbkp_running { background-image: url('../../../../wp-admin/images/wpspin_light.gif'); background-repeat: no-repeat; background-position: 2px 2px; padding-left: 20px; }
View
@@ -13,7 +13,7 @@ jQuery( document ).ready( function( $ ) {
}
);
}
-
+
$.get( ajaxurl, { 'action' : 'hmbkp_cron_test' },
function( data ) {
if ( data != 1 ) {
@@ -22,29 +22,43 @@ jQuery( document ).ready( function( $ ) {
}
);
+ $( '#hmbkp_backup' ).click( function( e ) {
+
+ ajaxRequest = $.get( ajaxurl, { 'action' : 'hmbkp_backup' } );
+
+ setTimeout( function() {
+
+ ajaxRequest.abort();
+
+ hmbkpRedirectOnBackupComplete();
+
+ }, 50 );
+
+ e.preventDefault();
+
+ } );
+
$( '.hmbkp-settings-toggle' ).click( function( e ) {
$( '#hmbkp-settings' ).toggle();
-
+
e.preventDefault();
} );
-
+
if ( typeof( screenMeta ) != 'undefined' ) {
$( '.hmbkp-show-help-tab' ).click( screenMeta.toggleEvent );
}
-
+
if ( window.location.hash == '#hmbkp-settings' ){
- $( '#hmbkp-settings' ).show();
+ $( '#hmbkp-settings' ).show();
}
-
+
} );
function hmbkpRedirectOnBackupComplete() {
- img = jQuery( '<div>' ).append( jQuery( '.hmbkp_running a.add-new-h2[disabled]:first img' ).clone() ).remove().html();
-
jQuery.get( ajaxurl, { 'action' : 'hmbkp_is_in_progress' },
function( data ) {
@@ -55,9 +69,9 @@ function hmbkpRedirectOnBackupComplete() {
} else {
- setTimeout( 'hmbkpRedirectOnBackupComplete();', 5000 );
+ setTimeout( 'hmbkpRedirectOnBackupComplete();', 1000 );
- jQuery( '.hmbkp_running a.add-new-h2[disabled]:first' ).html( img + data );
+ jQuery( '#hmbkp_backup' ).addClass( 'hmbkp_running' ).text( data );
}
}

0 comments on commit 7d4b536

Please sign in to comment.