Advertisement
dgwatkins

duplicate-post-wpml.php

Oct 26th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1. <?php
  2. add_action( 'admin_init', 'duplicate_post_wpml_init' );
  3.  
  4. function duplicate_post_wpml_init() {
  5.     if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
  6.         add_action('dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10, 3);
  7.         add_action('dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10, 3);
  8.         add_action('shutdown', 'duplicate_wpml_string_packages', 11 );
  9.     }
  10. }
  11.  
  12. global $duplicated_posts;
  13. $duplicated_posts= array();
  14.  
  15. function duplicate_post_wpml_copy_translations($post_id, $post, $status = '') {
  16.     global $sitepress;
  17.     global $duplicated_posts;
  18.  
  19.     remove_action('dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10);
  20.     remove_action('dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10);
  21.  
  22.     $current_language = $sitepress->get_current_language();
  23.     $trid = $sitepress->get_element_trid($post->ID);
  24.     if (!empty($trid)) {
  25.         $translations = $sitepress->get_element_translations($trid);
  26.         $new_trid = $sitepress->get_element_trid($post_id);
  27.         foreach ($translations as $code => $details) {
  28.             if ($code != $current_language) {
  29.                 if ( $details->element_id ) {
  30.                     $translation = get_post( $details->element_id );
  31.                     $new_post_id = duplicate_post_create_duplicate( $translation, $status );
  32.                     $sitepress->set_element_language_details( $new_post_id, 'post_' . $translation->post_type, $new_trid, $code, $current_language );
  33.                 }
  34.             }
  35.         }
  36.         $duplicated_posts[ $post->ID ] = $post_id;
  37.     }
  38. }
  39.  
  40. function duplicate_wpml_string_packages() {
  41.     global $duplicated_posts;
  42.  
  43.     foreach ( $duplicated_posts as $original_post_id => $duplicate_post_id ) {
  44.  
  45.         $original_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $original_post_id );
  46.         $new_string_packages      = apply_filters( 'wpml_st_get_post_string_packages', false, $duplicate_post_id );
  47.         if ( is_array( $original_string_packages ) ) {
  48.             foreach ( $original_string_packages as $original_string_package ) {
  49.                 $translated_original_strings = $original_string_package->get_translated_strings( array() );
  50.  
  51.                 foreach ( $new_string_packages as $new_string_package ) {
  52.                     $cache = new WPML_WP_Cache( 'WPML_Package' );
  53.                     $cache->flush_group_cache();
  54.                     $new_strings = $new_string_package->get_package_strings();
  55.                     foreach ( $new_strings as $new_string ) {
  56.  
  57.                         if ( isset( $translated_original_strings[ $new_string->name ] ) ) {
  58.                             foreach ( $translated_original_strings[ $new_string->name ] as $language => $translated_string ) {
  59.  
  60.                                 do_action(
  61.                                     'wpml_add_string_translation',
  62.                                     $new_string->id,
  63.                                     $language,
  64.                                     $translated_string['value'],
  65.                                     $translated_string['status']
  66.                                 );
  67.  
  68.                             }
  69.                         }
  70.  
  71.                     }
  72.  
  73.                 }
  74.  
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement