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 +355 -201 2.73.3 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,70 +60,89 @@
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 + * Checks if there are languages or not.
45 70 *
46 - * List of parameters accepted in $args:
71 + * @since 3.3
47 72 *
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 )
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.
50 91 *
51 92 * @since 0.1
52 93 *
53 - * @param array $args
54 - * @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.
55 99 */
56 100 public function get_languages_list( $args = array() ) {
57 101 if ( false === $languages = $this->cache->get( 'languages' ) ) {
102 + $languages = array();
58 103
59 - // Create the languages from taxonomies
104 + // Create the languages from taxonomies.
60 105 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
61 - $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
62 - $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
106 + $languages = array();
63 107
64 - $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 ) );
65 111 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
66 112 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
67 113
68 - 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 - foreach ( $languages as $k => $v ) {
71 - $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 + }
72 119 }
73 120
74 - // 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.
75 122 $this->cache->set( 'languages', $languages );
76 123
77 124 /**
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
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.
80 127 *
81 128 * @since 1.7.5
82 129 *
83 - * @param array $languages the list of language objects
84 - * @param object $model PLL_Model object
130 + * @param PLL_Language[] $languages The list of language objects.
131 + * @param PLL_Model $model PLL_Model object.
85 132 */
86 133 $languages = apply_filters( 'pll_languages_list', $languages, $this );
87 134
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;
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 + */
91 140 set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
92 141 }
93 - else {
94 - $languages = array(); // In case something went wrong
95 - }
96 142 }
97 143
98 - // Create the languages directly from arrays stored in transients
144 + // Create the languages directly from arrays stored in transients.
99 145 else {
100 146 foreach ( $languages as $k => $v ) {
101 147 $languages[ $k ] = new PLL_Language( $v );
102 148 }
@@ -101,22 +147,15 @@
101 147 $languages[ $k ] = new PLL_Language( $v );
102 148 }
103 149 }
104 150
105 - // Custom flags
106 - if ( ! PLL_ADMIN ) {
107 - foreach ( $languages as $language ) {
108 - $language->set_custom_flag();
109 - }
110 - }
111 -
112 151 /**
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
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.
115 154 *
116 155 * @since 1.8
117 156 *
118 - * @param array $languages the list of language objects
157 + * @param PLL_Language[] $languages The list of language objects.
119 158 */
120 159 $languages = apply_filters( 'pll_after_languages_cache', $languages );
121 160 $this->cache->set( 'languages', $languages );
122 161 }
@@ -122,9 +161,9 @@
122 161 }
123 162
124 163 $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
125 164
126 - // Remove empty languages if requested
165 + // Remove empty languages if requested.
127 166 if ( $args['hide_empty'] ) {
128 167 $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
129 168 }
130 169
@@ -139,8 +178,9 @@
139 178 * @since 1.2
140 179 *
141 180 * @param int $term not used
142 181 * @param string $taxonomy taxonomy name
182 + * @return void
143 183 */
144 184 public function clean_languages_cache( $term = 0, $taxonomy = null ) {
145 185 if ( empty( $taxonomy ) || 'language' == $taxonomy ) {
146 186 delete_transient( 'pll_languages_list' );
@@ -163,14 +203,14 @@
163 203 return $args;
164 204 }
165 205
166 206 /**
167 - * 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.
168 208 *
169 209 * @since 0.1
170 210 *
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
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.
173 213 */
174 214 public function get_language( $value ) {
175 215 if ( is_object( $value ) ) {
176 216 return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language
@@ -190,15 +230,15 @@
190 230 return $return;
191 231 }
192 232
193 233 /**
194 - * 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.
195 235 *
196 236 * @since 1.2
197 237 *
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
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.
201 241 */
202 242 public function terms_clauses( $clauses, $lang ) {
203 243 if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
204 244 $clauses['join'] .= $this->term->join_clause();
@@ -207,31 +247,34 @@
207 247 return $clauses;
208 248 }
209 249
210 250 /**
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
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.
214 255 *
215 256 * @since 1.2
216 257 *
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
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.
219 260 */
220 261 public function get_translated_post_types( $filter = true ) {
221 262 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
222 263 $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
223 264
224 - if ( ! empty( $this->options['media_support'] ) ) {
225 - $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'] ) );
226 267 }
227 268
228 - if ( ! empty( $this->options['post_types'] ) && is_array( $this->options['post_types'] ) ) {
229 - $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';
230 273 }
231 274
232 275 /**
233 - * Filter the list of post types available for translation.
276 + * Filters the list of post types available for translation.
234 277 * The default are post types which have the parameter ‘public’ set to true.
235 278 * The filter must be added soon in the WordPress loading process:
236 279 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
237 280 *
@@ -236,10 +279,10 @@
236 279 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
237 280 *
238 281 * @since 0.8
239 282 *
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
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.
242 285 */
243 286 $post_types = apply_filters( 'pll_get_post_types', $post_types, false );
244 287
245 288 if ( did_action( 'after_setup_theme' ) ) {
@@ -250,13 +293,13 @@
250 293 return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types;
251 294 }
252 295
253 296 /**
254 - * 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.
255 298 *
256 299 * @since 1.2
257 300 *
258 - * @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.
259 302 * @return bool
260 303 */
261 304 public function is_translated_post_type( $post_type ) {
262 305 $post_types = $this->get_translated_post_types( false );
@@ -263,14 +306,14 @@
263 306 return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) );
264 307 }
265 308
266 309 /**
267 - * Return taxonomies that need to be translated
310 + * Returns taxonomies that need to be translated.
268 311 *
269 312 * @since 1.2
270 313 *
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
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.
273 316 */
274 317 public function get_translated_taxonomies( $filter = true ) {
275 318 if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
276 319 $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' );
@@ -279,9 +322,9 @@
279 322 $taxonomies = array_merge( $taxonomies, array_combine( $this->options['taxonomies'], $this->options['taxonomies'] ) );
280 323 }
281 324
282 325 /**
283 - * Filter the list of taxonomies available for translation.
326 + * Filters the list of taxonomies available for translation.
284 327 * The default are taxonomies which have the parameter ‘public’ set to true.
285 328 * The filter must be added soon in the WordPress loading process:
286 329 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
287 330 *
@@ -286,10 +329,10 @@
286 329 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
287 330 *
288 331 * @since 0.8
289 332 *
290 - * @param array $taxonomies list of taxonomy names
291 - * @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.
292 335 */
293 336 $taxonomies = apply_filters( 'pll_get_taxonomies', $taxonomies, false );
294 337 if ( did_action( 'after_setup_theme' ) ) {
295 338 $this->cache->set( 'taxonomies', $taxonomies );
@@ -299,13 +342,13 @@
299 342 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
300 343 }
301 344
302 345 /**
303 - * Returns true if Polylang manages languages and translations for this taxonomy
346 + * Returns true if Polylang manages languages and translations for this taxonomy.
304 347 *
305 348 * @since 1.2
306 349 *
307 - * @param string|array $tax taxonomy name or array of taxonomy names
350 + * @param string|string[] $tax Taxonomy name or array of taxonomy names.
308 351 * @return bool
309 352 */
310 353 public function is_translated_taxonomy( $tax ) {
311 354 $taxonomies = $this->get_translated_taxonomies( false );
@@ -312,14 +355,14 @@
312 355 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
313 356 }
314 357
315 358 /**
316 - * Return taxonomies that need to be filtered ( post_format like )
359 + * Return staxonomies that need to be filtered ( post_format like ).
317 360 *
318 361 * @since 1.7
319 362 *
320 - * @param bool $filter true if we should return only valid registered taxonomies
321 - * @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.
322 365 */
323 366 public function get_filtered_taxonomies( $filter = true ) {
324 367 if ( did_action( 'after_setup_theme' ) ) {
325 368 static $taxonomies = null;
@@ -328,9 +371,9 @@
328 371 if ( empty( $taxonomies ) ) {
329 372 $taxonomies = array( 'post_format' => 'post_format' );
330 373
331 374 /**
332 - * Filter the list of taxonomies not translatable but filtered by language.
375 + * Filters the list of taxonomies not translatable but filtered by language.
333 376 * Includes only the post format by default
334 377 * The filter must be added soon in the WordPress loading process:
335 378 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
336 379 *
@@ -335,10 +378,10 @@
335 378 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
336 379 *
337 380 * @since 1.7
338 381 *
339 - * @param array $taxonomies list of taxonomy names
340 - * @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.
341 384 */
342 385 $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false );
343 386 }
344 387
@@ -345,13 +388,13 @@
345 388 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
346 389 }
347 390
348 391 /**
349 - * Returns true if Polylang filters this taxonomy per language
392 + * Returns true if Polylang filters this taxonomy per language.
350 393 *
351 394 * @since 1.7
352 395 *
353 - * @param string|array $tax taxonomy name or array of taxonomy names
396 + * @param string|string[] $tax Taxonomy name or array of taxonomy names.
354 397 * @return bool
355 398 */
356 399 public function is_filtered_taxonomy( $tax ) {
357 400 $taxonomies = $this->get_filtered_taxonomies( false );
@@ -358,85 +401,93 @@
358 401 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
359 402 }
360 403
361 404 /**
362 - * Returns the query vars of all filtered taxonomies
405 + * Returns the query vars of all filtered taxonomies.
363 406 *
364 407 * @since 1.7
365 408 *
366 - * @return array
409 + * @return string[]
367 410 */
368 411 public function get_filtered_taxonomies_query_vars() {
369 412 $query_vars = array();
370 413 foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) {
371 414 $tax = get_taxonomy( $filtered_tax );
372 - $query_vars[] = $tax->query_var;
415 + if ( ! empty( $tax ) && is_string( $tax->query_var ) ) {
416 + $query_vars[] = $tax->query_var;
417 + }
373 418 }
374 419 return $query_vars;
375 420 }
376 421
377 422 /**
378 - * Create a default category for a language
423 + * It is possible to have several terms with the same name in the same taxonomy ( one per language )
424 + * but the native term_exists() will return true even if only one exists.
425 + * So here the function adds the language parameter.
379 426 *
380 - * @since 1.2
427 + * @since 1.4
381 428 *
382 - * @param object|string|int $lang language
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.
383 434 */
384 - public function create_default_category( $lang ) {
385 - $lang = $this->get_language( $lang );
435 + public function term_exists( $term_name, $taxonomy, $parent, $language ) {
436 + global $wpdb;
386 437
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 ) );
438 + $language = $this->get_language( $language );
439 + if ( empty( $language ) ) {
440 + return 0;
441 + }
392 442
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'];
443 + $term_name = trim( wp_unslash( $term_name ) );
444 + $term_name = _wp_specialchars( $term_name );
395 445
396 - // set language
397 - $this->term->set_language( (int) $cat, $lang );
446 + $select = "SELECT t.term_id FROM $wpdb->terms AS t";
447 + $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
448 + $join .= $this->term->join_clause();
449 + $where = $wpdb->prepare( ' WHERE tt.taxonomy = %s AND t.name = %s', $taxonomy, $term_name );
450 + $where .= $this->term->where_clause( $language );
398 451
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 - }
452 + if ( $parent > 0 ) {
453 + $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
409 454 }
410 455
411 - $this->term->save_translations( (int) $cat, $translations );
456 + // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
457 + return $wpdb->get_var( $select . $join . $where );
412 458 }
413 459
414 460 /**
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
461 + * Checks if a term slug exists in a given language, taxonomy, hierarchy.
418 462 *
419 - * @since 1.4
463 + * @since 1.9
464 + * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug().
420 465 *
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
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.
426 471 */
427 - public function term_exists( $term_name, $taxonomy, $parent, $language ) {
472 + public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) {
428 473 global $wpdb;
429 474
430 - $term_name = trim( wp_unslash( $term_name ) );
431 - $term_name = _wp_specialchars( $term_name );
475 + $language = $this->get_language( $language );
476 + if ( empty( $language ) ) {
477 + return 0;
478 + }
432 479
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 ) );
480 + $select = "SELECT t.term_id FROM {$wpdb->terms} AS t";
481 + $join = " INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id";
482 + $join .= $this->term->join_clause();
483 + $where = $wpdb->prepare( ' WHERE t.slug = %s', $slug );
484 + $where .= $this->term->where_clause( $language );
438 485
486 + if ( ! empty( $taxonomy ) ) {
487 + $where .= $wpdb->prepare( ' AND tt.taxonomy = %s', $taxonomy );
488 + }
489 +
439 490 if ( $parent > 0 ) {
440 491 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
441 492 }
442 493
@@ -443,15 +494,28 @@
443 494 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
444 495 return $wpdb->get_var( $select . $join . $where );
445 496 }
446 497
498 +
447 499 /**
448 500 * Gets the number of posts per language in a date, author or post type archive.
449 501 *
450 502 * @since 1.2
451 503 *
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 ).
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 + * }
454 518 * @return int
455 519 */
456 520 public function count_posts( $lang, $q = array() ) {
457 521 global $wpdb;
@@ -475,12 +539,14 @@
475 539 $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
476 540 $counts = wp_cache_get( $cache_key, 'counts' );
477 541
478 542 if ( false === $counts ) {
543 + $counts = array();
544 +
479 545 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
480 546 $join = $this->post->join_clause();
481 547 $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'] ) ) );
548 + $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", implode( "', '", esc_sql( $q['post_type'] ) ) );
483 549 $where .= $this->post->where_clause( $this->get_languages_list() );
484 550 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
485 551
486 552 if ( ! empty( $q['m'] ) ) {
@@ -540,13 +606,13 @@
540 606 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
541 607 }
542 608
543 609 /**
544 - * Setup the links model based on options
610 + * Setup the links model based on options.
545 611 *
546 612 * @since 1.2
547 613 *
548 - * @return object implementing "links_model interface"
614 + * @return PLL_Links_Model
549 615 */
550 616 public function get_links_model() {
551 617 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
552 618 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
@@ -551,14 +617,14 @@
551 617 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
552 618 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
553 619
554 620 /**
555 - * Filter the links model class to use
556 - * /!\ 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.
557 623 *
558 624 * @since 2.1.1
559 625 *
560 - * @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.
561 627 */
562 628 $class = apply_filters( 'pll_links_model', $class );
563 629
564 630 return new $class( $this );
@@ -564,80 +630,168 @@
564 630 return new $class( $this );
565 631 }
566 632
567 633 /**
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
634 + * Returns posts and terms ids without language ( used in settings ).
571 635 *
572 - * @since 1.8
636 + * @since 0.9
637 + * @since 2.2.6 Add the $limit argument.
573 638 *
574 - * @param string $func Function name
575 - * @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 + * }
576 646 */
577 - public function __call( $func, $args ) {
578 - $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 );
579 658
580 - switch ( $func ) {
581 - case 'get_object_term':
582 - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term';
583 - 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 );
584 661
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;
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 + }
593 671
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;
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 + }
605 686
606 - case 'where_clause':
607 - case 'get_objects_in_language':
608 - $o = $args[1];
609 - unset( $args[1] );
610 - 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.
611 720 }
612 721
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
722 + $taxonomies = (array) $taxonomies;
617 723
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 );
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' );
630 745 }
631 746
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 - );
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;
642 796 }
643 797 }