Skip to content

Commit 4aaab32

Browse files
committed
updating geolocation fixes
1 parent 0998576 commit 4aaab32

File tree

6 files changed

+101
-210
lines changed

6 files changed

+101
-210
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/).
66

7+
## [10.1.0]
8+
9+
### Fixed
10+
11+
- Geolocation and manifest cache for countries.
12+
713
## [10.0.0]
814

915
### Removed
@@ -833,6 +839,7 @@ Init setup
833839
- Gutenberg Blocks Registration.
834840
- Assets Manifest data.
835841

842+
[10.1.0]: https://github.com/infinum/eightshift-libs/compare/10.0.0...10.1.0
836843
[10.0.0]: https://github.com/infinum/eightshift-libs/compare/9.3.5...10.0.0
837844
[9.3.5]: https://github.com/infinum/eightshift-libs/compare/9.3.4...9.3.5
838845
[9.3.4]: https://github.com/infinum/eightshift-libs/compare/9.3.3...9.3.4

src/Cache/AbstractManifestCache.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ protected function getCacheBuilder(): array
214214
if ($this->useGeolocation()) {
215215
$output[self::TYPE_GEOLOCATION] = [
216216
self::COUNTRIES_KEY => [
217-
'path' => 'eightshift',
218-
'fileName' => "countries.json",
217+
'path' => 'libsPrefixedGeolocation',
219218
],
220219
];
221220
}

src/Geolocation/AbstractGeolocation.php

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ abstract public function getGeolocationPharLocation(): string;
4242
abstract public function getGeolocationDbLocation(): string;
4343

4444
/**
45-
* Tooggle geolocation usage based on this flag.
45+
* Toggle geolocation usage based on this flag.
4646
*
4747
* @return boolean
4848
*/
@@ -136,27 +136,102 @@ public function getCountries(): array
136136
'label' => \__('Europe', 'eightshift-libs'),
137137
'value' => 'europe',
138138
'group' => [
139-
'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR',
140-
'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK',
141-
'SI', 'ES', 'SE', 'AL', 'AD', 'AM', 'BY', 'BA', 'FO', 'GE', 'GI', 'IS',
142-
'IM', 'XK', 'LI', 'MK', 'MD', 'MC', 'NO', 'RU', 'SM', 'RS', 'CH', 'TR',
143-
'UA', 'GB', 'VA',
139+
'AT',
140+
'BE',
141+
'BG',
142+
'HR',
143+
'CY',
144+
'CZ',
145+
'DK',
146+
'EE',
147+
'FI',
148+
'FR',
149+
'DE',
150+
'GR',
151+
'HU',
152+
'IE',
153+
'IT',
154+
'LV',
155+
'LT',
156+
'LU',
157+
'MT',
158+
'NL',
159+
'PL',
160+
'PT',
161+
'RO',
162+
'SK',
163+
'SI',
164+
'ES',
165+
'SE',
166+
'AL',
167+
'AD',
168+
'AM',
169+
'BY',
170+
'BA',
171+
'FO',
172+
'GE',
173+
'GI',
174+
'IS',
175+
'IM',
176+
'XK',
177+
'LI',
178+
'MK',
179+
'MD',
180+
'MC',
181+
'NO',
182+
'RU',
183+
'SM',
184+
'RS',
185+
'CH',
186+
'TR',
187+
'UA',
188+
'GB',
189+
'VA',
144190
],
145191
],
146192
[
147193
'label' => \__('European Union', 'eightshift-libs'),
148194
'value' => 'european-union',
149195
'group' => [
150-
'BE', 'EL', 'LT', 'PT', 'BG', 'ES', 'LU', 'RO', 'CZ',
151-
'FR', 'HU', 'SI', 'DK', 'HR', 'MT', 'SK', 'DE', 'IT',
152-
'NL', 'FI', 'EE', 'CY', 'AT', 'SE', 'IE', 'LV', 'PL',
196+
'BE',
197+
'EL',
198+
'LT',
199+
'PT',
200+
'BG',
201+
'ES',
202+
'LU',
203+
'RO',
204+
'CZ',
205+
'FR',
206+
'HU',
207+
'SI',
208+
'DK',
209+
'HR',
210+
'MT',
211+
'SK',
212+
'DE',
213+
'IT',
214+
'NL',
215+
'FI',
216+
'EE',
217+
'CY',
218+
'AT',
219+
'SE',
220+
'IE',
221+
'LV',
222+
'PL',
153223
],
154224
],
155225
[
156226
'label' => \__('Ex Yugoslavia', 'eightshift-libs'),
157227
'value' => 'ex-yugoslavia',
158228
'group' => [
159-
'HR', 'RS', 'BA', 'ME', 'SI', 'MK'
229+
'HR',
230+
'RS',
231+
'BA',
232+
'ME',
233+
'SI',
234+
'MK'
160235
],
161236
],
162237
];

src/Geolocation/Readme.md

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ Every time a library has a new release we should make an update.
1010
All releases are listed here: https://github.com/maxmind/GeoIP2-php/releases
1111

