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