<?php

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

add_action('template_redirect', 'prwfr_product_page_load');


/**
 * Function to display products in widget .
 *
 * @param array  $product_ids_array .
 * @param string $stock_status .
 */
function prwfr_get_products_widget($product_ids_array, $stock_status)
{
	$iterator = 0;

	foreach ($product_ids_array as $product_id) {
		if (intval($product_id) !== 0 && $iterator < 6) {
?>
			<div class="product-container-widget">
				<?php

				echo '<div class="widget-product-thumbnail"><a href="' . esc_url(get_permalink($product_id)) . '"><span class = "prwfr-image-container">' . esc_attr(get_the_post_thumbnail($product_id)) . '</span></a></div>';
				echo '<div class="prwfr-product-title"><a href="' . esc_url(get_permalink($product_id)) . '">' . esc_html(get_the_title($product_id)) . '</a></div>';

				// If the product is on sale then display sale price else regular price.
				prwfr_get_product_price($product_id);
				++$iterator;
				?>
			</div>
		<?php
		}
	}
}

/**
 * Function to display products in shortcode.
 *
 * @param array   $product_ids_array .
 * @param integer $desktop_products_limit .
 * @param integer $tab_products_limit .
 * @param integer $mobile_products_limit .
 * @param string  $feature_name .
 * @param string  $stock_status .
 */