1212
Files used:
13-
* GeoLite2-Country.mmdb - Database you can get here: https://www.maxmind.com/en/accounts/722673/geoip/downloads - GeoLite2 Country.
14-
* geoip2.phar - executable file for reading the DB.
13+
14+
- GeoLite2-Country.mmdb - Database you can get here: https://www.maxmind.com/en/accounts/722673/geoip/downloads - GeoLite2 Country.
15+
- geoip2.phar - executable file for reading the DB.
1516

1617
## Country list
1718

@@ -20,82 +21,5 @@ We use DataHub list country list for providing the rest data that is used in the
2021
All releases are listed here: https://datahub.io/core/country-list
2122

2223
Files used:
23-
* manifest.json
24-
25-
# WP-ROCKET Plugin usage
26-
27-
When geolocation is used in combination with the WP-Rocket plugin it will not work by default because the cookie is set after the page is loaded and at that point, it is too late to provide alternative content.
28-
29-
WP-Rocket plugin provides options/hooks to fix this. By using the `rocket_advanced_cache_file` hook we are able to inject our custom function into the page generation process inside the `wp-content/advanced-cache.php` file. By modifying this file we can detect the users' geolocation from the IP and set the cookie manually before the page is loaded. This way WP-Rocket can detect that cookie and provide the necessary cache. Below is the process of setting it:
30-
31-
## Set dynamic cookie:
32-
33-
You must provide the list of cookies that will be used as dynamic cookies to generate cached versions.
34-
35-
**Filter:**
36-
```php
37-
\add_filter('rocket_cache_dynamic_cookies', [$this, 'dynamicCookiesList']);
38-
```
39-
40-
**Callback:**
41-
```php
42-
/**
43-
* List all dynamic cookies that will create new cached version.
44-
*
45-
* @param array<string, mixed> $items Items from the admin.
46-
*
47-
* @return array<int|string, mixed>
48-
*/
49-
public function dynamicCookiesList(array $items): array
50-
{
51-
$items[] = 'esForms-country';
52-
53-
return $items;
54-
}
55-
```
56-
57-
## Set custom function to advanced-cached.php file.
58-
59-
By providing a custom function in the `advanced-cached.php` file, you will be able to detect geolocation and set cookies before the cached version is provided.
60-
61-
For the `$esFormsPath` variable you must provide the absolute path to the `geolocationDetect.php` file located in the `vendor` folder.
62-
The provided example works if you have Eightshift-forms in your project.
63-
64-
**Filter:**
65-
```php
66-
\add_filter('rocket_advanced_cache_file', [$this, 'addNginxAdvanceCacheRules']);
67-
```
68-
69-
**Callback:**
70-
```php
71-
/**
72-
* Add geolocation function in the advance-cache.php config file on plugin activation
73-
*
74-
* Used only with Nginx web servers.
75-
*
76-
* @param string $content Original file output.
77-
*/
78-
public function addNginxAdvanceCacheRules( string $content ) : string {
79-
$position = \strpos($content, '$rocket_config_class');
80-
81-
// This part is string on purpose.
82-
$cors_function = '
83-
$esFormsPath = ABSPATH . "wp-content/plugins/eightshift-forms/src/Geolocation/geolocationDetect.php";
84-
if (file_exists($esFormsPath)) {
85-
require_once $esFormsPath;
86-
};';
87-
88-
return \substr_replace($content, $cors_function, $position, 0);
89-
}
90-
```
91-
92-
## Disable `setLocationCookie` filter in your project.
93-
94-
If you have installed Geolocation.php class in your project and you have this filter set, make sure to disable it, or provide the check to be disabled when the WP-Rocket cache plugin is active.
9524

96-
**Example:**
97-
```php
98-
if (!\is_plugin_active('wp-rocket/wp-rocket.php')) {
99-
\add_filter('init', [$this, 'setLocationCookie']);
100-
}
101-
```
25+
- manifest.json

src/Geolocation/geolocationDetect.php

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/Helpers/Helpers.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static function render(
212212
*
213213
* @return string
214214
*/
215-
public static function getProjectPaths(string $type, array|string $suffix = ''): string
215+
public static function getProjectPaths(string $type = '', array|string $suffix = ''): string
216216
{
217217
$root = \dirname(__FILE__, 6);
218218

@@ -232,7 +232,9 @@ public static function getProjectPaths(string $type, array|string $suffix = ''):
232232
case 'public':
233233
return self::joinPaths([$root, 'public', ...$suffix]);
234234
case 'libsPrefixed':
235-
return self::joinPaths([$root, 'vendor-prefixed', 'infinum', 'eightshift-libs']);
235+
return self::joinPaths([$root, 'vendor-prefixed', 'infinum', 'eightshift-libs', ...$suffix]);
236+
case 'libsPrefixedGeolocation':
237+
return self::joinPaths([$root, 'vendor-prefixed', 'infinum', 'eightshift-libs', 'src', 'Geolocation', ...$suffix]);
236238
case 'blocksRoot':
237239
return self::joinPaths([$root, 'src', 'Blocks', ...$suffix]);
238240
case 'blocks':

0 commit comments

Comments
 (0)