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
VariationTaxonomies.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Synchronization\Component; |
| 4 | |
| 5 | use WCML\Terms\SuspendWpmlFiltersFactory; |
| 6 | use WCML\Utilities\DB; |
| 7 | use WCML\Utilities\SyncHash; |
| 8 | use WPML_Non_Persistent_Cache; |
| 9 | |
| 10 | class VariationTaxonomies extends Synchronizer { |
| 11 | |
| 12 | public function run( $variation, $translationsIds, $translationsLanguages ) { |
| 13 | $filtersSuspend = SuspendWpmlFiltersFactory::create(); |
| 14 | foreach ( $translationsLanguages as $translationId => $language ) { |
| 15 | $this->runForTranslation( $variation->ID, $translationId, $language ); |
| 16 | } |
| 17 | $filtersSuspend->resume(); |
| 18 | } |
| 19 | |
| 20 | private function runForTranslation( $variationId, $translationId, $language ) { |
| 21 | $taxonomies = get_object_taxonomies( 'product_variation' ); |
| 22 | $taxonomiesToSync = array_values( array_diff( $taxonomies, [ 'translation_priority' ] ) ); |
| 23 | $taxonomiesToSync = apply_filters( 'wcml_product_variations_taxonomies_to_sync', $taxonomiesToSync, $variationId, $translationId, $language ); |
| 24 | $found = false; |
| 25 | $allTerms = WPML_Non_Persistent_Cache::get( $variationId, __CLASS__, $found ); |
| 26 | if ( ! $found ) { |
| 27 | $allTerms = wp_get_object_terms( $variationId, $taxonomiesToSync ); |
| 28 | if ( is_wp_error( $allTerms ) ) { |
| 29 | $allTerms = []; |
| 30 | } |
| 31 | WPML_Non_Persistent_Cache::set( $variationId, $allTerms, __CLASS__ ); |
| 32 | } |
| 33 | |
| 34 | $termIds = wp_list_pluck( $allTerms, 'term_id' ); |
| 35 | $currentHash = md5( join( ',', $termIds ) ); |
| 36 | |
| 37 | if ( $this->syncHashManager->isNewGroupValue( $translationId, SyncHash::GROUP_TAXONOMIES, $currentHash ) ) { |
| 38 | foreach ( $taxonomiesToSync as $taxonomy ) { |
| 39 | $terms = array_filter( |
| 40 | $allTerms, |
| 41 | function ( $term ) use ( $taxonomy ) { |
| 42 | return $term->taxonomy === $taxonomy; |
| 43 | } |
| 44 | ); |
| 45 | |
| 46 | if ( empty ( $terms ) ) { |
| 47 | if ( ! $this->woocommerceWpml->terms->is_translatable_wc_taxonomy( $taxonomy ) ) { |
| 48 | wp_set_object_terms( $translationId, [], $taxonomy ); |
| 49 | } |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | $ttIds = []; |
| 54 | $ttIdsTrans = []; |
| 55 | $termIds = []; |
| 56 | $termIdsTrans = []; |
| 57 | |
| 58 | foreach ( $terms as $term ) { |
| 59 | if ( $this->sitepress->is_translated_taxonomy( $taxonomy ) ) { |
| 60 | $ttIds[] = $term->term_taxonomy_id; |
| 61 | } else { |
| 62 | $termIds[] = $term->term_id; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | foreach ( $ttIds as $ttId ) { |
| 67 | $ttIdTrans = $this->elementTranslations->element_id_in( $ttId, $language ); |
| 68 | if ( $ttIdTrans ) { |
| 69 | $ttIdsTrans[] = $ttIdTrans; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $ttIdsTrans = array_values( array_unique( array_map( 'intval', $ttIdsTrans ) ) ); |
| 74 | if ( ! empty( $ttIdsTrans ) ) { |
| 75 | $termIdsTrans = $this->wpdb->get_col( |
| 76 | $this->wpdb->prepare( |
| 77 | "SELECT term_id FROM {$this->wpdb->term_taxonomy} WHERE term_taxonomy_id IN (" . DB::prepareIn( $ttIdsTrans, '%d' ) . ") LIMIT %d", |
| 78 | count( $ttIdsTrans ) |
| 79 | ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | $termsToSync = array_merge( $termIds, $termIdsTrans ); |
| 84 | $termsToSync = array_unique( array_map( 'intval', $termsToSync ) ); |
| 85 | |
| 86 | if ( empty( $termsToSync ) ) { |
| 87 | continue; |
| 88 | } |
| 89 | wp_set_object_terms( $translationId, $termsToSync, $taxonomy, true ); |
| 90 | } |
| 91 | |
| 92 | $this->syncHashManager->updateGroupValue( $translationId, SyncHash::GROUP_TAXONOMIES, $currentHash ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | } |
| 97 |