WCML_The_Events_Calendar

The WordPress Core WCML The Events Calendar class.

Defined (1)

The class is defined in the following location(s).

/compatibility/class-wcml-the-events-calendar.php  
  1. class WCML_The_Events_Calendar{ 
  2.  
  3. /** @var SitePress */ 
  4. private $sitepress; 
  5. /** @var woocommerce_wpml */ 
  6. private $woocommerce_wpml; 
  7.  
  8. /** @var WPML_Element_Translation_Package */ 
  9. private $tp; 
  10.  
  11. function __construct( $sitepress, $woocommerce_wpml ) { 
  12.  
  13. $this->sitepress =& $sitepress; 
  14. $this->woocommerce_wpml =& $woocommerce_wpml; 
  15.  
  16. if( isset( $_POST['action'] ) && strpos( $_POST['action'], 'tribe-ticket-add-') === 0 ) { 
  17. add_action( 'tribe_tickets_ticket_add', array( $this, 'unset_post_post_id' ) ); 
  18. add_action( 'event_tickets_after_save_ticket', array( $this, 'restore_post_post_id' ) ); 
  19.  
  20.  
  21. if( is_admin() ) { 
  22. $this->tp = new WPML_Element_Translation_Package; 
  23.  
  24. add_action( 'save_post', array( $this, 'synchronize_event_for_ticket' ), 20, 3 ); 
  25.  
  26. add_action( 'wpml_pb_shortcode_content_for_translation' , array( $this, 'maybe_mark_event_as_needs_update' ), 100, 2 ); 
  27.  
  28. add_filter( 'wpml_tm_translation_job_data', array( $this, 'append_RSVP_tickets_to_translation_job' ), 10, 2 ); 
  29. add_action( 'wpml_pro_translation_completed', array( $this, 'save_RSVP_tickets_translations' ), 10, 3 ); 
  30.  
  31. add_filter( 'wpml_tm_translation_job_data', array( $this, 'append_woo_tickets_to_translation_job' ), 10, 2 ); 
  32. add_action( 'wpml_pro_translation_completed', array( $this, 'save_woo_tickets_translations' ), 10, 3 ); 
  33.  
  34. add_action( 'save_post', array( $this, 'synchronize_venue_for_event' ), 20, 3 ); 
  35. add_action( 'admin_footer', array( $this, 'pre_select_translated_venue' ), 1000 ); 
  36. add_action( 'wpml_pro_translation_completed', array( $this, 'save_venue_for_translation' ), 10, 3 ); 
  37.  
  38. } else { 
  39. add_action( 'event_tickets_rsvp_tickets_generated', array( $this, 'sync_rsvp_fields_on_attendee_created' ), 10, 3 ); 
  40. add_filter( 'tribe_get_organizer_ids', array( $this, 'get_translated_organizer_ids' ), 10, 2 ); 
  41.  
  42.  
  43. public function unset_post_post_id() { 
  44. if( isset( $_POST['post_ID'] ) ) { 
  45. $this->ticket_post_id_backup = $_POST['post_ID']; 
  46. unset( $_POST['post_ID'] ); 
  47.  
  48. public function restore_post_post_id() { 
  49. if( isset( $this->ticket_post_id_backup ) ) { 
  50. $_POST['post_ID'] = $this->ticket_post_id_backup; 
  51.  
  52. public function synchronize_event_for_ticket( $post_id, $post, $update ) { 
  53.  
  54. if( $post->post_type == 'product' ) { 
  55.  
  56. if( !$this->woocommerce_wpml->products->is_original_product( $post_id ) ) { 
  57.  
  58. $original_product_id = apply_filters( 'translate_object_id', $post_id, 'product', false, $this->sitepress->get_default_language() ); 
  59. if( $original_product_id ) { 
  60. $original_event_id = get_post_meta( $original_product_id, '_tribe_wooticket_for_event', true ); 
  61. if( $original_event_id ) { 
  62. $event_id = apply_filters( 'translate_object_id', $original_event_id, 'tribe_events', false ); 
  63. if( $event_id ) { 
  64. update_post_meta( $post_id, '_tribe_wooticket_for_event', $event_id ); 
  65.  
  66.  
  67.  
  68. public function maybe_mark_event_as_needs_update( $content, $post_id ) { 
  69. $post = get_post( $post_id ); 
  70.  
  71. if( $post->post_type == 'tribe_events' ) { 
  72.  
  73. if( $post_id == $this->sitepress->get_original_element_id( $post_id, 'post_tribe_events' ) ) { 
  74.  
  75. $tickets = array(); 
  76. $ticket_meta = array(); 
  77.  
  78. if( class_exists('Tribe__Tickets__RSVP') ) { 
  79. $ticket_ids = Tribe__Tickets__RSVP::get_instance()->get_tickets_ids( $post_id ); 
  80. foreach ( $ticket_ids as $ticket_id ) { 
  81. $ticket = Tribe__Tickets__RSVP::get_instance()->get_ticket( $post_id, $ticket_id ); 
  82. $tickets[] = $ticket->name . "|#|" . $ticket->description; 
  83. $ticket_meta[] = get_post_meta( $ticket_id, '_tribe_tickets_meta', true ); 
  84.  
  85. if( class_exists('Tribe__Tickets_Plus__Commerce__WooCommerce__Main') ) { 
  86. $ticket_ids = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_tickets_ids( $post->ID ); 
  87. foreach ( $ticket_ids as $ticket_id ) { 
  88. $ticket = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_ticket( $post_id, $ticket_id ); 
  89. $tickets[] = $ticket->name . "|#|" . $ticket->description; 
  90. $ticket_meta[] = get_post_meta( $ticket_id, '_tribe_tickets_meta', true ); 
  91.  
  92. $content .= md5( serialize( $tickets ) ); 
  93. $content .= md5( serialize( $ticket_meta ) ); 
  94.  
  95.  
  96.  
  97. return $content; 
  98.  
  99. public function append_RSVP_tickets_to_translation_job( $package, $post ) { 
  100.  
  101. if( $post->post_type == 'tribe_events' && class_exists('Tribe__Tickets__RSVP') ) { 
  102.  
  103. $ticket_lang = $this->sitepress->get_language_for_element( $post->ID, 'post_tribe_events' ); 
  104. $this->sitepress->switch_lang( $ticket_lang ); 
  105. $ticket_ids = Tribe__Tickets__RSVP::get_instance()->get_tickets_ids( $post->ID ); 
  106. $this->sitepress->switch_lang(); 
  107.  
  108. if( $ticket_ids ) { 
  109. foreach( $ticket_ids as $ticket_id ) { 
  110.  
  111. $ticket_post = get_post( $ticket_id ); 
  112. $original_ticket_id = $this->sitepress->get_original_element_id( $ticket_id, 'post_tribe_rsvp_tickets' ); 
  113.  
  114. if( !empty( $ticket_post->post_title ) ) { 
  115. $package['contents'][ 'rsvp_tickets_' . $original_ticket_id . '_title' ] = array( 
  116. 'translate' => 1,  
  117. 'data' => $this->tp->encode_field_data( $ticket_post->post_title, 'base64' ),  
  118. 'format' => 'base64',  
  119. ); 
  120. if( !empty( $ticket_post->post_excerpt ) ) { 
  121. $package['contents'][ 'rsvp_tickets_' . $original_ticket_id . '_excerpt' ] = array( 
  122. 'translate' => 1,  
  123. 'data' => $this->tp->encode_field_data( $ticket_post->post_excerpt, 'base64' ),  
  124. 'format' => 'base64',  
  125. ); 
  126.  
  127. // fieldsets 
  128. $package = $this->append_tickets_meta( $package, $ticket_id, $original_ticket_id ); 
  129.  
  130.  
  131.  
  132.  
  133. return $package; 
  134.  
  135. public function save_RSVP_tickets_translations( $post_id, $data, $job ) { 
  136.  
  137. $translations = array(); 
  138.  
  139. foreach ( $data as $key => $value ) { 
  140.  
  141. if ( preg_match( '/rsvp_tickets_([0-9]+)_title/', $key, $matches ) ) { 
  142. $rsvp_post_id = $matches[1]; 
  143. if( $value['finished'] == 'on' ) { 
  144. $translations[ $rsvp_post_id ]['post_title'] = $value['data']; 
  145. } elseif ( preg_match( '/rsvp_tickets_([0-9]+)_excerpt/', $key, $matches ) ) { 
  146. $rsvp_post_id = $matches[1]; 
  147. if( $value['finished'] == 'on' ) { 
  148. $translations[ $rsvp_post_id ]['post_excerpt'] = $value['data']; 
  149.  
  150.  
  151. foreach ( $translations as $rsvp_post_id => $translation ) { 
  152.  
  153. $translated_rsvp_post_id = apply_filters( 'translate_object_id', $rsvp_post_id, 'tribe_rsvp_tickets', false, $job->language_code ); 
  154.  
  155. $postarr = array( 
  156. 'post_type' => 'tribe_rsvp_tickets',  
  157. 'post_status' => 'publish',  
  158. 'post_title' => $translation['post_title'],  
  159. 'post_excerpt' => $translation['post_excerpt'],  
  160. 'post_name' => sanitize_title_with_dashes( $translation['post_title'] ) 
  161. ); 
  162.  
  163. if( $translated_rsvp_post_id && $translated_rsvp_post_id != $rsvp_post_id ) { 
  164. global $wpml_post_translations; 
  165. $postarr['ID'] = $translated_rsvp_post_id; 
  166. remove_action( 'save_post', array( $wpml_post_translations, 'save_post_actions' ), 100 ); 
  167. wp_update_post( $postarr ); 
  168. add_action( 'save_post', array( $wpml_post_translations, 'save_post_actions' ), 100 ); 
  169.  
  170. } else{ 
  171. $translated_rsvp_post_id = wp_insert_post( $postarr ); 
  172. $trid = $this->sitepress->get_element_trid( $rsvp_post_id, 'post_tribe_rsvp_tickets' ); 
  173. $this->sitepress->set_element_language_details( $translated_rsvp_post_id, 'post_tribe_rsvp_tickets', $trid, $job->language_code ); 
  174.  
  175. $event_id = get_post_meta( $rsvp_post_id, '_tribe_rsvp_for_event', true); 
  176.  
  177. $translated_event_id = apply_filters( 'translate_object_id', $event_id, 'tribe_events', false, $job->language_code); 
  178. update_post_meta( $translated_rsvp_post_id, '_tribe_rsvp_for_event', $translated_event_id); 
  179.  
  180. $this->sync_custom_fields( $rsvp_post_id, $translated_rsvp_post_id ); 
  181.  
  182. $this->save_ticket_meta_translations( $rsvp_post_id, $translated_rsvp_post_id ); 
  183.  
  184.  
  185. public function append_woo_tickets_to_translation_job( $package, $post ) { 
  186.  
  187. if( $post->post_type == 'tribe_events' && class_exists('Tribe__Tickets_Plus__Commerce__WooCommerce__Main') ) { 
  188.  
  189. $ticket_lang = $this->sitepress->get_language_for_element( $post->ID, 'post_tribe_events' ); 
  190. $this->sitepress->switch_lang( $ticket_lang ); 
  191. $ticket_ids = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance()->get_tickets_ids( $post->ID ); 
  192. $this->sitepress->switch_lang(); 
  193.  
  194. if( $ticket_ids ) { 
  195. foreach( $ticket_ids as $ticket_id ) { 
  196.  
  197. $ticket_post = get_post( $ticket_id ); 
  198. $original_ticket_id = $this->sitepress->get_original_element_id( $ticket_id, 'post_product' ); 
  199.  
  200. if( !empty( $ticket_post->post_title ) ) { 
  201. $package['contents'][ 'woo_tickets_' . $original_ticket_id . '_title' ] = array( 
  202. 'translate' => 1,  
  203. 'data' => $this->tp->encode_field_data( $ticket_post->post_title, 'base64' ),  
  204. 'format' => 'base64',  
  205. ); 
  206. if( !empty( $ticket_post->post_excerpt ) ) { 
  207. $package['contents'][ 'woo_tickets_' . $original_ticket_id . '_excerpt' ] = array( 
  208. 'translate' => 1,  
  209. 'data' => $this->tp->encode_field_data( $ticket_post->post_excerpt, 'base64' ),  
  210. 'format' => 'base64',  
  211. ); 
  212.  
  213. // fieldsets 
  214. $package = $this->append_tickets_meta( $package, $ticket_id, $original_ticket_id ); 
  215.  
  216.  
  217.  
  218. return $package; 
  219.  
  220. public function save_woo_tickets_translations( $post_id, $data, $job ) { 
  221. global $wpml_post_translations; 
  222.  
  223. $translations = array(); 
  224.  
  225. foreach ( $data as $key => $value ) { 
  226.  
  227. if ( preg_match( '/woo_tickets_([0-9]+)_title/', $key, $matches ) ) { 
  228. $ticket_post_id = $matches[1]; 
  229. if( $value['finished'] == 'on' ) { 
  230. $translations[ $ticket_post_id ]['post_title'] = $value['data']; 
  231. } elseif ( preg_match( '/woo_tickets_([0-9]+)_excerpt/', $key, $matches ) ) { 
  232. $ticket_post_id = $matches[1]; 
  233. if( $value['finished'] == 'on' ) { 
  234. $translations[ $ticket_post_id ]['post_excerpt'] = $value['data']; 
  235.  
  236.  
  237. foreach ( $translations as $ticket_post_id => $translation ) { 
  238.  
  239. $translated_ticket_post_id = apply_filters( 'translate_object_id', $ticket_post_id, 'product', false, $job->language_code ); 
  240.  
  241. $postarr = array( 
  242. 'post_type' => 'product',  
  243. 'post_status' => 'publish',  
  244. 'post_title' => $translation['post_title'],  
  245. 'post_excerpt' => $translation['post_excerpt'],  
  246. 'post_name' => sanitize_title_with_dashes( $translation['post_title'] ) 
  247. ); 
  248.  
  249. remove_action( 'save_post', array( $wpml_post_translations, 'save_post_actions' ), 100, 2 ); 
  250. if( $translated_ticket_post_id && $translated_ticket_post_id != $ticket_post_id ) { 
  251. $postarr['ID'] = $translated_ticket_post_id; 
  252. wp_update_post( $postarr ); 
  253. } else{ 
  254. $translated_ticket_post_id = wp_insert_post( $postarr ); 
  255. $trid = $this->sitepress->get_element_trid( $ticket_post_id, 'post_product' ); 
  256. $this->sitepress->set_element_language_details( $translated_ticket_post_id, 'post_product', $trid, $job->language_code ); 
  257. add_action( 'save_post', array( $wpml_post_translations, 'save_post_actions' ), 100, 2 ); 
  258.  
  259. $event_id = get_post_meta( $ticket_post_id, '_tribe_wooticket_for_event', true); 
  260.  
  261. $translated_event_id = apply_filters( 'translate_object_id', $event_id, 'tribe_events', false, $job->language_code); 
  262. update_post_meta( $translated_ticket_post_id, '_tribe_wooticket_for_event', $translated_event_id); 
  263.  
  264. $this->sync_custom_fields( $ticket_post_id, $translated_ticket_post_id ); 
  265.  
  266. $this->save_ticket_meta_translations( $ticket_post_id, $translated_ticket_post_id ); 
  267.  
  268.  
  269. private function append_tickets_meta( $package, $ticket_id, $original_ticket_id ) { 
  270.  
  271. $ticket_meta = get_post_meta( $ticket_id, '_tribe_tickets_meta', true ); 
  272. if( is_array( $ticket_meta ) ) { 
  273. foreach ( $ticket_meta as $k => $meta ) { 
  274. $package['contents'][ 'rsvp_tickets_' . $original_ticket_id . '_meta_' . $k . '_label' ] = array( 
  275. 'translate' => 1,  
  276. 'data' => $this->tp->encode_field_data( $meta['label'], 'base64' ),  
  277. 'format' => 'base64',  
  278. ); 
  279. $package['contents'][ 'rsvp_tickets_' . $original_ticket_id . '_meta_' . $k . '_slug' ] = array( 
  280. 'translate' => 1,  
  281. 'data' => $this->tp->encode_field_data( $meta['slug'], 'base64' ),  
  282. 'format' => 'base64',  
  283. ); 
  284. if ( isset( $meta['extra']['options'] ) ) { 
  285. foreach ( $meta['extra']['options'] as $option_id => $option_name ) { 
  286.  
  287. $package['contents'][ 'rsvp_tickets_' . $original_ticket_id . '_meta_' . $k . '_option_' . $option_id ] = array( 
  288. 'translate' => 1,  
  289. 'data' => $this->tp->encode_field_data( $option_name, 'base64' ),  
  290. 'format' => 'base64',  
  291. ); 
  292.  
  293.  
  294.  
  295. return $package; 
  296.  
  297. private function save_ticket_meta_translations( $ticket_id, $translated_ticket_id ) { 
  298.  
  299. $ticket_meta = get_post_meta( $ticket_id, '_tribe_tickets_meta', true ); 
  300. $translated_ticket_meta = $ticket_meta; 
  301. if( is_array( $ticket_meta ) ) { 
  302.  
  303. foreach ( $ticket_meta as $k => $meta ) { 
  304.  
  305. $key = 'rsvp_tickets_' . $ticket_id . '_meta_' . $k . '_label'; 
  306. if( isset( $data[$key] ) && $data[$key]['finished'] == 'on' ) { 
  307. $translated_ticket_meta[$k]['label'] = $data[$key]['data']; 
  308.  
  309. $key = 'rsvp_tickets_' . $ticket_id . '_meta_' . $k . '_slug'; 
  310. if( isset( $data[$key] ) && $data[$key]['finished'] == 'on' ) { 
  311. $translated_ticket_meta[$k]['slug'] = $data[$key]['data']; 
  312.  
  313. if ( isset( $meta['extra']['options'] ) ) { 
  314. foreach ( $meta['extra']['options'] as $option_id => $option_name ) { 
  315. $key = 'rsvp_tickets_' . $ticket_id . '_meta_' . $k . '_option_' . $option_id; 
  316. if( isset( $data[$key] ) && $data[$key]['finished'] == 'on' ) { 
  317. $translated_ticket_meta[$k]['extra']['options'][$option_id] = $data[$key]['data']; 
  318.  
  319.  
  320.  
  321. update_post_meta( $translated_ticket_id, '_tribe_tickets_meta', $translated_ticket_meta ); 
  322.  
  323. update_post_meta( $translated_ticket_id, '_tribe_tickets_meta_enabled',  
  324. get_post_meta( $ticket_id, '_tribe_tickets_meta_enabled', true ) 
  325. ); 
  326.  
  327.  
  328.  
  329. private function sync_custom_fields( $original_ticket_id, $translated_ticket_id ) { 
  330. // sync custom fields 
  331. $custom_fields_sync = array( '_stock', '_manage_stock', 'total_sales', '_price' ); 
  332. foreach( $custom_fields_sync as $custom_field ) { 
  333. $value = get_post_meta( $original_ticket_id, $custom_field, true ); 
  334. if( $value !== false ) { 
  335. update_post_meta( $translated_ticket_id, $custom_field, $value); 
  336.  
  337. public function synchronize_venue_for_event( $post_id, $post, $update ) { 
  338.  
  339. if( $post->post_type == 'tribe_event' ) { 
  340.  
  341. $venue_id = get_post_meta( $post_id, '_EventVenueID', true ); 
  342.  
  343. $original_event_id = $this->sitepress->get_original_element_id( $post_id, 'post_tribe_event' ); 
  344.  
  345. if( $original_event_id == $post_id ) { 
  346. $event_trid = $this->sitepress->get_element_trid( $post_id, 'post_tribe_venue' ); 
  347. $event_translations = $this->sitepress->get_element_translations( $event_trid, 'post_tribe_venue' ); 
  348.  
  349. if ( $venue_id ) { 
  350. foreach ( $event_translations as $language_code => $event_translation ) { 
  351. if ( $event_translation->element_id != $original_event_id ) { 
  352. $translated_venue_id = apply_filters( 'translate_object_id', $venue_id, 'tribe_venue', false, $language_code ); 
  353. if ( $translated_venue_id ) { 
  354. update_post_meta( $event_translation->element_id, '_EventVenueID', $translated_venue_id ); 
  355. } else { // delete venue from translations 
  356. foreach ( $event_translations as $language_code => $event_translation ) { 
  357. if ( $event_translation->element_id != $original_event_id ) { 
  358. delete_post_meta( $event_translation->element_id, '_EventVenueID' ); 
  359.  
  360.  
  361.  
  362.  
  363. public function pre_select_translated_venue() { 
  364. $current_screen = get_current_screen(); 
  365. if( $current_screen->base === 'post' && $current_screen->action === 'add' && $current_screen->id === 'tribe_events' ) { 
  366. if( isset( $_GET['trid'] ) && isset( $_GET['lang'] ) && isset( $_GET['source_lang'] ) ) { 
  367. $event_translations = $this->sitepress->get_element_translations( $_GET['trid'], 'post_tribe_events' ); 
  368. $original_event_id = $event_translations[ $_GET['source_lang'] ]->element_id; 
  369. $original_venue_id = get_post_meta( $original_event_id, '_EventVenueID', true ); 
  370. if( $original_venue_id ) { 
  371. $translated_venue_id = apply_filters( 'translate_object_id', $original_venue_id, 'tribe_venue', false, $_GET['lang'] ); 
  372. if( $translated_venue_id ) { 
  373. echo "<script type=\"text/javascript\"> 
  374. jQuery('#saved_tribe_venue').val($translated_venue_id); 
  375. </script>"; 
  376.  
  377. public function save_venue_for_translation( $post_id, $data, $job ) { 
  378.  
  379. $original_event_id = $this->sitepress->get_original_element_id( $post_id, 'post_tribe_event' ); 
  380. $original_venue_id = get_post_meta( $original_event_id, '_EventVenueID', true ); 
  381.  
  382. if( $original_venue_id ) { 
  383. $translated_venue_id = apply_filters( 'translate_object_id', $original_venue_id, 'tribe_venue', false, $job->language_code ); 
  384. if( $translated_venue_id ) { 
  385. update_post_meta( $post_id, '_EventVenueID', $translated_venue_id ); 
  386.  
  387.  
  388.  
  389. public function sync_rsvp_fields_on_attendee_created( $order_id, $post_id, $attendee_order_status ) { 
  390.  
  391. $rsvp_post_id = isset( $_POST['product_id'][0] ) ? $_POST['product_id'][0] : false; 
  392. if( $rsvp_post_id ) { 
  393. $rsvp_trid = $this->sitepress->get_element_trid( $rsvp_post_id, 'post_tribe_rsvp_tickets' ); 
  394. $rsvp_translations = $this->sitepress->get_element_translations( $rsvp_trid, 'post_tribe_rsvp_tickets' ); 
  395.  
  396. foreach ( $rsvp_translations as $translation ) { 
  397. if ( $translation->element_id != $rsvp_post_id ) { 
  398. $this->sync_custom_fields( $rsvp_post_id, $translation->element_id ); 
  399.  
  400. public function get_translated_organizer_ids( $organizer_ids, $event_id ) { 
  401. foreach( $organizer_ids as $key => $organizer_id ) { 
  402. $organizer_ids[$key] = apply_filters( 'translate_object_id', $organizer_id, 'tribe_organizer', true ); 
  403. return $organizer_ids;