WCML_Adventure_tours

The WooCommerce Multilingual WCML Adventure tours class.

Defined (1)

The class is defined in the following location(s).

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