<?php
namespace DisableWPSitemap;

use WP_PowerSuite\Core\AbstractModule;

class Module extends AbstractModule {

	private const MODULE_ID = 'disable-wp-sitemap';
	private const MODULE_VERSION = '1.2.1';

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Disable WordPress Sitemap', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Disables the sitemap WordPress generates by default, so you avoid duplicate sitemaps if your SEO plugin already provides one.', 'wp-powersuite' );
	}

	public function getContext(): array {
		return [ 'admin', 'frontend' ];
	}

	protected function registerHooks(): void {
		add_filter( 'wp_sitemaps_enabled', '__return_false', 999 );
		if ( ! is_admin() ) {
			add_filter( 'robots_txt', [ $this, 'removeSitemapFromRobots' ], 999, 2 );
		}
	}

	public function removeSitemapFromRobots( string $output, bool $public ): string {
		unset( $public );

		$stripped = preg_replace( '/^\h*Sitemap:\h*\S*wp-sitemap[^\h]*\h*\R?/mi', '', $output );

		return is_string( $stripped ) ? $stripped : $output;
	}
}
