gravityflow_step_schedule_timestamp

Description

The hook gravityflow_step_schedule_timestamp allows customizaton of the step’s schedule time.

Parameters

ParameterTypeDetails
$schedule_timestampIntegerThe current scheduled timestamp (UTC)
$schedule_typeString The type of schedule defined in step settings.
$stepStepThe current step.

Examples

Adjust delay based on a field value

add_filter( 'gravityflow_step_schedule_timestamp', 'schedule_business_hours', 10, 3 );
function schedule_business_hours( $schedule_timestamp, $schedule_type, $step ) {
	//Ensure you are only adjusting the desired form/step
	if ( $step->get_id() !== 76 ) {
		return $schedule_timestamp;
	}
	$entry = $step->get_entry();
	if ( $entry && isset( $entry['2'] ) && $entry['2'] == 'No Service Level Agreement' ) {
		$schedule_timestamp = strtotime('+1 day', $schedule_timestamp );
	}
        return $schedule_timestamp;
}
	

Delay based on business hours

Scheduling a step gives you many options for how to delay the start of a step. This filter supports additional cases where more complex logic is needed to determine when the step should start – aka the length of the delay. 

The example below covers a use case of delaying notifications for a particular step to only occur during business hours. After accounting for your timezone (as defined in WordPress settings), it compares the default scheduled timestamp of 5 minutes to a Monday-to-Friday, 9 to 5 business day. If appropriate, it will delay the notification to the next business day.

  • Create a notification step
    Specify a schedule delay of 5 minutes. This will be the default that applies to steps that are attempted during business hours.
    Take note of the Step ID.

Add the snippet

  • Include it in your theme functions.php file or a site plugin and update the values in lines 4 to 9 to match your scenario:
    – Line 4 – The Step ID (76 in example)
    – Line 8 – The $start_hour variable (24-hour time-zone matched)
    – Line 9 – The $end_hour variable (24-hour time-zone matched)
add_filter( 'gravityflow_step_schedule_timestamp', 'schedule_business_hours', 10, 3 );
function schedule_business_hours( $schedule_timestamp, $schedule_type, $step ) {
	//Ensure you are only adjusting the desired form/step
	if ( $step->get_id() !== 76 ) {
		return $schedule_timestamp;
	}
	gravity_flow()->log_debug( __METHOD__ . '(): Default Schedule: ' . date( 'Y-m-d H:i:s', $schedule_timestamp ) );
	//Define for your business irrespective of time-zone
	$start_hour = 9;
	$end_hour = 17;
	$current_timestamp = time();
	$tz = get_option('timezone_string');
	$dt = new DateTime("now", new DateTimeZone($tz));
	$dt->setTimestamp($current_timestamp);
	$current_hour = $dt->format('H');
	$current_day_of_week = $dt->format('N');
	//Modify the value of $current_timestamp if you want to test the step arriving separate from actual time.
	gravity_flow()->log_debug( __METHOD__ . '(): Comparing '. $current_hour . ' against ' . $start_hour . ' - ' . $end_hour);
	//Weekday + Business Hours
	if ( in_array( $current_day_of_week, array( 1, 2, 3, 4, 5 ) ) && $current_hour >= $start_hour && $current_hour <= $end_hour ) {
		gravity_flow()->log_debug( __METHOD__ . '(): Business Hour Request - Proceeding with default schedule');
		return $schedule_timestamp;
	}
	
	//Weekday + Before Business Hours
	if( in_array( $current_day_of_week, array( 1, 2, 3, 4, 5 ) ) && $current_hour <= $start_hour ) {
		$delay_hours = $start_hour - $current_hour;
	//Mon to Thurs + After Business Hours
	} else if( in_array( $current_day_of_week, array( 1, 2, 3, 4 ) ) && $current_hour >= $end_hour ) {
		$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour;
	//Friday + After Business Hours
	} else if( in_array( $current_day_of_week, array( 5 ) ) && $current_hour >= $end_hour ) {
		$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour + 48;
	//Saturday 
	} else if( in_array( $current_day_of_week, array( 6 ) ) ) {
		$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour + 24;
	//Sunday
	} else if( in_array( $current_day_of_week, array( 7 ) ) ) {
		$delay_hours = ( 23 - $current_hour ) + 1 + $start_hour;
	//Edge case handling
	} else {
		$delay_hours = 0;
	}
	$delayed_timestamp = $schedule_timestamp + ( $delay_hours * 60 * 60 );
	$dt->setTimestamp($delayed_timestamp);
	gravity_flow()->log_debug( __METHOD__ . '(): Outside Business Hour Request');
	gravity_flow()->log_debug( __METHOD__ . '(): Delaying by ' . $delay_hours . ' hour(s) to ' . $dt->format('Y-m-d h:m:s') );
	return $delayed_timestamp;
}

Note: The snippet above does not take into consideration minutes, only the hours and day such that:

  • If the workflow step was requested at 11:43PM on a Tuesday,
    It would be scheduled to send out at 9:43AM Wednesday.
  • If the workflow step was requested at 6:12AM on a Saturday,
    It would be scheduled to send out at 9:12AM Monday.

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?