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