| @@ -15,8 +15,11 @@ | ||
| 15 | 15 | * @since 2.8 |
| 16 | 16 | */ |
| 17 | 17 | public function init() { |
| 18 | 18 | add_filter( 'option_duplicate_post_taxonomies_blacklist', array( $this, 'taxonomies_blacklist' ) ); |
| 19 | + add_filter( 'pll_copy_post_metas', array( $this, 'exclude_post_metas' ) ); | |
| 20 | + | |
| 21 | + add_action( 'duplicate_post_after_rewriting', array( $this, 'after_rewriting' ), 20, 2 ); | |
| 19 | 22 | } |
| 20 | 23 | |
| 21 | 24 | /** |
| 22 | 25 | * Avoid duplicating the 'post_translations' taxonomy. |
| @@ -32,6 +35,44 @@ | ||
| 32 | 35 | } |
| 33 | 36 | |
| 34 | 37 | $taxonomies[] = 'post_translations'; |
| 35 | 38 | return $taxonomies; |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Exclude Duplicate Post metas from the copy/synchronization. | |
| 43 | + * | |
| 44 | + * This avoids synchronized posts to be deleted when using "Rewrite & Republish". | |
| 45 | + * | |
| 46 | + * @since 3.9 | |
| 47 | + * | |
| 48 | + * @param string[] $keys List of meta keys. | |
| 49 | + * @return string[] | |
| 50 | + */ | |
| 51 | + public function exclude_post_metas( $keys ): array { | |
| 52 | + $to_remove = array( | |
| 53 | + '_dp_original', | |
| 54 | + '_dp_is_rewrite_republish_copy', | |
| 55 | + '_dp_has_rewrite_republish_copy', | |
| 56 | + '_dp_creation_date_gmt', | |
| 57 | + ); | |
| 58 | + | |
| 59 | + return array_diff( $keys, $to_remove ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Fixes the translations group just after a post is republished. | |
| 64 | + * | |
| 65 | + * @since 3.9 | |
| 66 | + * | |
| 67 | + * @param int $copy_id The copy's ID. | |
| 68 | + * @param int $post_id The original post's ID. | |
| 69 | + * @return void | |
| 70 | + */ | |
| 71 | + public function after_rewriting( $copy_id, $post_id ): void { | |
| 72 | + $language = pll_get_post_language( $post_id ); | |
| 73 | + $translations = pll_get_post_translations( $post_id ); | |
| 74 | + | |
| 75 | + $translations[ $language ] = $post_id; | |
| 76 | + pll_save_post_translations( $translations ); | |
| 36 | 77 | } |
| 37 | 78 | } |