function prwfr_display_products($product_ids_array, $desktop_products_limit, $tab_products_limit, $mobile_products_limit, $feature_name, $stock_status)
{

	$button_color = (string) get_option('prwfr_color_picker_btn');
	$row_bg_color = (string) get_option('prwfr_color_picker_background_front');
	$arrow_color  = get_option('prwfr_button_arrow_color') ? (string) get_option('prwfr_button_arrow_color') : '#fff';

	$products_array_imploded = array();
	$iterator                = 0;
	$products_array_imploded = implode(',', $product_ids_array);

	$check_all_products = explode(',', $products_array_imploded);

	// Usage.
	$product_ids = prwfr_get_products_ids_by_language();

	$product_ids_array            = array_unique(array_intersect($check_all_products, $product_ids));
	$translated_products_imploded = implode(',', $product_ids_array);

	if ('' === $row_bg_color) {
		$row_bg_color = '#e8e8e8';
	}

	if ('' === $button_color) {
		$button_color = '#000000';
	}

	if (! intval($desktop_products_limit)) {
		$desktop_products_limit = 4;
	}

	if (! intval($tab_products_limit)) {
		$tab_products_limit = 3;
	}

	if (! intval($mobile_products_limit)) {
		$mobile_products_limit = 2;
	}

	// Add it to class to identify no of columns for desktop view.
	if (4 === $desktop_products_limit) {
		$desktop = 'four';
	} elseif (5 === $desktop_products_limit) {
		$desktop = 'five';
	} elseif (6 === $desktop_products_limit) {
		$desktop = 'six';
	}

	// Add it to class to identify no of columns for tablet view.
	if (3 === $tab_products_limit) {
		$tab = 'three';
	} elseif (4 === $tab_products_limit) {
		$tab = 'four';
	}

	// Add it to class to identify no of columns for mobile view.
	if (1 === $mobile_products_limit) {
		$mobile = 'one';
	} elseif (2 === $mobile_products_limit) {
		$mobile = 'two';
	}

	if (empty($product_ids_array) || (1 === count($product_ids_array) && '' === $product_ids_array[0]) || count($product_ids_array) < $desktop_products_limit) {

		wp_enqueue_script('jquery');
		?>
		<script>
			jQuery(document).ready(function() {
				jQuery(".prwfr-<?php echo esc_attr($feature_name); ?>-front-title").hide();
			})
		</script>
	<?php
	} else {
	?>
		<script>
			jQuery(document).ready(function() {
				var productsArray = '<?php echo esc_js($translated_products_imploded); ?>';
				var featureName = '<?php echo esc_js($feature_name); ?>';
				var sliderProductsLimit = 4;

				if (jQuery(window).width() >= 1200) {
					sliderProductsLimit = <?php echo esc_js($desktop_products_limit); ?>;
				} else if (jQuery(window).width() >= 450 && jQuery(window).width() < 1200) {
					sliderProductsLimit = <?php echo esc_js($tab_products_limit); ?>;
				} else if (jQuery(window).width() < 450) {
					sliderProductsLimit = <?php echo esc_js($mobile_products_limit); ?>;
				}

				sliderProductsLimit = parseInt(sliderProductsLimit);

				jQuery.ajax({
					type: "POST",
					url: prwfr_ajax_action_obj.url,
					data: {
						action: 'prwfr_ajax_slider',
						page_reload: 1,
						products_array: productsArray,
						feature_name: featureName,
						slider_products_limit: sliderProductsLimit,
						nonce: '<?php echo esc_js(wp_create_nonce('sft-product-recommendations-for-woocommerce')); ?>',
					},
					success: function(response) {

						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-parent-front-container').attr('data-limit', sliderProductsLimit);
						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-back-button').show();
						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-next-button').show();
						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-parent-front-container').empty().html(response);

						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-parent-front-container').attr('data-page_count', '1');
						var pageNos = <?php echo esc_js(count($product_ids_array)); ?> / sliderProductsLimit;
						pageNos = Math.ceil(pageNos);
						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-parent-front-container').attr('data-pages', pageNos);
						var page_count = parseInt(jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-parent-front-container').attr('data-page_count'));
						jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-page-display').html('Page 1 of ' + parseInt(pageNos));

						if (page_count === pageNos) {
							jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-next-button').hide();
							jQuery('.prwfr-<?php echo esc_js($feature_name); ?>-back-button').hide();
						}
					}
				})

			})
		</script>

		<!-- Parent container to display rvps, rvps and related on sale, rvps related, purchase history related, back and next button, image loader. -->
		<div class="prwfr-product-list-wrap prwfr-desktop-<?php echo esc_html($desktop); ?>-columns prwfr-tab-<?php echo esc_html($tab); ?>-columns prwfr-mobile-<?php echo esc_html($mobile); ?>-columns">

			<!-- div for back button.  -->
			<div class="prwfr-parent-back-button">
				<button class="prwfr-back-button prwfr-<?php echo esc_html($feature_name); ?>-back-button" type="button" value="<" style="background:<?php echo esc_html($button_color); ?> !important; border-radius: 4px; overflow: hidden;">
					<svg style="fill:<?php echo esc_html($arrow_color); ?> !important; width:20px; height:100%;" fill="<?php echo esc_html($arrow_color); ?>" viewBox="-6.5 0 32 32" version="1.1" xmlns="http://www.w3.org/2000/svg" transform="rotate(90)matrix(1, 0, 0, 1, 0, 0)">
						<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
						<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
						<g id="SVGRepo_iconCarrier">
							<title>dropdown</title>
							<path d="M18.813 11.406l-7.906 9.906c-0.75 0.906-1.906 0.906-2.625 0l-7.906-9.906c-0.75-0.938-0.375-1.656 0.781-1.656h16.875c1.188 0 1.531 0.719 0.781 1.656z"></path>
						</g>
					</svg>
				</button>
			</div>

			<!-- Parent container to display product image, title, price, rating -->
			<div class="prwfr-parent-front-product-container prwfr-<?php echo esc_html($feature_name); ?>-parent-front-container" data-limit="4" data-limit-desktop="<?php echo esc_html($desktop_products_limit); ?>" data-limit-tablet="<?php echo esc_html($tab_products_limit); ?>" data-limit-mobile="<?php echo esc_html($mobile_products_limit); ?>" data-array="<?php print_r($translated_products_imploded); ?>" data-name="<?php echo esc_html($feature_name); ?>" data-page_count="1" data-page_count_back="0" data-pages="1" style="background:<?php echo esc_html($row_bg_color); ?> !important;">

				<?php

				foreach ($product_ids_array as $product_id) {
					if (intval($product_id) !== 0 && $iterator < $desktop_products_limit) {

				?>
						<div class="prwfr-product-container">
							<a href="<?php echo esc_url(plugins_url('../../assets/images/shimmer-loader5.gif', __FILE__)); ?>">
								<img src="<?php echo esc_url(plugins_url('../../assets/images/shimmer-loader5.gif', __FILE__)); ?>" style="width: 100%;">
							</a>
							<a href="<?php echo esc_url(plugins_url('../../assets/images/shimmer-loader-2nd-half.gif', __FILE__)); ?>">
								<img src="<?php echo esc_url(plugins_url('../../assets/images/shimmer-loader-2nd-half.gif', __FILE__)); ?>" style="width: 100%;">
							</a>
						</div>
				<?php
						++$iterator;
					}
				}

				?>
			</div>

			<div class="prwfr-parent-next-button">
				<button class="prwfr-next-button prwfr-<?php echo esc_html($feature_name); ?>-next-button" type="button" value=">" style="background:<?php echo esc_html($button_color); ?> !important; border-radius: 4px; overflow: hidden;">
					<svg style="fill:<?php echo esc_html($arrow_color); ?> !important; width:20px; height:100%;" fill="<?php echo esc_html($arrow_color); ?>" viewBox="-6.5 0 32 32" version="1.1" xmlns="http://www.w3.org/2000/svg" transform="rotate(270)">
						<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
						<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
						<g id="SVGRepo_iconCarrier">
							<title>dropdown</title>
							<path d="M18.813 11.406l-7.906 9.906c-0.75 0.906-1.906 0.906-2.625 0l-7.906-9.906c-0.75-0.938-0.375-1.656 0.781-1.656h16.875c1.188 0 1.531 0.719 0.781 1.656z"></path>
						</g>
					</svg>
				</button>
			</div>

		</div>
		<?php
	}
}

/**
 * This function stores viewed products in user_meta for logged in users or in cookie for non logged in users.
 */
function prwfr_product_page_load()
{

	global $wpdb;
	$user_id               = get_current_user_id();
	$user_browsing_history = get_user_meta($user_id, 'browsing_history_on_off_' . $user_id, true); // Get 1 if browsing history is on or 0.
	$current_timestamp     = time();
	$current_date          = gmdate('Y-m-d');
	$table_name            = $wpdb->prefix . 'sft_user_product_interactions';

	// If user is logged in then viewed product's id will be stored in user meta.
	if (is_user_logged_in() && intval($user_browsing_history)) {

		$products_array = array();
		if (is_product() && is_singular() && ! is_page()) {

			$post_id = get_post()->ID;

			// Prepare and execute the query safely using wpdb->prepare for values only.
			$result = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT product_details FROM `$table_name` WHERE interaction_type=%s AND DATE(interaction_timestamp)=%s AND customer_id=%d",
					'recently-viewed-products',
					$current_date,
					$user_id
				)
			);

			if (! empty($result)) {
				$viewed_products = json_decode($result[0]->product_details, true);
				$viewed_products = array_merge($viewed_products['product_ids'], (array) $post_id);
				$product_data    = wp_json_encode(array('product_ids' => $viewed_products));

				// Execute the query using $wpdb->query().
				$wpdb->query(
					$wpdb->prepare(
						"UPDATE $table_name SET product_details = %s, interaction_timestamp = %s WHERE DATE(interaction_timestamp) = %s AND interaction_type = %s AND customer_id = %d",
						$product_data,
						gmdate('Y-m-d H:i:s', $current_timestamp),
						$current_date,
						'recently-viewed-products',
						$user_id
					)
				);
			} else {
				$product_data = wp_json_encode(array('product_ids' => array($post_id)));
				$wpdb->insert(
					$table_name,
					array(
						'customer_id'           => $user_id,
						'interaction_timestamp' => gmdate('Y-m-d H:i:s', $current_timestamp),
						'interaction_type'      => 'recently-viewed-products',
						'product_details'       => $product_data,
						'order_ids'             => '',
						'total_sales'           => 0,
					)
				);
			}
		}
	} else {

		$products_array = array();
		// If product is viewed then for non logged in user, ids will be stored in  cookie.
		if (is_product() && is_singular() && ! is_page()) {

			$products_array = array();
			// If product is viewed then for non logged in user, ids will be stored in  cookie.
			if (is_product() && is_singular() && ! is_page()) {

				if (isset($_SERVER['SERVER_NAME'])) {

					// Sanitize the server name to create a safe cookie name.
					$recently_viewed_products_cookie_name = sanitize_key($_SERVER['SERVER_NAME']);
					$recently_viewed_products_cookie_name = str_replace('.', '_', $recently_viewed_products_cookie_name);

					// Retrieve the option value using the sanitized cookie name.
					$cookie_exist = get_option('prwfr_' . $recently_viewed_products_cookie_name);
				}

				if (! $cookie_exist) {
					$current_date           = gmdate('Y-m-d', strtotime('now')); // Current date.
					$date_object            = date_create($current_date);
					$date_array             = (array) $date_object;
					$formatted_current_date = substr($date_array['date'], 0, 10);
					update_option('prwfr_' . $recently_viewed_products_cookie_name, $recently_viewed_products_cookie_name . '_' . $formatted_current_date);
				}

				$post_data                            = get_post();
				$post_id                              = $post_data->ID;
				$recently_viewed_products_cookie_name = get_option('prwfr_' . $recently_viewed_products_cookie_name);

				if (isset($_COOKIE[$recently_viewed_products_cookie_name])) { // Stores cookie for 30days.
					$products_array = sanitize_text_field(wp_unslash($_COOKIE[$recently_viewed_products_cookie_name]));

					$products_array = explode(',', $products_array);
					array_push($products_array, $post_id);

					setcookie($recently_viewed_products_cookie_name, implode(',', $products_array), time() + (30 * 24 * 60 * 60), '/');
				} else {
					setcookie($recently_viewed_products_cookie_name, strval($post_id), time() + (30 * 24 * 60 * 60), '/');
				}
			} else {
				return;
			}
		}
	}
}



