| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages the compatibility with Duplicate Post. |
| 8 |
* |
| 9 |
* @since 2.8 |
| 10 |
*/ |
| 11 |
class PLL_Duplicate_Post { |
| 12 |
/** |
| 13 |
* Setups actions. |
| 14 |
* |
| 15 |
* @since 2.8 |
| 16 |
*/ |
| 17 |
public function init() { |
| 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 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Avoid duplicating the 'post_translations' taxonomy. |
| 26 |
* |
| 27 |
* @since 1.8 |
| 28 |
* |
| 29 |
* @param array|string $taxonomies The list of taxonomies not to duplicate. |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public function taxonomies_blacklist( $taxonomies ) { |
| 33 |
if ( empty( $taxonomies ) ) { |
| 34 |
$taxonomies = array(); // As we get an empty string when there is no taxonomy. |
| 35 |
} |
| 36 |
|
| 37 |
$taxonomies[] = 'post_translations'; |
| 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 ); |
| 77 |
} |
| 78 |
} |
| 79 |
|