/** * 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; }
public function setUp() { $this->schedule_intervals = HMBKP_Scheduled_Backup::get_cron_schedules(); }
/** * @param string $type the type of the schedule * @param array $times { * An array of time arguments. Optional. * * @type int $minutes The minute to start the schedule on. Defaults to current time + 10 minutes. Accepts * any valid `date( 'i' )` output. * @type int $hours The hour to start the schedule on. Defaults to current time + 10 minutes. Accepts * any valid `date( 'G' )` output. * @type string $day_of_week The day of the week to start the schedule on. Defaults to current time + 10 minutes. Accepts * any valid `date( 'l' )` output. * @type int $day_of_month The day of the month to start the schedule on. Defaults to current time + 10 minutes. Accepts * any valid `date( 'j' )` output. * @type int $now The current time. Defaults to `time()`. Accepts any valid timestamp. * * } * @return int $timestamp Returns the resulting timestamp on success and Int 0 on failure */ function hmbkp_determine_start_time($type, $times = array()) { // Default to in 10 minutes if (!empty($times['now'])) { $default_timestamp = $times['now'] + 600; } else { $default_timestamp = time() + 600; } $default_times = array('minutes' => date('i', $default_timestamp), 'hours' => date('G', $default_timestamp), 'day_of_week' => date('l', $default_timestamp), 'day_of_month' => date('j', $default_timestamp), 'now' => time()); $args = wp_parse_args($times, $default_times); $schedule_start = ''; $intervals = HMBKP_Scheduled_Backup::get_cron_schedules(); // Allow the hours and minutes to be overwritten by a constant if (defined('HMBKP_SCHEDULE_TIME') && HMBKP_SCHEDULE_TIME) { $hm = HMBKP_SCHEDULE_TIME; } else { $hm = $args['hours'] . ':' . $args['minutes'] . ':00'; } switch ($type) { case 'hmbkp_hourly': case 'hmbkp_daily': case 'hmbkp_twicedaily': // The next occurance of the specified time $schedule_start = $hm; break; case 'hmbkp_weekly': case 'hmbkp_fortnightly': // The next day of the week at the specified time $schedule_start = $args['day_of_week'] . ' ' . $hm; break; case 'hmbkp_monthly': // The occurance of the time on the specified day of the month $schedule_start = date('F', $args['now']) . ' ' . $args['day_of_month'] . ' ' . $hm; // If we've already gone past that day this month then we'll need to start next month if (strtotime($schedule_start, $args['now']) <= $args['now']) { $schedule_start = date('F', strtotime('+ 1 month', $args['now'])) . ' ' . $args['day_of_month'] . ' ' . $hm; } // If that's still in the past then we'll need to jump to next year if (strtotime($schedule_start, $args['now']) <= $args['now']) { $schedule_start = date('F', strtotime('+ 1 month', $args['now'])) . ' ' . $args['day_of_month'] . ' ' . date('Y', strtotime('+ 1 year', $args['now'])) . ' ' . $hm; } break; default: return 0; break; } $timestamp = strtotime($schedule_start, $args['now']); // Convert to UTC $timestamp -= get_option('gmt_offset') * 3600; // If the scheduled time already passed then keep adding the interval until we get to a future date while ($timestamp <= $args['now']) { $timestamp += $intervals[$type]['interval']; } return $timestamp; }
/** * Setup the schedule * * @access public */ public function setUp() { $this->schedule = new HMBKP_Scheduled_Backup('unit-test'); $this->recurrences = HMBKP_Scheduled_Backup::get_cron_schedules(); }