/**
 * Function to display products in back shortcode
 *
 * @param array  $all_product_ids .
 * @param string $stock_status .
 */
function prwfr_get_back_shortcode_products($all_product_ids, $stock_status)
{
	$iterator           = 0;
	$image_size         = get_option('prwfr_product_image_size');
	$stock_status_check = 0;

	// Usage.
	$product_ids = prwfr_get_products_ids_by_language();

	$all_product_ids = array_unique(array_intersect($all_product_ids, $product_ids));

	// Display all products  in a page -> back shortcode.
	foreach ($all_product_ids as $product_id) {
		if (intval($product_id) !== 0) {
			$product = wc_get_product($product_id);
		?>
			<div class="prwfr-back-product-div">
				<?php

				$image_id = $product->get_image_id();
				// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce core filter.
				$image_size = apply_filters('single_product_archive_thumbnail_size', 'woocommerce_thumbnail');

				// Use WooCommerce function to get the image HTML
				$image_html = $image_id ? wp_get_attachment_image($image_id, $image_size) : wc_placeholder_img($image_size);

				echo '<div style="width: fit-content; margin: auto;"><a href="' . esc_url(get_permalink($product_id)) . '">' . wp_kses_post($image_html) . '</a></div>';

				echo '<div class="prwfr-product-title"><a href="' . esc_url(get_permalink($product_id)) . '">' . esc_attr(get_the_title($product_id)) . '</a></div>';

				// Check if product is on sale or not, to display price.
				prwfr_get_product_price($product);

				++$iterator;
				?>
			</div>
		<?php
		}
	}
}

/**
 * Gives array of product ids specific to particular language.
 */
function prwfr_get_products_ids_by_language()
{
	$args = array(
		'post_type'        => 'product',
		'posts_per_page'   => -1,
		'post_status'      => 'publish',
		'fields'           => 'ids',
		'suppress_filters' => false, // Important for WPML to filter by language.
	);

	$products_ids = get_posts($args);

	return $products_ids;
}

/**
 * Function to add reorder button on order detail page, this function is called.
 *
 * @param mixed $actions .
 * @return $actions
 */
function prwfr_reorder($actions, $order)
{
	$show_reorder = get_option('prwfr_display_reorder');
	if (intval($show_reorder)) {
		$actions['reorder'] = array(
			'url' => add_query_arg(
				array(
					'prwfr_order_id' => $order->get_id(),
				),
				wc_get_cart_url()
			),
			'name' => __('Reorder', 'sft-product-recommendations-for-woocommerce'),
		);
	}

	return $actions;
}

/**
 * Return Recommnedation for products array set by Ai Recommenation.
 *
 * @param [type] $product_ids
 * @return array
 */
function prwfr_get_ai_recommendation($product_ids, $meta)
{

	$product_recommendations = array();

	$product_ids = array_unique($product_ids);

	foreach ($product_ids as $product_id) {
		// Get AI-generated related product recommendations
		$pro_rel = get_post_meta($product_id, $meta, true);

		if (! empty($pro_rel) && is_array($pro_rel)) {
			$product_recommendations = array_merge($product_recommendations, $pro_rel);
		}
	}
	$product_recommendations = array_unique($product_recommendations);

	return $product_recommendations;
}


/**
 * Return Recommnedation for products array by its category and tag.
 *
 * @param [type] $product_ids
 * @return array
 */
function prwfr_get_category_tag_recommendation($product_ids)
{

	$product_recommendations = array();
	$slugs_purchase          = array();

	// $slugs_purchase stores category slugs of purchased products
	foreach ($product_ids as $id) {

		$product   = wc_get_product($id);
		$post_type = get_post_type($id);

		if ('product_variation' == $post_type) {
			$id = $product->get_parent_id();
		}
		$product_cats = wp_get_post_terms($id, 'product_cat');

		foreach ($product_cats as $product_cat) {
			array_push($slugs_purchase, $product_cat->slug);
		}
	}

	$slugs_purchase = array_unique($slugs_purchase);
	$slugs_purchase = array_diff($slugs_purchase, array('uncategorized'));

	// Retrieves products containing categories from $slugs_purchase.
	$args1  = array(
		'posts_per_page' => -1,
		'post_type'      => 'product',
		'tax_query'      => array(
			array(
				'taxonomy' => 'product_cat',
				'field'    => 'slug',
				'terms'    => $slugs_purchase,
			),
		),
	);
	$query1 = new WP_Query($args1);

	foreach ($query1->posts as $post) {
		array_push($product_recommendations, $post->ID);
	}

	return $product_recommendations;
}


/**
 *  Return Recommnedation for products array by Ai Recommendation is set otherwise category and tag.
 *
 * @param [type] $product_ids
 * @param [type] $meta
 * @return array
 */
function prwfr_get_combine_recommendation($product_ids, $meta)
{

	// Sort Products with the ai recommendations and not with ai recommendation.

	$product_with_ai_recommendation = array();
	$product_recommendations        = array();

	foreach ($product_ids as $product_id) {
		// Get AI-generated related product recommendations
		$pro_rel = get_post_meta($product_id, $meta, true);

		if (! empty($pro_rel) && is_array($pro_rel)) {
			$product_recommendations          = array_merge($product_recommendations, $pro_rel);
			$product_with_ai_recommendation[] = $product_id;
		}
	}

	$product_without_ai_recomm = array_diff($product_ids, $product_with_ai_recommendation);
	$cat_tag_recommendation    = prwfr_get_category_tag_recommendation($product_without_ai_recomm);

	// Combine recommendationd for ai and catgeory/tags.
	$product_recommendations = array_merge($product_recommendations, $cat_tag_recommendation);

	$product_recommendations = array_unique($product_recommendations);
	return $product_recommendations;
}



