| @@ -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,9 +25,9 @@ | ||
| 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' ) ); |
| @@ -33,9 +33,9 @@ | ||
| 33 | 33 | add_action( 'update_option_home', array( $this, 'clean_languages_cache' ) ); |
| 34 | 34 | |
| 35 | 35 | add_filter( 'get_terms_args', array( $this, 'get_terms_args' ) ); |
| 36 | 36 | |
| 37 | - // Just in case someone would like to display the language description ;- ) | |
| 37 | + // just in case someone would like to display the language description ;- ) | |
| 38 | 38 | add_filter( 'language_description', '__return_empty_string' ); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
| @@ -42,9 +42,9 @@ | ||
| 42 | 42 | * Returns the list of available languages |
| 43 | 43 | * caches the list in a db transient ( except flags ), unless PLL_CACHE_LANGUAGES is set to false |
| 44 | 44 | * caches the list ( with flags ) in the private property $languages |
| 45 | 45 | * |
| 46 | - * List of parameters accepted in $args: | |
| 46 | + * list of parameters accepted in $args: | |
| 47 | 47 | * |
| 48 | 48 | * hide_empty => hides languages with no posts if set to true ( defaults to false ) |
| 49 | 49 | * fields => return only that field if set ( see PLL_Language for a list of fields ) |
| 50 | 50 | * |
| @@ -55,9 +55,9 @@ | ||
| 55 | 55 | */ |
| 56 | 56 | public function get_languages_list( $args = array() ) { |
| 57 | 57 | if ( false === $languages = $this->cache->get( 'languages' ) ) { |
| 58 | 58 | |
| 59 | - // Create the languages from taxonomies | |
| 59 | + // create the languages from taxonomies | |
| 60 | 60 | if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || false === ( $languages = get_transient( 'pll_languages_list' ) ) ) { |
| 61 | 61 | $languages = get_terms( 'language', array( 'hide_empty' => false, 'orderby' => 'term_group' ) ); |
| 62 | 62 | $languages = empty( $languages ) || is_wp_error( $languages ) ? array() : $languages; |
| 63 | 63 | |
| @@ -65,14 +65,14 @@ | ||
| 65 | 65 | $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ? |
| 66 | 66 | array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages ); |
| 67 | 67 | |
| 68 | 68 | 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 | |
| 69 | + // don't use array_map + create_function to instantiate an autoloaded class as it breaks badly in old versions of PHP | |
| 70 | 70 | foreach ( $languages as $k => $v ) { |
| 71 | 71 | $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] ); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - // 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 | |
| 75 | 75 | $this->cache->set( 'languages', $languages ); |
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * Filter the list of languages *before* it is stored in the persistent cache |
| @@ -84,19 +84,19 @@ | ||
| 84 | 84 | * @param object $model PLL_Model object |
| 85 | 85 | */ |
| 86 | 86 | $languages = apply_filters( 'pll_languages_list', $languages, $this ); |
| 87 | 87 | |
| 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; | |
| 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; | |
| 91 | 91 | set_transient( 'pll_languages_list', array_map( 'get_object_vars', $languages ) ); |
| 92 | 92 | } |
| 93 | 93 | else { |
| 94 | - $languages = array(); // In case something went wrong | |
| 94 | + $languages = array(); // in case something went wrong | |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - // Create the languages directly from arrays stored in transients | |
| 98 | + // create the languages directly from arrays stored in transients | |
| 99 | 99 | else { |
| 100 | 100 | foreach ( $languages as $k => $v ) { |
| 101 | 101 | $languages[ $k ] = new PLL_Language( $v ); |
| 102 | 102 | } |
| @@ -101,9 +101,9 @@ | ||
| 101 | 101 | $languages[ $k ] = new PLL_Language( $v ); |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - // Custom flags | |
| 105 | + // custom flags | |
| 106 | 106 | if ( ! PLL_ADMIN ) { |
| 107 | 107 | foreach ( $languages as $language ) { |
| 108 | 108 | $language->set_custom_flag(); |
| 109 | 109 | } |
| @@ -122,9 +122,9 @@ | ||
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | $args = wp_parse_args( $args, array( 'hide_empty' => false ) ); |
| 125 | 125 | |
| 126 | - // Remove empty languages if requested | |
| 126 | + // remove empty languages if requested | |
| 127 | 127 | if ( $args['hide_empty'] ) { |
| 128 | 128 | $languages = wp_list_filter( $languages, array( 'count' => 0 ), 'NOT' ); |
| 129 | 129 | } |
| 130 | 130 | |
| @@ -181,9 +181,8 @@ | ||
| 181 | 181 | $this->cache->set( 'language:' . $lang->term_id, $lang ); |
| 182 | 182 | $this->cache->set( 'language:' . $lang->tl_term_id, $lang ); |
| 183 | 183 | $this->cache->set( 'language:' . $lang->slug, $lang ); |
| 184 | 184 | $this->cache->set( 'language:' . $lang->locale, $lang ); |
| 185 | - $this->cache->set( 'language:' . $lang->w3c, $lang ); | |
| 186 | 185 | } |
| 187 | 186 | $return = $this->cache->get( 'language:' . $value ); |
| 188 | 187 | } |
| 189 | 188 | |
| @@ -218,9 +217,9 @@ | ||
| 218 | 217 | * @return array post type names for which Polylang manages languages and translations |
| 219 | 218 | */ |
| 220 | 219 | public function get_translated_post_types( $filter = true ) { |
| 221 | 220 | if ( false === $post_types = $this->cache->get( 'post_types' ) ) { |
| 222 | - $post_types = array( 'post' => 'post', 'page' => 'page', 'wp_block' => 'wp_block' ); | |
| 221 | + $post_types = array( 'post' => 'post', 'page' => 'page' ); | |
| 223 | 222 | |
| 224 | 223 | if ( ! empty( $this->options['media_support'] ) ) { |
| 225 | 224 | $post_types['attachment'] = 'attachment'; |
| 226 | 225 | } |
| @@ -385,9 +384,9 @@ | ||
| 385 | 384 | $lang = $this->get_language( $lang ); |
| 386 | 385 | |
| 387 | 386 | // create a new category |
| 388 | 387 | // FIXME this is translated in admin language when we would like it in $lang |
| 389 | - $cat_name = __( 'Uncategorized', 'polylang' ); | |
| 388 | + $cat_name = __( 'Uncategorized' ); | |
| 390 | 389 | $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug ); |
| 391 | 390 | $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) ); |
| 392 | 391 | |
| 393 | 392 | // check that the category was not previously created ( in case the language was deleted and recreated ) |
| @@ -427,9 +426,8 @@ | ||
| 427 | 426 | public function term_exists( $term_name, $taxonomy, $parent, $language ) { |
| 428 | 427 | global $wpdb; |
| 429 | 428 | |
| 430 | 429 | $term_name = trim( wp_unslash( $term_name ) ); |
| 431 | - $term_name = _wp_specialchars( $term_name ); | |
| 432 | 430 | |
| 433 | 431 | $select = "SELECT t.term_id FROM $wpdb->terms AS t"; |
| 434 | 432 | $join = " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; |
| 435 | 433 | $join .= $this->term->join_clause(); |
| @@ -439,25 +437,24 @@ | ||
| 439 | 437 | if ( $parent > 0 ) { |
| 440 | 438 | $where .= $wpdb->prepare( ' AND tt.parent = %d', $parent ); |
| 441 | 439 | } |
| 442 | 440 | |
| 443 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 444 | 441 | return $wpdb->get_var( $select . $join . $where ); |
| 445 | 442 | } |
| 446 | 443 | |
| 447 | 444 | /** |
| 448 | - * Gets the number of posts per language in a date, author or post type archive. | |
| 445 | + * Gets the number of posts per language in a date, author or post type archive | |
| 449 | 446 | * |
| 450 | 447 | * @since 1.2 |
| 451 | 448 | * |
| 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 ). | |
| 449 | + * @param object $lang | |
| 450 | + * @param array $q WP_Query arguments ( accepted: post_type, m, year, monthnum, day, author, author_name, post_format ) | |
| 454 | 451 | * @return int |
| 455 | 452 | */ |
| 456 | 453 | public function count_posts( $lang, $q = array() ) { |
| 457 | 454 | global $wpdb; |
| 458 | 455 | |
| 459 | - $q = wp_parse_args( $q, array( 'post_type' => 'post', 'post_status' => 'publish' ) ); | |
| 456 | + $q = wp_parse_args( $q, array( 'post_type' => 'post' ) ); | |
| 460 | 457 | |
| 461 | 458 | if ( ! is_array( $q['post_type'] ) ) { |
| 462 | 459 | $q['post_type'] = array( $q['post_type'] ); |
| 463 | 460 | } |
| @@ -468,18 +465,18 @@ | ||
| 468 | 465 | } |
| 469 | 466 | } |
| 470 | 467 | |
| 471 | 468 | if ( empty( $q['post_type'] ) ) { |
| 472 | - $q['post_type'] = array( 'post' ); // We *need* a post type. | |
| 469 | + $q['post_type'] = array( 'post' ); // we *need* a post type | |
| 473 | 470 | } |
| 474 | 471 | |
| 475 | - $cache_key = 'pll_count_posts_' . md5( maybe_serialize( $q ) ); | |
| 476 | - $counts = wp_cache_get( $cache_key, 'counts' ); | |
| 472 | + $cache_key = md5( serialize( $q ) ); | |
| 473 | + $counts = wp_cache_get( $cache_key, 'pll_count_posts' ); | |
| 477 | 474 | |
| 478 | 475 | if ( false === $counts ) { |
| 479 | 476 | $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS num_posts FROM {$wpdb->posts}"; |
| 480 | 477 | $join = $this->post->join_clause(); |
| 481 | - $where = sprintf( " WHERE post_status = '%s'", esc_sql( $q['post_status'] ) ); | |
| 478 | + $where = " WHERE post_status = 'publish'"; | |
| 482 | 479 | $where .= sprintf( " AND {$wpdb->posts}.post_type IN ( '%s' )", join( "', '", esc_sql( $q['post_type'] ) ) ); |
| 483 | 480 | $where .= $this->post->where_clause( $this->get_languages_list() ); |
| 484 | 481 | $groupby = ' GROUP BY pll_tr.term_taxonomy_id'; |
| 485 | 482 | |
| @@ -516,9 +513,9 @@ | ||
| 516 | 513 | if ( ! empty( $q['author'] ) ) { |
| 517 | 514 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $q['author'] ); |
| 518 | 515 | } |
| 519 | 516 | |
| 520 | - // Filtered taxonomies ( post_format ). | |
| 517 | + // filtered taxonomies ( post_format ) | |
| 521 | 518 | foreach ( $this->get_filtered_taxonomies_query_vars() as $tax_qv ) { |
| 522 | 519 | |
| 523 | 520 | if ( ! empty( $q[ $tax_qv ] ) ) { |
| 524 | 521 | $join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = {$wpdb->posts}.ID"; |
| @@ -527,15 +524,14 @@ | ||
| 527 | 524 | $where .= $wpdb->prepare( ' AND t.slug = %s', $q[ $tax_qv ] ); |
| 528 | 525 | } |
| 529 | 526 | } |
| 530 | 527 | |
| 531 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 532 | 528 | $res = $wpdb->get_results( $select . $join . $where . $groupby, ARRAY_A ); |
| 533 | 529 | foreach ( (array) $res as $row ) { |
| 534 | 530 | $counts[ $row['term_taxonomy_id'] ] = $row['num_posts']; |
| 535 | 531 | } |
| 536 | 532 | |
| 537 | - wp_cache_set( $cache_key, $counts, 'counts' ); | |
| 533 | + wp_cache_set( $cache_key, $counts, 'pll_count_posts' ); | |
| 538 | 534 | } |
| 539 | 535 | |
| 540 | 536 | return empty( $counts[ $lang->term_taxonomy_id ] ) ? 0 : $counts[ $lang->term_taxonomy_id ]; |
| 541 | 537 | } |
| @@ -611,33 +607,19 @@ | ||
| 611 | 607 | } |
| 612 | 608 | |
| 613 | 609 | if ( ! empty( $o ) && is_object( $this->$o ) && method_exists( $this->$o, $f ) ) { |
| 614 | 610 | if ( WP_DEBUG ) { |
| 615 | - $debug = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 611 | + $debug = debug_backtrace(); | |
| 616 | 612 | $i = 1 + empty( $debug[1]['line'] ); // the file and line are in $debug[2] if the function was called using call_user_func |
| 617 | 613 | |
| 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 | - ); | |
| 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 | + ) ); | |
| 628 | 618 | } |
| 629 | 619 | return call_user_func_array( array( $this->$o, $f ), $args ); |
| 630 | 620 | } |
| 631 | 621 | |
| 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 | - ); | |
| 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 ); | |
| 642 | 624 | } |
| 643 | 625 | } |