get_var( "SELECT COUNT(*) FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = '_awb_seo_robots_index' WHERE p.post_type = 'post' AND p.post_status = 'publish' AND (m.meta_value IS NULL OR m.meta_value != 'noindex')" ); // Pages. // Use WP function for accuracy (handles page exclusions etc.). $pages_count = (int) wp_count_posts( 'page' )->publish; // Taxonomy terms. $tax_count = 0; $configured_taxes = $settings['sitemap_taxonomies'] ?? [ 'category' ]; foreach ( $configured_taxes as $tax ) { $terms = get_terms( [ 'taxonomy' => $tax, 'hide_empty' => true, 'fields' => 'count' ] ); if ( ! is_wp_error( $terms ) ) { $tax_count += (int) $terms; } } // Images — posts/pages that contain at least one tag. $images_count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('post','page') AND post_status = 'publish' AND post_content LIKE '%get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date_gmt >= %s", $two_days_ago ) ); $total = $posts_count + $pages_count + $tax_count + $images_count + $news_count; return compact( 'posts_count', 'pages_count', 'tax_count', 'images_count', 'news_count', 'total' ); } /** * Check whether the sitemap index is reachable (basic HTTP check). * * @return array { reachable: bool, code: int|null, latency_ms: int } */ public static function check_reachability() { $url = home_url( '/sitemap.xml' ); $start = microtime( true ); $resp = wp_remote_head( $url, [ 'timeout' => 5, 'sslverify' => false ] ); $ms = (int) round( ( microtime( true ) - $start ) * 1000 ); if ( is_wp_error( $resp ) ) { return [ 'reachable' => false, 'code' => null, 'latency_ms' => $ms ]; } $code = (int) wp_remote_retrieve_response_code( $resp ); return [ 'reachable' => $code >= 200 && $code < 400, 'code' => $code, 'latency_ms' => $ms ]; } private static function output( $xml ) { header( 'Content-Type: application/xml; charset=UTF-8' ); header( 'X-Robots-Tag: noindex, follow' ); echo $xml; } private static function header( $extra_ns = '' ) { return '' . "\n" . '\n"; } private static function footer() { return "\n"; } private static function build_index() { $sitemaps = [ home_url( '/sitemap-posts.xml' ), home_url( '/sitemap-pages.xml' ), home_url( '/sitemap-taxonomies.xml' ), home_url( '/sitemap-images.xml' ), home_url( '/sitemap-news.xml' ), ]; // Include products sub-sitemap only when WooCommerce is active. if ( post_type_exists( 'product' ) ) { $sitemaps[] = home_url( '/sitemap-products.xml' ); } $xml = '' . "\n"; $xml .= '' . "\n"; foreach ( $sitemaps as $url ) { $xml .= " \n " . esc_url( $url ) . "\n \n"; } $xml .= ''; return $xml; } private static function build_posts() { $posts = get_posts( [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => [ [ 'key' => '_awb_seo_robots_index', 'value' => 'noindex', 'compare' => '!=', ] ], ] ); return self::build_url_set( $posts ); } private static function build_pages() { $pages = get_posts( [ 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => -1, ] ); return self::build_url_set( $pages ); } private static function build_url_set( $posts, $with_images = false ) { $xml = self::header(); foreach ( $posts as $post ) { $robots = AWBSEO_Meta_Manager::get( $post->ID, 'robots_index', 'index' ); if ( $robots === 'noindex' ) continue; $xml .= " \n"; $xml .= ' ' . esc_url( get_permalink( $post->ID ) ) . "\n"; $xml .= ' ' . get_the_modified_date( 'c', $post->ID ) . "\n"; $xml .= " weekly\n"; $xml .= " 0.8\n"; if ( $with_images ) { preg_match_all( '/]+src=["\']([^"\']+)["\'][^>]*>/i', $post->post_content, $imgs ); foreach ( $imgs[1] as $img_url ) { $xml .= " \n"; $xml .= ' ' . esc_url( $img_url ) . "\n"; $xml .= " \n"; } } $xml .= " \n"; } return $xml . self::footer(); } private static function build_taxonomies() { $settings = get_option( 'awbseo_settings', AWBSEO_Core::default_settings() ); $xml = self::header(); foreach ( ( $settings['sitemap_taxonomies'] ?? [ 'category' ] ) as $tax ) { $terms = get_terms( [ 'taxonomy' => $tax, 'hide_empty' => true ] ); if ( is_wp_error( $terms ) ) continue; foreach ( $terms as $term ) { $xml .= " \n"; $xml .= ' ' . esc_url( get_term_link( $term ) ) . "\n"; $xml .= " weekly\n"; $xml .= " 0.6\n"; $xml .= " \n"; } } return $xml . self::footer(); } private static function build_products() { if ( ! post_type_exists( 'product' ) ) { return self::build_url_set( [] ); } // No meta_query filter here — products often have no _awb_seo_robots_index // meta row at all, which would cause a != compare to exclude them. // build_url_set() already skips noindex posts in PHP. $products = get_posts( [ 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, ] ); return self::build_url_set( $products ); } private static function build_images() { $post_types = [ 'post', 'page' ]; if ( post_type_exists( 'product' ) ) { $post_types[] = 'product'; } $posts = get_posts( [ 'post_type' => $post_types, 'post_status' => 'publish', 'posts_per_page' => -1 ] ); return self::build_url_set( $posts, true ); } private static function build_news() { // Last 2 days for Google News. $posts = get_posts( [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1000, 'date_query' => [ [ 'after' => '2 days ago' ] ], ] ); $xml = '' . "\n" . '' . "\n"; foreach ( $posts as $post ) { $xml .= " \n"; $xml .= ' ' . esc_url( get_permalink( $post->ID ) ) . "\n"; $xml .= " \n"; $xml .= ' ' . esc_html( get_bloginfo( 'name' ) ) . "en\n"; $xml .= ' ' . get_the_date( 'c', $post->ID ) . "\n"; $xml .= ' ' . esc_html( get_the_title( $post->ID ) ) . "\n"; $xml .= " \n"; $xml .= " \n"; } $xml .= "\n"; return $xml; } }