PluginProbe
Polylang / 2.2
Polylang v2.2
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 +46 -111 2.82.2 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Setups the language and translations model based on WordPress taxonomies
8 5 *
@@ -8,11 +5,11 @@
8 5 *
9 6 * @since 1.2
10 7 */
11 8 class PLL_Model {
12 - public $cache; // Our internal non persistent cache object
9 + public $cache; // our internal non persistent cache object
13 10 public $options;
14 - public $post, $term; // Translated objects models
11 + public $post, $term; // translated objects models
15 12
16 13 /**
17 14 * Constructor
18 15 * setups translated objects sub models
@@ -28,17 +25,15 @@
28 25 $this->cache = new PLL_Cache();
29 26 $this->post = new PLL_Translated_Post( $this ); // translated post sub model
30 27 $this->term = new PLL_Translated_Term( $this ); // translated term sub model
31 28
32 - // 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
33 30 add_action( 'edited_term_taxonomy', array( $this, 'clean_languages_cache' ), 10, 2 );
34 31 add_action( 'update_option_permalink_structure', array( $this, 'clean_languages_cache' ) );
35 32 add_action( 'update_option_siteurl', array( $this, 'clean_languages_cache' ) );
36 33 add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
37 34
38 - add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
39 -
40 - // Just in case someone would like to display the language description ;- )
35 + // just in case someone would like to display the language description ;- )
41 36 add_filter( 'language_description', '__return_empty_string' );
42 37 }
43 38
44 39 /**
@@ -45,9 +40,9 @@
45 40 * Returns the list of available languages
46 41 * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false
47 42 * caches the list ( with flags ) in the private property $languages
48 43 *
49 - * List of parameters accepted in $args:
44 + * list of parameters accepted in $args:
50 45 *
51 46 * hide_empty => hides languages with no posts if set to true ( defaults to false )
52 47 * fields => return only that field if set ( see PLL_Language for a list of fields )
53 48 *
@@ -58,9 +53,9 @@
58 53 */
59 54 public function get_languages_list( $args = array() ) {
60 55 if ( false === $languages = $this->cache->get( 'languages' ) ) {
61 56
62 - // Create the languages from taxonomies
57 + // create the languages from taxonomies
63 58 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
64 59 $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
65 60 $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
66 61
@@ -68,14 +63,14 @@
68 63 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
69 64 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
70 65
71 66 if ( ! empty( $languages ) && ! empty( $term_languages ) ) {
72 - // Don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
67 + // don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
73 68 foreach ( $languages as $k => $v ) {
74 69 $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
75 70 }
76 71
77 - // 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
78 73 $this->cache->set( 'languages', $languages );
79 74
80 75 /**
81 76 * Filter the list of languages *before* it is stored in the persistent cache
@@ -87,19 +82,19 @@
87 82 * @param object $model PLL_Model object
88 83 */
89 84 $languages = apply_filters( 'pll_languages_list', $languages, $this );
90 85
91 - // Don't store directly objects as it badly break with some hosts ( GoDaddy ) due to race conditions when using object cache
92 - // Thanks to captin411 for catching this!
93 - // See https://wordpress.org/support/topic/fatal-error-pll_model_languages_list?replies=8#post-6782255;
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;
94 89 set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
95 90 }
96 91 else {
97 - $languages = array(); // In case something went wrong
92 + $languages = array(); // in case something went wrong
98 93 }
99 94 }
100 95
101 - // Create the languages directly from arrays stored in transients
96 + // create the languages directly from arrays stored in transients
102 97 else {
103 98 foreach ( $languages as $k => $v ) {
104 99 $languages[ $k ] = new PLL_Language( $v );
105 100 }
@@ -104,8 +99,15 @@
104 99 $languages[ $k ] = new PLL_Language( $v );
105 100 }
106 101 }
107 102
103 + // custom flags
104 + if ( ! PLL_ADMIN ) {
105 + foreach ( $languages as $language ) {
106 + $language->set_custom_flag();
107 + }
108 + }
109 +
108 110 /**
109 111 * Filter the list of languages *after* it is stored in the persistent cache
110 112 * /!\ this filter is fired *before* the $polylang object is available
111 113 *
@@ -118,9 +120,9 @@
118 120 }
119 121
120 122 $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
121 123
122 - // Remove empty languages if requested
124 + // remove empty languages if requested
123 125 if ( $args['hide_empty'] ) {
124 126 $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
125 127 }
126 128
@@ -144,23 +146,8 @@
144 146 }
145 147 }
146 148
147 149 /**
148 - * Don't query term metas when only our taxonomies are queried
149 - *
150 - * @since 2.3
151 - *
152 - * @param array $args WP_Term_Query arguments
153 - * @return array
154 - */
155 - public function get_terms_args( $args ) {
156 - if ( isset( $args['taxonomy'] ) && ! array_diff( (array) $args['taxonomy'], array( 'language', 'term_language', 'post_translations', 'term_translations' ) ) ) {
157 - $args['update_term_meta_cache'] = false;
158 - }
159 - return $args;
160 - }
161 -
162 - /**
163 150 * Returns the language by its term_id, tl_term_id, slug or locale
164 151 *
165 152 * @since 0.1
166 153 *
@@ -177,9 +164,8 @@
177 164 $this->cache->set( 'language:' . $lang->term_id, $lang );
178 165 $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
179 166 $this->cache->set( 'language:' . $lang->slug, $lang );
180 167 $this->cache->set( 'language:' . $lang->locale, $lang );
181 - $this->cache->set( 'language:' . $lang->w3c, $lang );
182 168 }
183 169 $return = $this->cache->get( 'language:' . $value );
184 170 }
185 171
@@ -192,9 +178,9 @@
192 178 * @since 1.2
193 179 *
194 180 * @param array $clauses the list of sql clauses in terms query
195 181 * @param object $lang PLL_Language object
196 - * @return array modified list of clauses
182 + * @return array modifed list of clauses
197 183 */
198 184 public function terms_clauses( $clauses, $lang ) {
199 185 if ( ! empty( $lang ) && false === strpos( $clauses['join'], 'pll_tr' ) ) {
200 186 $clauses['join'] .= $this->term->join_clause();
@@ -214,9 +200,9 @@
214 200 * @return array post type names for which Polylang manages languages and translations
215 201 */
216 202 public function get_translated_post_types( $filter = true ) {
217 203 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
218 - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
204 + $post_types = array( 'post' => 'post', 'page' => 'page' );
219 205
220 206 if ( ! empty( $this->options['media_support'] ) ) {
221 207 $post_types['attachment'] = 'attachment';
222 208 }
@@ -221,9 +207,9 @@
221 207 $post_types['attachment'] = 'attachment';
222 208 }
223 209
224 210 if ( ! empty( $this->options['post_types'] ) && is_array( $this->options['post_types'] ) ) {
225 - $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
211 + $post_types = array_merge( $post_types, array_combine( $this->options['post_types'], $this->options['post_types'] ) );
226 212 }
227 213
228 214 /**
229 215 * Filter the list of post types available for translation.
@@ -255,9 +241,9 @@
255 241 * @return bool
256 242 */
257 243 public function is_translated_post_type( $post_type ) {
258 244 $post_types = $this->get_translated_post_types( false );
259 - 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 ) );
260 246 }
261 247
262 248 /**
263 249 * Return taxonomies that need to be translated
@@ -263,9 +249,9 @@
263 249 * Return taxonomies that need to be translated
264 250 *
265 251 * @since 1.2
266 252 *
267 - * @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
268 254 * @return array array of registered taxonomy names for which Polylang manages languages and translations
269 255 */
270 256 public function get_translated_taxonomies( $filter = true ) {
271 257 if ( false === $taxonomies = $this->cache->get( 'taxonomies' ) ) {
@@ -381,9 +367,9 @@
381 367 $lang = $this->get_language( $lang );
382 368
383 369 // create a new category
384 370 // FIXME this is translated in admin language when we would like it in $lang
385 - $cat_name = __( 'Uncategorized', 'polylang' );
371 + $cat_name = __( 'Uncategorized' );
386 372 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
387 373 $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) );
388 374
389 375 // check that the category was not previously created ( in case the language was deleted and recreated )
@@ -423,9 +409,8 @@
423 409 public function term_exists( $term_name, $taxonomy, $parent, $language ) {
424 410 global $wpdb;
425 411
426 412 $term_name = trim( wp_unslash( $term_name ) );
427 - $term_name = _wp_specialchars( $term_name );
428 413
429 414 $select = "SELECT t.term_id FROM $wpdb->terms AS t";
430 415 $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
431 416 $join .= $this->term->join_clause();
@@ -435,59 +420,24 @@
435 420 if ( $parent > 0 ) {
436 421 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
437 422 }
438 423
439 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
440 424 return $wpdb->get_var( $select . $join . $where );
441 425 }
442 426
443 427 /**
444 - * Checks if a term slug exists in a given language, taxonomy, hierarchy
428 + * Gets the number of posts per language in a date, author or post type archive
445 429 *
446 - * @since 1.9
447 - * @since 2.8 Moved from PLL_Share_Term_Slug::term_exists() to PLL_Model::term_exists_by_slug()
448 - *
449 - * @param string $slug The term slug to test.
450 - * @param string|object $language The language slug or object.
451 - * @param string $taxonomy Optional taxonomy name.
452 - * @param int $parent Optional parent term id.
453 - * @return null|int The term_id of the found term.
454 - */
455 - public function term_exists_by_slug( $slug, $language, $taxonomy = '', $parent = 0 ) {
456 - global $wpdb;
457 -
458 - $select = "SELECT t.term_id FROM {$wpdb->terms} AS t";
459 - $join = " INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id";
460 - $join .= $this->term->join_clause();
461 - $where = $wpdb->prepare( ' WHERE t.slug = %s', $slug );
462 - $where .= $this->term->where_clause( $this->get_language( $language ) );
463 -
464 - if ( ! empty( $taxonomy ) ) {
465 - $where .= $wpdb->prepare( ' AND tt.taxonomy = %s', $taxonomy );
466 - }
467 -
468 - if ( $parent > 0 ) {
469 - $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
470 - }
471 -
472 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
473 - return $wpdb->get_var( $select . $join . $where );
474 - }
475 -
476 -
477 - /**
478 - * Gets the number of posts per language in a date, author or post type archive.
479 - *
480 430 * @since 1.2
481 431 *
482 - * @param object $lang PLL_Language instance.
483 - * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ).
432 + * @param object $lang
433 + * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format )
484 434 * @return int
485 435 */
486 436 public function count_posts( $lang, $q = array() ) {
487 437 global $wpdb;
488 438
489 - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
439 + $q = wp_parse_args( $q, array( 'post_type' => 'post' ) );
490 440
491 441 if ( ! is_array( $q['post_type'] ) ) {
492 442 $q['post_type'] = array( $q['post_type'] );
493 443 }
@@ -498,19 +448,19 @@
498 448 }
499 449 }
500 450
501 451 if ( empty( $q['post_type'] ) ) {
502 - $q['post_type'] = array( 'post' ); // We *need* a post type.
452 + $q['post_type'] = array( 'post' ); // we *need* a post type
503 453 }
504 454
505 - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
506 - $counts = wp_cache_get( $cache_key, 'counts' );
455 + $cache_key = md5( serialize( $q ) );
456 + $counts = wp_cache_get( $cache_key, 'pll_count_posts' );
507 457
508 458 if ( false === $counts ) {
509 459 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
510 460 $join = $this->post->join_clause();
511 - $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
512 - $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", esc_sql( $q['post_type'] ) ) );
461 + $where = " WHERE post_status = 'publish'";
462 + $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", $q['post_type'] ) );
513 463 $where .= $this->post->where_clause( $this->get_languages_list() );
514 464 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
515 465
516 466 if ( ! empty( $q['m'] ) ) {
@@ -536,9 +486,9 @@
536 486 $where .= $wpdb->prepare( " AND DAYOFMONTH( {$wpdb->posts}.post_date ) = %d", $q['day'] );
537 487 }
538 488
539 489 if ( ! empty( $q['author_name'] ) ) {
540 - $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
490 + $author = get_user_by( 'slug', sanitize_title_for_query( $q['author_name'] ) );
541 491 if ( $author ) {
542 492 $q['author'] = $author->ID;
543 493 }
544 494 }
@@ -546,9 +496,9 @@
546 496 if ( ! empty( $q['author'] ) ) {
547 497 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
548 498 }
549 499
550 - // Filtered taxonomies ( post_format ).
500 + // filtered taxonomies ( post_format )
551 501 foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
552 502
553 503 if ( ! empty( $q[ $tax_qv ] ) ) {
554 504 $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
@@ -557,15 +507,14 @@
557 507 $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] );
558 508 }
559 509 }
560 510
561 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
562 511 $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A );
563 512 foreach ( (array) $res as $row ) {
564 513 $counts[ $row['term_taxonomy_id'] ] = $row['num_posts'];
565 514 }
566 515
567 - wp_cache_set( $cache_key, $counts, 'counts' );
516 + wp_cache_set( $cache_key, $counts, 'pll_count_posts' );
568 517 }
569 518
570 519 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
571 520 }
@@ -641,33 +590,19 @@
641 590 }
642 591
643 592 if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
644 593 if ( WP_DEBUG ) {
645 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
594 + $debug = debug_backtrace();
646 595 $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
647 596
648 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
649 - sprintf(
650 - '%1$s was called incorrectly in %4$s on line %5$s: the call to $polylang->model->%1$s() has been deprecated in Polylang 1.8, use PLL()->model->%2$s->%3$s() instead.' . "\nError handler",
651 - esc_html( $func ),
652 - esc_html( $o ),
653 - esc_html( $f ),
654 - esc_html( $debug[ $i ]['file'] ),
655 - absint( $debug[ $i ]['line'] )
656 - )
657 - );
597 + trigger_error( sprintf(
598 + '%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",
599 + $func, $o, $f, $debug[ $i ]['file'], $debug[ $i ]['line']
600 + ) );
658 601 }
659 602 return call_user_func_array( array( $this->$o, $f ), $args );
660 603 }
661 604
662 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
663 - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions
664 - sprintf(
665 - 'Call to undefined function PLL()->model->%1$s() in %2$s on line %3$s' . "\nError handler",
666 - esc_html( $func ),
667 - esc_html( $debug[0]['file'] ),
668 - absint( $debug[0]['line'] )
669 - ),
670 - E_USER_ERROR
671 - );
605 + $debug = debug_backtrace();
606 + 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 );
672 607 }
673 608 }