Daily Form Schedule

Allow your form schedule to apply daily.

Usage

  1. Install and activate.
  2. Set your form schedule via Form Settings › Restrictions › Schedule form. a. Specify your desired daily start and end times via the Start and End Time settings. b. Leave the Start and End Date inputs EMPTY!

Want to make your schedule weekly?

  1. Follow the steps above.
  2. Specify the day of the week your schedule starts and ends with it’s numeric equivalent. a. Sunday = 0 Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4 Friday = 5 Saturday = 6 b. Enter the applicable day of the week number into the Start and End Date inputs respectively.

That’s it. This super simple plugin will automatically ensure that your schedule will apply daily (or weekly). 🙂

Code

Filename: gw-daily-form-schedule.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Daily Form Schedule
 *
 * Allow your form schedule to apply daily.
 *
 * @version  1.3
 * @author   David Smith <david@gravitywiz.com>
 * @license  GPL-2.0+
 * @link     https://gravitywiz.com/
 *
 * Plugin Name:  Gravity Forms Daily Form Schedule
 * Plugin URI:   https://gravitywiz.com/
 * Description:  Allow your form schedule to apply daily.
 * Author:       Gravity Wiz
 * Version:      1.3
 * Author URI:   https://gravitywiz.com/
 *
 * Usage:
 *
 * 1. Install and activate.
 * 2. Set your form schedule via Form Settings › Restrictions › Schedule form.
 *  a. Specify your desired daily start and end times via the Start and End Time settings.
 *  b. Leave the Start and End Date inputs EMPTY!
 *
 * Want to make your schedule weekly?
 *
 * 1. Follow the steps above.
 * 2. Specify the day of the week your schedule starts and ends with it's numeric equivalent.
 *  a. Sunday    = 0
 *     Monday    = 1
 *     Tuesday   = 2
 *     Wednesday = 3
 *     Thursday  = 4
 *     Friday    = 5
 *     Saturday  = 6
 *  b. Enter the applicable day of the week number into the Start and End Date inputs respectively.
 *
 * That's it. This super simple plugin will automatically ensure that your schedule will apply daily (or weekly). 🙂
 */
add_filter( 'gform_pre_render', 'gw_daily_form_schedule' );
add_filter( 'gform_pre_validation', 'gw_daily_form_schedule' );
function gw_daily_form_schedule( $form ) {

	// Skip "Gravity Forms Daily Form Schedule" for the forms having set schedule start and schedule end date via Form Settings.
	if ( strstr( $form['scheduleStart'], '/' ) || strstr( $form['scheduleEnd'], '/' ) ) {
		return $form;
	}

	$days = array( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' );

	if ( rgar( $form, 'scheduleForm' ) ) {

		// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
		$time         = current_time( 'timestamp' );
		$is_interweek = false;

		if ( ! $form['scheduleStart'] || $form['scheduleStart'] <= 6 ) {
			if ( ! rgblank( $form['scheduleStart'] ) && $form['scheduleStart'] <= 6 ) {

				$is_interweek = $form['scheduleStart'] > $form['scheduleEnd'];
				// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
				$current_day = (int) date( 'w' );

				// If it's a Thursday and the schedule starts on Friday, assume the current schedule is for the previous week.
				// Pro Tip: In PHP, Monday is the first day of the week.
				$is_current_day_less_than_schedule_day = $current_day < $form['scheduleStart'] && $current_day !== 0;
				$use_last_week                         = $is_interweek && $is_current_day_less_than_schedule_day;
				$week_phrase                           = (int) $form['scheduleStart'] === 0 || $use_last_week ? 'last week' : 'this week';

				// Sunday last week, Monday this week.
				$time = strtotime( "{$days[ $form['scheduleStart'] ]} {$week_phrase}", $time );

			}

			// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
			$form['scheduleStart'] = date( 'm/d/Y', $time );
		}

		if ( ! $form['scheduleEnd'] || $form['scheduleEnd'] <= 6 ) {
			if ( ! rgblank( $form['scheduleEnd'] ) && $form['scheduleEnd'] <= 6 ) {
				$week_phrase = $is_interweek ? 'next week' : 'this week';
				$time        = strtotime( "{$days[ $form['scheduleEnd'] ]} {$week_phrase}", $time );
			}

			// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
			$form['scheduleEnd'] = date( 'm/d/Y', $time );
		}
	}

	return $form;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.