PluginProbe
Polylang / 2.1
Polylang v2.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/model.php +73 -94 2.72.1 View file →
@@ -5,11 +5,11 @@
5 5 *
6 6 * @since 1.2
7 7 */
8 8 class PLL_Model {
9 - public $cache; // Our internal non persistent cache object
9 + public $cache; // our internal non persistent cache object
10 10 public $options;
11 - public $post, $term; // Translated objects models
11 + public $post, $term; // translated objects models
12 12
13 13 /**
14 14 * Constructor
15 15 * setups translated objects sub models
@@ -25,17 +25,15 @@
25 25 $this->cache = new PLL_Cache();
26 26 $this->post = new PLL_Translated_Post( $this ); // translated post sub model
27 27 $this->term = new PLL_Translated_Term( $this ); // translated term sub model
28 28
29 - // We need to clean languages cache when editing a language and when modifying the permalink structure
29 + // we need to clean languages cache when editing a language and when modifying the permalink structure
30 30 add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
31 31 add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
32 32 add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
33 33 add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
34 34
35 - add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
36 -
37 - // Just in case someone would like to display the language description ;- )
35 + // just in case someone would like to display the language description ;- )
38 36 add_filter( 'language_description', '__return_empty_string' );
39 37 }
40 38
41 39 /**
@@ -42,9 +40,9 @@
42 40 * Returns the list of available languages
43 41 * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false
44 42 * caches the list ( with flags ) in the private property $languages
45 43 *
46 - * List of parameters accepted in $args:
44 + * list of parameters accepted in $args:
47 45 *
48 46 * hide_empty => hides languages with no posts if set to true ( defaults to false )
49 47 * fields => return only that field if set ( see PLL_Language for a list of fields )
50 48 *
@@ -55,9 +53,9 @@
55 53 */
56 54 public function get_languages_list( $args = array() ) {
57 55 if ( false === $languages = $this->cache->get( 'languages' ) ) {
58 56
59 - // Create the languages from taxonomies
57 + // create the languages from taxonomies
60 58 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
61 59 $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
62 60 $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
63 61
@@ -65,14 +63,14 @@
65 63 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
66 64 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
67 65
68 66 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
67 + // don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
70 68 foreach ( $languages as $k => $v ) {
71 69 $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
72 70 }
73 71
74 - // We will need the languages list to allow its access in the filter below
72 + // we will need the languages list to allow its access in the filter below
75 73 $this->cache->set( 'languages', $languages );
76 74
77 75 /**
78 76 * Filter the list of languages *before* it is stored in the persistent cache
@@ -84,19 +82,19 @@
84 82 * @param object $model PLL_Model object
85 83 */
86 84 $languages = apply_filters( 'pll_languages_list', $languages, $this );
87 85
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;
86 + // don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache
87 + // thanks to captin411 for catching this!
88 + // see https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255;
91 89 set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
92 90 }
93 91 else {
94 - $languages = array(); // In case something went wrong
92 + $languages = array(); // in case something went wrong
95 93 }
96 94 }
97 95
98 - // Create the languages directly from arrays stored in transients
96 + // create the languages directly from arrays stored in transients
99 97 else {
100 98 foreach ( $languages as $k => $v ) {
101 99 $languages[ $k ] = new PLL_Language( $v );
102 100 }
@@ -101,9 +99,9 @@
101 99 $languages[ $k ] = new PLL_Language( $v );
102 100 }
103 101 }
104 102
105 - // Custom flags
103 + // custom flags
106 104 if ( ! PLL_ADMIN ) {
107 105 foreach ( $languages as $language ) {
108 106 $language->set_custom_flag();
109 107 }
@@ -122,9 +120,9 @@
122 120 }
123 121
124 122 $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
125 123
126 - // Remove empty languages if requested
124 + // remove empty languages if requested
127 125 if ( $args['hide_empty'] ) {
128 126 $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
129 127 }
130 128
@@ -148,28 +146,13 @@
148 146 }
149 147 }
150 148
151 149 /**
152 - * Don't query term metas when only our taxonomies are queried
153 - *
154 - * @since 2.3
155 - *
156 - * @param array $args WP_Term_Query arguments
157 - * @return array
158 - */
159 - public function get_terms_args( $args ) {
160 - if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], array( 'language', 'term_language', 'post_translations', 'term_translations' ) ) ) {
161 - $args['update_term_meta_cache'] = false;
162 - }
163 - return $args;
164 - }
165 -
166 - /**
167 150 * Returns the language by its term_id, tl_term_id, slug or locale
168 151 *
169 152 * @since 0.1
170 153 *
171 - * @param int|string $value term_id, tl_term_id, slug or locale of the queried language
154 + * @param int|string term_id, tl_term_id, slug or locale of the queried language
172 155 * @return object|bool PLL_Language object, false if no language found
173 156 */
174 157 public function get_language( $value ) {
175 158 if ( is_object( $value ) ) {
@@ -181,9 +164,8 @@
181 164 $this->cache->set( 'language:' . $lang->term_id, $lang );
182 165 $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
183 166 $this->cache->set( 'language:' . $lang->slug, $lang );
184 167 $this->cache->set( 'language:' . $lang->locale, $lang );
185 - $this->cache->set( 'language:' . $lang->w3c, $lang );
186 168 }
187 169 $return = $this->cache->get( 'language:' . $value );
188 170 }
189 171
@@ -196,9 +178,9 @@
196 178 * @since 1.2
197 179 *
198 180 * @param array $clauses the list of sql clauses in terms query
199 181 * @param object $lang PLL_Language object
200 - * @return array modified list of clauses
182 + * @return array modifed list of clauses
201 183 */
202 184 public function terms_clauses( $clauses, $lang ) {
203 185 if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
204 186 $clauses['join'] .= $this->term->join_clause();
@@ -218,9 +200,9 @@
218 200 * @return array post type names for which Polylang manages languages and translations
219 201 */
220 202 public function get_translated_post_types( $filter = true ) {
221 203 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
222 - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
204 + $post_types = array( 'post' => 'post', 'page' => 'page' );
223 205
224 206 if ( ! empty( $this->options['media_support'] ) ) {
225 207 $post_types['attachment'] = 'attachment';
226 208 }
@@ -225,9 +207,9 @@
225 207 $post_types['attachment'] = 'attachment';
226 208 }
227 209
228 210 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'] ) );
211 + $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
230 212 }
231 213
232 214 /**
233 215 * Filter the list of post types available for translation.
@@ -259,9 +241,9 @@
259 241 * @return bool
260 242 */
261 243 public function is_translated_post_type( $post_type ) {
262 244 $post_types = $this->get_translated_post_types( false );
263 - return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) || 'any' === $post_type && ! empty( $post_types ) );
245 + return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) );
264 246 }
265 247
266 248 /**
267 249 * Return taxonomies that need to be translated
@@ -267,9 +249,9 @@
267 249 * Return taxonomies that need to be translated
268 250 *
269 251 * @since 1.2
270 252 *
271 - * @param bool $filter true if we should return only valid registered taxonomies
253 + * @param bool $filter true if we should return only valid registered taxonmies
272 254 * @return array array of registered taxonomy names for which Polylang manages languages and translations
273 255 */
274 256 public function get_translated_taxonomies( $filter = true ) {
275 257 if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
@@ -312,8 +294,33 @@
312 294 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
313 295 }
314 296
315 297 /**
298 + * Check if translated taxonomy is queried
299 + * Compatible with nested queries introduced in WP 4.1
300 + * @see https://wordpress.org/support/topic/tax_query-bug
301 + *
302 + * @since 1.7
303 + *
304 + * @param array $tax_queries
305 + * @return bool
306 + */
307 + public function have_translated_taxonomy( $tax_queries ) {
308 + foreach ( $tax_queries as $tax_query ) {
309 + if ( isset( $tax_query['taxonomy'] ) && $this->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) {
310 + return true;
311 + }
312 +
313 + // Nested queries
314 + elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) {
315 + return true;
316 + }
317 + }
318 +
319 + return false;
320 + }
321 +
322 + /**
316 323 * Return taxonomies that need to be filtered ( post_format like )
317 324 *
318 325 * @since 1.7
319 326 *
@@ -385,9 +392,9 @@
385 392 $lang = $this->get_language( $lang );
386 393
387 394 // create a new category
388 395 // FIXME this is translated in admin language when we would like it in $lang
389 - $cat_name = __( 'Uncategorized', 'polylang' );
396 + $cat_name = __( 'Uncategorized' );
390 397 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
391 398 $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) );
392 399
393 400 // check that the category was not previously created ( in case the language was deleted and recreated )
@@ -427,9 +434,8 @@
427 434 public function term_exists( $term_name, $taxonomy, $parent, $language ) {
428 435 global $wpdb;
429 436
430 437 $term_name = trim( wp_unslash( $term_name ) );
431 - $term_name = _wp_specialchars( $term_name );
432 438
433 439 $select = "SELECT t.term_id FROM $wpdb->terms AS t";
434 440 $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
435 441 $join .= $this->term->join_clause();
@@ -439,25 +445,24 @@
439 445 if ( $parent > 0 ) {
440 446 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
441 447 }
442 448
443 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
444 449 return $wpdb->get_var( $select . $join . $where );
445 450 }
446 451
447 452 /**
448 - * Gets the number of posts per language in a date, author or post type archive.
453 + * Gets the number of posts per language in a date, author or post type archive
449 454 *
450 455 * @since 1.2
451 456 *
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 ).
457 + * @param object $lang
458 + * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format )
454 459 * @return int
455 460 */
456 461 public function count_posts( $lang, $q = array() ) {
457 462 global $wpdb;
458 463
459 - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
464 + $q = wp_parse_args( $q, array( 'post_type' => 'post' ) );
460 465
461 466 if ( ! is_array( $q['post_type'] ) ) {
462 467 $q['post_type'] = array( $q['post_type'] );
463 468 }
@@ -468,19 +473,19 @@
468 473 }
469 474 }
470 475
471 476 if ( empty( $q['post_type'] ) ) {
472 - $q['post_type'] = array( 'post' ); // We *need* a post type.
477 + $q['post_type'] = array( 'post' ); // we *need* a post type
473 478 }
474 479
475 - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
476 - $counts = wp_cache_get( $cache_key, 'counts' );
480 + $cache_key = md5( serialize( $q ) );
481 + $counts = wp_cache_get( $cache_key, 'pll_count_posts' );
477 482
478 483 if ( false === $counts ) {
479 484 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
480 485 $join = $this->post->join_clause();
481 - $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'] ) ) );
486 + $where = " WHERE post_status = 'publish'";
487 + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", $q['post_type'] ) );
483 488 $where .= $this->post->where_clause( $this->get_languages_list() );
484 489 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
485 490
486 491 if ( ! empty( $q['m'] ) ) {
@@ -506,9 +511,9 @@
506 511 $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", $q['day'] );
507 512 }
508 513
509 514 if ( ! empty( $q['author_name'] ) ) {
510 - $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
515 + $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
511 516 if ( $author ) {
512 517 $q['author'] = $author->ID;
513 518 }
514 519 }
@@ -516,9 +521,9 @@
516 521 if ( ! empty( $q['author'] ) ) {
517 522 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
518 523 }
519 524
520 - // Filtered taxonomies ( post_format ).
525 + // filtered taxonomies ( post_format )
521 526 foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
522 527
523 528 if ( ! empty( $q[ $tax_qv ] ) ) {
524 529 $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
@@ -527,15 +532,14 @@
527 532 $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] );
528 533 }
529 534 }
530 535
531 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
532 536 $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A );
533 537 foreach ( (array) $res as $row ) {
534 538 $counts[ $row['term_taxonomy_id'] ] = $row['num_posts'];
535 539 }
536 540
537 - wp_cache_set( $cache_key, $counts, 'counts' );
541 + wp_cache_set( $cache_key, $counts, 'pll_count_posts' );
538 542 }
539 543
540 544 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
541 545 }
@@ -549,19 +553,8 @@
549 553 */
550 554 public function get_links_model() {
551 555 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
552 556 $class = get_option( 'permalink_structure' ) ? 'PLL_Links_' . $c[ $this->options['force_lang'] ] : 'PLL_Links_Default';
553 -
554 - /**
555 - * Filter the links model class to use
556 - * /!\ this filter is fired *before* the $polylang object is available
557 - *
558 - * @since 2.1.1
559 - *
560 - * @param string $class A class name: PLL_Links_Default, PLL_Links_Directory, PLL_Links_Subdomain, PLL_Links_Domain
561 - */
562 - $class = apply_filters( 'pll_links_model', $class );
563 -
564 557 return new $class( $this );
565 558 }
566 559
567 560 /**
@@ -570,10 +563,10 @@
570 563 * this works but should be slower than the direct call, thus an error is triggered in debug mode
571 564 *
572 565 * @since 1.8
573 566 *
574 - * @param string $func Function name
575 - * @param array $args Function arguments
567 + * @param string $func function name
568 + * @param array $args function arguments
576 569 */
577 570 public function __call( $func, $args ) {
578 571 $f = $func;
579 572
@@ -578,10 +571,10 @@
578 571 $f = $func;
579 572
580 573 switch ( $func ) {
581 574 case 'get_object_term':
582 - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term';
583 - break;
575 + $o = false === strpos( $args[1], 'term' ) ? 'post' : 'term';
576 + break;
584 577
585 578 case 'save_translations':
586 579 case 'delete_translation':
587 580 case 'get_translations':
@@ -588,9 +581,9 @@
588 581 case 'get_translation':
589 582 case 'join_clause':
590 583 $o = ( 'post' == $args[0] || $this->is_translated_post_type( $args[0] ) ) ? 'post' : ( 'term' == $args[0] || $this->is_translated_taxonomy( $args[0] ) ? 'term' : false );
591 584 unset( $args[0] );
592 - break;
585 + break;
593 586
594 587 case 'set_post_language':
595 588 case 'get_post_language':
596 589 case 'set_term_language':
@@ -600,44 +593,30 @@
600 593 case 'get_term':
601 594 $str = explode( '_', $func );
602 595 $f = empty( $str[2] ) ? $str[0] : $str[0] . '_' . $str[2];
603 596 $o = $str[1];
604 - break;
597 + break;
605 598
606 599 case 'where_clause':
607 600 case 'get_objects_in_language':
608 601 $o = $args[1];
609 602 unset( $args[1] );
610 - break;
603 + break;
611 604 }
612 605
613 606 if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
614 607 if ( WP_DEBUG ) {
615 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
608 + $debug = debug_backtrace();
616 609 $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
617 610
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 - );
611 + trigger_error( sprintf(
612 + '%1$s was called incorrectly in %4$s on line %5$s: the call to $polylang->model->%1$s() has been deprecated in Polylang 1.8, use PLL()->model->%2$s->%3$s() instead.' . "\nError handler",
613 + $func, $o, $f, $debug[ $i ]['file'], $debug[ $i ]['line']
614 + ) );
628 615 }
629 616 return call_user_func_array( array( $this->$o, $f ), $args );
630 617 }
631 618
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 - );
619 + $debug = debug_backtrace();
620 + trigger_error( sprintf( 'Call to undefined function PLL()->model->%1$s() in %2$s on line %3$s' . "\nError handler", $func, $debug[0]['file'], $debug[0]['line'] ), E_USER_ERROR );
642 621 }
643 622 }