| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages copy and synchronization of terms and post metas on front |
| 8 | 5 | * |
| @@ -8,36 +5,11 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 2.4 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Sync { |
| 12 | - /** | |
| 13 | - * @var PLL_Sync_Tax | |
| 14 | - */ | |
| 15 | - public $taxonomies; | |
| 9 | + public $taxonomies, $post_metas, $term_meta; | |
| 16 | 10 | |
| 17 | 11 | /** |
| 18 | - * @var PLL_Sync_Post_Metas | |
| 19 | - */ | |
| 20 | - public $post_metas; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * @var PLL_Sync_Term_Metas | |
| 24 | - */ | |
| 25 | - public $term_metas; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Stores the plugin options. | |
| 29 | - * | |
| 30 | - * @var array | |
| 31 | - */ | |
| 32 | - protected $options; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * @var PLL_Model | |
| 36 | - */ | |
| 37 | - protected $model; | |
| 38 | - | |
| 39 | - /** | |
| 40 | 12 | * Constructor |
| 41 | 13 | * |
| 42 | 14 | * @since 1.2 |
| 43 | 15 | * |
| @@ -69,13 +41,13 @@ | ||
| 69 | 41 | add_filter( 'pre_update_option_sticky_posts', array( $this, 'sync_sticky_posts' ), 10, 2 ); |
| 70 | 42 | } |
| 71 | 43 | |
| 72 | 44 | /** |
| 73 | - * Get post fields to synchronize. | |
| 45 | + * Get post fields to synchornize | |
| 74 | 46 | * |
| 75 | 47 | * @since 2.4 |
| 76 | 48 | * |
| 77 | - * @param WP_Post $post Post object. | |
| 49 | + * @param object $post Post object | |
| 78 | 50 | * @return array |
| 79 | 51 | */ |
| 80 | 52 | protected function get_fields_to_sync( $post ) { |
| 81 | 53 | $postarr = array(); |
| @@ -110,8 +82,9 @@ | ||
| 110 | 82 | */ |
| 111 | 83 | public function can_sync_post_parent( $post_parent, $post_id, $postarr ) { |
| 112 | 84 | if ( ! empty( $postarr['ID'] ) && ! $this->model->post->current_user_can_synchronize( $postarr['ID'] ) ) { |
| 113 | 85 | $tr_ids = $this->model->post->get_translations( $postarr['ID'] ); |
| 86 | + $orig_lang = array_search( $postarr['ID'], $tr_ids ); | |
| 114 | 87 | foreach ( $tr_ids as $tr_id ) { |
| 115 | 88 | if ( $tr_id !== $postarr['ID'] && $post = get_post( $tr_id ) ) { |
| 116 | 89 | $post_parent = $post->post_parent; |
| 117 | 90 | break; |
| @@ -143,16 +116,15 @@ | ||
| 143 | 116 | return $data; |
| 144 | 117 | } |
| 145 | 118 | |
| 146 | 119 | /** |
| 147 | - * Synchronizes post fields in translations. | |
| 120 | + * Synchronizes post fields in translations | |
| 148 | 121 | * |
| 149 | 122 | * @since 2.4 |
| 150 | 123 | * |
| 151 | - * @param int $post_id Post id. | |
| 152 | - * @param WP_Post $post Post object. | |
| 153 | - * @param int[] $translations Post translations. | |
| 154 | - * @return void | |
| 124 | + * @param int $post_id post id | |
| 125 | + * @param object $post post object | |
| 126 | + * @param array $translations post translations | |
| 155 | 127 | */ |
| 156 | 128 | public function pll_save_post( $post_id, $post, $translations ) { |
| 157 | 129 | global $wpdb; |
| 158 | 130 | |
| @@ -167,9 +139,9 @@ | ||
| 167 | 139 | |
| 168 | 140 | $tr_arr = $postarr; |
| 169 | 141 | unset( $tr_arr['post_parent'] ); |
| 170 | 142 | |
| 171 | - // Do not udpate the translation parent if the user set a parent with no translation. | |
| 143 | + // Do not udpate the translation parent if the user set a parent with no translation | |
| 172 | 144 | if ( isset( $postarr['post_parent'] ) ) { |
| 173 | 145 | $post_parent = $postarr['post_parent'] ? $this->model->post->get_translation( $postarr['post_parent'], $lang ) : 0; |
| 174 | 146 | if ( ! ( $postarr['post_parent'] && ! $post_parent ) ) { |
| 175 | 147 | $tr_arr['post_parent'] = $post_parent; |
| @@ -175,14 +147,12 @@ | ||
| 175 | 147 | $tr_arr['post_parent'] = $post_parent; |
| 176 | 148 | } |
| 177 | 149 | } |
| 178 | 150 | |
| 179 | - // Update all the rows at once. | |
| 180 | - if ( ! empty( $tr_arr ) ) { | |
| 181 | - // Don't use wp_update_post to avoid infinite loop. | |
| 182 | - $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) ); | |
| 183 | - clean_post_cache( $tr_id ); | |
| 184 | - } | |
| 151 | + // Update all the row at once | |
| 152 | + // Don't use wp_update_post to avoid infinite loop | |
| 153 | + $wpdb->update( $wpdb->posts, $tr_arr, array( 'ID' => $tr_id ) ); | |
| 154 | + clean_post_cache( $tr_id ); | |
| 185 | 155 | } |
| 186 | 156 | } |
| 187 | 157 | } |
| 188 | 158 | } |
| @@ -195,9 +165,8 @@ | ||
| 195 | 165 | * |
| 196 | 166 | * @param int $term_id Term id. |
| 197 | 167 | * @param int $tt_id Term taxonomy id, not used. |
| 198 | 168 | * @param string $taxonomy Taxonomy name. |
| 199 | - * @return void | |
| 200 | 169 | */ |
| 201 | 170 | public function sync_term_parent( $term_id, $tt_id, $taxonomy ) { |
| 202 | 171 | global $wpdb; |
| 203 | 172 | |
| @@ -202,27 +171,19 @@ | ||
| 202 | 171 | global $wpdb; |
| 203 | 172 | |
| 204 | 173 | if ( is_taxonomy_hierarchical( $taxonomy ) && $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| 205 | 174 | $term = get_term( $term_id ); |
| 175 | + $translations = $this->model->term->get_translations( $term_id ); | |
| 206 | 176 | |
| 207 | - if ( $term instanceof WP_Term ) { | |
| 208 | - $translations = $this->model->term->get_translations( $term_id ); | |
| 177 | + foreach ( $translations as $lang => $tr_id ) { | |
| 178 | + if ( ! empty( $tr_id ) && $tr_id !== $term_id && $tr_parent = $this->model->term->get_translation( $term->parent, $lang ) ) { | |
| 179 | + $wpdb->update( | |
| 180 | + $wpdb->term_taxonomy, | |
| 181 | + array( 'parent' => isset( $tr_parent ) ? $tr_parent : 0 ), | |
| 182 | + array( 'term_taxonomy_id' => get_term( (int) $tr_id, $taxonomy )->term_taxonomy_id ) | |
| 183 | + ); | |
| 209 | 184 | |
| 210 | - foreach ( $translations as $lang => $tr_id ) { | |
| 211 | - if ( ! empty( $tr_id ) && $tr_id !== $term_id ) { | |
| 212 | - $tr_parent = $this->model->term->get_translation( $term->parent, $lang ); | |
| 213 | - $tr_term = get_term( (int) $tr_id, $taxonomy ); | |
| 214 | - | |
| 215 | - if ( $tr_term instanceof WP_Term ) { | |
| 216 | - $wpdb->update( | |
| 217 | - $wpdb->term_taxonomy, | |
| 218 | - array( 'parent' => $tr_parent ? $tr_parent : 0 ), | |
| 219 | - array( 'term_taxonomy_id' => $tr_term->term_taxonomy_id ) | |
| 220 | - ); | |
| 221 | - | |
| 222 | - clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9. | |
| 223 | - } | |
| 224 | - } | |
| 185 | + clean_term_cache( $tr_id, $taxonomy ); // OK since WP 3.9 | |
| 225 | 186 | } |
| 226 | 187 | } |
| 227 | 188 | } |
| 228 | 189 | } |
| @@ -232,9 +193,8 @@ | ||
| 232 | 193 | * |
| 233 | 194 | * @since 1.8 |
| 234 | 195 | * |
| 235 | 196 | * @param int $post_id post id |
| 236 | - * @return void | |
| 237 | 197 | */ |
| 238 | 198 | public function edit_attachment( $post_id ) { |
| 239 | 199 | $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) ); |
| 240 | 200 | } |
| @@ -239,15 +199,15 @@ | ||
| 239 | 199 | $this->pll_save_post( $post_id, get_post( $post_id ), $this->model->post->get_translations( $post_id ) ); |
| 240 | 200 | } |
| 241 | 201 | |
| 242 | 202 | /** |
| 243 | - * Synchronize sticky posts. | |
| 203 | + * Synchronize sticky posts | |
| 244 | 204 | * |
| 245 | 205 | * @since 2.3 |
| 246 | 206 | * |
| 247 | - * @param int[] $value New option value. | |
| 248 | - * @param int[] $old_value Old option value. | |
| 249 | - * @return int[] | |
| 207 | + * @param array $value New option value | |
| 208 | + * @param array $old_value Old option value | |
| 209 | + * @return array | |
| 250 | 210 | */ |
| 251 | 211 | public function sync_sticky_posts( $value, $old_value ) { |
| 252 | 212 | if ( in_array( 'sticky_posts', $this->options['sync'] ) ) { |
| 253 | 213 | // Stick post |