class-wp-ajax.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class Booked_WC_Ajax {
  3. private function __construct() {
  4. $functions = array(
  5. 'wp_ajax_' => array(
  6. // 'action' => 'function_name'
  7. 'load_variations' => 'load_product_variations',
  8. 'add_to_cart' => 'add_appointment_to_cart',
  9. 'mark_paid' => 'mark_appointment_as_paid'
  10. ),
  11. 'wp_ajax_nopriv_' => array(
  12. // 'action' => 'function_name
  13. 'load_variations' => 'load_product_variations',
  14. 'add_to_cart' => 'add_appointment_to_cart'
  15. )
  16. );
  17. foreach ($functions as $ajax_type => $requests) {
  18. foreach ($requests as $action_value => $function_name) {
  19. $filter_name = $ajax_type . BOOKED_WC_PLUGIN_PREFIX . $action_value;
  20. add_action($filter_name, array($this, $function_name));
  21. }
  22. }
  23. }
  24. protected function verify_domain() {
  25. $domain = $_SERVER['SERVER_NAME'];
  26. return strstr(home_url('/'), $domain);
  27. }
  28. public static function setup() {
  29. return new self();
  30. }
  31. public function mark_appointment_as_paid() {
  32. if (empty($_POST['appt_id']) || !current_user_can('manage_booked_options')){
  33. return;
  34. }
  35. $appt_id = (int) $_POST['appt_id'];
  36. $appointment = Booked_WC_Appointment::get($appt_id);
  37. if (!$appointment->order_id){
  38. $_oid = false;
  39. update_post_meta($appt_id, '_booked_wc_appointment_order_id', 'manual');
  40. } else {
  41. $_oid = $appointment->order_id;
  42. $order = new WC_Order($appointment->order_id);
  43. $order->update_status('completed');
  44. }
  45. echo ( $_oid ? get_edit_post_link( $_oid ) : 'no_order' );
  46. exit;
  47. }
  48. public function load_product_variations() {
  49. if (
  50. empty($_POST['product_id'])
  51. || !get_post($_POST['product_id'])
  52. ) {
  53. return;
  54. }
  55. $product_id = intval($_POST['product_id']);
  56. $calendar_id = isset($_POST['calendar_id']) ? intval($_POST['calendar_id']) : 0;
  57. $field_name = $_POST['field_name'];
  58. $is_required = false;
  59. if ( $field_name ) {
  60. $field_parts = explode('---',$field_name);
  61. $field_type = $field_parts[0];
  62. $end_of_string = explode('___',$field_parts[1]);
  63. $numbers_only = $end_of_string[0];
  64. $is_required = (isset($end_of_string[1]) ? true : false);
  65. $field_name = 'paid-service-variation---' . $numbers_only;
  66. if ( $is_required ) {
  67. $field_name .= '___' . $end_of_string[1];
  68. }
  69. }
  70. try {
  71. $product = Booked_WC_Product::get($product_id);
  72. $fragment_file = Booked_WC_Fragments::get_path('ajax-loaded/product','variations');
  73. include($fragment_file);
  74. } catch (Exception $e) {
  75. $message = __('An error has occur.', 'booked-woocommerce-payments');
  76. throw new Exception($message);
  77. }
  78. exit;
  79. }
  80. public function add_appointment_to_cart() {
  81. $response = new Booked_WC_Response();
  82. $app_id = (!empty($_POST['app_id']) && $_POST['app_id']) ? intval($_POST['app_id']) : false;
  83. if ( !$app_id ) {
  84. $response->add_message(__('Appointment ID is not defined.', 'booked-woocommerce-payments'));
  85. $response->create();
  86. }
  87. try {
  88. $appointment = Booked_WC_Appointment::get($app_id);
  89. if ( !$appointment->products ) {
  90. $response->add_message($e->getMessage());
  91. $response->create();
  92. }
  93. // add the appointment to cart
  94. Booked_WC_Cart::add_appointment($app_id);
  95. } catch (Exception $e) {
  96. $response->add_message($e->getMessage());
  97. $response->create();
  98. }
  99. $response->add_message(__('Appointment has been added to the cart', 'booked-woocommerce-payments'));
  100. $response->set_status(true);
  101. $response->create();
  102. }
  103. }