Attachments.php
1 week ago
Attributes.php
1 week ago
DownloadableFiles.php
1 week ago
Linked.php
1 week ago
Meta.php
1 week ago
Post.php
1 week ago
Stock.php
1 week ago
Synchronizer.php
1 week ago
SynchronizerForMeta.php
1 week ago
Taxonomies.php
1 week ago
VariationAttachments.php
1 week ago
VariationMeta.php
1 week ago
VariationTaxonomies.php
1 week ago
Variations.php
1 week ago
Taxonomies.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Synchronization\Component; |
| 4 | |
| 5 | use WCML\Terms\SuspendWpmlFiltersFactory; |
| 6 | use WCML\Utilities\DB; |
| 7 | use WPML_Non_Persistent_Cache; |
| 8 | class Taxonomies extends Synchronizer { |
| 9 | |
| 10 | public function run( $product, $translationsIds, $translationsLanguages ) { |
| 11 | $filtersSuspend = SuspendWpmlFiltersFactory::create(); |
| 12 | foreach ( $translationsLanguages as $translationId => $language ) { |
| 13 | $this->runForTranslation( $product->ID, $translationId, $language ); |
| 14 | } |
| 15 | $filtersSuspend->resume(); |
| 16 | } |
| 17 | |
| 18 | private function runForTranslation( $productId, $translationId, $language ) { |
| 19 | $taxonomyExceptions = [ 'product_type', 'product_visibility' ]; |
| 20 | $taxonomySyncEmpty = [ \WCML_Terms::PRODUCT_SHIPPING_CLASS ]; |
| 21 | $taxonomies = $taxonomyExceptions; |
| 22 | if ( $this->sitepress->get_setting( 'sync_post_taxonomies' ) ) { |
| 23 | $taxonomies = get_object_taxonomies( 'product' ); |
| 24 | } |
| 25 | |
| 26 | $found = false; |
| 27 | $allTerms = WPML_Non_Persistent_Cache::get( $productId, __CLASS__, $found ); |
| 28 | if ( ! $found ) { |
| 29 | $allTerms = wp_get_object_terms( $productId, $taxonomies ); |
| 30 | WPML_Non_Persistent_Cache::set( $productId, $allTerms, __CLASS__ ); |
| 31 | } |
| 32 | if ( ! is_wp_error( $allTerms ) ) { |
| 33 | foreach ( $taxonomies as $taxonomy ) { |
| 34 | if ( ! apply_filters( 'wpml_is_translated_taxonomy', false, $taxonomy ) ) { |
| 35 | $taxonomyExceptions[] = $taxonomy; |
| 36 | } |
| 37 | $ttIds = []; |
| 38 | $ttNames = []; |
| 39 | $terms = array_filter( |
| 40 | $allTerms, |
| 41 | function ( $term ) use ( $taxonomy ) { |
| 42 | return $term->taxonomy === $taxonomy; |
| 43 | } |
| 44 | ); |
| 45 | if ( ! $terms && ! in_array( $taxonomy, $taxonomySyncEmpty, true ) ) { |
| 46 | continue; |
| 47 | } |
| 48 | foreach ( $terms as $term ) { |
| 49 | if ( in_array( $term->taxonomy, $taxonomyExceptions, true ) ) { |
| 50 | $ttNames[] = $term->name; |
| 51 | continue; |
| 52 | } |
| 53 | $ttIds[] = $term->term_taxonomy_id; |
| 54 | } |
| 55 | |
| 56 | if ( $this->woocommerceWpml->terms->is_translatable_wc_taxonomy( $taxonomy ) && ! in_array( $taxonomy, $taxonomyExceptions, true ) ) { |
| 57 | $this->setTranslatedTerms( $ttIds, $language, $taxonomy, $translationId ); |
| 58 | } else { |
| 59 | wp_set_post_terms( $translationId, $ttNames, $taxonomy ); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | private function setTranslatedTerms( $ttIds, $language, $taxonomy, $translationId ) { |
| 67 | $ttIdsTrans = []; |
| 68 | |
| 69 | foreach ( $ttIds as $ttId ) { |
| 70 | $ttIdTrans = $this->elementTranslations->element_id_in( $ttId, $language ); |
| 71 | if ( $ttIdTrans ) { |
| 72 | $ttIdsTrans[] = $ttIdTrans; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $ttIdsTrans = array_values( array_unique( array_map( 'intval', $ttIdsTrans ) ) ); |
| 77 | |
| 78 | if ( empty( $ttIdsTrans ) ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if ( in_array( $taxonomy, [ 'product_cat', 'product_tag' ] ) ) { |
| 83 | $this->sitepress->switch_lang( $language ); |
| 84 | wp_update_term_count( $ttIdsTrans, $taxonomy ); |
| 85 | $this->sitepress->switch_lang(); |
| 86 | } |
| 87 | |
| 88 | $termIds = $this->wpdb->get_col( |
| 89 | $this->wpdb->prepare( |
| 90 | "SELECT term_id FROM {$this->wpdb->term_taxonomy} WHERE term_taxonomy_id IN (" . DB::prepareIn( $ttIdsTrans, '%d' ) . ") LIMIT %d", |
| 91 | count( $ttIdsTrans ) |
| 92 | ) |
| 93 | ); |
| 94 | |
| 95 | $termIds = array_unique( array_map( 'intval', $termIds ) ); |
| 96 | wp_set_post_terms( $translationId, $termIds, $taxonomy ); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 |