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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 / Component / VariationMeta.php
woocommerce-multilingual / classes / Synchronization / Component Last commit date
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
VariationMeta.php
182 lines
1 <?php
2
3 namespace WCML\Synchronization\Component;
4
5 use WCML\Utilities\DB;
6 use WCML\Utilities\SyncHash;
7 use WPML\FP\Obj;
8 use WPML_Post_Custom_Field_Setting_Keys;
9
10 class VariationMeta extends SynchronizerForMeta {
11
12 public function run( $variation, $translationsIds, $translationsLanguages ) {
13 $delayedFields = [];
14 foreach ( $translationsLanguages as $translationId => $language ) {
15 $delayedFields = $this->synchronizeVariationMeta( $variation->ID, $translationId, $language, $delayedFields );
16 }
17 $this->processDelayedFields( $delayedFields, $translationsIds );
18 $this->deleteOrphanedFields( $variation->ID, $translationsIds );
19 }
20
21 protected function synchronizeVariationMeta( $variationId, $translationId, $language, $delayedFields ) {
22 $variationMeta = get_post_custom( $variationId );
23 unset( $variationMeta[ SyncHash::META_KEY ] );
24 $currentHash = $this->getCurrentHash( $variationMeta, $variationId, $translationId, $language );
25 $isSyncNeeded = $this->syncHashManager->isNewGroupValue( $translationId, SyncHash::GROUP_FIELDS, $currentHash );
26
27 if ( ! $isSyncNeeded ) {
28 return [];
29 }
30
31 global $iclTranslationManagement;
32 $settings = $iclTranslationManagement->settings['custom_fields_translation'];
33 $excludedKeys = WPML_Post_Custom_Field_Setting_Keys::get_excluded_keys();
34
35 foreach ( $variationMeta as $metaKey => $meta ) {
36 if ( in_array( $metaKey, $excludedKeys, true ) ) {
37 continue;
38 }
39
40 $metaValue = reset( $meta );
41 if ( false === $metaValue ) {
42 $metaValue = '';
43 }
44
45 if ( substr( $metaKey, 0, 10 ) === 'attribute_' ) {
46 if ( '' !== $metaValue ) {
47 $trn_post_meta = $this->woocommerceWpml->attributes->get_translated_variation_attribute_post_meta( $metaValue, $metaKey, $variationId, $translationId, $language );
48 $metaValue = $trn_post_meta['meta_value'];
49 $metaKey = $trn_post_meta['meta_key'];
50 } else {
51 $metaValue = '';
52 }
53 $delayedFields[] = [
54 'post_id' => $translationId,
55 'meta_key' => $metaKey,
56 'meta_value' => maybe_unserialize( $metaValue ),
57 ];
58 continue;
59 }
60
61 if ( ! isset( $settings[ $metaKey ] ) || (int) $settings[ $metaKey ] === WPML_IGNORE_CUSTOM_FIELD ) {
62 continue;
63 }
64
65 if (
66 in_array( $metaKey, [ '_sale_price', '_regular_price', '_price' ] ) &&
67 (int) $this->woocommerceWpml->settings['enable_multi_currency'] === WCML_MULTI_CURRENCIES_INDEPENDENT
68 ) {
69 $delayedFields[] = [
70 'post_id' => $translationId,
71 'meta_key' => $metaKey,
72 'meta_value' => $metaValue,
73 ];
74 continue;
75 }
76
77 if ( (int) Obj::prop( $metaKey, $settings ) === WPML_COPY_CUSTOM_FIELD ) {
78 $delayedFields[] = [
79 'post_id' => $translationId,
80 'meta_key' => $metaKey,
81 'meta_value' => maybe_unserialize( $metaValue ),
82 ];
83 continue;
84 }
85 }
86
87 $this->syncHashManager->updateGroupValue( $translationId, SyncHash::GROUP_FIELDS, $currentHash );
88
89 return $delayedFields;
90 }
91
92 private function processDelayedFields( $delayedFields, $translationsIds ) {
93 if ( empty( $delayedFields ) ) {
94 return;
95 }
96
97 $metaRawData = $this->wpdb->get_results(
98 "
99 SELECT meta_id, post_id, meta_key
100 FROM {$this->wpdb->postmeta}
101 WHERE post_id IN (" . DB::prepareIn( $translationsIds, '%d' ) . ")
102 "
103 );
104
105 $metaData = [];
106 foreach ( $metaRawData as $metaEntry ) {
107 $metaData[ $metaEntry->post_id ][ $metaEntry->meta_id ] = $metaEntry->meta_key;
108 }
109
110 $delayedFieldsActions = [];
111 foreach ( $delayedFields as $delayedFieldData ) {
112 $fieldPostId = $delayedFieldData['post_id'];
113 $fieldMetaKey = $delayedFieldData['meta_key'];
114 $fieldMetaValue = $delayedFieldData['meta_value'];
115 $delayedFieldsActions[ $fieldMetaKey ]['meta_value'] = $fieldMetaValue;
116 $metaDataByVariationId = Obj::propOr( [], $fieldPostId, $metaData );
117 if ( in_array( $fieldMetaKey, $metaDataByVariationId, true ) ) {
118 $fieldMetaIds = array_keys( $metaDataByVariationId, $fieldMetaKey, true );
119 if ( count( $fieldMetaIds ) > 1 ) {
120 $delayedFieldsActions[ $fieldMetaKey ]['delete'][ $fieldPostId ] = $fieldMetaIds;
121 $delayedFieldsActions[ $fieldMetaKey ]['insert'][ $fieldPostId ] = $fieldMetaValue;
122 } else {
123 $fieldMetaValueHash = md5( maybe_serialize( $fieldMetaValue ) );
124 $delayedFieldsActions[ $fieldMetaKey ]['update'][ $fieldMetaValueHash ][ $fieldPostId ] = $fieldMetaValue;
125 }
126 } else {
127 $delayedFieldsActions[ $fieldMetaKey ]['insert'][ $fieldPostId ] = $fieldMetaValue;
128 }
129 }
130
131 foreach ( $delayedFieldsActions as $delayedFieldMetaKey => $delayedFieldMetaData ) {
132 $dataToDelete = Obj::propOr( [], 'delete', $delayedFieldMetaData );
133 if ( ! empty( $dataToDelete ) ) {
134 $metaIdsToDelete = [];
135 foreach ( $dataToDelete as $itemMetaIdsToDelete ) {
136 $metaIdsToDelete = array_merge( $metaIdsToDelete, $itemMetaIdsToDelete );
137 }
138 $this->deleteMetaByIds( $metaIdsToDelete );
139 $this->wpdb->query(
140 "
141 DELETE FROM {$this->wpdb->postmeta}
142 WHERE meta_id IN (" . DB::prepareIn( $metaIdsToDelete, '%d' ) . ")
143 "
144 );
145 }
146
147 $dataToInsert = Obj::propOr( [], 'insert', $delayedFieldMetaData );
148 if ( ! empty( $dataToInsert )) {
149 $this->insertMeta( $delayedFieldMetaKey, $dataToInsert );
150 }
151
152 $dataToUpdate = Obj::propOr( [], 'update', $delayedFieldMetaData );
153 if ( ! empty( $dataToUpdate ) ) {
154 foreach ( $dataToUpdate as $itemsPerValue ) {
155 $idsToUpdate = array_values( array_unique( array_map( 'intval', array_keys( $itemsPerValue ) ) ) );
156 $updateMetaValue = reset( $itemsPerValue );
157 $this->unifyMeta( $delayedFieldMetaKey, $updateMetaValue, $idsToUpdate );
158 }
159 }
160 }
161 }
162
163 private function getCurrentHash( $variationMeta, $variationId, $translationId, $language ) {
164 $translationMeta = $variationMeta;
165 foreach ( $variationMeta as $metaKey => $meta ) {
166 if ( substr( $metaKey, 0, 10 ) !== 'attribute_' ) {
167 continue;
168 }
169 $metaValue = reset( $meta );
170 if ( false === $metaValue ) {
171 continue;
172 }
173 $trn_post_meta = $this->woocommerceWpml->attributes->get_translated_variation_attribute_post_meta( $metaValue, $metaKey, $variationId, $translationId, $language );
174 $metaValue = $trn_post_meta['meta_value'];
175 $metaKey = $trn_post_meta['meta_key'];
176 $translationMeta[ $metaKey ] = [ $metaValue ];
177 }
178 return md5( serialize( $translationMeta ) );
179 }
180
181 }
182