<?php
namespace RemoveVersionFromScripts;

use WP_PowerSuite\Core\AbstractModule;

class Module extends AbstractModule {

	private const MODULE_ID = 'remove-version-from-scripts';
	private const MODULE_VERSION = '1.2.1';

	private ?string $site_host = null;

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

	public function getName(): string {
		return __( 'Remove Version from Scripts', 'wp-powersuite' );
	}

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

	public function getDescription(): string {
		return __( 'Removes the WordPress core version from public stylesheet and script links while keeping theme and plugin version markers for cache busting.', 'wp-powersuite' );
	}

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

	protected function registerHooks(): void {
		add_filter( 'script_loader_src', [ $this, 'removeVersionFromUrl' ], 999, 2 );
		add_filter( 'style_loader_src', [ $this, 'removeVersionFromUrl' ], 999, 2 );
	}

	public function removeVersionFromUrl( string $src, string $handle ): string {
		unset( $handle );

		if ( $src === '' || strpos( $src, 'ver=' ) === false ) {
			return $src;
		}

		if ( apply_filters( 'wp_powersuite_remove_version_all_logged_out', false ) && ! is_user_logged_in() ) {
			if ( ! $this->isLocalStaticAssetUrl( $src ) ) {
				return $src;
			}

			return remove_query_arg( 'ver', $src );
		}

		$core = $this->getCoreWpVersion();
		if ( $core === '' ) {
			return $src;
		}

		$query = wp_parse_url( $src, PHP_URL_QUERY );
		if ( ! is_string( $query ) || $query === '' ) {
			return $src;
		}

		$params = [];
		wp_parse_str( $query, $params );
		if ( ! isset( $params['ver'] ) || (string) $params['ver'] !== $core ) {
			return $src;
		}

		return remove_query_arg( 'ver', $src );
	}

	private function isLocalStaticAssetUrl( string $src ): bool {
		$site_host = $this->getSiteHost();
		if ( $site_host === '' ) {
			return false;
		}

		$parts = wp_parse_url( $src );
		if ( ! is_array( $parts ) ) {
			return false;
		}

		if ( ! empty( $parts['host'] ) && strcasecmp( (string) $parts['host'], $site_host ) !== 0 ) {
			return false;
		}

		$path = isset( $parts['path'] ) ? (string) $parts['path'] : '';
		if ( $path === '' ) {
			return false;
		}

		$path_lower = strtolower( $path );

		return str_contains( $path_lower, 'wp-content' ) || str_contains( $path_lower, 'wp-includes' );
	}

	private function getSiteHost(): string {
		if ( $this->site_host !== null ) {
			return $this->site_host;
		}

		$host = wp_parse_url( home_url(), PHP_URL_HOST );

		return $this->site_host = is_string( $host ) ? $host : '';
	}

	private function getCoreWpVersion(): string {
		static $cached = null;
		if ( $cached !== null ) {
			return $cached;
		}

		if ( function_exists( 'wp_get_wp_version' ) ) {
			return $cached = wp_get_wp_version();
		}

		require_once ABSPATH . WPINC . '/version.php';
		global $wp_version;

		return $cached = is_string( $wp_version ?? null ) ? $wp_version : '';
	}
}
