<?php

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

class AIEO_Perf {

    
    private static $defer_handles_default = array(
        
        'aieo-side-cart',
        'aieo-sticky-atc',          
        'aieo-wishlist-heart',
        'aieo-waitlist',
        'aieo-brand-gifts',
        'aieo-recommendations',
        'aieo-complementary',
        'aieo-push-optin',          
        'aieo-dmm-navigator',       
        'aieo-badge-system',
        'aieo-chat-frontend',       
        
        'aieo-payment-logos',
        'aieo-trust-badges',
        'aieo-product-video',
        
        'rx-fibosearch',            
        'dgwt-wcas-style',          
        'roosterx-gravity-forms',   
        'wc-address-autocomplete',  
        'ppcp-pwc-payment-method',  
        'newsletter',               
        'photoswipe',               
        'photoswipe-default-skin',  
    );

    public static function init() {
        if ( is_admin() ) { return; }
        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return; }
        if ( defined( 'DOING_CRON' ) && DOING_CRON ) { return; }

        add_filter( 'style_loader_tag', array( __CLASS__, 'defer_non_critical_css' ), 10, 4 );
        add_action( 'wp_head', array( __CLASS__, 'preload_lcp_image' ), 1 );

        
        
        
        
        
        
        
        
        
        
        
        
        add_filter( 'aioseo_schema_graphs', array( __CLASS__, 'strip_aioseo_product_schema_on_archives' ), 5, 1 );
    }

    
    public static function strip_aioseo_product_schema_on_archives( $graphs ) {
        if ( ! is_array( $graphs ) || empty( $graphs ) ) { return $graphs; }
        
        
        
        if ( function_exists( 'is_singular' ) && is_singular( 'product' ) ) { return $graphs; }
        if ( function_exists( 'is_singular' ) && is_singular() && ! is_archive() ) { return $graphs; }

        $strip = array( 'WooCommerceProduct', 'Product', 'Offer', 'AggregateOffer' );
        return array_values( array_diff( $graphs, $strip ) );
    }

    
    public static function defer_non_critical_css( $tag, $handle, $href, $media ) {
        $handles = (array) apply_filters( 'aieo_perf_defer_css_handles', self::$defer_handles_default );
        if ( ! in_array( $handle, $handles, true ) ) { return $tag; }

        
        
        $current_media = is_string( $media ) ? strtolower( $media ) : '';
        if ( $current_media && $current_media !== 'all' && $current_media !== 'screen' && strpos( $current_media, 'screen' ) === false ) {
            return $tag;
        }

        $href_esc = esc_url( $href );
        $deferred  = sprintf(
            '<link rel="stylesheet" id="%1$s-css" href="%2$s" media="print" onload="this.media=\'all\';this.onload=null;" />', 
            esc_attr( $handle ),
            $href_esc
        );
        $deferred .= sprintf(
            '<noscript><link rel="stylesheet" id="%1$s-css-noscript" href="%2$s" media="all" /></noscript>', 
            esc_attr( $handle ),
            $href_esc
        );
        return $deferred;
    }

    
    public static function preload_lcp_image() {
        if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; }
        if ( ! function_exists( 'wc_get_product' ) ) { return; }

        global $post;
        if ( ! $post ) { return; }

        $product = wc_get_product( $post->ID );
        if ( ! $product ) { return; }

        $image_id = (int) $product->get_image_id();
        if ( ! $image_id ) { return; }

        
        
        
        
        $size = has_image_size( 'woocommerce_single' ) ? 'woocommerce_single' : 'medium_large';
        $src  = wp_get_attachment_image_src( $image_id, $size );
        if ( ! $src || empty( $src[0] ) ) { return; }

        $href  = $src[0];
        $w     = isset( $src[1] ) ? (int) $src[1] : 0;
        $h     = isset( $src[2] ) ? (int) $src[2] : 0;

        
        
        $srcset = function_exists( 'wp_get_attachment_image_srcset' )
            ? wp_get_attachment_image_srcset( $image_id, $size )
            : '';
        $sizes  = function_exists( 'wp_get_attachment_image_sizes' )
            ? wp_get_attachment_image_sizes( $image_id, $size )
            : '';

        $attrs = array(
            'rel="preload"',
            'as="image"',
            'fetchpriority="high"',
            'href="' . esc_url( $href ) . '"',
        );
        if ( $srcset ) {
            $attrs[] = 'imagesrcset="' . esc_attr( $srcset ) . '"';
        }
        if ( $sizes ) {
            $attrs[] = 'imagesizes="' . esc_attr( $sizes ) . '"';
        }

        echo '<link ' . implode( ' ', $attrs ) . " />\n"; 
    }
}

AIEO_Perf::init();
