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 |