PluginProbe
Polylang / 3.1.3
Polylang v3.1.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
polylang / include / model.php

model.php in Polylang 3.1.3, at include/model.php

706 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Setups the language and translations model based on WordPress taxonomies
8 *
9 * @since 1.2
10 */
11 class PLL_Model {
12 /**
13 * Internal non persistent cache object.
14 *
15 * @var PLL_Cache
16 */
17 public $cache;
18
19 /**
20 * Stores the plugin options.
21 *
22 * @var array
23 */
24 public $options;
25
26 /**
27 * Translated post model.
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 *
45 * @since 1.2
46 *
47 * @param array $options Polylang options.
48 */
49 public function __construct( &$options ) {
50 $this->options = &$options;
51
52 $this->cache = new PLL_Cache();
53 $this->post = new PLL_Translated_Post( $this ); // Translated post sub model.
54 $this->term = new PLL_Translated_Term( $this ); // Translated term sub model.
55
56 // We need to clean languages cache when editing a language and when modifying the permalink structure.
57 add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
58 add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
59 add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
60 add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
61
62 add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
63
64 // Just in case someone would like to display the language description ;).
65 add_filter( 'language_description', '__return_empty_string' );
66 }
67
68 /**
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.
72 *
73 * @since 0.1
74 *
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.
80 */
81 public function get_languages_list( $args = array() ) {
82 if ( false === $languages = $this->cache->get( 'languages' ) ) {
83
84 // Create the languages from taxonomies.
85 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
86 $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
87 $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
88
89 $term_languages = get_terms( 'term_language', array( 'hide_empty' => false ) );
90 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
91 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
92
93 if ( ! empty( $languages ) && ! empty( $term_languages ) ) {
94 foreach ( $languages as $k => $v ) {
95 $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
96 }
97
98 // We will need the languages list to allow its access in the filter below.
99 $this->cache->set( 'languages', $languages );
100
101 /**
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.
104 *
105 * @since 1.7.5
106 *
107 * @param PLL_Language[] $languages The list of language objects.
108 * @param PLL_Model $model PLL_Model object.
109 */
110 $languages = apply_filters( 'pll_languages_list', $languages, $this );
111
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 */
117 set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
118 }
119 else {
120 $languages = array(); // In case something went wrong.
121 }
122 }
123
124 // Create the languages directly from arrays stored in transients.
125 else {
126 foreach ( $languages as $k => $v ) {
127 $languages[ $k ] = new PLL_Language( $v );
128 }
129 }
130
131 /**
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.
134 *
135 * @since 1.8
136 *
137 * @param PLL_Language[] $languages The list of language objects.
138 */
139 $languages = apply_filters( 'pll_after_languages_cache', $languages );
140 $this->cache->set( 'languages', $languages );
141 }
142
143 $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
144
145 // Remove empty languages if requested.
146 if ( $args['hide_empty'] ) {
147 $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
148 }
149
150 return empty( $args['fields'] ) ? $languages : wp_list_pluck( $languages, $args['fields'] );
151 }
152
153 /**
154 * Cleans language cache
155 * can be called directly with no parameter
156 * called by the 'edited_term_taxonomy' filter with 2 parameters when count needs to be updated
157 *
158 * @since 1.2
159 *
160 * @param int $term not used
161 * @param string $taxonomy taxonomy name
162 * @return void
163 */
164 public function clean_languages_cache( $term = 0, $taxonomy = null ) {
165 if ( empty( $taxonomy ) || 'language' == $taxonomy ) {
166 delete_transient( 'pll_languages_list' );
167 $this->cache->clean();
168 }
169 }
170
171 /**
172 * Don't query term metas when only our taxonomies are queried
173 *
174 * @since 2.3
175 *
176 * @param array $args WP_Term_Query arguments
177 * @return array
178 */
179 public function get_terms_args( $args ) {
180 if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], array( 'language', 'term_language', 'post_translations', 'term_translations' ) ) ) {
181 $args['update_term_meta_cache'] = false;
182 }
183 return $args;
184 }
185
186 /**
187 * Returns the language by its term_id, tl_term_id, slug or locale.
188 *
189 * @since 0.1
190 *
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.
193 */
194 public function get_language( $value ) {
195 if ( is_object( $value ) ) {
196 return $value instanceof PLL_Language ? $value : $this->get_language( $value->term_id ); // will force cast to PLL_Language
197 }
198
199 if ( false === $return = $this->cache->get( 'language:' . $value ) ) {
200 foreach ( $this->get_languages_list() as $lang ) {
201 $this->cache->set( 'language:' . $lang->term_id, $lang );
202 $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
203 $this->cache->set( 'language:' . $lang->slug, $lang );
204 $this->cache->set( 'language:' . $lang->locale, $lang );
205 $this->cache->set( 'language:' . $lang->w3c, $lang );
206 }
207 $return = $this->cache->get( 'language:' . $value );
208 }
209
210 return $return;
211 }
212
213 /**
214 * Adds terms clauses to the term query to filter them by languages.
215 *
216 * @since 1.2
217 *
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.
221 */
222 public function terms_clauses( $clauses, $lang ) {
223 if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
224 $clauses['join'] .= $this->term->join_clause();
225 $clauses['where'] .= $this->term->where_clause( $lang );
226 }
227 return $clauses;
228 }
229
230 /**
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.
235 *
236 * @since 1.2
237 *
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.
240 */
241 public function get_translated_post_types( $filter = true ) {
242 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
243 $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
244
245 if ( ! empty( $this->options['media_support'] ) ) {
246 $post_types['attachment'] = 'attachment';
247 }
248
249 if ( ! empty( $this->options['post_types'] ) && is_array( $this->options['post_types'] ) ) {
250 $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
251 }
252
253 /**
254 * Filters the list of post types available for translation.
255 * The default are post types which have the parameter ‘public’ set to true.
256 * The filter must be added soon in the WordPress loading process:
257 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
258 *
259 * @since 0.8
260 *
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.
263 */
264 $post_types = apply_filters( 'pll_get_post_types', $post_types, false );
265
266 if ( did_action( 'after_setup_theme' ) ) {
267 $this->cache->set( 'post_types', $post_types );
268 }
269 }
270
271 return $filter ? array_intersect( $post_types, get_post_types() ) : $post_types;
272 }
273
274 /**
275 * Returns true if Polylang manages languages and translations for this post type.
276 *
277 * @since 1.2
278 *
279 * @param string|string[] $post_type Post type name or array of post type names.
280 * @return bool
281 */
282 public function is_translated_post_type( $post_type ) {
283 $post_types = $this->get_translated_post_types( false );
284 return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) );
285 }
286
287 /**
288 * Returns taxonomies that need to be translated.
289 *
290 * @since 1.2
291 *
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.
294 */
295 public function get_translated_taxonomies( $filter = true ) {
296 if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
297 $taxonomies = array( 'category' => 'category', 'post_tag' => 'post_tag' );
298
299 if ( ! empty( $this->options['taxonomies'] ) && is_array( $this->options['taxonomies'] ) ) {
300 $taxonomies = array_merge( $taxonomies, array_combine( $this->options['taxonomies'], $this->options['taxonomies'] ) );
301 }
302
303 /**
304 * Filters the list of taxonomies available for translation.
305 * The default are taxonomies which have the parameter ‘public’ set to true.
306 * The filter must be added soon in the WordPress loading process:
307 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
308 *
309 * @since 0.8
310 *
311 * @param string[] $taxonomies List of taxonomy names.
312 * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings.
313 */
314 $taxonomies = apply_filters( 'pll_get_taxonomies', $taxonomies, false );
315 if ( did_action( 'after_setup_theme' ) ) {
316 $this->cache->set( 'taxonomies', $taxonomies );
317 }
318 }
319
320 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
321 }
322
323 /**
324 * Returns true if Polylang manages languages and translations for this taxonomy.
325 *
326 * @since 1.2
327 *
328 * @param string|string[] $tax Taxonomy name or array of taxonomy names.
329 * @return bool
330 */
331 public function is_translated_taxonomy( $tax ) {
332 $taxonomies = $this->get_translated_taxonomies( false );
333 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
334 }
335
336 /**
337 * Return staxonomies that need to be filtered ( post_format like ).
338 *
339 * @since 1.7
340 *
341 * @param bool $filter True if we should return only valid registered taxonomies.
342 * @return string[] Array of registered taxonomy names.
343 */
344 public function get_filtered_taxonomies( $filter = true ) {
345 if ( did_action( 'after_setup_theme' ) ) {
346 static $taxonomies = null;
347 }
348
349 if ( empty( $taxonomies ) ) {
350 $taxonomies = array( 'post_format' => 'post_format' );
351
352 /**
353 * Filters the list of taxonomies not translatable but filtered by language.
354 * Includes only the post format by default
355 * The filter must be added soon in the WordPress loading process:
356 * in a function hooked to ‘plugins_loaded’ or directly in functions.php for themes.
357 *
358 * @since 1.7
359 *
360 * @param string[] $taxonomies List of taxonomy names.
361 * @param bool $is_settings True when displaying the list of custom taxonomies in Polylang settings.
362 */
363 $taxonomies = apply_filters( 'pll_filtered_taxonomies', $taxonomies, false );
364 }
365
366 return $filter ? array_intersect( $taxonomies, get_taxonomies() ) : $taxonomies;
367 }
368
369 /**
370 * Returns true if Polylang filters this taxonomy per language.
371 *
372 * @since 1.7
373 *
374 * @param string|string[] $tax Taxonomy name or array of taxonomy names.
375 * @return bool
376 */
377 public function is_filtered_taxonomy( $tax ) {
378 $taxonomies = $this->get_filtered_taxonomies( false );
379 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
380 }
381
382 /**
383 * Returns the query vars of all filtered taxonomies.
384 *
385 * @since 1.7
386 *
387 * @return array
388 */
389 public function get_filtered_taxonomies_query_vars() {
390 $query_vars = array();
391 foreach ( $this->get_filtered_taxonomies() as $filtered_tax ) {
392 $tax = get_taxonomy( $filtered_tax );
393 if ( ! empty( $tax ) ) {
394 $query_vars[] = $tax->query_var;
395 }
396 }
397 return $query_vars;
398 }
399
400 /**
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.
404 *
405 * @since 1.4
406 *
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.
412 */
413 public function term_exists( $term_name, $taxonomy, $parent, $language ) {
414 global $wpdb;
415
416 $term_name = trim( wp_unslash( $term_name ) );
417 $term_name = _wp_specialchars( $term_name );
418
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 ) );
424
425 if ( $parent > 0 ) {
426 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
427 }
428
429 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
430 return $wpdb->get_var( $select . $join . $where );
431 }
432
433 /**
434 * Checks if a term slug exists in a given language, taxonomy, hierarchy.
435 *
436 * @since 1.9
437 * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug().
438 *
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.
444 */
445 public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) {
446 global $wpdb;
447
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 ) );
453
454 if ( ! empty( $taxonomy ) ) {
455 $where .= $wpdb->prepare( ' AND tt.taxonomy = %s', $taxonomy );
456 }
457
458 if ( $parent > 0 ) {
459 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
460 }
461
462 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
463 return $wpdb->get_var( $select . $join . $where );
464 }
465
466
467 /**
468 * Gets the number of posts per language in a date, author or post type archive.
469 *
470 * @since 1.2
471 *
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 * }
486 * @return int
487 */
488 public function count_posts( $lang, $q = array() ) {
489 global $wpdb;
490
491 $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
492
493 if ( ! is_array( $q['post_type'] ) ) {
494 $q['post_type'] = array( $q['post_type'] );
495 }
496
497 foreach ( $q['post_type'] as $key => $type ) {
498 if ( ! post_type_exists( $type ) ) {
499 unset( $q['post_type'][ $key ] );
500 }
501 }
502
503 if ( empty( $q['post_type'] ) ) {
504 $q['post_type'] = array( 'post' ); // We *need* a post type.
505 }
506
507 $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
508 $counts = wp_cache_get( $cache_key, 'counts' );
509
510 if ( false === $counts ) {
511 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
512 $join = $this->post->join_clause();
513 $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
514 $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", implode( "', '", esc_sql( $q['post_type'] ) ) );
515 $where .= $this->post->where_clause( $this->get_languages_list() );
516 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
517
518 if ( ! empty( $q['m'] ) ) {
519 $q['m'] = '' . preg_replace( '|[^0-9]|', '', $q['m'] );
520 $where .= $wpdb->prepare( " AND YEAR( {$wpdb->posts}.post_date ) = %d", substr( $q['m'], 0, 4 ) );
521 if ( strlen( $q['m'] ) > 5 ) {
522 $where .= $wpdb->prepare( " AND MONTH( {$wpdb->posts}.post_date ) = %d", substr( $q['m'], 4, 2 ) );
523 }
524 if ( strlen( $q['m'] ) > 7 ) {
525 $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", substr( $q['m'], 6, 2 ) );
526 }
527 }
528
529 if ( ! empty( $q['year'] ) ) {
530 $where .= $wpdb->prepare( " AND YEAR( {$wpdb->posts}.post_date ) = %d", $q['year'] );
531 }
532
533 if ( ! empty( $q['monthnum'] ) ) {
534 $where .= $wpdb->prepare( " AND MONTH( {$wpdb->posts}.post_date ) = %d", $q['monthnum'] );
535 }
536
537 if ( ! empty( $q['day'] ) ) {
538 $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", $q['day'] );
539 }
540
541 if ( ! empty( $q['author_name'] ) ) {
542 $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
543 if ( $author ) {
544 $q['author'] = $author->ID;
545 }
546 }
547
548 if ( ! empty( $q['author'] ) ) {
549 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
550 }
551
552 // Filtered taxonomies ( post_format ).
553 foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
554
555 if ( ! empty( $q[ $tax_qv ] ) ) {
556 $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
557 $join .= " INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
558 $join .= " INNER JOIN {$wpdb->terms} AS t ON t.term_id = tt.term_id";
559 $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] );
560 }
561 }
562
563 // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
564 $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A );
565 foreach ( (array) $res as $row ) {
566 $counts[ $row['term_taxonomy_id'] ] = $row['num_posts'];
567 }
568
569 wp_cache_set( $cache_key, $counts, 'counts' );
570 }
571
572 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
573 }
574
575 /**
576 * Setup the links model based on options.
577 *
578 * @since 1.2
579 *
580 * @return PLL_Links_Model
581 */
582 public function get_links_model() {
583 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
584 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
585
586 /**
587 * Filters the links model class to use.
588 * /!\ this filter is fired *before* the $polylang object is available.
589 *
590 * @since 2.1.1
591 *
592 * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain.
593 */
594 $class = apply_filters( 'pll_links_model', $class );
595
596 return new $class( $this );
597 }
598
599 /**
600 * Returns posts and terms ids without language ( used in settings ).
601 *
602 * @since 0.9
603 * @since 2.2.6 Add the $limit argument.
604 *
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 * }
612 */
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 );
624
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 );
627
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 }
637
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 }
665
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;
677
678 $taxonomies = (array) $taxonomies;
679
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' );
701 }
702
703 return $term_ids;
704 }
705 }
706