<?php
/**
 * WooCommerce visual editor sections (Ultimate Bundle).
 *
 * @package EasySite_Pro
 */

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

/**
 * Register commerce sections.
 *
 * @param array $sections Sections.
 * @return array
 */
function easysite_register_commerce_sections( $sections ) {
	if ( ! easysite_bundle_active() ) {
		return $sections;
	}
	$sections['shop_hero'] = array(
		'label'       => __( 'Shop Hero', 'easysite-pro' ),
		'description' => __( 'Headline area for your online store.', 'easysite-pro' ),
		'icon'        => 'dashicons-store',
		'fields'      => array(
			'title'       => array( 'type' => 'text', 'label' => __( 'Headline', 'easysite-pro' ) ),
			'subtitle'    => array( 'type' => 'textarea', 'label' => __( 'Subheadline', 'easysite-pro' ) ),
			'button_text' => array( 'type' => 'text', 'label' => __( 'Button text', 'easysite-pro' ) ),
			'button_url'  => array( 'type' => 'url', 'label' => __( 'Button link', 'easysite-pro' ) ),
			'bg_image'    => array( 'type' => 'image', 'label' => __( 'Background image', 'easysite-pro' ) ),
			'style'       => array( 'type' => 'select', 'label' => __( 'Style', 'easysite-pro' ), 'options' => array( 'gradient-overlay' => __( 'Gradient overlay', 'easysite-pro' ), 'centered' => __( 'Centered', 'easysite-pro' ) ) ),
			'gradient'    => array( 'type' => 'select', 'label' => __( 'Gradient', 'easysite-pro' ), 'options' => easysite_gradient_select_options() ),
		),
	);
	if ( ! class_exists( 'WooCommerce' ) ) {
		return $sections;
	}
	$sections['category_hero'] = array(
		'label'       => __( 'Category Hero', 'easysite-pro' ),
		'description' => __( 'Headline for a product category page.', 'easysite-pro' ),
		'icon'        => 'dashicons-category',
		'fields'      => array(
			'title'    => array( 'type' => 'text', 'label' => __( 'Category name', 'easysite-pro' ) ),
			'subtitle' => array( 'type' => 'textarea', 'label' => __( 'Description', 'easysite-pro' ) ),
			'bg_image' => array( 'type' => 'image', 'label' => __( 'Background image', 'easysite-pro' ) ),
			'style'    => array( 'type' => 'select', 'label' => __( 'Style', 'easysite-pro' ), 'options' => array( 'gradient-overlay' => __( 'Gradient overlay', 'easysite-pro' ), 'centered' => __( 'Centered', 'easysite-pro' ) ) ),
			'gradient' => array( 'type' => 'select', 'label' => __( 'Gradient', 'easysite-pro' ), 'options' => easysite_gradient_select_options() ),
		),
	);
	$sections['products'] = array(
		'label'       => __( 'Featured Products', 'easysite-pro' ),
		'description' => __( 'Showcase WooCommerce products.', 'easysite-pro' ),
		'icon'        => 'dashicons-cart',
		'fields'      => array(
			'title' => array( 'type' => 'text', 'label' => __( 'Section title', 'easysite-pro' ) ),
			'subtitle' => array( 'type' => 'textarea', 'label' => __( 'Intro text', 'easysite-pro' ) ),
			'count' => array( 'type' => 'select', 'label' => __( 'Number of products', 'easysite-pro' ), 'options' => array( '3' => '3', '4' => '4', '6' => '6' ) ),
			'button_text' => array( 'type' => 'text', 'label' => __( 'Shop button text', 'easysite-pro' ) ),
			'button_url'  => array( 'type' => 'url', 'label' => __( 'Shop button link', 'easysite-pro' ) ),
		),
	);
	return $sections;
}
add_filter( 'easysite_builder_sections', 'easysite_register_commerce_sections' );

/**
 * Render products section.
 *
 * @param array $d Fields.
 */
