PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/transfers-plugin/includes/plugin_ajax.php

https://gitlab.com/luyxtran264/myproject
PHP | 196 lines | 139 code | 54 blank | 3 comment | 21 complexity | 8b3dbbaa5202894faef47e87500f6f51 MD5 | raw file
  1. <?php
  2. class Transfers_Plugin_Ajax extends Transfers_BaseSingleton {
  3. protected function __construct() {
  4. // our parent class might contain shared code in its constructor
  5. parent::__construct();
  6. }
  7. public function init() {
  8. add_action( 'wp_ajax_number_format_i18n_request', array( $this, 'number_format_i18n_request' ) );
  9. add_action( 'wp_ajax_nopriv_number_format_i18n_request', array( $this, 'number_format_i18n_request' ) );
  10. add_action( 'wp_ajax_book_transfer_ajax_request', array( $this, 'book_transfer_ajax_request') );
  11. add_action( 'wp_ajax_nopriv_book_transfer_ajax_request', array( $this, 'book_transfer_ajax_request') );
  12. add_action( 'wp_ajax_transfers_extra_tables_ajax_request', array( $this, 'transfers_extra_tables_ajax_request' ) );
  13. add_action( 'wp_ajax_nopriv_transfers_extra_tables_ajax_request', array( $this, 'transfers_extra_tables_ajax_request' ) );
  14. }
  15. function transfers_extra_tables_ajax_request() {
  16. if ( isset($_REQUEST) ) {
  17. $nonce = $_REQUEST['nonce'];
  18. if ( wp_verify_nonce( $nonce, 'optionsframework-options' ) ) {
  19. global $transfers_plugin_post_types;
  20. $transfers_plugin_post_types->create_extra_tables(true);
  21. echo "1";
  22. } else {
  23. echo "00";
  24. }
  25. } else {
  26. echo "-1";
  27. }
  28. die();
  29. }
  30. function book_transfer_ajax_request() {
  31. if ( isset($_REQUEST) ) {
  32. $nonce = $_REQUEST['nonce'];
  33. if ( wp_verify_nonce( $nonce, 'transfers-ajax-nonce' ) ) {
  34. global $transfers_plugin_globals, $transfers_plugin_post_types;
  35. $enc_key = $transfers_plugin_globals->get_enc_key();
  36. $add_captcha_to_forms = $transfers_plugin_globals->add_captcha_to_forms();
  37. $c_val_s = 0;
  38. $c_val_1 = 0;
  39. $c_val_2 = 0;
  40. if ($add_captcha_to_forms) {
  41. $c_val_s = intval(wp_kses($_REQUEST['cValS'], ''));
  42. $c_val_1_str = transfers_decrypt(wp_kses($_REQUEST['cVal1'], ''), $enc_key);
  43. $c_val_2_str = transfers_decrypt(wp_kses($_REQUEST['cVal2'], ''), $enc_key);
  44. $c_val_1 = intval($c_val_1_str);
  45. $c_val_2 = intval($c_val_2_str);
  46. }
  47. if ($add_captcha_to_forms && $c_val_s != ($c_val_1 + $c_val_2)) {
  48. echo 'captcha_error';
  49. } else {
  50. $booking_object = $transfers_plugin_post_types->retrieve_booking_object_from_request();
  51. $people_count = $booking_object->departure_booking_args['people_count'];
  52. $departure_date = $booking_object->departure_booking_args['booking_datetime'];
  53. $departure_is_private = $booking_object->departure_booking_args['is_private'];
  54. $departure_slot_minutes = $booking_object->departure_slot_minutes;
  55. $departure_destination_from = $booking_object->departure_destination_from;
  56. $departure_destination_to = $booking_object->departure_destination_to;
  57. $departure_transport_type = $booking_object->departure_transport_type;
  58. $both_legs_price = $booking_object->departure_booking_args['total_price'];
  59. $departure_extra_items_string = $booking_object->departure_extra_items_string;
  60. $first_name = $booking_object->departure_booking_args['first_name'];
  61. $last_name = $booking_object->departure_booking_args['last_name'];
  62. $phone = $booking_object->departure_booking_args['phone'];
  63. $email = $booking_object->departure_booking_args['email'];
  64. $address = $booking_object->departure_booking_args['address'];
  65. $zip = $booking_object->departure_booking_args['zip'];
  66. $town = $booking_object->departure_booking_args['town'];
  67. $state = $booking_object->departure_booking_args['state'];
  68. $country = $booking_object->departure_booking_args['country'];
  69. $first_name = $booking_object->departure_booking_args['first_name'];
  70. $departure_booking_id = $transfers_plugin_post_types->create_booking_entry($booking_object->departure_booking_args);
  71. $return_booking_id = 0;
  72. $return_availability = null;
  73. $return_slot_minutes = '';
  74. $return_destination_from = '';
  75. $return_destination_to = '';
  76. $return_transport_type = '';
  77. $return_is_private = false;
  78. $return_extra_items_string = '';
  79. $return_date = null;
  80. if (isset($booking_object->return_booking_args) && $booking_object->return_booking_args != null) {
  81. $return_booking_id = $transfers_plugin_post_types->create_booking_entry($booking_object->return_booking_args);
  82. $both_legs_price += $booking_object->return_booking_args['total_price'];
  83. $return_slot_minutes = $booking_object->return_slot_minutes;
  84. $return_is_private = $booking_object->return_booking_args['is_private'];
  85. $return_extra_items_string = $booking_object->return_extra_items_string;
  86. $return_date = $booking_object->return_booking_args['booking_datetime'];
  87. $return_destination_from = $booking_object->return_destination_from;
  88. $return_destination_to = $booking_object->return_destination_to;
  89. $return_transport_type = $booking_object->return_transport_type;
  90. }
  91. $admin_email = get_bloginfo('admin_email');
  92. $admin_name = get_bloginfo('name');
  93. $headers = "From: $admin_name <$admin_email>\n";
  94. $subject = esc_html__('New transfer booking', 'transfers');
  95. $price_decimal_places = $transfers_plugin_globals->get_price_decimal_places();
  96. $default_currency_symbol = $transfers_plugin_globals->get_default_currency_symbol();
  97. $show_currency_symbol_after = $transfers_plugin_globals->show_currency_symbol_after();
  98. $formatted_both_legs_price = number_format_i18n( $both_legs_price, $price_decimal_places );
  99. if ($show_currency_symbol_after) {
  100. $formatted_both_legs_price = $formatted_both_legs_price . ' ' . $default_currency_symbol;
  101. } else {
  102. $formatted_both_legs_price = $default_currency_symbol . ' ' . $formatted_both_legs_price;
  103. }
  104. $message = '';
  105. if ($booking_object->return_booking_args['availability_id'] > 0) {
  106. $message = esc_html__("New transfer booking: \n\nFirst name: %s \n\nLast name: %s \n\nEmail: %s \n\nPhone: %s \n\nAddress: %s \n\nTown: %s \n\nZip: %s \n\nState: %s \n\nCountry: %s \n\nPeople: %d \n\nDeparture Date: %s \n\nDeparture Private? %s \n\nDeparture From: %s \n\nDeparture To: %s \n\nDeparture Transport type: %s \n\nDeparture Extra items: %s \n\nReturn Date: %s \n\nReturn Private? %s \n\nReturn From: %s\n\nReturn To: %s \n\nReturn Transport Type: %s \n\nReturn Extra items: %s \n\nTotal Price: %s", 'transfers');
  107. $message = sprintf($message, $first_name, $last_name, $email, $phone, $address, $town, $zip, $state, $country, $people_count, $departure_date, ($departure_is_private ? esc_html__('Yes', 'transfers') : esc_html__('No', 'transfers')), $departure_destination_from, $departure_destination_to, $departure_transport_type, $departure_extra_items_string, $return_date, ($return_is_private ? esc_html__('Yes', 'transfers') : esc_html__('No', 'transfers')), $return_destination_from, $return_destination_to, $return_transport_type, $return_extra_items_string, $formatted_both_legs_price);
  108. } else {
  109. $message = esc_html__("New transfer booking: \n\nFirst name: %s \n\nLast name: %s \n\nEmail: %s \n\nPhone: %s \n\nAddress: %s \n\nTown: %s \n\nZip: %s \n\nState: %s \n\nCountry: %s \n\nPeople: %d \n\nDeparture Date: %s \n\nDeparture Private? %s \n\nDeparture From: %s \n\nDeparture To: %s \n\nDeparture Transport type: %s \n\nDeparture Extra items: %s \n\nTotal Price: %s", 'transfers');
  110. $message = sprintf($message, $first_name, $last_name, $email, $phone, $address, $town, $zip, $state, $country, $people_count, $departure_date, ($departure_is_private ? esc_html__('Yes', 'transfers') : esc_html__('No', 'transfers')), $departure_destination_from, $departure_destination_to, $departure_transport_type, $departure_extra_items_string, $formatted_both_legs_price);
  111. }
  112. echo esc_html($departure_booking_id);
  113. $emails = array();
  114. $emails[] = $email;
  115. $emails = apply_filters('transfers_book_transfer_emails', $emails);
  116. foreach ($emails as $e) {
  117. if (!empty($e)) {
  118. wp_mail($e, $subject, $message, $headers);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. die();
  125. }
  126. function number_format_i18n_request() {
  127. if ( isset($_REQUEST) ) {
  128. $nonce = $_REQUEST['nonce'];
  129. if ( wp_verify_nonce( $nonce, 'transfers-ajax-nonce' ) ) {
  130. global $transfers_plugin_globals;
  131. $price_decimal_places = $transfers_plugin_globals->get_price_decimal_places();
  132. $number = floatval(wp_kses($_REQUEST['number'], ''));
  133. echo number_format_i18n( $number, $price_decimal_places );
  134. }
  135. }
  136. // Always die in functions echoing ajax content
  137. die();
  138. }
  139. }
  140. // store the instance in a variable to be retrieved later and call init
  141. $transfers_plugin_ajax = Transfers_Plugin_Ajax::get_instance();
  142. $transfers_plugin_ajax->init();