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 |