WCML_Endpoints

The WooCommerce Multilingual WCML Endpoints class.

Defined (1)

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

/inc/class-wcml-endpoints.php  
  1. class WCML_Endpoints{ 
  2.  
  3. /** @var woocommerce_wpml */ 
  4. private $woocommerce_wpml; 
  5. var $endpoints_strings = array(); 
  6.  
  7. function __construct( $woocommerce_wpml ) { 
  8.  
  9. $this->woocommerce_wpml =& $woocommerce_wpml; 
  10. add_action( 'icl_ajx_custom_call', array( $this, 'rewrite_rule_endpoints' ), 11, 2 ); 
  11. add_action( 'woocommerce_update_options', array( $this, 'add_endpoints' ) ); 
  12. add_filter( 'pre_update_option_rewrite_rules', array( $this, 'update_rewrite_rules' ), 100, 2 ); 
  13.  
  14. add_filter( 'page_link', array( $this, 'endpoint_permalink_filter' ), 10, 2 ); //after WPML 
  15.  
  16. if( !is_admin() ) { 
  17. //endpoints hooks 
  18. $this->maybe_flush_rules(); 
  19. $this->register_endpoints_translations(); 
  20. add_filter('pre_get_posts', array($this, 'check_if_endpoint_exists')); 
  21.  
  22. add_filter( 'woocommerce_get_endpoint_url', array( $this, 'filter_get_endpoint_url' ), 10, 4 ); 
  23.  
  24. add_filter( 'woocommerce_settings_saved', array( $this, 'update_original_endpoints_strings') ); 
  25.  
  26.  
  27. function register_endpoints_translations( $language = null ) { 
  28.  
  29. if( !class_exists( 'WooCommerce' ) || !defined( 'ICL_SITEPRESS_VERSION' ) || ICL_PLUGIN_INACTIVE || version_compare( WOOCOMMERCE_VERSION, '2.2', '<' ) ) return false; 
  30.  
  31. $wc_vars = WC()->query->query_vars; 
  32.  
  33. if ( !empty( $wc_vars ) ) { 
  34. $query_vars = array( 
  35. // Checkout actions 
  36. 'order-pay' => $this->get_endpoint_translation( 'order-pay', $wc_vars['order-pay'], $language ),  
  37. 'order-received' => $this->get_endpoint_translation( 'order-received', $wc_vars['order-received'], $language ),  
  38.  
  39. // My account actions 
  40. 'view-order' => $this->get_endpoint_translation( 'view-order', $wc_vars['view-order'], $language ),  
  41. 'edit-account' => $this->get_endpoint_translation( 'edit-account', $wc_vars['edit-account'], $language ),  
  42. 'edit-address' => $this->get_endpoint_translation( 'edit-address', $wc_vars['edit-address'], $language ),  
  43. 'lost-password' => $this->get_endpoint_translation( 'lost-password', $wc_vars['lost-password'], $language ),  
  44. 'customer-logout' => $this->get_endpoint_translation( 'customer-logout', $wc_vars['customer-logout'], $language ),  
  45. 'add-payment-method' => $this->get_endpoint_translation( 'add-payment-method', $wc_vars['add-payment-method'], $language ) 
  46. ); 
  47.  
  48. if( isset( $wc_vars['orders'] ) ) $query_vars[ 'orders' ] = $this->get_endpoint_translation( 'orders', $wc_vars['orders'], $language ); 
  49. if( isset( $wc_vars['downloads'] ) ) $query_vars[ 'downloads' ] = $this->get_endpoint_translation( 'downloads', $wc_vars['downloads'], $language ); 
  50. if( isset( $wc_vars['payment-methods'] ) ) $query_vars[ 'payment-methods' ] = $this->get_endpoint_translation( 'payment-methods', $wc_vars['payment-methods'], $language ); 
  51. if( isset( $wc_vars['delete-payment-method'] ) ) $query_vars[ 'delete-payment-method' ] = $this->get_endpoint_translation( 'delete-payment-method', $wc_vars['delete-payment-method'], $language ); 
  52. if( isset( $wc_vars['set-default-payment-method'] ) ) $query_vars[ 'set-default-payment-method' ] = $this->get_endpoint_translation( 'set-default-payment-method', $wc_vars['set-default-payment-method'], $language ); 
  53.  
  54. $query_vars = apply_filters( 'wcml_register_endpoints_query_vars', $query_vars, $wc_vars, $this ); 
  55.  
  56. $query_vars = array_merge( $wc_vars , $query_vars ); 
  57. WC()->query->query_vars = $query_vars; 
  58.  
  59.  
  60. return WC()->query->query_vars; 
  61.  
  62. function get_endpoint_translation( $key, $endpoint, $language = null ) { 
  63.  
  64. $this->register_endpoint_string( $key, $endpoint ); 
  65.  
  66. if( function_exists('icl_t') ) { 
  67. $trnsl = apply_filters( 'wpml_translate_single_string', $endpoint, 'WooCommerce Endpoints', $key, $language ); 
  68.  
  69. if( !empty( $trnsl ) ) { 
  70. return $trnsl; 
  71. }else{ 
  72. return $endpoint; 
  73. }else{ 
  74. return $endpoint; 
  75.  
  76. public function register_endpoint_string( $key, $endpoint ) { 
  77.  
  78. $string = icl_get_string_id( $endpoint, 'WooCommerce Endpoints', $key ); 
  79.  
  80. if( !$string && function_exists( 'icl_register_string' ) ) { 
  81. do_action( 'wpml_register_single_string', 'WooCommerce Endpoints', $key, $endpoint ); 
  82. }else{ 
  83. $this->endpoints_strings[] = $string; 
  84.  
  85.  
  86. function rewrite_rule_endpoints( $call, $data ) { 
  87.  
  88. if( $call == 'icl_st_save_translation' && in_array( $data['icl_st_string_id'], $this->endpoints_strings ) ) { 
  89. $this->add_endpoints(); 
  90. $this->flush_rules_for_endpoints_translations(); 
  91.  
  92. function flush_rules_for_endpoints_translations( ) { 
  93. add_option( 'flush_rules_for_endpoints_translations', true ); 
  94.  
  95. function maybe_flush_rules() { 
  96. if( get_option( 'flush_rules_for_endpoints_translations' ) ) { 
  97. delete_option( 'flush_rules_for_endpoints_translations' ); 
  98. WC()->query->init_query_vars(); 
  99. WC()->query->add_endpoints(); 
  100. WC()->query->query_vars = apply_filters( 'wcml_flush_rules_query_vars', WC()->query->query_vars, $this ); 
  101.  
  102. remove_filter( 'gettext_with_context', array( $this->woocommerce_wpml->strings, 'category_base_in_strings_language' ), 99, 3 ); 
  103. flush_rewrite_rules( false ); 
  104. add_filter( 'gettext_with_context', array( $this->woocommerce_wpml->strings, 'category_base_in_strings_language' ), 99, 3 ); 
  105. delete_option( 'flush_rules_for_endpoints_translations' ); 
  106.  
  107. function update_rewrite_rules( $value, $old_value ) { 
  108. $this->add_endpoints(); 
  109. $this->flush_rules_for_endpoints_translations(); 
  110.  
  111. return $value; 
  112.  
  113. function add_endpoints() { 
  114. if( !isset( $this->endpoints_strings ) ) 
  115. return; 
  116.  
  117. global $wpdb; 
  118. //add endpoints and flush rules 
  119. foreach( $this->endpoints_strings as $string_id ) { 
  120.  
  121. $string_translations = icl_get_string_translations_by_id( $string_id ); 
  122.  
  123. foreach( $string_translations as $string ) { 
  124. add_rewrite_endpoint( $string['value'], EP_ROOT | EP_PAGES ); 
  125.  
  126.  
  127. function endpoint_permalink_filter( $p, $pid ) { 
  128. global $post; 
  129.  
  130. if( isset($post->ID) && !is_admin() && version_compare( WOOCOMMERCE_VERSION, '2.2', '>=' ) && defined( 'ICL_SITEPRESS_VERSION' ) && !ICL_PLUGIN_INACTIVE ) { 
  131. global $wp, $sitepress; 
  132.  
  133. $current_lang = $sitepress->get_current_language(); 
  134. $page_lang = $sitepress->get_language_for_element( $post->ID, 'post_page'); 
  135.  
  136. if( 
  137. $current_lang != $page_lang && 
  138. apply_filters( 'translate_object_id', $pid, 'page', false, $page_lang ) == $post->ID 
  139. ) || 
  140. apply_filters( 'wcml_endpoint_force_permalink_filter', false, $current_lang, $page_lang ) 
  141. ) { 
  142.  
  143. $endpoints = WC()->query->get_query_vars(); 
  144.  
  145. foreach( $endpoints as $key => $endpoint ) { 
  146. if( isset($wp->query_vars[$key]) ) { 
  147. if( $key === 'order-pay' ) { 
  148. $endpoint = get_option( 'woocommerce_checkout_pay_endpoint' ); 
  149. $p .= isset( $_SERVER[ 'QUERY_STRING' ] ) ? '?'.$_SERVER[ 'QUERY_STRING' ] : ''; 
  150. }elseif( $key === 'order-received' ) { 
  151. $endpoint = get_option( 'woocommerce_checkout_order_received_endpoint' ); 
  152. }else{ 
  153. $endpoint = get_option( 'woocommerce_myaccount_'.str_replace( '-', '_', $key).'_endpoint', $endpoint ); 
  154.  
  155. $endpoint = apply_filters( 'wcml_endpoint_permalink_filter', $endpoint, $key ); 
  156.  
  157. $p = $this->get_endpoint_url( $this->get_endpoint_translation( $key, $endpoint, $current_lang ), $wp->query_vars[ $key ], $p, $page_lang ); 
  158.  
  159. return $p; 
  160.  
  161. function get_endpoint_url($endpoint, $value = '', $permalink = '', $page_lang = false ) { 
  162. global $sitepress; 
  163.  
  164. if( $page_lang ) { 
  165. $edit_address_shipping = $this->get_translated_edit_address_slug( 'shipping', $page_lang ); 
  166. $edit_address_billing = $this->get_translated_edit_address_slug( 'billing', $page_lang ); 
  167.  
  168. if( $edit_address_shipping == urldecode( $value ) ) { 
  169. $value = $this->get_translated_edit_address_slug( 'shipping', $sitepress->get_current_language() ); 
  170. }elseif( $edit_address_billing == urldecode( $value ) ) { 
  171. $value = $this->get_translated_edit_address_slug( 'billing', $sitepress->get_current_language() ); 
  172.  
  173.  
  174.  
  175. if ( get_option( 'permalink_structure' ) ) { 
  176. if ( strstr( $permalink, '?' ) ) { 
  177. $query_string = '?' . parse_url( $permalink, PHP_URL_QUERY ); 
  178. $permalink = current( explode( '?', $permalink ) ); 
  179. } else { 
  180. $query_string = ''; 
  181. $url = trailingslashit( $permalink ) . $endpoint . '/' . $value . $query_string; 
  182. } else { 
  183. $url = add_query_arg( $endpoint, $value, $permalink ); 
  184. return $url; 
  185.  
  186. /** 
  187. * We need check special case - when you manually put in URL default not translated endpoint it not generated 404 error 
  188. */ 
  189. function check_if_endpoint_exists($q) { 
  190. global $wp_query; 
  191.  
  192. $my_account_id = wc_get_page_id('myaccount'); 
  193.  
  194. $current_id = $q->query_vars['page_id']; 
  195. if(!$current_id) { 
  196. $current_id = $q->queried_object_id; 
  197.  
  198. if( !$q->is_404 && $current_id == $my_account_id && $q->is_page ) { 
  199.  
  200. $uri_vars = array_filter( explode( '/', $_SERVER['REQUEST_URI']) ); 
  201. $endpoints = WC()->query->get_query_vars(); 
  202. $endpoint_in_url = urldecode( end( $uri_vars ) ); 
  203.  
  204. $endpoints['shipping'] = urldecode( $this->get_translated_edit_address_slug( 'shipping' ) ); 
  205. $endpoints['billing'] = urldecode( $this->get_translated_edit_address_slug( 'billing' ) ); 
  206.  
  207. if( urldecode( $q->query['pagename'] ) != $endpoint_in_url && !in_array( $endpoint_in_url, $endpoints ) && is_numeric( $endpoint_in_url ) && !in_array( urldecode( prev( $uri_vars ) ) , $q->query_vars ) ) { 
  208. $wp_query->set_404(); 
  209. status_header(404); 
  210. include( get_query_template( '404' ) ); 
  211. die(); 
  212.  
  213.  
  214.  
  215. function get_translated_edit_address_slug( $slug, $language = false ) { 
  216. global $woocommerce_wpml; 
  217.  
  218. $strings_language = $woocommerce_wpml->strings->get_string_language( $slug, 'woocommerce', 'edit-address-slug: '.$slug ); 
  219.  
  220. if( $strings_language == $language ) { 
  221. return $slug; 
  222.  
  223. $translated_slug = apply_filters( 'wpml_translate_single_string', $slug, 'woocommerce', 'edit-address-slug: '.$slug, $language ); 
  224.  
  225. if( $translated_slug == $slug ) { 
  226.  
  227. if( $language ) { 
  228. $translated_slug = $woocommerce_wpml->strings->get_translation_from_woocommerce_mo_file( 'edit-address-slug'. chr(4) .$slug, $language ); 
  229. }else{ 
  230. $translated_slug = _x( $slug, 'edit-address-slug', 'woocommerce' ); 
  231.  
  232.  
  233. return $translated_slug; 
  234.  
  235. function filter_get_endpoint_url( $url, $endpoint, $value, $permalink ) { 
  236.  
  237. // return translated edit account slugs 
  238. if( isset( WC()->query->query_vars[ 'edit-address' ] ) && WC()->query->query_vars[ 'edit-address' ] == $endpoint && in_array( $value, array('shipping', 'billing'))) { 
  239. remove_filter('woocommerce_get_endpoint_url', array( $this, 'filter_get_endpoint_url'), 10, 4); 
  240. $url = wc_get_endpoint_url( 'edit-address', $this->get_translated_edit_address_slug( $value ) ); 
  241. add_filter('woocommerce_get_endpoint_url', array( $this, 'filter_get_endpoint_url'), 10, 4); 
  242.  
  243. return $url; 
  244.  
  245. public function update_original_endpoints_strings() { 
  246.  
  247. foreach( WC()->query->query_vars as $endpoint_key => $endpoint ) { 
  248.  
  249. $this->register_endpoint_string( $endpoint_key, $endpoint ); 
  250.  
  251.  
  252.  
  253.