wpml-queried-object.php
3.15 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
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
<?php
/**
* Class WPML_Queried_Object
*
* @author OnTheGoSystems
*/
class WPML_Queried_Object {
/** @var SitePress $sitepress */
private $sitepress;
/** @var null|object */
private $queried_object;
/** @var stdClass $queried_object_details */
private $queried_object_details;
/**
* WPML_TF_Queried_Object constructor.
*
* @param SitePress $sitepress
*/
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
$this->queried_object = get_queried_object();
}
public function has_object() {
return (bool) $this->queried_object;
}
/**
* @return null|string
*/
public function get_source_language_code() {
return $this->get_queried_object_detail( 'source_language_code' );
}
/**
* @return string
*/
public function get_language_code() {
return $this->get_queried_object_detail( 'language_code' );
}
/**
* @param string $key
*
* @return null|mixed
*/
private function get_queried_object_detail( $key ) {
$detail = null;
if ( ! $this->queried_object_details ) {
if ( $this->is_post() ) {
$this->queried_object_details = $this->sitepress->get_element_language_details(
$this->get_id(),
$this->get_element_type()
);
}
}
if ( isset( $this->queried_object_details->{$key} ) ) {
$detail = $this->queried_object_details->{$key};
}
return $detail;
}
/**
* @return bool
*/
public function is_post() {
return isset( $this->queried_object->ID, $this->queried_object->post_type );
}
/**
* @return null|int
*/
public function get_id() {
$id = null;
if ( isset( $this->queried_object->ID ) ) {
$id = $this->queried_object->ID;
}
return $id;
}
/**
* @return null|string
*/
public function get_element_type() {
$type = null;
if ( $this->is_instance_of_post_type() ) {
$type = 'post_' . $this->get_post_type_name();
}
if ( $this->is_instance_of_post() ) {
$type = 'post_' . $this->get_post_type();
}
if ( $this->is_instance_of_taxonomy() ) {
$type = 'tax_' . $this->get_taxonomy();
}
return $type;
}
/**
* @return null|string
*/
public function get_source_url() {
$url = null;
$language_links = $this->sitepress->get_ls_languages();
if ( isset( $language_links[ $this->get_source_language_code() ]['url'] ) ) {
$url = $language_links[ $this->get_source_language_code() ]['url'];
}
return $url;
}
public function get_post_type() {
if ( $this->is_instance_of_post() ) {
return $this->queried_object->post_type;
}
return null;
}
public function get_taxonomy() {
if ( $this->is_instance_of_taxonomy() ) {
return $this->queried_object->taxonomy;
}
}
public function get_post_type_name() {
if ( $this->is_instance_of_post_type() ) {
return $this->queried_object->name;
}
}
public function is_instance_of_post() {
return $this->queried_object instanceof WP_Post;
}
public function is_instance_of_taxonomy() {
return $this->queried_object instanceof WP_Term;
}
public function is_instance_of_post_type() {
return $this->queried_object instanceof WP_Post_Type;
}
public function is_instance_of_user() {
return $this->queried_object instanceof WP_User;
}
}