Advertisement
dgwatkins

em-wpml-event-recurring.php

Jul 31st, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2.  
  3. add_filter( 'em_event_save_events', 'em_wpml_connect_recurring_translations', 10, 4 );
  4.  
  5. function em_wpml_connect_recurring_translations( $result, $event, $event_ids, $post_ids ) {
  6.     global $wpdb;
  7.  
  8.     // Only do this for successfully saved recurring events.
  9.     if ( $result && $event->recurrence ) {
  10.  
  11.         // Get the original post for this recurring event.
  12.         $trid = apply_filters( 'wpml_element_trid', false, $event->post_id, 'post_event-recurring' );
  13.         $translations = apply_filters( 'wpml_get_element_translations', false, $trid, 'post_event-recurring' );
  14.         $original = wp_list_filter( $translations, array( 'original' => 1 ) );
  15.         $original = reset( $original );
  16.  
  17.         // We dont need to do anything to originals.
  18.         if ( empty( $original ) || $original->element_id == $event->post_id ) {
  19.             return;
  20.         }
  21.  
  22.         // We are saving a translation, lets connect them to their originals.
  23.         $original->event_id = get_post_meta( $original->element_id, '_event_id', true );
  24.         $originals = $wpdb->get_col( $wpdb->prepare(
  25.             "SELECT post_id FROM {$wpdb->prefix}em_events WHERE recurrence_id = %d",
  26.             $original->event_id
  27.         ) );
  28.         $language = apply_filters( 'wpml_element_language_code', null, array(
  29.             'element_id' => $event->post_id,
  30.             'element_type' => 'post_event-recurring'
  31.         ) );
  32.         foreach ( $post_ids as $i => $post_id ) {
  33.             $trid = apply_filters( 'wpml_element_trid', false, $originals[ $i ], 'post_event' );
  34.             $args = array(
  35.                 'element_id'    => $post_id,
  36.                 'element_type'  => 'post_event',
  37.                 'trid'   => $trid,
  38.                 'language_code'   => $language,
  39.                 'source_language_code' => $original->language_code
  40.             );
  41.             do_action( 'wpml_set_element_language_details', $args );
  42.         }
  43.     }
  44.  
  45.     return $result;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement