Exemplo n.º 1
0
/**
 * Setup the default backup schedules
 */
function hmbkp_setup_default_schedules()
{
    $schedules = HMBKP_Schedules::get_instance();
    if ($schedules->get_schedules()) {
        return;
    }
    /**
     * Schedule a database backup daily and store backups
     * for the last 2 weeks
     */
    $database_daily = new HMBKP_Scheduled_Backup((string) time());
    $database_daily->set_type('database');
    $database_daily->set_schedule_start_time(hmbkp_determine_start_time('hmbkp_daily', array('hours' => '23', 'minutes' => '0')));
    $database_daily->set_reoccurrence('hmbkp_daily');
    $database_daily->set_max_backups(7);
    $database_daily->save();
    /**
     * Schedule a complete backup to run weekly and store backups for
     * the last 3 months
     */
    $complete_weekly = new HMBKP_Scheduled_Backup((string) (time() + 1));
    $complete_weekly->set_type('complete');
    $complete_weekly->set_schedule_start_time(hmbkp_determine_start_time('hmbkp_weekly', array('day_of_week' => 'sunday', 'hours' => '3', 'minutes' => '0')));
    $complete_weekly->set_reoccurrence('hmbkp_weekly');
    $complete_weekly->set_max_backups(3);
    $complete_weekly->save();
    $schedules->refresh_schedules();
    function hmbkp_default_schedules_setup_warning()
    {
        echo '<div id="hmbkp-warning" class="updated fade"><p><strong>' . __('BackUpWordPress has setup your default schedules.', 'hmbkp') . '</strong> ' . __('By default BackUpWordPress performs a daily backup of your database and a weekly backup of your database &amp; files. You can modify these schedules.', 'hmbkp') . '</p></div>';
    }
    add_action('admin_notices', 'hmbkp_default_schedules_setup_warning');
}
Exemplo n.º 2
0
/**
 * Catch the schedule settings form submission
 *
 * Validate and either return errors or update the schedule
 */
