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 |