class-wpml-resolve-object-url-helper.php
3.27 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
<?php
class WPML_Resolve_Object_Url_Helper implements IWPML_Resolve_Object_Url {
const CACHE_GROUP = 'resolve_object_url';
/**
* @var bool
*/
protected $lock = false;
/**
* @var SitePress
*/
private $sitepress;
/**
* @var WP_Query
*/
private $wp_query;
/**
* @var WPML_Term_Translation
*/
private $wpml_term_translations;
/**
* @var WPML_Post_Translation
*/
private $wpml_post_translations;
/**
* @param SitePress $sitepress
* @param WP_Query $wp_query
* @param WPML_Term_Translation $wpml_term_translations
* @param WPML_Post_Translation $wpml_post_translations
*/
public function __construct(
SitePress &$sitepress = null,
WP_Query &$wp_query = null,
WPML_Term_Translation $wpml_term_translations = null,
WPML_Post_Translation $wpml_post_translations = null
) {
$this->sitepress = &$sitepress;
$this->wp_query = &$wp_query;
$this->wpml_term_translations = $wpml_term_translations;
$this->wpml_post_translations = $wpml_post_translations;
}
/**
* Try to parse the URL to find a related post or term
*
* @param string $url
* @param string $lang_code
*
* @return string|bool
*/
public function resolve_object_url( $url, $lang_code ) {
if ( $this->lock ) {
return false;
}
$this->lock = true;
$new_url = false;
$translations = $this->cached_retrieve_translations( $url );
if ( $translations && isset( $translations[ $lang_code ]->element_type ) ) {
$current_lang = $this->sitepress->get_current_language();
$this->sitepress->switch_lang( $lang_code );
$element = explode( '_', $translations[ $lang_code ]->element_type );
$type = array_shift( $element );
$subtype = implode( '_', $element );
switch ( $type ) {
case 'post':
$new_url = get_permalink( $translations[ $lang_code ]->element_id );
break;
case 'tax':
/** @var WP_Term|false $term */
$term = get_term_by( 'term_taxonomy_id', $translations[ $lang_code ]->element_id, $subtype );
$new_url = $term ? get_term_link( $term ) : false;
break;
}
$this->sitepress->switch_lang( $current_lang );
}
$this->lock = false;
return $new_url;
}
/**
* @param string $url
*
* @return array<string,\stdClass>
*/
private function cached_retrieve_translations( $url ) {
$cache_key = md5( $url );
$cache_found = false;
$cache = new WPML_WP_Cache( self::CACHE_GROUP );
$translations = $cache->get( $cache_key, $cache_found );
if ( ! $cache_found && is_object( $this->wp_query ) ) {
$translations = $this->retrieve_translations();
$cache->set( $cache_key, $translations );
}
return $translations;
}
/**
* @return array<string,\stdClass>
*/
private function retrieve_translations() {
$this->sitepress->set_wp_query(); // Make sure $sitepress->wp_query is set
$_wp_query_back = clone $this->wp_query;
$wp_query = $this->sitepress->get_wp_query();
$wp_query = is_object( $wp_query ) ? clone $wp_query : clone $_wp_query_back;
$languages_helper = new WPML_Languages( $this->wpml_term_translations, $this->sitepress, $this->wpml_post_translations );
list( $translations) = $languages_helper->get_ls_translations( $wp_query, $_wp_query_back, $this->sitepress->get_wp_query() );
return $translations;
}
}