Example #1
0
/**
 * Display the row of actions for a schedule
 *
 * @access public
 * @param HM\BackUpWordPress\Scheduled_Backup $schedule
 * @return void
 */
function hmbkp_schedule_status(HM\BackUpWordPress\Scheduled_Backup $schedule, $echo = true)
{
    ob_start();
    ?>

	<span class="hmbkp-status"<?php 
    if ($schedule->get_status()) {
        ?>
 title="<?php 
        printf(__('Started %s ago', 'backupwordpress'), human_time_diff($schedule->get_schedule_running_start_time()));
        ?>
"<?php 
    }
    ?>
>
		<?php 
    echo $schedule->get_status() ? wp_kses_data($schedule->get_status()) : __('Starting Backup', 'backupwordpress');
    ?>
		<a href="<?php 
    echo hmbkp_admin_action_url('request_cancel_backup', array('hmbkp_schedule_id' => $schedule->get_id()));
    ?>
"><?php 
    _e('cancel', 'backupwordpress');
    ?>
</a>
	</span>

	<?php 
    $output = ob_get_clean();
    if (!$echo) {
        return $output;
    }
    echo $output;
}
 /**
  * Setup the schedule
  *
  */
 public function setUp()
 {
     $this->schedule = new HM\BackUpWordPress\Scheduled_Backup('unit-test');
     $this->recurrences = HM\BackUpWordPress\Scheduled_Backup::get_cron_schedules();
 }
 function update_schedule()
 {
     $sch_id = isset($_POST['schedule_id']) ? $_POST['schedule_id'] : 0;
     $sch_id = sanitize_text_field(urldecode($sch_id));
     $options = isset($_POST['options']) ? unserialize(base64_decode($_POST['options'])) : false;
     if (!is_array($options) || empty($options) || empty($sch_id)) {
         return array('error' => 'Error: Schedule data');
     }
     $current_value = get_option('hmbkp_schedule_' . $sch_id);
     if (is_array($current_value) && isset($current_value['excludes'])) {
         // do not update 'excludes' value
         $options['excludes'] = $current_value['excludes'];
     }
     $out = array();
     if (is_array($options)) {
         update_option('hmbkp_schedule_' . $sch_id, $options);
         $out['result'] = 'SUCCESS';
     } else {
         $out['result'] = 'NOTCHANGE';
     }
     $schedule = new HM\BackUpWordPress\Scheduled_Backup($sch_id);
     if (!empty($options['reoccurrence']) && !empty($options['schedule_start_time'])) {
         // Calculate the start time depending on the recurrence
         $start_time = $options['schedule_start_time'];
         if ($start_time) {
             $schedule->set_schedule_start_time($start_time);
         }
     }
     if (!empty($options['reoccurrence'])) {
         $schedule->set_reoccurrence($options['reoccurrence']);
     }
     $out['next_occurrence'] = $schedule->get_next_occurrence(false);
     return $out;
 }
Example #4
0
/**
 * Display the running status via ajax
 */
function hmbkp_ajax_is_backup_in_progress()
{
    check_ajax_referer('hmbkp_nonce', 'nonce');
    if (empty($_POST['hmbkp_schedule_id'])) {
        die;
    }
    $schedule = new HM\BackUpWordPress\Scheduled_Backup(sanitize_text_field(urldecode($_POST['hmbkp_schedule_id'])));
    if (!$schedule->get_status()) {
        echo 0;
    } else {
        hmbkp_schedule_status($schedule);
    }
    die;
}
Example #5
0
/**
 * @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);
    $intervals = HM\BackUpWordPress\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 'hourly':
        case 'daily':
        case 'twicedaily':
            // The next occurance of the specified time
            $schedule_start = $hm;
            break;
        case 'weekly':
        case 'fortnightly':
            // The next day of the week at the specified time
            $schedule_start = $args['day_of_week'] . ' ' . $hm;
            break;
        case '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;
    }
    $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;
}
/**
 * Returns a formatted string containing the calculated total site size or a message
 * to indicate it is being calculated.
 *
 * @param HM\BackUpWordPress\Scheduled_Backup $schedule
 *
 * @return string
 */
function hmbkp_get_site_size_text(HM\BackUpWordPress\Scheduled_Backup $schedule)
{
    if (isset($_GET['hmbkp_add_schedule'])) {
        return '';
    } elseif ('database' === $schedule->get_type() || $schedule->is_site_size_cached()) {
        return sprintf('(<code title="' . __('Backups will be compressed and should be smaller than this.', 'backupwordpress') . '">%s</code>)', esc_attr($schedule->get_formatted_site_size()));
    } else {
        return sprintf('(<code class="calculating" title="' . __('this shouldn\'t take long&hellip;', 'backupwordpress') . '">' . __('calculating the size of your backup&hellip;', 'backupwordpress') . '</code>)');
    }
}