1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-19 14:46:40 +02:00
gdk/public/sitemap-xml.php

71 lines
2.4 KiB
PHP
Raw Normal View History

2020-01-25 07:52:35 +01:00
<?php
2020-02-11 17:27:35 +01:00
/**
* sitemap xml
*/
2020-01-25 07:52:35 +01:00
2020-03-15 14:29:06 +01:00
function gdk_sitemap_xml_api_handlers($template)
{
$hook = explode('-', get_query_var('sitemap'));
if (isset($hook[0]) && $hook[0] === 'gdk') {
if (isset($hook[1])) {
2020-01-25 07:52:35 +01:00
status_header(404);
header('HTTP/1.0 404 Not Found');
$GLOBALS['wp_query']->set_404();
2020-03-15 14:29:06 +01:00
include get_query_template('404');
exit;
2020-01-25 07:52:35 +01:00
}
2020-01-27 05:06:36 +01:00
$sitemap = get_transient('gdk-sitemap');
2020-03-15 14:29:06 +01:00
if (false === $sitemap || empty($sitemap)) {
2020-01-27 05:06:36 +01:00
$sitemap = gdk_create_sitemap();
2020-03-15 14:29:06 +01:00
set_transient('gdk-sitemap', $sitemap);
2020-01-25 07:52:35 +01:00
}
header("Content-type: text/xml");
echo $sitemap;
return;
}
2020-03-15 14:29:06 +01:00
return $template;
2020-01-25 07:52:35 +01:00
}
2020-03-15 14:29:06 +01:00
add_filter('template_include', 'gdk_sitemap_xml_api_handlers', 99);
2020-02-11 16:41:23 +01:00
2020-03-15 14:29:06 +01:00
function gdk_create_sitemap()
{
if (str_replace('-', '', get_option('gmt_offset')) < 10) {
$tempo = '-0' . str_replace('-', '', get_option('gmt_offset'));
2020-01-25 07:52:35 +01:00
} else {
2020-03-15 14:29:06 +01:00
$tempo = get_option('gmt_offset');
2020-01-25 07:52:35 +01:00
}
2020-03-15 14:29:06 +01:00
if (strlen($tempo) == 3) {$tempo = $tempo . ':00';}
$postsForSitemap = get_posts(array(
2020-01-25 07:52:35 +01:00
'numberposts' => -1,
'orderby' => 'modified',
'post_type' => array('post'),
'order' => 'DESC',
2020-03-15 14:29:06 +01:00
));
2020-01-25 07:52:35 +01:00
$sitemap = '';
$sitemap .= '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= "\n" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$sitemap .= "\t" . '<url>' . "\n" .
2020-03-15 14:29:06 +01:00
"\t\t" . '<loc>' . esc_url(home_url('/')) . '</loc>' .
"\n\t\t" . '<lastmod>' . date("Y-m-d\TH:i:s", current_time('timestamp', 0)) . $tempo . '</lastmod>' .
2020-01-25 07:52:35 +01:00
"\n\t\t" . '<changefreq>daily</changefreq>' .
"\n\t\t" . '<priority>1.0</priority>' .
"\n\t" . '</url>' . "\n";
2020-03-15 14:29:06 +01:00
foreach ($postsForSitemap as $post) {
setup_postdata($post);
$postdate = explode(" ", $post->post_modified);
2020-01-25 07:52:35 +01:00
$sitemap .= "\t" . '<url>' . "\n" .
2020-03-15 14:29:06 +01:00
"\t\t" . '<loc>' . get_permalink($post->ID) . '</loc>' .
2020-01-25 07:52:35 +01:00
"\n\t\t" . '<lastmod>' . $postdate[0] . 'T' . $postdate[1] . $tempo . '</lastmod>' .
"\n\t\t" . '<changefreq>Weekly</changefreq>' .
"\n\t\t" . '<priority>0.5</priority>' .
"\n\t" . '</url>' . "\n";
}
$sitemap .= '</urlset>';
return $sitemap;
}