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 |