| @@ -398,46 +398,8 @@ | ||
| 398 | 398 | return $query_vars; |
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | /** |
| 402 | - * Create a default category for a language | |
| 403 | - * | |
| 404 | - * @since 1.2 | |
| 405 | - * | |
| 406 | - * @param object|string|int $lang language | |
| 407 | - * @return void | |
| 408 | - */ | |
| 409 | - public function create_default_category( $lang ) { | |
| 410 | - $lang = $this->get_language( $lang ); | |
| 411 | - | |
| 412 | - // create a new category | |
| 413 | - // FIXME this is translated in admin language when we would like it in $lang | |
| 414 | - $cat_name = __( 'Uncategorized', 'polylang' ); | |
| 415 | - $cat_slug = sanitize_title( $cat_name . '-' . $lang->slug ); | |
| 416 | - $cat = wp_insert_term( $cat_name, 'category', array( 'slug' => $cat_slug ) ); | |
| 417 | - | |
| 418 | - // check that the category was not previously created ( in case the language was deleted and recreated ) | |
| 419 | - $cat = isset( $cat->error_data['term_exists'] ) ? $cat->error_data['term_exists'] : $cat['term_id']; | |
| 420 | - | |
| 421 | - // set language | |
| 422 | - $this->term->set_language( (int) $cat, $lang ); | |
| 423 | - | |
| 424 | - // this is a translation of the default category | |
| 425 | - $default = (int) get_option( 'default_category' ); | |
| 426 | - $translations = $this->term->get_translations( $default ); | |
| 427 | - if ( empty( $translations ) ) { | |
| 428 | - if ( $lg = $this->term->get_language( $default ) ) { | |
| 429 | - $translations[ $lg->slug ] = $default; | |
| 430 | - } | |
| 431 | - else { | |
| 432 | - $translations = array(); | |
| 433 | - } | |
| 434 | - } | |
| 435 | - | |
| 436 | - $this->term->save_translations( (int) $cat, $translations ); | |
| 437 | - } | |
| 438 | - | |
| 439 | - /** | |
| 440 | 402 | * It is possible to have several terms with the same name in the same taxonomy ( one per language ) |
| 441 | 403 | * but the native term_exists() will return true even if only one exists. |
| 442 | 404 | * So here the function adds the language parameter. |
| 443 | 405 | * |
| @@ -632,6 +594,113 @@ | ||
| 632 | 594 | */ |
| 633 | 595 | $class = apply_filters( 'pll_links_model', $class ); |
| 634 | 596 | |
| 635 | 597 | return new $class( $this ); |
| 598 | + } | |
| 599 | + | |
| 600 | + /** | |
| 601 | + * Returns posts and terms ids without language ( used in settings ). | |
| 602 | + * | |
| 603 | + * @since 0.9 | |
| 604 | + * @since 2.2.6 Add the $limit argument. | |
| 605 | + * | |
| 606 | + * @param int $limit Max number of posts or terms to return. Defaults to -1 (no limit). | |
| 607 | + * @return array { | |
| 608 | + * Objects without language. | |
| 609 | + * | |
| 610 | + * @type int[] $posts Array of post ids. | |
| 611 | + * @type int[] $terms Array of term ids. | |
| 612 | + * } | |
| 613 | + */ | |
| 614 | + public function get_objects_with_no_lang( $limit = -1 ) { | |
| 615 | + /** | |
| 616 | + * Filters the max number of posts or terms to return when searching objects with no language. | |
| 617 | + * This filter can be used to decrease the memory usage in case the number of objects | |
| 618 | + * without language is too big. Using a negative value is equivalent to have no limit. | |
| 619 | + * | |
| 620 | + * @since 2.2.6 | |
| 621 | + * | |
| 622 | + * @param int $limit Max number of posts or terms to retrieve from the database. | |
| 623 | + */ | |
| 624 | + $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit ); | |
| 625 | + | |
| 626 | + $posts = $this->get_posts_with_no_lang( $this->get_translated_post_types(), $limit ); | |
| 627 | + $terms = $this->get_terms_with_no_lang( $this->get_translated_taxonomies(), $limit ); | |
| 628 | + | |
| 629 | + /** | |
| 630 | + * Filters the list of untranslated posts ids and terms ids | |
| 631 | + * | |
| 632 | + * @since 0.9 | |
| 633 | + * | |
| 634 | + * @param array|false $objects false if no ids found, list of post and/or term ids otherwise. | |
| 635 | + */ | |
| 636 | + return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) ); | |
| 637 | + } | |
| 638 | + | |
| 639 | + /** | |
| 640 | + * Returns ids of post without language. | |
| 641 | + * | |
| 642 | + * @since 3.1 | |
| 643 | + * | |
| 644 | + * @param string|string[] $post_types A translated post type or an array of translated post types. | |
| 645 | + * @param int $limit Max number of posts to return. | |
| 646 | + * @return int[] | |
| 647 | + */ | |
| 648 | + public function get_posts_with_no_lang( $post_types, $limit ) { | |
| 649 | + return get_posts( | |
| 650 | + array( | |
| 651 | + 'numberposts' => $limit, | |
| 652 | + 'nopaging' => $limit <= 0, | |
| 653 | + 'post_type' => $post_types, | |
| 654 | + 'post_status' => 'any', | |
| 655 | + 'fields' => 'ids', | |
| 656 | + 'tax_query' => array( | |
| 657 | + array( | |
| 658 | + 'taxonomy' => 'language', | |
| 659 | + 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ), | |
| 660 | + 'operator' => 'NOT IN', | |
| 661 | + ), | |
| 662 | + ), | |
| 663 | + ) | |
| 664 | + ); | |
| 665 | + } | |
| 666 | + | |
| 667 | + /** | |
| 668 | + * Returns ids of terms without language. | |
| 669 | + * | |
| 670 | + * @since 3.1 | |
| 671 | + * | |
| 672 | + * @param string|string[] $taxonomies A translated taxonomy or an array of taxonomies post types. | |
| 673 | + * @param int $limit Max number of terms to return. | |
| 674 | + * @return int[] | |
| 675 | + */ | |
| 676 | + public function get_terms_with_no_lang( $taxonomies, $limit ) { | |
| 677 | + global $wpdb; | |
| 678 | + | |
| 679 | + $taxonomies = (array) $taxonomies; | |
| 680 | + | |
| 681 | + $sql = sprintf( | |
| 682 | + "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy} | |
| 683 | + WHERE taxonomy IN ('%s') | |
| 684 | + AND {$wpdb->term_taxonomy}.term_id NOT IN ( | |
| 685 | + SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s) | |
| 686 | + ) | |
| 687 | + %s", | |
| 688 | + implode( "','", array_map( 'esc_sql', $taxonomies ) ), | |
| 689 | + implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ), | |
| 690 | + $limit > 0 ? sprintf( 'LIMIT %d', intval( $limit ) ) : '' | |
| 691 | + ); | |
| 692 | + | |
| 693 | + $key = md5( $sql ); | |
| 694 | + $last_changed = wp_cache_get_last_changed( 'terms' ); | |
| 695 | + $cache_key = "terms_no_lang:{$key}:{$last_changed}"; | |
| 696 | + | |
| 697 | + $term_ids = wp_cache_get( $cache_key, 'terms' ); | |
| 698 | + | |
| 699 | + if ( false === $term_ids ) { | |
| 700 | + $term_ids = $wpdb->get_col( $sql ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 701 | + wp_cache_set( $cache_key, $term_ids, 'terms' ); | |
| 702 | + } | |
| 703 | + | |
| 704 | + return $term_ids; | |
| 636 | 705 | } |
| 637 | 706 | } |