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