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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Synchronization / Manager.php
woocommerce-multilingual / classes / Synchronization Last commit date
Component 1 week ago Hooks.php 1 week ago Manager.php 1 week ago Store.php 1 week ago
Manager.php
157 lines
1 <?php
2
3 namespace WCML\Synchronization;
4
5 class Manager {
6
7 const CONTEXT_PRODUCT_EDIT_SCREEN_UPDATE = 'product_edit_screen_update';
8 const CONTEXT_PRODUCT_BULK_OR_QUICK_EDIT = 'product_bulk_or_quick_edit';
9
10 private $postTranslations;
11
12 private $syncStore;
13
14 private $context;
15
16 public function __construct( Store $syncStore ) {
17 $this->syncStore = $syncStore;
18
19 global $wpml_post_translations;
20 $this->postTranslations = $wpml_post_translations;
21 }
22
23 public function setContext( $context ) {
24 $this->context = $context;
25 }
26
27 private function isInContext( $context ) {
28 $context = (array) $context;
29
30 return in_array( $this->context, $context, true );
31 }
32
33 public function getOriginalProduct( $product ) {
34 $originalProduct = $product;
35 $originalProductId = $this->postTranslations->get_original_element( $product->ID ) ?: $product->ID;
36 if ( $originalProductId !== $product->ID ) {
37 $originalProduct = get_post( $originalProductId );
38 }
39 return $originalProduct;
40 }
41
42 public function isOriginalProduct( $product ) {
43 return null === $this->postTranslations->get_source_lang_code( $product->ID );
44 }
45
46 public function getElementLanguage( $elementId ) {
47 return $this->postTranslations->get_element_lang_code( $elementId );
48 }
49
50 public function getElementTranslations( $elementId, $trid = false, $actualtranslationsOnly = true ) {
51 return $this->postTranslations->get_element_translations( $elementId, $trid, $actualtranslationsOnly );
52 }
53
54 public function run( $product, $translationsIds = [], $translationsLanguages = [] ) {
55 $originalProduct = $this->getOriginalProduct( $product );
56
57 if ( empty( $translationsIds ) ) {
58 $translationsIds = $this->postTranslations->get_element_translations( $originalProduct->ID, false, true );
59 }
60
61 if ( empty( $translationsIds ) ) {
62 return;
63 }
64
65 foreach ( $translationsIds as $translationId ) {
66 if ( isset( $translationsLanguages[ $translationId ] ) ) {
67 continue;
68 }
69 $translationsLanguages[ $translationId ] = $this->getElementLanguage( $translationId );
70 }
71
72 do_action( 'wcml_before_sync_product', $originalProduct->ID, $product->ID );
73 $this->runProductComponents( $originalProduct, $translationsIds, $translationsLanguages );
74 do_action( 'wcml_after_sync_product', $originalProduct->ID, $product->ID );
75 }
76
77 public function runProductComponents( $product, $translationsIds, $translationsLanguages ) {
78 $components = $this->getComponentsByPostType( $product->post_type );
79 if ( empty( $components ) ) {
80 return;
81 }
82
83 foreach ( $translationsIds as $translationId ) {
84 do_action( 'wcml_before_sync_product_data', $product->ID, $translationId, $translationsLanguages[ $translationId ] );
85 }
86
87
88 foreach ( $components as $componentName ) {
89 $this->runComponent( $product, $translationsIds, $translationsLanguages, $componentName );
90 }
91
92 $wcmlDataStore = wcml_product_data_store_cpt();
93
94 foreach ( $translationsIds as $translationId ) {
95 $wcmlDataStore->update_lookup_table_data( $translationId );
96 wc_delete_product_transients( $translationId );
97 do_action( 'wcml_after_sync_product_data', $product->ID, $translationId, $translationsLanguages[ $translationId ] );
98 }
99
100 }
101
102 public function runProductVariationComponents( $variation, $translationsIds, $translationsLanguages ) {
103 $components = $this->getComponentsByPostType( $variation->post_type );
104 if ( empty( $components ) ) {
105 return;
106 }
107
108 foreach ( $components as $componentName ) {
109 $this->runComponent( $variation, $translationsIds, $translationsLanguages, $componentName );
110 }
111
112 $wcmlDataStore = wcml_product_data_store_cpt();
113
114 foreach ( $translationsIds as $translationId ) {
115 $wcmlDataStore->update_lookup_table_data( $translationId );
116 }
117 }
118
119 public function runComponent( $product, $translationsIds, $translationsLanguages, $componentName ) {
120 $component = $this->syncStore->getComponent( $componentName );
121 $component->run( $product, $translationsIds, $translationsLanguages );
122 }
123
124 private function getComponentsByPostType( $postType ) {
125 $components = [];
126
127 if ( 'product' === $postType ) {
128 $components = [
129 Store::COMPONENT_ATTACHMENTS,
130 Store::COMPONENT_ATTRIBUTES,
131 Store::COMPONENT_DOWNLOADABLE_FILES,
132 Store::COMPONENT_LINKED,
133 Store::COMPONENT_POST,
134 Store::COMPONENT_STOCK,
135 Store::COMPONENT_TAXONOMIES,
136 Store::COMPONENT_META,
137 ];
138
139 if ( ! $this->isInContext( [ self::CONTEXT_PRODUCT_EDIT_SCREEN_UPDATE, self::CONTEXT_PRODUCT_BULK_OR_QUICK_EDIT ] ) ) {
140 $components[] = Store::COMPONENT_VARIATIONS;
141 }
142
143 } elseif ( 'product_variation' === $postType ) {
144 $components = [
145 Store::COMPONENT_VARIATION_ATTACHMENTS,
146 Store::COMPONENT_VARIATION_META,
147 Store::COMPONENT_DOWNLOADABLE_FILES,
148 Store::COMPONENT_VARIATION_TAXONOMIES,
149 Store::COMPONENT_STOCK,
150 ];
151 }
152
153 return $components;
154 }
155
156 }
157