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 |