PluginProbe
Polylang / 2.5.2
Polylang v2.5.2
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 / modules / sync / sync.php

sync.php in Polylang 2.5.2, at modules/sync/sync.php

178 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages copy and synchronization of terms and post metas on front
5 *
6 * @since 2.4
7 */
8 class PLL_Sync {
9 public $taxonomies, $post_metas, $term_meta;
10
11 /**
12 * Constructor
13 *
14 * @since 1.2
15 *
16 * @param object $polylang
17 */
18 public function __construct( &$polylang ) {
19 $this->model = &$polylang->model;
20 $this->options = &$polylang->options;
21
22 $this->taxonomies = new PLL_Sync_Tax( $polylang );
23 $this->post_metas = new PLL_Sync_Post_Metas( $polylang );
24 $this->term_metas = new PLL_Sync_Term_Metas( $polylang );
25
26 add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 10, 3 );
27 add_action( 'created_term', array( $this, 'sync_term_parent' ), 10, 3 );
28 add_action( 'edited_term', array( $this, 'sync_term_parent' ), 10, 3 );
29
30 add_action( 'pll_duplicate_term', array( $this->term_metas, 'copy' ), 10, 3 );
31
32 if ( $this->options['media_support'] ) {
33 add_action( 'pll_translate_media', array( $this->taxonomies, 'copy' ), 10, 3 );
34 add_action( 'pll_translate_media', array( $this->post_metas, 'copy' ), 10, 3 );
35 add_action( 'edit_attachment', array( $this, 'edit_attachment' ) );
36 }
37
38 add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 );
39 }
40
41 /**
42 * Get post fields to synchornize
43 *
44 * @since 2.4
45 *
46 * @param object $post Post object
47 * @return array
48 */
49 protected function get_fields_to_sync( $post ) {
50 $postarr = array();
51
52 foreach ( array( 'comment_status', 'ping_status', 'menu_order' ) as $property ) {
53 if ( in_array( $property, $this->options['sync'] ) ) {
54 $postarr[ $property ] = $post->$property;
55 }
56 }
57
58 if ( in_array( 'post_date', $this->options['sync'] ) ) {
59 $postarr['post_date'] = $post->post_date;
60 $postarr['post_date_gmt'] = $post->post_date_gmt;
61 }
62
63 if ( in_array( 'post_parent', $this->options['sync'] ) ) {
64 $postarr['post_parent'] = wp_get_post_parent_id( $post->ID );
65 }
66
67 return $postarr;
68 }
69
70 /**
71 * Synchronizes post fields in translations
72 *
73 * @since 2.4
74 *
75 * @param int $post_id post id
76 * @param object $post post object
77 * @param array $translations post translations
78 */
79 public function pll_save_post( $post_id, $post, $translations ) {
80 global $wpdb;
81
82 $postarr = $this->get_fields_to_sync( $post );
83
84 if ( ! empty( $postarr ) ) {
85 foreach ( $translations as $lang => $tr_id ) {
86 if ( ! $tr_id || $tr_id === $post_id ) {
87 continue;
88 }
89
90 $tr_arr = $postarr;
91 unset( $tr_arr['post_parent'] );
92
93 // Do not udpate the translation parent if the user set a parent with no translation
94 if ( isset( $postarr['post_parent'] ) ) {
95 $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0;
96 if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) {
97 $tr_arr['post_parent'] = $post_parent;
98 }
99 }
100
101 // Update all the row at once
102 // Don't use wp_update_post to avoid infinite loop
103 $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) );
104 clean_post_cache( $tr_id );
105 }
106 }
107 }
108
109 /**
110 * Synchronize term parent in translations
111 * Calling clean_term_cache *after* this is mandatory otherwise the $taxonomy_children option is not correctly updated
112 *
113 * @since 2.3
114 *
115 * @param int $term_id Term id.
116 * @param int $tt_id Term taxonomy id, not used.
117 * @param string $taxonomy Taxonomy name.
118 */
119 public function sync_term_parent( $term_id, $tt_id, $taxonomy ) {
120 global $wpdb;
121
122 if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) {
123 $term = get_term( $term_id );
124 $translations = $this->model->term->get_translations( $term_id );
125
126 foreach ( $translations as $lang => $tr_id ) {
127 if ( ! empty( $tr_id ) && $tr_id !== $term_id && $tr_parent = $this->model->term->get_translation( $term->parent, $lang ) ) {
128 $wpdb->update(
129 $wpdb->term_taxonomy,
130 array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ),
131 array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id )
132 );
133
134 clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9
135 }
136 }
137 }
138 }
139
140 /**
141 * Synchronizes terms and metas in translations for media
142 *
143 * @since 1.8
144 *
145 * @param int $post_id post id
146 */
147 public function edit_attachment( $post_id ) {
148 $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) );
149 }
150
151 /**
152 * Synchronize sticky posts
153 *
154 * @since 2.3
155 *
156 * @param array $value New option value
157 * @param array $old_value Old option value
158 * @return array
159 */
160 public function sync_sticky_posts( $value, $old_value ) {
161 if ( in_array( 'sticky_posts', $this->options['sync'] ) ) {
162 // Stick post
163 if ( $sticked = array_diff( $value, $old_value ) ) {
164 $translations = $this->model->post->get_translations( reset( $sticked ) );
165 $value = array_unique( array_merge( $value, array_values( $translations ) ) );
166 }
167
168 // Unstick post
169 if ( $unsticked = array_diff( $old_value, $value ) ) {
170 $translations = $this->model->post->get_translations( reset( $unsticked ) );
171 $value = array_unique( array_diff( $value, array_values( $translations ) ) );
172 }
173 }
174
175 return $value;
176 }
177 }
178