<?php
/**
 * WooCommerce support for FansPaysite theme.
 *
 * Loaded only when WooCommerce is active. Declares theme support, adjusts
 * the product-grid wrapper to use the theme's layout, and enqueues a
 * stylesheet that applies the theme's light/dark mode variables to WC
 * elements.
 *
 * @package FansPaysite
 * @since   2.3
 */

// Bail if WooCommerce is not active.
if ( ! class_exists( 'WooCommerce' ) ) {
	return;
}

/**
 * Declare WooCommerce theme support.
 *
 * @since 2.3
 */
function fanspaysite_woocommerce_setup() {
	add_theme_support( 'woocommerce', array(
		'thumbnail_image_width' => 300,
		'single_image_width'    => 600,
		'product_grid'          => array(
			'default_rows'    => 3,
			'min_rows'        => 1,
			'default_columns' => 3,
			'min_columns'     => 1,
			'max_columns'     => 4,
		),
	) );
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'fanspaysite_woocommerce_setup' );

/**
 * Add a body class on WooCommerce pages for targeted CSS overrides.
 *
 * @since 2.3
 * @param  array $classes Existing body classes.
 * @return array
 */
function fanspaysite_woocommerce_body_class( $classes ) {
	if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) {
		$classes[] = 'has-woocommerce';
	}
	return $classes;
}
add_filter( 'body_class', 'fanspaysite_woocommerce_body_class' );

/**
 * Replace the default WooCommerce wrapper with the theme's content wrapper.
 *
 * WooCommerce outputs its own <main class="woocommerce"> wrapper by default.
 * We replace it so products sit inside the same content area as regular pages.
 *
 * @since 2.3
 */
function fanspaysite_woocommerce_wrapper_before() {
	echo '<div id="primary" class="content-area"><div class="site-main">';
}

function fanspaysite_woocommerce_wrapper_after() {
	echo '</div></div>';
}

remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
add_action( 'woocommerce_before_main_content', 'fanspaysite_woocommerce_wrapper_before' );
add_action( 'woocommerce_after_main_content', 'fanspaysite_woocommerce_wrapper_after' );

/**
 * Remove the default WooCommerce sidebar.
 *
 * The theme handles sidebars via its own layout system (left-sidebar or
 * top-bar), so we disable WC's built-in sidebar call.
 *
 * @since 2.3
 */
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );

/**
 * Enqueue the WooCommerce stylesheet on the front end.
 *
 * Depends on the main theme stylesheet so CSS variable declarations are
 * already available when WooCommerce rules are parsed.
 *
 * @since 2.3
 */
function fanspaysite_woocommerce_styles() {
	$path    = get_template_directory() . '/css/woocommerce.css';
	$version = file_exists( $path ) ? filemtime( $path ) : '2.3';
	wp_enqueue_style(
		'fanspaysite-woocommerce',
		get_template_directory_uri() . '/css/woocommerce.css',
		array( 'fanspaysite-style' ),
		$version
	);
}
add_action( 'wp_enqueue_scripts', 'fanspaysite_woocommerce_styles' );
