PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 +107 -39 3.0.23.1.4 View file →
@@ -90,9 +90,8 @@
90 90 $term_languages = empty( $term_languages ) || is_wp_error( $term_languages ) ?
91 91 array() : array_combine( wp_list_pluck( $term_languages, 'slug' ), $term_languages );
92 92
93 93 if ( ! empty( $languages ) && ! empty( $term_languages ) ) {
94 - // Don't use array_map + create_function() to instantiate an autoloaded class as it breaks badly in old versions of PHP.
95 94 foreach ( $languages as $k => $v ) {
96 95 $languages[ $k ] = new PLL_Language( $v, $term_languages[ 'pll_' . $v->slug ] );
97 96 }
98 97
@@ -398,46 +397,8 @@
398 397 return $query_vars;
399 398 }
400 399
401 400 /**
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 401 * It is possible to have several terms with the same name in the same taxonomy ( one per language )
441 402 * but the native term_exists() will return true even if only one exists.
442 403 * So here the function adds the language parameter.
443 404 *
@@ -632,6 +593,113 @@
632 593 */
633 594 $class = apply_filters( 'pll_links_model', $class );
634 595
635 596 return new $class( $this );
597 + }
598 +
599 + /**
600 + * Returns posts and terms ids without language ( used in settings ).
601 + *
602 + * @since 0.9
603 + * @since 2.2.6 Add the $limit argument.
604 + *
605 + * @param int $limit Max number of posts or terms to return. Defaults to -1 (no limit).
606 + * @return array {
607 + * Objects without language.
608 + *
609 + * @type int[] $posts Array of post ids.
610 + * @type int[] $terms Array of term ids.
611 + * }
612 + */
613 + public function get_objects_with_no_lang( $limit = -1 ) {
614 + /**
615 + * Filters the max number of posts or terms to return when searching objects with no language.
616 + * This filter can be used to decrease the memory usage in case the number of objects
617 + * without language is too big. Using a negative value is equivalent to have no limit.
618 + *
619 + * @since 2.2.6
620 + *
621 + * @param int $limit Max number of posts or terms to retrieve from the database.
622 + */
623 + $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit );
624 +
625 + $posts = $this->get_posts_with_no_lang( $this->get_translated_post_types(), $limit );
626 + $terms = $this->get_terms_with_no_lang( $this->get_translated_taxonomies(), $limit );
627 +
628 + /**
629 + * Filters the list of untranslated posts ids and terms ids
630 + *
631 + * @since 0.9
632 + *
633 + * @param array|false $objects false if no ids found, list of post and/or term ids otherwise.
634 + */
635 + return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) );
636 + }
637 +
638 + /**
639 + * Returns ids of post without language.
640 + *
641 + * @since 3.1
642 + *
643 + * @param string|string[] $post_types A translated post type or an array of translated post types.
644 + * @param int $limit Max number of posts to return.
645 + * @return int[]
646 + */
647 + public function get_posts_with_no_lang( $post_types, $limit ) {
648 + return get_posts(
649 + array(
650 + 'numberposts' => $limit,
651 + 'nopaging' => $limit <= 0,
652 + 'post_type' => $post_types,
653 + 'post_status' => 'any',
654 + 'fields' => 'ids',
655 + 'tax_query' => array(
656 + array(
657 + 'taxonomy' => 'language',
658 + 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ),
659 + 'operator' => 'NOT IN',
660 + ),
661 + ),
662 + )
663 + );
664 + }
665 +
666 + /**
667 + * Returns ids of terms without language.
668 + *
669 + * @since 3.1
670 + *
671 + * @param string|string[] $taxonomies A translated taxonomy or an array of taxonomies post types.
672 + * @param int $limit Max number of terms to return.
673 + * @return int[]
674 + */
675 + public function get_terms_with_no_lang( $taxonomies, $limit ) {
676 + global $wpdb;
677 +
678 + $taxonomies = (array) $taxonomies;
679 +
680 + $sql = sprintf(
681 + "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy}
682 + WHERE taxonomy IN ('%s')
683 + AND {$wpdb->term_taxonomy}.term_id NOT IN (
684 + SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s)
685 + )
686 + %s",
687 + implode( "','", array_map( 'esc_sql', $taxonomies ) ),
688 + implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ),
689 + $limit > 0 ? sprintf( 'LIMIT %d', intval( $limit ) ) : ''
690 + );
691 +
692 + $key = md5( $sql );
693 + $last_changed = wp_cache_get_last_changed( 'terms' );
694 + $cache_key = "terms_no_lang:{$key}:{$last_changed}";
695 +
696 + $term_ids = wp_cache_get( $cache_key, 'terms' );
697 +
698 + if ( false === $term_ids ) {
699 + $term_ids = $wpdb->get_col( $sql ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
700 + wp_cache_set( $cache_key, $term_ids, 'terms' );
701 + }
702 +
703 + return $term_ids;
636 704 }
637 705 }