PluginProbe
Easy Hotel – Powerful Hotel Booking / 2.0.2
Easy Hotel – Powerful Hotel Booking v2.0.2
2.0.8 2.0.7 2.0.6 2.0.5 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.9.9 1.9.8 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 All 110 releases
easy-hotel / admin / includes / classes / class.core.php

class.core.php in Easy Hotel – Powerful Hotel Booking 2.0.2, at admin/includes/classes/class.core.php

633 lines 24.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 use SureCart\Support\Currency;
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4 class ESHB_Core {
5
6 private static $_instance = null;
7
8 public static function instance() {
9
10 if ( is_null( self::$_instance ) ) {
11 self::$_instance = new self();
12 }
13 return self::$_instance;
14
15 }
16
17 public function __construct() {}
18
19 public function eshb_send_html_email($to, $subject, $message, $from_name = 'reactheme.com/easyhotel', $from_email = 'rubel.reacthemes@gmail.com') {
20
21 require_once( trailingslashit( ABSPATH ) .'wp-load.php' );
22
23 if (!function_exists('wp_mail')) {
24 //error_log('Error: wp_mail() function is missing!');
25 return false;
26 }
27
28 // Guard the global function declaration. Without this, calling
29 // this method twice in the same request (e.g. customer + admin
30 // emails) raises a fatal "Cannot redeclare function" error.
31 if ( ! function_exists( 'eshb_html_email_filter_calback' ) ) {
32 function eshb_html_email_filter_calback(){
33 return 'text/html';
34 }
35 }
36
37 // Set content type to HTML
38 add_filter('wp_mail_content_type', 'eshb_html_email_filter_calback');
39
40 // Set email headers
41 $headers = array();
42 $headers[] = "From: {$from_name} <{$from_email}>";
43 $headers[] = "Reply-To: {$from_email}";
44
45 // Send email
46 $sent = wp_mail($to, $subject, $message, $headers);
47
48 // Remove filter to avoid conflicts
49 remove_filter('wp_mail_content_type', 'eshb_html_email_filter_calback');
50
51 //return $sent; // Returns true if sent, false if failed
52 return $sent;
53 }
54
55 public function get_eshb_currency_symbol(){
56
57 $eshb_settings = get_option( 'eshb_settings' ,[]);
58 $booking_type = isset($eshb_settings['booking-type']) ? $eshb_settings['booking-type'] : '';
59
60 if( !empty($eshb_settings['currency_symbol']) ){
61 $currency_symbol = $eshb_settings['currency_symbol'];
62 }else if($booking_type == 'woocommerce' && class_exists('woocommerce')){
63 $currency_symbol = get_woocommerce_currency_symbol();
64 }else if($booking_type == 'surecart' && class_exists('SureCart')){
65 $currency = new Currency();
66 $currency_symbol = $currency->getCurrencySymbol();
67 }else {
68 $currency_symbol = '$';
69 }
70
71 $currency_symbol = apply_filters('eshb_currency_symbol', $currency_symbol, $booking_type);
72
73 return $currency_symbol;
74 }
75
76 public function get_eshb_currency_position(){
77
78 $currency_position = 'left';
79
80 $eshb_settings = get_option( 'eshb_settings' ,[]);
81 if(isset($eshb_settings['currency_position']) && !empty($eshb_settings['currency_position'])){
82 $currency_position = $eshb_settings['currency_position'];
83 }
84
85 return $currency_position;
86 }
87
88 public function get_eshb_default_start_end_date(){
89 $today_date = gmdate('Y-m-d'); // Get today's date
90
91 // Create a DateTime object from today's date
92 $date = new DateTime($today_date);
93
94 // Add one day
95 $date->modify('+1 day');
96
97 // Get the new date in 'Y-m-d' format
98 $tomorrow_date = $date->format('Y-m-d');
99
100 return array(
101 'start_date' => $today_date,
102 'end_date' => $tomorrow_date
103 );
104 }
105
106
107 public function has_upcoming_or_current_session_price($accomodation_id, $start_date, $end_date, $night = 1, $adult = 1, $children = '') {
108 $metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true);
109 $regular_price = !empty($metaboxes['regular_price']) ? $metaboxes['regular_price'] : 0;
110
111 // Get all sessions
112 $qargs = array(
113 'post_type' => 'eshb_session',
114 'post_status' => 'publish',
115 'posts_per_page' => -1,
116 );
117 $query = new WP_Query($qargs);
118
119 $sessions = [];
120
121 if ($query->have_posts()) {
122 while ($query->have_posts()) {
123 $query->the_post();
124 $post_id = get_the_ID();
125 $metaboxes = maybe_unserialize(get_post_meta($post_id, 'eshb_session_metaboxes', true));
126
127 if (empty($metaboxes['start_date']) || empty($metaboxes['end_date'])) continue;
128
129 $sessions[] = [
130 'id' => $post_id,
131 'start_date' => $metaboxes['start_date'],
132 'end_date' => $metaboxes['end_date'],
133 'price' => $metaboxes['session_price'] ?? $regular_price,
134 'accomodation_ids' => $metaboxes['accomodation_ids'] ?? [],
135 'longstay_pricing' => $metaboxes['longstay_pricing'] ?? [],
136 'variable_pricing' => $metaboxes['variable_pricing'] ?? [],
137 'days' => $metaboxes['days'] ?? [],
138 ];
139 }
140 wp_reset_postdata();
141 }
142
143 // Create date range (exclude checkout date if multi-day)
144 $period = new DatePeriod(
145 new DateTime($start_date),
146 new DateInterval('P1D'),
147 (new DateTime($start_date) == new DateTime($end_date))
148 ? (new DateTime($end_date))->modify('+1 day')
149 : new DateTime($end_date)
150 );
151
152 $total_nights = iterator_count($period);
153
154 // Initialize flags
155 $longstay_applied = false;
156 $variable_applied = false;
157 $session_price_applied = false;
158
159 foreach ($period as $day) {
160 $current_date = $day->format('Y-m-d');
161 $current_day_name = strtolower($day->format('l'));
162 foreach ($sessions as $session) {
163
164 if(empty($session['accomodation_ids'])){
165 continue;
166 }
167
168 if (!empty($session['accomodation_ids']) && !in_array($accomodation_id, $session['accomodation_ids'])) {
169 continue;
170 }
171 $session_days = !empty($session['days']) ? $session['days'] : ['saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
172 if(is_array($session['days']) && in_array('all', $session['days'])){
173 $session_days = ['saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
174 }
175 if ($current_date >= $session['start_date'] && $current_date <= $session['end_date'] && in_array($current_day_name, $session_days)) {
176
177 // 1. Longstay Pricing
178 if (!empty($session['longstay_pricing']) && $total_nights >= 1) {
179 // reverse the longstay_pricing array
180 $session['longstay_pricing'] = array_reverse($session['longstay_pricing']);
181 foreach ($session['longstay_pricing'] as $ls) {
182 if (!empty($ls['night']) && $total_nights >= $ls['night']) {
183 $longstay_applied = true;
184 break 2; // early return since found
185 }
186 }
187 }
188
189 // 2. Variable Pricing
190 if (!empty($session['variable_pricing'])) {
191 foreach ($session['variable_pricing'] as $vp) {
192 $vp_adult = $vp['adult_quantity'] ?? 0;
193 $vp_children = $vp['children_quantity'] ?? 0;
194 if ($vp_adult == $adult && $vp_children == $children) {
195 $variable_applied = true;
196 break 2;
197 }
198 }
199 }
200
201 // 3. Session Price (basic price)
202 if (!empty($session['price']) && $session['price'] != $regular_price) {
203 $session_price_applied = true;
204 break 2;
205 }
206 }
207 }
208 }
209
210 if ($longstay_applied || $variable_applied || $session_price_applied) {
211 return true;
212 }
213
214 return false;
215 }
216
217 public function get_eshb_price_by_session($accomodation_id, $start_date, $end_date, $night = '', $adult = '', $children = '') {
218
219 $metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true);
220 $regular_price = !empty($metaboxes['regular_price']) ? $metaboxes['regular_price'] : 0;
221
222
223 // Get all sessions
224 $qargs = array(
225 'post_type' => 'eshb_session',
226 'post_status' => 'publish',
227 'posts_per_page' => -1,
228 );
229 $query = new WP_Query($qargs);
230
231 $sessions = [];
232
233 if ($query->have_posts()) {
234 while ($query->have_posts()) {
235 $query->the_post();
236 $post_id = get_the_ID();
237 $metaboxes = maybe_unserialize(get_post_meta($post_id, 'eshb_session_metaboxes', true));
238
239 if (empty($metaboxes['start_date']) || empty($metaboxes['end_date'])) continue;
240
241 $sessions[] = [
242 'id' => $post_id,
243 'start_date' => $metaboxes['start_date'],
244 'end_date' => $metaboxes['end_date'],
245 'price' => $metaboxes['session_price'] ?? $regular_price,
246 'accomodation_ids' => $metaboxes['accomodation_ids'] ?? [],
247 'longstay_pricing' => $metaboxes['longstay_pricing'] ?? [],
248 'variable_pricing' => $metaboxes['variable_pricing'] ?? [],
249 'days' => $metaboxes['days'] ?? [],
250 ];
251 }
252 wp_reset_postdata();
253 }
254
255 // Create date range (exclude checkout date if multi-day)
256 $period = new DatePeriod(
257 new DateTime($start_date),
258 new DateInterval('P1D'),
259 (new DateTime($start_date) == new DateTime($end_date))
260 ? (new DateTime($end_date))->modify('+1 day') // same-day booking
261 : new DateTime($end_date) // exclude checkout
262 );
263
264 $total_nights = iterator_count($period);
265 $per_day_prices = [];
266
267
268 foreach ($period as $day) {
269 $current_date = $day->format('Y-m-d');
270 $current_day_name = strtolower($day->format('l'));
271 $matched_price = $regular_price;
272
273 foreach ($sessions as $session) {
274
275
276 if(empty($session['accomodation_ids'])){
277 continue;
278 }
279
280 if (!empty($session['accomodation_ids']) && !in_array($accomodation_id, $session['accomodation_ids'])) {
281 continue;
282 }
283
284 $session_days = !empty($session['days']) ? $session['days'] : ['saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
285 if(in_array('all', $session_days)){
286 $session_days = ['saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
287 }
288
289 if ($current_date >= $session['start_date'] && $current_date <= $session['end_date'] && in_array($current_day_name, $session_days)) {
290
291 // 1. Longstay Pricing (highest priority)
292 $longstay_applied = false;
293
294
295 if (!empty($session['longstay_pricing']) && $total_nights >= 1) {
296 $session['longstay_pricing'] = array_reverse($session['longstay_pricing']);
297 foreach ($session['longstay_pricing'] as $ls) {
298 if (!empty($ls['night']) && $total_nights >= $ls['night']) {
299 $matched_price = floatval($ls['price']);
300 $longstay_applied = true;
301 break;
302 }
303 }
304
305 }
306
307 if ($longstay_applied) break;
308
309 // 2. Variable Pricing
310 $variable_applied = false;
311 if (!empty($session['variable_pricing'])) {
312 foreach ($session['variable_pricing'] as $vp) {
313 $vp_adult = $vp['adult_quantity'] ?? 0;
314 $vp_children = $vp['children_quantity'] ?? 0;
315 if ($vp_adult == $adult && $vp_children == $children) {
316 $matched_price = floatval($vp['price']);
317 $variable_applied = true;
318 break;
319 }
320 }
321 }
322
323 if ($variable_applied) break;
324
325 // 3. Session price
326 $matched_price = floatval($session['price']);
327 break;
328 }
329 }
330
331
332
333
334 $per_day_prices[] = $matched_price;
335 }
336
337 $total_price = array_sum($per_day_prices);
338 $total_price = apply_filters('eshb_session_price', $total_price);
339 return $total_price;
340 }
341
342 public function get_eshb_price($start_date = null, $end_date = null, $accomodation_id = null, $session_price = true, $night = '', $adult = '', $children = '', $sale = true, $single_day = true) {
343
344 if($start_date == null || empty($start_date) || $end_date == null || empty($end_date)){
345 $dates = $this->get_eshb_default_start_end_date();
346 $start_date = $dates['start_date'];
347 $end_date = $dates['end_date'];
348 }
349
350
351 if( $accomodation_id == null || empty($accomodation_id) ){
352 global $post;
353 $accomodation_id = $post->ID;
354 }
355 $price = '';
356 $metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true);
357
358 $regular_price = !empty($metaboxes['regular_price']) ? $metaboxes['regular_price'] : 0;
359 $sale_price = !empty($metaboxes['sale_price']) ? $metaboxes['sale_price'] : 0;
360
361 if($single_day){
362 $single_day_price = apply_filters( 'eshb_single_day_price', 0, $accomodation_id);
363 $eshb_single_day_sale_price = apply_filters( 'eshb_single_day_sale_price', 0, $accomodation_id);
364 $regular_price = !empty($single_day_price) ? $single_day_price : $regular_price;
365 $sale_price = !empty($eshb_single_day_sale_price) ? $eshb_single_day_sale_price : $sale_price;
366 }
367
368 $price = $regular_price;
369
370 if($sale == true){
371 $price = !empty($sale_price) ? $sale_price : $regular_price;
372 }
373
374 if($session_price == true){
375 $session_price = $this->get_eshb_price_by_session($accomodation_id, $start_date, $end_date, $night, $adult, $children);
376 $price = !empty($session_price) ? $session_price : $price;
377 }
378
379 $price = apply_filters('eshb_price', $price);
380
381 return $price;
382 }
383
384 public function eshb_price ($price, $currency_symbol = NULL){
385
386 if(!$currency_symbol || $currency_symbol = NULL || empty($currency_symbol)) {
387 // Get the currency symbol
388 $currency_symbol = $this->get_eshb_currency_symbol();
389 }
390
391 // Prepare the price HTML, with or without currency symbol
392 $currency_position = $this->get_eshb_currency_position();
393
394 $eshb_settings = get_option('eshb_settings');
395 $booking_type = isset($eshb_settings['booking-type']) ? $eshb_settings['booking-type'] : 'woocommerce';
396
397
398 if($booking_type == 'woocommerce' && class_exists('WooCommerce')){
399 $price = wc_price($price, []);
400 return $price;
401 }else{
402 if(!empty($price)){
403 $price = number_format($price,2);
404 }
405 $price_html = $currency_symbol . $price;
406 if($currency_position == 'right') {
407 $price_html = $price . $currency_symbol;
408 }
409 }
410 return $price_html;
411 }
412
413 public function get_eshb_price_html($start_date = null, $end_date = null, $accomodation_id = null, $show_currency = true, $include_single_day_price = true, $include_day_wise_price = true, $include_session_price = true, $format = 'sale') {
414
415 if($start_date == null || empty($start_date) || $end_date == null || empty($end_date)){
416 $dates = $this->get_eshb_default_start_end_date();
417 $start_date = $dates['start_date'];
418 $end_date = $dates['end_date'];
419 }
420
421 $eshb_settings = get_option('eshb_settings');
422 $booking_type = isset($eshb_settings['booking-type']) ? $eshb_settings['booking-type'] : 'woocommerce';
423
424 $is_wc_price = false;
425 if($booking_type == 'woocommerce' && class_exists('WooCommerce')){
426 $is_wc_price = true;
427 }
428
429 // Get the currency symbol
430 $currency_symbol = $this->get_eshb_currency_symbol();
431
432 // Get price values from different sources
433 $regular_price = $this->get_eshb_price($start_date, $end_date, $accomodation_id, false, '', '', '', false);
434 $sale_price = $this->get_eshb_price($start_date, $end_date, $accomodation_id, false, '', '', '', true);
435
436 $prices = [
437 'regular_price' => $regular_price,
438 'sale_price' => $sale_price
439 ];
440
441 $day_wise_price = $this->get_eshb_day_wise_price($start_date, $end_date, $accomodation_id, false, 1, 1, 0);
442
443 $has_session_price = $this->has_upcoming_or_current_session_price($accomodation_id, $start_date, $end_date, 1, 1, 0);
444
445 if ($has_session_price && $include_session_price) {
446 $include_session_price = true;
447 } else {
448 $include_session_price = false;
449 }
450
451
452 $session_price = $this->get_eshb_price_by_session($accomodation_id, $start_date, $end_date);
453 $single_day_price = apply_filters( 'eshb_single_day_price', 0, $accomodation_id );
454
455 // Determine the price to use
456 if ($include_session_price === true && !empty($session_price) && ($session_price !== $regular_price && $session_price !== $sale_price)) {
457 $price = $session_price;
458 } elseif ($include_day_wise_price === true && !empty($day_wise_price) && ($day_wise_price !== $regular_price && $day_wise_price !== $sale_price)) {
459 $price = $day_wise_price;
460 } elseif ($include_single_day_price === true && !empty($single_day_price) && ($single_day_price !== $regular_price && $single_day_price !== $sale_price)) {
461 $price = $single_day_price;
462 } else {
463 if(!empty($sale_price ) && $sale_price !== $regular_price){
464 if($format == 'sale'){
465 $price = $prices;
466 }else{
467 $price = $sale_price;
468 }
469 }else{
470 $price = $regular_price;
471 }
472 }
473
474
475 // Return empty string if price is empty
476 if (empty($price)) {
477 return '';
478 }
479
480
481 // Prepare the price HTML, with or without currency symbol
482 $currency_position = $this->get_eshb_currency_position();
483 $price_html = '';
484 $price = apply_filters('eshb_price_html_price', $price);
485
486 if ( is_array( $price ) ) {
487 // Regular & Sale Price
488 $regular_price = $price['regular_price'];
489 $sale_price = $price['sale_price'];
490
491 if ( $show_currency && !$is_wc_price ) {
492 if ( $currency_position == 'right' ) {
493 $regular_price = $regular_price . $currency_symbol;
494 $sale_price = $sale_price . $currency_symbol;
495 } else {
496 $regular_price = $currency_symbol . $regular_price;
497 $sale_price = $currency_symbol . $sale_price;
498 }
499 }
500
501 if($is_wc_price){
502 $regular_price = wc_price($regular_price, []);
503 $sale_price = wc_price($sale_price, []);
504 }
505
506 $price_html = '<span class="eshb-price">';
507 $price_html .= '<del aria-hidden="true"><span class="eshb-price-amount amount"><bdi>' . $regular_price . '</bdi></span></del>';
508 $price_html .= '<span class="screen-reader-text">Original price was: ' . $regular_price . '.</span>';
509 $price_html .= '<ins aria-hidden="true"><span class="eshb-price-amount amount"><bdi>' . $sale_price . '</bdi></span></ins>';
510 $price_html .= '<span class="screen-reader-text">Current price is: ' . $sale_price . '.</span>';
511 $price_html .= '</span>';
512
513 } else {
514 // Single Price (No Sale)
515 $single_price = $price;
516
517 if ( $show_currency && !$is_wc_price) {
518 if ( $currency_position == 'right' ) {
519 $single_price = $single_price . $currency_symbol;
520 } else {
521 $single_price = $currency_symbol . $single_price;
522 }
523 }
524
525 if($is_wc_price){
526 $single_price = wc_price($single_price, []);
527 }
528 $price_html = '<span class="eshb-price">';
529 $price_html .= '<span class="eshb-price-amount amount"><bdi>' . $single_price . '</bdi></span>';
530 $price_html .= '</span>';
531 }
532
533 $price_html = apply_filters('eshb_price_html', $price_html, $price);
534 return $price_html;
535 }
536
537 public function get_eshb_day_names_from_date_ranges($start_date, $end_date){
538 $period = new DatePeriod(
539 new DateTime($start_date),
540 new DateInterval('P1D'),
541 new DateTime($end_date) // Stop before the end date
542 );
543
544 $dayNames = [];
545 foreach ($period as $date) {
546 $dayNames[] = strtolower($date->format('l')); // Convert to lowercase
547 }
548
549 return $dayNames;
550 }
551
552 public function get_eshb_day_wise_price($start_date, $end_date, $accomodation_id, $fallback_regular_price = true, $night = '', $adult = '', $children = ''){
553
554 if($start_date == null || empty($start_date) || $end_date == null || empty($end_date) || $accomodation_id == null || empty($accomodation_id)) return 0;
555
556
557 $regular_price = $fallback_regular_price == true ? $this->get_eshb_price($start_date, $end_date, $accomodation_id, false, $night, $adult, $children,) : 0;
558
559 if ($start_date === $end_date) {
560 // Add 1 day to end date if they are the same
561 $end_date = gmdate('Y-m-d', strtotime($end_date . ' +1 day'));
562 }
563
564 $period = new DatePeriod(
565 new DateTime($start_date),
566 new DateInterval('P1D'),
567 new DateTime($end_date) // Stop before the end date
568 );
569
570 $dayNames = [];
571 $days_count = 0;
572 foreach ($period as $date) {
573 $dayNames[] = strtolower($date->format('l')); // Convert to lowercase
574 $days_count++;
575 }
576
577 // If only one day is selected, return the price for that day
578 // if(is_archive() || isset($_GET['eshb_global_nonce_action']) || isset($_GET['eshb_search_nonce'])){
579 // $todayName = strtolower(gmdate('l'));
580 // //remove all day names from the array but today's name
581 // $dayNames = array_filter($dayNames, function($day) use ($todayName) {
582 // return $day === $todayName;
583 // });
584 // }
585
586 $metaboxes = get_post_meta($accomodation_id, 'eshb_accomodation_metaboxes', true);
587 $day_wise_price = isset($metaboxes['day_wise_price']) && !empty($metaboxes['day_wise_price']) ? $metaboxes['day_wise_price'] : '';
588
589 $price_by_day = 0;
590
591 if(!empty($day_wise_price) && isset($day_wise_price[0]) && !empty($day_wise_price[0])){
592 $day_wise_price = $day_wise_price[0];
593 foreach ($dayNames as $key => $day) {
594 if( !empty($day_wise_price[$day]) ){
595 $price_by_day += $day_wise_price[$day];
596 }else{
597 $price_by_day += $regular_price;
598 }
599
600 }
601 }else{
602
603 $price_by_day = $regular_price * $days_count;
604
605 }
606 $price_by_day = apply_filters('eshb_day_wise_price', $price_by_day);
607 return $price_by_day;
608 }
609 }
610 ESHB_Core::instance();
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633