/**
 * Creating pages for back shortcode.
 */
function prwfr_view_pages_back()
{

	if (! current_user_can('activate_plugins')) {
		return;
	}

	// rvps shortcode page - prwfr_rvps_page_id.
	$rvps_page_title = get_option('prwfr_rvps_title');

	if (! $rvps_page_title) {
		$rvps_page_title = 'Recently Viewed Products'; // If title is not provided.
	}

	$pro_shortcode      = '[prprow_recently_viewed_products_back]';
	$shortcode      = '[prwfr_recently_viewed_products_back]';
	$option_page_id = 'prwfr_rvps_see_more_option';
	$page_id        = 'prwfr_rvps_page_id';
	prwfr_new_page_template_back($option_page_id, $rvps_page_title, $shortcode, $page_id, $pro_shortcode);

	// rvps on sale shortcode page - prwfr_rvps_onsale_page_id.
	$rvps_onsale_page_title = get_option('prwfr_rvps_onsale_title');

	if (! $rvps_onsale_page_title) {
		$rvps_onsale_page_title = 'Trending Deals';
	}

	$pro_shortcode      = '[prprow_onsale_recently_viewed_products_back]';
	$shortcode      = '[prwfr_onsale_recently_viewed_products_back]';
	$option_page_id = 'prwfr_rvps_onsale_see_more_option';
	$page_id        = 'prwfr_rvps_onsale_page_id';
	prwfr_new_page_template_back($option_page_id, $rvps_onsale_page_title, $shortcode, $page_id, $pro_shortcode);

	// viewed related shortcode page - prwfr_viewed_related_page_id.
	$view_related_page_title = get_option('prwfr_viewed_related_title');

	if (! $view_related_page_title) {
		$view_related_page_title = 'Related to items you\'ve viewed';
	}

	$pro_shortcode      = '[prprow_related_recently_viewed_products_back]';
	$shortcode      = '[prwfr_related_recently_viewed_products_back]';
	$option_page_id = 'prwfr_viewed_related_see_more_option';
	$page_id        = 'prwfr_viewed_related_page_id';
	prwfr_new_page_template_back($option_page_id, $view_related_page_title, $shortcode, $page_id, $pro_shortcode);

	// ---------------------------  Product Discovery Features ---------------
	// '[prwfr_all_onsale_products_front]'
	// '[prwfr_best_selling_front]'
	// '[prwfr_featured_products_front]'
	// '[prwfr_new_arrivals_front]'

	// -----------------  All On sale ---------------------
	$all_onsale_products_front_title = get_option('prwfr_all_onsale_products_back_title');

	if (! $all_onsale_products_front_title) {
		$all_onsale_products_front_title = 'Blockbuster deals';
	}
	$pro_shortcode      = '[prprow_all_onsale_products_back]';
	$shortcode      = '[prwfr_all_onsale_products_back]';
	$option_page_id = 'prwfr_all_onsale_products_see_more_option';
	$page_id        = 'prwfr_all_onsale_products_back_page_id';
	prwfr_new_page_template_back($option_page_id, $all_onsale_products_front_title, $shortcode, $page_id, $pro_shortcode);

	// ----------------------- Best Selling ---------------
	$best_selling_front_title = get_option('prwfr_best_selling_back_title');

	if (! $best_selling_front_title) {
		$best_selling_front_title = 'Best Selling Products';
	}
	$pro_shortcode      = '[prprow_best_selling_back]';
	$shortcode      = '[prwfr_best_selling_back]';
	$option_page_id = 'prwfr_best_selling_see_more_option';
	$page_id        = 'prwfr_best_selling_back_page_id';
	prwfr_new_page_template_back($option_page_id, $best_selling_front_title, $shortcode, $page_id, $pro_shortcode);

	// ------------------------ Feature Products -----------------
	$featured_products_front_title = get_option('prwfr_featured_products_back_title');

	if (! $featured_products_front_title) {
		$featured_products_front_title = 'Featured Products';
	}
	$pro_shortcode      = '[prprow_featured_products_back]';
	$shortcode      = '[prwfr_featured_products_back]';
	$option_page_id = 'prwfr_featured_see_more_option';
	$page_id        = 'prwfr_featured_products_back_page_id';
	prwfr_new_page_template_back($option_page_id, $featured_products_front_title, $shortcode, $page_id, $pro_shortcode);

	// --------------------- New Arrival Page -----------
	$view_related_page_title = get_option('prwfr_new_arrivals_back_title');

	if (! $view_related_page_title) {
		$view_related_page_title = 'New Arrivals';
	}
	$pro_shortcode      = '[prprow_new_arrivals_back]';
	$shortcode      = '[prwfr_new_arrivals_back]';
	$option_page_id = 'prwfr_new_arrivals_see_more_option';
	$page_id        = 'prwfr_new_arrivals_back_page_id';
	prwfr_new_page_template_back($option_page_id, $view_related_page_title, $shortcode, $page_id, $pro_shortcode);
}

/**
 * Callback for Creating pages for back shortcode.
 *
 * @param string $option_page_id .
 * @param string $title .
 * @param string $shortcode .
 * @param string $page_id .
 */
function prwfr_new_page_template_back($option_page_id, $title, $shortcode, $page_id, $pro_shortcode)
{

	$iterator = 0;
	$new_page = 'default';

	if ('default' === $new_page || '' === $new_page || ! $new_page) {

		$args  = array(
			'title'     => $title,
			'post_type' => 'page',
			'post_status' => 'publish'
		);
		$query = new WP_Query($args);

		// If page already exists.
		if ($query->posts[0]->ID) {
			foreach ($query->posts as $post) {
				$page    = get_post($post->ID);
				$content = $page->post_content;

				if ($content === $shortcode) {
					$iterator = 1;
					update_option($page_id, $post->ID);
				} else if ($content === $pro_shortcode) { // To remove the duplicacy of the pages

					// Changes the post content and updates the page
					$page->post_content = $shortcode;
					wp_update_post($page);
					$iterator = 1;
					update_option($page_id, $post->ID);
				}
			}
		}

		// if page with shortcode or page does not exists.
		if (0 === $iterator) {
			$current_user = wp_get_current_user();

			// create post object.
			$page = array(
				'post_title'   => $title,
				'post_content' => $shortcode,
				'post_status'  => 'publish',
				'post_author'  => $current_user->ID,
				'post_type'    => 'page',
			);

			// insert the post into the database.
			wp_insert_post($page);
			$args  = array(
				'title'     => $title,
				'post_type' => 'page',
			);
			$query = new WP_Query($args);
			update_option($page_id, $query->posts[0]->ID);
		}
	} else {
		$page = get_option($option_page_id);
		update_option($page_id, $page);
	}
}

