-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpml-automatic-language-with-geoip.php
163 lines (137 loc) · 4.7 KB
/
wpml-automatic-language-with-geoip.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
156
157
158
159
160
161
162
163
<?php
/**
* Plugin Name: WPML Automatic language with GeoIP
* Plugin URI: https://github.com/ocReaper/wpml-automatic-language-with-geoip
* Description: Automatically changes the load of language in WPML based on the GeoIP service
* Version: 0.1.0
* Author: ocReaper
* Author URI: https://github.com/ocReaper
* Donate link: https://github.com/ocReaper
* License: MIT
*/
require 'vendor/autoload.php';
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use WPML\Language\Detection\CookieLanguage;
use WPML\Language\Detection\Frontend;
/**
* Class Custom_WPML_Frontend_Request
*
* @since 04/06/2016
*/
class Custom_WPML_Frontend_Request extends Frontend {
public function get_requested_lang() {
if ( is_null( $this->get_request_uri_lang() ) ) {
return $this->get_cookie_lang();
}
return $this->get_request_uri_lang();
}
}
/**
* Class Wpml_automatic_language_with_geoip
*
* @since 04/06/2016
*/
class Wpml_automatic_language_with_geoip {
protected $redirected_cookie_name = 'language-redirected';
protected $preferred_default_language = '';
/**
* Wpml_automatic_language_with_geoip constructor.
*
* @since 04/06/2016
*/
public function __construct() {
if ( is_admin() ) {
return;
}
$this->preferred_default_language = apply_filters( 'wpml_automatic_language_with_geoip_preferred_default_language', $this->preferred_default_language );
add_action( 'plugins_loaded', array( &$this, 'replace_wpml_frontend_request_handler' ), - 1 );
add_action( 'plugins_loaded', array( &$this, 'wpml_language_redirect_via_geoip' ), 11 );
}
/**
* Fix WPML's cookie handling, because of on "/" it'll return null and clears the lang cookie, which is bad for us.
*
* @since 04/06/2016
*/
public function replace_wpml_frontend_request_handler() {
global $wpml_request_handler, $wpml_url_converter, $wpml_language_resolution, $sitepress;
$cookieLanguage = new CookieLanguage( new WPML_Cookie(), $sitepress->get_default_language() );
$wpml_request_handler = new Custom_WPML_Frontend_Request(
$wpml_url_converter,
$wpml_language_resolution->get_active_language_codes(),
$sitepress->get_default_language(),
$cookieLanguage,
new WPML_WP_API()
);
}
/**
* Do the language change
*
* @since 04/06/2016
*/
public function wpml_language_redirect_via_geoip() {
if ( ! isset( $_COOKIE[ $this->redirected_cookie_name ] ) || empty( $_COOKIE[ $this->redirected_cookie_name ] ) ) {
return;
}
global $sitepress;
$language = $this->get_geoip_country_code();
$filtered_language = $this->map_country_code_to_wpml_language( $language );
$is_not_filtered = strtolower( $language ) === $filtered_language;
$is_not_exists = ! array_key_exists( $filtered_language, wpml_get_active_languages_filter( null ) );
if ( $is_not_filtered && $is_not_exists ) {
if ( ! empty( $this->preferred_default_language ) ) {
$default_language = $this->preferred_default_language;
} else {
$default_language = $sitepress->get_default_language();
}
$sitepress->switch_lang( $default_language, true );
} else {
$sitepress->switch_lang( $filtered_language, true );
}
setcookie( $this->redirected_cookie_name, 1, strtotime( '+7 days' ), '/' );
}
/**
* Return the visitor's country code based on various GeoIP services
*
* @return string
* @throws \GeoIp2\Exception\AddressNotFoundException
* @throws \MaxMind\Db\Reader\InvalidDatabaseException
* @since 04/06/2016
*/
protected function get_geoip_country_code() {
if ( $this->is_wp_engine_server() ) {
return getenv( 'HTTP_GEOIP_COUNTRY_CODE' );
}
$remote_address = isset($_SERVER['REMOTE_ADDR']) ? filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) : '127.0.0.1';
$reader = new Reader( plugin_dir_path( __FILE__ ) . 'assets/GeoLite2-Country.mmdb' );
try {
$record = $reader->country( $remote_address );
} catch ( AddressNotFoundException $e ) {
return ICL_LANGUAGE_CODE;
}
return $record->country->isoCode;
}
/**
* Return the language code of the visitor by mapping the GeoIP country code to the WPML languages
*
* @param string $language
*
* @return string
* @since 04/06/2016
*/
protected function map_country_code_to_wpml_language( $language ) {
$iso_3166_2_to_gettext_map = require_once 'assets/iso-3166-2-to-gettext.php';
$iso_3166_2_to_gettext_map = apply_filters( 'wpml_automatic_language_with_geoip_country_code_map', $iso_3166_2_to_gettext_map );
return $iso_3166_2_to_gettext_map[ $language ];
}
/**
* Determine if the hosting provider is WP Engine
*
* @return bool
* @since 04/06/2016
*/
protected function is_wp_engine_server() {
return class_exists( 'WPE_API', false );
}
}
new Wpml_automatic_language_with_geoip();