Ejemplo n.º 1
0
/**
 * Catch the edit schedule form
 *
 * Validate and either return errors or update the schedule
 */
function hmnkp_edit_schedule_submit()
{
    if (empty($_GET['hmbkp_schedule_id'])) {
        die;
    }
    $schedule = new HMBKP_Scheduled_Backup(sanitize_text_field($_GET['hmbkp_schedule_id']));
    $errors = array();
    if (isset($_GET['hmbkp_schedule_type'])) {
        $schedule_type = sanitize_text_field($_GET['hmbkp_schedule_type']);
        if (!trim($schedule_type)) {
            $errors['hmbkp_schedule_type'] = __('Backup type cannot be empty', 'hmbkp');
        } elseif (!in_array($schedule_type, array('complete', 'file', 'database'))) {
            $errors['hmbkp_schedule_type'] = __('Invalid backup type', 'hmbkp');
        } else {
            $schedule->set_type($schedule_type);
        }
    }
    if (isset($_GET['hmbkp_schedule_reoccurrence'])) {
        $schedule_reoccurrence = sanitize_text_field($_GET['hmbkp_schedule_reoccurrence']);
        if (empty($schedule_reoccurrence)) {
            $errors['hmbkp_schedule_reoccurrence'] = __('Schedule cannot be empty', 'hmbkp');
        } elseif (!in_array($schedule_reoccurrence, array_keys($schedule->get_cron_schedules())) && $schedule_reoccurrence !== 'manually') {
            $errors['hmbkp_schedule_reoccurrence'] = __('Invalid schedule', 'hmbkp');
        } else {
            $schedule->set_reoccurrence($schedule_reoccurrence);
        }
    }
    if (isset($_GET['hmbkp_schedule_max_backups'])) {
        $schedule_max_backups = sanitize_text_field($_GET['hmbkp_schedule_max_backups']);
        if (empty($schedule_max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups can\'t be empty', 'hmbkp');
        } elseif (!is_numeric($schedule_max_backups)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups must be a number', 'hmbkp');
        } elseif (!($schedule_max_backups >= 1)) {
            $errors['hmbkp_schedule_max_backups'] = __('Max backups must be greater than 0', 'hmbkp');
        } else {
            $schedule->set_max_backups((int) $schedule_max_backups);
        }
        // Remove any old backups in-case max backups was reduced
        $schedule->delete_old_backups();
    }
    // Save the service options
    foreach (HMBKP_Services::get_services($schedule) as $service) {
        $errors = array_merge($errors, $service->save());
    }
    $schedule->save();
    if ($errors) {
        echo json_encode($errors);
    }
    die;
}