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