CurrencySwitcherWidget.php
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* WooCommerce Payments Currency Switcher Widget
*
* @package WooCommerce\Payments
*/
namespace WCPay\MultiCurrency;
use WC_Widget;
defined( 'ABSPATH' ) || exit;
/**
* Currency Switcher Widget Class
*/
class CurrencySwitcherWidget extends WC_Widget {
const DEFAULT_SETTINGS = [
'title' => '',
'symbol' => true,
'flag' => false,
];
/**
* Compatibility instance.
*
* @var Compatibility
*/
protected $compatibility;
/**
* Multi-Currency instance.
*
* @var MultiCurrency
*/
protected $multi_currency;
/**
* Register widget with WordPress.
*
* @param MultiCurrency $multi_currency The MultiCurrency instance.
* @param Compatibility $compatibility The Compatibility instance.
*/
public function __construct( MultiCurrency $multi_currency, Compatibility $compatibility ) {
$this->multi_currency = $multi_currency;
$this->compatibility = $compatibility;
$this->widget_id = 'currency_switcher_widget';
$this->widget_name = __( 'Currency Switcher Widget', 'woocommerce-payments' );
$this->widget_description = __( 'Let your customers switch between your enabled currencies', 'woocommerce-payments' );
$this->settings = [
'title' => [
'type' => 'text',
'std' => '',
'label' => __( 'Title', 'woocommerce-payments' ),
],
'symbol' => [
'type' => 'checkbox',
'std' => true,
'label' => __( 'Display currency symbols', 'woocommerce-payments' ),
],
'flag' => [
'type' => 'checkbox',
'std' => false,
'label' => __( 'Display flags in supported devices', 'woocommerce-payments' ),
],
];
parent::__construct();
}
/**
* Front-end display of widget.
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
if ( $this->compatibility->should_hide_widgets() ) {
return;
}
$enabled_currencies = $this->multi_currency->get_enabled_currencies();
if ( 1 === count( $enabled_currencies ) ) {
return;
}
$instance = wp_parse_args(
$instance,
self::DEFAULT_SETTINGS
);
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput
}
?>
<form>
<?php $this->output_get_params(); ?>
<select
name="currency"
aria-label="<?php echo esc_attr( $title ); ?>"
onchange="this.form.submit()"
>
<?php
foreach ( $enabled_currencies as $currency ) {
$this->display_currency_option( $currency, $instance['symbol'], $instance['flag'] );
}
?>
</select>
</form>
<?php
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
}
/**
* Create an <option> element with provided currency. With symbol and flag if requested.
*
* @param Currency $currency Currency to use for <option> element.
* @param boolean $with_symbol Whether to show the currency symbol.
* @param boolean $with_flag Whether to show the currency flag.
*
* @return void Displays HTML of currency <option>
*/
private function display_currency_option( Currency $currency, bool $with_symbol, bool $with_flag ) {
$code = $currency->get_code();
$same_symbol = html_entity_decode( $currency->get_symbol() ) === $code;
$text = $code;
$selected = $this->multi_currency->get_selected_currency()->code === $code ? ' selected' : '';
if ( $with_symbol && ! $same_symbol ) {
$text = $currency->get_symbol() . ' ' . $text;
}
if ( $with_flag ) {
$text = $currency->get_flag() . ' ' . $text;
}
echo "<option value=\"$code\"$selected>$text</option>"; // phpcs:ignore WordPress.Security.EscapeOutput
}
/**
* Output hidden inputs for every $_GET param.
* This prevent the switcher form to remove them on submit.
*
* @return void
*/
private function output_get_params() {
// phpcs:disable WordPress.Security.NonceVerification
if ( empty( $_GET ) ) {
return;
}
$params = explode( '&', urldecode( http_build_query( $_GET ) ) );
foreach ( $params as $param ) {
$name_value = explode( '=', $param );
$name = $name_value[0];
$value = $name_value[1];
if ( 'currency' === $name ) {
continue;
}
echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" />';
}
// phpcs:enable WordPress.Security.NonceVerification
}
}