<?php
/**
 * Theme modifications for WooCommerce
 *
 * @package Multi
 */

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

add_action(
	'wp_enqueue_scripts',
	function () {
		wp_enqueue_script( 'wc-cart-fragments' );
	}
);


remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 15 );

add_filter( 'woocommerce_enqueue_styles', '__return_false' );
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );

// Single product page.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 15 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_after_single_product', 'woocommerce_upsell_display', 4 );
add_action( 'woocommerce_after_single_product', 'woocommerce_output_related_products', 5 );

/*
 * Hook in on activation
 *
 */
add_action( 'init', 'multimagazine_woocommerce_image_dimensions', 1 );

/**
 * Define image sizes
 */
function multimagazine_woocommerce_image_dimensions() {
	$catalog     = array(
		'width'  => '540',
		'height' => '540',
		'crop'   => 1,
	);
	$single_size = apply_filters( 'multimagazine_product_single_size', '600' );

	$single = array(
		'width'  => $single_size,
		'height' => $single_size,
		'crop'   => 1,
	);

	$multimagazine_thumbnail = array(
		'width'  => '100',
		'height' => '100',
		'crop'   => 1,
	);

	// Image sizes.
	update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs.
	update_option( 'shop_single_image_size', $single ); // Single product image.
	update_option( 'shop_thumbnail_image_size', $multimagazine_thumbnail ); // Image gallery thumbs.
}

/**
 * Limits product number per page.
 *
 * @param Integer $cols Column number. Ignored.
 */
function multimagazine_filter_loop_shop_per_page( $cols ) {
	return 12;
}
add_filter( 'loop_shop_per_page', 'multimagazine_filter_loop_shop_per_page', 20 );

/**
 * Sets up some wc-useful features.
 */
function multimagazine_woocommerce_setup() {
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );

	remove_theme_support( 'wc-product-gallery-lightbox' );
}
add_action( 'after_setup_theme', 'multimagazine_woocommerce_setup' );


/**
 * Cart
 */
function multimagazine_cart_shopping_bag() {
	$button_type      = get_theme_mod( 'multi_buttons_type', MULTIMAGAZINE_BUTTONS_TYPE );
	$button_animation = get_theme_mod( 'multi_button_animation', MULTIMAGAZINE_BUTTON_ANIMATION );
	$button_classes   = array( 'btn-type-' . $button_type, 'animated-buttons-' . $button_animation );
	?>
	<div class="shopping-bag"><div class="widget_shopping_cart_content">
	<?php

	if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0' ) >= 0 ) {
		the_widget( 'WC_Widget_Cart', array(), 'title=' );
	} else {
		the_widget( 'WooCommerce_Widget_Cart', 'title=' );
	}
	?>
	</div></div>
	<?php
}

// Custom mini cart buttons.
add_action(
	'woocommerce_widget_shopping_cart_buttons',
	function() {
		remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10 );
		remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

		add_action( 'woocommerce_widget_shopping_cart_buttons', 'multimagazine_widget_shopping_cart_button_view_cart', 10 );
		add_action( 'woocommerce_widget_shopping_cart_buttons', 'multimagazine_widget_shopping_cart_proceed_to_checkout', 20 );
	},
	1
);

/**
 * Renders the "view cart" button.
 */
function multimagazine_widget_shopping_cart_button_view_cart() {
	echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button secondary wc-forward">' . esc_html__( 'View cart', 'multimagazine' ) . '</a>';
}
/**
 * Renders the "proceed to checkout button.
 */
function multimagazine_widget_shopping_cart_proceed_to_checkout() {
	echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="button wc-forward">' . esc_html__( 'Checkout', 'multimagazine' ) . '</a>';
}

/**
 * Renders the product number on mini-cart icon.
 *
 * @param Array $fragments Fragments.
 */
function multimagazine_mini_cart_refresh_number( $fragments ) {
	ob_start();
	?>
	<span class="cart-link-qty">
		<?php
		if ( WC()->cart->get_cart_contents_count() ) {
			echo intval( WC()->cart->get_cart_contents_count() );
		}
		?>
	</span>
	<?php
	$fragments['.cart-link-qty'] = ob_get_clean();
	return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'multimagazine_mini_cart_refresh_number' );

