date-time-location
10 months ago
class-date-time-location-block-integration.php
10 months ago
class-date-time-location-block.php
10 months ago
class-date-time-location-storage.php
10 months ago
class-date-time-location-block.php
335 lines
| 1 | <?php |
| 2 | namespace PISOL\DTT\BLOCK; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema; |
| 5 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema; |
| 6 | |
| 7 | class DateTimeLocationBlock{ |
| 8 | |
| 9 | private $extend; |
| 10 | |
| 11 | protected static $instance = null; |
| 12 | |
| 13 | static $IDENTIFIER = 'pisol_dtt'; |
| 14 | |
| 15 | public static function get_instance( ) { |
| 16 | if ( is_null( self::$instance ) ) { |
| 17 | self::$instance = new self(); |
| 18 | } |
| 19 | return self::$instance; |
| 20 | } |
| 21 | |
| 22 | function __construct(){ |
| 23 | add_action('init', [$this, 'register_date_time_location_block']); |
| 24 | |
| 25 | add_action('woocommerce_blocks_loaded', [$this, 'register_block']); |
| 26 | |
| 27 | add_action('woocommerce_blocks_loaded', [$this, 'add_data']); |
| 28 | |
| 29 | add_action( 'woocommerce_blocks_loaded', [$this, 'type_change_callback']); |
| 30 | add_action( 'woocommerce_blocks_loaded', [$this, 'date_selected_callback']); |
| 31 | add_action( 'woocommerce_blocks_loaded', [$this, 'time_selected_callback']); |
| 32 | add_action( 'woocommerce_blocks_loaded', [$this, 'pickup_location_selected_callback']); |
| 33 | |
| 34 | add_action('woocommerce_blocks_loaded', [$this, 'extension_data_declaration']); |
| 35 | |
| 36 | add_filter('pisol_dtt_settings_filter', [$this, 'enableDateOnLoadingOfPage'],1); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * This is needed else the block will not work in the Block based Wordpress themes |
| 41 | */ |
| 42 | function register_block(){ |
| 43 | require_once( 'class-date-time-location-block-integration.php' ); |
| 44 | add_action( |
| 45 | 'woocommerce_blocks_checkout_block_registration', |
| 46 | function( $integration_registry ) { |
| 47 | $integration_registry->register( new \Date_Time_Location_Block_Integration() ); |
| 48 | } |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | function enableDateOnLoadingOfPage($settings){ |
| 53 | /** |
| 54 | * this will make the date to be loaded when plugin is used with different date and time plugin |
| 55 | */ |
| 56 | $checkout_page_id = wc_get_page_id('checkout'); |
| 57 | if(has_block('woocommerce/checkout', $checkout_page_id)){ |
| 58 | $settings['allowDatesOnLoad'] = true; |
| 59 | } |
| 60 | |
| 61 | return $settings; |
| 62 | } |
| 63 | |
| 64 | function register_date_time_location_block(){ |
| 65 | |
| 66 | register_block_type(__DIR__.'/date-time-location'); |
| 67 | } |
| 68 | |
| 69 | function add_data(){ |
| 70 | woocommerce_store_api_register_endpoint_data( |
| 71 | array( |
| 72 | 'endpoint' => CartSchema::IDENTIFIER, |
| 73 | 'namespace' => self::$IDENTIFIER, |
| 74 | 'data_callback' => [__CLASS__, 'data'], |
| 75 | 'schema_type' => ARRAY_A, |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | static function data(){ |
| 81 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 82 | |
| 83 | $data = \pi_dtt_js::dateOptions(); |
| 84 | |
| 85 | $data['available_types'] = self::get_available_types(); |
| 86 | |
| 87 | $data['selected_type'] = self::get_selected_type(); |
| 88 | |
| 89 | $data['disabled_types'] = []; |
| 90 | |
| 91 | $data['selected_date'] = self::get_selected_date($data['allowedDates']); |
| 92 | |
| 93 | if(empty($data['selected_date'])){ |
| 94 | $data['allSlotsBooked'] = __('First select a date', 'pisol-dtt'); |
| 95 | } |
| 96 | |
| 97 | $data['available_time'] = self::get_time( $data['selected_date'], $data['selected_type']); |
| 98 | |
| 99 | $data['selected_time'] = self::get_selected_time($data['available_time']); |
| 100 | |
| 101 | $data['pickup_locations'] = self::get_pickup_locations(); |
| 102 | |
| 103 | $data['active_locations'] = count($data['pickup_locations']) >= 1 ? true : false; |
| 104 | |
| 105 | $data['show_pickup_location_as'] = 'radio'; |
| 106 | |
| 107 | $data['pickup_locations_for_radio'] = self::get_location_for_radio($data['pickup_locations']); |
| 108 | |
| 109 | $data['selected_location'] = self::get_selected_location($data['pickup_locations'], $data['selected_type']); |
| 110 | |
| 111 | $data['date_field_enabled'] = \pi_dtt_display_fields::showDateAndTime($data['selected_type']); |
| 112 | $data['time_field_enabled'] = \pi_dtt_display_fields::showDateAndTime($data['selected_type']) && \pi_dtt_display_fields::enableTimeField($data['selected_type']); |
| 113 | |
| 114 | $data['section_title'] = __('Order delivery details', 'pisol-dtt'); |
| 115 | |
| 116 | $data['pickup_location_label'] = __("Select a pickup location",'pisol-dtt'); |
| 117 | |
| 118 | |
| 119 | return apply_filters('pisol_dtt_block_content', $data); |
| 120 | } |
| 121 | |
| 122 | static function get_available_types(){ |
| 123 | |
| 124 | $aloud_types = pisol_dtt_get_setting('pi_type', "Both"); |
| 125 | |
| 126 | $types = array( |
| 127 | 'delivery' => pisol_dtt_get_setting('pi_delivery_label',__('Delivery','pisol-dtt')), |
| 128 | 'pickup' => pisol_dtt_get_setting('pi_pickup_label',__('Pickup','pisol-dtt')) |
| 129 | ); |
| 130 | |
| 131 | if($aloud_types == 'Delivery'){ |
| 132 | unset($types['pickup']); |
| 133 | }elseif($aloud_types == 'Pickup'){ |
| 134 | unset($types['delivery']); |
| 135 | } |
| 136 | |
| 137 | return $types; |
| 138 | } |
| 139 | |
| 140 | static function get_selected_type(){ |
| 141 | $delivery_type = \pi_dtt_delivery_type::getType(); |
| 142 | return $delivery_type; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | function type_change_callback(){ |
| 147 | woocommerce_store_api_register_update_callback( |
| 148 | [ |
| 149 | 'namespace' => self::$IDENTIFIER.'_type_change', |
| 150 | 'callback' => [$this, 'type_change'] |
| 151 | ] |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | function date_selected_callback(){ |
| 156 | woocommerce_store_api_register_update_callback( |
| 157 | [ |
| 158 | 'namespace' => self::$IDENTIFIER.'_date_selected', |
| 159 | 'callback' => [$this, 'date_selected'] |
| 160 | ] |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | function time_selected_callback(){ |
| 165 | woocommerce_store_api_register_update_callback( |
| 166 | [ |
| 167 | 'namespace' => self::$IDENTIFIER.'_time_selected', |
| 168 | 'callback' => [$this, 'time_selected'] |
| 169 | ] |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | function pickup_location_selected_callback(){ |
| 174 | woocommerce_store_api_register_update_callback( |
| 175 | [ |
| 176 | 'namespace' => self::$IDENTIFIER.'_pickup_location_selected', |
| 177 | 'callback' => [$this, 'pickup_location_selected'] |
| 178 | ] |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | function type_change($data){ |
| 183 | $type = sanitize_text_field($data['type']); |
| 184 | $obj = new \pi_dtt_delivery_type(); |
| 185 | $set_type = $obj->setDeliveryType($type); |
| 186 | } |
| 187 | |
| 188 | function date_selected($data){ |
| 189 | $date = sanitize_text_field($data['date']); |
| 190 | WC()->session->set('pi_selected_date', $date); |
| 191 | } |
| 192 | |
| 193 | function time_selected($data){ |
| 194 | $time = sanitize_text_field($data['time']); |
| 195 | WC()->session->set('pi_selected_time', $time); |
| 196 | } |
| 197 | |
| 198 | function pickup_location_selected($data){ |
| 199 | $pickup_location = sanitize_text_field($data['pickup_location_id']); |
| 200 | WC()->session->set('pickup_location', $pickup_location); |
| 201 | } |
| 202 | |
| 203 | static function get_selected_date($available_dates){ |
| 204 | $selected_date = WC()->session->get('pi_selected_date'); |
| 205 | |
| 206 | if(empty($selected_date)){ |
| 207 | return ''; |
| 208 | } |
| 209 | |
| 210 | if(empty($available_dates) || !is_array($available_dates)){ |
| 211 | return ''; |
| 212 | } |
| 213 | |
| 214 | return in_array($selected_date, $available_dates) ? $selected_date : ''; |
| 215 | } |
| 216 | |
| 217 | static function get_selected_location($available_locations, $type){ |
| 218 | $selected_location = WC()->session->get('pickup_location'); |
| 219 | |
| 220 | if(empty($available_locations) || !is_array($available_locations)){ |
| 221 | return ''; |
| 222 | } |
| 223 | |
| 224 | if(empty($type) || $type != 'pickup'){ |
| 225 | return ''; |
| 226 | } |
| 227 | |
| 228 | if(count($available_locations) === 1){ |
| 229 | return array_keys($available_locations)[0]; |
| 230 | } |
| 231 | |
| 232 | $array_keys = array_keys($available_locations); |
| 233 | |
| 234 | return in_array($selected_location, $array_keys) ? $selected_location : ''; |
| 235 | } |
| 236 | |
| 237 | static function get_selected_time($available_time){ |
| 238 | $selected_time = WC()->session->get('pi_selected_time'); |
| 239 | |
| 240 | if(empty($selected_time)){ |
| 241 | return ''; |
| 242 | } |
| 243 | |
| 244 | if(empty($available_time) || !is_array($available_time)){ |
| 245 | return ''; |
| 246 | } |
| 247 | |
| 248 | $time_ids = array_column($available_time, 'id'); |
| 249 | |
| 250 | return in_array($selected_time, $time_ids) ? $selected_time : ''; |
| 251 | } |
| 252 | |
| 253 | static function get_time($date, $type){ |
| 254 | |
| 255 | if(empty($date)){ |
| 256 | return []; |
| 257 | } |
| 258 | |
| 259 | ob_start(); |
| 260 | if(\pisol_dtt_time::useTimeSlot()){ |
| 261 | \pisol_dtt_time_slot::getTimeSlotJson($date, $type); |
| 262 | }else{ |
| 263 | \pisol_dtt_time_range::getTimeRangeJson($date, $type); |
| 264 | } |
| 265 | |
| 266 | $content = ob_get_clean(); |
| 267 | return json_decode($content, true); |
| 268 | } |
| 269 | |
| 270 | static function get_pickup_locations(){ |
| 271 | $location1 = get_option('pi_pickup_address_1', ""); |
| 272 | $location2 = get_option('pi_pickup_address_2', ""); |
| 273 | $locations = []; |
| 274 | if(!empty($location1)){ |
| 275 | $locations['pi_pickup_address_1'] = strip_tags( $location1 ); |
| 276 | } |
| 277 | |
| 278 | if(!empty($location2)){ |
| 279 | $locations['pi_pickup_address_2'] = strip_tags( $location2 ); |
| 280 | } |
| 281 | |
| 282 | return $locations; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | static function get_location_for_radio(){ |
| 287 | $locations = self::get_pickup_locations(); |
| 288 | $data = []; |
| 289 | foreach($locations as $location => $title){ |
| 290 | $content = ''; |
| 291 | $image_id = ''; |
| 292 | $map = ''; |
| 293 | $image = ''; |
| 294 | |
| 295 | $data[] = ['value' => $location, 'label' => $title, 'content' => $content, 'image' => $image, 'map' => $map]; |
| 296 | } |
| 297 | return $data; |
| 298 | } |
| 299 | |
| 300 | function extension_data_declaration(){ |
| 301 | woocommerce_store_api_register_endpoint_data( |
| 302 | array( |
| 303 | 'endpoint' => CheckoutSchema::IDENTIFIER, |
| 304 | 'namespace' => 'pisol-dtt/date-time-location', |
| 305 | 'schema_type' => ARRAY_A, |
| 306 | 'schema_callback' => [__CLASS__, 'data_structure'] |
| 307 | ) |
| 308 | ); |
| 309 | } |
| 310 | |
| 311 | static function data_structure(){ |
| 312 | return array( |
| 313 | 'pi_delivery_type' => array( |
| 314 | 'type' => 'string', |
| 315 | 'description' => __('Type of delivery selected by the customer.', 'pisol-dtt'), |
| 316 | 'enum' => array('pickup', 'delivery'), // Example of predefined values |
| 317 | ), |
| 318 | 'pi_system_delivery_date' => array( |
| 319 | 'type' => 'string', |
| 320 | 'description' => __('Date of delivery selected by the customer.', 'pisol-dtt'), |
| 321 | ), |
| 322 | 'pi_delivery_time' => array( |
| 323 | 'type' => 'string', |
| 324 | 'description' => __('Delivery timing selected by customer.', 'pisol-dtt'), |
| 325 | ), |
| 326 | 'pickup_location_id' => array( |
| 327 | 'type' => ['string','null'], |
| 328 | 'description' => __('Pickup location id', 'pisol-dtt'), |
| 329 | ), |
| 330 | ); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | DateTimeLocationBlock::get_instance(); |
| 335 |