• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / duplicate-post / 21609297401

02 Feb 2026 10:57PM UTC coverage: 57.839% (+0.2%) from 57.637%
21609297401

push

github

web-flow
Merge pull request #451 from Yoast/enrico/adds-republish-hooks

Add action hooks before and after republishing.

2 of 2 new or added lines in 1 file covered. (100.0%)

90 existing lines in 2 files now uncovered.

1520 of 2628 relevant lines covered (57.84%)

7.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/compat/wpml-functions.php
1
<?php
2
/**
3
 * WPML compatibility functions
4
 *
5
 * @global array $duplicated_posts Array to store the posts being duplicated.
6
 *
7
 * @package Yoast\WP\Duplicate_Post
8
 * @since   3.2
9
 */
10

11
add_action( 'admin_init', 'duplicate_post_wpml_init' );
12

13
/**
14
 * Add handlers for WPML compatibility.
15
 *
16
 * @return void
17
 */
18
function duplicate_post_wpml_init() {
19
        if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
×
20
                add_action( 'duplicate_post_after_duplicated', 'duplicate_post_wpml_copy_translations', 10, 3 );
×
21
                add_action( 'shutdown', 'duplicate_wpml_string_packages', 11 );
×
22
        }
23
}
24

25
global $duplicated_posts;
26

27
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: Renaming a global variable is a BC break.
28
$duplicated_posts = [];
29

30
/**
31
 * Copy post translations.
32
 *
33
 * @global SitePress $sitepress        Instance of the Main WPML class.
34
 * @global array     $duplicated_posts Array of duplicated posts.
35
 *
36
 * @param int     $post_id ID of the copy.
37
 * @param WP_Post $post    Original post object.
38
 * @param string  $status  Status of the new post.
39
 *
40
 * @return void
41
 */
42
function duplicate_post_wpml_copy_translations( $post_id, $post, $status = '' ) {
UNCOV
43
        global $sitepress;
×
44
        global $duplicated_posts;
×
45

UNCOV
46
        remove_action( 'duplicate_post_after_duplicated', 'duplicate_post_wpml_copy_translations', 10 );
×
47

48
        $current_language = $sitepress->get_current_language();
×
UNCOV
49
        $trid             = $sitepress->get_element_trid( $post->ID );
×
50
        if ( ! empty( $trid ) ) {
×
51
                $translations = $sitepress->get_element_translations( $trid );
×
52
                $new_trid     = $sitepress->get_element_trid( $post_id );
×
53
                foreach ( $translations as $code => $details ) {
×
54
                        if ( $code !== $current_language ) {
×
55
                                if ( $details->element_id ) {
×
56
                                        $translation = get_post( $details->element_id );
×
57
                                        if ( ! $translation ) {
×
58
                                                continue;
×
59
                                        }
60
                                        $new_post_id = duplicate_post_create_duplicate( $translation, $status );
×
UNCOV
61
                                        if ( ! is_wp_error( $new_post_id ) ) {
×
62
                                                $sitepress->set_element_language_details(
×
63
                                                        $new_post_id,
×
64
                                                        'post_' . $translation->post_type,
×
65
                                                        $new_trid,
×
66
                                                        $code,
×
67
                                                        $current_language
×
68
                                                );
×
69
                                        }
70
                                }
71
                        }
72
                }
73

74
                // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: see above.
UNCOV
75
                $duplicated_posts[ $post->ID ] = $post_id;
×
76
        }
77
}
78

79
/**
80
 * Duplicate string packages.
81
 *
82
 * @global array() $duplicated_posts Array of duplicated posts.
83
 *
84
 * @return void
85
 */
86
function duplicate_wpml_string_packages() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: renaming the function would be a BC-break.
UNCOV
87
        global $duplicated_posts;
×
88

89
        foreach ( $duplicated_posts as $original_post_id => $duplicate_post_id ) {
×
90
                // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: using WPML native filter.
91
                $original_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $original_post_id );
×
92

93
                // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: using WPML native filter.
UNCOV
94
                $new_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $duplicate_post_id );
×
95

96
                if ( is_array( $original_string_packages ) ) {
×
UNCOV
97
                        foreach ( $original_string_packages as $original_string_package ) {
×
98
                                $translated_original_strings = $original_string_package->get_translated_strings( [] );
×
99

100
                                foreach ( $new_string_packages as $new_string_package ) {
×
UNCOV
101
                                        $cache = new WPML_WP_Cache( 'WPML_Package' );
×
102
                                        $cache->flush_group_cache();
×
103
                                        $new_strings = $new_string_package->get_package_strings();
×
104
                                        foreach ( $new_strings as $new_string ) {
×
105

106
                                                if ( isset( $translated_original_strings[ $new_string->name ] ) ) {
×
UNCOV
107
                                                        foreach ( $translated_original_strings[ $new_string->name ] as $language => $translated_string ) {
×
108

109
                                                                do_action(
×
110
                                                                        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: using WPML native filter.
111
                                                                        'wpml_add_string_translation',
×
UNCOV
112
                                                                        $new_string->id,
×
113
                                                                        $language,
×
114
                                                                        $translated_string['value'],
×
115
                                                                        $translated_string['status']
×
116
                                                                );
×
117
                                                        }
118
                                                }
119
                                        }
120
                                }
121
                        }
122
                }
123
        }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc