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 4.7.6 4.7.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 / compatibility / AdventureTours / class-wcml-adventure-tours.php
woocommerce-multilingual / compatibility / AdventureTours Last commit date
Factory.php 4 weeks ago class-wcml-adventure-tours.php 4 weeks ago
class-wcml-adventure-tours.php
368 lines
1 <?php
2
3 use WCML\Utilities\AdminPages;
4
5 class WCML_Adventure_Tours implements \IWPML_Action {
6
7 private $woocommerce_wpml;
8 private $sitepress;
9
10 private $tp;
11
12 public function __construct( woocommerce_wpml $woocommerce_wpml, SitePress $sitepress, WPML_Element_Translation_Package $tp ) {
13 $this->woocommerce_wpml = $woocommerce_wpml;
14 $this->sitepress = $sitepress;
15 $this->tp = $tp;
16 }
17
18 public function add_hooks() {
19 add_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10, 4 );
20
21 add_filter( 'wpml_tm_translation_job_data', [ $this, 'append_tour_data_translation_package' ], 10, 2 );
22 add_action( 'wpml_translation_job_saved', [ $this, 'save_tour_data_translation' ], 10, 3 );
23
24 add_filter( 'wcml_is_variable_product', [ $this, 'is_variable_tour' ], 10, 2 );
25 add_filter( 'wcml_variation_term_taxonomy_ids', [ $this, 'add_tour_tax_id' ] );
26
27 if ( is_admin() ) {
28 add_action( 'wcml_gui_additional_box_html', [ $this, 'custom_box_html' ], 10, 3 );
29 add_filter( 'wcml_gui_additional_box_data', [ $this, 'custom_box_html_data' ], 10, 4 );
30 add_action( 'wcml_update_extra_fields', [ $this, 'tour_data_update' ], 10, 3 );
31
32 add_action( 'admin_footer', [ $this, 'load_assets' ] );
33 add_action( 'wcml_after_custom_prices_block', [ $this, 'add_custom_prices_block' ] );
34 add_action( 'wcml_after_save_custom_prices', [ $this, 'save_custom_costs' ] );
35
36 add_filter( 'wcml_is_attributes_page', [ $this, 'is_attributes_page' ] );
37
38 add_filter(
39 'wcml_do_not_display_custom_fields_for_product',
40 [
41 $this,
42 'replace_tm_editor_custom_fields_with_own_sections',
43 ]
44 );
45 } elseif ( WCML_MULTI_CURRENCIES_INDEPENDENT === $this->woocommerce_wpml->settings['enable_multi_currency'] ) {
46 add_filter( 'get_post_metadata', [ $this, 'product_price_filter' ], 9, 4 );
47 }
48 }
49
50
51 public function sync_tour_data_across_translations( $meta_id, $post_id, $meta_key, $tour_tabs_meta ) {
52 if ( 'tour_tabs_meta' !== $meta_key ) {
53 return false;
54 }
55
56 $post = get_post( $post_id );
57
58 if ( 'auto-draft' === $post->post_status || isset( $_POST['autosave'] ) ) {
59 return false;
60 }
61
62 if ( 'product' === $post->post_type ) {
63
64 remove_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10 );
65
66 $original_product_id = $post_id;
67 if ( ! $this->woocommerce_wpml->products->is_original_product( $post_id ) ) {
68 $original_product_language = $this->woocommerce_wpml->products->get_original_product_language( $post_id );
69 $original_product_id = apply_filters( 'wpml_object_id', $post_id, 'product', true, $original_product_language );
70 }
71
72 $product_trid = $this->sitepress->get_element_trid( $original_product_id, 'post_product' );
73 $product_translations = $this->sitepress->get_element_translations( $product_trid, 'post_product' );
74
75 foreach ( $product_translations as $product_translation ) {
76
77 if ( empty( $product_translation->original ) ) {
78
79 $trnsl_tour_tabs_meta = get_post_meta( $product_translation->element_id, 'tour_tabs_meta', true );
80
81 $trnsl_tour_tabs_meta['tour_badge'] = $tour_tabs_meta['tour_badge'];
82
83 update_post_meta( $product_translation->element_id, 'tour_tabs_meta', $trnsl_tour_tabs_meta );
84
85 }
86 }
87
88 add_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10, 4 );
89
90 }
91
92 }
93
94 public function custom_box_html( $obj, $product_id, $data ) {
95
96 if ( $tour_tabs_meta = get_post_meta( $product_id, 'tour_tabs_meta', true ) ) {
97
98 $tour_section = new WPML_Editor_UI_Field_Section( __( 'Tour Data', 'woocommerce-multilingual' ) );
99
100 $divider = true;
101
102 foreach ( $tour_tabs_meta['tabs'] as $tour_tab_id => $tour_tab_meta ) {
103
104 $group = new WPML_Editor_UI_Field_Group( '', $divider );
105 $composite_field = new WPML_Editor_UI_Single_Line_Field( 'adventure_tour_' . $tour_tab_id . '_title', __( 'Title', 'woocommerce-multilingual' ), $data, false );
106 $group->add_field( $composite_field );
107 $composite_field = new WPML_Editor_UI_Single_Line_Field( 'adventure_tour_' . $tour_tab_id . '_content', __( 'Content', 'woocommerce-multilingual' ), $data, false );
108 $group->add_field( $composite_field );
109 $tour_section->add_field( $group );
110
111 }
112
113 if ( ! empty( $tour_tabs_meta['tabs'] ) ) {
114 $obj->add_field( $tour_section );
115 }
116 }
117
118 }
119
120 public function custom_box_html_data( $data, $product_id, $translation, $lang ) {
121
122 if ( $tour_tabs_meta = get_post_meta( $product_id, 'tour_tabs_meta', true ) ) {
123
124 foreach ( $tour_tabs_meta['tabs'] as $tour_tab_id => $tour_tab_meta ) {
125 $data[ 'adventure_tour_' . $tour_tab_id . '_title' ] = [ 'original' => $tour_tab_meta['title'] ];
126 $data[ 'adventure_tour_' . $tour_tab_id . '_content' ] = [ 'original' => $tour_tab_meta['content'] ];
127 }
128
129 if ( $translation ) {
130 $translated_tour_tabs_meta = get_post_meta( $translation->ID, 'tour_tabs_meta', true );
131
132 if ( $translated_tour_tabs_meta ) {
133 foreach ( $translated_tour_tabs_meta['tabs'] as $tour_tab_id => $tour_tab_meta ) {
134 $data[ 'adventure_tour_' . $tour_tab_id . '_title' ]['translation'] = $tour_tab_meta['title'];
135 $data[ 'adventure_tour_' . $tour_tab_id . '_content' ]['translation'] = $tour_tab_meta['content'];
136 }
137 }
138 }
139 }
140
141 return $data;
142 }
143
144 public function tour_data_update( $original_product_id, $product_id, $data ) {
145
146 $tour_tabs_meta = get_post_meta( $original_product_id, 'tour_tabs_meta', true );
147
148 if ( isset( $tour_tabs_meta['tabs'] ) && is_array( $tour_tabs_meta['tabs'] ) ) {
149 foreach ( $tour_tabs_meta['tabs'] as $tour_tab_id => $tour_tab_meta ) {
150
151 if ( ! empty( $data[ md5( 'adventure_tour_' . $tour_tab_id . '_title' ) ] ) ) {
152 $tour_tabs_meta['tabs'][ $tour_tab_id ]['title'] = $data[ md5( 'adventure_tour_' . $tour_tab_id . '_title' ) ];
153 }
154
155 if ( ! empty( $data[ md5( 'adventure_tour_' . $tour_tab_id . '_content' ) ] ) ) {
156 $tour_tabs_meta['tabs'][ $tour_tab_id ]['content'] = $data[ md5( 'adventure_tour_' . $tour_tab_id . '_content' ) ];
157 }
158 }
159 remove_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10 );
160
161 update_post_meta( $product_id, 'tour_tabs_meta', $tour_tabs_meta );
162
163 add_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10, 4 );
164 }
165
166 }
167
168 public function append_tour_data_translation_package( $package, $post ) {
169
170 if ( 'product' === $post->post_type ) {
171
172 $tour_tabs_meta = get_post_meta( $post->ID, 'tour_tabs_meta', true );
173
174 if ( $tour_tabs_meta ) {
175
176 $fields = [ 'title', 'content' ];
177
178 foreach ( $tour_tabs_meta['tabs'] as $tour_tab_id => $tour_tab_meta ) {
179
180 foreach ( $fields as $field ) {
181 if ( ! empty( $tour_tab_meta[ $field ] ) ) {
182
183 $package['contents'][ 'wc_adventure_tour:' . $tour_tab_id . ':' . $field ] = [
184 'translate' => 1,
185 'data' => $this->tp->encode_field_data( $tour_tab_meta[ $field ] ),
186 'format' => 'base64',
187 ];
188
189 }
190 }
191 }
192 }
193 }
194
195 return $package;
196
197 }
198
199 public function save_tour_data_translation( $post_id, $data, $job ) {
200
201 $translated_tour_data = [];
202 foreach ( $data as $value ) {
203
204 if ( preg_match( '/wc_adventure_tour:([0-9]+):(.+)/', $value['field_type'], $matches ) ) {
205
206 $tour_tab_id = $matches[1];
207 $field = $matches[2];
208
209 $translated_tour_data[ $tour_tab_id ][ $field ] = $value['data'];
210
211 }
212 }
213
214 if ( $translated_tour_data ) {
215
216 $tour_tabs_meta = get_post_meta( $job->original_doc_id, 'tour_tabs_meta', true );
217
218 foreach ( $tour_tabs_meta['tabs'] as $tour_tab_id => $tour_tab_meta ) {
219
220 if ( isset( $translated_tour_data[ $tour_tab_id ]['title'] ) ) {
221 $tour_tabs_meta['tabs'][ $tour_tab_id ]['title'] = $translated_tour_data[ $tour_tab_id ]['title'];
222 }
223
224 if ( isset( $translated_tour_data[ $tour_tab_id ]['content'] ) ) {
225 $tour_tabs_meta['tabs'][ $tour_tab_id ]['content'] = $translated_tour_data[ $tour_tab_id ]['content'];
226 }
227 }
228
229 remove_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10 );
230 update_post_meta( $post_id, 'tour_tabs_meta', $tour_tabs_meta );
231 add_action( 'updated_post_meta', [ $this, 'sync_tour_data_across_translations' ], 10, 4 );
232 }
233
234 }
235
236 public function load_assets() {
237 global $pagenow;
238
239 if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
240 wp_register_script( 'wcml-adventure-tours', WCML_PLUGIN_URL . '/compatibility/res/js/wcml-adventure-tours.js', [ 'jquery' ], WCML_VERSION, true );
241 wp_enqueue_script( 'wcml-adventure-tours' );
242 }
243 }
244
245 public function add_custom_prices_block( $product_id ) {
246
247 if ( 'new' !== $product_id ) {
248 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies();
249 $tour_booking_periods = get_post_meta( $product_id, 'tour_booking_periods', true );
250 $custom_periods_prices = get_post_meta( $product_id, 'custom_booking_periods_prices', true );
251 if ( $tour_booking_periods ) {
252 foreach ( $tour_booking_periods as $per_key => $tour_booking_period ) {
253 foreach ( $currencies as $key => $currency ) {
254
255 $value = isset( $custom_periods_prices[ $per_key ][ $key ] ) ? $custom_periods_prices[ $per_key ][ $key ] : '';
256
257 echo '<div class="wcml_custom_cost_field" data-tour="' . esc_html( $per_key ) . '" style="display: none;">';
258 echo '<div>' . wp_kses_post( get_woocommerce_currency_symbol( $key ) ) . '</div>';
259 echo '<input type="text" class="wc_input_price" style="width: 60px;" name="tour_spec_price[' . esc_html( $per_key ) . '][' . esc_html( $key ) . ']" value="' . esc_html( $value ) . '" />';
260 echo '</div>';
261 }
262 }
263 }
264
265 echo '<div class="wcml_custom_cost_field_empty" style="display: none;">';
266 echo '<div></div>';
267 echo '<input type="text" class="wc_input_price" style="width: 60px;" name="tour_spec_price" value="" />';
268 echo '</div>';
269 }
270 }
271
272 public function save_custom_costs( $post_id ) {
273
274 $tour_spec_price = [];
275 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies();
276
277 if ( isset( $_POST['tour_spec_price'] ) && is_array( $_POST['tour_spec_price'] ) ) {
278
279 foreach ( $_POST['tour_spec_price'] as $per_key => $costs ) {
280
281 foreach ( $currencies as $code => $currency ) {
282
283 $tour_spec_price[ $per_key ][ $code ] = $costs[ $code ];
284
285 }
286 }
287
288 update_post_meta( $post_id, 'custom_booking_periods_prices', $tour_spec_price );
289 }
290 }
291
292 public function product_price_filter( $value, $object_id, $meta_key, $single ) {
293
294 if (
295 $meta_key === 'tour_booking_periods' &&
296 get_post_type( $object_id ) === 'product' &&
297 ( $currency = $this->woocommerce_wpml->multi_currency->get_client_currency() ) !== wcml_get_woocommerce_currency_option()
298 ) {
299
300 remove_filter( 'get_post_metadata', [ $this, 'product_price_filter' ], 9 );
301
302 $original_language = $this->woocommerce_wpml->products->get_original_product_language( $object_id );
303 $original_product = apply_filters( 'wpml_object_id', $object_id, 'product', true, $original_language );
304
305 if ( get_post_meta( $original_product, '_wcml_custom_prices_status' ) ) {
306 $custom_periods_prices = get_post_meta( $object_id, 'custom_booking_periods_prices', true );
307 $tours_data = get_post_meta( $object_id, 'tour_booking_periods', true );
308 if ( $tours_data ) {
309 foreach ( $tours_data as $key => $periods ) {
310 if ( isset( $custom_periods_prices[ $key ][ $currency ] ) ) {
311 $tours_data[ $key ]['spec_price'] = $custom_periods_prices[ $key ][ $currency ];
312 }
313 }
314
315 if ( $single ) {
316 $value[0] = $tours_data;
317 } else {
318 $value = $tours_data;
319 }
320 }
321 }
322 add_filter( 'get_post_metadata', [ $this, 'product_price_filter' ], 9, 4 );
323 }
324
325 return $value;
326 }
327
328 public function add_tour_tax_id( $variation_term_taxonomy_ids ) {
329 global $wpdb;
330 $tour_taxonomy_id = $wpdb->get_var( "SELECT tt.term_taxonomy_id FROM {$wpdb->terms} AS t LEFT JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE t.name = 'tour' AND tt.taxonomy = 'product_type'" );
331
332 if ( $tour_taxonomy_id ) {
333 $variation_term_taxonomy_ids[] = $tour_taxonomy_id;
334 }
335
336 return $variation_term_taxonomy_ids;
337
338 }
339
340 public function is_variable_tour( $is_variable, $product_id ) {
341 $var_tour_meta = get_post_meta( $product_id, '_variable_tour', true );
342
343 if ( $is_variable && 'yes' === $var_tour_meta ) {
344 $is_variable = true;
345 } elseif ( 'no' === $var_tour_meta ) {
346 $is_variable = false;
347 }
348
349 return $is_variable;
350 }
351
352 public function is_attributes_page( $is_attributes_page ) {
353
354 if ( AdminPages::isPage( 'product_attributes_extended' ) && isset( $_GET['post_type'] ) && 'product' === $_GET['post_type'] ) {
355 $is_attributes_page = true;
356 }
357
358 return $is_attributes_page;
359 }
360
361 public function replace_tm_editor_custom_fields_with_own_sections( $fields ) {
362 $fields[] = 'tour_tabs_meta';
363
364 return $fields;
365 }
366
367 }
368