PluginProbe
Polylang / 2.1
Polylang v2.1
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 +89 -235 3.02.1 View file →
@@ -1,137 +1,65 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * Setups the objects languages and translations model.
4 + * Setups the objects languages and translations model
8 5 *
9 6 * @since 1.8
10 7 */
11 8 abstract class PLL_Translated_Object {
12 - /**
13 - * @var PLL_Model
14 - */
15 9 public $model;
10 + protected $object_type, $tax_language, $tax_translations, $tax_tt;
16 11
17 12 /**
18 - * Object type to use when registering the taxonomies.
19 - * Left empty for posts.
13 + * Constructor
20 14 *
21 - * @var string|null
22 - */
23 - protected $object_type;
24 -
25 - /**
26 - * Object type to use when checking capabilities.
27 - *
28 - * @var string
29 - */
30 - protected $type;
31 -
32 - /**
33 - * Taxonomy name for the languages.
34 - *
35 - * @var string
36 - */
37 - protected $tax_language;
38 -
39 - /**
40 - * Taxonomy name for the translation groups.
41 - *
42 - * @var string
43 - */
44 - protected $tax_translations;
45 -
46 - /**
47 - * PLL_Language property name for the term_taxonomy id.
48 - *
49 - * @var string
50 - */
51 - protected $tax_tt;
52 -
53 - /**
54 - * Constructor.
55 - *
56 15 * @since 1.8
57 16 *
58 - * @param PLL_Model $model Instance of PLL_Model.
17 + * @param object $model
59 18 */
60 19 public function __construct( &$model ) {
61 20 $this->model = &$model;
62 21
63 - /*
64 - * Register our taxonomies as soon as possible.
65 - * This is early registration, not ready for rewrite rules as $wp_rewrite will be setup later.
66 - */
22 + // register our taxonomies as soon as possible
23 + // this is early registration, not ready for rewrite rules as wp_rewrite will be setup later
67 24 $args = array( 'label' => false, 'public' => false, 'query_var' => false, 'rewrite' => false, '_pll' => true );
68 25 register_taxonomy( $this->tax_language, $this->object_type, $args );
69 - $args['update_count_callback'] = '_update_generic_term_count'; // Count *all* objects to avoid deleting in clean_translations_terms.
26 + $args['update_count_callback'] = '_update_generic_term_count'; // count *all* posts to avoid deleting in clean_translations_terms
70 27 register_taxonomy( $this->tax_translations, $this->object_type, $args );
71 28 }
72 29
73 30 /**
74 - * Stores the language in the database.
31 + * Wrap wp_get_object_terms to cache it and return only one object
32 + * inspired by the function get_the_terms
75 33 *
76 - * @since 0.6
77 - *
78 - * @param int $id Object id.
79 - * @param int|string|PLL_Language $lang Language (term_id or slug or object).
80 - * @return void
81 - */
82 - abstract public function set_language( $id, $lang );
83 -
84 - /**
85 - * Returns the language of an object.
86 - *
87 - * @since 0.1
88 - *
89 - * @param int $id Object id.
90 - * @return PLL_Language|false PLL_Language object, false if no language is associated to that object.
91 - */
92 - abstract public function get_language( $id );
93 -
94 - /**
95 - * Wrap wp_get_object_terms() to cache it and return only one object.
96 - * inspired by the WordPress function get_the_terms().
97 - *
98 34 * @since 1.2
99 35 *
100 - * @param int $object_id Object id ( typically a post_id or term_id ).
101 - * @param string $taxonomy Polylang taxonomy depending if we are looking for a post ( or term ) language ( or translation ).
102 - * @return WP_Term|false The term associated to the object in the requested taxonomy if it exists, false otherwise.
36 + * @param int $object_id post_id or term_id
37 + * @param string $taxonomy Polylang taxonomy depending if we are looking for a post ( or term ) language ( or translation )
38 + * @return bool|object the term associated to the object in the requested taxonomy if exists, false otherwise
103 39 */
104 40 public function get_object_term( $object_id, $taxonomy ) {
105 - if ( empty( $object_id ) || is_wp_error( $object_id ) ) {
41 + if ( empty( $object_id ) ) {
106 42 return false;
107 43 }
108 44
109 45 $object_id = (int) $object_id;
110 -
111 - if ( $object_id < 0 ) {
112 - return false;
113 - }
114 -
115 46 $term = get_object_term_cache( $object_id, $taxonomy );
116 47
117 48 if ( false === $term ) {
118 - // Query language and translations at the same time.
49 + // query language and translations at the same time
119 50 $taxonomies = array( $this->tax_language, $this->tax_translations );
120 51
121 - // Query terms.
122 - $terms = array();
123 - $object_terms = wp_get_object_terms( $object_id, $taxonomies, array( 'update_term_meta_cache' => false ) );
124 - if ( is_array( $object_terms ) ) {
125 - foreach ( $object_terms as $t ) {
126 - $terms[ $t->taxonomy ] = $t;
127 - if ( $t->taxonomy == $taxonomy ) {
128 - $term = $t;
129 - }
52 + // query terms
53 + foreach ( wp_get_object_terms( $object_id, $taxonomies, array( 'update_term_meta_cache' => false ) ) as $t ) {
54 + $terms[ $t->taxonomy ] = $t;
55 + if ( $t->taxonomy == $taxonomy ) {
56 + $term = $t;
130 57 }
131 58 }
132 59
133 - // Stores it the way WP expects it. Set an empty cache if no term was found in the taxonomy.
60 + // store it the way WP wants it
61 + // set an empty cache if no term found in the taxonomy
134 62 foreach ( $taxonomies as $tax ) {
135 63 wp_cache_add( $object_id, empty( $terms[ $tax ] ) ? array() : array( $terms[ $tax ] ), $tax . '_relationships' );
136 64 }
137 65 }
@@ -142,69 +70,66 @@
142 70 return empty( $term ) ? false : $term;
143 71 }
144 72
145 73 /**
146 - * Tells whether a translation term must be updated.
74 + * Tells wether to store a translation term
147 75 *
148 - * @since 2.3
76 + * @since 1.8
149 77 *
150 - * @param int $id Object id ( typically a post_id or term_id ).
151 - * @param int[] $translations An associative array of translations with language code as key and translation id as value.
152 - * @return bool
78 + * @param array $translations an associative array of translations with language code as key and translation id as value
153 79 */
154 - protected function should_update_translation_group( $id, $translations ) {
155 - // Don't do anything if no translations have been added to the group.
156 - $old_translations = $this->get_translations( $id ); // Includes at least $id itself.
157 - return count( array_diff_assoc( $translations, $old_translations ) ) > 0;
80 + protected function keep_translation_group( $translations ) {
81 + return count( $translations ) > 1;
158 82 }
159 83
160 84 /**
161 - * Saves translations for posts or terms.
85 + * Saves translations for posts or terms
162 86 *
163 87 * @since 0.5
164 88 *
165 - * @param int $id Object id ( typically a post_id or term_id ).
166 - * @param int[] $translations An associative array of translations with language code as key and translation id as value.
167 - * @return void
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
168 91 */
169 92 public function save_translations( $id, $translations ) {
170 93 $id = (int) $id;
171 94
172 - if ( ( $lang = $this->get_language( $id ) ) && is_array( $translations ) ) {
173 - // Sanitize the translations array.
95 + if ( ( $lang = $this->get_language( $id ) ) && isset( $translations ) && is_array( $translations ) ) {
96 + // sanitize the translations array
174 97 $translations = array_map( 'intval', $translations );
175 - $translations = array_merge( array( $lang->slug => $id ), $translations ); // Make sure this object is in translations.
176 - $translations = array_diff( $translations, array( 0 ) ); // Don't keep non translated languages.
177 - $translations = array_intersect_key( $translations, array_flip( $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ); // Keep only valid languages slugs as keys.
98 + $translations = array_merge( array( $lang->slug => $id ), $translations ); // make sure this object is in translations
99 + $translations = array_diff( $translations, array( 0 ) ); // don't keep non translated languages
100 + $translations = array_intersect_key( $translations, array_flip( $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ); // keep only valid languages slugs as keys
178 101
179 - // Unlink removed translations.
102 + // unlink removed translations
180 103 $old_translations = $this->get_translations( $id );
181 104 foreach ( array_diff_assoc( $old_translations, $translations ) as $object_id ) {
182 105 $this->delete_translation( $object_id );
183 106 }
184 107
185 - // Check id we need to create or update the translation group.
186 - 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 ) ) {
187 111 $terms = wp_get_object_terms( $translations, $this->tax_translations );
188 112 $term = reset( $terms );
189 113
190 - // Create a new term if necessary.
114 + // create a new term if necessary
191 115 if ( empty( $term ) ) {
192 - wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => maybe_serialize( $translations ) ) );
193 - } else {
194 - // Take care not to overwrite extra data stored in the description field, if any.
195 - $d = maybe_unserialize( $term->description );
196 - $d = is_array( $d ) ? array_diff_key( $d, $old_translations ) : array(); // Remove old translations.
197 - $d = array_merge( $d, $translations ); // Add new one.
198 - wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) );
116 + wp_insert_term( $group = uniqid( 'pll_' ), $this->tax_translations, array( 'description' => serialize( $translations ) ) );
199 117 }
118 + else {
119 + // take care not to overwrite extra data stored in description field, if any
120 + $d = unserialize( $term->description );
121 + $d = is_array( $d ) ? array_diff_key( $d, $old_translations ) : array(); // remove old translations
122 + $d = array_merge( $d, $translations ); // add new one
123 + wp_update_term( $group = (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) );
124 + }
200 125
201 - // Link all translations to the new term.
126 + // link all translations to the new term
202 127 foreach ( $translations as $p ) {
203 128 wp_set_object_terms( $p, $group, $this->tax_translations );
204 129 }
205 130
206 - // Clean now unused translation groups.
131 + // clean now unused translation groups
207 132 foreach ( wp_list_pluck( $terms, 'term_id' ) as $term_id ) {
208 133 $term = get_term( $term_id, $this->tax_translations );
209 134 if ( empty( $term->count ) ) {
210 135 wp_delete_term( $term_id, $this->tax_translations );
@@ -214,14 +139,13 @@
214 139 }
215 140 }
216 141
217 142 /**
218 - * Deletes a translation of a post or term.
143 + * Deletes a translation of a post or term
219 144 *
220 145 * @since 0.5
221 146 *
222 - * @param int $id Object id ( typically a post_id or term_id ).
223 - * @return void
147 + * @param int $id post id or term id
224 148 */
225 149 public function delete_translation( $id ) {
226 150 $id = (int) $id;
227 151 $term = $this->get_object_term( $id, $this->tax_translations );
@@ -226,40 +150,39 @@
226 150 $id = (int) $id;
227 151 $term = $this->get_object_term( $id, $this->tax_translations );
228 152
229 153 if ( ! empty( $term ) ) {
230 - $d = maybe_unserialize( $term->description );
231 - if ( is_array( $d ) ) {
232 - $slug = array_search( $id, $this->get_translations( $id ) ); // In case some plugin stores the same value with different key.
233 - unset( $d[ $slug ] );
234 - }
154 + $d = unserialize( $term->description );
155 + $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
156 + unset( $d[ $slug ] );
235 157
236 158 if ( empty( $d ) ) {
237 159 wp_delete_term( (int) $term->term_id, $this->tax_translations );
238 - } else {
239 - wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => maybe_serialize( $d ) ) );
240 160 }
161 + else {
162 + wp_update_term( (int) $term->term_id, $this->tax_translations, array( 'description' => serialize( $d ) ) );
163 + }
241 164 }
242 165 }
243 166
244 167 /**
245 - * Returns an array of translations of a post or term.
168 + * Returns an array of translations of a post or term
246 169 *
247 170 * @since 0.5
248 171 *
249 - * @param int $id Object id ( typically a post_id or term_id ).
250 - * @return int[] An associative array of translations with language code as key and translation id as value.
172 + * @param int $id post id or term id
173 + * @return array an associative array of translations with language code as key and translation id as value
251 174 */
252 175 public function get_translations( $id ) {
253 176 $term = $this->get_object_term( $id, $this->tax_translations );
254 - $translations = empty( $term ) ? array() : maybe_unserialize( $term->description );
177 + $translations = empty( $term ) ? array() : unserialize( $term->description );
255 178
256 - // 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 )
257 180 if ( is_array( $translations ) ) {
258 181 $translations = array_intersect_key( $translations, array_flip( $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) );
259 182 }
260 183
261 - // Make sure to return at least the passed object in its translation array.
184 + // make sure to return at least the passed post or term in its translation array
262 185 if ( empty( $translations ) && $lang = $this->get_language( $id ) ) {
263 186 $translations = array( $lang->slug => $id );
264 187 }
265 188
@@ -266,15 +189,15 @@
266 189 return $translations;
267 190 }
268 191
269 192 /**
270 - * Returns the id of the translation of a post or term.
193 + * Returns the id of the translation of a post or term
271 194 *
272 195 * @since 0.5
273 196 *
274 - * @param int $id Object id ( typically a post_id or term_id ).
275 - * @param PLL_Language|string $lang Language ( slug or object ).
276 - * @return int|false Object id of the translation, false if there is none.
197 + * @param int $id post id or term id
198 + * @param object|string $lang object or slug
199 + * @return bool|int post id or term id of the translation, false if there is none
277 200 */
278 201 public function get_translation( $id, $lang ) {
279 202 if ( ! $lang = $this->model->get_language( $lang ) ) {
280 203 return false;
@@ -289,60 +212,45 @@
289 212 * Among the object and its translations, returns the id of the object which is in $lang
290 213 *
291 214 * @since 0.1
292 215 *
293 - * @param int $id Object id ( typically a post_id or term_id ).
294 - * @param int|string|PLL_Language $lang Language ( term_id or slug or object ).
295 - * @return int|false The translation object id if exists, otherwise the passed id, false if the passed object has no language.
216 + * @param int $id post id or term id
217 + * @param int|string|object $lang language ( term_id or slug or object )
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
296 219 */
297 220 public function get( $id, $lang ) {
298 - $id = (int) $id;
299 - $lang = $this->model->get_language( $lang );
300 - $obj_lang = $this->get_language( $id );
301 - if ( empty( $lang ) || empty( $obj_lang ) ) {
221 + $obj_lang = $this->get_language( $id ); // FIXME is this necessary?
222 + if ( ! $lang || ! $obj_lang ) {
302 223 return false;
303 224 }
304 225
226 + $lang = $this->model->get_language( $lang );
305 227 return $obj_lang->term_id == $lang->term_id ? $id : $this->get_translation( $id, $lang );
306 228 }
307 229
308 230 /**
309 - * A join clause to add to sql queries when filtering by language is needed directly in query.
231 + * A where clause to add to sql queries when filtering by language is needed directly in query
310 232 *
311 233 * @since 1.2
312 234 *
313 - * @param string $alias Optional alias for object table.
314 - * @return string Join clause.
235 + * @param object|array|string $lang a PLL_Language object or a comma separated list of languag slug or an array of language slugs
236 + * @return string where clause
315 237 */
316 - abstract public function join_clause( $alias = '' );
317 -
318 - /**
319 - * A where clause to add to sql queries when filtering by language is needed directly in query.
320 - *
321 - * @since 1.2
322 - *
323 - * @param PLL_Language|string|string[] $lang PLL_Language object or a comma separated list of language slug or an array of language slugs.
324 - * @return string Where clause.
325 - */
326 238 public function where_clause( $lang ) {
239 + global $wpdb;
327 240 $tt_id = $this->tax_tt;
328 241
329 - /*
330 - * $lang is an object.
331 - * This is generally the case if the query is coming from Polylang.
332 - */
242 + // $lang is an object
243 + // generally the case if the query is coming from Polylang
333 244 if ( is_object( $lang ) ) {
334 - 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 );
335 246 }
336 247
337 - /*
338 - * $lang is a comma separated list of slugs ( or an array of slugs ).
339 - * This is generally the case is the query is coming from outside with a 'lang' parameter.
340 - */
341 - $slugs = is_array( $lang ) ? $lang : explode( ',', $lang );
342 - $languages = array();
248 + // $lang is a comma separated list of slugs ( or an array of slugs )
249 + // generally the case is the query is coming from outside with 'lang' parameter
250 + $slugs = is_array( $lang ) ? $lang : explode( ',', $lang );
343 251 foreach ( $slugs as $slug ) {
344 - $languages[] = absint( $this->model->get_language( $slug )->$tt_id );
252 + $languages[] = (int) $this->model->get_language( $slug )->$tt_id;
345 253 }
346 254
347 255 return ' AND pll_tr.term_taxonomy_id IN ( ' . implode( ',', $languages ) . ' )';
348 256 }
@@ -347,72 +255,18 @@
347 255 return ' AND pll_tr.term_taxonomy_id IN ( ' . implode( ',', $languages ) . ' )';
348 256 }
349 257
350 258 /**
351 - * Returns ids of objects in a language similarly to get_objects_in_term() for a taxonomy.
352 - * It is faster than get_objects_in_term() as it avoids a JOIN.
259 + * Returns ids of objects in a language similarly to get_objects_in_term for a taxonomy
260 + * faster than get_objects_in_term as it avoids a JOIN
353 261 *
354 262 * @since 1.4
355 263 *
356 - * @param PLL_Language $lang PLL_Language object.
357 - * @return int[] Object ids.
264 + * @param object $lang a PLL_Language object
265 + * @return array
358 266 */
359 267 public function get_objects_in_language( $lang ) {
360 268 global $wpdb;
361 269 $tt_id = $this->tax_tt;
362 -
363 - $last_changed = wp_cache_get_last_changed( 'terms' );
364 - $cache_key = "polylang:get_objects_in_language:{$lang->$tt_id}:{$last_changed}";
365 - $cache = wp_cache_get( $cache_key, 'terms' );
366 -
367 - if ( false === $cache ) {
368 - $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $lang->$tt_id ) );
369 - wp_cache_set( $cache_key, $object_ids, 'terms' );
370 - } else {
371 - $object_ids = (array) $cache;
372 - }
373 -
374 - if ( ! $object_ids ) {
375 - return array();
376 - }
377 -
378 - return $object_ids;
379 - }
380 -
381 - /**
382 - * Check if a user can synchronize translations.
383 - *
384 - * @since 2.6
385 - *
386 - * @param int $id Object id.
387 - * @return bool
388 - */
389 - public function current_user_can_synchronize( $id ) {
390 - /**
391 - * Filters whether a synchronization capability check should take place.
392 - *
393 - * @since 2.6
394 - *
395 - * @param bool|null $check Null to enable the capability check,
396 - * true to always allow the synchronization,
397 - * false to always disallow the synchronization.
398 - * Defaults to true.
399 - * @param int $id The synchronization source object id.
400 - */
401 - $check = apply_filters( "pll_pre_current_user_can_synchronize_{$this->type}", true, $id );
402 - if ( null !== $check ) {
403 - return $check;
404 - }
405 -
406 - if ( ! current_user_can( "edit_{$this->type}", $id ) ) {
407 - return false;
408 - }
409 -
410 - foreach ( $this->get_translations( $id ) as $tr_id ) {
411 - if ( $tr_id !== $id && ! current_user_can( "edit_{$this->type}", $tr_id ) ) {
412 - return false;
413 - }
414 - }
415 -
416 - return true;
270 + return $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $lang->$tt_id ) );
417 271 }
418 272 }