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