PluginProbe
Polylang / 2.0.3
Polylang v2.0.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 +79 -94 2.7.32.0.3 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
@@ -141,8 +139,14 @@
141 139 * @param int $term not used
142 140 * @param string $taxonomy taxonomy name
143 141 */
144 142 public function clean_languages_cache( $term = 0, $taxonomy = null ) {
143 + // depending on WP version, the action is passed an object or a string
144 + // backward compatibility with WP < 4.2
145 + if ( ! empty( $taxonomy ) && is_object( $taxonomy ) ) {
146 + $taxonomy = $taxonomy->name;
147 + }
148 +
145 149 if ( empty( $taxonomy ) || 'language' == $taxonomy ) {
146 150 delete_transient( 'pll_languages_list' );
147 151 $this->cache->clean();
148 152 }
@@ -148,28 +152,13 @@
148 152 }
149 153 }
150 154
151 155 /**
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 156 * Returns the language by its term_id, tl_term_id, slug or locale
168 157 *
169 158 * @since 0.1
170 159 *
171 - * @param int|string $value term_id, tl_term_id, slug or locale of the queried language
160 + * @param int|string term_id, tl_term_id, slug or locale of the queried language
172 161 * @return object|bool PLL_Language object, false if no language found
173 162 */
174 163 public function get_language( $value ) {
175 164 if ( is_object( $value ) ) {
@@ -181,9 +170,8 @@
181 170 $this->cache->set( 'language:' . $lang->term_id, $lang );
182 171 $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
183 172 $this->cache->set( 'language:' . $lang->slug, $lang );
184 173 $this->cache->set( 'language:' . $lang->locale, $lang );
185 - $this->cache->set( 'language:' . $lang->w3c, $lang );
186 174 }
187 175 $return = $this->cache->get( 'language:' . $value );
188 176 }
189 177
@@ -196,9 +184,9 @@
196 184 * @since 1.2
197 185 *
198 186 * @param array $clauses the list of sql clauses in terms query
199 187 * @param object $lang PLL_Language object
200 - * @return array modified list of clauses
188 + * @return array modifed list of clauses
201 189 */
202 190 public function terms_clauses( $clauses, $lang ) {
203 191 if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
204 192 $clauses['join'] .= $this->term->join_clause();
@@ -218,9 +206,9 @@
218 206 * @return array post type names for which Polylang manages languages and translations
219 207 */
220 208 public function get_translated_post_types( $filter = true ) {
221 209 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
222 - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
210 + $post_types = array( 'post' => 'post', 'page' => 'page' );
223 211
224 212 if ( ! empty( $this->options['media_support'] ) ) {
225 213 $post_types['attachment'] = 'attachment';
226 214 }
@@ -225,9 +213,9 @@
225 213 $post_types['attachment'] = 'attachment';
226 214 }
227 215
228 216 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'] ) );
217 + $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
230 218 }
231 219
232 220 /**
233 221 * Filter the list of post types available for translation.
@@ -259,9 +247,9 @@
259 247 * @return bool
260 248 */
261 249 public function is_translated_post_type( $post_type ) {
262 250 $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 ) );
251 + return ( is_array( $post_type ) && array_intersect( $post_type, $post_types ) || in_array( $post_type, $post_types ) );
264 252 }
265 253
266 254 /**
267 255 * Return taxonomies that need to be translated
@@ -267,9 +255,9 @@
267 255 * Return taxonomies that need to be translated
268 256 *
269 257 * @since 1.2
270 258 *
271 - * @param bool $filter true if we should return only valid registered taxonomies
259 + * @param bool $filter true if we should return only valid registered taxonmies
272 260 * @return array array of registered taxonomy names for which Polylang manages languages and translations
273 261 */
274 262 public function get_translated_taxonomies( $filter = true ) {
275 263 if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
@@ -312,8 +300,33 @@
312 300 return ( is_array( $tax ) && array_intersect( $tax, $taxonomies ) || in_array( $tax, $taxonomies ) );
313 301 }
314 302
315 303 /**
304 + * Check if translated taxonomy is queried
305 + * Compatible with nested queries introduced in WP 4.1
306 + * @see https://wordpress.org/support/topic/tax_query-bug
307 + *
308 + * @since 1.7
309 + *
310 + * @param array $tax_queries
311 + * @return bool
312 + */
313 + public function have_translated_taxonomy( $tax_queries ) {
314 + foreach ( $tax_queries as $tax_query ) {
315 + if ( isset( $tax_query['taxonomy'] ) && $this->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) {
316 + return true;
317 + }
318 +
319 + // Nested queries
320 + elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) {
321 + return true;
322 + }
323 + }
324 +
325 + return false;
326 + }
327 +
328 + /**
316 329 * Return taxonomies that need to be filtered ( post_format like )
317 330 *
318 331 * @since 1.7
319 332 *
@@ -385,9 +398,9 @@
385 398 $lang = $this->get_language( $lang );
386 399
387 400 // create a new category
388 401 // FIXME this is translated in admin language when we would like it in $lang
389 - $cat_name = __( 'Uncategorized', 'polylang' );
402 + $cat_name = __( 'Uncategorized' );
390 403 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
391 404 $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) );
392 405
393 406 // check that the category was not previously created ( in case the language was deleted and recreated )
@@ -427,9 +440,8 @@
427 440 public function term_exists( $term_name, $taxonomy, $parent, $language ) {
428 441 global $wpdb;
429 442
430 443 $term_name = trim( wp_unslash( $term_name ) );
431 - $term_name = _wp_specialchars( $term_name );
432 444
433 445 $select = "SELECT t.term_id FROM $wpdb->terms AS t";
434 446 $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
435 447 $join .= $this->term->join_clause();
@@ -439,25 +451,24 @@
439 451 if ( $parent > 0 ) {
440 452 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
441 453 }
442 454
443 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
444 455 return $wpdb->get_var( $select . $join . $where );
445 456 }
446 457
447 458 /**
448 - * Gets the number of posts per language in a date, author or post type archive.
459 + * Gets the number of posts per language in a date, author or post type archive
449 460 *
450 461 * @since 1.2
451 462 *
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 ).
463 + * @param object $lang
464 + * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format )
454 465 * @return int
455 466 */
456 467 public function count_posts( $lang, $q = array() ) {
457 468 global $wpdb;
458 469
459 - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
470 + $q = wp_parse_args( $q, array( 'post_type' => 'post' ) );
460 471
461 472 if ( ! is_array( $q['post_type'] ) ) {
462 473 $q['post_type'] = array( $q['post_type'] );
463 474 }
@@ -468,19 +479,19 @@
468 479 }
469 480 }
470 481
471 482 if ( empty( $q['post_type'] ) ) {
472 - $q['post_type'] = array( 'post' ); // We *need* a post type.
483 + $q['post_type'] = array( 'post' ); // we *need* a post type
473 484 }
474 485
475 - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
476 - $counts = wp_cache_get( $cache_key, 'counts' );
486 + $cache_key = md5( serialize( $q ) );
487 + $counts = wp_cache_get( $cache_key, 'pll_count_posts' );
477 488
478 489 if ( false === $counts ) {
479 490 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
480 491 $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'] ) ) );
492 + $where = " WHERE post_status = 'publish'";
493 + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", $q['post_type'] ) );
483 494 $where .= $this->post->where_clause( $this->get_languages_list() );
484 495 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
485 496
486 497 if ( ! empty( $q['m'] ) ) {
@@ -506,9 +517,9 @@
506 517 $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", $q['day'] );
507 518 }
508 519
509 520 if ( ! empty( $q['author_name'] ) ) {
510 - $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
521 + $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
511 522 if ( $author ) {
512 523 $q['author'] = $author->ID;
513 524 }
514 525 }
@@ -516,9 +527,9 @@
516 527 if ( ! empty( $q['author'] ) ) {
517 528 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
518 529 }
519 530
520 - // Filtered taxonomies ( post_format ).
531 + // filtered taxonomies ( post_format )
521 532 foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
522 533
523 534 if ( ! empty( $q[ $tax_qv ] ) ) {
524 535 $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
@@ -527,15 +538,14 @@
527 538 $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] );
528 539 }
529 540 }
530 541
531 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
532 542 $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A );
533 543 foreach ( (array) $res as $row ) {
534 544 $counts[ $row['term_taxonomy_id'] ] = $row['num_posts'];
535 545 }
536 546
537 - wp_cache_set( $cache_key, $counts, 'counts' );
547 + wp_cache_set( $cache_key, $counts, 'pll_count_posts' );
538 548 }
539 549
540 550 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
541 551 }
@@ -549,19 +559,8 @@
549 559 */
550 560 public function get_links_model() {
551 561 $c = array( 'Directory', 'Directory', 'Subdomain', 'Domain' );
552 562 $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 563 return new $class( $this );
565 564 }
566 565
567 566 /**
@@ -570,10 +569,10 @@
570 569 * this works but should be slower than the direct call, thus an error is triggered in debug mode
571 570 *
572 571 * @since 1.8
573 572 *
574 - * @param string $func Function name
575 - * @param array $args Function arguments
573 + * @param string $func function name
574 + * @param array $args function arguments
576 575 */
577 576 public function __call( $func, $args ) {
578 577 $f = $func;
579 578
@@ -578,10 +577,10 @@
578 577 $f = $func;
579 578
580 579 switch ( $func ) {
581 580 case 'get_object_term':
582 - $o = ( false === strpos( $args[1], 'term' ) ) ? 'post' : 'term';
583 - break;
581 + $o = false === strpos( $args[1], 'term' ) ? 'post' : 'term';
582 + break;
584 583
585 584 case 'save_translations':
586 585 case 'delete_translation':
587 586 case 'get_translations':
@@ -588,9 +587,9 @@
588 587 case 'get_translation':
589 588 case 'join_clause':
590 589 $o = ( 'post' == $args[0] || $this->is_translated_post_type( $args[0] ) ) ? 'post' : ( 'term' == $args[0] || $this->is_translated_taxonomy( $args[0] ) ? 'term' : false );
591 590 unset( $args[0] );
592 - break;
591 + break;
593 592
594 593 case 'set_post_language':
595 594 case 'get_post_language':
596 595 case 'set_term_language':
@@ -600,44 +599,30 @@
600 599 case 'get_term':
601 600 $str = explode( '_', $func );
602 601 $f = empty( $str[2] ) ? $str[0] : $str[0] . '_' . $str[2];
603 602 $o = $str[1];
604 - break;
603 + break;
605 604
606 605 case 'where_clause':
607 606 case 'get_objects_in_language':
608 607 $o = $args[1];
609 608 unset( $args[1] );
610 - break;
609 + break;
611 610 }
612 611
613 612 if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
614 613 if ( WP_DEBUG ) {
615 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
614 + $debug = debug_backtrace();
616 615 $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
617 616
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 - );
617 + trigger_error( sprintf(
618 + '%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",
619 + $func, $o, $f, $debug[ $i ]['file'], $debug[ $i ]['line']
620 + ) );
628 621 }
629 622 return call_user_func_array( array( $this->$o, $f ), $args );
630 623 }
631 624
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 - );
625 + $debug = debug_backtrace();
626 + 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 627 }
643 628 }