<?php
/**
 * WooCommerce integration helpers and template hooks.
 *
 * @package EasySite_Pro
 */

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

/**
 * WooCommerce theme support tweaks.
 */
function easysite_woocommerce_setup() {
	if ( ! easysite_wc_active() ) {
		return;
	}

	add_filter( 'woocommerce_enqueue_styles', 'easysite_dequeue_wc_default_styles' );
	add_action( 'wp_enqueue_scripts', 'easysite_enqueue_woocommerce_styles', 20 );
}
add_action( 'after_setup_theme', 'easysite_woocommerce_setup', 20 );

/**
 * Use theme styles instead of default WooCommerce CSS.
 *
 * @param array $styles Registered styles.
 * @return array
 */
function easysite_dequeue_wc_default_styles( $styles ) {
	unset( $styles['woocommerce-general'] );
	unset( $styles['woocommerce-layout'] );
	unset( $styles['woocommerce-smallscreen'] );
	return $styles;
}

/**
 * Enqueue EasySite WooCommerce styles.
 */
function easysite_enqueue_woocommerce_styles() {
	if ( ! easysite_wc_active() ) {
		return;
	}

	wp_enqueue_style(
		'easysite-woocommerce',
		EASYSITE_PRO_URI . '/assets/css/woocommerce.css',
		array( 'easysite-pro-main' ),
		EASYSITE_PRO_VERSION
	);
}

/**
 * Products per row on shop archive.
 *
 * @return int
 */
function easysite_loop_columns() {
	return 3;
}
add_filter( 'loop_shop_columns', 'easysite_loop_columns' );

/**
 * Products per page on shop archive.
 *
 * @return int
 */
function easysite_products_per_page() {
	return 12;
}
add_filter( 'loop_shop_per_page', 'easysite_products_per_page' );

/**
 * Wrap main WooCommerce content.
 */
function easysite_wc_before_main_content() {
	echo '<div class="easysite-wc-main">';
}
add_action( 'woocommerce_before_main_content', 'easysite_wc_before_main_content', 5 );

/**
 * Close main WooCommerce wrapper.
 */
function easysite_wc_after_main_content() {
	echo '</div>';
}
add_action( 'woocommerce_after_main_content', 'easysite_wc_after_main_content', 50 );

/**
 * Remove default sidebar output; theme handles layout.
 */
function easysite_remove_wc_sidebar() {
	remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
}
add_action( 'wp', 'easysite_remove_wc_sidebar' );

/**
 * Custom add-to-cart button text on archives.
 *
 * @return string
 */
function easysite_custom_add_to_cart_text() {
	return __( 'View product', 'easysite-pro' );
}
add_filter( 'woocommerce_product_add_to_cart_text', 'easysite_custom_add_to_cart_text' );
