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.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 year ago Attributes.php 4 months ago DownloadableFiles.php 4 months ago Linked.php 10 months ago Meta.php 1 year ago Post.php 3 months ago Stock.php 1 year ago Synchronizer.php 1 year ago SynchronizerForMeta.php 9 months ago Taxonomies.php 4 months ago VariationAttachments.php 1 year ago VariationMeta.php 8 months ago VariationTaxonomies.php 8 months ago Variations.php 1 month ago
SynchronizerForMeta.php
246 lines
1 <?php
2
3 namespace WCML\Synchronization\Component;
4
5 use WCML\Utilities\DB;
6
7 abstract class SynchronizerForMeta extends Synchronizer {
8
9 /**
10 * @param string $metaKey
11 * @param int[] $itemIds
12 *
13 * @return array
14 */
15 protected function getMeta( $metaKey, $itemIds ) {
16 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
17 $storedRawData = $this->wpdb->get_results(
18 $this->wpdb->prepare(
19 "
20 SELECT post_id, meta_value
21 FROM {$this->wpdb->postmeta}
22 WHERE meta_key = %s
23 AND post_id IN (" . DB::prepareIn( $itemIds ) . ")
24 LIMIT %d
25 ",
26 $metaKey,
27 count( $itemIds )
28 ),
29 OBJECT_K
30 );
31 // phpcs:enable
32
33 if ( empty( $storedRawData ) ) {
34 return [];
35 }
36
37 $storedData = array_map( function( $data ) {
38 return maybe_unserialize( $data->meta_value );
39 }, $storedRawData );
40
41 return $storedData;
42 }
43
44 /**
45 * @param string $metaKey
46 * @param array<int,mixed> $metaValues
47 */
48 protected function insertMeta( $metaKey, $metaValues ) {
49 if ( empty( $metaValues ) ) {
50 return;
51 }
52
53 $insertValues = [];
54 foreach ( $metaValues as $idToInsert => $valueToInsert ) {
55 $insertValues[] = $this->wpdb->prepare( "(%d,%s,%s)", $idToInsert, $metaKey, maybe_serialize( $valueToInsert ) );
56 }
57 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
58 $this->wpdb->query(
59 "INSERT INTO {$this->wpdb->postmeta}
60 (`post_id`,`meta_key`,`meta_value`)
61 VALUES " . implode( ',', $insertValues )
62 );
63 // phpcs:enable
64 }
65
66 /**
67 * @param string $metaKey
68 * @param array<int,mixed> $metaValues
69 */
70 protected function updateMeta( $metaKey, $metaValues ) {
71 if ( empty( $metaValues ) ) {
72 return;
73 }
74 foreach ( $metaValues as $idToUpdate => $valueToUpdate ) {
75 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
76 $this->wpdb->update(
77 $this->wpdb->postmeta,
78 [ 'meta_value' => maybe_serialize( $valueToUpdate ) ],
79 [ 'post_id' => $idToUpdate, 'meta_key' => $metaKey ]
80 );
81 // phpcs:enable
82 }
83 }
84
85 /**
86 * @param int[] $metaIds
87 */
88 protected function deleteMetaByIds( $metaIds ) {
89 if ( empty( $metaIds ) ) {
90 return;
91 }
92 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
93 $this->wpdb->query(
94 $this->wpdb->prepare(
95 "
96 DELETE FROM {$this->wpdb->postmeta}
97 WHERE meta_id IN (" . DB::prepareIn( $metaIds ) . ")
98 LIMIT %d
99 ",
100 count( $metaIds )
101 )
102 );
103 // phpcs:enable
104 }
105
106 /**
107 * @param string $metaKey
108 * @param mixed $metaValue
109 * @param int[] $idsToUnify
110 */
111 protected function unifyMeta( $metaKey, $metaValue, $idsToUnify ) {
112 if ( empty( $idsToUnify ) ) {
113 return;
114 }
115 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
116 $this->wpdb->query(
117 $this->wpdb->prepare(
118 "UPDATE {$this->wpdb->postmeta}
119 SET meta_value = %s
120 WHERE meta_key = %s
121 AND post_id IN (" . DB::prepareIn( $idsToUnify ) . ")",
122 maybe_serialize( $metaValue ),
123 $metaKey
124 )
125 );
126 // phpcs:enable
127 }
128
129 /**
130 * @param int $productId
131 * @param int[] $translationsIds
132 * @param string $metaKey
133 */
134 protected function synchronizeMeta( $productId, $translationsIds, $metaKey ) {
135 $productsIds = array_merge( [ $productId ], $translationsIds );
136 $storedMeta = $this->getMeta( $metaKey, $productsIds );
137 $productMeta = $storedMeta[ $productId ] ?? null;
138 if ( null === $productMeta ) {
139 return;
140 }
141
142 $metaToInsert = [];
143 $idsToUpdate = [];
144 foreach ( $translationsIds as $translationId ) {
145 if ( ! array_key_exists( $translationId, $storedMeta ) ) {
146 $metaToInsert[ $translationId ] = $productMeta;
147 continue;
148 }
149 if ( $productMeta === $storedMeta[ $translationId ] ) {
150 continue;
151 }
152 $idsToUpdate[] = $translationId;
153 }
154
155 $this->insertMeta( $metaKey, $metaToInsert );
156 $this->unifyMeta( $metaKey, $productMeta, $idsToUpdate );
157 }
158
159 /**
160 * @param int[] $translationsIds
161 * @param array<int,mixed> $storedData
162 * @param string $metaKey
163 */
164 protected function spreadEmptyValue( $translationsIds, $storedData, $metaKey ) {
165 $metaToInsert = [];
166 $metaToUpdate = [];
167 foreach ( $translationsIds as $translationId ) {
168 if ( ! array_key_exists( $translationId, $storedData ) ) {
169 $metaToInsert[ $translationId ] = [];
170 continue;
171 }
172 $translationStoredData = $storedData[ $translationId ];
173 if ( ! empty( $translationStoredData ) ) {
174 $metaToUpdate[ $translationId ] = [];
175 }
176 }
177
178 $this->insertMeta( $metaKey, $metaToInsert );
179 $this->updateMeta( $metaKey, $metaToUpdate );
180 }
181
182 /**
183 * @param int[] $translationsIds
184 * @param string $metaKey
185 */
186 protected function clearTranslationsValue( $translationsIds, $metaKey ) {
187 if ( empty( $translationsIds ) ) {
188 return;
189 }
190 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
191 $this->wpdb->query(
192 $this->wpdb->prepare(
193 "
194 DELETE FROM {$this->wpdb->postmeta}
195 WHERE meta_key = %s
196 AND post_id IN (" . DB::prepareIn( $translationsIds ) . ")
197 LIMIT %d
198 ",
199 $metaKey,
200 count( $translationsIds )
201 )
202 );
203 // phpcs:enable
204 }
205
206 /**
207 * @param int $productId
208 * @param int[] $translationsIds
209 */
210 protected function deleteOrphanedFields( $productId, $translationsIds ) {
211 $productsIds = array_merge( [ $productId ], $translationsIds );
212 // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
213 $storedRawData = $this->wpdb->get_results(
214 "
215 SELECT *
216 FROM {$this->wpdb->postmeta}
217 WHERE post_id IN (" . DB::prepareIn( $productsIds ) . ")
218 "
219 );
220 // phpcs:enable
221
222 $storedData = [];
223 foreach ( $storedRawData as $rawData ) {
224 $storedData[ $rawData->post_id ][ $rawData->meta_key ] = $rawData->meta_id;
225 }
226 $productData = $storedData[ $productId ] ?? [];
227
228 $orphanedMetaIds = [];
229 $settingsFactory = wpml_load_core_tm()->settings_factory();
230 foreach ( $translationsIds as $translationId ) {
231 $translationData = $storedData[ $translationId ] ?? [];
232 foreach ( $translationData as $metaKey => $metaId ) {
233 if ( WPML_COPY_CUSTOM_FIELD !== $settingsFactory->post_meta_setting( $metaKey )->status() ) {
234 continue;
235 }
236 if ( ! array_key_exists( $metaKey, $productData ) ) {
237 $orphanedMetaIds[] = $metaId;
238 }
239 }
240 }
241
242 $this->deleteMetaByIds( $orphanedMetaIds );
243 }
244
245 }
246