css
10 months ago
js
10 months ago
class-css.php
10 months ago
class-date.php
10 months ago
class-delivery-type.php
10 months ago
class-display-field.php
10 months ago
class-js.php
10 months ago
class-main.php
10 months ago
class-myaccount.php
10 months ago
class-order.php
10 months ago
class-pickup-location.php
10 months ago
class-shipping-method.php
10 months ago
class-time-range.php
10 months ago
class-time-slot.php
10 months ago
class-time.php
10 months ago
class-validate.php
10 months ago
class-woo-app.php
10 months ago
class-date.php
173 lines
| 1 | <?php |
| 2 | if ( ! defined( 'WPINC' ) ) { |
| 3 | die; |
| 4 | } |
| 5 | class pi_dtt_date{ |
| 6 | |
| 7 | public $format; |
| 8 | public $type; |
| 9 | public $today; |
| 10 | public $preparation_days; |
| 11 | public $pre_order_days; |
| 12 | public $maxDays; |
| 13 | public $allowed_dates; |
| 14 | public $holidays; |
| 15 | |
| 16 | function __construct( $type = "" ){ |
| 17 | $this->format = 'Y/m/d'; |
| 18 | |
| 19 | if(empty( $type )){ |
| 20 | $obj = new pi_dtt_delivery_type(); |
| 21 | $this->type = $obj->getDeliveryType(); |
| 22 | }else{ |
| 23 | $this->type = $type; |
| 24 | } |
| 25 | |
| 26 | $this->today = current_time($this->format); |
| 27 | $this->preparation_days = $this->preparationDaysCalculator(); |
| 28 | $this->pre_order_days = empty(pisol_dtt_get_setting('pi_preorder_days', 10)) ? 0 : abs(pisol_dtt_get_setting('pi_preorder_days', 10)); |
| 29 | $this->maxDays = $this->preparation_days + $this->pre_order_days; |
| 30 | |
| 31 | $this->allowed_dates = $this->allowedDates($this->type); |
| 32 | $this->holidays = $this->getHolidays($this->type); |
| 33 | } |
| 34 | |
| 35 | function preparationDaysCalculator(){ |
| 36 | $preparation_days = (int)pisol_dtt_get_setting('pi_order_preparation_days',0); |
| 37 | return $preparation_days; |
| 38 | } |
| 39 | |
| 40 | static function isDateValid($date, $type = ""){ |
| 41 | $obj = new self($type); |
| 42 | $valid_dates = $obj->getValidDates(); |
| 43 | if(in_array($date, $valid_dates)) return true; |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | function getValidDates(){ |
| 49 | $valid_dates = array(); |
| 50 | |
| 51 | $forced_dates = apply_filters('pisol_forced_valid_dates',array(), $this->type); |
| 52 | if(!empty( $forced_dates )) return self::removeDatesOutSidePreparationTime($forced_dates); |
| 53 | |
| 54 | for($i = $this->preparation_days; $i <= $this->maxDays; $i++){ |
| 55 | $date = date($this->format, strtotime($this->today.' + '.$i.' days')); |
| 56 | |
| 57 | if($this->nonWorkingDay($date)) continue; |
| 58 | |
| 59 | if($this->isHoliday($date)) continue; |
| 60 | |
| 61 | if($this->isTimeNotAvailable( $date )) continue; |
| 62 | |
| 63 | $valid_dates[] = $date; |
| 64 | } |
| 65 | |
| 66 | $valid_dates = apply_filters('pisol_valid_dates', $valid_dates, $this->type); |
| 67 | $valid_dates = self::removePastDates($valid_dates); |
| 68 | $valid_dates = self::removeDatesOutSidePreparationTime($valid_dates); |
| 69 | return is_array($valid_dates) ? array_values($valid_dates) : array(); |
| 70 | } |
| 71 | |
| 72 | function removeDatesOutSidePreparationTime($dates){ |
| 73 | if(apply_filters('pisol_dtt_make_special_dates_not_follow_preparation_time',false)) return $dates; |
| 74 | |
| 75 | $today = current_time('Y/m/d'); |
| 76 | $today_timestamp = strtotime($today); |
| 77 | $first_allowed_date = date('Y/m/d', strtotime($today.' + '.$this->preparation_days.' days')); |
| 78 | foreach($dates as $key => $date){ |
| 79 | if(strtotime($date) < strtotime($first_allowed_date)){ |
| 80 | if(apply_filters('pisol_dtt_make_date_follow_preparation_time',true, $date, $this->preparation_days)){ |
| 81 | unset($dates[$key]); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if(empty($dates)) return array(); |
| 87 | |
| 88 | return is_array($dates) ? array_values($dates) : array(); |
| 89 | } |
| 90 | |
| 91 | static function removePastDates($dates){ |
| 92 | if(!is_array($dates)) array(); |
| 93 | $today = current_time('Y/m/d'); |
| 94 | $today_timestamp = strtotime($today); |
| 95 | $new_dates = array(); |
| 96 | foreach($dates as $date){ |
| 97 | if(!empty($date)){ |
| 98 | $date_timestamp = strtotime($date); |
| 99 | if($date_timestamp >= $today_timestamp){ |
| 100 | $new_dates[] = $date; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return $new_dates; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Handle once time class implemented |
| 109 | */ |
| 110 | function isTimeNotAvailable( $date ){ |
| 111 | return !pisol_dtt_time::isTimeAvailable( $date ); |
| 112 | } |
| 113 | |
| 114 | function nonWorkingDay($date){ |
| 115 | $week = date('w', strtotime($date)); |
| 116 | $allowed_days = $this->allowed_dates; |
| 117 | if(is_array($allowed_days) && in_array($week, $allowed_days)){ |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | function allowedDates($type){ |
| 124 | $var_name = 'pi_'.$type.'_days'; |
| 125 | $allowed_days = pisol_dtt_get_setting($var_name, array()); |
| 126 | if(empty( $allowed_days )){ |
| 127 | return array(0,1,2,3,4,5,6); |
| 128 | }else{ |
| 129 | return $allowed_days; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function isHoliday($date){ |
| 134 | $holidays = $this->holidays; |
| 135 | if(is_array($holidays) && in_array($date,$holidays)){ |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | function getHolidays($type){ |
| 142 | $var_name = 'pisol_dtt_'.$type.'_dd'; |
| 143 | $holidays = pisol_dtt_get_setting($var_name, array()); |
| 144 | return $holidays; |
| 145 | } |
| 146 | |
| 147 | static function formatedDate($date){ |
| 148 | $format = pisol_dtt_get_setting('date_format','F j, Y'); |
| 149 | $formated_date = strtotime($date) ? date($format, strtotime($date)) : $date; |
| 150 | return $formated_date; |
| 151 | } |
| 152 | |
| 153 | static function translatedDate( $date ){ |
| 154 | /** |
| 155 | * H:i:s is needed else it create different time stamp when time is missing |
| 156 | */ |
| 157 | $original_timezone = date_default_timezone_get(); |
| 158 | date_default_timezone_set( 'UTC' ); |
| 159 | $date = str_replace( '/', '-', $date ); |
| 160 | $datestamp = strtotime($date); |
| 161 | if($datestamp){ |
| 162 | if(apply_filters('pi_dtt_bypass_date_i18n',false)){ |
| 163 | $date = date(pisol_dtt_get_setting( 'date_format' ), $datestamp); |
| 164 | }else{ |
| 165 | $date = date_i18n(pisol_dtt_get_setting( 'date_format' ), $datestamp); |
| 166 | } |
| 167 | date_default_timezone_set( $original_timezone ); |
| 168 | return $date; |
| 169 | } |
| 170 | date_default_timezone_set( $original_timezone ); |
| 171 | return $date; |
| 172 | } |
| 173 | } |