Skip to content

Instantly share code, notes, and snippets.

@atamaniuc
Forked from pento/backup-scheduler.php
Created November 12, 2013 13:26
Show Gist options
  • Save atamaniuc/7430762 to your computer and use it in GitHub Desktop.
Save atamaniuc/7430762 to your computer and use it in GitHub Desktop.
<?php
function my_backup_scheduler( $event ) {
// 'wp_maybe_auto_update' is the cron hook for the auto update process
if ( 'wp_maybe_auto_update' !== $event['hook'] )
return;
wp_schedule_single_event( $event['timestamp'] - 5 * MINUTE_IN_SECONDS, 'my_backup' );
}
add_filter( 'schedule_event', 'my_backup_scheduler', 10, 1 );
function my_backup() {
$will_probably_update = false;
$au = new WP_Automatic_Updater;
wp_update_plugins(); // Check for Plugin updates
$plugin_updates = get_site_transient( 'update_plugins' );
if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
foreach ( array_keys( $plugin_updates->response ) as $plugin ) {
if ( $au->should_update( 'plugin', $plugin, WP_PLUGIN_DIR ) ) {
$will_probably_update = true;
break;
}
}
}
if ( ! $will_probably_update ) {
wp_update_themes(); // Check for Theme updates
$theme_updates = get_site_transient( 'update_themes' );
if ( $theme_updates && !empty( $theme_updates->response ) ) {
foreach ( array_keys( $theme_updates->response ) as $theme ) {
if ( $au->should_update( 'theme', $theme, get_theme_root( $theme ) ) ) {
$will_probably_update = true;
break;
}
}
}
if ( ! $will_probably_update ) {
wp_version_check(); // Check for Core updates
$core_update = find_core_auto_update();
if ( $core_update && $au->should_update( 'core', $core_update, ABSPATH ) )
$will_probably_update = true;
}
if ( $will_probably_update ) {
// There are updates, so you should do your backup
}
}
add_action( 'my_backup', 'my_backup' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment