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