Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created October 24, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/23d125268684bc26bd8307285ab847a3 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/23d125268684bc26bd8307285ab847a3 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Appointments +] - Exclude specific hours
* Plugin URI: https://premium.wpmudev.org/
* Description: A helper snippet to exclude specific times from specific days
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'app-is_busy', function( $is_busy, $period ){
/**
* ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
* Days format :
* 1 = Monday
* 2 = Tuesday
* 3 = Wednesday
* 4 = Thursday
* 5 = Friday
* 6 = Saturday
* 7 = Sunday
*/
$exclusions = array();
// Thurday 11:00 to 11:30 is unavailable
$exclusions[] = array(
'day' => 4,
'start' => "11:00",
'end' => '11:30',
);
// Thurday 13:30 to 15:00 is unavailable
$exclusions[] = array(
'day' => 4,
'start' => "13:30",
'end' => '15:00',
);
// Friday 12:00 to 14:40 is unavailable
$exclusions[] = array(
'day' => 5,
'start' => "12:00",
'end' => '14:40',
);
$period_start = $period->get_start();
$period_end = $period->get_end();
$period_start_time = strtotime( date( "H:i", $period_start ) );
$period_end_time = strtotime( date( "H:i", $period_end ) );
$current_day_num = date( 'N', $period_start );
// Quicly skip check for irrelevant days
$excluded_day_nums = array_unique( array_column( $exclusions, 'day') );
if ( ! in_array( $current_day_num, $excluded_day_nums ) ) {
return $is_busy;
}
// Check days and times
$current_day_exclusions = array();
foreach( $exclusions as $key => $exclusion ) {
if ( $current_day_num != $exclusion[ 'day' ] ) {
continue;
}
$excluded_start_time = strtotime( $exclusion[ 'start' ] );
$excluded_end_time = strtotime( $exclusion[ 'end' ] );
if (
( $period_start_time >= $excluded_start_time && $period_start_time <= $excluded_end_time )
// ||
//( $period_end_time >= $excluded_start_time && $period_end_time <= $excluded_end_time )
){
$is_busy = true;
break;
}
}
return $is_busy;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment