PluginProbe
WP Directory Kit / 1.5.9
WP Directory Kit v1.5.9
1.5.9 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 All 37 releases
wpdirectorykit / elementor-extensions / class-ajax-handler.php

class-ajax-handler.php in WP Directory Kit 1.5.9, at elementor-extensions/class-ajax-handler.php

852 lines 53.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Wdk\Elementor\Extensions;
3
4 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5
6 class AjaxHandler {
7 /**
8 * data array
9 *
10 * @var array
11 */
12 protected $data = array();
13 public $_translate_strings = array();
14 protected $WMVC = NULL;
15
16
17 public function helper_transalte_strings($data = array(), $args = null) {
18
19 $this->_translate_strings = array(
20 __('Guests number', 'wpdirectorykit'),
21 __('Listing link', 'wpdirectorykit'),
22 __('Name', 'wpdirectorykit'),
23 __('Date from', 'wpdirectorykit'),
24 __('Date to', 'wpdirectorykit'),
25 );
26 }
27
28 public function __construct($data = array(), $args = null) {
29 add_action( 'init', array( $this, 'helper_transalte_strings' ) );
30 add_action( 'eli/ajax-handler/after', array( $this, 'after' ) );
31 add_filter( 'eli/ajax-handler/filter_from_data', array( $this, 'filter_from_data' ) );
32 }
33
34
35 public function filter_output ($filter_output = array()) {
36 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-primary" role="alert">The form was sent successfully.</div>';
37 return $filter_output;
38 }
39
40 public function filter_from_data ($form_data = array()) {
41 if (isset($_POST['date_from']) && isset($_POST['date_to'])) {
42 /* disable double sent to admin mail */
43 $form_data['settings']['disable_mail_send'] = 1;
44 }
45 if (isset($_POST['listing_id']) && !empty($_POST['listing_id'])) {
46 /* disable double sent to admin mail */
47 $form_data['settings']['disable_mail_send'] = 1;
48 }
49 return $form_data;
50 }
51
52 public function after ($form_data = array()) {
53
54 $this->WMVC = &wdk_get_instance();
55 $this->WMVC->model('messages_m');
56
57 $message_skip = false;
58
59 $email = '';
60 $message = '';
61 $post_id = '';
62 $wdk_widget = '';
63 foreach ($_POST as $key => $value) {
64 if (stripos($key, 'mail') !== false || filter_var($value, FILTER_VALIDATE_EMAIL)) {
65 $email = sanitize_email($value);
66 }
67 if (stripos($key, 'message') !== false) {
68 $message = sanitize_text_field($value);
69 }
70 if (stripos($key, 'wdk_widget') !== false) {
71 $wdk_widget = sanitize_text_field($value);
72 }
73 if (stripos($key, 'listing_id') !== false) {
74 $post_id = (int)sanitize_text_field($value);
75 }
76 }
77
78 if(function_exists('run_wdk_bookings') && (
79 is_user_logged_in() ||
80 $wdk_widget == 'wdk-booking-quick-submission' ||
81 wdk_get_option('wdk_booking_disable_for_not_login') ||
82 (!wdk_get_option('wdk_booking_disable_for_not_login') && wdk_get_option('wdk_bookings_enable_woocommerce_payments'))
83 )
84 ) {
85 if(isset($_POST['date_from']) && isset($_POST['date_to']) && !empty($_POST['date_from']) && !empty($_POST['date_to'])) {
86 $date_from = sanitize_text_field($_POST['date_from']);
87 $date_to = sanitize_text_field($_POST['date_to']);
88 $errors = '';
89
90 global $Winter_MVC_wdk_bookings;
91 $Winter_MVC_wdk_bookings->model('reservation_m');
92 $Winter_MVC_wdk_bookings->model('calendar_m');
93 $Winter_MVC_wdk_bookings->model('price_m');
94
95 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id'=>$post_id), TRUE); // date_package_expire package_id
96
97
98 if(wdk_is_date($date_from))
99 {
100 $date_from = wdk_normalize_date_db($date_from, 'Y-m-d H:i:s', (!wmvc_show_data('is_hour_enabled', $calendar,false)) ? 'Y-m-d': 'Y-m-d H:i:s');
101 }
102 else
103 {
104 return false;
105 }
106
107 if(wdk_is_date($date_to))
108 {
109 $date_to = wdk_normalize_date_db($date_to, 'Y-m-d H:i:s', (!wmvc_show_data('is_hour_enabled', $calendar,false)) ? 'Y-m-d': 'Y-m-d H:i:s');
110 }
111 else
112 {
113 return false;
114 }
115
116
117 /* message data */
118
119
120 $price = $Winter_MVC_wdk_bookings->reservation_m->calculate_price($post_id, $date_from, $date_to);
121
122 $calendar_fees = array();
123 if($calendar && !empty($calendar->json_data_fees))
124 $calendar_fees = json_decode($calendar->json_data_fees );
125
126 if($price) {
127 $guests_number = intval(wmvc_show_data('guests_number_adults', $_POST, 0)) + intval(wmvc_show_data('guests_number_childs', $_POST, 0));
128 $nights = (int)abs(strtotime($date_to) - strtotime($date_from))/(60*60*24);
129 foreach ($calendar_fees as $fee) {
130 if(!wmvc_show_data('is_activated', $fee, false,TRUE,TRUE)) continue;
131 if(is_intval(wmvc_show_data('value', $fee,'',TRUE,TRUE))) {
132 $field = wdk_generate_slug(strtolower(esc_html(wmvc_show_data('title', $fee,'-',TRUE,TRUE))));
133 if(!wmvc_show_data('is_required', $fee, false,TRUE,TRUE) && !isset($_POST['fee_'.$field])) {
134
135 } else {
136 if(wmvc_show_data('calculation_base', $fee,'',TRUE,TRUE) == 'per_night') {
137 $price['price'] += intval(wmvc_show_data('value', $fee,'',TRUE,TRUE)) * $nights;
138 } else if(wmvc_show_data('calculation_base', $fee,'',TRUE,TRUE) == 'per_person') {
139 $price['price'] += intval(wmvc_show_data('value', $fee,'',TRUE,TRUE)) * $guests_number;
140 } else {
141 $price['price'] += intval(wmvc_show_data('value', $fee,'',TRUE,TRUE));
142 }
143 }
144 }
145 }
146 }
147
148 $note = '<br>';
149 if(empty($calendar->is_guests_disabled)){
150 if(wmvc_show_data ('guests_number_adults', $_POST, false))
151 $note .= '<b>'.__('Guests Adult', 'wpdirectorykit').':</b> '.sanitize_text_field(wmvc_show_data('guests_number_adults', $_POST)).'<br>'.PHP_EOL;
152 if(wmvc_show_data ('guests_number_childs', $_POST, false))
153 $note .= '<b>'.__('Guests Childs', 'wpdirectorykit').':</b> '.sanitize_text_field(wmvc_show_data('guests_number_childs', $_POST)).'<br>'.PHP_EOL;
154 }
155
156 if(wmvc_show_data ('сhildrens_allowed', $_POST, false) || intval(wmvc_show_data ('guests_number_childs', $_POST, false)) > 0)
157 $note .= '<b>'.__('Childrens', 'wpdirectorykit').':</b> yes'.'<br>'.PHP_EOL;
158
159 if(wmvc_show_data ('pets_allowed', $_POST, false))
160 $note .= '<b>'.__('Pets', 'wpdirectorykit').':</b> yes'.'<br>'.PHP_EOL;
161
162 if(wmvc_show_data ('Name', $_POST, false))
163 $note .= '<b>'.__('Name', 'wpdirectorykit').':</b> '.sanitize_text_field(wmvc_show_data ('Name', $_POST)).'<br>'.PHP_EOL;
164
165 if(wmvc_show_data ('Email', $_POST, false))
166 $note .= '<b>'.__('Email', 'wpdirectorykit').':</b> '.sanitize_text_field(wmvc_show_data ('Email', $_POST)).'<br>'.PHP_EOL;
167
168 if(wmvc_show_data ('Message', $_POST, false))
169 $note .= '<b>'.__('Message', 'wpdirectorykit').':</b> '.sanitize_text_field(wmvc_show_data ('Message', $_POST)).'<br>'.PHP_EOL;
170
171 if(wmvc_show_data ('Phone', $_POST, false))
172 $note .= '<b>'.__('Phone', 'wpdirectorykit').':</b> '.sanitize_text_field(wmvc_show_data ('Phone', $_POST)).'<br>'.PHP_EOL;
173
174 if(true)
175 foreach($_POST as $key => $value) {
176 if($key=='element_id') continue;
177 if(in_array($key, array('eli_page_id', 'g-recaptcha-response','eli_id', 'eli_type','ID','filter','action','send_action_type','Phone','Message','Email','Name','pets_allowed','сhildrens_allowed','guests_number','guests_number_childs','guests_number_adults'))) continue;
178 if(in_array($key, array('listing_link','date_from','date_to','listing_id','wdk_widget', 'function','page','eli_nonce','eli_token','_wp_http_referer'))) continue;
179
180 if(filter_var($value, FILTER_VALIDATE_URL ) || strpos( $value, 'http' ) !== FALSE) {
181 // Dynamic field values are registered in the translation catalog separately.
182 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
183 $note .= '<b> '.__(str_replace('_',' ', ucfirst($key)), 'wpdirectorykit').':</b> <a href="'.esc_url($value).'">'.$value.'</a><br>';
184 } else {
185 // Dynamic field values are registered in the translation catalog separately.
186 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
187 $note .= '<b> '.__(str_replace('_',' ', ucfirst($key)), 'wpdirectorykit').':</b> '.$value.'<br>';
188 }
189 }
190
191
192 if( !is_user_logged_in()) {
193
194 if(
195 $wdk_widget == 'wdk-booking-quick-submission' ||
196 (!wdk_get_option('wdk_booking_disable_for_not_login') && wdk_get_option('wdk_bookings_enable_woocommerce_payments'))
197 ) {
198
199 /* if not disable for not login users, try create user */
200 $userlogin = $email;
201 $username = '';
202 $email_address = $email;
203 $password = wp_generate_password();
204
205 if(wmvc_show_data('name', $_POST, false))
206 $username = sanitize_text_field(wmvc_show_data('name', $_POST, false));
207
208 $user_id = wp_create_user( $userlogin, $password, $email_address );
209
210 if(is_intval($user_id)) {
211 // Set the nickname
212 wp_update_user(
213 array(
214 'ID' => $user_id,
215 'nickname' => $email_address
216 )
217 );
218
219 $user = new \WP_User( $user_id );
220 $user->set_role('wdk_visitor');
221
222 wp_update_user( array ('ID' => $user_id, 'display_name' => esc_html( $username ) ) );
223
224 $userdata = get_userdata($user_id);
225
226 $subject = __('New user on our website!', 'wpdirectorykit');
227 $data_message = array();
228 $data_message['login'] = $userlogin;
229 $data_message['password'] = $password;
230 $data_message['user'] = $userdata;
231 $data_message['data'] = '';
232
233 update_user_meta( $user_id, 'first_name', $username);
234
235 wdk_mail($email_address, $subject, $data_message, 'new_user_auto_created');
236
237 /* auto login */
238 wp_set_current_user($user_id, $userdata->user_login );
239 wp_set_auth_cookie($user_id);
240
241 } elseif($user = get_user_by( 'email', $email_address)) {
242 $user_id = $user->ID;
243 /* translators: 1: User email address. */
244 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.wdk_sprintf(__( 'For Booking required login, user with email %1$s already exists, please first try login', 'wpdirectorykit' ), $email_address).' <a href="'.wdk_login_url().'" target="_blank" style="text-decoration: underline;">'.esc_html__('here', 'wpdirectorykit').'</a></div>';
245 } else {
246 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('For Booking please login', 'wpdirectorykit').' <a href="'.wdk_login_url().'" target="_blank" style="text-decoration: underline;">'.esc_html__('here', 'wpdirectorykit').'</div>';
247 }
248 } else {
249 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('For Booking please login', 'wpdirectorykit').' <a href="'.wdk_login_url().'" target="_blank" style="text-decoration: underline;">'.esc_html__('here', 'wpdirectorykit').'</div>';
250 }
251 }
252
253 $data = array(
254 'post_id' => ($post_id),
255 'date_from' => ($date_from),
256 'date_to' => ($date_to),
257 'notes' => $message,
258 'user_id' => get_current_user_id(),
259 'price' => wmvc_show_data('price', $price),
260 'currency_code' => wdk_booking_currency_symbol(),
261 'calendar_id' => wmvc_show_data('idcalendar', $calendar),
262 'notes' => $note,
263 );
264
265 if(wmvc_show_data('is_enable_noapprovements', $calendar, false))
266 $data['is_approved'] = 1;
267
268 $results = $Winter_MVC_wdk_bookings->reservation_m->is_booked($post_id, $date_from, $date_to);
269
270
271 $Winter_MVC_wdk_bookings->db->where("date_from <='".$date_from."'");
272 $Winter_MVC_wdk_bookings->db->where("date_to >= '".$date_to."'");
273 $price_reservation = $Winter_MVC_wdk_bookings->price_m->get_by(array('post_id'=>$post_id), TRUE);
274 /* manual validation */
275 if( !$results ){
276 if($date_from == $date_to) {
277 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Please set different dates', 'wpdirectorykit').'</div>';
278 }
279
280 if(empty($calendar->is_guests_disabled)){
281 $guests_number = intval(wmvc_show_data('guests_number_adults', $_POST, 0)) + intval(wmvc_show_data('guests_number_childs', $_POST, 0));
282 if(!empty(wmvc_show_data('guests', $calendar, 0, TRUE, TRUE)) && $guests_number > wmvc_show_data('guests', $calendar, 0, TRUE, TRUE)) {
283 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Possible max', 'wpdirectorykit').' '.esc_html(wmvc_show_data('guests', $calendar, 0, TRUE, TRUE)).' '.esc_html__('guest(s)', 'wpdirectorykit').'</div>';
284 }
285 }
286
287 if((wmvc_show_data('сhildrens_allowed', $_POST, false) || wmvc_show_data('guests_number_childs', $_POST, 0) > 0) && !wmvc_show_data('is_children_acceptable', $calendar, false, TRUE, TRUE)) {
288 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Only without children\'s', 'wpdirectorykit').'</div>';
289 }
290
291 if(wmvc_show_data('pets_allowed', $_POST, false) && !wmvc_show_data('is_pets_acceptable', $calendar, false, TRUE, TRUE)) {
292 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Only without pet\'s', 'wpdirectorykit').'</div>';
293 }
294
295 $days = array(
296 '1'=>__('Monday', 'wpdirectorykit'),
297 '2'=>__('Tuesday', 'wpdirectorykit'),
298 '3'=>__('Wednesday', 'wpdirectorykit'),
299 '4'=>__('Thursday', 'wpdirectorykit'),
300 '5'=>__('Friday', 'wpdirectorykit'),
301 '6'=>__('Saturday', 'wpdirectorykit'),
302 '7'=>__('Sunday', 'wpdirectorykit'),
303 );
304
305 $day_num_from = gmdate('N', strtotime($date_from));
306 $day_num_to = gmdate('N', strtotime($date_to));
307
308 if(wmvc_show_data('changeover_day', $price_reservation, false, TRUE, TRUE) && (
309 wmvc_show_data('changeover_day', $price_reservation, false, TRUE, TRUE) != $day_num_from
310 || wmvc_show_data('changeover_day', $price_reservation, false, TRUE, TRUE) != $day_num_to
311 )
312 ) {
313 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Changeover day is', 'wpdirectorykit').' '.esc_html($days[wmvc_show_data('changeover_day', $price_reservation, false, TRUE, TRUE)]).'</div>';
314 }
315
316 $hours_reservation = abs(strtotime($date_to) - strtotime($date_from))/(60*60);
317
318 if(wmvc_show_data('min_hours', $price_reservation, false, TRUE, TRUE) && wmvc_show_data('min_hours', $price_reservation, false, TRUE, TRUE) > $hours_reservation) {
319 $period = '';
320 $min_hours = intval(wmvc_show_data('min_hours', $price_reservation, false, TRUE, TRUE));
321 if((int)($min_hours/24) >= 1) {
322 $days = (int)($min_hours/24);
323 $period .= esc_html(sprintf(
324 /* translators: 1: Number of days. */
325 _nx(
326 '%1$s day',
327 '%1$s days',
328 $days,
329 'days',
330 'wpdirectorykit'
331 ), $days));
332
333 $min_hours -= $days * 24;
334 }
335
336 if(!empty($min_hours)) {
337 $period .= ' '.esc_html(sprintf(
338 /* translators: 1: Number of hours. */
339 _nx(
340 '%1$s hour',
341 '%1$s hours',
342 (int)($min_hours),
343 'hours',
344 'wpdirectorykit'
345 ), (int)($min_hours)));
346 }
347
348 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Min', 'wpdirectorykit').' '.esc_html($period).'</div>';
349 }
350
351 if(wmvc_show_data('max_hours', $price_reservation, false, TRUE, TRUE) && wmvc_show_data('max_hours', $price_reservation, false, TRUE, TRUE) < $hours_reservation) {
352 $period = '';
353 $max_hours = intval(wmvc_show_data('max_hours', $price_reservation, false, TRUE, TRUE));
354 if((int)($max_hours/24) >= 1) {
355 $days = (int)($max_hours/24);
356 $period .= esc_html(sprintf(
357 /* translators: 1: Number of days. */
358 _nx(
359 '%1$s day',
360 '%1$s days',
361 $days,
362 'days',
363 'wpdirectorykit'
364 ), $days));
365
366 $max_hours -= $days * 24;
367 }
368
369 if(!empty($max_hours)) {
370 $period .= ' '.esc_html(sprintf(
371 /* translators: 1: Number of hours. */
372 _nx(
373 '%1$s hour',
374 '%1$s hours',
375 (int)($max_hours),
376 'hours',
377 'wpdirectorykit'
378 ), (int)($max_hours)));
379 }
380
381 $errors .= '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Max', 'wpdirectorykit').' '.esc_html($period).'</div>';
382 }
383 }
384
385 if( $results ) {
386 add_filter( 'eli/ajax-handler/filter_output', function($filter_output){
387 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.esc_html__('Date not available', 'wpdirectorykit').'</div>';
388 $filter_output['no_clear_from'] = true;
389 $filter_output['success'] = false;
390 return $filter_output;
391 } );
392 } elseif( !empty($errors) ) {
393 add_filter( 'eli/ajax-handler/filter_output', function($filter_output) use ($errors) {
394 $filter_output['message'] = $errors;
395 $filter_output['success'] = false;
396 $filter_output['no_clear_from'] = true;
397 return $filter_output;
398 } );
399 } else {
400
401 if(wmvc_show_data('is_hour_enabled', $calendar, false) && !empty($data['date_from']) && !empty($data['date_to'])) {
402 $data['date_from'] = gmdate('Y-m-d 00:00:00', strtotime($data['date_from']));
403 $data['date_to'] = gmdate('Y-m-d 00:00:00', strtotime($data['date_to']));
404 }
405 $insert_id = $Winter_MVC_wdk_bookings->reservation_m->insert($data, NULL);
406
407 /* auto is_enable_noapprovements enabled */
408 if(wmvc_show_data('is_enable_noapprovements', $calendar, false)) {
409 /* if woo payment enabled */
410 if(wdk_get_option('wdk_bookings_enable_woocommerce_payments')) {
411 global $Winter_MVC_WDK;
412 $Winter_MVC_WDK->model('listingusers_m');
413 $Winter_MVC_WDK->model('listing_m');
414 $Winter_MVC_WDK->load_helper('listing');
415 $listing = $Winter_MVC_WDK->listing_m->get($post_id, TRUE);
416 $user_owner_id = NULL;
417 $user_data_owner = NULL;
418
419 if( wmvc_show_data('user_id_editor', $listing, '', TRUE, TRUE )) {
420 $user_owner_id = wmvc_show_data('user_id_editor', $listing, '', TRUE, TRUE );
421 if(wmvc_show_data('user_id', $user_listing, false, TRUE, TRUE )) {
422 $user_data_owner = get_userdata( wmvc_show_data('user_id', $user_listing, false, TRUE, TRUE ) );
423 }
424 }
425
426 if(empty($user_data_owner)) {
427 $user_data_owner = array(
428 'display_name' => __('Administrator', 'wpdirectorykit'),
429 'user_email' => get_bloginfo('admin_email'),
430 );
431 }
432
433 /* auto create woo item and redirect to order */
434 $url_pay = '';
435
436 if(class_exists( 'WooCommerce' )) {
437
438 $title = __('Reservation for', 'wpdirectorykit').' '.wdk_field_value ('post_title', $post_id).' #'.$post_id;
439 if($user_owner_id) {
440 $title .= ' A'.$user_owner_id;
441 }
442 $title .= ' '.wdk_get_date($date_from).' - '.wdk_get_date($date_to); // The product's Title
443
444 $post_args = array (
445 'post_author' => $user_owner_id, // The user's ID
446 'post_title' => $title, // The product's Title
447 'post_content' => str_replace(PHP_EOL,'<br style="line-height: 0;">', $note), // The product's Title
448 'post_type' => 'product',
449 'post_status' => 'publish' // This could also be $data['status'];
450 );
451
452 $woo_id = wp_insert_post( $post_args );
453 // If the post was created okay, let's try update the WooCommerce values.
454 if ( ! empty( $woo_id ) && function_exists( 'wc_get_product' ) ) {
455 $product = wc_get_product( $woo_id );
456 $product->set_virtual(true);
457 $product->set_sold_individually(true);
458 $product->set_sku( 'wdk-booking-' . $woo_id ); // Generate a SKU with a prefix. (i.e. 'pre-123')
459 $product->set_regular_price(wmvc_show_data('price', $price)); // Be sure to use the correct decimal price.
460 $product->save(); // Save/update the WooCommerce order object.
461
462 $terms = array( 'exclude-from-catalog', 'exclude-from-search' );
463 wp_set_object_terms( $woo_id, $terms, 'product_visibility' );
464
465 update_post_meta($woo_id, '_manage_stock', 'yes');
466 update_post_meta($woo_id, '_stock', 1);
467
468 $wdk_attach_id = NULL;
469 $image_ids = explode(',', trim(wdk_show_data('listing_images' , $listing, '', TRUE, TRUE), ','));
470
471 if(is_array($image_ids))
472 $wdk_attach_id = $image_ids[0];
473
474 set_post_thumbnail( $woo_id, $wdk_attach_id );
475
476 /* set woo product id for reservation */
477 $update_data = array('woocommerce_product_id'=>$woo_id);
478 $Winter_MVC_wdk_bookings->reservation_m->insert($update_data, $insert_id);
479
480 if(function_exists('wc_get_cart_url')) {
481 $url_pay = wdk_url_suffix(wc_get_cart_url(), 'add-to-cart='.esc_attr($woo_id).'&reservation_id='.esc_attr($insert_id));
482
483 add_filter( 'eli/ajax-handler/filter_output', function($filter_output) use ($url_pay){
484 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-success" role="alert">'.esc_html__('Reservation created, auto redirect to order page', 'wpdirectorykit').'</div>';
485 $filter_output['redirect'] = $url_pay;
486 return $filter_output;
487 } );
488 }
489 }
490 } else {
491
492 }
493 /* message */
494 $data_message = array();
495 $data_message['user'] = get_userdata(get_current_user_id());
496 $data_message['user_owner'] = get_userdata(get_current_user_id());
497 $data_message['listing'] = $listing;
498 $data_message['user_owner'] = $user_data_owner;
499 $data_message['pay_link'] = $url_pay;
500 $data_message['hours_for_payment'] = false;
501 $data_message['reservation'] = $data;
502 $data_message['reservation_id'] = $insert_id;
503
504 if(wdk_get_option(('wdk_bookings_time_for_payment'))) {
505 $data_message['hours_for_payment'] = intval(wdk_get_option(('wdk_bookings_time_for_payment')));
506
507 /* set date_expire for reservation */
508 $update_data = array('date_expire'=>esc_sql(gmdate("Y-m-d H:i:s", strtotime("+".intval(wdk_get_option(('wdk_bookings_time_for_payment')))." hours"))));
509 $Winter_MVC_wdk_bookings->reservation_m->insert($update_data, $insert_id);
510 }
511
512 wdk_mail(wdk_show_data('user_email', $data_message['user'], '' , TRUE, TRUE), __('Reservation waiting for payment', 'wpdirectorykit'), $data_message, 'reservation_user_payment_notify');
513 } else {
514 /* manual pay */
515 global $Winter_MVC_WDK;
516 $user_owner_id = NULL;
517 $Winter_MVC_WDK->model('listingusers_m');
518 $Winter_MVC_WDK->model('listing_m');
519 $Winter_MVC_WDK->load_helper('listing');
520 $listing = $Winter_MVC_WDK->listing_m->get($post_id, TRUE);
521 $user = get_userdata( wmvc_show_data('user_id', $data) );
522
523 $user_owner_id = NULL;
524 $user_data_owner = NULL;
525 if(wmvc_show_data('user_id_editor', $listing, '', TRUE, TRUE )) {
526 $user_owner_id = wmvc_show_data('user_id_editor', $listing, '', TRUE, TRUE );
527 $user_data_owner = get_userdata( wmvc_show_data('user_id_editor', $listing, false, TRUE, TRUE ) );
528 }
529
530 if(empty($user_data_owner)) {
531 $user_data_owner = array(
532 'display_name' => __('Administrator', 'wpdirectorykit'),
533 'user_email' => get_bloginfo('admin_email'),
534 );
535 }
536
537 /* message */
538 $data_message = array();
539 $data_message['user'] = $user;
540 $data_message['user_owner'] = $user_data_owner;
541 $data_message['listing'] = $listing;
542 $data_message['reservation'] = $data;
543 $data_message['reservation_id'] = $insert_id;
544 $data_message['data'] = array(
545 __('Reservation ID', 'wpdirectorykit')=> $insert_id,
546 __('Date From', 'wpdirectorykit')=> wdk_get_date($date_from),
547 __('Date To', 'wpdirectorykit')=> wdk_get_date($date_to),
548 __('Nights', 'wpdirectorykit')=> wdk_calculate_nights(wdk_get_date($date_from), wdk_get_date($date_to)),
549 );
550
551 $nights = (int)abs(strtotime($date_to) - strtotime($date_from))/(60*60*24);
552 $guests_number = intval(wmvc_show_data('guests_number_adults', $_POST, 0)) + intval(wmvc_show_data('guests_number_childs', $_POST, 0));
553 foreach ($calendar_fees as $fee) {
554 if(!wmvc_show_data('is_activated', $fee, false,TRUE,TRUE)) continue;
555
556 $field = wdk_generate_slug(strtolower(esc_html(wmvc_show_data('title', $fee,'-',TRUE,TRUE))));
557 if(!wmvc_show_data('is_required', $fee, false,TRUE,TRUE) && !isset($_POST['fee_'.$field])) {
558
559 } else {
560 if(wmvc_show_data('calculation_base', $fee,'',TRUE,TRUE) == 'per_night') {
561 $data_message['data'][__('Price', 'wpdirectorykit').' '.wmvc_show_data('title', $fee, '-', TRUE, TRUE)] = intval(wmvc_show_data('value', $fee,'',TRUE,TRUE)) * $nights;
562 } else if(wmvc_show_data('calculation_base', $fee,'',TRUE,TRUE) == 'per_person') {
563 $data_message['data'][__('Price', 'wpdirectorykit').' '.wmvc_show_data('title', $fee, '-', TRUE, TRUE)] = intval(wmvc_show_data('value', $fee,'',TRUE,TRUE)) * $guests_number;
564 } else {
565 $data_message['data'][__('Price', 'wpdirectorykit').' '.wmvc_show_data('title', $fee, '-', TRUE, TRUE)] = intval(wmvc_show_data('value', $fee,'',TRUE,TRUE));
566 }
567 }
568 }
569
570 $data_message['data'][__('Total Price', 'wpdirectorykit')] = wmvc_show_data('price', $price).wdk_booking_currency_symbol();
571
572 $data_message['notes'] = $note;
573 if(wmvc_show_data('payment_info', $calendar, false, TRUE, TRUE)) {
574 $data_message['data'][__('Payment info', 'wpdirectorykit')] = str_replace(PHP_EOL,'<br style="line-height: 0;">', wmvc_show_data('payment_info', $calendar, false, TRUE, TRUE));
575 }
576
577 $ret = wdk_mail(wdk_show_data('user_email', $data_message['user'], '' , TRUE, TRUE), __('Reservation approved, waiting for payment', 'wpdirectorykit'), $data_message, 'reservation_approved_visitor');
578
579 $ret = wdk_mail(wdk_show_data('user_email', $data_message['user_owner'], '' , TRUE, TRUE), __('New Reservation approved, please confirm as paid when you receive payment', 'wpdirectorykit'), $data_message, 'reservation_approved_owner');
580
581 if( $ret) {
582 add_filter( 'eli/ajax-handler/filter_output', function($filter_output) {
583 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-success" role="alert">'.esc_html__('Reservation sent, please check your email with payment details', 'wpdirectorykit').'</div>';
584 return $filter_output;
585 } );
586 }
587 else
588 {
589 add_filter( 'eli/ajax-handler/filter_output', function($filter_output) {
590 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'. sprintf(
591 /* translators: 1: Tag with link open, 2: close link tag. */
592 esc_html__('Server can\'t send emails, please use SMTP mail configuration. Check %1$sGuide%2$s', 'wpdirectorykit'),
593 '<a href="https://wpdirectorykit.com/e-mail-sending-issues-smtp-mail-configuration/" target="_blank" style="text-decoration: underline">',
594 '</a>'
595 ).'</div>';
596 $filter_output['no_clear_from'] = true;
597 return $filter_output;
598 } );
599 }
600 }
601
602 } else {
603 /* auto is_enable_noapprovements disabled */
604 global $Winter_MVC_WDK;
605 $Winter_MVC_WDK->model('listingusers_m');
606 $Winter_MVC_WDK->model('listing_m');
607 $Winter_MVC_WDK->load_helper('listing');
608
609 $data_listing = $Winter_MVC_WDK->listing_m->get($post_id, TRUE);
610 $user_client = get_userdata( wmvc_show_data('user_id', $data) );
611 /* owner data */
612 $user_owner = NULL;
613 $owner_email = get_bloginfo('admin_email');
614
615 if(wmvc_show_data('user_id_editor', $data_listing, false, TRUE, TRUE )) {
616 $user_owner = get_userdata( wmvc_show_data('user_id_editor', $data_listing, false, TRUE, TRUE ) );
617 if($user_owner)
618 $owner_email = $user_owner->user_email;
619 }
620
621 /* waiting approve owner */
622 $data_message = array();
623 $data_message['user_owner'] = $user_owner;
624 $data_message['user_client'] = $user_client;
625 $data_message['reservation_id'] = $insert_id;
626 $data_message['reservation'] = $data; /* reservation data */
627 $data_message['listing'] = $data_listing;
628 $data_message['data'] = array(
629 __('Reservation ID', 'wpdirectorykit')=> $insert_id,
630 __('Date From', 'wpdirectorykit')=> wdk_get_date($date_from),
631 __('Date To', 'wpdirectorykit')=> wdk_get_date($date_to),
632 __('Nights', 'wpdirectorykit')=> wdk_calculate_nights(wdk_get_date($date_from), wdk_get_date($date_to)),
633 );
634 $nights = (int)abs(strtotime($date_to) - strtotime($date_from))/(60*60*24);
635 $guests_number = intval(wmvc_show_data('guests_number_adults', $_POST, 0)) + intval(wmvc_show_data('guests_number_childs', $_POST, 0));
636 foreach ($calendar_fees as $fee) {
637 if(!wmvc_show_data('is_activated', $fee, false,TRUE,TRUE)) continue;
638
639 $field = wdk_generate_slug(strtolower(esc_html(wmvc_show_data('title', $fee,'-',TRUE,TRUE))));
640 if(!wmvc_show_data('is_required', $fee, false,TRUE,TRUE) && !isset($_POST['fee_'.$field])) {
641
642 } else {
643 if(wmvc_show_data('calculation_base', $fee,'',TRUE,TRUE) == 'per_night') {
644 $data_message['data'][__('Price', 'wpdirectorykit').' '.wmvc_show_data('title', $fee, '-', TRUE, TRUE)] = intval(wmvc_show_data('value', $fee,'',TRUE,TRUE)) * $nights;
645 } else if(wmvc_show_data('calculation_base', $fee,'',TRUE,TRUE) == 'per_person') {
646 $data_message['data'][__('Price', 'wpdirectorykit').' '.wmvc_show_data('title', $fee, '-', TRUE, TRUE)] = intval(wmvc_show_data('value', $fee,'',TRUE,TRUE)) * $guests_number;
647 } else {
648 $data_message['data'][__('Price', 'wpdirectorykit').' '.wmvc_show_data('title', $fee, '-', TRUE, TRUE)] = intval(wmvc_show_data('value', $fee,'',TRUE,TRUE));
649 }
650 }
651 }
652
653 $data_message['data'][__('Total Price', 'wpdirectorykit')] = wmvc_show_data('price', $price).wdk_booking_currency_symbol();
654
655 $data_message['notes'] = $note;
656
657 if(wmvc_show_data ('сhildrens_allowed', $_POST, false))
658 $data_message['data'][__('Childrens', 'wpdirectorykit')] = 'yes';
659
660 if(wmvc_show_data ('pets_allowed', $_POST, false))
661 $data_message['data'][__('Pets', 'wpdirectorykit')] = 'yes';
662
663 if(empty($calendar->is_guests_disabled)){
664 if(wmvc_show_data ('guests_number_adults', $_POST, false))
665 $data_message['data'][__('Guests Adult', 'wpdirectorykit')] = sanitize_text_field(wmvc_show_data ('guests_number_adults', $_POST));
666 if(wmvc_show_data ('guests_number_childs', $_POST, false))
667 $data_message['data'][__('Guests Childs', 'wpdirectorykit')] = sanitize_text_field(wmvc_show_data ('guests_number_childs', $_POST));
668 }
669
670 if(wmvc_show_data ('Name', $_POST, false))
671 $data_message['data'][__('Name', 'wpdirectorykit')] = sanitize_text_field(wmvc_show_data ('Name', $_POST));
672
673 if(wmvc_show_data ('Email', $_POST, false))
674 $data_message['data'][__('Email', 'wpdirectorykit')] = sanitize_text_field(wmvc_show_data ('Email', $_POST));
675
676 if(wmvc_show_data ('Message', $_POST, false))
677 $data_message['data'][__('Message', 'wpdirectorykit')] = sanitize_text_field(wmvc_show_data ('Message', $_POST));
678
679 $ret = wdk_mail( $owner_email, __('New Reservation Waiting For Approvement', 'wpdirectorykit'), $data_message, 'reservation_waiting_for_approve_owner');
680
681 /* waiting approve to client */
682 $ret = wdk_mail(wdk_show_data('user_email', $user_client, '' , TRUE, TRUE), __('Your reservation waiting approvement by owner', 'wpdirectorykit'), $data_message, 'reservation_waiting_for_approve_visitor');
683
684 if( $ret) {
685 add_filter( 'eli/ajax-handler/filter_output', function($filter_output){
686 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-success" role="alert">'.esc_html__('Thanks on reservation, after owner approvement you will receive details for payment', 'wpdirectorykit').'</div>';
687 return $filter_output;
688 } );
689 }
690 else
691 {
692 add_filter( 'eli/ajax-handler/filter_output', function($filter_output) {
693 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'. sprintf(
694 /* translators: 1: Tag with link open, 2: close link tag. */
695 esc_html__('Server can\'t send emails, please use SMTP mail configuration. Check %1$sGuide%2$s', 'wpdirectorykit'),
696 '<a href="https://wpdirectorykit.com/e-mail-sending-issues-smtp-mail-configuration/" target="_blank" style="text-decoration: underline">',
697 '</a>'
698 ).'</div>';
699 $filter_output['no_clear_from'] = true;
700 return $filter_output;
701 } );
702 }
703 }
704
705
706 }
707
708 /* skip default message */
709 $message_skip = true;
710 }
711 }
712
713 if(!$message_skip) {
714 /* message data */
715 if (!empty($post_id) && !empty($email)) {
716 $data = array(
717 'post_id' => ($post_id),
718 'email_sender' => ($email),
719 'message' => ($message),
720 'json_object' => json_encode($_POST),
721 'user_id_sender' => get_current_user_id()
722 );
723
724 $insert_idmessage = $this->WMVC->messages_m->insert($data, null);
725 if ($insert_idmessage && isset($_POST['listing_id'])) {
726 $data_mess= array();
727
728 foreach($_POST as $key => $value){
729 if($key=='element_id') continue;
730 if(in_array($key, array('eli_id','eli_page_id', 'eli_type','ID','filter','action','send_action_type', 'page_link', 'g-recaptcha-response','eli_nonce','eli_token','_wp_http_referer'))) continue;
731
732 if(empty($value)) continue;
733
734 if(filter_var($value, FILTER_VALIDATE_URL ) || strpos( $value, 'http' ) !== FALSE) {
735 // Dynamic field values are registered in the translation catalog separately.
736 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
737 $data_mess []= '<p><strong>'.__(str_replace('_',' ', ucfirst($key)), 'wpdirectorykit').':</strong> <a href="'.esc_url($value).'">'.$value.'</a></p>';
738 } else {
739 // Dynamic field values are registered in the translation catalog separately.
740 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
741 $data_mess []= '<p><strong>'.__(str_replace('_',' ', ucfirst($key)), 'wpdirectorykit').':</strong> '.$value.'</p>';
742 }
743 }
744
745 global $Winter_MVC_WDK;
746 $Winter_MVC_WDK->model('listingusers_m');
747 $Winter_MVC_WDK->model('listing_m');
748 $Winter_MVC_WDK->load_helper('listing');
749 $data_listing = $Winter_MVC_WDK->listing_m->get($post_id, TRUE);
750
751 if(function_exists('run_wdk_bookings')) {
752 if(isset($_POST['date_from']) && isset($_POST['date_to']) && !empty($_POST['date_from']) && !empty($_POST['date_to'])) {
753 $data_mess []= '<p><strong>'.__('Nights', 'wpdirectorykit').':</strong> '.wdk_calculate_nights(wdk_get_date($_POST['date_from']), wdk_get_date($_POST['date_to'])).'</p>';
754 }
755 }
756
757 /* message for user */
758 if (!empty($email)) {
759 $data_message = array();
760 $data_message['email_data'] = $data;
761 $data_message['listing'] = $data;
762 $data_message['message'] = '';
763
764 if(function_exists('run_wdk_bookings')) {
765 if(isset($_POST['date_from']) && isset($_POST['date_to']) && !empty($_POST['date_from']) && !empty($_POST['date_to'])) {
766 $data_message['message'] .= '<h2>'.__('Date reservation change to', 'wpdirectorykit').'</h2>';
767 $data_message['message'] .= '<p>'.__('Details from contact form', 'wpdirectorykit').'</p>';
768 }
769 }
770
771 $data_message['message'] .= '<p>'.__('Contact message related to listing', 'wpdirectorykit').' <a href="'.esc_url(get_permalink($data_listing)).'">'.esc_html(wmvc_show_data('post_title', $data_listing, '', TRUE, TRUE)).'</a></p>';
772
773 $data_message['message'] .= implode('', $data_mess);
774
775 $ret = wdk_mail($email, __('Your messages sent on', 'wpdirectorykit').' '.html_entity_decode(get_bloginfo('name')), $data_message, 'new_message');
776 }
777
778 /* message for admin */
779 $data_message = array();
780 $data_message['email_data'] = $data;
781 $data_message['message'] = '';
782
783
784 $message_title = __('New Message', 'wpdirectorykit');
785
786 if(function_exists('run_wdk_bookings')) {
787
788 if(isset($_POST['date_from']) && isset($_POST['date_to']) && !empty($_POST['date_from']) && !empty($_POST['date_to'])) {
789 $data_message['message'] .= '<h2>'.__('Date reservation change to', 'wpdirectorykit').'</h2>';
790 $data_message['message'] .= '<p>'.__('Details from contact form', 'wpdirectorykit').'</p>';
791
792 $message_title = __('Contact message related to reservation on page', 'wpdirectorykit').' '.get_bloginfo('name');
793 }
794 }
795
796 $data_message['message'] .= '<p>'.__('Contact message related to listing', 'wpdirectorykit').' <a href="'.esc_url(get_permalink($data_listing)).'">'.esc_html(wmvc_show_data('post_title', $data_listing, '', TRUE, TRUE)).'</a></p>';
797 $data_message['message'] .= implode('', $data_mess);
798
799 /* owner data */
800 $user_owner = NULL;
801 $owner_email = get_bloginfo('admin_email');
802 if(wmvc_show_data('user_id_editor', $data_listing, false, TRUE, TRUE )) {
803 $user_owner = get_userdata( wmvc_show_data('user_id_editor', $data_listing, false, TRUE, TRUE ) );
804 if($user_owner)
805 $owner_email = $user_owner->user_email;
806 }
807
808 $ret = wdk_mail($owner_email, $message_title, $data_message, 'new_message', '', NULL, $email);
809
810 if( $ret) {
811 add_filter( 'eli/ajax-handler/filter_output', function($filter_output){
812 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-success" role="alert">'.esc_html__('Thanks for the message.', 'wpdirectorykit').'</div>';
813 return $filter_output;
814 } );
815 add_filter( 'eli/ajax-handler/filter_from_data', function ($form_data = array()) {
816 /* disable double sent to admin mail */
817 $form_data['settings']['disable_mail_send'] = 1;
818 return $form_data;
819 }
820 );
821 }
822 else
823 {
824 add_filter( 'eli/ajax-handler/filter_output', function($filter_output) {
825 $filter_output['message'] = '<div class="elementinvader_addons_for_elementor_alert elementinvader_addons_for_elementor_alert-danger" role="alert">'.
826 sprintf(
827 /* translators: 1: Tag with link open, 2: close link tag. */
828 esc_html__('Server can\'t send emails, please use SMTP mail configuration. Check %1$sGuide%2$s', 'wpdirectorykit'),
829 '<a href="https://wpdirectorykit.com/e-mail-sending-issues-smtp-mail-configuration/" target="_blank" style="text-decoration: underline">',
830 '</a>'
831 )
832 .'</div>';
833
834
835 $filter_output['success'] = false;
836 $filter_output['no_clear_from'] = true;
837 return $filter_output;
838 } );
839 }
840
841
842
843
844 }
845 }
846 }
847
848
849 }
850
851 }
852