filename ) ); ?>

true ), 'objects' ); $selected_post_types = get_option( 'ast_sitemap_post_types', array( 'post', 'page' ) ); foreach ( $post_types as $post_type ) { printf( '
', esc_attr( $post_type->name ), checked( in_array( $post_type->name, $selected_post_types ), true, false ), esc_html( $post_type->label ) ); } ?>
true ), 'objects' ); $selected_taxonomies = get_option( 'ast_sitemap_taxonomies', array( 'category', 'post_tag' ) ); foreach ( $taxonomies as $taxonomy ) { printf( '
', esc_attr( $taxonomy->name ), checked( in_array( $taxonomy->name, $selected_taxonomies ), true, false ), esc_html( $taxonomy->label ) ); } ?>

get_sitemap_content(); $sitemap_preview = $this->format_xml( $sitemap_content ); echo '
' . esc_html( $sitemap_preview ) . '
'; ?>
get_sitemap_content(); $this->write_sitemap( $sitemap_content ); } private function get_sitemap_content() { $sitemap = new SimpleXMLElement( '' ); $post_types = get_option( 'ast_sitemap_post_types', array( 'post', 'page' ) ); $taxonomies = get_option( 'ast_sitemap_taxonomies', array( 'category', 'post_tag' ) ); // Add homepage $this->add_url( $sitemap, home_url( '/' ), '1.0', 'daily' ); // Add post type archives foreach ( $post_types as $post_type ) { if ( $post_type !== 'page' && $archive_url = get_post_type_archive_link( $post_type ) ) { $this->add_url( $sitemap, $archive_url, '0.8', 'daily' ); } } // Add individual posts and pages foreach ( $post_types as $post_type ) { $posts = get_posts( array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => -1, ) ); foreach ( $posts as $post ) { $this->add_url( $sitemap, get_permalink( $post ), '0.6', 'weekly', get_the_modified_date( 'c', $post ) ); } } // Add taxonomy terms foreach ( $taxonomies as $taxonomy ) { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => true ) ); foreach ( $terms as $term ) { $this->add_url( $sitemap, get_term_link( $term ), '0.4', 'weekly' ); } } return $sitemap->asXML(); } private function add_url( $sitemap, $loc, $priority, $changefreq, $lastmod = '' ) { $url = $sitemap->addChild( 'url' ); $url->addChild( 'loc', esc_url( $loc ) ); $url->addChild( 'priority', esc_attr( $priority ) ); $url->addChild( 'changefreq', esc_attr( $changefreq ) ); if ( $lastmod ) { $url->addChild( 'lastmod', esc_attr( $lastmod ) ); } } private function write_sitemap( $content ) { $sitemap_path = ABSPATH . $this->filename; file_put_contents( $sitemap_path, $content ); // Ping search engines $this->ping_search_engines(); } private function ping_search_engines() { $sitemap_url = home_url( '/' . $this->filename ); $ping_urls = array( "https://www.google.com/webmasters/tools/ping?sitemap=" . urlencode( $sitemap_url ), "https://www.bing.com/ping?sitemap=" . urlencode( $sitemap_url ) ); foreach ( $ping_urls as $url ) { wp_safe_remote_get( esc_url_raw( $url ) ); } } public function update_sitemap( $post_id ) { // Check if this is a revision or autosave if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } // Check if the post type should be included in the sitemap $post_type = get_post_type( $post_id ); $included_post_types = get_option( 'ast_sitemap_post_types', array( 'post', 'page' ) ); if ( ! in_array( $post_type, $included_post_types ) ) { return; } // Regenerate the sitemap $this->generate_sitemap(); } private function format_xml( $xml ) { $dom = new DOMDocument( '1.0' ); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML( $xml ); $formatted = $dom->saveXML(); // Add syntax highlighting (you may want to use a proper XML syntax highlighter library for better results) $formatted = preg_replace( array( '/<urlset.*?>/', '/<\/urlset>/', '/<url>/', '/<\/url>/', '/<(\w+)>/', '/<\/(\w+)>/', '/>(.+?)</', ), array( '<urlset$1>', '</urlset>', '<url>', '</url>', '<$1>', '</$1>', '>$1<', ), htmlspecialchars( $formatted ) ); return $formatted; } }