css
1 year ago
js
1 year ago
class-css.php
1 year ago
class-date.php
1 year ago
class-delivery-type.php
1 year ago
class-display-field.php
1 year ago
class-js.php
1 year ago
class-main.php
1 year ago
class-order.php
1 year ago
class-pickup-location.php
1 year ago
class-shipping-method.php
1 year ago
class-time-range.php
1 year ago
class-time-slot.php
1 year ago
class-time.php
1 year ago
class-validate.php
1 year ago
class-woo-app.php
1 year ago
class-time.php
185 lines
| 1 | <?php |
| 2 | if ( ! defined( 'WPINC' ) ) { |
| 3 | die; |
| 4 | } |
| 5 | class pisol_dtt_time{ |
| 6 | |
| 7 | function __construct(){ |
| 8 | |
| 9 | add_action('wp_ajax_pisol_dtt_get_time', array($this,"getTime") ); |
| 10 | add_action('wp_ajax_nopriv_pisol_dtt_get_time', array($this,"getTime") ); |
| 11 | |
| 12 | } |
| 13 | |
| 14 | function getTime(){ |
| 15 | if(!isset($_POST['selected_date'])) return; |
| 16 | |
| 17 | $type = ''; |
| 18 | if(isset($_POST['pi_dtt_delivery_type'])){ |
| 19 | $type = sanitize_text_field($_POST['pi_dtt_delivery_type']); |
| 20 | } |
| 21 | /** |
| 22 | * have removed this check as we do validation of time slots as per date |
| 23 | * during checkout |
| 24 | */ |
| 25 | /* if(!pi_dtt_date::isDateValid($_POST['selected_date'])) return; */ |
| 26 | |
| 27 | if(self::useTimeSlot()){ |
| 28 | pisol_dtt_time_slot::getTimeSlotJson(sanitize_text_field($_POST['selected_date']), $type); |
| 29 | die; |
| 30 | }else{ |
| 31 | pisol_dtt_time_range::getTimeRangeJson(sanitize_text_field($_POST['selected_date']), $type); |
| 32 | die; |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | |
| 37 | static function getTimeArray($date, $type = ""){ |
| 38 | $obj = new self(); |
| 39 | if(!isset($date)) return; |
| 40 | |
| 41 | if(!pi_dtt_date::isDateValid($date, $type)) return; |
| 42 | |
| 43 | if(self::useTimeSlot($type)){ |
| 44 | return pisol_dtt_time_slot::getTimeSlotArray($date, $type); |
| 45 | }else{ |
| 46 | return pisol_dtt_time_range::getTimeRangeArray($date, $type); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | } |
| 51 | |
| 52 | static function isTimeValid($date, $input_time, $type = ""){ |
| 53 | |
| 54 | if(empty( $type )){ |
| 55 | $type = pi_dtt_delivery_type::getType(); |
| 56 | }else{ |
| 57 | $type = $type; |
| 58 | } |
| 59 | |
| 60 | $obj = new self(); |
| 61 | if(!isset($date)) return false; |
| 62 | |
| 63 | if(!pi_dtt_date::isDateValid($date, $type)) return false; |
| 64 | |
| 65 | if(self::useTimeSlot($type)){ |
| 66 | $time = pisol_dtt_time_slot::getTimeSlotArray($date, $type); |
| 67 | }else{ |
| 68 | $time = pisol_dtt_time_range::getTimeRangeArray($date, $type); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | if(empty($time)) return false; |
| 73 | |
| 74 | $input_time = str_replace(' ', '', $input_time); |
| 75 | |
| 76 | $time = str_replace(' ', '', array_column($time, 'id')); |
| 77 | |
| 78 | if(array_search($input_time, $time) !== false) { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | |
| 84 | |
| 85 | } |
| 86 | |
| 87 | static function isTimeAvailable( $date ){ |
| 88 | $time = array(); |
| 89 | |
| 90 | if(pi_dtt_display_fields::enableTimeField()){ |
| 91 | |
| 92 | if(self::useTimeSlot()){ |
| 93 | $time = pisol_dtt_time_slot::getTimeSlotArray($date); |
| 94 | }else{ |
| 95 | $time = pisol_dtt_time_range::getTimeRangeArray($date); |
| 96 | } |
| 97 | |
| 98 | if( empty($time) ) return false; |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static function dayOfTheWeek($date){ |
| 106 | return strtolower(date("l",strtotime($date))); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static function formatTimeForDisplay($times){ |
| 111 | $format = pisol_dtt_time::getDisplayFormat(); |
| 112 | return pisol_dtt_time::formatTimeForStorage($times, $format); |
| 113 | } |
| 114 | |
| 115 | static function formatTimeForStorage($times, $format = 'H:i'){ |
| 116 | if(strpos($times,'-')){ |
| 117 | $times_array = explode("-",$times); |
| 118 | }else{ |
| 119 | $times_array[] = $times; |
| 120 | } |
| 121 | |
| 122 | $formated_array = array(); |
| 123 | foreach($times_array as $time){ |
| 124 | $formated_array[] = strtotime(trim($time)) ? date($format, strtotime(trim($time))) : trim($time); |
| 125 | } |
| 126 | $return = ""; |
| 127 | if(count($formated_array) == 2){ |
| 128 | $return .= $formated_array[0].' - '.$formated_array[1]; |
| 129 | }else{ |
| 130 | $return .= $formated_array[0]; |
| 131 | } |
| 132 | return $return; |
| 133 | } |
| 134 | |
| 135 | static function getDisplayFormat(){ |
| 136 | $format = pisol_dtt_get_setting('pi_time_format','h:mm p'); |
| 137 | |
| 138 | switch($format){ |
| 139 | case 'h:mm p': |
| 140 | return "g:i A"; |
| 141 | break; |
| 142 | |
| 143 | case 'H:mm': |
| 144 | return "H:i"; |
| 145 | break; |
| 146 | |
| 147 | case 'H:mm p': |
| 148 | return "H:i A"; |
| 149 | break; |
| 150 | |
| 151 | default: |
| 152 | return "g:i A"; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static function useTimeSlot($type = ""){ |
| 158 | if(empty($type)){ |
| 159 | $type = pi_dtt_delivery_type::getType(); |
| 160 | } |
| 161 | $user_setting = pisol_dtt_get_setting('pi_use_time_slot','all_range'); |
| 162 | switch($user_setting){ |
| 163 | case 'all_slot': |
| 164 | return true; |
| 165 | break; |
| 166 | |
| 167 | case 'all_range': |
| 168 | return false; |
| 169 | break; |
| 170 | case 'pickup_slot_delivery_range': |
| 171 | if($type == 'pickup') return true; |
| 172 | |
| 173 | return false; |
| 174 | break; |
| 175 | case 'delivery_slot_pickup_range': |
| 176 | if($type == 'delivery') return true; |
| 177 | |
| 178 | return false; |
| 179 | break; |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | new pisol_dtt_time(); |