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
Post.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Synchronization\Component; |
| 4 | |
| 5 | use WCML\Utilities\DB; |
| 6 | |
| 7 | class Post extends Synchronizer { |
| 8 | |
| 9 | public function run( $product, $translationsIds, $translationsLanguages ) { |
| 10 | $productsIds = array_merge( [ $product->ID ], $translationsIds ); |
| 11 | $fields = [ 'ID', 'post_parent' ]; |
| 12 | |
| 13 | if ( isset( $this->woocommerceWpml->settings['products_sync_order'] ) && $this->woocommerceWpml->settings['products_sync_order'] ) { |
| 14 | $fields[] = 'menu_order'; |
| 15 | } |
| 16 | if ( ! empty( $this->woocommerceWpml->settings['products_sync_date'] ) ) { |
| 17 | $fields[] = 'post_date'; |
| 18 | $fields[] = 'post_date_gmt'; |
| 19 | } |
| 20 | |
| 21 | $fieldsInQuery = implode( ',', $fields ); |
| 22 | |
| 23 | $productsData = $this->wpdb->get_results( |
| 24 | $this->wpdb->prepare( |
| 25 | " |
| 26 | SELECT {$fieldsInQuery} |
| 27 | FROM {$this->wpdb->posts} |
| 28 | WHERE ID IN (" . DB::prepareIn( $productsIds ) . ") |
| 29 | LIMIT %d |
| 30 | ", |
| 31 | count( $productsIds ) |
| 32 | ), |
| 33 | OBJECT_K |
| 34 | ); |
| 35 | |
| 36 | $productData = $productsData[ $product->ID ] ?? null; |
| 37 | if ( ! $productData ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | unset( $productsData[ $product->ID ] ); |
| 42 | |
| 43 | $this->managePostParent( $productData, $productsData, $translationsLanguages ); |
| 44 | $this->manageMenuOrder( $productData, $productsData ); |
| 45 | $this->manageDate( $productData, $productsData ); |
| 46 | } |
| 47 | |
| 48 | private function managePostParent( $productData, $translationsData, $translationsLanguages ) { |
| 49 | $productParent = $productData->post_parent; |
| 50 | if ( ! $productParent ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $productParentTranslations = $this->elementTranslations->get_element_translations( $productParent, false, true ); |
| 55 | foreach ( $translationsData as $translationID => $translationData ) { |
| 56 | if ( ! in_array( (int) $translationData->post_parent, $productParentTranslations, true ) ) { |
| 57 | $translationLanguage = $translationsLanguages[ $translationID ]; |
| 58 | $translationParentId = (int) $this->elementTranslations->element_id_in( $productParent, $translationLanguage, false ); |
| 59 | $this->wpdb->update( |
| 60 | $this->wpdb->posts, |
| 61 | [ 'post_parent' => $translationParentId ], |
| 62 | [ 'id' => $translationID ] |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private function manageMenuOrder( $productData, $translationsData ) { |
| 69 | if ( ! isset( $this->woocommerceWpml->settings['products_sync_order'] ) || !$this->woocommerceWpml->settings['products_sync_order'] ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $productMenuOrder = $productData->menu_order; |
| 74 | $translationsToUpdate = []; |
| 75 | |
| 76 | foreach ( $translationsData as $translationID => $translationData ) { |
| 77 | if ( $translationData->menu_order !== $productMenuOrder ) { |
| 78 | $translationsToUpdate[] = $translationID; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if ( ! empty( $translationsToUpdate ) ) { |
| 83 | $this->wpdb->query( |
| 84 | $this->wpdb->prepare( |
| 85 | " |
| 86 | UPDATE {$this->wpdb->posts} |
| 87 | SET menu_order = %d |
| 88 | WHERE ID IN (" . DB::prepareIn( $translationsToUpdate ) . ") |
| 89 | ", |
| 90 | $productMenuOrder |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private function manageDate( $productData, $translationsData ) { |
| 97 | if ( empty( $this->woocommerceWpml->settings['products_sync_date'] ) ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | $productDate = $productData->post_date; |
| 102 | $productDateGmt = $productData->post_date_gmt; |
| 103 | $translationsToUpdate = []; |
| 104 | |
| 105 | foreach ( $translationsData as $translationID => $translationData ) { |
| 106 | if ( $translationData->post_date !== $productDate || $translationData->post_date_gmt !== $productDateGmt ) { |
| 107 | $translationsToUpdate[] = $translationID; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if ( ! empty( $translationsToUpdate ) ) { |
| 112 | $this->wpdb->query( |
| 113 | $this->wpdb->prepare( |
| 114 | " |
| 115 | UPDATE {$this->wpdb->posts} |
| 116 | SET post_date = %s, post_date_gmt = %s |
| 117 | WHERE ID IN (" . DB::prepareIn( $translationsToUpdate ) . ") |
| 118 | ", |
| 119 | $productDate, |
| 120 | $productDateGmt |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |
| 127 |