function easysite_pb_render_products( $d ) {
	if ( ! class_exists( 'WooCommerce' ) ) {
		return;
	}
	$count    = absint( easysite_pb_field( $d, 'count', 3 ) );
	$products = wc_get_products( array( 'limit' => $count, 'status' => 'publish', 'featured' => true ) );
	if ( empty( $products ) ) {
		$products = wc_get_products( array( 'limit' => $count, 'status' => 'publish' ) );
	}
	$empty = empty( $products );
	$sub = easysite_pb_field( $d, 'subtitle' );
	$btn = easysite_pb_field( $d, 'button_text', __( 'View all products', 'easysite-pro' ) );
	$url = easysite_pb_field( $d, 'button_url', wc_get_page_permalink( 'shop' ) );
	?>
	<section class="easysite-pb-section easysite-pb-products">
		<div class="easysite-container">
			<?php
			$t = easysite_pb_field( $d, 'title' );
			$title = easysite_ve_display( $t, __( 'Featured Products', 'easysite-pro' ) );
			if ( $title || easysite_ve_active() ) :
				?>
				<h2 class="easysite-pb-section-title<?php echo $t ? '' : ' esb-placeholder'; ?>"<?php echo easysite_ve_attr( 'title' ); ?>><?php echo esc_html( $title ); ?></h2>
			<?php endif; ?>
			<?php if ( $sub || easysite_ve_active() ) : ?>
				<p class="easysite-pb-lead<?php echo $sub ? '' : ' esb-placeholder'; ?>"<?php echo easysite_ve_richtext_attr( 'subtitle' ); ?>><?php echo easysite_pb_output_html( easysite_ve_display( $sub, __( 'Browse our best sellers.', 'easysite-pro' ) ) ); // phpcs:ignore ?></p>
			<?php endif; ?>
			<div class="easysite-pb-cards easysite-pb-cards--3 easysite-products-grid">
				<?php if ( $empty && easysite_ve_active() ) : ?>
					<?php for ( $i = 1; $i <= 3; $i++ ) : ?>
						<div class="easysite-pb-card easysite-product-card easysite-product-card--placeholder">
							<img src="<?php echo esc_url( easysite_placeholder_image( 'product' ) ); ?>" alt="" />
							<h3><?php esc_html_e( 'Sample product', 'easysite-pro' ); ?></h3>
							<p class="easysite-pb-price">£25.00</p>
						</div>
					<?php endfor; ?>
					<p class="description easysite-products-empty-hint"><?php esc_html_e( 'Add products in WooCommerce to replace these placeholders.', 'easysite-pro' ); ?></p>
				<?php elseif ( $empty ) : ?>
					<p class="easysite-pb-lead"><?php esc_html_e( 'No products yet. Add products in WooCommerce.', 'easysite-pro' ); ?></p>
				<?php else : ?>
				<?php foreach ( $products as $product ) : ?>
					<div class="easysite-pb-card easysite-product-card">
						<a href="<?php echo esc_url( $product->get_permalink() ); ?>">
							<?php echo $product->get_image( 'easysite-card' ); // phpcs:ignore ?>
							<h3><?php echo esc_html( $product->get_name() ); ?></h3>
							<p class="easysite-pb-price"><?php echo wp_kses_post( $product->get_price_html() ); ?></p>
						</a>
					</div>
				<?php endforeach; ?>
				<?php endif; ?>
			</div>
			<?php if ( $btn || easysite_ve_active() ) : ?>
				<p class="easysite-pb-cta-wrap">
					<a href="<?php echo esc_url( $url ); ?>" class="easysite-btn esb-editable-button" data-esb-field="button_text" data-esb-link-field="button_url"><?php echo esc_html( easysite_ve_display( $btn, __( 'View shop', 'easysite-pro' ) ) ); ?></a>
				</p>
			<?php endif; ?>
		</div>
	</section>
	<?php
}

/**
 * Render shop hero section.
 *
 * @param array $d Fields.
 */
