PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
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
← All changes | include/translated-object.php +25 -75 2.82.0.3 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Setups the objects languages and translations model
8 5 *
@@ -9,9 +6,9 @@
9 6 * @since 1.8
10 7 */
11 8 abstract class PLL_Translated_Object {
12 9 public $model;
13 - protected $object_type, $type, $tax_language, $tax_translations, $tax_tt;
10 + protected $object_type, $tax_language, $tax_translations, $tax_tt;
14 11
15 12 /**
16 13 * Constructor
17 14 *
@@ -40,18 +37,13 @@
40 37 * @param string $taxonomy Polylang taxonomy depending if we are looking for a post ( or term ) language ( or translation )
41 38 * @return bool|object the term associated to the object in the requested taxonomy if exists, false otherwise
42 39 */
43 40 public function get_object_term( $object_id, $taxonomy ) {
44 - if ( empty( $object_id ) || is_wp_error( $object_id ) ) {
41 + if ( empty( $object_id ) ) {
45 42 return false;
46 43 }
47 44
48 45 $object_id = (int) $object_id;
49 -
50 - if ( $object_id < 0 ) {
51 - return false;
52 - }
53 -
54 46 $term = get_object_term_cache( $object_id, $taxonomy );
55 47
56 48 if ( false === $term ) {
57 49 // query language and translations at the same time
@@ -57,10 +49,9 @@
57 49 // query language and translations at the same time
58 50 $taxonomies = array( $this->tax_language, $this->tax_translations );
59 51
60 52 // query terms
61 - $terms = array();
62 - foreach ( wp_get_object_terms( $object_id, $taxonomies, array( 'update_term_meta_cache' => false ) ) as $t ) {
53 + foreach ( wp_get_object_terms( $object_id, $taxonomies ) as $t ) {
63 54 $terms[ $t->taxonomy ] = $t;
64 55 if ( $t->taxonomy == $taxonomy ) {
65 56 $term = $t;
66 57 }
@@ -79,19 +70,16 @@
79 70 return empty( $term ) ? false : $term;
80 71 }
81 72
82 73 /**
83 - * Tells whether a translation term must be updated
74 + * Tells wether to store a translation term
84 75 *
85 - * @since 2.3
76 + * @since 1.8
86 77 *
87 - * @param array $id Post id or term id
88 - * @param array $translations An associative array of translations with language code as key and translation id as value
78 + * @param array $translations an associative array of translations with language code as key and translation id as value
89 79 */
90 - protected function should_update_translation_group( $id, $translations ) {
91 - // Don't do anything if no translations have been added to the group
92 - $old_translations = $this->get_translations( $id ); // Includes at least $id itself
93 - return count( array_diff_assoc( $translations, $old_translations ) ) > 0;
80 + protected function keep_translation_group( $translations ) {
81 + return count( $translations ) > 1;
94 82 }
95 83
96 84 /**
97 85 * Saves translations for posts or terms
@@ -97,10 +85,10 @@
97 85 * Saves translations for posts or terms
98 86 *
99 87 * @since 0.5
100 88 *
101 - * @param int $id Post id or term id
102 - * @param array $translations An associative array of translations with language code as key and translation id as value
89 + * @param int $id post id or term id
90 + * @param array $translations an associative array of translations with language code as key and translation id as value
103 91 */
104 92 public function save_translations( $id, $translations ) {
105 93 $id = (int) $id;
106 94
@@ -116,23 +104,24 @@
116 104 foreach ( array_diff_assoc( $old_translations, $translations ) as $object_id ) {
117 105 $this->delete_translation( $object_id );
118 106 }
119 107
120 - // Check id we need to create or update the translation group
121 - if ( $this->should_update_translation_group( $id, $translations ) ) {
108 + // don't create a translation group for untranslated posts as it is useless
109 + // but we need one for terms to allow relationships remap when importing from a WXR file
110 + if ( $this->keep_translation_group( $translations ) ) {
122 111 $terms = wp_get_object_terms( $translations, $this->tax_translations );
123 112 $term = reset( $terms );
124 113
125 114 // create a new term if necessary
126 115 if ( empty( $term ) ) {
127 - wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => maybe_serialize( $translations ) ) );
116 + wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => serialize( $translations ) ) );
128 117 }
129 118 else {
130 119 // take care not to overwrite extra data stored in description field, if any
131 - $d = maybe_unserialize( $term->description );
120 + $d = unserialize( $term->description );
132 121 $d = is_array( $d ) ? array_diff_key( $d, $old_translations ) : array(); // remove old translations
133 122 $d = array_merge( $d, $translations ); // add new one
134 - wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) );
123 + wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) );
135 124 }
136 125
137 126 // link all translations to the new term
138 127 foreach ( $translations as $p ) {
@@ -161,9 +150,9 @@
161 150 $id = (int) $id;
162 151 $term = $this->get_object_term( $id, $this->tax_translations );
163 152
164 153 if ( ! empty( $term ) ) {
165 - $d = maybe_unserialize( $term->description );
154 + $d = unserialize( $term->description );
166 155 $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
167 156 unset( $d[ $slug ] );
168 157
169 158 if ( empty( $d ) ) {
@@ -169,9 +158,9 @@
169 158 if ( empty( $d ) ) {
170 159 wp_delete_term( (int) $term->term_id, $this->tax_translations );
171 160 }
172 161 else {
173 - wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) );
162 + wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) );
174 163 }
175 164 }
176 165 }
177 166
@@ -184,11 +173,11 @@
184 173 * @return array an associative array of translations with language code as key and translation id as value
185 174 */
186 175 public function get_translations( $id ) {
187 176 $term = $this->get_object_term( $id, $this->tax_translations );
188 - $translations = empty( $term ) ? array() : maybe_unserialize( $term->description );
177 + $translations = empty( $term ) ? array() : unserialize( $term->description );
189 178
190 - // make sure we return only translations ( thus we allow plugins to store other information in the array )
179 + // make sure we return only translations ( thus we allow plugins to store other informations in the array )
191 180 if ( is_array( $translations ) ) {
192 181 $translations = array_intersect_key( $translations, array_flip( $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) );
193 182 }
194 183
@@ -228,9 +217,8 @@
228 217 * @param int|string|object $lang language ( term_id or slug or object )
229 218 * @return bool|int the translation post id or term id if exists, otherwise the post id or term id, false if the post has no language
230 219 */
231 220 public function get( $id, $lang ) {
232 - $id = (int) $id;
233 221 $obj_lang = $this->get_language( $id ); // FIXME is this necessary?
234 222 if ( ! $lang || ! $obj_lang ) {
235 223 return false;
236 224 }
@@ -243,26 +231,26 @@
243 231 * A where clause to add to sql queries when filtering by language is needed directly in query
244 232 *
245 233 * @since 1.2
246 234 *
247 - * @param object|array|string $lang a PLL_Language object or a comma separated list of language slug or an array of language slugs
235 + * @param object|array|string $lang a PLL_Language object or a comma separated list of languag slug or an array of language slugs
248 236 * @return string where clause
249 237 */
250 238 public function where_clause( $lang ) {
239 + global $wpdb;
251 240 $tt_id = $this->tax_tt;
252 241
253 242 // $lang is an object
254 243 // generally the case if the query is coming from Polylang
255 244 if ( is_object( $lang ) ) {
256 - return ' AND pll_tr.term_taxonomy_id = ' . absint( $lang->$tt_id );
245 + return $wpdb->prepare( ' AND pll_tr.term_taxonomy_id = %d', $lang->$tt_id );
257 246 }
258 247
259 248 // $lang is a comma separated list of slugs ( or an array of slugs )
260 249 // generally the case is the query is coming from outside with 'lang' parameter
261 - $slugs = is_array( $lang ) ? $lang : explode( ',', $lang );
262 - $languages = array();
250 + $slugs = is_array( $lang ) ? $lang : explode( ',', $lang );
263 251 foreach ( $slugs as $slug ) {
264 - $languages[] = absint( $this->model->get_language( $slug )->$tt_id );
252 + $languages[] = (int) $this->model->get_language( $slug )->$tt_id;
265 253 }
266 254
267 255 return ' AND pll_tr.term_taxonomy_id IN ( ' . implode( ',', $languages ) . ' )';
268 256 }
@@ -279,44 +267,6 @@
279 267 public function get_objects_in_language( $lang ) {
280 268 global $wpdb;
281 269 $tt_id = $this->tax_tt;
282 270 return $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $lang->$tt_id ) );
283 - }
284 -
285 - /**
286 - * Check if a user can synchronize translations
287 - *
288 - * @since 2.6
289 - *
290 - * @param int $id Object id
291 - * @return bool
292 - */
293 - public function current_user_can_synchronize( $id ) {
294 - /**
295 - * Filters whether a synchronization capability check should take place
296 - *
297 - * @since 2.6
298 - *
299 - * @param $check null to enable the capability check,
300 - * true to always allow the synchronization,
301 - * false to always disallow the synchronization.
302 - * Defaults to true.
303 - * @param $id The synchronization source object id
304 - */
305 - $check = apply_filters( "pll_pre_current_user_can_synchronize_{$this->type}", true, $id );
306 - if ( null !== $check ) {
307 - return $check;
308 - }
309 -
310 - if ( ! current_user_can( "edit_{$this->type}", $id ) ) {
311 - return false;
312 - }
313 -
314 - foreach ( $this->get_translations( $id ) as $tr_id ) {
315 - if ( $tr_id !== $id && ! current_user_can( "edit_{$this->type}", $tr_id ) ) {
316 - return false;
317 - }
318 - }
319 -
320 - return true;
321 271 }
322 272 }