PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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 +40 -88 2.8.12.3.6 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,9 +25,9 @@
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' ) );
@@ -36,9 +33,9 @@
36 33 add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) );
37 34
38 35 add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) );
39 36
40 - // Just in case someone would like to display the language description ;- )
37 + // just in case someone would like to display the language description ;- )
41 38 add_filter( 'language_description', '__return_empty_string' );
42 39 }
43 40
44 41 /**
@@ -45,9 +42,9 @@
45 42 * Returns the list of available languages
46 43 * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false
47 44 * caches the list ( with flags ) in the private property $languages
48 45 *
49 - * List of parameters accepted in $args:
46 + * list of parameters accepted in $args:
50 47 *
51 48 * hide_empty => hides languages with no posts if set to true ( defaults to false )
52 49 * fields => return only that field if set ( see PLL_Language for a list of fields )
53 50 *
@@ -58,9 +55,9 @@
58 55 */
59 56 public function get_languages_list( $args = array() ) {
60 57 if ( false === $languages = $this->cache->get( 'languages' ) ) {
61 58
62 - // Create the languages from taxonomies
59 + // create the languages from taxonomies
63 60 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) {
64 61 $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) );
65 62 $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages;
66 63
@@ -68,14 +65,14 @@
68 65 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
69 66 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
70 67
71 68 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
69 + // don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP
73 70 foreach ( $languages as $k => $v ) {
74 71 $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
75 72 }
76 73
77 - // We will need the languages list to allow its access in the filter below
74 + // we will need the languages list to allow its access in the filter below
78 75 $this->cache->set( 'languages', $languages );
79 76
80 77 /**
81 78 * Filter the list of languages *before* it is stored in the persistent cache
@@ -87,19 +84,19 @@
87 84 * @param object $model PLL_Model object
88 85 */
89 86 $languages = apply_filters( 'pll_languages_list', $languages, $this );
90 87
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;
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;
94 91 set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) );
95 92 }
96 93 else {
97 - $languages = array(); // In case something went wrong
94 + $languages = array(); // in case something went wrong
98 95 }
99 96 }
100 97
101 - // Create the languages directly from arrays stored in transients
98 + // create the languages directly from arrays stored in transients
102 99 else {
103 100 foreach ( $languages as $k => $v ) {
104 101 $languages[ $k ] = new PLL_Language( $v );
105 102 }
@@ -104,8 +101,15 @@
104 101 $languages[ $k ] = new PLL_Language( $v );
105 102 }
106 103 }
107 104
105 + // custom flags
106 + if ( ! PLL_ADMIN ) {
107 + foreach ( $languages as $language ) {
108 + $language->set_custom_flag();
109 + }
110 + }
111 +
108 112 /**
109 113 * Filter the list of languages *after* it is stored in the persistent cache
110 114 * /!\ this filter is fired *before* the $polylang object is available
111 115 *
@@ -118,9 +122,9 @@
118 122 }
119 123
120 124 $args = wp_parse_args( $args, array( 'hide_empty' => false ) );
121 125
122 - // Remove empty languages if requested
126 + // remove empty languages if requested
123 127 if ( $args['hide_empty'] ) {
124 128 $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' );
125 129 }
126 130
@@ -177,9 +181,8 @@
177 181 $this->cache->set( 'language:' . $lang->term_id, $lang );
178 182 $this->cache->set( 'language:' . $lang->tl_term_id, $lang );
179 183 $this->cache->set( 'language:' . $lang->slug, $lang );
180 184 $this->cache->set( 'language:' . $lang->locale, $lang );
181 - $this->cache->set( 'language:' . $lang->w3c, $lang );
182 185 }
183 186 $return = $this->cache->get( 'language:' . $value );
184 187 }
185 188
@@ -214,9 +217,9 @@
214 217 * @return array post type names for which Polylang manages languages and translations
215 218 */
216 219 public function get_translated_post_types( $filter = true ) {
217 220 if ( false === $post_types = $this->cache->get( 'post_types' ) ) {
218 - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' );
221 + $post_types = array( 'post' => 'post', 'page' => 'page' );
219 222
220 223 if ( ! empty( $this->options['media_support'] ) ) {
221 224 $post_types['attachment'] = 'attachment';
222 225 }
@@ -381,9 +384,9 @@
381 384 $lang = $this->get_language( $lang );
382 385
383 386 // create a new category
384 387 // FIXME this is translated in admin language when we would like it in $lang
385 - $cat_name = __( 'Uncategorized', 'polylang' );
388 + $cat_name = __( 'Uncategorized' );
386 389 $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug );
387 390 $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) );
388 391
389 392 // check that the category was not previously created ( in case the language was deleted and recreated )
@@ -423,9 +426,8 @@
423 426 public function term_exists( $term_name, $taxonomy, $parent, $language ) {
424 427 global $wpdb;
425 428
426 429 $term_name = trim( wp_unslash( $term_name ) );
427 - $term_name = _wp_specialchars( $term_name );
428 430
429 431 $select = "SELECT t.term_id FROM $wpdb->terms AS t";
430 432 $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
431 433 $join .= $this->term->join_clause();
@@ -435,59 +437,24 @@
435 437 if ( $parent > 0 ) {
436 438 $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent );
437 439 }
438 440
439 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
440 441 return $wpdb->get_var( $select . $join . $where );
441 442 }
442 443
443 444 /**
444 - * Checks if a term slug exists in a given language, taxonomy, hierarchy
445 + * Gets the number of posts per language in a date, author or post type archive
445 446 *
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 447 * @since 1.2
481 448 *
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 ).
449 + * @param object $lang
450 + * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format )
484 451 * @return int
485 452 */
486 453 public function count_posts( $lang, $q = array() ) {
487 454 global $wpdb;
488 455
489 - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) );
456 + $q = wp_parse_args( $q, array( 'post_type' => 'post' ) );
490 457
491 458 if ( ! is_array( $q['post_type'] ) ) {
492 459 $q['post_type'] = array( $q['post_type'] );
493 460 }
@@ -498,18 +465,18 @@
498 465 }
499 466 }
500 467
501 468 if ( empty( $q['post_type'] ) ) {
502 - $q['post_type'] = array( 'post' ); // We *need* a post type.
469 + $q['post_type'] = array( 'post' ); // we *need* a post type
503 470 }
504 471
505 - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) );
506 - $counts = wp_cache_get( $cache_key, 'counts' );
472 + $cache_key = md5( serialize( $q ) );
473 + $counts = wp_cache_get( $cache_key, 'pll_count_posts' );
507 474
508 475 if ( false === $counts ) {
509 476 $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}";
510 477 $join = $this->post->join_clause();
511 - $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) );
478 + $where = " WHERE post_status = 'publish'";
512 479 $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", esc_sql( $q['post_type'] ) ) );
513 480 $where .= $this->post->where_clause( $this->get_languages_list() );
514 481 $groupby = ' GROUP BY pll_tr.term_taxonomy_id';
515 482
@@ -546,9 +513,9 @@
546 513 if ( ! empty( $q['author'] ) ) {
547 514 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] );
548 515 }
549 516
550 - // Filtered taxonomies ( post_format ).
517 + // filtered taxonomies ( post_format )
551 518 foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) {
552 519
553 520 if ( ! empty( $q[ $tax_qv ] ) ) {
554 521 $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID";
@@ -557,15 +524,14 @@
557 524 $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] );
558 525 }
559 526 }
560 527
561 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
562 528 $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A );
563 529 foreach ( (array) $res as $row ) {
564 530 $counts[ $row['term_taxonomy_id'] ] = $row['num_posts'];
565 531 }
566 532
567 - wp_cache_set( $cache_key, $counts, 'counts' );
533 + wp_cache_set( $cache_key, $counts, 'pll_count_posts' );
568 534 }
569 535
570 536 return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ];
571 537 }
@@ -641,33 +607,19 @@
641 607 }
642 608
643 609 if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) {
644 610 if ( WP_DEBUG ) {
645 - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
611 + $debug = debug_backtrace();
646 612 $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func
647 613
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 - );
614 + trigger_error( sprintf(
615 + '%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",
616 + $func, $o, $f, $debug[ $i ]['file'], $debug[ $i ]['line']
617 + ) );
658 618 }
659 619 return call_user_func_array( array( $this->$o, $f ), $args );
660 620 }
661 621
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 - );
622 + $debug = debug_backtrace();
623 + 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 624 }
673 625 }