/**
 * Function for manage history on rvps all products page, , this function is called in sales-booster.php
 */
function prwfr_manage_history_content()
{
	$show_browsing_history = get_option('prwfr_manage_history_access');
	if (intval($show_browsing_history)) {
		echo '<h3 class="prwfr-manage-account-page-title">' . esc_html__('Manage Browsing history', 'sft-product-recommendations-for-woocommerce') . '</h3>';
		$user_id               = get_current_user_id();
		$user_browsing_history = get_user_meta($user_id, 'browsing_history_on_off_' . $user_id, true);
		?>

		<!-- Manage history is available on account details page and page containing all recently viewed products. -->
		<div class="prwfr-manage-account-page">
			<div style="display: flex; gap: 20px;"><?php esc_html_e('Turn On or Off Browsing History', 'sft-product-recommendations-for-woocommerce'); ?>
				<label class="switch">
					<input class="prwfr-browsing-history-switch-display" type="checkbox" name="<?php echo esc_html('browsing_history_on_off_') . esc_html($user_id); ?>" value="1" <?php echo checked('1', esc_html($user_browsing_history), false); ?> style="padding-right: 12px;">
					<span class="slider round" style="background: #1770bbe6;"></span>
				</label>
			</div>
		</div>
	<?php
	}
}

/**
 * Retrieve all the pages by specific language .
 *
 * @param int $original_id .
 */
function prwfr_translate_woocommerce_title($original_id)
{

	global $wpdb;

	$language_code = substr(get_locale(), 0, 2);

	$table_name = esc_sql($wpdb->prefix . 'icl_translations');

	if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name)) === $table_name) {
		// Table exists, proceed with the query.

		$translated_id = $wpdb->get_row(
			$wpdb->prepare(
				"SELECT t.element_id
				FROM {$wpdb->prefix}icl_translations t
				JOIN {$wpdb->prefix}icl_translations tr ON t.trid = tr.trid
				WHERE tr.element_id = %d
				AND t.language_code = %s
				AND t.element_type = %s",
				$original_id,
				$language_code,
				'post_page'
			)
		);
	} else {
		// Table does not exist, handle accordingly.
		$translated_id = $original_id;
	}

	return $translated_id ? $translated_id : $original_id;
}

/**
 * Send email to users based on their notification type.
 *
 * @param array  $user_mails Array containing user emails and product details.
 * @param string $email_type Type of email notification to be sent.
 * @param int    $similar_products_pageid Page ID for displaying similar products.
 * @param string $email_content_text Content text for the email.
 * @param string $email_header_content Header content for the email.
 */
function prwfr_send_mails($user_mails, $email_type, $similar_products_pageid, $email_content_text, $email_header_content)
{

	global $wpdb;
	$custom_upload_dir = wp_upload_dir()['baseurl'] . '/prwfr-email-notification-logo';
	$logo_img          = get_option('prwfr_email_logo') ? str_replace(' ', '%20', get_option('prwfr_email_logo')) : '';
	$current_timestamp = time();
	$iterator          = 1;
	$email_no_thanks   = get_option('email_gdpr_no_thanks') ? json_decode(get_option('email_gdpr_no_thanks'), true) : array();
	$table_name        = $wpdb->prefix . 'sft_user_product_interactions';
	foreach ($user_mails as $email => $value) {

		if ($iterator % 10 == 0) {
			sleep(10);
		}

		if (! empty($value) && ! in_array($email, $email_no_thanks)) {

			$temp_products_array = array_unique($value);
			$products_array      = count($temp_products_array) > 5 ? array_slice($temp_products_array, 0, 5) : $temp_products_array;

			// Reindex the array to ensure sequential keys.
			$products_array = array_values($products_array);

			$product_data = wp_json_encode(array('product_ids' => $products_array));

			$user = get_user_by('email', $email);

			$wpdb->insert(
				$table_name,
				array(
					'customer_id'           => $user->ID,
					'interaction_timestamp' => gmdate('Y-m-d H:i:s', $current_timestamp),
					'interaction_type'      => 'email-sent-' . $email_type,
					'product_details'       => $product_data,
					'order_ids'             => '',
					'total_sales'           => '',
				)
			);

			$email_header_content = get_option('prwfr_' . $email_type . '_email_header_content') ? get_option('prwfr_' . $email_type . '_email_header_content') : $email_header_content;
			$options              = get_option('prwfr_' . $email_type . '_email_content');
			$actual_string        = wpautop($options['prwfr_' . $email_type . '_email_content_text']);

			$user_info = get_userdata($user->ID);
			$username  = $user_info->user_login;

			$first_name = $user->user_firstname;
			$last_name  = $user->user_lastname;

			$value   = '"' . get_the_title($products_array[0]) . '" and more...';
			$subject = apply_filters('prwfr_' . $email_type . '_email_subject', $value);

			$encoded_string = $subject;

			$decoded_string_subject = html_entity_decode($encoded_string, ENT_HTML5, 'UTF-8');
			$replacements           = array(
				'{User_name}'  => $username,
				'{First_name}' => $first_name,
				'{Last_name}'  => $last_name,
			);

			if ($actual_string != '') {
				// Perform replacements.
				foreach ($replacements as $old => $new) {
					$actual_string = str_replace($old, $new, $actual_string);
				}
			}
			// // Perform the replacement
			$content = $actual_string != '' ? $actual_string : 'Hello ' . $first_name . ',<br><br>' . $email_content_text;

			$email_header = '<p style="
			color: white;
			font-size: 25px;
			margin: 0;
			line-height: 1.2;
			">' . $email_header_content . '</p>';

			// // echo $custom_upload_dir . '/' . $logo_img;
			// $email_logo = '<a href="#" style="display: inline-block; margin: 0; padding: 20px 10px;"><img src="' . $custom_upload_dir . '/' . $logo_img . '" alt="" width="" height="" style="display: block; height:120px; border: 0px;"></a>';

			$email_logo = '';
			if ($logo_img != '') {
				$email_logo = '<tr><td align="center" valign="top" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px;" class="mobile-center"><a href="#" style="display: inline-block; margin: 0; padding: 0px 10px 20px 10px;"><img src="' . $custom_upload_dir . '/' . $logo_img . '" alt="" width="" height="" style="display: block; height:110px; border: 0px;"></a></td></tr>';
			}

			$similar_products_section = '';
			if (intval(get_option('prwfr_display_similar_products_link')) == 1) {
				$similar_products_section = '<tr>
					<td align="left" style="background-color: #FFF; padding: 20px 15px;" bgcolor="#FFF">
					<!--[if (gte mso 9)|(IE)]>
					<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
					<tr>
					<td align="center" valign="top" width="600">
					<![endif]-->  
					<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:600px;">
						<tr>
							<td align="left" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px;">
							<img src="' . esc_url(plugins_url('../../assets/images/arrow-left.png', __FILE__)) . '" alt="" width="10" height="12" style="width:15px; height: 12px; block; max-width: 100%; height: auto; border: 0px; display: inline-block; vertical-align: sub;"> 

								<a href="' . get_permalink($similar_products_pageid) . '" style="display: inline-block; margin: 0; padding: 0; font-size: 18px; font-weight: 600; color:#2282ca;">
									See similar products
								</a>
							</td>
						</tr>
					</table>    
					<!--[if (gte mso 9)|(IE)]>
					</td>
					</tr>
					</table>
					<![endif]-->
					</td>
				</tr>';
			}

			wp_mail($email, $decoded_string_subject, prwfr_get_email_content($content, $similar_products_section, $products_array, $email_logo, $email_header));
		}

		++$iterator;
	}
}

