<?php
/**
 * XML sitemap masking for restricted content.
 *
 * @package Polanger_Dashboard_Tools
 */

if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

class PDT_Content_Visibility_Sitemaps {

  /**
   * Register sitemap hooks.
   *
   * @return void
   */
  public function register_hooks() {
    add_filter( 'wp_sitemaps_posts_query_args', array( $this, 'filter_sitemap_post_query_args' ), 10, 2 );
  }

  /**
   * Exclude restricted content from WordPress XML sitemaps.
   *
   * @param array  $args      Query args.
   * @param string $post_type Post type.
   * @return array
   */
  public function filter_sitemap_post_query_args( $args, $post_type ) {
    if ( ! PDT_Content_Visibility::should_hide_from_sitemaps() ) {
      return $args;
    }

    if ( ! in_array( $post_type, PDT_Content_Visibility::get_supported_post_types(), true ) ) {
      return $args;
    }

    return PDT_Content_Visibility::append_visibility_query_args( $args, 0 );
  }
}
