id, 'agencyboost' ) === false ) return;
$rules = get_option( 'rewrite_rules' );
if ( empty( $rules ) || ! isset( $rules['^sitemap\\.xml$'] ) ) {
echo '
'
. 'AgencyBoost Sitemap: Go to '
. 'Settings → Permalinks'
. ' and click Save Changes once to activate the sitemap URL.
';
}
}
private static function output_index() {
$cached = get_transient( 'agenli_sitemap_index' );
if ( false !== $cached ) {
header( 'Content-Type: application/xml; charset=utf-8' );
echo $cached; // phpcs:ignore WordPress.Security.EscapeOutput
return;
}
$types = self::get_enabled_types();
$xml = '' . "\n";
$xml .= '' . "\n";
/* Always include homepage */
$xml .= "\t\n";
$xml .= "\t\t" . esc_url( home_url( '/sitemap-page.xml' ) ) . "\n";
$xml .= "\t\t" . esc_html( gmdate( 'Y-m-d' ) ) . "\n";
$xml .= "\t\n";
foreach ( $types as $type ) {
if ( 'page' === $type ) continue; // already added above
$count = wp_count_posts( $type );
if ( empty( $count->publish ) || 0 === (int) $count->publish ) continue;
$xml .= "\t\n";
$xml .= "\t\t" . esc_url( home_url( '/sitemap-' . $type . '.xml' ) ) . "\n";
$xml .= "\t\t" . esc_html( gmdate( 'Y-m-d' ) ) . "\n";
$xml .= "\t\n";
}
$xml .= '';
set_transient( 'agenli_sitemap_index', $xml, 12 * HOUR_IN_SECONDS );
header( 'Content-Type: application/xml; charset=utf-8' );
echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput
}
private static function output_type( $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
status_header( 404 ); return;
}
$cache_key = 'agenli_sitemap_' . $post_type;
$cached = get_transient( $cache_key );
if ( false !== $cached ) {
header( 'Content-Type: application/xml; charset=utf-8' );
echo $cached; // phpcs:ignore WordPress.Security.EscapeOutput
return;
}
/* Get excluded post IDs for this type */
$excluded_ids = self::get_excluded_ids( $post_type );
$posts = get_posts( array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => 1000,
'orderby' => 'modified',
'order' => 'DESC',
'post__not_in' => $excluded_ids,
) );
$xml = '' . "\n";
$xml .= '' . "\n";
if ( 'page' === $post_type ) {
$xml .= "\t\n\t\t" . esc_url( home_url( '/' ) ) . "\n";
$xml .= "\t\t" . esc_html( gmdate( 'Y-m-d' ) ) . "\n";
$xml .= "\t\tdaily\n\t\t1.0\n\t\n";
}
foreach ( $posts as $p ) {
$robots = agenli_get_post_meta( $p->ID, 'seo_robots', 'index,follow' );
if ( false !== strpos( $robots, 'noindex' ) ) continue;
$priority = 'page' === $post_type ? '0.8' : '0.6';
$changefreq = 'page' === $post_type ? 'monthly' : 'weekly';
$xml .= "\t\n";
$xml .= "\t\t" . esc_url( get_permalink( $p->ID ) ) . "\n";
$xml .= "\t\t" . esc_html( gmdate( 'Y-m-d', strtotime( $p->post_modified_gmt ) ) ) . "\n";
$xml .= "\t\t" . esc_html( $changefreq ) . "\n";
$xml .= "\t\t" . esc_html( $priority ) . "\n";
$xml .= "\t\n";
}
$xml .= '';
set_transient( $cache_key, $xml, 12 * HOUR_IN_SECONDS );
header( 'Content-Type: application/xml; charset=utf-8' );
echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput
}
public static function get_enabled_types() {
$saved = agenli_get_option( 'sitemap_post_types', '' );
if ( $saved ) {
return array_filter( array_map( 'sanitize_key', explode( ',', $saved ) ) );
}
return array_keys( get_post_types( array( 'public' => true ) ) );
}
/** Get post IDs manually excluded from sitemap */
public static function get_excluded_ids( $post_type ) {
$key = 'sitemap_excluded_' . $post_type;
$saved = agenli_get_option( $key, '' );
if ( empty( $saved ) ) return array();
return array_filter( array_map( 'absint', explode( ',', $saved ) ) );
}
/** Save excluded IDs for a post type */
public static function save_excluded_ids( $post_type, $ids ) {
$key = 'sitemap_excluded_' . $post_type;
agenli_set_option( $key, implode( ',', array_filter( array_map( 'absint', $ids ) ) ) );
}
public static function clear_cache() {
delete_transient( 'agenli_sitemap_index' );
foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
delete_transient( 'agenli_sitemap_' . $type );
}
}
public static function ajax_generate() {
check_ajax_referer( 'agenli_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => 'Unauthorised.' ) );
}
self::clear_cache();
$url = home_url( '/sitemap.xml' );
wp_remote_get(
'https://www.google.com/ping?sitemap=' . rawurlencode( $url ),
array( 'timeout' => 5, 'blocking' => false )
);
wp_send_json_success( array(
'url' => $url,
'message' => 'Sitemap regenerated and Google pinged successfully.',
) );
}
}