/**
 * Schedule mails for Recently Viewed Products (RVP).
 * This function retrieves users' interactions with products from the database
 * and sends reminder emails about recently viewed products.
 */
function prwfr_schedule_mails_rvp()
{

	global $wpdb;
	$user_mails_rvp = array();
	$current_date   = gmdate('Y-m-d');
	$table_name     = $wpdb->prefix . 'sft_user_product_interactions';

	// Create a DateTime object for UTC.
	$utc_time = new DateTime('now', new DateTimeZone('UTC'));

	if (get_option('prwfr_rvp_email_recurrence_interval') === 'hours') {

		$n_hours_ago = get_option('prwfr_rvp_email_hours_interval') ? intval(get_option('prwfr_rvp_email_hours_interval')) : 8;

		$mail_already_sent_users = array();
		$existing_row            = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT customer_id, product_details, interaction_timestamp FROM {$table_name} WHERE interaction_type = %s AND DATE(interaction_timestamp) = %s",
				'email-sent-rvp',
				$current_date
			)
		);
		foreach ($existing_row as $row) {
			// date-time string.
			$date_time_string = $row->interaction_timestamp;

			// Create a DateTime object.
			$current_date_time = new DateTime($date_time_string);
			// Calculate the difference between the two times.
			$interval = $current_date_time->diff($utc_time);

			// Display the difference in hours.
			if (intval($interval->format('%h')) < $n_hours_ago) {
				array_push($mail_already_sent_users, $row->customer_id);
			}
		}

		// Construct the SQL query using $wpdb->prepare.
		$result = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT customer_id, product_details 
				FROM {$table_name} 
				WHERE interaction_type = %s 
				AND DATE(interaction_timestamp) = %s",
				'recently-viewed-products',
				$current_date,
			)
		);

		// // Example usage of the results
		foreach ($result as $row) {
			if (! in_array($row->customer_id, $mail_already_sent_users)) {

				$user_info  = get_userdata($row->customer_id);
				$user_email = $user_info->user_email;

				$email_sent = $wpdb->get_row(
					$wpdb->prepare(
						"SELECT product_details FROM {$table_name}
					WHERE interaction_type = %s AND DATE(interaction_timestamp) = %s AND customer_id = %d",
						'email-sent-rvp',
						$current_date,
						$row->customer_id,
					)
				);

				$products_sent = json_decode($email_sent->product_details, true);
				$products_sent = $products_sent['product_ids'];

				$viewed_products = json_decode($row->product_details, true);
				$viewed_products = $viewed_products['product_ids'];

				if (! empty($products_sent)) {
					$products_to_send = array_values(array_diff($viewed_products, $products_sent));
				} else {
					$products_to_send = array_values($viewed_products);
				}

				$user_mails_rvp[$user_email] = array_unique($products_to_send);
				usort($user_mails_rvp[$user_email], 'prwfr_compare_product_prices');
			}
		}
	}

	if (! empty($user_mails_rvp)) {

		$similar_products_pageid = get_option('prwfr_rvps_page_id');

		$email_content_text = 'We noticed you\'ve been checking out some great items on our site and wanted to give you a quick recap to help you make your decision. Here\'s a reminder of what caught your eye:';

		$email_header_content = 'Still Thinking About Them? It\'s Time to Make Those Recently Viewed Items Yours!';

		prwfr_send_mails($user_mails_rvp, 'rvp', $similar_products_pageid, $email_content_text, $email_header_content);
	}
}

/**
 * Generate email content with product grid and similar products link.
 *
 * @param string $content               The main content of the email.
 * @param int    $similar_products_section ID of the page with similar products.
 * @param array  $products              Array of product IDs.
 * @param string $email_logo            HTML for the email logo.
 * @param string $email_header          Header text for the email.
 * @return string                       HTML content for the email.
 */
