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