<?php

namespace EasyCriticalCSS;

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

class Compatibility_WooCommerce extends Compatibility {
	public static function init() {
		self::add_conditional_filter( [ __CLASS__, 'is_woocommerce_installed' ], 'easy_cc_skip_critical', [ __CLASS__, 'skip_critical' ] );
		self::add_conditional_filter( [ __CLASS__, 'is_woocommerce_installed' ], 'easy_cc_critical_status', [ __CLASS__, 'critical_status' ], 10, 2 );
	}

	public static function is_woocommerce_installed() {
		return class_exists( 'woocommerce' );
	}

	public static function skip_critical( $should_skip ) {
		if ( function_exists( 'is_cart' ) && is_cart() ) {
			return true;
		}

		if ( function_exists( 'is_checkout' ) && is_checkout() ) {
			return true;
		}

		if ( function_exists( 'is_account_page' ) && is_account_page() ) {
			return true;
		}

		return $should_skip;
	}

	public static function critical_status( $status, $identifier ) {
		$id = intval( $identifier );

		// Is this the cart?
		$cart_id = wc_get_page_id( 'cart' );
		if ( $id === $cart_id ) {
			return 'excluded';
		}

		// What about the checkout?
		$checkout_id = wc_get_page_id( 'checkout' );
		if ( $id === $checkout_id ) {
			return 'excluded';
		}

		// Finally, check My Account?
		$myaccount_id = wc_get_page_id( 'myaccount' );
		if ( $id === $myaccount_id ) {
			return 'excluded';
		}

		return $status;
	}
}
