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