class-appointment.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. class Booked_WC_Appointment {
  3. private static $appointments = array();
  4. public $post_id;
  5. public $user_id;
  6. public $order_id = null;
  7. public $timestamp;
  8. public $timeslot;
  9. public $timeslot_text;
  10. public $custom_fields = array();
  11. public $is_paid = false;
  12. public $payment_status;
  13. public $payment_status_text;
  14. public $products = array();
  15. public $post;
  16. public $calendar = null;
  17. public $products_extended = array();
  18. private function __construct( $post_id ) {
  19. $this->post_id = $post_id;
  20. $this->get_data();
  21. $this->get_metas();
  22. $this->get_custom_fields_info();
  23. $this->get_timeslot_text();
  24. $this->get_calendar();
  25. $this->get_products();
  26. $this->get_products_data();
  27. $this->get_order();
  28. $this->get_payment_status();
  29. }
  30. public static function get( $post_id=null ) {
  31. if ( !is_integer($post_id) ) {
  32. $message = sprintf( __('%s integer expected when %s given.', 'booked-woocommerce-payments'), 'Booked_WC_Appointment::get($post_id)', gettype($post_id) );
  33. throw new Exception($message);
  34. } else if ( $post_id===0 ) {
  35. self::$appointments[$post_id] = false;
  36. } else if ( !isset(self::$appointments[$post_id]) ) {
  37. self::$appointments[$post_id] = new self($post_id);
  38. }
  39. return self::$appointments[$post_id];
  40. }
  41. protected function get_data() {
  42. $this->post = get_post($this->post_id);
  43. if ( !$this->post ) {
  44. return false;
  45. }
  46. return $this;
  47. }
  48. protected function get_metas() {
  49. $this->timestamp = get_post_meta($this->post_id, '_appointment_timestamp', true);
  50. $this->timeslot = get_post_meta($this->post_id, '_appointment_timeslot', true);
  51. $this->user_id = get_post_meta($this->post_id, '_appointment_user', true);
  52. return $this;
  53. }
  54. protected function get_custom_fields_info() {
  55. $i = 0;
  56. $meta_exists = true;
  57. $separator = '--SEP--';
  58. $meta_key = '_' . BOOKED_WC_PLUGIN_PREFIX . 'cfield_';
  59. do {
  60. $this_meta_key = $meta_key . strval($i);
  61. $meta_value = get_post_meta($this->post_id, $this_meta_key, true);
  62. if ( empty($meta_value) || !strpos($meta_value, $separator) ) {
  63. $meta_exists = false;
  64. break;
  65. }
  66. $meta_value_parts = explode($separator, $meta_value);
  67. $label = $meta_value_parts[0];
  68. $value = $meta_value_parts[1];
  69. $this->custom_fields[$label] = $value;
  70. $i++;
  71. } while ( $meta_exists );
  72. return $this;
  73. }
  74. protected function get_timeslot_text() {
  75. global $timeslot_saved,$timestamp_saved;
  76. if ( !$this->timeslot && isset($timeslot_saved) && $timeslot_saved ):
  77. $this->timeslot = $timeslot_saved;
  78. endif;
  79. if ( !$this->timestamp && isset($timestamp_saved) && $timestamp_saved ):
  80. $this->timestamp = $timestamp_saved;
  81. endif;
  82. if ( !empty($this->timeslot) ):
  83. $timeslots = explode('-', $this->timeslot);
  84. $time_format = get_option('time_format');
  85. $date_format = get_option('date_format');
  86. $time_start = date_i18n($time_format, strtotime($timeslots[0]));
  87. $time_end = date_i18n($time_format, strtotime($timeslots[1]));
  88. $hide_end_times = get_option('booked_hide_end_times');
  89. if ( $this->timestamp ):
  90. $timestamp_saved = $this->timestamp;
  91. endif;
  92. $day_year = date_i18n($date_format, $timestamp_saved);
  93. if ($timeslots[0] == '0000' && $timeslots[1] == '2400') {
  94. $timeslotText = $day_year . ' (' . esc_html__('All day','booked-woocommerce-payments') . ')';
  95. } else if ( !$hide_end_times ) {
  96. $timeslotText = sprintf(__('from %1$s to %2$s on %3$s', 'booked-woocommerce-payments'), $time_start, $time_end, $day_year);
  97. } else if ( $hide_end_times ) {
  98. $timeslotText = sprintf(__('at %1$s on %2$s', 'booked-woocommerce-payments'), $time_start, $day_year);
  99. } else {
  100. $timeslotText = 'N/A';
  101. }
  102. $this->timeslot_text = $timeslotText;
  103. $timeslot_saved = $this->timeslot;
  104. return $this;
  105. endif;
  106. }
  107. protected function get_calendar() {
  108. $calendars = wp_get_post_terms($this->post_id, BOOKED_WC_TAX_CALENDAR);
  109. if ( !is_wp_error($calendars) && isset($calendars[0]) ) {
  110. $calendar_obj = $calendars[0];
  111. $calendar_page = Booked_WC_Functions::get_calendar_page_info($calendar_obj->term_id);
  112. } else {
  113. $calendar_obj = false;
  114. $calendar_page = Booked_WC_Functions::get_calendar_page_info();
  115. }
  116. $this->calendar = (object) array(
  117. 'calendar_obj' => $calendar_obj,
  118. 'calendar_page' => $calendar_page,
  119. 'calendar_link' => $calendar_page ? get_permalink($calendar_page->ID) : false
  120. );
  121. return $this;
  122. }
  123. protected function get_products() {
  124. $cf_meta = get_post_meta($this->post_id, '_cf_meta_value', true);
  125. if ( !$cf_meta ) {
  126. return $this;
  127. }
  128. $pattern = '~<!--\s([^\s]+)\s-->~mi';
  129. // match all products assigned to that appointment
  130. preg_match_all($pattern, $cf_meta, $products);
  131. if ( !$products ) {
  132. return $this;
  133. }
  134. foreach ($products[1] as $product_string) {
  135. $product_info = explode('|', $product_string);
  136. $product_info_to_add = array();
  137. foreach ($product_info as $product_info_string) {
  138. $info = explode('::', $product_info_string);
  139. $label = $info[0];
  140. $value = $info[1];
  141. $product_info_to_add[$label] = $value;
  142. }
  143. $this->products[] = (object) $product_info_to_add;
  144. }
  145. return $this;
  146. }
  147. protected function get_products_data() {
  148. foreach ($this->products as $product_data) {
  149. $product_id = intval($product_data->product_id);
  150. $data_to_add = array();
  151. $product_obj = wc_get_product($product_id);
  152. $data_to_add['product'] = $product_obj;
  153. if ( isset($product_data->variation_id) ) {
  154. $variation_id = intval($product_data->variation_id);
  155. if (!empty($product_obj->variations[$variation_id])):
  156. $variation_data = $product_obj->variations[$variation_id];
  157. $data_to_add['variation'] = $variation_data;
  158. else:
  159. $data_to_add['variation'] = false;
  160. endif;
  161. } else {
  162. $data_to_add['variation'] = false;
  163. }
  164. $this->products_extended[] = (object) $data_to_add;
  165. }
  166. return $this;
  167. }
  168. protected function get_order() {
  169. $this->order_id = get_post_meta($this->post_id, '_' . BOOKED_WC_PLUGIN_PREFIX . 'appointment_order_id', true);
  170. return $this;
  171. }
  172. protected function get_payment_status() {
  173. $payment_status = new Booked_WC_Appointment_Payment_Status($this->post_id);
  174. $this->is_paid = $payment_status->is_paid;
  175. $this->payment_status = $payment_status->payment_status;
  176. $this->payment_status_text = $payment_status->payment_status_text;
  177. return $this;
  178. }
  179. }