<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Hook module functions if plugin is active
 */
if ( defined( 'WCV_VERSION' ) ) {
	add_action( 'init', 'shrk_wcvendors_acf_fields' );
	add_action( 'wp', 'shrk_wcvendors_template_mods', 15 );
	add_filter( 'woocommerce_locate_template', 'shrk_wcvendors_template_path', 10, 3 );
	add_filter( 'shrk-cover-image', 'shrk_wcvendors_cover_image', 20 );
	add_filter( 'shrk-cover-image-id', 'shrk_wcvendors_cover_image_id', 20 );
	add_action( 'woocommerce_before_template_part', 'shrk_wcvendors_templates_wrapper', 10, 4 );
	add_action( 'woocommerce_after_template_part', 'shrk_wcvendors_templates_wrapper_close', 10, 4 );
	add_action( 'wp_enqueue_scripts', 'shrk_wcvendors_scripts' );
}

/**
 * Enqueue module scripts and styles
 */





function shrk_wcvendors_scripts() {
	global $shrk_theme_info;
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	wp_enqueue_style( 'shrk-wc-vendors', SHRK_THEME_MODULES_URI . 'wc-vendors/css/wc-vendors' . $suffix . '.css' , array( 'shrk-style' ), $shrk_theme_info['Version']  );
}

/**
 * Modify template path for selected templates
 * @return string the template file's path
 */
function shrk_wcvendors_template_path( $template, $template_name, $template_path ) {
	$new_template_path = str_replace( trailingslashit( get_template_directory() ), '', SHRK_THEME_MODULES ) . '/wc-vendors/templates/' . $template_path;
	$for_templates = array( 
		'vendor-main-header.php',
		'links.php'
	);

	if ( strpos( $template_path, 'wc-vendors' ) !== false && in_array( $template_name, $for_templates ) ) {
		$new_template = locate_template(
			array(
				trailingslashit( $new_template_path ) . $template_name,
				$template_name,
			)
		);

		if ( $new_template ) {
			return $new_template;
		}
	}

	return $template;
}

/**
 * Return names of templates to wrap in wrapper
 * @return array template names
 */
function shrk_wcvendors_templates_for_wrapper() {
	$templates = array( 
		'vendor-main-header.php',
		'vendor-mini-header.php',
		'denied.php',
		'links.php',
		'reports.php',
		'orders.php',
		'settings.php',
		'csv-export.php'
	);

	return $templates;
}

/**
 * Hook function to start template wrapper
 */
function shrk_wcvendors_templates_wrapper( $template_name, $template_path, $located, $args ) {
	$for_templates = shrk_wcvendors_templates_for_wrapper();

	if ( strpos( $template_path, 'wc-vendors' ) !== false && $located && in_array( $template_name, $for_templates ) ) {
		echo '<div class="shrk-wcvendors-wrapper template-' . esc_attr( str_replace('.php', '', $template_name ) ) . '">';
	}
}

/**
 * Hook function to end template wrapper
 */
function shrk_wcvendors_templates_wrapper_close( $template_name, $template_path, $located, $args ) {
	$for_templates = shrk_wcvendors_templates_for_wrapper();

	if ( strpos( $template_path, 'wc-vendors' ) !== false && $located && in_array( $template_name, $for_templates ) ) {
		echo '</div><!-- close .shrk-wcvendors-wrapper template-' . esc_attr( str_replace('.php', '', $template_name ) ) . ' -->';

	}
}

/**
 * Re-position wcvendors template hooks
 */
function shrk_wcvendors_template_mods() {
	if ( WCV_Vendors::is_vendor_page() ) {
		if (WC_Vendors::$pv_options->get_option( 'shop_headers_enabled' ) ) { 
			remove_action( 'woocommerce_before_main_content', array( 'WCV_Vendor_Shop', 'vendor_main_header'), 20 ); 
			add_action( 'shrk-page-cover-header-after', array( 'WCV_Vendor_Shop', 'vendor_main_header'), 10 ); 

			// Remove shop page menus
			remove_action( 'shrk-page-cover-header-after', 'woo_shoppage_afterheader_content' );

			add_filter( 'shrk-page-cover-classes', function( $classes ) {
				$classes[] = 'wc-vendors-cover-title';
				return $classes;
			});
		}
	}

	// Add sold by to product loop before add to cart
	if ( WC_Vendors::$pv_options->get_option( 'sold_by' ) ) { 
		remove_action( 'woocommerce_after_shop_loop_item', array('WCV_Vendor_Shop', 'template_loop_sold_by'), 9 );
		add_action( 'woocommerce_after_shop_loop_item_title', array('WCV_Vendor_Shop', 'template_loop_sold_by'), 17 );
	} 
}

/**
 * Get Vendor's cover image
 * @return string the cover image's url
 */
function shrk_wcvendors_cover_image( $cover ) {
	if ( class_exists( 'WCV_Vendors' ) && WCV_Vendors::is_vendor_page() ) {
		$cover = false;
		$user_id = shrk_wcvendors_get_current_vendor();

		if ( function_exists( 'get_field' ) && $user_id ) {
			$image_id = get_field( 'vendor_cover_image', 'user_' . $user_id );
			$cover    = $image_id ? wp_get_attachment_image_src( $image_id, 'full' ) : false;
			$cover    = $cover ? $cover[0] : false;
		}
	}

	return $cover;
}

/**
 * Get Vendor's cover image id
 * @return integer the cover image's id
 */
function shrk_wcvendors_cover_image_id( $cover_id ) {
	if ( class_exists( 'WCV_Vendors' ) && WCV_Vendors::is_vendor_page() ) {
		$cover_id = false;
		$user_id = shrk_wcvendors_get_current_vendor();

		if ( function_exists( 'get_field' ) && $user_id ) {
			$cover_id = get_field( 'vendor_cover_image', 'user_' . $user_id );
		}
	}

	return $cover_id;
}

/**
 * Helper to get current vendor id
 * @return integer the vendor's id
 */
function shrk_wcvendors_get_current_vendor() {
	$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
	$vendor_id   = WCV_Vendors::get_vendor_id( $vendor_shop );

	return $vendor_id;
}

/**
 * Register ACF fields for Vendors
 * if ACF is active
 */
function shrk_wcvendors_acf_fields() {

	if ( function_exists( 'register_field_group' ) ) {
		register_field_group(array (
			'id'     => 'acf_vendor-custom-fields',
			'title'  => 'Vendor custom fields',
			'fields' => array (
				array (
					'key'          => 'field_59c42e83f31ad',
					'label'        => 'Vendor Cover Image',
					'name'         => 'vendor_cover_image',
					'type'         => 'image',
					'save_format'  => 'id',
					'preview_size' => 'thumbnail',
					'library'      => 'all',
				),
			),
			'location' => array (
				array (
					array (
						'param'    => 'ef_user',
						'operator' => '==',
						'value'    => 'vendor',
						'order_no' => 0,
						'group_no' => 0,
					),
				),
			),
			'options' => array (
				'position'       => 'normal',
				'layout'         => 'no_box',
				'hide_on_screen' => array (
				),
			),
			'menu_order' => 0,
		));
	}
}


?>