function easysite_pb_render_shop_hero( $d ) {
	$title = easysite_ve_display( easysite_pb_field( $d, 'title' ), __( 'Shop Our Collection', 'easysite-pro' ) );
	$sub   = easysite_pb_field( $d, 'subtitle' );
	$btn   = easysite_pb_field( $d, 'button_text', __( 'Browse products', 'easysite-pro' ) );
	$url   = easysite_pb_field( $d, 'button_url', class_exists( 'WooCommerce' ) ? wc_get_page_permalink( 'shop' ) : '/shop' );
	$bg    = easysite_pb_field( $d, 'bg_image' );
	$style = easysite_pb_field( $d, 'style', 'gradient-overlay' );
	?>
	<section class="easysite-pb-section easysite-pb-hero easysite-pb-hero--<?php echo esc_attr( $style ); ?>"<?php echo $bg ? ' style="background-image:url(' . esc_url( $bg ) . ')"' : ''; ?>>
		<div class="easysite-container easysite-pb-hero__inner">
			<h1 class="easysite-pb-hero__title<?php echo easysite_pb_field( $d, 'title' ) ? '' : ' esb-placeholder'; ?>"<?php echo easysite_ve_attr( 'title' ); ?>><?php echo esc_html( $title ); ?></h1>
			<?php if ( $sub || easysite_ve_active() ) : ?>
				<p class="easysite-pb-hero__subtitle<?php echo $sub ? '' : ' esb-placeholder'; ?>"<?php echo easysite_ve_richtext_attr( 'subtitle' ); ?>><?php echo easysite_pb_output_html( easysite_ve_display( $sub, __( 'Quality products delivered to your door.', 'easysite-pro' ) ) ); // phpcs:ignore ?></p>
			<?php endif; ?>
			<a href="<?php echo esc_url( $url ); ?>" class="easysite-btn esb-editable-button" data-esb-field="button_text" data-esb-link-field="button_url"><?php echo esc_html( $btn ); ?></a>
		</div>
		<?php if ( easysite_ve_active() ) : ?>
			<button type="button" class="esb-ve-change-bg" data-esb-field="bg_image" data-esb-type="image"><span class="dashicons dashicons-format-image"></span> <?php esc_html_e( 'Change background', 'easysite-pro' ); ?></button>
		<?php endif; ?>
	</section>
	<?php
}

/**
 * Render category hero section.
 *
 * @param array $d Fields.
 */
function easysite_pb_render_category_hero( $d ) {
	$title = easysite_ve_display( easysite_pb_field( $d, 'title' ), __( 'Shop Category', 'easysite-pro' ) );
	$sub   = easysite_pb_field( $d, 'subtitle' );
	$bg    = easysite_pb_field( $d, 'bg_image' );
	$style = easysite_pb_field( $d, 'style', 'gradient-overlay' );
	?>
	<section class="easysite-pb-section easysite-pb-hero easysite-pb-category-hero easysite-pb-hero--<?php echo esc_attr( $style ); ?>"<?php echo $bg ? ' style="background-image:url(' . esc_url( $bg ) . ')"' : ''; ?>>
		<div class="easysite-container easysite-pb-hero__inner">
			<h1 class="easysite-pb-hero__title<?php echo easysite_pb_field( $d, 'title' ) ? '' : ' esb-placeholder'; ?>"<?php echo easysite_ve_attr( 'title' ); ?>><?php echo esc_html( $title ); ?></h1>
			<?php if ( $sub || easysite_ve_active() ) : ?>
				<p class="easysite-pb-hero__subtitle<?php echo $sub ? '' : ' esb-placeholder'; ?>"<?php echo easysite_ve_richtext_attr( 'subtitle' ); ?>><?php echo easysite_pb_output_html( easysite_ve_display( $sub, __( 'Browse products in this category.', 'easysite-pro' ) ) ); // phpcs:ignore ?></p>
			<?php endif; ?>
		</div>
		<?php if ( easysite_ve_active() ) : ?>
			<button type="button" class="esb-ve-change-bg" data-esb-field="bg_image" data-esb-type="image"><span class="dashicons dashicons-format-image"></span> <?php esc_html_e( 'Change background', 'easysite-pro' ); ?></button>
		<?php endif; ?>
	</section>
	<?php
}
