Last active
December 27, 2015 12:59
-
-
Save pento/7330005 to your computer and use it in GitHub Desktop.
Backup scheduler for WordPress Automatic Updates
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 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