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