Localization.php
6.16 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace AIOSEO\Plugin\Common\Sitemap;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles sitemap localization logic.
*
* @since 4.2.1
*/
class Localization {
/**
* This is cached so we don't do the lookup each query.
*
* @since 4.0.0
*
* @var boolean
*/
private static $wpml = null;
/**
* Class constructor.
*
* @since 4.2.1
*/
public function __construct() {
if ( aioseo()->helpers->isWpmlActive() ) {
self::$wpml = [
'defaultLanguage' => apply_filters( 'wpml_default_language', null ),
'activeLanguages' => apply_filters( 'wpml_active_languages', null )
];
add_filter( 'aioseo_sitemap_term', [ $this, 'localizeEntry' ], 10, 4 );
add_filter( 'aioseo_sitemap_post', [ $this, 'localizeEntry' ], 10, 4 );
}
}
/**
* Localize the entries if WPML (or others in the future) are active.
*
* @since 4.0.0
*
* @param array $entry The entry.
* @param int) $entryId The post/term ID.
* @param string $objectName The post type or taxonomy name.
* @param string $objectType Whether the entry is a post or term.
* @param bool $rss Whether or not we are localizing for the RSS sitemap.
* @return array The entry.
*/
public function localizeEntry( $entry, $entryId, $objectName, $objectType ) {
$translationGroupId = apply_filters( 'wpml_element_trid', null, $entryId );
$translations = apply_filters( 'wpml_get_element_translations', null, $translationGroupId, $objectName );
if ( empty( $translations ) ) {
return $entry;
}
$entry['languages'] = [];
$hiddenLanguages = apply_filters( 'wpml_setting', [], 'hidden_languages' );
foreach ( $translations as $translation ) {
if (
empty( $translation->element_id ) ||
! isset( self::$wpml['activeLanguages'][ $translation->language_code ] ) ||
in_array( $translation->language_code, $hiddenLanguages, true )
) {
continue;
}
if ( (int) $entryId === (int) $translation->element_id ) {
$entry['language'] = $translation->language_code;
continue;
}
$translatedObjectId = apply_filters( 'wpml_object_id', $entryId, $objectName, false, $translation->language_code );
if (
( 'post' === $objectType && $this->isExcludedPost( $translatedObjectId ) ) ||
( 'term' === $objectType && $this->isExcludedTerm( $translatedObjectId ) )
) {
continue;
}
$permalink = get_permalink( $translatedObjectId );
// Special treatment for the home page translations.
if ( 'page' === get_option( 'show_on_front' ) && aioseo()->helpers->wpmlIsHomePage( $entryId ) ) {
$permalink = aioseo()->helpers->wpmlHomeUrl( $translation->language_code );
}
$currentLanguage = ! empty( self::$wpml['activeLanguages'][ $translation->language_code ] ) ? self::$wpml['activeLanguages'][ $translation->language_code ] : null;
$languageCode = ! empty( $currentLanguage['tag'] ) ? $currentLanguage['tag'] : $translation->language_code;
if ( $languageCode && $permalink ) {
$entry['languages'][] = [
'language' => $languageCode,
'location' => $permalink
];
}
}
// Also include the main page as a translated variant, per Google's specifications, but only if we found at least one other language.
if ( ! empty( $entry['language'] ) && ! empty( $entry['languages'] ) ) {
$entry['languages'][] = [
'language' => $entry['language'],
'location' => $entry['loc']
];
} else {
unset( $entry['languages'] );
}
$entry = $this->validateSubentries( $entry );
return $entry;
}
/**
* Validates the subentries with translated variants to ensure all required values are set.
*
* @since 4.2.3
*
* @param array $entry The entry.
* @return array The validated entry.
*/
private function validateSubentries( $entry ) {
if ( ! isset( $entry['languages'] ) ) {
return $entry;
}
foreach ( $entry['languages'] as $index => $subentry ) {
if ( empty( $subentry['language'] ) || empty( $subentry['location'] ) ) {
unset( $entry['languages'][ $index ] );
}
}
return $entry;
}
/**
* Checks whether the given post should be excluded.
*
* @since 4.2.4
*
* @param int $postId The post ID.
* @return bool Whether the post should be excluded.
*/
private function isExcludedPost( $postId ) {
static $excludedPostIds = null;
if ( null === $excludedPostIds ) {
$excludedPostIds = explode( ', ', aioseo()->sitemap->helpers->excludedPosts() );
$excludedPostIds = array_map( function ( $postId ) {
return (int) $postId;
}, $excludedPostIds );
}
if ( in_array( $postId, $excludedPostIds, true ) ) {
return true;
}
// Let's also check if the post is published and not password-protected.
$post = get_post( $postId );
if ( ! is_a( $post, 'WP_Post' ) ) {
return true;
}
if ( ! empty( $post->post_password ) || 'publish' !== $post->post_status ) {
return true;
}
// Now, we must also check for noindex.
$metaData = aioseo()->meta->metaData->getMetaData( $post );
if ( ! empty( $metaData->robots_noindex ) ) {
return true;
}
return false;
}
/**
* Checks whether the given term should be excluded.
*
* @since 4.2.4
*
* @param int $termId The term ID.
* @return bool Whether the term should be excluded.
*/
private function isExcludedTerm( $termId ) {
static $excludedTermIds = null;
if ( null === $excludedTermIds ) {
$excludedTermIds = explode( ', ', aioseo()->sitemap->helpers->excludedTerms() );
$excludedTermIds = array_map( function ( $termId ) {
return (int) $termId;
}, $excludedTermIds );
}
if ( in_array( $termId, $excludedTermIds, true ) ) {
return true;
}
// At least one post must be assigned to the term.
$posts = aioseo()->core->db->start( 'term_relationships' )
->select( 'object_id' )
->where( 'term_taxonomy_id =', $termId )
->limit( 1 )
->run()
->result();
if ( empty( $posts ) ) {
return true;
}
// Now, we must also check for noindex.
$term = get_term( $termId );
if ( ! is_a( $term, 'WP_Term' ) ) {
return true;
}
$metaData = aioseo()->meta->metaData->getMetaData( $term );
if ( ! empty( $metaData->robots_noindex ) ) {
return true;
}
return false;
}
}