css
3 weeks ago
js
3 weeks ago
class-css.php
3 weeks ago
class-date.php
3 weeks ago
class-delivery-type.php
3 weeks ago
class-display-field.php
3 weeks ago
class-extra-message.php
3 weeks ago
class-js.php
3 weeks ago
class-main.php
3 weeks ago
class-myaccount.php
3 weeks ago
class-order.php
3 weeks ago
class-pickup-location.php
3 weeks ago
class-shipping-method.php
3 weeks ago
class-time-range.php
3 weeks ago
class-time-slot.php
3 weeks ago
class-time.php
3 weeks ago
class-validate.php
3 weeks ago
class-woo-app.php
3 weeks ago
class-time-slot.php
155 lines
| 1 | <?php |
| 2 | if ( ! defined( 'WPINC' ) ) { |
| 3 | die; |
| 4 | } |
| 5 | class pisol_dtt_time_slot{ |
| 6 | |
| 7 | public $selected_date; |
| 8 | public $delivery_type; |
| 9 | public $day; |
| 10 | public $time_slot; |
| 11 | |
| 12 | function __construct($selected_date){ |
| 13 | $this->selected_date = $selected_date; |
| 14 | } |
| 15 | |
| 16 | static function getTimeSlotArray( $date, $type = "" ){ |
| 17 | $obj = new self($date); |
| 18 | $time_slot = $obj->init($type); |
| 19 | return $time_slot; |
| 20 | } |
| 21 | |
| 22 | static function getTimeSlotJson($date, $type = "" ){ |
| 23 | $obj = new self($date); |
| 24 | $obj->getTime($type); |
| 25 | } |
| 26 | |
| 27 | function getTime($type = ""){ |
| 28 | $time_slot = $this->init($type); |
| 29 | echo json_encode($time_slot); |
| 30 | } |
| 31 | |
| 32 | function init($type = ""){ |
| 33 | |
| 34 | if(empty($this->selected_date)) return array(); |
| 35 | if(empty($type)){ |
| 36 | $this->delivery_type = pi_dtt_delivery_type::getType(); |
| 37 | }else{ |
| 38 | $this->delivery_type = $type; |
| 39 | } |
| 40 | |
| 41 | $this->day = strtolower(pisol_dtt_time::dayOfTheWeek( $this->selected_date )); |
| 42 | $this->time_slot = $this->getTimeSlot($this->day, $this->delivery_type); |
| 43 | return apply_filters('pisol_dtt_time_slot_filter',$this->time_slot, $this->selected_date, $this->delivery_type); |
| 44 | } |
| 45 | |
| 46 | function getTimeSlot($day, $delivery_type){ |
| 47 | |
| 48 | $forced_time_slots = apply_filters('pisol_forced_valid_time_slots',null, $day, $this->selected_date, $delivery_type); |
| 49 | if($forced_time_slots !== null && is_array($forced_time_slots)) return $forced_time_slots; |
| 50 | |
| 51 | $slots = $this->getGeneralTimeSlots( $delivery_type ); |
| 52 | |
| 53 | $slots = apply_filters('pisol_dtt_forced_time_slots', $slots, $day, $this->selected_date, $delivery_type); |
| 54 | |
| 55 | return $this->filterSlots($slots); |
| 56 | } |
| 57 | |
| 58 | function getGeneralTimeSlots( $delivery_type ){ |
| 59 | $slots = pisol_dtt_get_setting('pi_general_time_slot_'.$delivery_type, array()); |
| 60 | if(empty($slots)) return array(); |
| 61 | |
| 62 | return $slots; |
| 63 | } |
| 64 | |
| 65 | function filterSlots($slots){ |
| 66 | if(empty($slots)) return array(); |
| 67 | |
| 68 | $return1 = $this->removePastTime($slots); |
| 69 | |
| 70 | $return2 = $this->customFilter($return1); |
| 71 | |
| 72 | $return3 = $this->convertToString($return2); |
| 73 | |
| 74 | return $return3; |
| 75 | } |
| 76 | |
| 77 | static function slotToString($slot){ |
| 78 | $key = pisol_dtt_time::formatTimeForStorage($slot['from']).' - '.pisol_dtt_time::formatTimeForStorage($slot['to']); |
| 79 | return $key; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | function removePastTime($time_array){ |
| 84 | |
| 85 | |
| 86 | if(!is_array($time_array) || $time_array == ""){ |
| 87 | return ""; |
| 88 | } |
| 89 | |
| 90 | if(!$this->isSelectedDateToday()) return $time_array; |
| 91 | |
| 92 | $order_preparation_time = pisol_dtt_get_setting('pi_order_preparation_hours',60); |
| 93 | $order_preparation_time = $order_preparation_time == "" ? 0 : $order_preparation_time; |
| 94 | |
| 95 | $now = current_time('h:i A'); |
| 96 | |
| 97 | $minutes_to_add = "+".$order_preparation_time." minutes"; |
| 98 | |
| 99 | $time_limit = strtotime($minutes_to_add, strtotime($now)); |
| 100 | foreach($time_array as $key => $time){ |
| 101 | if($time['to'] != ""){ |
| 102 | if(strtotime($time['to']) < $time_limit){ |
| 103 | unset($time_array[$key]); |
| 104 | } |
| 105 | }else{ |
| 106 | if($time['from'] != ""){ |
| 107 | if(strtotime($time['from']) < $time_limit){ |
| 108 | unset($time_array[$key]); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | return $time_array; |
| 114 | } |
| 115 | |
| 116 | function customFilter($return){ |
| 117 | return apply_filters("pisol_dtt_custom_remove_time_slots", $return, $this->selected_date); |
| 118 | } |
| 119 | |
| 120 | function isSelectedDateToday(){ |
| 121 | $today = current_time('Y/m/d'); |
| 122 | if($this->selected_date == $today){ |
| 123 | return true; |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | function convertToString($slots){ |
| 129 | $return = array(); |
| 130 | if(!empty($slots)){ |
| 131 | foreach($slots as $slot){ |
| 132 | $key = pisol_dtt_time::formatTimeForStorage($slot['from']).' - '.pisol_dtt_time::formatTimeForStorage($slot['to']); |
| 133 | |
| 134 | $value = self::removeEmptyTimeFromDisplay(pisol_dtt_time::formatTimeForDisplay($slot['from']), pisol_dtt_time::formatTimeForDisplay($slot['to'])); |
| 135 | $return[] = array('id'=>$key, 'text'=>$value); |
| 136 | } |
| 137 | } |
| 138 | return $return; |
| 139 | } |
| 140 | |
| 141 | static function removeEmptyTimeFromDisplay($from, $to){ |
| 142 | if(!empty($from) && !empty($to)){ |
| 143 | return $from.' - '.$to; |
| 144 | } |
| 145 | |
| 146 | if(!empty($from) && empty($to)){ |
| 147 | return $from; |
| 148 | } |
| 149 | |
| 150 | if(empty($from) && !empty($to)){ |
| 151 | return $to; |
| 152 | } |
| 153 | return ""; |
| 154 | } |
| 155 | } |