説明なし

duplicate-post-wpml.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * WPML compatibility functions
  4. *
  5. * @global array $duplicated_posts Array to store the posts being duplicated.
  6. *
  7. * @package Duplicate Post
  8. * @since 3.2
  9. */
  10. add_action( 'admin_init', 'duplicate_post_wpml_init' );
  11. /**
  12. * Add handlers for WPML compatibility.
  13. */
  14. function duplicate_post_wpml_init() {
  15. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
  16. add_action( 'dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10, 3 );
  17. add_action( 'dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10, 3 );
  18. add_action( 'shutdown', 'duplicate_wpml_string_packages', 11 );
  19. }
  20. }
  21. global $duplicated_posts; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  22. $duplicated_posts = array(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  23. /**
  24. * Copy post translations.
  25. *
  26. * @global SitePress $sitepress Instance of the Main WPML class.
  27. * @global array $duplicated_posts Array of duplicated posts.
  28. *
  29. * @param int $post_id ID of the copy.
  30. * @param WP_Post $post Original post object.
  31. * @param string $status Status of the new post.
  32. */
  33. function duplicate_post_wpml_copy_translations( $post_id, $post, $status = '' ) {
  34. global $sitepress;
  35. global $duplicated_posts;
  36. remove_action( 'dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10 );
  37. remove_action( 'dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10 );
  38. $current_language = $sitepress->get_current_language();
  39. $trid = $sitepress->get_element_trid( $post->ID );
  40. if ( ! empty( $trid ) ) {
  41. $translations = $sitepress->get_element_translations( $trid );
  42. $new_trid = $sitepress->get_element_trid( $post_id );
  43. foreach ( $translations as $code => $details ) {
  44. if ( $code !== $current_language ) {
  45. if ( $details->element_id ) {
  46. $translation = get_post( $details->element_id );
  47. if ( ! $translation ) {
  48. continue;
  49. }
  50. $new_post_id = duplicate_post_create_duplicate( $translation, $status );
  51. if ( ! is_wp_error( $new_post_id ) ) {
  52. $sitepress->set_element_language_details(
  53. $new_post_id,
  54. 'post_' . $translation->post_type,
  55. $new_trid,
  56. $code,
  57. $current_language
  58. );
  59. }
  60. }
  61. }
  62. }
  63. $duplicated_posts[ $post->ID ] = $post_id; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  64. }
  65. }
  66. /**
  67. * Duplicate string packages.
  68. *
  69. * @global array() $duplicated_posts Array of duplicated posts.
  70. */
  71. function duplicate_wpml_string_packages() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  72. global $duplicated_posts;
  73. foreach ( $duplicated_posts as $original_post_id => $duplicate_post_id ) {
  74. $original_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $original_post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  75. $new_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $duplicate_post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  76. if ( is_array( $original_string_packages ) ) {
  77. foreach ( $original_string_packages as $original_string_package ) {
  78. $translated_original_strings = $original_string_package->get_translated_strings( array() );
  79. foreach ( $new_string_packages as $new_string_package ) {
  80. $cache = new WPML_WP_Cache( 'WPML_Package' );
  81. $cache->flush_group_cache();
  82. $new_strings = $new_string_package->get_package_strings();
  83. foreach ( $new_strings as $new_string ) {
  84. if ( isset( $translated_original_strings[ $new_string->name ] ) ) {
  85. foreach ( $translated_original_strings[ $new_string->name ] as $language => $translated_string ) {
  86. do_action(
  87. 'wpml_add_string_translation', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
  88. $new_string->id,
  89. $language,
  90. $translated_string['value'],
  91. $translated_string['status']
  92. );
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }