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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / currencies / class-wcml-admin-currency-selector.php
woocommerce-multilingual / classes / currencies Last commit date
class-wcml-admin-currency-selector.php 1 month ago class-wcml-currencies.php 1 month ago
class-wcml-admin-currency-selector.php
137 lines
1 <?php
2
3 use WCML\Utilities\WpAdminPages;
4 use WPML\API\Sanitize;
5
6 class WCML_Admin_Currency_Selector {
7
8 private $woocommerce_wpml;
9 private $currency_cookie;
10
11 const NONCE_KEY = 'wcml-admin-currency-selector';
12
13 public function __construct( woocommerce_wpml $woocommerce_wpml, WCML_Admin_Cookie $currency_cookie ) {
14 $this->woocommerce_wpml = $woocommerce_wpml;
15 $this->currency_cookie = $currency_cookie;
16 }
17
18 public function add_hooks() {
19 if ( is_admin() ) {
20
21 if ( $this->user_can_manage_woocommerce() ) {
22 add_action( 'init', [ $this, 'set_dashboard_currency' ] );
23 add_action( 'wp_ajax_wcml_dashboard_set_currency', [ $this, 'set_dashboard_currency_ajax' ] );
24 add_filter( 'woocommerce_currency_symbol', [ $this, 'filter_dashboard_currency_symbol' ] );
25 }
26
27 add_action(
28 'woocommerce_after_dashboard_status_widget',
29 [
30 $this,
31 'show_dashboard_currency_selector',
32 ]
33 );
34 add_action( 'admin_enqueue_scripts', [ $this, 'load_js' ] );
35 }
36 }
37
38 private function user_can_manage_woocommerce() {
39 return current_user_can( 'view_woocommerce_reports' ) ||
40 current_user_can( 'manage_woocommerce' ) ||
41 current_user_can( 'publish_shop_orders' );
42
43 }
44
45 public function load_js() {
46 wp_enqueue_script(
47 'wcml-admin-currency-selector',
48 $this->woocommerce_wpml->plugin_url() .
49 '/res/js/admin-currency-selector' . $this->woocommerce_wpml->js_min_suffix() . '.js',
50 [ 'jquery' ],
51 $this->woocommerce_wpml->version(),
52 true
53 );
54 wp_localize_script(
55 'wcml-admin-currency-selector',
56 'wcml_admin_currency_selector',
57 [
58 'nonce' => wp_create_nonce( self::NONCE_KEY ),
59 ]
60 );
61 }
62
63 public function show_dashboard_currency_selector() {
64
65 $current_dashboard_currency = $this->get_cookie_dashboard_currency();
66
67 $wc_currencies = get_woocommerce_currencies();
68 $currency_codes = $this->woocommerce_wpml->multi_currency->get_currency_codes();
69 ?>
70 <select id="dropdown_dashboard_currency" style="display: none; margin : 10px; ">
71 <?php if ( empty( $currency_codes ) ) : ?>
72 <option value=""><?php _e( 'Currency - no orders found', 'woocommerce-multilingual' ); ?></option>
73 <?php else : ?>
74 <?php foreach ( $currency_codes as $currency ) : ?>
75
76 <option value="<?php echo esc_html( $currency ); ?>" <?php echo esc_html( $current_dashboard_currency === $currency ? 'selected="selected"' : '' ); ?>>
77 <?php echo esc_html( $wc_currencies[ $currency ] ); ?>
78 </option>
79
80 <?php endforeach; ?>
81 <?php endif; ?>
82 </select>
83 <?php
84 }
85
86 public function set_dashboard_currency_ajax() {
87 $nonce = sanitize_text_field( $_POST['wcml_nonce'] );
88
89 if ( ! $nonce || ! wp_verify_nonce( $nonce, self::NONCE_KEY ) ) {
90 wp_send_json_error( __( 'Invalid nonce', 'woocommerce-multilingual' ), 403 );
91 } else {
92 $this->set_dashboard_currency( sanitize_text_field( $_POST['currency'] ) );
93 wp_send_json_success();
94 }
95 }
96
97 public function set_dashboard_currency( $currency_code = '' ) {
98 global $pagenow;
99
100 if ( ! $currency_code && 'index.php' === $pagenow && ! headers_sent() ) {
101 $currency_code = $this->get_cookie_dashboard_currency();
102 }
103
104 if ( $currency_code ) {
105 $this->currency_cookie->set_value( $currency_code, time() + DAY_IN_SECONDS );
106 }
107 }
108
109 public function get_cookie_dashboard_currency() {
110
111 $currency = $this->currency_cookie->get_value();
112 if ( null === $currency ) {
113 $currency = wcml_get_woocommerce_currency_option();
114 }
115
116 return $currency;
117 }
118
119 public function filter_dashboard_currency_symbol( $currencySymbol ) {
120 if (
121 ( WpAdminPages::isDashboard() && empty( $_REQUEST['action'] ) )
122 || self::isDashboardWidgetRequest()
123 ) {
124 remove_filter( 'woocommerce_currency_symbol', [ $this, 'filter_dashboard_currency_symbol' ] );
125 $currencySymbol = get_woocommerce_currency_symbol( $this->get_cookie_dashboard_currency() );
126 add_filter( 'woocommerce_currency_symbol', [ $this, 'filter_dashboard_currency_symbol' ] );
127 }
128
129 return $currencySymbol;
130 }
131
132 public static function isDashboardWidgetRequest() {
133 return wp_doing_ajax()
134 && 'woocommerce_load_status_widget' === Sanitize::stringProp( 'action', $_GET );
135 }
136 }
137