• Skip to sidebar navigation
  • Skip to content

Bitbucket

  • More
    ProjectsRepositories
    • Help
      • Online help
      • Learn Git
      • Welcome to Bitbucket
      • Keyboard shortcuts
    • Log In
    Blackrock
    1. Blackrock

    main

    Public
    Actions
    • Clone

    Learn more about cloning repositories

    You have read-only access

    Navigation
    • Source
    • Commits
    • Branches
    • All Branches Graph
    • Graphs
    • Forks
    1. Blackrock
    2. main

    Source

    main/wp-content/plugins/sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translation-utils.class.php
    Артем ШиряевАртем Ширяев committed 554847ddcb906 Apr 2021
    Raw file
    Source viewDiff to previous
    ​x
     
    1
    <?php
    2
    ​
    3
    class WPML_Term_Translation_Utils extends WPML_SP_User {
    4
    ​
    5
        /**
    6
         * Duplicates all terms, that exist in the given target language,
    7
         * from the original post to the translation in that language.
    8
         *
    9
         * @param int    $original_post_id
    10
         * @param string $lang
    11
         */
    12
        function sync_terms( $original_post_id, $lang ) {
    13
            $this->synchronize_terms( $original_post_id, $lang, false );
    14
        }
    15
    ​
    16
        /**
    17
         * Duplicates all terms on the original post to its translation in the given target language.
    18
         * Missing terms are created with the same name as their originals.
    19
         *
    20
         * @param int    $original_post_id
    21
         * @param string $lang
    22
         */
    23
        function duplicate_terms( $original_post_id, $lang ) {
    24
            $this->synchronize_terms( $original_post_id, $lang, true );
    25
        }
    26
    ​
    27
        /**
    28
         * @param int    $original_post_id
    29
         * @param string $lang
    30
         * @param bool   $duplicate sets whether missing terms should be created by duplicating the original term
    31
         */
    32
        private function synchronize_terms( $original_post_id, $lang, $duplicate ) {
    33
            global $wpml_post_translations;
    34
    ​
    35
            $wpml_post_translations->reload();
    36
            $translated_post_id = $wpml_post_translations->element_id_in( $original_post_id, $lang );
    37
            if ( (bool) $translated_post_id === true ) {
    38
                $taxonomies = get_post_taxonomies( $original_post_id );
    39
    ​
    40
                foreach ( $taxonomies as $tax ) {
    41
                    $terms_on_original = wp_get_object_terms( $original_post_id, $tax );
    42
    ​
    43
                    if ( ! $this->sitepress->is_translated_taxonomy( $tax ) ) {
    44
                        if ( $this->sitepress->get_setting( 'sync_post_taxonomies' ) ) {
    45
                            // Taxonomy is not translated so we can just copy from the original
    46
                            foreach ( $terms_on_original as $key => $term ) {
    47
                                $terms_on_original[ $key ] = $term->term_id;
    48
                            }
    49
                            wp_set_object_terms( $translated_post_id, $terms_on_original, $tax );
    50
                        }
    51
                    } else {
    52
    ​
    53
                        /** @var int[] $translated_terms translated term_ids */
    54
                        $translated_terms = $this->get_translated_term_ids( $terms_on_original, $lang, $tax, $duplicate );
    55
                        wp_set_object_terms( $translated_post_id, $translated_terms, $tax );
    56
                    }
    57
                }
    58
            }
    59
            clean_object_term_cache( $original_post_id, get_post_type( $original_post_id ) );
    60
        }
    61
    ​
    62
        /**
    63
         * @param object[] $terms
    64
         * @param string   $lang
    65
         * @param string   $taxonomy
    66
         * @param bool     $duplicate sets whether missing terms should be created by duplicating the original term
    67
         *
    68
         * @return array
    69
         */
    70
        private function get_translated_term_ids( $terms, $lang, $taxonomy, $duplicate ) {
    71
            /** @var WPML_Term_Translation $wpml_term_translations */
    72
            global $wpml_term_translations;
    73
    ​
    74
            $term_utils = new WPML_Terms_Translations();
    75
            $wpml_term_translations->reload();
    76
            $translated_terms = array();
    77
            foreach ( $terms as $orig_term ) {
    78
                $translated_id = (int) $wpml_term_translations->term_id_in( $orig_term->term_id, $lang );
    79
                if ( ! $translated_id && $duplicate ) {
    80
                    $translation   = $term_utils->create_automatic_translation(
    81
                        array(
    82
                            'lang_code'       => $lang,
    83
                            'taxonomy'        => $taxonomy,
    84
                            'trid'            => $wpml_term_translations->get_element_trid( $orig_term->term_taxonomy_id ),
    85
                            'source_language' => $wpml_term_translations->get_element_lang_code(
    86
                                $orig_term->term_taxonomy_id
    87
                            ),
    88
                        )
    89
                    );
    90
                    $translated_id = isset( $translation['term_id'] ) ? $translation['term_id'] : false;
    91
                }
    92
                if ( $translated_id ) {
    • Git repository management for enterprise teams powered by Atlassian Bitbucket
    • Atlassian Bitbucket v5.10.1
    • Documentation
    • Contact Support
    • Request a feature
    • About
    • Contact Atlassian
    Atlassian

    Everything looks good. Well let you know here if theres anything you should know about.