/**
 * Wrapping cart table in a scrollable element
 */
function multimagazine_table_wrapper_begin() {
	echo '<div class="table">';
}
add_action( 'woocommerce_before_cart_table', 'multimagazine_table_wrapper_begin' );

/**
 * Ends table wrapper
 */
function multimagazine_table_wrapper_end() {
	echo '</div>';
}
add_action( 'woocommerce_after_cart_table', 'multimagazine_table_wrapper_end' );

add_action( 'woocommerce_before_cart', 'multimagazine_cart_page_start', 5 );
add_action( 'woocommerce_after_cart', 'multimagazine_close_div', 5 );
add_action( 'woocommerce_after_cart', 'multimagazine_cart_summary_start', 10 );
add_action( 'woocommerce_after_cart', 'woocommerce_cart_totals', 15 );
add_action( 'woocommerce_after_cart', 'multimagazine_close_div', 20 );
add_action( 'woocommerce_after_cart', 'multimagazine_close_div', 25 );

/**
 * Renders the beginning of cart page.
 */
function multimagazine_cart_page_start() {
	?>
<div class="cart-page"><div class="cart-page-main">
	<?php
}

/**
 * Renders the beginning of cart summary.
 */
function multimagazine_cart_summary_start() {
	?>
<div class="cart-summary">
	<?php
}

/**
 * Product list actions
 */
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

add_action( 'woocommerce_shop_loop_item_title', 'multimagazine_product_list_item_title', 10 );

/**
 * Renders product list item title.
 */
function multimagazine_product_list_item_title() {
	global $post;
	?>
	<h4 class="product-item-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
	<?php
}

$multimagazine_product_item_layout = absint( apply_filters( 'multimagazine_product_list_item_layout', 0 ) );
if ( 0 === $multimagazine_product_item_layout ) {
	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}

/**
 * Renders product list item thumbnail element.
 */
function multimagazine_product_list_item_thumbnail() {
	global $post;
	global $product;
	$product_item_layout = absint( apply_filters( 'multimagazine_product_list_item_layout', 0 ) );

	if ( has_post_thumbnail( $post->ID ) ) :
		$img_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'multimagazine-thumbnail-medium' );
		$img_src       = $img_src_array[0];
		$img_width     = $img_src_array[1];
		$img_height    = $img_src_array[2];
		if ( ! empty( $img_src ) ) :
			?>
		<a href="<?php the_permalink(); ?>">
		<img src="<?php echo esc_url( $img_src ); ?>" alt="<?php the_title(); ?>" width="<?php echo esc_attr( $img_width ); ?>" height="<?php echo esc_attr( $img_height ); ?>" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail">
		</a>
			<?php
		endif;
		$attachment_ids = $product->get_gallery_image_ids();
		if ( count( $attachment_ids ) ) :
			$url = wp_get_attachment_image_src( $attachment_ids[0], array( 400, 400 ) );
			if ( ! empty( $url ) ) :
				?>
			<a href="<?php the_permalink(); ?>" class="product-thumbnail-link">
				<span class="img-border">
				<?php if ( ! empty( $url[0] ) ) : ?>
					<img src="<?php echo esc_url( $url[0] ); ?>" alt="<?php the_title(); ?>" class="rollover-image">
				<?php endif; ?>
				</span>
			</a>
				<?php
			endif;
		endif;

		if ( 0 === $product_item_layout ) {
			wc_get_template_part( 'loop/add-to-cart' );
		}
	endif;
}
add_action( 'woocommerce_before_shop_loop_item_title', 'multimagazine_product_list_item_thumbnail', 10 );

/**
 * Opens product thumbnail.
 */
function multimagazine_product_thumbnail_open() {
	?>
<div class="img">
	<?php
}
add_action( 'woocommerce_before_shop_loop_item', 'multimagazine_product_thumbnail_open', 10 );

/**
 * Closes product thumbnail.
 */
function multimagazine_product_thumbnail_close() {
	?>
</div>
	<?php
}
add_action( 'woocommerce_before_shop_loop_item_title', 'multimagazine_product_thumbnail_close', 15 );

