sitemap_build_index(); }
if ( $type === 'posts' ) { $xml = $this->sitemap_build_posts(); }
if ( $type === 'pages' ) { $xml = $this->sitemap_build_pages(); }
if ( ! $xml ) { return; }
header( 'Content-Type: application/xml; charset=utf-8' );
header( 'X-Robots-Tag: noindex' );
echo $xml;
exit;
}
private function sitemap_build_index() {
$cached = get_transient( 'aije_sitemap_index' );
if ( $cached ) { return $cached; }
$now = gmdate( 'Y-m-d\TH:i:s+00:00' );
$p = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'post', 'orderby' => 'modified', 'order' => 'DESC', 'post_status' => 'publish', 'no_found_rows' => true ) );
$mod_posts = ! empty( $p ) ? mysql2date( 'c', $p[0]->post_modified_gmt ) : $now;
$pg = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'page', 'orderby' => 'modified', 'order' => 'DESC', 'post_status' => 'publish', 'no_found_rows' => true ) );
$mod_pages = ! empty( $pg ) ? mysql2date( 'c', $pg[0]->post_modified_gmt ) : $now;
$xml = '' . "\n";
$xml .= '' . "\n";
$xml .= '' . esc_url( home_url( '/sitemap-posts.xml' ) ) . '' . esc_html( $mod_posts ) . '' . "\n";
$xml .= '' . esc_url( home_url( '/sitemap-pages.xml' ) ) . '' . esc_html( $mod_pages ) . '' . "\n";
$xml .= '';
set_transient( 'aije_sitemap_index', $xml, 6 * HOUR_IN_SECONDS );
return $xml;
}
private function sitemap_build_posts() {
$cached = get_transient( 'aije_sitemap_posts' );
if ( $cached ) { return $cached; }
$posts = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1000,
'orderby' => 'modified',
'order' => 'DESC',
'no_found_rows' => true,
) );
$xml = $this->sitemap_build_urlset( $posts );
set_transient( 'aije_sitemap_posts', $xml, 6 * HOUR_IN_SECONDS );
return $xml;
}
private function sitemap_build_pages() {
$cached = get_transient( 'aije_sitemap_pages' );
if ( $cached ) { return $cached; }
$pages = get_posts( array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 500,
'orderby' => 'modified',
'order' => 'DESC',
'no_found_rows' => true,
) );
$xml = $this->sitemap_build_urlset( $pages );
set_transient( 'aije_sitemap_pages', $xml, 6 * HOUR_IN_SECONDS );
return $xml;
}
private function sitemap_build_urlset( $posts ) {
$xml = '' . "\n";
$xml .= '' . "\n";
foreach ( $posts as $post ) {
if ( get_post_meta( $post->ID, '_aije_sitemap_exclude', true ) ) { continue; }
$mod = mysql2date( 'c', $post->post_modified_gmt );
$priority = $post->post_type === 'page' ? '0.8' : '0.6';
$xml .= '';
$xml .= '' . esc_url( get_permalink( $post->ID ) ) . '';
$xml .= '' . esc_html( $mod ) . '';
$xml .= 'weekly';
$xml .= '' . $priority . '';
$xml .= '' . "\n";
}
$xml .= '';
return $xml;
}
public function sitemap_get_url() {
return home_url( '/sitemap_index.xml' );
}
}