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.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Synchronization / Component / Post.php
woocommerce-multilingual / classes / Synchronization / Component Last commit date
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