seancojr
4/5/2014 - 1:35 AM

Backup scheduler for WordPress Automatic Updates

Backup scheduler for WordPress Automatic Updates

<?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' );