class-wpml-url-cached-converter.php
1.3 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
<?php
class WPML_URL_Cached_Converter extends WPML_URL_Converter {
/** @var string[] $cache */
private $cache;
/**
* @param string $url
* @param string|bool $lang_code
*
* @return string
*/
public function convert_url( $url, $lang_code = false ) {
global $sitepress;
if ( ! $lang_code ) {
$lang_code = $sitepress->get_current_language();
}
$negotiation_type = $sitepress->get_setting( 'language_negotiation_type' );
$skip_convert_url_string = $this->get_strategy()->skip_convert_url_string( $url, $lang_code );
$cache_key_args = array( $url, $lang_code, $negotiation_type, $skip_convert_url_string );
$cache_key = md5( (string) wp_json_encode( $cache_key_args ) );
$cache_group = 'convert_url';
$cache_found = false;
$cache = new WPML_WP_Cache( $cache_group );
$new_url = $cache->get( $cache_key, $cache_found );
if ( ! $cache_found ) {
$new_url = parent::convert_url( $url, $lang_code );
$cache->set( $cache_key, $new_url );
}
return $new_url;
}
/**
* @param string $url
*
* @return string
*/
public function get_language_from_url( $url ) {
if ( isset( $this->cache[ $url ] ) ) {
return $this->cache[ $url ];
}
$lang = parent::get_language_from_url( $url );
$this->cache[ $url ] = $lang;
return $lang;
}
}