PluginProbe
Polylang / 2.9
Polylang v2.9
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/model.php +169 -133 3.02.9 View file →
@@ -8,53 +8,29 @@
8 8 *
9 9 * @since 1.2
10 10 */
11 11 class PLL_Model {
12 - /**
13 - * Internal non persistent cache object.
14 - *
15 - * @var PLL_Cache
16 - */
17 - public $cache;
18 -
19 - /**
20 - * Stores the plugin options.
21 - *
22 - * @var array
23 - */
12 + public $cache; // Our internal non persistent cache object
24 13 public $options;
14 + public $post, $term; // Translated objects models
25 15
26 16 /**
27 - * Translated post model.
17 + * Constructor
18 + * setups translated objects sub models
19 + * setups filters and actions
28 20 *
29 - * @var PLL_Translated_Post
30 - */
31 - public $post;
32 -
33 - /**
34 - * Translated term model.
35 - *
36 - * @var PLL_Translated_Term
37 - */
38 - public $term;
39 -
40 - /**
41 - * Constructor.
42 - * Setups translated objects sub models.
43 - * Setups filters and actions.
44 - *
45 21 * @since 1.2
46 22 *
47 - * @param array $options Polylang options.
23 + * @param array $options Polylang options
48 24 */
49 25 public function __construct( &$options ) {
50 26 $this->options = &$options;
51 27
52 28 $this->cache = new PLL_Cache();
53 - $this->post = new PLL_Translated_Post( $this ); // Translated post sub model.
54 - $this->term = new PLL_Translated_Term( $this ); // Translated term sub model.
29 + $this->post = new PLL_Translated_Post( $this ); // translated post sub model
30 + $this->term = new PLL_Translated_Term( $this ); // translated term sub model
55 31
56 - // We need to clean languages cache when editing a language and when modifying the permalink structure.
32 + // We need to clean languages cache when editing a language and when modifying the permalink structure
57 33 add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
58 34 add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
59 35 add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
60 36 add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
@@ -60,29 +36,31 @@
60 36 add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
61 37
62 38 add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
63 39
64 - // Just in case someone would like to display the language description ;).
40 + // Just in case someone would like to display the language description ;- )
65 41 add_filter( 'language_description', '__return_empty_string' );
66 42 }
67 43
68 44 /**
69 - * Returns the list of available languages.
70 - * - Stores the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false.
71 - * - Caches the list ( with flags ) in a PLL_Cache object.
45 + * Returns the list of available languages
46 + * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false
47 + * caches the list ( with flags ) in the private property $languages
72 48 *
49 + * List of parameters accepted in $args:
50 + *
51 + * hide_empty => hides languages with no posts if set to true ( defaults to false )
52 + * fields => return only that field if set ( see PLL_Language for a list of fields )
53 + *
73 54 * @since 0.1
74 55 *
75 - * @param array $args {
76 - * @type bool $hide_empty Hides languages with no posts if set to true ( defaults to false ).
77 - * @type string $fields Returns only that field if set; {@see PLL_Language} for a list of fields.
78 - * }
79 - * @return array List of PLL_Language objects or PLL_Language object properties.
56 + * @param array $args
57 + * @return array|string|int list of PLL_Language objects or PLL_Language object properties
80 58 */
81 59 public function get_languages_list( $args = array() ) {
82 60 if ( false === $languages = $this->cache->get( 'languages' ) ) {
83 61
84 - // Create the languages from taxonomies.
62 + // Create the languages from taxonomies
85 63 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
86 64 $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
87 65 $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
88 66
@@ -90,40 +68,38 @@
90 68 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
91 69 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
92 70
93 71 if ( ! empty( $languages ) && ! empty( $term_languages ) ) {
94 - // Don't use array_map + create_function() to instantiate an autoloaded class as it breaks badly in old versions of PHP.
72 + // Don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
95 73 foreach ( $languages as $k => $v ) {
96 74 $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
97 75 }
98 76
99 - // We will need the languages list to allow its access in the filter below.
77 + // We will need the languages list to allow its access in the filter below
100 78 $this->cache->set( 'languages', $languages );
101 79
102 80 /**
103 - * Filters the list of languages *before* it is stored in the persistent cache.
104 - * /!\ This filter is fired *before* the $polylang object is available.
81 + * Filter the list of languages *before* it is stored in the persistent cache
82 + * /!\ this filter is fired *before* the $polylang object is available
105 83 *
106 84 * @since 1.7.5
107 85 *
108 - * @param PLL_Language[] $languages The list of language objects.
109 - * @param PLL_Model $model PLL_Model object.
86 + * @param array $languages the list of language objects
87 + * @param object $model PLL_Model object
110 88 */
111 89 $languages = apply_filters( 'pll_languages_list', $languages, $this );
112 90
113 - /*
114 - * Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache.
115 - * Thanks to captin411 for catching this!
116 - * @see https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255
117 - */
91 + // Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache
92 + // Thanks to captin411 for catching this!
93 + // See https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255;
118 94 set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
119 95 }
120 96 else {
121 - $languages = array(); // In case something went wrong.
97 + $languages = array(); // In case something went wrong
122 98 }
123 99 }
124 100
125 - // Create the languages directly from arrays stored in transients.
101 + // Create the languages directly from arrays stored in transients
126 102 else {
127 103 foreach ( $languages as $k => $v ) {
128 104 $languages[ $k ] = new PLL_Language( $v );
129 105 }
@@ -129,14 +105,14 @@
129 105 }
130 106 }
131 107
132 108 /**
133 - * Filters the list of languages *after* it is stored in the persistent cache.
134 - * /!\ This filter is fired *before* the $polylang object is available.
109 + * Filter the list of languages *after* it is stored in the persistent cache
110 + * /!\ this filter is fired *before* the $polylang object is available
135 111 *
136 112 * @since 1.8
137 113 *
138 - * @param PLL_Language[] $languages The list of language objects.
114 + * @param array $languages the list of language objects
139 115 */
140 116 $languages = apply_filters( 'pll_after_languages_cache', $languages );
141 117 $this->cache->set( 'languages', $languages );
142 118 }
@@ -142,9 +118,9 @@
142 118 }
143 119
144 120 $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
145 121
146 - // Remove empty languages if requested.
122 + // Remove empty languages if requested
147 123 if ( $args['hide_empty'] ) {
148 124 $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
149 125 }
150 126
@@ -159,9 +135,8 @@
159 135 * @since 1.2
160 136 *
161 137 * @param int $term not used
162 138 * @param string $taxonomy taxonomy name
163 - * @return void
164 139 */
165 140 public function clean_languages_cache( $term = 0, $taxonomy = null ) {
166 141 if ( empty( $taxonomy ) || 'language' == $taxonomy ) {
167 142 delete_transient( 'pll_languages_list' );
@@ -184,14 +159,14 @@
184 159 return $args;
185 160 }
186 161
187 162 /**
188 - * Returns the language by its term_id, tl_term_id, slug or locale.
163 + * Returns the language by its term_id, tl_term_id, slug or locale
189 164 *
190 165 * @since 0.1
191 166 *
192 - * @param mixed $value term_id, tl_term_id, slug or locale of the queried language.
193 - * @return PLL_Language|false Language object, false if no language found.
167 + * @param int|string $value term_id, tl_term_id, slug or locale of the queried language
168 + * @return object|bool PLL_Language object, false if no language found
194 169 */
195 170 public function get_language( $value ) {
196 171 if ( is_object( $value ) ) {
197 172 return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language
@@ -211,15 +186,15 @@
211 186 return $return;
212 187 }
213 188
214 189 /**
215 - * Adds terms clauses to the term query to filter them by languages.
190 + * Adds terms clauses to get_terms to filter them by languages - used in both frontend and admin
216 191 *
217 192 * @since 1.2
218 193 *
219 - * @param string[] $clauses The list of sql clauses in terms query.
220 - * @param PLL_Language $lang PLL_Language object.
221 - * @return string[] Modified list of clauses.
194 + * @param array $clauses the list of sql clauses in terms query
195 + * @param object $lang PLL_Language object
196 + * @return array modified list of clauses
222 197 */
223 198 public function terms_clauses( $clauses, $lang ) {
224 199 if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
225 200 $clauses['join'] .= $this->term->join_clause();
@@ -228,17 +203,16 @@
228 203 return $clauses;
229 204 }
230 205
231 206 /**
232 - * Returns post types that need to be translated.
233 - * The post types list is cached for better better performance.
234 - * The method waits for 'after_setup_theme' to apply the cache
235 - * to allow themes adding the filter in functions.php.
207 + * Returns post types that need to be translated
208 + * the post types list is cached for better better performance
209 + * wait for 'after_setup_theme' to apply the cache to allow themes adding the filter in functions.php
236 210 *
237 211 * @since 1.2
238 212 *
239 - * @param bool $filter True if we should return only valid registered post types.
240 - * @return string[] Post type names for which Polylang manages languages and translations.
213 + * @param bool $filter true if we should return only valid registered post types
214 + * @return array post type names for which Polylang manages languages and translations
241 215 */
242 216 public function get_translated_post_types( $filter = true ) {
243 217 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
244 218 $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
@@ -251,9 +225,9 @@
251 225 $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
252 226 }
253 227
254 228 /**
255 - * Filters the list of post types available for translation.
229 + * Filter the list of post types available for translation.
256 230 * The default are post types which have the parameter ‘public’ set to true.
257 231 * The filter must be added soon in the WordPress loading process:
258 232 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
259 233 *
@@ -258,10 +232,10 @@
258 232 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
259 233 *
260 234 * @since 0.8
261 235 *
262 - * @param string[] $post_types List of post type names.
263 - * @param bool $is_settings True when displaying the list of custom post types in Polylang settings.
236 + * @param array $post_types list of post type names
237 + * @param bool $is_settings true when displaying the list of custom post types in Polylang settings
264 238 */
265 239 $post_types = apply_filters( 'pll_get_post_types', $post_types, false );
266 240
267 241 if ( did_action( 'after_setup_theme' ) ) {
@@ -272,13 +246,13 @@
272 246 return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types;
273 247 }
274 248
275 249 /**
276 - * Returns true if Polylang manages languages and translations for this post type.
250 + * Returns true if Polylang manages languages and translations for this post type
277 251 *
278 252 * @since 1.2
279 253 *
280 - * @param string|string[] $post_type Post type name or array of post type names.
254 + * @param string|array $post_type post type name or array of post type names
281 255 * @return bool
282 256 */
283 257 public function is_translated_post_type( $post_type ) {
284 258 $post_types = $this->get_translated_post_types( false );
@@ -285,14 +259,14 @@
285 259 return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) );
286 260 }
287 261
288 262 /**
289 - * Returns taxonomies that need to be translated.
263 + * Return taxonomies that need to be translated
290 264 *
291 265 * @since 1.2
292 266 *
293 - * @param bool $filter True if we should return only valid registered taxonomies.
294 - * @return string[] Array of registered taxonomy names for which Polylang manages languages and translations.
267 + * @param bool $filter true if we should return only valid registered taxonomies
268 + * @return array array of registered taxonomy names for which Polylang manages languages and translations
295 269 */
296 270 public function get_translated_taxonomies( $filter = true ) {
297 271 if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
298 272 $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' );
@@ -301,9 +275,9 @@
301 275 $taxonomies = array_merge( $taxonomies, array_combine( $this->options['taxonomies'], $this->options['taxonomies'] ) );
302 276 }
303 277
304 278 /**
305 - * Filters the list of taxonomies available for translation.
279 + * Filter the list of taxonomies available for translation.
306 280 * The default are taxonomies which have the parameter ‘public’ set to true.
307 281 * The filter must be added soon in the WordPress loading process:
308 282 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
309 283 *
@@ -308,10 +282,10 @@
308 282 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
309 283 *
310 284 * @since 0.8
311 285 *
312 - * @param string[] $taxonomies List of taxonomy names.
313 - * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings.
286 + * @param array $taxonomies list of taxonomy names
287 + * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings
314 288 */
315 289 $taxonomies = apply_filters( 'pll_get_taxonomies', $taxonomies, false );
316 290 if ( did_action( 'after_setup_theme' ) ) {
317 291 $this->cache->set( 'taxonomies', $taxonomies );
@@ -321,13 +295,13 @@
321 295 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
322 296 }
323 297
324 298 /**
325 - * Returns true if Polylang manages languages and translations for this taxonomy.
299 + * Returns true if Polylang manages languages and translations for this taxonomy
326 300 *
327 301 * @since 1.2
328 302 *
329 - * @param string|string[] $tax Taxonomy name or array of taxonomy names.
303 + * @param string|array $tax taxonomy name or array of taxonomy names
330 304 * @return bool
331 305 */
332 306 public function is_translated_taxonomy( $tax ) {
333 307 $taxonomies = $this->get_translated_taxonomies( false );
@@ -334,14 +308,14 @@
334 308 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
335 309 }
336 310
337 311 /**
338 - * Return staxonomies that need to be filtered ( post_format like ).
312 + * Return taxonomies that need to be filtered ( post_format like )
339 313 *
340 314 * @since 1.7
341 315 *
342 - * @param bool $filter True if we should return only valid registered taxonomies.
343 - * @return string[] Array of registered taxonomy names.
316 + * @param bool $filter true if we should return only valid registered taxonomies
317 + * @return array array of registered taxonomy names
344 318 */
345 319 public function get_filtered_taxonomies( $filter = true ) {
346 320 if ( did_action( 'after_setup_theme' ) ) {
347 321 static $taxonomies = null;
@@ -350,9 +324,9 @@
350 324 if ( empty( $taxonomies ) ) {
351 325 $taxonomies = array( 'post_format' => 'post_format' );
352 326
353 327 /**
354 - * Filters the list of taxonomies not translatable but filtered by language.
328 + * Filter the list of taxonomies not translatable but filtered by language.
355 329 * Includes only the post format by default
356 330 * The filter must be added soon in the WordPress loading process:
357 331 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
358 332 *
@@ -357,10 +331,10 @@
357 331 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
358 332 *
359 333 * @since 1.7
360 334 *
361 - * @param string[] $taxonomies List of taxonomy names.
362 - * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings.
335 + * @param array $taxonomies list of taxonomy names
336 + * @param bool $is_settings true when displaying the list of custom taxonomies in Polylang settings
363 337 */
364 338 $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false );
365 339 }
366 340
@@ -367,13 +341,13 @@
367 341 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
368 342 }
369 343
370 344 /**
371 - * Returns true if Polylang filters this taxonomy per language.
345 + * Returns true if Polylang filters this taxonomy per language
372 346 *
373 347 * @since 1.7
374 348 *
375 - * @param string|string[] $tax Taxonomy name or array of taxonomy names.
349 + * @param string|array $tax taxonomy name or array of taxonomy names
376 350 * @return bool
377 351 */
378 352 public function is_filtered_taxonomy( $tax ) {
379 353 $taxonomies = $this->get_filtered_taxonomies( false );
@@ -380,9 +354,9 @@
380 354 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
381 355 }
382 356
383 357 /**
384 - * Returns the query vars of all filtered taxonomies.
358 + * Returns the query vars of all filtered taxonomies
385 359 *
386 360 * @since 1.7
387 361 *
388 362 * @return array
@@ -390,11 +364,9 @@
390 364 public function get_filtered_taxonomies_query_vars() {
391 365 $query_vars = array();
392 366 foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) {
393 367 $tax = get_taxonomy( $filtered_tax );
394 - if ( ! empty( $tax ) ) {
395 - $query_vars[] = $tax->query_var;
396 - }
368 + $query_vars[] = $tax->query_var;
397 369 }
398 370 return $query_vars;
399 371 }
400 372
@@ -403,9 +375,8 @@
403 375 *
404 376 * @since 1.2
405 377 *
406 378 * @param object|string|int $lang language
407 - * @return void
408 379 */
409 380 public function create_default_category( $lang ) {
410 381 $lang = $this->get_language( $lang );
411 382
@@ -437,18 +408,18 @@
437 408 }
438 409
439 410 /**
440 411 * It is possible to have several terms with the same name in the same taxonomy ( one per language )
441 - * but the native term_exists() will return true even if only one exists.
442 - * So here the function adds the language parameter.
412 + * but the native term_exists will return true even if only one exists
413 + * so here the function adds the language parameter
443 414 *
444 415 * @since 1.4
445 416 *
446 - * @param string $term_name The term name.
447 - * @param string $taxonomy Taxonomy name.
448 - * @param int $parent Parent term id.
449 - * @param string|PLL_Language $language The language slug or object.
450 - * @return null|int The term_id of the found term.
417 + * @param string $term_name the term name
418 + * @param string $taxonomy taxonomy name
419 + * @param int $parent parent term id
420 + * @param string|object $language the language slug or object
421 + * @return null|int the term_id of the found term
451 422 */
452 423 public function term_exists( $term_name, $taxonomy, $parent, $language ) {
453 424 global $wpdb;
454 425
@@ -469,17 +440,17 @@
469 440 return $wpdb->get_var( $select . $join . $where );
470 441 }
471 442
472 443 /**
473 - * Checks if a term slug exists in a given language, taxonomy, hierarchy.
444 + * Checks if a term slug exists in a given language, taxonomy, hierarchy
474 445 *
475 446 * @since 1.9
476 - * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug().
447 + * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug()
477 448 *
478 - * @param string $slug The term slug to test.
479 - * @param string|PLL_Language $language The language slug or object.
480 - * @param string $taxonomy Optional taxonomy name.
481 - * @param int $parent Optional parent term id.
449 + * @param string $slug The term slug to test.
450 + * @param string|object $language The language slug or object.
451 + * @param string $taxonomy Optional taxonomy name.
452 + * @param int $parent Optional parent term id.
482 453 * @return null|int The term_id of the found term.
483 454 */
484 455 public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) {
485 456 global $wpdb;
@@ -507,22 +478,10 @@
507 478 * Gets the number of posts per language in a date, author or post type archive.
508 479 *
509 480 * @since 1.2
510 481 *
511 - * @param PLL_Language $lang PLL_Language instance.
512 - * @param array $q {
513 - * WP_Query arguments:
514 - *
515 - * @type string|string[] $post_type Post type or array of post types.
516 - * @type int $m Combination YearMonth. Accepts any four-digit year and month.
517 - * @type int $year Four-digit year.
518 - * @type int $monthnum Two-digit month.
519 - * @type int $day Day of the month.
520 - * @type int $author Author id.
521 - * @type string $author_name User 'user_nicename'.
522 - * @type string $post_format Post format.
523 - * @type string $post_status Post status.
524 - * }
482 + * @param object $lang PLL_Language instance.
483 + * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ).
525 484 * @return int
526 485 */
527 486 public function count_posts( $lang, $q = array() ) {
528 487 global $wpdb;
@@ -549,9 +508,9 @@
549 508 if ( false === $counts ) {
550 509 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
551 510 $join = $this->post->join_clause();
552 511 $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
553 - $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", implode( "', '", esc_sql( $q['post_type'] ) ) );
512 + $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", esc_sql( $q['post_type'] ) ) );
554 513 $where .= $this->post->where_clause( $this->get_languages_list() );
555 514 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
556 515
557 516 if ( ! empty( $q['m'] ) ) {
@@ -611,13 +570,13 @@
611 570 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
612 571 }
613 572
614 573 /**
615 - * Setup the links model based on options.
574 + * Setup the links model based on options
616 575 *
617 576 * @since 1.2
618 577 *
619 - * @return PLL_Links_Model
578 + * @return object implementing "links_model interface"
620 579 */
621 580 public function get_links_model() {
622 581 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
623 582 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
@@ -622,16 +581,93 @@
622 581 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
623 582 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
624 583
625 584 /**
626 - * Filters the links model class to use.
627 - * /!\ this filter is fired *before* the $polylang object is available.
585 + * Filter the links model class to use
586 + * /!\ this filter is fired *before* the $polylang object is available
628 587 *
629 588 * @since 2.1.1
630 589 *
631 - * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain.
590 + * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain
632 591 */
633 592 $class = apply_filters( 'pll_links_model', $class );
634 593
635 594 return new $class( $this );
595 + }
596 +
597 + /**
598 + * Some backward compatibility with Polylang < 1.8
599 + * allows for example to call $polylang->model->get_post_languages( $post_id ) instead of $polylang->model->post->get_language( $post_id )
600 + * this works but should be slower than the direct call, thus an error is triggered in debug mode
601 + *
602 + * @since 1.8
603 + *
604 + * @param string $func Function name
605 + * @param array $args Function arguments
606 + */
607 + public function __call( $func, $args ) {
608 + $f = $func;
609 +
610 + switch ( $func ) {
611 + case 'get_object_term':
612 + $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term';
613 + break;
614 +
615 + case 'save_translations':
616 + case 'delete_translation':
617 + case 'get_translations':
618 + case 'get_translation':
619 + case 'join_clause':
620 + $o = ( 'post' == $args[0] || $this->is_translated_post_type( $args[0] ) ) ? 'post' : ( 'term' == $args[0] || $this->is_translated_taxonomy( $args[0] ) ? 'term' : false );
621 + unset( $args[0] );
622 + break;
623 +
624 + case 'set_post_language':
625 + case 'get_post_language':
626 + case 'set_term_language':
627 + case 'get_term_language':
628 + case 'delete_term_language':
629 + case 'get_post':
630 + case 'get_term':
631 + $str = explode( '_', $func );
632 + $f = empty( $str[2] ) ? $str[0] : $str[0] . '_' . $str[2];
633 + $o = $str[1];
634 + break;
635 +
636 + case 'where_clause':
637 + case 'get_objects_in_language':
638 + $o = $args[1];
639 + unset( $args[1] );
640 + break;
641 + }
642 +
643 + if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
644 + if ( WP_DEBUG ) {
645 + $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
646 + $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
647 +
648 + trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
649 + sprintf(
650 + '%1$s was called incorrectly in %4$s on line %5$s: the call to $polylang->model->%1$s() has been deprecated in Polylang 1.8, use PLL()->model->%2$s->%3$s() instead.' . "\nError handler",
651 + esc_html( $func ),
652 + esc_html( $o ),
653 + esc_html( $f ),
654 + esc_html( $debug[ $i ]['file'] ),
655 + absint( $debug[ $i ]['line'] )
656 + )
657 + );
658 + }
659 + return call_user_func_array( array( $this->$o, $f ), $args );
660 + }
661 +
662 + $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
663 + trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
664 + sprintf(
665 + 'Call to undefined function PLL()->model->%1$s() in %2$s on line %3$s' . "\nError handler",
666 + esc_html( $func ),
667 + esc_html( $debug[0]['file'] ),
668 + absint( $debug[0]['line'] )
669 + ),
670 + E_USER_ERROR
671 + );
636 672 }
637 673 }