function hmbkp_edit_schedule_submit()
{
    check_admin_referer('hmbkp-edit-schedule', 'hmbkp-edit-schedule-nonce');
    if (empty($_POST['hmbkp_schedule_id'])) {
        die;
    }
    $schedule = new HMBKP_Scheduled_Backup(sanitize_text_field($_POST['hmbkp_schedule_id']));
    hmbkp_clear_settings_errors();
    $errors = array();
    $settings = array();
    if (isset($_POST['hmbkp_schedule_type'])) {
        $schedule_type = sanitize_text_field($_POST['hmbkp_schedule_type']);
        if (!trim($schedule_type)) {
            $errors['hmbkp_schedule_type'] = __('Backup type cannot be empty', 'backupwordpress');
        } elseif (!in_array($schedule_type, array('complete', 'file', 'database'))) {
            $errors['hmbkp_schedule_type'] = __('Invalid backup type', 'backupwordpress');
        } else {
            $settings['type'] = $schedule_type;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_type'])) {
        $schedule_recurrence_type = sanitize_text_field($_POST['hmbkp_schedule_recurrence']['hmbkp_type']);
        if (empty($schedule_recurrence_type)) {
            $errors['hmbkp_schedule_recurrence']['hmbkp_type'] = __('Schedule cannot be empty', 'backupwordpress');
        } elseif (!in_array($schedule_recurrence_type, array_keys(hmbkp_get_cron_schedules())) && 'manually' !== $schedule_recurrence_type) {
            $errors['hmbkp_schedule_recurrence']['hmbkp_type'] = __('Invalid schedule', 'backupwordpress');
        } else {
            $settings['recurrence'] = $schedule_recurrence_type;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_week'])) {
        $day_of_week = sanitize_text_field($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_week']);
        if (!in_array($day_of_week, array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'))) {
            $errors['hmbkp_schedule_start_day_of_week'] = __('Day of the week must be a valid lowercase day name', 'backupwordpress');
        } else {
            $settings['start_time']['day_of_week'] = $day_of_week;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_month'])) {
        $day_of_month = absint($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_day_of_month']);
        $options = array('min_range' => 1, 'max_range' => 31);
        if (false === filter_var($day_of_month, FILTER_VALIDATE_INT, array('options' => $options))) {
            $errors['hmbkp_schedule_start_day_of_month'] = __('Day of month must be between 1 and 31', 'backupwordpress');
        } else {
            $settings['start_time']['day_of_month'] = $day_of_month;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_hours'])) {
        $hours = absint($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_hours']);
        $options = array('min_range' => 0, 'max_range' => 23);
        if (false === filter_var($hours, FILTER_VALIDATE_INT, array('options' => $options))) {
            $errors['hmbkp_schedule_start_hours'] = __('Hours must be between 0 and 23', 'backupwordpress');
        } else {
            $settings['start_time']['hours'] = $hours;
        }
    }
    if (isset($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_minutes'])) {
        $minutes = absint($_POST['hmbkp_schedule_recurrence']['hmbkp_schedule_start_minutes']);
        $options = array('min_range' => 0, 'max_range' => 59);
        if (false === filter_var($minutes, FILTER_VALIDATE_INT, array('options' => $options))) {
            $errors['hmbkp_schedule_start_minutes'] = __('Minutes must be between 0 and 59', 'backupwordpress');
        } else {
            $settings['start_time']['minutes'] = $minutes;
        }
    }
    if (isset($_POST['hmbkp_schedule_max_backups'])) {
        $max_backups = sanitize_text_field($_POST['hmbkp_schedule_max_backups']);
        if (empty($max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups can\'t be empty', 'backupwordpress');
        } elseif (!is_numeric($max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups must be a number', 'backupwordpress');
        } elseif (!($max_backups >= 1)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups must be greater than 0', 'backupwordpress');
        } else {
            $settings['max_backups'] = absint($max_backups);
        }
    }
    // Save the service options
    foreach (HMBKP_Services::get_services($schedule) as $service) {
        $errors = array_merge($errors, $service->save());
    }
    if (!empty($settings['recurrence']) && !empty($settings['start_time'])) {
        // Calculate the start time depending on the recurrence
        $start_time = hmbkp_determine_start_time($settings['recurrence'], $settings['start_time']);
        if ($start_time) {
            $schedule->set_schedule_start_time($start_time);
        }
    }
    if (!empty($settings['recurrence'])) {
        $schedule->set_reoccurrence($settings['recurrence']);
    }
    if (!empty($settings['type'])) {
        $schedule->set_type($settings['type']);
    }
    if (!empty($settings['max_backups'])) {
        $schedule->set_max_backups($settings['max_backups']);
    }
    // Save the new settings
    $schedule->save();
    // Remove any old backups in-case max backups was reduced
    $schedule->delete_old_backups();
    if ($errors) {
        foreach ($errors as $error) {
            hmbkp_add_settings_error($error);
        }
    }
    $redirect = remove_query_arg(array('hmbkp_panel', 'action'), wp_get_referer());
    if ($errors) {
        $redirect = wp_get_referer();
    }
    wp_safe_redirect($redirect, '303');
    die;
}
 /**
  * Test that setting the monthly schedule to various future times works as expected
  *
  */
 public function testMonthlyFutureStart()
 {
     self::$now = strtotime('2014-03-05T12:00:00+00:00');
     // 23:59 on the 25th
     $timestamp = hmbkp_determine_start_time('hmbkp_monthly', array('day_of_month' => '25', 'hours' => 23, 'minutes' => 59, 'now' => $this->time()));
     $this->assertEquals(strtotime('2014-03-25T23:59:00+00:00'), $timestamp, '', 30);
     // 23:59 on the Dec 31
     self::$now = strtotime('2013-12-31T23:59:00+00:00');
     $timestamp = hmbkp_determine_start_time('hmbkp_monthly', array('day_of_month' => '31', 'hours' => 23, 'minutes' => 59, 'now' => $this->time()));
     $this->assertEquals(strtotime('2014-01-31T23:59:00+00:00'), $timestamp, '', 30);
 }