/**
 * Opens product info.
 */
function multimagazine_product_info_open() {
	?>
<div class="product-info">
	<?php
}
add_action( 'woocommerce_before_shop_loop_item_title', 'multimagazine_product_info_open', 20 );

/**
 * Closes product info
 */
function multimagazine_product_info_close() {
	?>
</div>
	<?php
}
add_action( 'woocommerce_after_shop_loop_item', 'multimagazine_product_info_close', 25 );

/**
 * Renders product navigation.
 */
function multimagazine_product_navigation() {
	global $post;
	$nav_classes = 'post-nav';
	$prev_post   = get_adjacent_post( false, '', true );
	if ( empty( $prev_post ) ) {
		$prev_post = multimagazine_get_newest_post( 'product' );
	}
	$next_post = get_adjacent_post( false, '', false );
	if ( empty( $next_post ) ) {
		$next_post = multimagazine_get_oldest_post( 'product' );
	}

	if ( empty( $prev_post ) ) {
		$nav_classes .= ' no-prev';
	}
	if ( empty( $next_post ) ) {
		$nav_classes .= ' no-next';
	}

	if ( ! empty( $prev_post ) || ! empty( $next_post ) ) :
		?>
		<nav class="<?php echo esc_attr( $nav_classes ); ?>" aria-label="<?php esc_attr_e( 'Product navigation', 'multimagazine' ); ?>">
			<?php
			if ( ! empty( $prev_post ) ) :
				$prev_id = $prev_post->ID;
				?>
				<a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>" class="post-nav-link prev cf">
					<i class="m-icon m-icon-long-arrow-left" aria-hidden="true"></i>
					<span class="post-nav-link-content">
						<span class="info" data-label="<?php esc_attr_e( 'Previous product', 'multimagazine' ); ?>"></span>
						<?php echo esc_html( $prev_post->post_title ); ?>
					</span>
				</a>
				<?php
			endif;

			if ( ! empty( $next_post ) ) :
				$next_id = $next_post->ID;
				?>
				<a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>" class="post-nav-link next cf">
					<i class="m-icon m-icon-long-arrow-right" aria-hidden="true"></i>
					<span class="post-nav-link-content">
						<span class="info" data-label="<?php esc_attr_e( 'Next product', 'multimagazine' ); ?>"></span>
						<?php echo esc_html( $next_post->post_title ); ?>
					</span>
				</a>
			<?php endif; ?>
		</nav>
		<?php
	endif;
}
add_action( 'woocommerce_after_single_product', 'multimagazine_product_navigation', 10 );

/**
 * Closes <div> element.
 */
function multimagazine_close_div() {
	?>
</div>
	<?php
}

/**
 * Prepares quantity control for the cart
 *
 * @param String $product_quantity The qty form field.
 */
function multimagazine_product_qty_filter( $product_quantity ) {
	?>
	<div class="product-qty-control">
		<a class="product-qty-btn product-qty-dec" href="#">－</a>
		<!-- <span class="product-qty">0</span> -->
		<?php echo $product_quantity; // phpcs:ignore
		// WooCommerce-generated quantity form field — output is trusted.
		?>
		<a class="product-qty-btn product-qty-inc" href="#">＋</a>
	</div>
	<?php
}

add_filter( 'woocommerce_cart_item_quantity', 'multimagazine_product_qty_filter' );

/**
 * Renders first part of product qty control in product page
 */
function multimagazine_product_qty_start() {
	?>
<div class="product-qty-control">
		<a class="product-qty-btn product-qty-dec" href="#">－</a>
		<span class="product-qty">0</span>
		<a class="product-qty-btn product-qty-inc" href="#">＋</a>
	<?php
}

add_action( 'woocommerce_before_add_to_cart_quantity', 'multimagazine_product_qty_start' );

/**
 * Renders last part of product qty control in product page
 */
function multimagazine_product_qty_end() {
	?>
	</div>
	<?php
}

add_action( 'woocommerce_after_add_to_cart_quantity', 'multimagazine_product_qty_end' );
