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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 / SynchronizerForMeta.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
SynchronizerForMeta.php
192 lines
1 <?php
2
3 namespace WCML\Synchronization\Component;
4
5 use WCML\Utilities\DB;
6
7 abstract class SynchronizerForMeta extends Synchronizer {
8
9 protected function getMeta( $metaKey, $itemIds ) {
10 $storedRawData = $this->wpdb->get_results(
11 $this->wpdb->prepare(
12 "
13 SELECT post_id, meta_value
14 FROM {$this->wpdb->postmeta}
15 WHERE meta_key = %s
16 AND post_id IN (" . DB::prepareIn( $itemIds ) . ")
17 LIMIT %d
18 ",
19 $metaKey,
20 count( $itemIds )
21 ),
22 OBJECT_K
23 );
24
25 if ( empty( $storedRawData ) ) {
26 return [];
27 }
28
29 $storedData = array_map( function( $data ) {
30 return maybe_unserialize( $data->meta_value );
31 }, $storedRawData );
32
33 return $storedData;
34 }
35
36 protected function insertMeta( $metaKey, $metaValues ) {
37 if ( empty( $metaValues ) ) {
38 return;
39 }
40
41 $insertValues = [];
42 foreach ( $metaValues as $idToInsert => $valueToInsert ) {
43 $insertValues[] = $this->wpdb->prepare( "(%d,%s,%s)", $idToInsert, $metaKey, maybe_serialize( $valueToInsert ) );
44 }
45 $this->wpdb->query(
46 "INSERT INTO {$this->wpdb->postmeta}
47 (`post_id`,`meta_key`,`meta_value`)
48 VALUES " . implode( ',', $insertValues )
49 );
50 }
51
52 protected function updateMeta( $metaKey, $metaValues ) {
53 if ( empty( $metaValues ) ) {
54 return;
55 }
56 foreach ( $metaValues as $idToUpdate => $valueToUpdate ) {
57 $this->wpdb->update(
58 $this->wpdb->postmeta,
59 [ 'meta_value' => maybe_serialize( $valueToUpdate ) ],
60 [ 'post_id' => $idToUpdate, 'meta_key' => $metaKey ]
61 );
62 }
63 }
64
65 protected function deleteMetaByIds( $metaIds ) {
66 if ( empty( $metaIds ) ) {
67 return;
68 }
69 $this->wpdb->query(
70 $this->wpdb->prepare(
71 "
72 DELETE FROM {$this->wpdb->postmeta}
73 WHERE meta_id IN (" . DB::prepareIn( $metaIds ) . ")
74 LIMIT %d
75 ",
76 count( $metaIds )
77 )
78 );
79 }
80
81 protected function unifyMeta( $metaKey, $metaValue, $idsToUnify ) {
82 if ( empty( $idsToUnify ) ) {
83 return;
84 }
85 $this->wpdb->query(
86 $this->wpdb->prepare(
87 "UPDATE {$this->wpdb->postmeta}
88 SET meta_value = %s
89 WHERE meta_key = %s
90 AND post_id IN (" . DB::prepareIn( $idsToUnify ) . ")",
91 maybe_serialize( $metaValue ),
92 $metaKey
93 )
94 );
95 }
96
97 protected function synchronizeMeta( $productId, $translationsIds, $metaKey ) {
98 $productsIds = array_merge( [ $productId ], $translationsIds );
99 $storedMeta = $this->getMeta( $metaKey, $productsIds );
100 $productMeta = $storedMeta[ $productId ] ?? null;
101 if ( null === $productMeta ) {
102 return;
103 }
104
105 $metaToInsert = [];
106 $idsToUpdate = [];
107 foreach ( $translationsIds as $translationId ) {
108 if ( ! array_key_exists( $translationId, $storedMeta ) ) {
109 $metaToInsert[ $translationId ] = $productMeta;
110 continue;
111 }
112 if ( $productMeta === $storedMeta[ $translationId ] ) {
113 continue;
114 }
115 $idsToUpdate[] = $translationId;
116 }
117
118 $this->insertMeta( $metaKey, $metaToInsert );
119 $this->unifyMeta( $metaKey, $productMeta, $idsToUpdate );
120 }
121
122 protected function spreadEmptyValue( $translationsIds, $storedData, $metaKey ) {
123 $metaToInsert = [];
124 $metaToUpdate = [];
125 foreach ( $translationsIds as $translationId ) {
126 if ( ! array_key_exists( $translationId, $storedData ) ) {
127 $metaToInsert[ $translationId ] = [];
128 continue;
129 }
130 $translationStoredData = $storedData[ $translationId ];
131 if ( ! empty( $translationStoredData ) ) {
132 $metaToUpdate[ $translationId ] = [];
133 }
134 }
135
136 $this->insertMeta( $metaKey, $metaToInsert );
137 $this->updateMeta( $metaKey, $metaToUpdate );
138 }
139
140 protected function clearTranslationsValue( $translationsIds, $metaKey ) {
141 if ( empty( $translationsIds ) ) {
142 return;
143 }
144 $this->wpdb->query(
145 $this->wpdb->prepare(
146 "
147 DELETE FROM {$this->wpdb->postmeta}
148 WHERE meta_key = %s
149 AND post_id IN (" . DB::prepareIn( $translationsIds ) . ")
150 LIMIT %d
151 ",
152 $metaKey,
153 count( $translationsIds )
154 )
155 );
156 }
157
158 protected function deleteOrphanedFields( $productId, $translationsIds ) {
159 $productsIds = array_merge( [ $productId ], $translationsIds );
160 $storedRawData = $this->wpdb->get_results(
161 "
162 SELECT *
163 FROM {$this->wpdb->postmeta}
164 WHERE post_id IN (" . DB::prepareIn( $productsIds ) . ")
165 "
166 );
167
168 $storedData = [];
169 foreach ( $storedRawData as $rawData ) {
170 $storedData[ $rawData->post_id ][ $rawData->meta_key ] = $rawData->meta_id;
171 }
172 $productData = $storedData[ $productId ] ?? [];
173
174 $orphanedMetaIds = [];
175 $settingsFactory = wpml_load_core_tm()->settings_factory();
176 foreach ( $translationsIds as $translationId ) {
177 $translationData = $storedData[ $translationId ] ?? [];
178 foreach ( $translationData as $metaKey => $metaId ) {
179 if ( WPML_COPY_CUSTOM_FIELD !== $settingsFactory->post_meta_setting( $metaKey )->status() ) {
180 continue;
181 }
182 if ( ! array_key_exists( $metaKey, $productData ) ) {
183 $orphanedMetaIds[] = $metaId;
184 }
185 }
186 }
187
188 $this->deleteMetaByIds( $orphanedMetaIds );
189 }
190
191 }
192