function prwfr_get_email_content($content, $similar_products_section, $products, $email_logo, $email_header)
{

	$email_body = '<!DOCTYPE html>
	<html>
	<head>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<style type="text/css">
	/* CLIENT-SPECIFIC STYLES */
	body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
	table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
	img { -ms-interpolation-mode: bicubic; }
	
	/* RESET STYLES */
	img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; }
	table { border-collapse: collapse !important; }
	body { height: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important; }
	
	/* iOS BLUE LINKS */
	a[x-apple-data-detectors] {
		color: inherit !important;
		text-decoration: none !important;
		font-size: inherit !important;
		font-family: inherit !important;
		font-weight: inherit !important;
		line-height: inherit !important;
	}
	
	/* MEDIA QUERIES */
	@media screen and (max-width: 480px) {
		.mobile-hide {
			display: none !important;
		}
		.mobile-center {
			text-align: center !important;
			padding: 0 16px;
		}
	}
	
	/* ANDROID CENTER FIX */
	div[style*="margin: 16px 0;"] { margin: 0 !important; }
	</style>
	<body style="margin: 0 !important; padding: 0 !important; background-color: #eeeeee;" bgcolor="#eeeeee">
	
	
	<table border="0" cellpadding="0" cellspacing="0" width="100%">
		<tr>
			<td align="center" style="background-color: #eeeeee;" bgcolor="#eeeeee">
			<!--[if (gte mso 9)|(IE)]>
			<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
			<tr>
			<td align="center" valign="top" width="600">
			<![endif]-->
			<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:600px; margin-top: 50px; margin-bottom: 50px;">
				' . $email_logo . '
				<tr>
					<td align="center" height="100%" valign="top" width="100%" style="font-size:0; padding: 35px; background:' . get_option('prwfr_email_template_header_bg_color') . ';" bgcolor="' . get_option('prwfr_email_template_header_bg_color') . '">
						<!--[if (gte mso 9)|(IE)]>
						<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
						<tr>
						<td align="center" valign="top" width="600">
						<![endif]-->
						<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:660px;">
		
							<tr>
								<td align="left" valign="top" style="font-size:0;">
									<!--[if (gte mso 9)|(IE)]>
									<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
									<tr>
									<td align="left" valign="top" width="300">
									<![endif]-->
									<div style="display:inline-block; vertical-align:top; width:100%;">
		
										<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%">
											<tr>
												<td align="left" valign="top" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px;" class="mobile-center">
													' . $email_header . '
												</td>
											</tr>
										</table>
									</div>
									<!--[if (gte mso 9)|(IE)]>
									</td>
									</tr>
									</table>
									<![endif]-->
								</td>
							</tr>
		
					 
						</table>
						<!--[if (gte mso 9)|(IE)]>
						</td>
						</tr>
						</table>
						<![endif]-->
						</td>
					
				</tr>
				<tr>
					<td align="center" style="padding: 15px 20px; background-color: #ffffff;" bgcolor="#ffffff">
					<!--[if (gte mso 9)|(IE)]>
					<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
					<tr>
					<td align="center" valign="top" width="600">
					<![endif]-->
					<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:600px;">
						<!-- <tr>
							<td align="left" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding-top: 25px;">
								<h2 style="font-size: 30px; font-weight: 800; line-height: 36px; color: #333333; margin: 0;">
									Thank You For Your Order!
								</h2>
							</td>
						</tr> -->
						<tr>
							<td align="left" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding-top: 10px;">
								<p style="font-size: 16px; font-weight: 400; line-height: 24px; color: #777777;">
								' . $content . '
								</p>
							</td>
						</tr>
					 
				   
					</table>
					<!--[if (gte mso 9)|(IE)]>
					</td>
					</tr>
					</table>
					<![endif]-->
					</td>
				</tr>
				<!-- prdoucts grid -->
				 <tr>
					<td align="center" height="100%" valign="top" width="100%" style="background-color: #ffffff;" bgcolor="#ffffff">
					<!--[if (gte mso 9)|(IE)]>
					<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
					<tr>
					<td align="center" valign="top" width="600">
					<![endif]-->
					<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:660px;">
						
						' . prwfr_get_products_grid($products) . '
				  
					</table>
					<!--[if (gte mso 9)|(IE)]>
					</td>
					</tr>
					</table>
					<![endif]-->
					</td>
				</tr>
				<!-- similar products link -->
				' . $similar_products_section . '                        
			</table>
			<!--[if (gte mso 9)|(IE)]>
			</td>
			</tr>
			</table>
			<![endif]-->
			</td>
		</tr>
	</table>
		
	</body>
	</html>';

	return $email_body;
}

/**
 * Generate HTML grid for displaying products.
 *
 * @param array $products Array of product IDs.
 * @return string HTML string representing the product grid.
 */
function prwfr_get_products_grid($products)
{
	$temp                = '';
	$learnmore_btn_color = get_option('prwfr_learn_more_btn_color') ? get_option('prwfr_learn_more_btn_color') : '#0473aa';
	foreach ($products as $id) {
		$product      = wc_get_product(intval($id));
		$actual_price = $product->is_type('variable') ? $product->get_variation_regular_price() : $product->get_regular_price();
		$sale_price   = $product->is_type('variable') ? $product->get_variation_sale_price() : $product->get_sale_price();

		if (strlen(get_the_title($id)) > 75) {
			$product_title  = substr(get_the_title($id), 0, 75);
			$product_title .= '...';
		} else {
			$product_title = get_the_title($id);
		}
		$savings = $sale_price ? intval($actual_price) - intval($sale_price) : 0;
		// $image_id = $product->get_image_id();
		// $image_url = wp_get_attachment_url($image_id); // Get the image URL

		$image_id = $product->get_image_id();
		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce core filter.
		$image_size = apply_filters('single_product_archive_thumbnail_size', 'woocommerce_thumbnail');

		// Use WooCommerce function to get the image URL of the specified size
		$image_url = $image_id ? wp_get_attachment_image_src($image_id, $image_size)[0] : wc_placeholder_img_src($image_size);

		// If you still need the image HTML
		$image_html = $image_id ? wp_get_attachment_image($image_id, $image_size) : wc_placeholder_img($image_size);

		$temp .= '<tr>
		<td align="center" valign="top" style="font-size:0;border: 1px solid #f5f5f5; background: #fdfdfd;">
			<!--[if (gte mso 9)|(IE)]>
			<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
			<tr>
			<td align="left" valign="top" width="200">
			<![endif]-->
			<div style="display:inline-block; max-width:35%; vertical-align:middle; width:100%;">

				<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:300px;">
					<tr>
						<td align="center" valign="top" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding: 15px 0px;">
							<img src="' . $image_url . '" width="100%" height="" style="display: block; max-width: 100%; height: auto; border: 0px;"/>
						</td>
					</tr>
				</table>
			</div>
			<!--[if (gte mso 9)|(IE)]>
			</td>
			<td align="left" valign="top" width="400">
			<![endif]-->
			<div style="display:inline-block; max-width:60%; vertical-align:middle; width:100%;">
				<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:100%;">
					<tr>
						<td align="left" valign="top" style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding: 0 0 0 15px;" >
							<p style="font-weight: 400;"><a href="' . get_permalink($id) . '" target="_blank" style="font-size:14px; text-decoration: underline;">' . $product_title . '</a></p>' . prwfr_get_product_prices($actual_price, $sale_price, $savings) . '<p><a href="' . get_permalink($id) . '" style="display: inline-block; font-size: 13px;box-sizing: border-box;  padding: 0 19px;line-height: 32px;background: ' . $learnmore_btn_color . '; color: #FFF; text-decoration: none;">Learn More</a></p>
						</td>
					</tr>
				</table>
			</div>
			<!--[if (gte mso 9)|(IE)]>
			</td>
			</tr>
			</table>
			<![endif]-->
		</td>
	</tr>';
	}

	return $temp;
}

