This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
155 lines (134 loc) · 3.98 KB
/
Plugin.php
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
<?php
namespace SehrGut\WpEuVatHelpers;
use Mpociot\VatCalculator\VatCalculator;
use NumberFormatter;
class Plugin
{
/**
* An instance of the VatCalculator that does all the actual processing.
*
* @var VatCalculator
*/
protected $vat_calculator;
/**
* Cached country code of the user based on their IP.
*
* @var string
*/
protected $ip_country;
/**
* Construct a new instance of the plugin.
*
* @param VatCalculator $vat_calculator
*/
public function __construct(VatCalculator $vat_calculator)
{
$this->vat_calculator = $vat_calculator;
$this->ip_country = $this->vat_calculator->getIPBasedCountry();
}
/**
* Register all shortcodes.
*
* @return $this
*/
public function registerShortcodes()
{
add_shortcode('localize_currency', [$this, 'localizeCurrencyShortcode']);
add_shortcode('if_taxable', [$this, 'ifTaxableShortcode']);
add_shortcode('unless_taxable', [$this, 'unlessTaxableShortcode']);
add_shortcode('vat_rate', [$this, 'vatRateShortcode']);
add_shortcode('ip_country', [$this, 'ipCountryShortcode']);
return $this;
}
/**
* Shortcode: Localize price based on user's IP address.
*
* @param array $attributes Attributes to the shortcode tag
*
* @return string
*/
public function localizeCurrencyShortcode($attributes = [])
{
$attributes = shortcode_atts([
'value' => null,
'country' => $this->ip_country,
'currency' => 'EUR',
], $attributes);
if (is_null($attributes['value'])) {
return '';
}
$gross = $this->vat_calculator->calculate($attributes['value'], $attributes['country']);
$formatter = $this->makeFormatter($attributes['country']);
return $formatter->formatCurrency($gross, $attributes['currency']);
}
/**
* Shortcode: Show body if EU VAT is applicable in given country.
*
* @param array $attributes Attributes to the shortcode tag
*
* @return string
*/
public function ifTaxableShortcode($attributes, $body)
{
$attributes = shortcode_atts([
'country' => $this->ip_country,
], $attributes);
if ($this->vat_calculator->shouldCollectVat($attributes['country'])) {
return do_shortcode($body);
}
return '';
}
/**
* Shortcode: Show body only if *no* EU VAT is applicable in given country.
*
* @param array $attributes Attributes to the shortcode tag
*
* @return string
*/
public function unlessTaxableShortcode($attributes, $body)
{
$attributes = shortcode_atts([
'country' => $this->ip_country,
], $attributes);
if ($this->vat_calculator->shouldCollectVat($attributes['country'])) {
return '';
}
return do_shortcode($body);
}
/**
* Return the current VAT rate based on the user's country.
*
* @param array $attributes Attributes to the shortcode tag
*
* @return string
*/
public function vatRateShortcode($attributes = [])
{
$attributes = shortcode_atts([
'country' => $this->ip_country,
], $attributes);
return (string) $this->vat_calculator->getTaxRateForLocation($attributes['country']) * 100;
}
/**
* Return the current user's country based on their IP address.
*
* @param array $attributes Attributes to the shortcode tag
*
* @return string
*/
public function ipCountryShortcode($attributes = [])
{
return $this->ip_country;
}
/**
* Return a NumberFormatter instance for given country.
*
* @param string $country two-letter country code
*
* @return NumberFormatter
*/
protected function makeFormatter($country)
{
return new NumberFormatter($country, NumberFormatter::CURRENCY);
}
}