class-wpml-seo-headlangs.php
6.76 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
<?php
class WPML_SEO_HeadLangs {
private $sitepress;
/**
* WPML_SEO_HeadLangs constructor.
*
* @param SitePress $sitepress
*/
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
private function get_seo_settings() {
$seo_settings = $this->sitepress->get_setting( 'seo', array() );
if ( ! array_key_exists( 'head_langs', $seo_settings ) ) {
$seo_settings['head_langs'] = 1;
}
if ( ! array_key_exists( 'head_langs_priority', $seo_settings ) ) {
$seo_settings['head_langs_priority'] = 1;
}
return $seo_settings;
}
public function init_hooks() {
if ( $this->sitepress->get_wp_api()->is_front_end() ) {
$seo_settings = $this->get_seo_settings();
$head_langs = $seo_settings['head_langs'];
if ( $head_langs ) {
$priority = $seo_settings['head_langs_priority'];
add_action( 'wp_head', array( $this, 'head_langs' ), (int) $priority );
}
}
}
function head_langs() {
$languages = $this->sitepress->get_ls_languages( array( 'skip_missing' => true ) );
/**
* @since 3.4.0
*/
$languages = apply_filters( 'wpml_head_langs', $languages );
if ( $this->must_render( $languages ) ) {
$hreflang_items = [];
$xdefault_href_lang = null;
foreach ( $languages as $lang ) {
/**
* @since 3.3.7
*/
$alternate_hreflang = apply_filters( 'wpml_alternate_hreflang', $lang['url'], $lang['code'] );
$hreflang_code = $this->get_hreflang_code( $lang );
if ( $hreflang_code ) {
$hreflang_items[ $hreflang_code ] = str_replace( '&', '&', $alternate_hreflang );
if ( $this->sitepress->get_default_language() === $lang['code'] ) {
$xdefault_href_lang = $hreflang_items[ $hreflang_code ];
}
}
}
if ( $xdefault_href_lang ) {
$hreflang_items['x-default'] = $xdefault_href_lang;
}
$hreflang_items = apply_filters( 'wpml_hreflangs', $hreflang_items );
$hreflang = '';
if ( is_array( $hreflang_items ) ) {
foreach ( $hreflang_items as $hreflang_code => $hreflang_url ) {
$hreflang .= '<link rel="alternate" hreflang="' . esc_attr( $hreflang_code ) . '" href="' . esc_url( $hreflang_url ) . '" />' . PHP_EOL;
}
echo apply_filters( 'wpml_hreflangs_html', $hreflang );
}
}
}
function render_menu() {
$seo = $this->get_seo_settings();
$options = array();
foreach ( array( 1, 10 ) as $priority ) {
$label = __( 'As early as possible', 'sitepress' );
if ( $priority > 1 ) {
$label = sprintf( esc_html__( 'Later in the head section (priority %d)', 'sitepress' ), $priority );
}
$options[ $priority ] = array(
'selected' => ( $priority == $seo['head_langs_priority'] ),
'label' => $label,
);
}
?>
<div class="wpml-section wpml-section-seo-options" id="lang-sec-9-5">
<div class="wpml-section-header">
<h3><?php esc_html_e( 'SEO Options', 'sitepress' ); ?></h3>
</div>
<div class="wpml-section-content">
<form id="icl_seo_options" name="icl_seo_options" action="">
<?php wp_nonce_field( 'icl_seo_options_nonce', '_icl_nonce' ); ?>
<p>
<input type="checkbox" id="icl_seo_head_langs" name="icl_seo_head_langs"
<?php
if ( $seo['head_langs'] ) {
echo 'checked="checked"';
}
?>
value="1"/>
<label for="icl_seo_head_langs"><?php esc_html_e( 'Display alternative languages in the HEAD section.', 'sitepress' ); ?></label>
</p>
<p>
<label for="wpml-seo-head-langs-priority"><?php esc_html_e( 'Position of hreflang links', 'sitepress' ); ?></label>
<select name="wpml_seo_head_langs_priority" id="wpml-seo-head-langs-priority"
<?php
if ( ! $seo['head_langs'] ) {
echo 'disabled="disabled"';
}
?>
>
<?php
foreach ( $options as $priority => $option ) {
?>
<option value="<?php echo esc_html( (string) $priority ); ?>" <?php echo $option['selected'] ? 'selected="selected"' : ''; ?>><?php echo esc_html( $option['label'] ); ?></option>
<?php
}
?>
</select>
</p>
<p class="buttons-wrap">
<span class="icl_ajx_response" id="icl_ajx_response_seo"> </span>
<input class="button button-primary" name="save" value="<?php esc_attr_e( 'Save', 'sitepress' ); ?>" type="submit"/>
</p>
</form>
</div>
</div>
<?php
}
private function must_render( $languages ) {
$must_render = false;
$wpml_queried_object = new WPML_Queried_Object( $this->sitepress );
$has_languages = is_array( $languages ) && count( $languages ) > 0;
if ( $has_languages && ! $this->sitepress->get_wp_api()->is_paged() ) {
if ( $wpml_queried_object->has_object() ) {
if ( $wpml_queried_object->is_instance_of_post() ) {
$post_id = $wpml_queried_object->get_id();
$is_single_or_page = $this->sitepress->get_wp_api()->is_single() || $this->sitepress->get_wp_api()->is_page();
$is_published = $is_single_or_page
&& $post_id
&& $this->sitepress->get_wp_api()->get_post_status( $post_id ) === 'publish';
$must_render = $this->sitepress->is_translated_post_type( $wpml_queried_object->get_post_type() )
&& ( $is_published || $this->is_home_front_or_archive_page() );
}
if ( $wpml_queried_object->is_instance_of_taxonomy() ) {
$must_render = $this->sitepress->is_translated_taxonomy( $wpml_queried_object->get_taxonomy() );
}
if ( $wpml_queried_object->is_instance_of_post_type() ) {
$must_render = $this->sitepress->is_translated_post_type( $wpml_queried_object->get_post_type_name() );
}
if ( $wpml_queried_object->is_instance_of_user() ) {
$must_render = true;
}
} elseif ( $this->is_home_front_or_archive_page() ) {
$must_render = true;
}
}
return $must_render;
}
/**
* @return bool
*/
private function is_home_front_or_archive_page() {
return $this->sitepress->get_wp_api()->is_home()
|| $this->sitepress->get_wp_api()->is_front_page()
|| $this->sitepress->get_wp_api()->is_archive()
|| is_search();
}
/**
* @param array $lang
*
* @return string
*/
private function get_hreflang_code( $lang ) {
$ordered_keys = [ 'tag', 'default_locale' ];
$hreflang_code = '';
foreach ( $ordered_keys as $key ) {
if ( array_key_exists( $key, $lang ) && trim( $lang[ $key ] ) ) {
$hreflang_code = $lang[ $key ];
break;
}
}
$hreflang_code = strtolower( str_replace( '_', '-', $hreflang_code ) );
if ( $this->is_valid_hreflang_code( $hreflang_code ) ) {
return trim( $hreflang_code );
}
return '';
}
private function is_valid_hreflang_code( $code ) {
return strlen( trim( $code ) ) >= 2;
}
}