| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module: Timings. |
| 4 |
* |
| 5 |
* Delivery/pickup date and time and lead times. |
| 6 |
* |
| 7 |
* @package Orderable/Classes |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Timings module class. |
| 14 |
*/ |
| 15 |
class Orderable_Timings { |
| 16 |
/** |
| 17 |
* Init. |
| 18 |
*/ |
| 19 |
public static function run() { |
| 20 |
self::load_classes(); |
| 21 |
add_action( 'init', array( __CLASS__, 'add_shortcodes' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Load classes for this module. |
| 26 |
*/ |
| 27 |
public static function load_classes() { |
| 28 |
$classes = array( |
| 29 |
'timings-blocks' => 'Orderable_Timings_Blocks', |
| 30 |
'timings-settings' => 'Orderable_Timings_Settings', |
| 31 |
'timings-checkout' => 'Orderable_Timings_Checkout', |
| 32 |
'timings-order' => 'Orderable_Timings_Order', |
| 33 |
); |
| 34 |
|
| 35 |
foreach ( $classes as $file_name => $class_name ) { |
| 36 |
require_once ORDERABLE_MODULES_PATH . 'timings/class-' . $file_name . '.php'; |
| 37 |
|
| 38 |
$class_name::run(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Add services shrotcodes. |
| 44 |
*/ |
| 45 |
public static function add_shortcodes() { |
| 46 |
add_shortcode( 'orderable-open-hours', array( __CLASS__, 'orderable_open_hours_shortcode' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get days of the week. |
| 51 |
* |
| 52 |
* @param string $label Label length 'full' or 'short'. |
| 53 |
* @param int $last_day Last day of the week. |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public static function get_days_of_the_week( $label = 'full', $last_day = 6 ) { |
| 58 |
$days = array( |
| 59 |
0 => 'full' === $label ? __( 'Sunday', 'orderable' ) : __( 'Sun', 'orderable' ), |
| 60 |
1 => 'full' === $label ? __( 'Monday', 'orderable' ) : __( 'Mon', 'orderable' ), |
| 61 |
2 => 'full' === $label ? __( 'Tuesday', 'orderable' ) : __( 'Tue', 'orderable' ), |
| 62 |
3 => 'full' === $label ? __( 'Wednesday', 'orderable' ) : __( 'Wed', 'orderable' ), |
| 63 |
4 => 'full' === $label ? __( 'Thursday', 'orderable' ) : __( 'Thu', 'orderable' ), |
| 64 |
5 => 'full' === $label ? __( 'Friday', 'orderable' ) : __( 'Fri', 'orderable' ), |
| 65 |
6 => 'full' === $label ? __( 'Saturday', 'orderable' ) : __( 'Sat', 'orderable' ), |
| 66 |
); |
| 67 |
|
| 68 |
if ( 6 !== $last_day ) { |
| 69 |
$index = $last_day + 1; |
| 70 |
$start = array_slice( $days, $index, null, true ); |
| 71 |
$end = array_slice( $days, 0, $index, true ); |
| 72 |
|
| 73 |
$days = $start + $end; |
| 74 |
} |
| 75 |
|
| 76 |
return $days; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get formatted date. |
| 81 |
* |
| 82 |
* @param int $timestamp GMT timestamp |
| 83 |
* |
| 84 |
* @return string|void |
| 85 |
* @throws Exception |
| 86 |
*/ |
| 87 |
public static function get_formatted_date( $timestamp ) { |
| 88 |
$format = get_option( 'date_format' ); |
| 89 |
$timestamp_adjusted = self::get_timestamp_adjusted( $timestamp ); |
| 90 |
$date = date_i18n( $format, $timestamp_adjusted ); |
| 91 |
|
| 92 |
if ( self::is_today( $timestamp ) ) { |
| 93 |
$date = __( 'Today', 'orderable' ); |
| 94 |
} elseif ( self::is_tomorrow( $timestamp ) ) { |
| 95 |
$date = __( 'Tomorrow', 'orderable' ); |
| 96 |
} |
| 97 |
|
| 98 |
return apply_filters( 'orderable_get_formatted_date', $date, $timestamp ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Is this timestamp today? |
| 103 |
* |
| 104 |
* @param int $timestamp GMT timestamp. |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
public static function is_today( $timestamp ) { |
| 109 |
$timestamp_date = date( 'Y-m-d', self::get_timestamp_adjusted( $timestamp ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 110 |
$current_date = current_time( 'Y-m-d' ); |
| 111 |
|
| 112 |
return $timestamp_date === $current_date; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Is this timestamp today? |
| 117 |
* |
| 118 |
* @param int $timestamp GMT timestamp |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
* @throws Exception |
| 122 |
*/ |
| 123 |
public static function is_tomorrow( $timestamp ) { |
| 124 |
$timestamp_date = date( 'Y-m-d', self::get_timestamp_adjusted( $timestamp ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 125 |
$current_date = current_time( 'timestamp' ); |
| 126 |
$tomorrows_date = date( 'Y-m-d', strtotime( '+1 day', $current_date ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 127 |
|
| 128 |
return $timestamp_date === $tomorrows_date; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Is weekday? |
| 133 |
* |
| 134 |
* @param int $timestamp GMT timestamp. |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public static function is_weekday( $timestamp ) { |
| 139 |
$weekdays = array( 1, 2, 3, 4, 5 ); |
| 140 |
$timestamp_date = absint( date( 'w', self::get_timestamp_adjusted( $timestamp ) ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 141 |
|
| 142 |
return in_array( $timestamp_date, $weekdays, true ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get timezone offset for the given timestamp. |
| 147 |
* |
| 148 |
* This function is inspired by wc_timezone_offset(). |
| 149 |
* wc_timezone_offset() always returns the offset for today's date, |
| 150 |
* which is inacurate during daylight savings. |
| 151 |
* Hence this function, which returns offset for specified date. |
| 152 |
* |
| 153 |
* @param int $timestamp Timestamp. |
| 154 |
* |
| 155 |
* @return mixed |
| 156 |
*/ |
| 157 |
public static function get_timezone_offset( $timestamp ) { |
| 158 |
$timezone = get_option( 'timezone_string' ); |
| 159 |
|
| 160 |
if ( $timezone ) { |
| 161 |
$timezone_object = new DateTimeZone( $timezone ); |
| 162 |
$datetime = new DateTime(); |
| 163 |
|
| 164 |
$datetime->setTimestamp( $timestamp ); |
| 165 |
|
| 166 |
return $timezone_object->getOffset( $datetime ); |
| 167 |
} else { |
| 168 |
return floatval( get_option( 'gmt_offset', 0 ) ) * HOUR_IN_SECONDS; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Adjust GMT timestamp to correct time zone. |
| 174 |
* |
| 175 |
* @param int $timestamp Timestamp (GMT). |
| 176 |
* |
| 177 |
* @return float|int|mixed |
| 178 |
*/ |
| 179 |
public static function get_timestamp_adjusted( $timestamp ) { |
| 180 |
return $timestamp + self::get_timezone_offset( $timestamp ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Open hours shortcode. |
| 185 |
* |
| 186 |
* @param array $args |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public static function orderable_open_hours_shortcode( $args = array() ) { |
| 191 |
$defaults = array( |
| 192 |
'location_id' => null, |
| 193 |
'services' => true, |
| 194 |
'date' => true, |
| 195 |
); |
| 196 |
|
| 197 |
$args = wp_parse_args( $args, $defaults ); |
| 198 |
$args['services'] = (bool) json_decode( strtolower( $args['services'] ) ); |
| 199 |
$args['date'] = (bool) json_decode( strtolower( $args['date'] ) ); |
| 200 |
$args['location'] = empty( $args['location_id'] ) || ! Orderable_Location::store_has_multi_locations() ? Orderable_Location::get_selected_location() : new Orderable_Location_Single( $args['location_id'] ); |
| 201 |
$args['upcoming_open_hours'] = $args['location']->get_upcoming_open_hours(); |
| 202 |
|
| 203 |
ob_start(); |
| 204 |
|
| 205 |
include Orderable_Helpers::get_template_path( 'open-hours.php', 'timings' ); |
| 206 |
|
| 207 |
return ob_get_clean(); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get date/time by timestamp in correct timezone. |
| 212 |
* |
| 213 |
* @param $timestamp |
| 214 |
* |
| 215 |
* @return DateTime |
| 216 |
* @throws Exception |
| 217 |
*/ |
| 218 |
public static function get_date_time_by_timestamp( $timestamp ) { |
| 219 |
$date = new DateTime( 'now', wp_timezone() ); |
| 220 |
$date->setTimestamp( $timestamp ); |
| 221 |
|
| 222 |
return $date; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Convert time array to 24 hour. |
| 227 |
* |
| 228 |
* @param array $time |
| 229 |
* |
| 230 |
* @return array |
| 231 |
*/ |
| 232 |
public static function convert_time_to_24_hour( $time ) { |
| 233 |
$time['hour'] = absint( $time['hour'] ); |
| 234 |
|
| 235 |
if ( 'PM' === $time['period'] && 12 !== $time['hour'] ) { |
| 236 |
$time['hour'] = $time['hour'] + 12; |
| 237 |
} |
| 238 |
|
| 239 |
if ( 'AM' === $time['period'] && 12 === $time['hour'] ) { |
| 240 |
$time['hour'] = 0; |
| 241 |
} |
| 242 |
|
| 243 |
return $time; |
| 244 |
} |
| 245 |
} |
| 246 |
|