WC_QTML

The WooCommerce-qTML WC QTML class.

Defined (1)

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

/woocommerce-qtml.php  
  1. class WC_QTML { 
  2.  
  3. var $version = '2.0.9'; 
  4.  
  5. var $enabled_languages; 
  6. var $enabled_locales; 
  7. var $default_language; 
  8. var $current_language; 
  9.  
  10. var $mode; 
  11.  
  12. var $domain_switched = false; 
  13.  
  14. var $email_textdomains = array( 
  15. 'woocommerce' => '/woocommerce/i18n/languages/woocommerce-',  
  16. 'wc_shipment_tracking' => '/woocommerce-shipment-tracking/languages/wc_shipment_tracking-',  
  17. 'woocommerce-bto' => '/woocommerce-composite-products/languages/woocommerce-bto-' 
  18. ); 
  19.  
  20. public function __construct() { 
  21.  
  22. if ( in_array( 'qtranslate/qtranslate.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || in_array( 'mqtranslate/mqtranslate.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 
  23.  
  24. add_action( 'init', array( $this, 'wc_qtml_init' ), 0 ); 
  25.  
  26. // Forces default language in admin area 
  27. add_action( 'plugins_loaded', array($this, 'wc_qtml_plugins_init' ), 1 ); 
  28.  
  29. add_action( 'plugins_loaded', array( $this, 'wc_qtml_plugins_loaded' ), 3 ); 
  30.  
  31. // Debug 
  32. // add_action( 'wp_head', array($this, 'print_debug' ) ); 
  33.  
  34.  
  35. function print_debug() { 
  36. echo '<br/><br/>Locale: '; 
  37. print_r( get_locale() ); 
  38. echo '<br/>Current Language: '; 
  39. print_r( $this->current_language ); 
  40. echo '<br/>Default Lang: '; 
  41. print_r( $this->default_language ); 
  42. echo '<br/>qTrans Lang: '; 
  43. print_r( qtrans_getLanguage() ); 
  44. echo '<br/>Session Lang: '; 
  45. print_r( $_SESSION[ 'qtrans_language' ] ); 
  46.  
  47.  
  48. function wc_qtml_plugins_loaded() { 
  49.  
  50. global $q_config; 
  51.  
  52. session_start(); 
  53.  
  54. $this->enabled_languages = $q_config[ 'enabled_languages' ]; 
  55. $this->default_language = $q_config[ 'default_language' ]; 
  56. $this->enabled_locales = $q_config[ 'locale' ]; 
  57.  
  58. if ( ! is_admin() || $this->is_ajax_woocommerce() ) { 
  59.  
  60. if ( in_array( qtrans_getLanguage(), $this->enabled_languages ) ) { 
  61.  
  62. $this->current_language = qtrans_getLanguage(); 
  63.  
  64. $_SESSION[ 'qtrans_language' ] = $this->current_language; 
  65.  
  66. } elseif ( ! empty( $_SESSION[ 'qtrans_language' ] ) ) { 
  67.  
  68. $this->current_language = $_SESSION[ 'qtrans_language' ]; 
  69. $q_config[ 'language' ] = $this->current_language; 
  70.  
  71. } else { 
  72.  
  73. $this->current_language = $this->default_language; 
  74.  
  75. } else { 
  76. $this->current_language = empty( $q_config[ 'language' ] ) ? $this->default_language : $q_config[ 'language' ]; 
  77.  
  78. // get url mode 
  79.  
  80. // QT_URL_QUERY - query: 1 
  81. // QT_URL_PATH - pre-path: 2 
  82. // QT_URL_DOMAIN - pre-domain: 3 
  83.  
  84. $this->mode = $q_config['url_mode']; 
  85.  
  86. // add textdomain names and locations for e-mail translations 
  87. $this->email_textdomains = apply_filters( 'wc_qtml_email_textdomain_locations', $this->email_textdomains ); 
  88.  
  89.  
  90.  
  91. function wc_qtml_plugins_init() { 
  92.  
  93. // customize localization of admin menu 
  94. if ( apply_filters( 'wc_qtml_admin_default_language', true ) ) { 
  95. // remove_action( 'admin_menu', 'qtrans_adminMenu' ); 
  96.  
  97. add_filter( 'locale', array( $this, 'wc_qtml_admin_locale' ), 1000 ); 
  98. add_filter( 'qtranslate_language', array( $this, 'wc_qtml_lang' ) ); 
  99.  
  100.  
  101. function wc_qtml_init() { 
  102.  
  103. global $woocommerce; 
  104.  
  105. // translate strings 
  106. $filters = array( 
  107. 'option_woocommerce_email_from_name' => 10,  
  108. 'the_title_attribute' => 10,  
  109. 'woocommerce_attribute_label' => 10,  
  110. 'woocommerce_cart_item_name' => 10,  
  111. 'woocommerce_cart_shipping_method_full_label' => 10,  
  112. 'woocommerce_rate_label' => 10,  
  113. 'woocommerce_email_footer_text' => 10,  
  114. 'woocommerce_gateway_description' => 10,  
  115. 'woocommerce_gateway_title' => 10,  
  116. 'woocommerce_page_title' => 10,  
  117. 'woocommerce_order_item_name' => 10,  
  118. 'woocommerce_order_product_title' => 10,  
  119. //'woocommerce_order_shipping_to_display' => 10,  
  120. //'woocommerce_order_subtotal_to_display' => 10,  
  121. 'woocommerce_variation_option_name' => 10,  
  122. 'woocommerce_composite_component_title' => 10,  
  123. 'woocommerce_composite_component_description' => 10,  
  124. 'woocommerce_composited_product_excerpt' => 10,  
  125. 'woocommerce_bto_component_title' => 10,  
  126. 'woocommerce_bto_component_description' => 10,  
  127. 'woocommerce_bto_product_excerpt' => 10,  
  128. 'woocommerce_product_title' => 10,  
  129. 'woocommerce_order_item_display_meta_value' => 10,  
  130. 'woocommerce_short_description' => 10,  
  131. 'woocommerce_bundled_item_title' => 10,  
  132. 'woocommerce_bundled_item_description' => 10 
  133. ); 
  134.  
  135. $filters = apply_filters( 'wc_qtml_translate_string_filters', $filters ); 
  136.  
  137. foreach ( $filters as $id => $priority ) { 
  138. add_filter( $id, array( $this, 'wc_qtml_split' ), $priority ); 
  139.  
  140.  
  141. // translate terms 
  142. $filters = array( 
  143. 'get_term' => 10,  
  144. ); 
  145.  
  146. $filters = apply_filters( 'wc_qtml_translate_term_filters', $filters ); 
  147.  
  148. foreach ( $filters as $id => $priority ) { 
  149. add_filter( $id, array( $this, 'wc_qtml_translate_term' ), $priority ); 
  150.  
  151.  
  152. // translate terms 
  153. $filters = array( 
  154. 'get_terms' => 10,  
  155. 'wp_get_object_terms' => 10,  
  156. ); 
  157.  
  158. $filters = apply_filters( 'wc_qtml_translate_terms_filters', $filters ); 
  159.  
  160. foreach ( $filters as $id => $priority ) { 
  161. add_filter( $id, array( $this, 'wc_qtml_translate_terms' ), $priority ); 
  162.  
  163.  
  164. // translate tax totals 
  165. add_filter( 'woocommerce_order_tax_totals', array( $this, 'wc_qtml_translate_tax_totals' ), 10 ); 
  166.  
  167.  
  168. // translate gateway settings 
  169. $filters = array( 
  170. 'option_woocommerce_bacs_settings' => 10,  
  171. 'option_woocommerce_cheque_settings' => 10,  
  172. 'option_woocommerce_cod_settings' => 10 
  173. ); 
  174.  
  175. $filters = apply_filters( 'wc_qtml_translate_gateway_settings_filters', $filters ); 
  176.  
  177. foreach ( $filters as $id => $priority ) { 
  178. if ( ! is_admin() || $this->is_ajax_woocommerce() ) 
  179. add_filter( $id, array( $this, 'wc_qtml_translate_gateway_settings' ), $priority ); 
  180.  
  181.  
  182. // add query var to URLs 
  183. $filters = array( 
  184. 'post_type_archive_link' => 10,  
  185. 'post_type_link' => 10,  
  186. 'woocommerce_add_to_cart_url' => 10,  
  187. 'woocommerce_breadcrumb_home_url' => 10,  
  188. 'woocommerce_product_add_to_cart_url' => 10,  
  189. 'woocommerce_layered_nav_link' => 10 
  190. ); 
  191.  
  192. $filters = apply_filters( 'wc_qtml_convertURL_filters', $filters ); 
  193.  
  194. foreach ( $filters as $id => $priority ) { 
  195. add_filter( $id, 'qtrans_convertURL', $priority ); 
  196.  
  197. add_filter( 'woocommerce_get_checkout_payment_url', array( $this, 'wc_qtml_checkout_payment_url_filter' ) ); 
  198. add_filter( 'woocommerce_payment_successful_result', array( $this, 'wc_qtml_payment_url' ) ); 
  199. add_filter( 'woocommerce_get_return_url', array( $this, 'wc_qtml_return_url' ) ); 
  200. add_filter( 'woocommerce_get_cancel_order_url', array( $this, 'wc_qtml_return_url' ) ); 
  201.  
  202.  
  203. // fix comment posting 
  204. add_filter( 'site_url', array( $this, 'wc_qtml_add_lang_query_var_to_site_url' ), 10, 2 ); 
  205.  
  206.  
  207. // fix mini-cart 
  208. add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'wc_qtml_cart_loaded_from_session' ) ); 
  209.  
  210.  
  211. // add query var to URLs 
  212. $filters = array( 
  213. 'woocommerce_get_cart_url' => 10,  
  214. 'woocommerce_get_checkout_url' => 10,  
  215. 'woocommerce_get_endpoint_url' => 10,  
  216. 'wp_redirect' => 10,  
  217. 'tml_action_url' => 10 
  218. ); 
  219.  
  220. $filters = apply_filters( 'wc_qtml_url_filters', $filters ); 
  221.  
  222. foreach ( $filters as $id => $priority ) { 
  223. add_filter( $id, array( $this, 'wc_qtml_add_lang_query_var_to_url' ), $priority ); 
  224.  
  225.  
  226. // fix wc params 
  227. $filters = array( 
  228. 'wc_add_to_cart_params' => 10,  
  229. 'wc_cart_fragments_params' => 10,  
  230. 'wc_cart_params' => 10,  
  231. 'wc_checkout_params' => 10,  
  232. 'woocommerce_params' => 10,  
  233. ); 
  234.  
  235. $filters = apply_filters( 'wc_qtml_woocommerce_params_filters', $filters ); 
  236.  
  237. foreach ( $filters as $id => $priority ) { 
  238.  
  239. add_filter( $id, array( $this, 'wc_qtml_add_lang_query_var_to_woocommerce_params' ), $priority ); 
  240.  
  241. // fix endpoints 
  242. //add_filter( 'woocommerce_get_endpoint_url', array( $this, 'wc_qtml_modify_endpoints_url' ) ); 
  243.  
  244. // fix almost everything 
  245. //add_filter( 'clean_url', array( $this, 'wc_qtml_add_lang_query_var_to_url' ) ); 
  246.  
  247. // store lang 
  248. add_action( 'woocommerce_new_order', array( $this, 'wc_qtml_store_order_language' ) ); 
  249.  
  250. // customize localization of customer emails 
  251.  
  252. add_action( 'woocommerce_new_customer_note', array( $this, 'wc_qtml_switch_email_textdomain_with_args' ), 1, 2 ); 
  253.  
  254. add_action( 'woocommerce_order_status_pending_to_processing', array( $this, 'wc_qtml_switch_email_textdomain' ), 1 ); 
  255. add_action( 'woocommerce_order_status_pending_to_on-hold', array( $this, 'wc_qtml_switch_email_textdomain' ), 1 ); 
  256. add_action( 'woocommerce_order_status_completed', array( $this, 'wc_qtml_switch_email_textdomain' ), 1 ); 
  257.  
  258. add_action( 'woocommerce_order_status_changed', array( $this, 'wc_qtml_reset_email_textdomain' ), 1 ); 
  259. add_action( 'woocommerce_before_send_customer_invoice', array( $this, 'wc_qtml_before_send_customer_invoice' ), 1 ); 
  260. add_action( 'woocommerce_before_resend_order_emails', array( $this, 'wc_qtml_before_resend_email' ), 1 ); 
  261.  
  262. // fix shipping method descriptions 
  263. if ( version_compare( $woocommerce->version, '2.1.0' ) < 0 ) 
  264. add_filter( 'woocommerce_available_shipping_methods', array( $this, 'wc_qtml_shipping_methods_filter' ) ); 
  265. else { 
  266. add_filter( 'woocommerce_order_shipping_method', array( $this, 'wc_qtml_order_shipping_methods_filter' ), 10, 2 ); 
  267. add_filter( 'woocommerce_shipping_method_title', array( $this, 'wc_qtml_order_shipping_method_admin_title' ), 10, 2 ); 
  268. add_action ( 'woocommerce_review_order_before_shipping', array( $this, 'wc_qtml_before_shipping' ), 10 ); 
  269.  
  270. // fix product category listing for translate tags 
  271. // add_filter( 'term_links-product_cat', array( $this, 'wc_qtml_term_links_filter' ) ); 
  272.  
  273. // fix taxonomy titles 
  274. //$this->wc_qtml_taxonomies_filter(); 
  275. add_filter( 'woocommerce_attribute_taxonomies', array( $this, 'wc_qtml_attribute_taxonomies_filter' ) ); 
  276. add_filter( 'woocommerce_attribute', array( $this, 'wc_qtml_attribute_filter' ), 10, 3 ); 
  277.  
  278. // hide coupons meta in emails 
  279. //add_filter( 'woocommerce_email_order_meta_keys', array( $this, 'wc_qtml_hide_email_coupons' ) ); 
  280.  
  281. // fix localization of date function 
  282. add_filter( 'date_i18n', array( $this, 'wc_qtml_date_i18n_filter' ), 10, 4); 
  283.  
  284. // fix review comment links and blog comment links 
  285. add_filter( 'get_comment_link', array( $this, 'wc_qtml_get_comment_link_filter' ) ); 
  286. add_filter( 'paginate_links', array( $this, 'wc_qtml_get_comment_page_link_filter' ) ); 
  287.  
  288. // fixes comment_form action 
  289. //add_action('comment_form', array( $this, 'wc_qtml_comment_post_lang' ) ); 
  290. //add_filter('comment_post_redirect', array( $this, 'wc_qtml_comment_post_redirect' ), 10, 2 ); 
  291.  
  292. //add_filter('wp_redirect', array( $this, 'wc_qtml_wp_redirect_filter' ) ); 
  293.  
  294.  
  295.  
  296. /** 
  297. * Check if this is a WooCommerce AJAX request 
  298. */ 
  299. function is_ajax_woocommerce() { 
  300.  
  301. if ( ! isset( $_REQUEST[ 'action' ] ) ) { 
  302. return FALSE; 
  303.  
  304. $actions = array( 
  305. 'woocommerce_add_new_attribute',  
  306. 'woocommerce_add_order_fee',  
  307. 'woocommerce_add_order_item',  
  308. 'woocommerce_add_order_item_meta',  
  309. 'woocommerce_add_order_note',  
  310. 'woocommerce_add_to_cart',  
  311. 'woocommerce_add_variation',  
  312. 'woocommerce_apply_coupon',  
  313. 'woocommerce_calc_line_taxes',  
  314. 'woocommerce_checkout',  
  315. 'woocommerce_delete_order_note',  
  316. 'woocommerce_feature_product',  
  317. 'woocommerce_get_customer_details',  
  318. 'woocommerce_get_refreshed_fragments',  
  319. 'woocommerce_grant_access_to_download',  
  320. 'woocommerce_increase_order_item_stock',  
  321. 'woocommerce_json_search_customers',  
  322. 'woocommerce_json_search_downloadable_products_and_variations',  
  323. 'woocommerce_json_search_products',  
  324. 'woocommerce_json_search_products_and_variations',  
  325. 'woocommerce_link_all_variations',  
  326. 'woocommerce_mark_order_complete',  
  327. 'woocommerce_mark_order_processing',  
  328. 'woocommerce_product_ordering',  
  329. 'woocommerce_reduce_order_item_stock',  
  330. 'woocommerce_remove_order_item',  
  331. 'woocommerce_remove_order_item_meta',  
  332. 'woocommerce_remove_variation',  
  333. 'woocommerce_remove_variations',  
  334. 'woocommerce_revoke_access_to_download',  
  335. 'woocommerce_save_attributes',  
  336. 'woocommerce_term_ordering',  
  337. 'woocommerce_update_order_review',  
  338. 'woocommerce_update_shipping_method',  
  339. 'woo_bto_show_product' 
  340. ); 
  341.  
  342. return in_array( $_REQUEST[ 'action' ], $actions ); 
  343.  
  344. /** 
  345. * Translate terms. 
  346. */ 
  347. public function wc_qtml_translate_terms( $terms ) { 
  348.  
  349. if ( 
  350. is_array( $terms ) 
  351. && count( $terms ) 
  352. ) { 
  353. foreach ( $terms as $key => $term ) { 
  354. $terms[ $key ] = $this->wc_qtml_translate_term( $term ); 
  355.  
  356. return $terms; 
  357.  
  358. /** 
  359. * Translate term name into current (or default) language. 
  360. */ 
  361. public function wc_qtml_translate_term( $term ) { 
  362.  
  363. if ( 
  364. is_object( $term ) 
  365. && isset( $term->name ) 
  366. ) { 
  367. $term = $this->wc_qtml_term_filter( $term ); 
  368.  
  369. return $term; 
  370.  
  371. /** 
  372. * Translate tax totals. 
  373. */ 
  374. public function wc_qtml_translate_tax_totals( $tax_totals ) { 
  375.  
  376. foreach ( $tax_totals as $key => $tax_total ) { 
  377. if ( isset( $tax_total->label ) ) { 
  378. $tax_totals[ $key ]->label = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage( 
  379. $tax_total->label 
  380. ); 
  381.  
  382. return $tax_totals; 
  383.  
  384. /** 
  385. * Translate gateway settings. 
  386. */ 
  387. public function wc_qtml_translate_gateway_settings( $settings ) { 
  388.  
  389. if ( is_array( $settings ) ) { 
  390. $keys = array( 
  391. 'title',  
  392. 'description',  
  393. 'instructions',  
  394. ); 
  395. foreach ( $keys as $key ) { 
  396. if ( isset( $settings[ $key ] ) ) { 
  397. $settings[ $key ] = $this->wc_qtml_split( $settings[ $key ] ); 
  398.  
  399. return $settings; 
  400.  
  401. /** 
  402. * Fixes comment posting. 
  403. */ 
  404. public function wc_qtml_add_lang_query_var_to_site_url( $url, $path ) { 
  405.  
  406. $paths = array( 
  407. '/wp-comments-post.php',  
  408. ); 
  409. if ( in_array( $path, $paths ) ) { 
  410. $url = $this->wc_qtml_add_lang_query_var_to_url( $url ); 
  411.  
  412. return $url; 
  413.  
  414. /** 
  415. * Forces the mini cart contents to be reloaded every time. 
  416. */ 
  417. function wc_qtml_cart_loaded_from_session( $cart ) { 
  418.  
  419. global $woocommerce; 
  420.  
  421. if ( ! empty( $cart->cart_contents ) ) { 
  422. $keys = array_keys( $cart->cart_contents ); 
  423. $firstkey = $keys[0]; 
  424. $woocommerce->cart->cart_contents[ $firstkey ][ 'lang' ] = $this->current_language; 
  425.  
  426.  
  427. function wc_qtml_order_shipping_method_admin_title( $title, $id ) { 
  428.  
  429. if ( is_admin() && ! is_ajax() ) 
  430. return __( $title ); 
  431.  
  432. return $title; 
  433.  
  434. function wc_qtml_before_shipping() { 
  435.  
  436. $packages = WC()->shipping->get_packages(); 
  437.  
  438. foreach ( $packages as $key => $package ) { 
  439. foreach ( $package[ 'rates' ] as $method_id => $data ) 
  440. WC()->shipping->packages[ $key ][ 'rates' ][ $method_id ]->label = __( $data->label ); 
  441.  
  442. function wc_qtml_modify_endpoints_url( $url ) { 
  443.  
  444. if ( $this->mode == 1 ) { 
  445. if ( preg_match( "#\/([&|\?]lang=[^\/]+)#i", $url, $match ) ) { 
  446. $url = preg_replace( "#\/([&|\?]lang=[^\/]+)#i", "", $url ); 
  447. $url = rtrim( $url, '/' ) . $match[0]; 
  448. } else { 
  449. $url = $this->wc_qtml_add_lang_query_var_to_url( $url ); 
  450.  
  451. return $url; 
  452.  
  453. function wc_qtml_split( $text ) { 
  454.  
  455. if ( isset( $GLOBALS[ 'order_lang' ] ) && in_array( $GLOBALS[ 'order_lang' ], $this->enabled_languages ) ) 
  456. $text = qtrans_use( $GLOBALS[ 'order_lang' ], $text ); 
  457. else 
  458. $text = __( $text ); 
  459.  
  460. return $text; 
  461.  
  462.  
  463. function wc_qtml_plugin_url() { 
  464. return plugins_url( basename( plugin_dir_path(__FILE__) ), basename( __FILE__ ) ); 
  465.  
  466. function wc_qtml_frontend_scripts() { 
  467. wp_register_script( 'qt-woo-fragments', $this->wc_qtml_plugin_url() . '/assets/js/wc-cart-fragments-fix.js', array( 'jquery', 'jquery-cookie' ), $this->version, true ); 
  468. wp_enqueue_script( 'qt-woo-fragments' ); 
  469.  
  470. function wc_qtml_dequeue_scripts() { 
  471. // wp_dequeue_script( 'wc-cart-fragments' ); 
  472.  
  473. function wc_qtml_comment_post_lang( $id ) { 
  474. echo '<input type="hidden" name="comment_post_lang" value="' . $this->current_language . '" id="comment_post_lang">'; 
  475.  
  476. function wc_qtml_comment_post_redirect( $location, $comment ) { 
  477.  
  478. if ( ! empty( $_POST['comment_post_lang'] ) ) { 
  479.  
  480. if ( $this->mode == 1 ) { 
  481. $lang = $_POST['comment_post_lang']; 
  482. $lang = rawurlencode( $lang ); 
  483. $arg = array('lang' => $lang ); 
  484. $location = add_query_arg( $arg, $location ); 
  485. elseif ( $this->mode == 2 ) { 
  486. $location = qtrans_convertURL( $location, $_POST['comment_post_lang'], true ); 
  487.  
  488. return $location; 
  489.  
  490. function wc_qtml_wp_redirect_filter( $location ) { 
  491.  
  492. if ( $this->mode == 1 && ( ! is_admin() || $this->is_ajax_woocommerce() ) && strpos( $location, 'wp-admin' ) === false ) { 
  493. $lang = ''; 
  494. if ( strpos( $location, 'lang=' ) === false ) { 
  495. $lang = $this->current_language; 
  496. $lang = rawurlencode( $lang ); 
  497. $arg = array('lang' => $lang ); 
  498. $location = add_query_arg($arg, $location); 
  499. elseif ( $this->mode == 2 && ( !is_admin() || $this->is_ajax_woocommerce() ) && strpos( $location, 'wp-admin' ) === false ) { 
  500. foreach ( $this->enabled_languages as $language ) { 
  501. if ( strpos( $location, '/' . $language . '/' ) > 0 ) { 
  502. return $location; 
  503. $location = str_replace( $this->wc_qtml_strip_protocol( site_url() ), $this->wc_qtml_strip_protocol( site_url() ) . '/' . $this->current_language, $location ); 
  504.  
  505. return $location; 
  506.  
  507.  
  508. function wc_qtml_shipping_methods_filter( $available_methods ) { 
  509. foreach ( $available_methods as $method ) : 
  510. $method->label = __( esc_html( $method->label ), 'woocommerce' ); 
  511. endforeach; 
  512. return $available_methods; 
  513.  
  514. function wc_qtml_order_shipping_methods_filter( $label, $order ) { 
  515. $labels = array(); 
  516.  
  517. $custom_values = get_post_custom_values( 'language', $order->id ); 
  518. $order_lang = $custom_values[0]; 
  519.  
  520. // Backwards compat < 2.1 - get shipping title stored in meta 
  521. if ( $order->shipping_method_title ) { 
  522. $labels[] = qtrans_use( $order_lang, $order->shipping_method_title ); 
  523. } else { 
  524. // 2.1+ get line items for shipping 
  525. $shipping_methods = $order->get_shipping_methods(); 
  526.  
  527. foreach ( $shipping_methods as $shipping ) { 
  528. $labels[] = qtrans_use( $order_lang, $shipping[ 'name' ] ); 
  529.  
  530. return implode( ', ', $labels ); 
  531.  
  532. function wc_qtml_attribute_label_filter( $label ) { 
  533. if ( isset( $GLOBALS['order_lang'] ) && in_array( $GLOBALS['order_lang'], $this->enabled_languages ) ) 
  534. $label = qtrans_use( $GLOBALS['order_lang'], $label ); 
  535. else $label = __( $label ); 
  536. return $label; 
  537.  
  538. function wc_qtml_attribute_taxonomies_filter( $attribute_taxonomies ) { 
  539. if ( $attribute_taxonomies ) { 
  540. foreach ( $attribute_taxonomies as $tax ) 
  541. if ( isset( $tax->attribute_label ) && ! ( strpos($tax->attribute_label, '[:') === false ) ) 
  542. $tax->attribute_label = __( $tax->attribute_label ); 
  543. return $attribute_taxonomies; 
  544.  
  545. function wc_qtml_taxonomies_filter() { 
  546. global $wp_taxonomies; 
  547.  
  548. if ( ! is_admin() ) 
  549. return; 
  550.  
  551. foreach ( $wp_taxonomies as $tax_name => $tax ) { 
  552. if ( $tax->labels ) 
  553. $tax->labels = qtrans_use( $this->current_language, $tax->labels ); 
  554.  
  555. function wc_qtml_term_filter( $term ) { 
  556. if ( $term ) { 
  557. if ( isset( $GLOBALS['order_lang'] ) && in_array( $GLOBALS['order_lang'], $this->enabled_languages ) ) 
  558. $term->name = qtrans_use( $GLOBALS['order_lang'], $term->name ); 
  559. elseif ( is_admin() && ! $this->is_ajax_woocommerce() ) { 
  560. // product categories and terms back end fix 
  561. $screen = get_current_screen(); 
  562. if ( ! empty( $screen ) && ! strstr( $screen->id, 'edit-pa_' ) && empty( $_GET['taxonomy'] ) ) 
  563. $term->name = __( $term->name ); 
  564. else { 
  565. $term->name = __( $term->name ); 
  566. return $term; 
  567.  
  568.  
  569. function wc_qtml_attribute_filter( $list, $attribute, $values ) { 
  570. return wpautop( wptexturize( implode( ', ', qtrans_use( $this->current_language, $values ) ) ) ); 
  571.  
  572. // fixes localization of date_i18n function 
  573. function wc_qtml_date_i18n_filter( $j, $format, $i, $gmt ) { 
  574. if ( strpos( $j, 'of' ) > 0 ) { 
  575. return date(__( 'l jS \of F Y h:i:s A', 'woocommerce' ), $i); 
  576. return $j; 
  577.  
  578. // hides coupons meta in emails 
  579. function wc_qtml_hide_email_coupons( $fields ) { 
  580. $empty_fields = array(); 
  581. return $empty_fields; 
  582.  
  583. function wc_qtml_get_comment_link_filter( $url ) { 
  584.  
  585. if ( $this->mode == 1 ) { 
  586. if( preg_match( "#(\?)lang=([^/]+/)#i", $url, $match ) ) { 
  587. $url = preg_replace( "#(\?)lang=([^/]+/)#i", "", $url ); 
  588. $url = preg_replace( "#(/)(\#)#i", '/'.rtrim( $match[0], '/' ).'#', $url ); 
  589. } else { 
  590. $url = preg_replace( "#(/)(\#)#i", '/'.'?lang='. $this->current_language .'#', $url ); 
  591. return $url; 
  592.  
  593. function wc_qtml_get_comment_page_link_filter( $url ) { 
  594.  
  595. if ( $this->mode == 1 ) { 
  596. if( preg_match( "#(\?)lang=([^/]+/)#i", $url, $match ) ) { 
  597. $url = preg_replace( "#(\?)lang=([^/]+/)#i", "", $url ); 
  598. $url = preg_replace( "#(/)(\#)#i", '/'.rtrim( $match[0], '/' ).'#', $url ); 
  599. } else { 
  600. $url = preg_replace( "#(/)(\#)#i", '/'.'?lang='. $this->current_language .'#', $url ); 
  601. return $url; 
  602.  
  603. function woocommerce_layered_nav_link_filter( $link ) { 
  604. return qtrans_convertURL( $link, $this->current_language ); 
  605.  
  606. function wc_qtml_checkout_payment_url_filter( $url ) { 
  607. if ( !is_admin() ) { 
  608. $url = qtrans_convertURL( $url, $this->current_language ); 
  609. } else { 
  610. if ( preg_match( "#(&|\?)order_id=([^&\#]+)#i", $url, $match ) ) { 
  611. $order_id = $match[2]; 
  612. $custom_values = get_post_custom_values( 'language', $order_id ); 
  613. $order_lang = $custom_values[0]; 
  614. $url = qtrans_convertURL( $url, $order_lang, true ); 
  615. return $url; 
  616.  
  617. // fixes product category listing for translate tags 
  618. function wc_qtml_term_links_filter( $term_links ) { 
  619. $fixed_links = array(); 
  620.  
  621. foreach ( $term_links as $term_link ) { 
  622. $start = strpos($term_link, '">') + 2; 
  623. $end = strpos($term_link, '</'); 
  624. $term = substr($term_link, $start, $end - $start); 
  625. $fixed_link = str_replace($term, __($term, 'custom'), $term_link); 
  626. $fixed_links[] = $fixed_link; 
  627. return $fixed_links; 
  628.  
  629. // resets admin locale to default only 
  630. function wc_qtml_admin_locale( $loc ) { 
  631. if ( is_admin() && ! is_ajax() ) { 
  632. $loc = $this->enabled_locales[ $this->default_language ]; 
  633. return $loc; 
  634.  
  635. function wc_qtml_lang( $lang ) { 
  636. if ( is_admin() && ! $this->is_ajax_woocommerce() ) { 
  637. return 'en'; 
  638. return $lang; 
  639.  
  640. function wc_qtml_reset_email_textdomain( $order_id ) { 
  641.  
  642. if ( $this->domain_switched ) { 
  643.  
  644. foreach ( $this->email_textdomains as $domain => $location ) { 
  645.  
  646. $mofile = WP_PLUGIN_DIR . $location . $this->enabled_locales[ $this->current_language ] . '.mo'; 
  647.  
  648. if ( file_exists( $mofile ) ) { 
  649. unload_textdomain( $domain ); 
  650. load_textdomain( $domain, $mofile ); 
  651.  
  652. $this->domain_switched = false; 
  653.  
  654. function wc_qtml_switch_email_textdomain( $order_id ) { 
  655.  
  656. if ( $order_id > 0 ) { 
  657. $custom_values = get_post_custom_values( 'language', $order_id ); 
  658. $order_lang = $custom_values[0]; 
  659. if ( isset( $order_lang ) && $order_lang != '' ) { 
  660. $GLOBALS['order_lang'] = $order_lang; 
  661.  
  662. foreach ( $this->email_textdomains as $domain => $location ) { 
  663.  
  664. $mofile = WP_PLUGIN_DIR . $location . $this->enabled_locales[$order_lang] . '.mo'; 
  665.  
  666. if ( file_exists( $mofile ) ) { 
  667. unload_textdomain( $domain ); 
  668. load_textdomain( $domain, $mofile ); 
  669.  
  670. $this->domain_switched = true; 
  671.  
  672. } else { $GLOBALS['order_lang'] = $this->current_language; } 
  673.  
  674. function wc_qtml_switch_email_textdomain_with_args ( $args ) { 
  675. $defaults = array( 
  676. 'order_id' => '',  
  677. 'customer_note' => '' 
  678. ); 
  679.  
  680. $args = wp_parse_args( $args, $defaults ); 
  681.  
  682. extract( $args ); 
  683.  
  684. $this->wc_qtml_switch_email_textdomain( $order_id ); 
  685.  
  686. function wc_qtml_before_resend_email( $order ) { 
  687. $this->wc_qtml_switch_email_textdomain( $order->id ); 
  688.  
  689. function wc_qtml_before_send_customer_invoice( $order ) { 
  690. $this->wc_qtml_switch_email_textdomain( $order->id ); 
  691.  
  692. function wc_qtml_after_send_customer_invoice( $order ) { 
  693. $domain = 'woocommerce'; 
  694. unload_textdomain( $domain ); 
  695. load_textdomain( $domain, WP_PLUGIN_DIR . '/woocommerce/languages/woocommerce-' . $this->enabled_locales[$this->current_language] . '.mo' ); 
  696.  
  697. // stores order language in order object meta 
  698. function wc_qtml_store_order_language( $order_id ) { 
  699. if( ! get_post_meta( $order_id, 'language' ) ) { 
  700. $language = $this->current_language; 
  701. update_post_meta( $order_id, 'language', $language ); 
  702.  
  703. function wc_qtml_add_lang_query_var_to_woocommerce_params( $params ) { 
  704.  
  705. if ( ! is_array( $params ) ) { 
  706. return $params; 
  707.  
  708. $keys = array( 
  709. 'ajax_url',  
  710. 'checkout_url',  
  711. 'cart_url' 
  712. ); 
  713.  
  714. foreach ( $keys as $key ) { 
  715. if ( isset( $params[ $key ] ) ) { 
  716. $params[ $key ] = $this->wc_qtml_add_lang_query_var_to_url( $params[ $key ] ); 
  717.  
  718. return $params; 
  719.  
  720. function wc_qtml_add_lang_query_var_to_url( $url ) { 
  721.  
  722. if ( ( ! is_admin() || $this->is_ajax_woocommerce() ) && strpos( $this->wc_qtml_strip_protocol( $url ), $this->wc_qtml_strip_protocol( site_url() ) . '/' . $this->current_language . '/' ) === false ) { 
  723. $url = str_replace( '&', '&', $url ); 
  724. $url = str_replace( '&', '&', $url ); 
  725. $url = add_query_arg( 'lang', $this->current_language, remove_query_arg( 'lang', $url ) ); 
  726. return $url; 
  727.  
  728. function wc_qtml_return_url( $return_url ) { 
  729.  
  730. if ( $this->mode == 1 ) 
  731. $return_url = add_query_arg( 'lang', $this->current_language, $return_url ); 
  732. elseif ( $this->mode == 2 && strpos( str_replace( array( 'https:', 'http:' ), '', $return_url ), str_replace( array( 'https:', 'http:' ), '', site_url() ) . '/' . $this->current_language . '/' ) === false ) 
  733. $return_url = str_replace( str_replace( array( 'https:', 'http:' ), '', site_url() ), str_replace( array( 'https:', 'http:' ), '', site_url() ) . '/' . $this->current_language, $return_url ); 
  734.  
  735. return $return_url; 
  736.  
  737. function wc_qtml_payment_url( $result ) { 
  738.  
  739. if ( $this->mode == 1 ) 
  740. $result['redirect'] = add_query_arg( 'lang', $this->current_language, $result['redirect'] ); 
  741. elseif ( $this->mode == 2 && strpos( str_replace( array( 'https:', 'http:' ), '', $result['redirect'] ), str_replace( array( 'https:', 'http:' ), '', site_url() ) . '/' . $this->current_language . '/' ) === false ) 
  742. $result['redirect'] = str_replace( str_replace( array( 'https:', 'http:' ), '', site_url() ), str_replace( array( 'https:', 'http:' ), '', site_url() ) . '/' . $this->current_language, $result['redirect'] ); 
  743.  
  744. return $result; 
  745.  
  746. function wc_qtml_remove_accents( $st ) { 
  747. $replacement = array( 
  748. "*"=>"*", "*"=>"*", "*"=>"*", "*"=>"*", "*"=>"*", "*"=>"*",  
  749. "*"=>"*" 
  750. ); 
  751.  
  752. foreach( $replacement as $i=>$u ) { 
  753. $st = mb_eregi_replace( $i, $u, $st ); 
  754. return $st; 
  755.  
  756. function wc_qtml_strip_protocol( $url ) { 
  757. // removes everything from start of url to last occurence of char in charlist 
  758.  
  759. $char = '//'; 
  760.  
  761. $pos = strrpos( $url, $char ); 
  762.  
  763. $url_stripped = substr( $url, $pos + 2 ); 
  764.  
  765. return $url_stripped; 
  766.  
  767.