-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate sitemap qtranslate.php
More file actions
116 lines (99 loc) · 3.33 KB
/
create sitemap qtranslate.php
File metadata and controls
116 lines (99 loc) · 3.33 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
<?php
#sitemap_language.xml
if(class_exists('WPSEO_Sitemaps_Router') and class_exists('WPSEO_Sitemaps_Renderer') and function_exists('qtrans_getLanguage') and class_exists('WPSEO_Sitemap_Timezone')){
add_action( 'init', 'omazz_init', 2 );
add_filter( 'redirect_canonical', 'omazz_redirect_canonical' );
add_action( 'pre_get_posts', 'omazz_redirect', 1 );
function omazz_redirect_canonical( $redirect ) {
if ( get_query_var( 'sitemap_language' )) {
return false;
}
return $redirect;
}
function omazz_init() {
global $wp;
$wp->add_query_var( 'sitemap_language' );
add_rewrite_rule( 'sitemap_language\.xml$', 'index.php?sitemap_language=1', 'top' );
}
function omazz_create_sitemap(){
$lastmod = omazz_lastmod();
$stylesheet_url = preg_replace( '/(^http[s]?:)/', '', omazz_xsl_url() );
$xml = '<?xml-stylesheet type="text/xsl" href="' . esc_url( $stylesheet_url ) . '"?>';
$enabled_languages = get_option('qtranslate_enabled_languages');
$links = array();
if(isset($enabled_languages) and is_array($enabled_languages)){
foreach($enabled_languages as $item){
$links[] = array(
'loc' => site_url($item).'/sitemap_index.xml',
'lastmod' => $lastmod
);
}
$xml .= omazz_get_index($links);
}
return $xml;
}
function omazz_lastmod(){
$res = null;
$args = array(
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC',
'post_status' => 'publish',
);
$lastmods = get_posts( $args );
foreach ( $lastmods as $lastmod ) : setup_postdata( $lastmod );
$res = get_the_modified_date("Y/m/d H:i:s",$lastmod->ID);
endforeach;
wp_reset_postdata();
return $res;
}
function omazz_xsl_url(){
return get_template_directory_uri().'/sitemap/main-sitemap.xsl';
}
function omazz_get_index($links){
$timezone = new WPSEO_Sitemap_Timezone();
$xml = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ( $links as $link ) {
$xml .= omazz_sitemap_index_url( $link ,$timezone);
}
$xml .= '</sitemapindex>';
return $xml;
}
function omazz_sitemap_index_url( $url,$timezone) {
$date = null;
if ( ! empty( $url['lastmod'] ) ) {
$date = $timezone->format_date( $url['lastmod'] );
}
$url['loc'] = htmlspecialchars( $url['loc'] );
$output = "\t<sitemap>\n";
$output .= "\t\t<loc>" . $url['loc'] . "</loc>\n";
$output .= empty( $date ) ? '' : "\t\t<lastmod>" . htmlspecialchars( $date ) . "</lastmod>\n";
$output .= "\t</sitemap>\n";
return $output;
}
function omazz_redirect($query){
if ( ! $query->is_main_query() ) {
return;
}
$renderer = new WPSEO_Sitemaps_Renderer();
$language = get_query_var( 'sitemap_language' );
if ( ! empty( $language ) ) {
header( 'HTTP/1.1 200 OK', true, 200 );
// Prevent the search engines from indexing the XML Sitemap.
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml; charset=' . esc_attr( get_bloginfo( 'charset' ) ) );
echo omazz_create_sitemap();
die();
}
return;
}
}
function omazz_wpseo_stylesheet_url( $stylesheet )
{
$stylesheet = str_replace('index-', 'main-', $stylesheet);
$stylesheet = str_replace('qtx-', 'main-', $stylesheet);
$stylesheet = str_replace('qwp-', 'main-', $stylesheet);
return $stylesheet;
}
add_filter('wpseo_stylesheet_url', 'omazz_wpseo_stylesheet_url');
?>