WCML_Admin_Currency_Selector

The WooCommerce Multilingual WCML Admin Currency Selector class.

Defined (1)

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

/inc/currencies/class-wcml-admin-currency-selector.php  
  1. class WCML_Admin_Currency_Selector{ 
  2. private $woocommerce_wpml; 
  3.  
  4. public function __construct() { 
  5.  
  6. if( is_admin() ) { 
  7. add_filter('init', array($this, 'admin_currency_selector_init') ); 
  8.  
  9.  
  10.  
  11. public function admin_currency_selector_init() { 
  12. global $pagenow, $woocommerce_wpml; 
  13.  
  14. $this->woocommerce_wpml =& $woocommerce_wpml; 
  15.  
  16. //dashboard status screen 
  17. if ( current_user_can( 'view_woocommerce_reports' ) || current_user_can( 'manage_woocommerce' ) || current_user_can( 'publish_shop_orders' ) ) { 
  18. $this->set_dashboard_currency(); 
  19.  
  20. if ( version_compare( WOOCOMMERCE_VERSION, '2.4', '<' ) && $pagenow == 'index.php' ) { 
  21. add_action( 'admin_footer', array($this, 'show_dashboard_currency_selector') ); 
  22. } else { 
  23. add_action( 'woocommerce_after_dashboard_status_widget', array($this, 'show_dashboard_currency_selector') ); 
  24.  
  25. add_action( 'wp_ajax_wcml_dashboard_set_currency', array($this, 'set_dashboard_currency_ajax') ); 
  26.  
  27. add_filter( 'woocommerce_currency_symbol', array($this, 'filter_dashboard_currency_symbol') ); 
  28.  
  29.  
  30. /** 
  31. * Add currency drop-down on dashboard page ( WooCommerce status block ) 
  32. */ 
  33. public function show_dashboard_currency_selector() { 
  34.  
  35. $current_dashboard_currency = $this->get_cookie_dashboard_currency(); 
  36.  
  37. $wc_currencies = get_woocommerce_currencies(); 
  38. $order_currencies = $this->woocommerce_wpml->multi_currency->orders->get_orders_currencies(); 
  39. ?> 
  40. <select id="dropdown_dashboard_currency" style="display: none; margin : 10px; "> 
  41. <?php if(empty($order_currencies)): ?> 
  42. <option value=""><?php _e('Currency - no orders found', 'woocommerce-multilingual') ?></option> 
  43. <?php else: ?> 
  44. <?php foreach($order_currencies as $currency => $count ): ?> 
  45.  
  46. <option value="<?php echo $currency ?>" <?php echo $current_dashboard_currency == $currency ? 'selected="selected"':''; ?>> 
  47. <?php echo $wc_currencies[$currency]; ?> 
  48. </option> 
  49.  
  50. <?php endforeach; ?> 
  51. <?php endif; ?> 
  52. </select> 
  53. <?php 
  54.  
  55. $wcml_dashboard_set_currency_nonce = wp_create_nonce( 'dashboard_set_currency' ); 
  56.  
  57. wc_enqueue_js( " 
  58.  
  59. jQuery(document).ready(function() { 
  60.  
  61. var dashboard_dropdown = jQuery('#dropdown_dashboard_currency').clone(); 
  62. jQuery('#dropdown_dashboard_currency').remove(); 
  63. dashboard_dropdown.insertBefore('.sales-this-month a').show(); 
  64. jQuery('#woocommerce_dashboard_status .wc_status_list li').css('display', 'table'); 
  65.  
  66. }); 
  67.  
  68. jQuery(document).on('change', '#dropdown_dashboard_currency', function() { 
  69. jQuery.ajax({ 
  70. url: ajaxurl,  
  71. type: 'post',  
  72. data: { 
  73. action: 'wcml_dashboard_set_currency',  
  74. currency: jQuery('#dropdown_dashboard_currency').val(),  
  75. wcml_nonce: '".$wcml_dashboard_set_currency_nonce."' 
  76. },  
  77. success: function( response ) { 
  78. if(typeof response.error !== 'undefined') { 
  79. alert(response.error); 
  80. }else{ 
  81. window.location = window.location.href; 
  82. }) 
  83. }); 
  84. "); 
  85.  
  86. public function set_dashboard_currency_ajax() { 
  87.  
  88. $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); 
  89. if(!$nonce || !wp_verify_nonce($nonce, 'dashboard_set_currency')) { 
  90. echo json_encode(array('error' => __('Invalid nonce', 'woocommerce-multilingual'))); 
  91. die(); 
  92.  
  93. $this->set_dashboard_currency(filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_FULL_SPECIAL_CHARS )); 
  94.  
  95. die(); 
  96.  
  97. /** 
  98. * Set dashboard currency cookie 
  99. */ 
  100. public function set_dashboard_currency( $currency_code = false ) { 
  101.  
  102. if( !$currency_code && !headers_sent()) { 
  103. $order_currencies = $this->woocommerce_wpml->multi_currency->orders->get_orders_currencies(); 
  104. $currency_code = get_woocommerce_currency(); 
  105. if(!isset($order_currencies[$currency_code])) { 
  106. foreach( $order_currencies as $currency_code => $count ) { 
  107. $currency_code = $currency_code; 
  108. break; 
  109.  
  110. setcookie('_wcml_dashboard_currency', $currency_code , time() + 86400, COOKIEPATH, COOKIE_DOMAIN); 
  111.  
  112. /** 
  113. * Get dashboard currency cookie 
  114. * @return string 
  115. */ 
  116. public function get_cookie_dashboard_currency() { 
  117.  
  118. if( isset( $_COOKIE [ '_wcml_dashboard_currency' ] ) ) { 
  119. $currency = $_COOKIE[ '_wcml_dashboard_currency' ]; 
  120. }else{ 
  121. $currency = get_woocommerce_currency(); 
  122.  
  123. return $currency; 
  124.  
  125. /** 
  126. * Filter currency symbol on dashboard page 
  127. * @param string $currency Currency code 
  128. * @return string 
  129. */ 
  130. public function filter_dashboard_currency_symbol( $currency ) { 
  131. global $pagenow; 
  132.  
  133. remove_filter( 'woocommerce_currency_symbol', array( $this, 'filter_dashboard_currency_symbol' ) ); 
  134. if( isset( $_COOKIE [ '_wcml_dashboard_currency' ] ) && $pagenow == 'index.php' ) { 
  135. $currency = get_woocommerce_currency_symbol( $_COOKIE [ '_wcml_dashboard_currency' ] ); 
  136. add_filter( 'woocommerce_currency_symbol', array( $this, 'filter_dashboard_currency_symbol' ) ); 
  137.  
  138. return $currency; 
  139.  
  140.