PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / integrations / duplicate-post / duplicate-post.php

duplicate-post.php in Polylang trunk, at src/integrations/duplicate-post/duplicate-post.php

79 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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