<?php
/**
 * Disable Cart Fragments module.
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Performance;

// Prevent direct access.
defined( 'ABSPATH' ) || exit;

/**
 * Cart Fragments module.
 *
 * @since 1.0.0
 */
final class Cart_Fragments extends \Kramar\Module {

	/**
	 * {@inheritDoc}
	 */
	public function get_slug(): string {
		return 'cart-fragments';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_category(): string {
		return 'performance';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_name(): string {
		return __( 'Disable Cart Fragments', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_description(): string {
		return __( 'Stops WooCommerce from firing an AJAX request on every page load to refresh the cart count. The #1 WooCommerce performance killer — safe to disable on pages that don\'t show a mini-cart widget.', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	protected function run(): void {
		add_action( 'wp_enqueue_scripts', [ $this, 'dequeue_fragments' ], 99 );
	}

	/**
	 * Dequeue the wc-cart-fragments script on non-cart/checkout pages.
	 *
	 * @since 1.0.0
	 */
	public function dequeue_fragments(): void {
		if ( is_cart() || is_checkout() ) {
			return;
		}

		wp_dequeue_script( 'wc-cart-fragments' );
	}
}
