get_upsell_ids() );
}
private static function holds_collection( array $block, string $collection, int $depth = 0 ): bool {
if ( 'woocommerce/product-collection' === ( $block['blockName'] ?? '' ) && $collection === ( $block['attrs']['collection'] ?? '' ) ) {
return true;
}
if ( $depth >= 3 ) {
return false;
}
foreach ( (array) ( $block['innerBlocks'] ?? [] ) as $inner ) {
if ( is_array( $inner ) && self::holds_collection( $inner, $collection, $depth + 1 ) ) {
return true;
}
}
return false;
}
public static function account_speculation( $rules ): void {
if ( ! $rules instanceof \WP_Speculation_Rules || ! is_user_logged_in() || ! function_exists( 'is_account_page' ) || ! is_account_page() || 'off' === \AIEO_Engine_Settings::get( 'speculation', 'moderate' ) ) {
return;
}
$rules->add_rule(
'prerender',
'rooster-account',
[
'source' => 'document',
'where' => [ 'selector_matches' => ':is(.rooster-account__item, .woocommerce-MyAccount-navigation-link, .wp-block-rooster-account-navigation li):not(.woocommerce-MyAccount-navigation-link--customer-logout, .is-active) > a' ],
'eagerness' => 'moderate',
]
);
}
public static function notice_printed( $template, $name ) {
if ( str_starts_with( (string) $name, 'notices/' ) ) {
\Rooster\Core\Performance::mark_rendered( 'woocommerce' );
}
return $template;
}
public static function hold_cart_refresh(): void {
if ( ( function_exists( 'is_cart' ) && is_cart() ) || ( function_exists( 'is_checkout' ) && is_checkout() ) || ! in_array( 'rooster-defer', wp_script_modules()->get_queue(), true ) ) {
return;
}
wp_print_inline_script_tag(
"(()=>{const f=window.fetch,d=document.documentElement;if(!f||/woocommerce_items_in_cart=1|woocommerce_cart_hash=/.test(document.cookie))return;let w=null;window.fetch=function(i,o){const u=typeof i==='string'?i:(i&&i.url)||'',m=String((o&&o.method)||(i&&i.method)||'GET').toUpperCase();if(m==='GET'&&/\\/wc\\/store\\/v1\\/cart(\\?|$)/.test(u)&&!d.classList.contains('rooster-human')){w=w||new Promise(r=>window.addEventListener('rooster:human',r,{once:true}));return w.then(()=>f.call(window,i,o));}return f.call(this,i,o);};})();",
[ 'id' => 'rooster-hold-cart-refresh' ]
);
}
public static function gallery_thumbnail_scroll( $content ) {
$content = (string) $content;
$at = strrpos( $content, '' );
if ( false === $at || ! str_contains( $content, 'wc-block-product-gallery-thumbnails' ) ) {
return $content;
}
\Rooster\Core\Defer::enqueue();
$long = 'long' === WooCommerce::gallery_arrow_style();
$buttons = '';
foreach ( [ 'prev' => [ $long ? 'move-up' : 'chevron-up', __( 'Previous thumbnails', 'ai-eshop-optimizer' ) ], 'next' => [ $long ? 'move-down' : 'chevron-down', __( 'More thumbnails', 'ai-eshop-optimizer' ) ] ] as $end => [ $icon, $label ] ) {
$buttons .= '';
}
return substr( $content, 0, $at ) . $buttons . substr( $content, $at );
}
public static function gallery_lcp_image( $content ) {
$content = (string) $content;
$tags = new \WP_HTML_Tag_Processor( $content );
while ( $tags->next_tag( 'img' ) ) {
if ( 'high' === $tags->get_attribute( 'fetchpriority' ) ) {
\Rooster\Core\Performance::lcp_image( (string) $tags->get_attribute( 'src' ), (string) $tags->get_attribute( 'srcset' ), (string) $tags->get_attribute( 'sizes' ), true );
break;
}
}
return $content;
}
public static function gate_gallery_slides( $content ) {
$content = (string) $content;
$tags = new \WP_HTML_Tag_Processor( $content );
if ( ! $tags->next_tag( [ 'class_name' => 'wc-block-product-gallery' ] ) ) {
return $content;
}
$context = json_decode( (string) $tags->get_attribute( 'data-wp-context' ), true );
$images = (array) ( $context['imageData'] ?? [] );
if ( count( $images ) < 2 || (int) ( $context['selectedImageId'] ?? 0 ) !== (int) $images[0] ) {
return $content;
}
\Rooster\Core\Defer::enqueue();
$tags->add_class( 'rooster-gallery-gate' );
return $tags->get_updated_html();
}
public static function cart_store_rest_url( $content ) {
$state = wp_interactivity_state( 'woocommerce' );
if ( empty( $state['restUrl'] ) ) {
wp_interactivity_state( 'woocommerce', [ 'restUrl' => get_rest_url() ] );
}
return (string) $content;
}
public static function keep_renderers( $keep ): bool {
return (bool) $keep && ! ( function_exists( 'is_cart' ) && is_cart() && 'classic' === WooCommerce::mode( 'woocommerce/cart' ) );
}
public static function classic_cart_checkout( $pre, $block ) {
$name = (string) ( $block['blockName'] ?? '' );
if ( null !== $pre || ! in_array( $name, [ 'woocommerce/cart', 'woocommerce/checkout' ], true ) || 'classic' !== WooCommerce::mode( $name ) ) {
return $pre;
}
if ( ! \WP_Block_Type_Registry::get_instance()->is_registered( 'woocommerce/classic-shortcode' ) ) {
return $pre;
}
$attrs = [ 'shortcode' => 'woocommerce/cart' === $name ? 'cart' : 'checkout' ];
if ( ! empty( $block['attrs']['align'] ) ) {
$attrs['align'] = (string) $block['attrs']['align'];
}
return render_block( [ 'blockName' => 'woocommerce/classic-shortcode', 'attrs' => $attrs, 'innerBlocks' => [], 'innerHTML' => '', 'innerContent' => [] ] );
}
public static function keep_classic_styles( $keep ): bool {
return (bool) $keep
|| ( function_exists( 'is_cart' ) && is_cart() && 'classic' === WooCommerce::mode( 'woocommerce/cart' ) )
|| ( function_exists( 'is_checkout' ) && is_checkout() && 'classic' === WooCommerce::mode( 'woocommerce/checkout' ) );
}
public static function card_first_paint( $content, $block, $instance = null ) {
if ( ! WooCommerce::is_card( $block, $instance ) || ! str_contains( (string) $content, '
next_tag( 'img' ) || null === $tags->get_attribute( 'srcset' ) ) {
return $content;
}
$small = \Rooster\Core\Performance::small_candidate( (string) $tags->get_attribute( 'srcset' ), (int) apply_filters( 'rooster/woocommerce/card_first_width', self::CARD_FIRST ) );
if ( '' === $small ) {
return $content;
}
$tags->set_attribute( 'data-rooster-srcset', (string) $tags->get_attribute( 'srcset' ) );
$tags->set_attribute( 'data-rooster-sizes', (string) preg_replace( '/^\s*auto\s*,\s*/', '', (string) $tags->get_attribute( 'sizes' ) ) );
$tags->set_attribute( 'src', $small );
$tags->remove_attribute( 'srcset' );
$tags->remove_attribute( 'sizes' );
if ( 'high' === $tags->get_attribute( 'fetchpriority' ) ) {
\Rooster\Core\Performance::lcp_image( $small, '', '', true );
}
\Rooster\Core\Defer::enqueue();
return $tags->get_updated_html();
}
public static function grid_first_paint( $content ) {
if ( ! str_contains( (string) $content, 'wc-block-grid__product-image' ) || ! class_exists( \Rooster\Core\Performance::class ) ) {
return $content;
}
$phone = class_exists( \Rooster\Core\DeviceClass::class ) && 'mobile' === \Rooster\Core\DeviceClass::current();
$hidden = self::grid_hidden( (string) $content );
$width = (int) apply_filters( 'rooster/woocommerce/grid_first_width', $phone ? self::GRID_FIRST_PHONE : self::grid_column_width( (string) $content ) );
$tags = new \WP_HTML_Tag_Processor( (string) $content );
$found = false;
$changed = false;
$noted = false;
while ( $tags->next_tag( 'img' ) ) {
$high = 'high' === $tags->get_attribute( 'fetchpriority' );
if ( $hidden ) {
$noted = $noted || $high;
$tags->set_attribute( 'loading', 'lazy' );
$tags->remove_attribute( 'fetchpriority' );
$changed = true;
}
$srcset = $tags->get_attribute( 'srcset' );
$small = is_string( $srcset ) ? \Rooster\Core\Performance::small_candidate( $srcset, $width ) : '';
if ( '' === $small ) {
continue;
}
$tags->set_attribute( 'data-rooster-srcset', $srcset );
$tags->set_attribute( 'data-rooster-sizes', (string) preg_replace( '/^\s*auto\s*,\s*/', '', (string) $tags->get_attribute( 'sizes' ) ) );
$tags->set_attribute( 'src', $small );
$tags->remove_attribute( 'srcset' );
$tags->remove_attribute( 'sizes' );
if ( $high && ! $hidden ) {
\Rooster\Core\Performance::lcp_image( $small, '', '', true );
}
$found = true;
$changed = true;
}
if ( $noted ) {
\Rooster\Core\Performance::reset_lcp_image();
}
if ( ! $changed ) {
return $content;
}
if ( $found ) {
\Rooster\Core\Defer::enqueue();
}
return $tags->get_updated_html();
}
private static function grid_hidden( string $content ): bool {
$hidden = function_exists( 'is_cart' ) && is_cart() && function_exists( 'WC' ) && WC()->cart && ! WC()->cart->is_empty();
return (bool) apply_filters( 'rooster/woocommerce/grid_hidden', $hidden, $content );
}
private static function grid_column_width( string $content ): int {
$columns = 3;
$wide = false;
$tags = new \WP_HTML_Tag_Processor( $content );
while ( $tags->next_tag() ) {
if ( ! $tags->has_class( 'wc-block-grid' ) ) {
continue;
}
if ( (int) $tags->get_attribute( 'data-columns' ) > 0 ) {
$columns = (int) $tags->get_attribute( 'data-columns' );
}
foreach ( $tags->class_list() as $class ) {
if ( preg_match( '/^has-(\d+)-columns$/', (string) $class, $m ) && (int) $tags->get_attribute( 'data-columns' ) <= 0 ) {
$columns = max( 1, (int) $m[1] );
} elseif ( 'alignwide' === $class || 'alignfull' === $class ) {
$wide = true;
}
}
break;
}
$layout = (array) wp_get_global_settings( [ 'layout' ] );
$size = self::px( (string) ( $layout[ $wide ? 'wideSize' : 'contentSize' ] ?? '' ) );
return $size > 0 ? max( self::CARD_FIRST, (int) ceil( $size / $columns ) ) : self::CARD_FIRST;
}
private static function px( string $length ): int {
if ( preg_match( '/^\s*([0-9.]+)\s*(px|rem|em)\s*$/i', $length, $m ) ) {
return (int) round( (float) $m[1] * ( 'px' === strtolower( $m[2] ) ? 1 : 16 ) );
}
return 0;
}
public static function place_order_click(): void {
if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url() ) || 'blocks' !== WooCommerce::mode( 'woocommerce/checkout' ) ) {
return;
}
$file = AIEO_ENGINE_DIR . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '/assets/js/inline/place-order-click.js' : '/assets/js/inline/place-order-click.min.js' );
wp_print_inline_script_tag( (string) file_get_contents( $file ), [ 'id' => 'rooster-place-order-click' ] );
}
public static function shortcode_loop_rendered(): void {
\Rooster\Core\Performance::mark_rendered( 'woocommerce' );
\Rooster\Core\Performance::mark_rendered( 'woocommerce/products' );
}
}