/**
 * Generate HTML displaying product prices including regular price, sale price, and savings.
 *
 * @param float      $regular_price Regular price of the product.
 * @param float|null $sale_price Sale price of the product, or null if not on sale.
 * @param float|null $savings Amount saved on the sale price, or null if not on sale.
 * @return string HTML string displaying the product prices.
 */
function prwfr_get_product_prices($regular_price, $sale_price, $savings)
{

	if ($sale_price) {
		$temp = '<p style="font-size: 14px; font-weight: 400; line-height: 18px;">
			Regular Price: <span style="color: red; font-size: 16px; font-weight: 400; text-decoration: line-through;">' . wc_price($regular_price) . '</span> <br/>
			Sale Price: <span style="color: red; font-size: 14px; font-weight: 600; text-decoration:none;">' . wc_price($sale_price) . '</span><br/>
			You Save: <span style="color: red; font-size: 14px; font-weight: 400; text-decoration:none;">' . wc_price($savings) . '</span><br/>
		</p>';
	} else {
		$temp = '<p style="font-size: 14px; font-weight: 400; line-height: 18px;">
			Regular Price: <span style="color: red; font-size: 16px; font-weight: 400;">' . wc_price($regular_price) . '</span> <br/>
		</p>';
	}

	return $temp;
}

/**
 * Custom comparison function used for sorting products by price.
 *
 * This function compares the prices of two products and determines their relative order for sorting.
 *
 * @param int $product_id_a The ID of the first product.
 * @param int $product_id_b The ID of the second product.
 * @return int Returns 0 if the prices are equal, 1 if the price of the first product is greater, and -1 if the price of the second product is greater.
 */
function prwfr_compare_product_prices($product_id_a, $product_id_b)
{
	$product_a = wc_get_product($product_id_a);
	$product_b = wc_get_product($product_id_b);

	$price_a = $product_a ? $product_a->get_price() : 0;
	$price_b = $product_b ? $product_b->get_price() : 0;

	if ($price_a == $price_b) {
		return 0;
	}

	return ($price_a < $price_b) ? 1 : -1;
}


/**
 * Function to display rating for every product.
 *
 * @param string $id .
 */
function prwfr_star_rating_for_product($id)
{
	$product       = wc_get_product($id);
	$rating        = $product->get_average_rating();
	$reviews_count = $product->get_review_count();
	$unchecked     = 0;

	$rating  = floatval($rating);
	$checked = floor($rating); // 3

	$half = $rating - $checked; // 0.6

	if ($half && ($rating - $checked) > 0.6) {
		$checked   = round($rating);
		$half      = 0;
		$unchecked = 5 - $checked;
	} else {
		$unchecked = 5 - ($checked + 1);
	}

	for ($i = 0; $i < $checked; $i++) {
	?>
		<span class="fa fa-star prwfr-rating-star-checked"></span>
	<?php
	}

	if ($half && (0.4 <= $half && $half <= 0.6)) {
	?>
		<span class="fa fa-star prwfr-rating-star-half"></span>
	<?php
	} else {
		$unchecked = 5 - $checked;
	}
	for ($i = 0; $i < $unchecked; $i++) {
	?>
		<span class="fa fa-star prwfr-rating-star-unchecked"></span>
	<?php
	}
	?>
	<style>
		.prwfr-rating-star-checked {
			color: #ffb136;
		}

		.prwfr-rating-star-half {
			background: linear-gradient(to right, #ffb136 50%, #d3d3d3 50%);
			background-clip: text;
			-webkit-background-clip: text;
			-webkit-text-fill-color: transparent;
		}

		.prwfr-rating-star-unchecked {
			color: #d3d3d3;
		}
	</style>
	<?php
	echo '<span class = "prwfr-product-review-count">(' . esc_html($reviews_count) . ')</span>';
}

/**
 * Function to get price of product with currency symbol
 *
 * @param mixed $product .
 */
function prwfr_get_product_price($product)
{

	if ('grouped' !== $product->get_type()) {
	?>
		<div class="prwfr-price-container" style="padding-top: 5px;">
			<?php
			if ($product->is_on_sale()) {
				if ($product->is_type('variable')) {
			?>
					<div>
						<s style="padding-right: 7px;">
							<?php
							echo wp_kses_post(wc_price(intval($product->get_variation_regular_price())));
							?>
						</s>
						<span class="prwfr-product-sale-price">
							<?php
							echo wp_kses_post(wc_price(intval($product->get_variation_sale_price())));
							?>
						</span>
					</div>
				<?php
				} else {
				?>
					<div>
						<s style="padding-right: 7px;">
							<?php
							echo wp_kses_post(wc_price(intval($product->get_regular_price())));
							?>
						</s>
						<span class="prwfr-product-sale-price">
							<?php
							echo wp_kses_post(wc_price(intval($product->get_sale_price())));
							?>
						</span>
					</div>
			<?php
				}
			} else {
				echo '<div>' . wp_kses_post(wc_price(intval($product->get_price()))) . '</div>';
			}
			?>
		</div>
<?php
	} else {
		$child_products = $product->get_children();

		if (is_array($child_products)) {
			$child_products = array();
		}

		$prices = array();
		foreach ($child_products as $child_id) {
			$child_product = wc_get_product($child_id);
			$prices[]      = $child_product->get_price();
		}

		// You can now use the $prices array to determine the price range or other pricing information.
		// For example, to get the minimum and maximum prices.
		$min_price = min($prices);
		$max_price = max($prices);
		echo wp_kses_post(wc_price($min_price)) . ' - ' . wp_kses_post(wc_price($max_price));
	}
}
