<?php
/**
 * Shortcode for Products element.
 *
 * @package woodmart
 */

use XTS\Gutenberg\Blocks_Assets;
use XTS\Gutenberg\Post_CSS;

if ( ! defined( 'WOODMART_THEME_DIR' ) ) {
	exit( 'No direct script access allowed' );
}

if ( ! function_exists( 'woodmart_shortcode_products' ) ) {
	/**
	 * Shortcode to display products.
	 *
	 * @param array  $atts    Shortcode attributes.
	 * @param string $content Shortcode content.
	 *
	 * @return string
	 */
	function woodmart_shortcode_products( $atts, $content = '' ) {
		global $product;

		$default_settings = woodmart_get_default_product_shortcode_atts();
		$parsed_atts      = shortcode_atts( $default_settings, $atts );

		extract( $parsed_atts ); // phpcs:ignore.

		$encoded_atts = wp_json_encode(
			array_merge(
				array_map(
					'unserialize',
					array_diff_assoc(
						array_map( 'serialize', $parsed_atts ),
						array_map( 'serialize', $default_settings )
					)
				),
				array(
					'force_not_ajax' => 'no',
				)
			)
		);

		$is_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'yes' !== $force_not_ajax && isset( $_POST['action'] ) && 'woodmart_load_html_dropdowns' !== $_POST['action'] && ! doing_action( 'wp_ajax_woodmart_get_header_html' ) ); // phpcs:ignore.

		$parsed_atts['force_not_ajax'] = 'no'; // :)

		$paged = ( ! empty( $pagination ) && get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

		if ( isset( $_GET['product-page'] ) ) { // phpcs:ignore
			$paged = wc_clean( wp_unslash( $_GET['product-page'] ) ); // phpcs:ignore
		}

		if ( $ajax_page > 1 ) {
			$paged = $ajax_page;
		}

		if ( 'recently_viewed' === $post_type ) {
			$orderby = 'menu_order';
		}

		$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );

		$meta_query = WC()->query->get_meta_query();

		$tax_query = WC()->query->get_tax_query();

		$products_element_classes  = apply_filters( 'vc_shortcodes_css_class', '', '', $parsed_atts );
		$products_element_classes .= $el_class ? ' ' . $el_class : '';

		if ( $parsed_atts['css'] ) {
			$products_element_classes .= ' ' . vc_shortcode_custom_css_class( $parsed_atts['css'] );
		}

		if ( 'wpb' === woodmart_get_current_page_builder() ) {
			$products_element_classes .= ' wd-wpb';
		}

		ob_start();

		if ( 'post__in' === $orderby ) {
			$ordering_args['orderby'] = $orderby;
		}

		$args = array(
			'post_type'           => $query_post_type,
			'post_status'         => 'publish',
			'ignore_sticky_posts' => 1,
			'paged'               => $paged,
			'orderby'             => $ordering_args['orderby'],
			'order'               => $ordering_args['order'],
			'posts_per_page'      => $items_per_page,
			'meta_query'          => $meta_query,
			'tax_query'           => $tax_query, // phpcs:ignore
		);

		if ( ! empty( $ordering_args['meta_key'] ) ) {
			$args['meta_key'] = $ordering_args['meta_key']; // phpcs:ignore
		}

		if ( ! empty( $meta_key ) ) {
			$args['meta_key'] = $meta_key; // phpcs:ignore
		}

		if ( 'ids' === $post_type && '' !== $include ) {
			$args['post__in'] = array_map( 'trim', explode( ',', $include ) );
		}

		if ( ! empty( $exclude ) ) {
			$args['post__not_in'] = array_map( 'trim', explode( ',', $exclude ) );
		}

		if ( ! empty( $taxonomies ) ) {
			$taxonomy_names = get_object_taxonomies( 'product' );
			$terms          = get_terms(
				array(
					'taxonomy'   => $taxonomy_names,
					'orderby'    => 'name',
					'include'    => $taxonomies,
					'hide_empty' => false,
				)
			);

			if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
				if ( 'featured' === $post_type ) {
					$args['tax_query'] = array( 'relation' => 'AND' ); // phpcs:ignore.
				}

				$relation = $query_type ? $query_type : 'OR';

				if ( count( $terms ) > 1 ) {
					$args['tax_query']['categories'] = array( 'relation' => $relation );
				}

				foreach ( $terms as $term ) {
					$args['tax_query']['categories'][] = array(
						'taxonomy'         => $term->taxonomy,
						'field'            => 'slug',
						'terms'            => array( $term->slug ),
						'include_children' => true,
						'operator'         => 'IN',
					);
				}
			}
		}

		if ( 'new' === $post_type ) {
			$meta_query_new_label = array(
				'relation' => 'OR',
				array(
					'key'     => '_woodmart_new_label',
					'value'   => 'on',
					'compare' => 'IN',
				),
				array(
					'key'     => '_woodmart_new_label_date',
					'value'   => gmdate( 'Y-m-d' ),
					'compare' => '>',
					'type'    => 'DATE',
				),
			);

			$days = woodmart_get_opt( 'new_label_days_after_create' );
			if ( $days ) {
				$date_query = new WP_Query(
					array_merge(
						$args,
						array(
							'fields'         => 'ids',
							'posts_per_page' => -1,
							'date_query'     => array(
								'after' => gmdate( 'Y-m-d', strtotime( '-' . $days . ' days' ) ),
							),
						)
					)
				);

				$date_ids = $date_query->posts;

				wp_reset_postdata();

				$meta_query_args                   = $args;
				$meta_query_args['fields']         = 'ids';
				$meta_query_args['posts_per_page'] = -1;
				$meta_query_args['meta_query'][]   = $meta_query_new_label;

				$meta_query_raw = new WP_Query( $meta_query_args );

				$meta_query_ids   = $meta_query_raw->posts;
				$args['post__in'] = array_merge( $date_ids, $meta_query_ids );

				wp_reset_postdata();

				if ( empty( $args['post__in'] ) ) {
					return '';
				}
			} else {
				$args['meta_query'][] = $meta_query_new_label;
			}
		}

		if ( 'featured' === $post_type ) {
			$args['tax_query'][] = array(
				'taxonomy'         => 'product_visibility',
				'field'            => 'name',
				'terms'            => 'featured',
				'operator'         => 'IN',
				'include_children' => false,
			);
		}

		if ( 'yes' === $hide_out_of_stock || ( apply_filters( 'woodmart_hide_out_of_stock_items', false ) && 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && empty( $is_wishlist ) ) ) {
			$args['meta_query'][] = array(
				'key'     => '_stock_status',
				'value'   => 'outofstock',
				'compare' => 'NOT IN',
			);
		}

		if ( ! empty( $order ) ) {
			$args['order'] = $order;
		}

		if ( ! empty( $offset ) ) {
			$args['offset'] = $offset;
		}

		if ( 'sale' === $post_type ) {
			$sale_products = wc_get_product_ids_on_sale();

			if ( ! empty( $args['post__not_in'] ) ) {
				$sale_products = array_diff( $sale_products, $args['post__not_in'] );
			}

			$args['post__in'] = array_merge( array( 0 ), $sale_products );
		}

		if ( 'bestselling' === $post_type ) {
			$args['orderby']  = 'meta_value_num';
			$args['meta_key'] = 'total_sales'; // phpcs:ignore
			$args['order']    = 'DESC';
		}

		if ( empty( $product_hover ) || 'inherit' === $product_hover ) {
			$product_hover_type   = woodmart_get_opt( 'products_hover_type' );
			$is_predefined_hover  = true;
			$product_custom_hover = woodmart_get_opt( 'product_custom_hover' );

			if ( 'custom' === $product_hover_type && $product_custom_hover && 'publish' === get_post_status( $product_custom_hover ) ) {
				$product_hover = 'custom';
			} else {
				$product_hover           = woodmart_get_opt( 'products_hover' );
				$product_custom_hover    = '';
				$stretch_product_desktop = woodmart_get_opt( 'stretch_product_desktop' );
				$stretch_product_tablet  = woodmart_get_opt( 'stretch_product_tablet' );
				$stretch_product_mobile  = woodmart_get_opt( 'stretch_product_mobile' );
			}
		}

		woodmart_set_loop_prop( 'product_hover', 'custom' === $product_hover ? 'base' : $product_hover );
		woodmart_set_loop_prop( 'product_hover_type', 'custom' === $product_hover && $product_custom_hover && 'publish' === get_post_status( $product_custom_hover ) ? 'custom' : 'predefined' );
		woodmart_set_loop_prop( 'product_custom_hover', $product_custom_hover );

		woodmart_set_loop_prop( 'timer', $sale_countdown );
		woodmart_set_loop_prop( 'stretch_product_desktop', $stretch_product_desktop );
		woodmart_set_loop_prop( 'stretch_product_tablet', $stretch_product_tablet );
		woodmart_set_loop_prop( 'stretch_product_mobile', $stretch_product_mobile );
		woodmart_set_loop_prop( 'progress_bar', $stock_progress_bar );
		woodmart_set_loop_prop( 'products_view', $layout );
		woodmart_set_loop_prop( 'is_shortcode', true );
		woodmart_set_loop_prop( 'img_size', $img_size );
		woodmart_set_loop_prop( 'products_columns', $columns );
		woodmart_set_loop_prop( 'products_columns_tablet', $columns_tablet );
		woodmart_set_loop_prop( 'products_columns_mobile', $columns_mobile );
		woodmart_set_loop_prop( 'products_color_scheme', $products_color_scheme );

		if ( 'custom' === $img_size && ! empty( $img_size_custom ) ) {
			woodmart_set_loop_prop( 'img_size_custom', $img_size_custom );
		}

		if ( ! empty( $grid_gallery ) ) {
			woodmart_set_loop_prop( 'grid_gallery', $grid_gallery );
		}

		if ( ! empty( $grid_gallery_enable_arrows ) ) {
			woodmart_set_loop_prop( 'grid_gallery_enable_arrows', $grid_gallery_enable_arrows );
		}

		if ( ! empty( $grid_gallery_control ) ) {
			woodmart_set_loop_prop( 'grid_gallery_control', $grid_gallery_control );
		}
		if ( $product_quantity ) {
			woodmart_set_loop_prop( 'product_quantity', 'enable' === $product_quantity );
		}
		if ( $products_masonry ) {
			woodmart_set_loop_prop( 'products_masonry', 'enable' === $products_masonry );
		}
		if ( $products_different_sizes ) {
			woodmart_set_loop_prop( 'products_different_sizes', 'enable' === $products_different_sizes );
		}
		if ( ! empty( $grid_items_different_sizes ) ) {
			woodmart_set_loop_prop( 'grid_items_different_sizes', explode( ',', $grid_items_different_sizes ) );
		}

		if ( isset( $_GET['orderby'] ) && 'yes' === $shop_tools ) { // phpcs:ignore
			$element_orderby = wc_clean( wp_unslash( $_GET['orderby'] ) ); // phpcs:ignore

			if ( 'date' === $element_orderby ) {
				$args['orderby'] = 'date';
				$args['order']   = 'DESC';
			} elseif ( 'price-desc' === $element_orderby ) {
				$args['orderby'] = 'price';
				$args['order']   = 'DESC';
			} else {
				$args['orderby'] = $element_orderby;
				$args['order']   = 'ASC';
			}
		}

		if ( 'price' === $args['orderby'] ) {
			$args['orderby']  = 'meta_value_num';
			$args['meta_key'] = '_price'; // phpcs:ignore
		}

		if ( isset( $_GET['per_page'] ) && 'yes' === $shop_tools ) { // phpcs:ignore
			$args['posts_per_page'] = (int) wc_clean( wp_unslash( $_GET['per_page'] ) ); // phpcs:ignore
		}

		if ( isset( $_GET['shop_view'] ) && 'yes' === $shop_tools ) { // phpcs:ignore
			$shop_view = sanitize_key( wp_unslash( $_GET['shop_view'] ) ); // phpcs:ignore

			if ( in_array( $shop_view, array( 'grid', 'list' ), true ) ) {
				woodmart_set_loop_prop( 'products_view', $shop_view );
			}
		}

		if ( isset( $_GET['per_row'] ) && 'yes' === $shop_tools ) { // phpcs:ignore
			$columns = (int) wc_clean( wp_unslash( $_GET['per_row'] ) ); // phpcs:ignore
			woodmart_set_loop_prop( 'products_columns', $columns );
		}

		if ( 'upsells' === $post_type && $product ) {
			$args['post__in']  = array_merge( array( 0 ), $product->get_upsell_ids() );
			$args['post_type'] = array( 'product', 'product_variation' );
		}

		if ( 'related' === $post_type && $product ) {
			$args['post__in']  = wc_get_related_products( $product->get_id(), (int) $args['posts_per_page'], $product->get_upsell_ids() );
			$args['post_type'] = array( 'product', 'product_variation' );
		}

		if ( 'cross-sells' === $post_type && is_object( WC()->cart ) ) {
			$cross_sells = WC()->cart->get_cross_sells();

			$args['post_type'] = array( 'product', 'product_variation' );
			$args['post__in']  = array_merge( array( 0 ), $cross_sells );
		}

		if ( 'recently_viewed' === $post_type ) {
			if ( 'yes' === $ajax_recently_viewed ) {
				woodmart_enqueue_js_script( 'product-recently-viewed' );
			}

			$viewed_products = ! empty( $_COOKIE['woodmart_recently_viewed_products'] ) ? explode( '|', wp_unslash( $_COOKIE['woodmart_recently_viewed_products'] ) ) : array(); // phpcs:ignore

			if ( ! $viewed_products && 'grid' === $layout && in_array( $product_hover, array( 'inherit', 'base', 'fw-button' ), true ) ) {
				woodmart_enqueue_js_script( 'product-hover' );
				woodmart_enqueue_js_script( 'product-more-description' );
			}

			$args['post__in'] = array_merge( array( 0 ), $viewed_products );
			$args['orderby']  = 'post__in';

			if ( woodmart_get_opt( 'show_single_variation' ) && woodmart_get_opt( 'hide_variation_parent' ) ) {
				$args['wd_show_variable_products'] = true;
			}
		}

		if ( 'top_rated_products' === $post_type ) {
			add_filter( 'posts_clauses', 'woodmart_order_by_rating_post_clauses' );
			$products = new WP_Query( apply_filters( 'woodmart_product_element_query_args', $args ) );
			remove_filter( 'posts_clauses', 'woodmart_order_by_rating_post_clauses' );
		} else {
			$products = new WP_Query( apply_filters( 'woodmart_product_element_query_args', $args ) );
		}

		wc_set_loop_prop( 'total', $products->found_posts );
		wc_set_loop_prop( 'total_pages', $products->max_num_pages );
		wc_set_loop_prop( 'current_page', $products->query['paged'] );
		wc_set_loop_prop( 'is_shortcode', true );

		WC()->query->remove_ordering_args();

		$parsed_atts['custom_sizes'] = apply_filters( 'woodmart_products_shortcode_custom_sizes', false );

		if ( 'small' === $product_hover ) {
			woodmart_enqueue_inline_style( 'woo-prod-loop-small' );
		}

		if ( woodmart_get_opt( 'quick_shop_variable' ) ) {
			if ( 'variation_form' === woodmart_get_opt( 'quick_shop_variable_type', 'select_options' ) ) {
				woodmart_enqueue_js_script( 'quick-shop-with-form' );
			} else {
				woodmart_enqueue_js_script( 'quick-shop' );
				woodmart_enqueue_js_script( 'swatches-variations' );
			}

			woodmart_enqueue_js_script( 'add-to-cart-all-types' );
			wp_enqueue_script( 'wc-add-to-cart-variation' );
		}

		if ( '' === $spacing ) {
			$spacing = woodmart_get_opt( 'products_spacing' );

			if ( '' === $spacing_tablet ) {
				$spacing_tablet = woodmart_get_opt( 'products_spacing_tablet' );
			}
			if ( '' === $spacing_mobile ) {
				$spacing_mobile = woodmart_get_opt( 'products_spacing_mobile' );
			}
		}

		// Simple products carousel.
		if ( 'carousel' === $layout ) {
			if ( ( 'auto' !== $slides_per_view_tablet && ! empty( $slides_per_view_tablet ) ) || ( 'auto' !== $slides_per_view_mobile && ! empty( $slides_per_view_mobile ) ) ) {
				$parsed_atts['custom_sizes'] = array(
					'desktop' => $slides_per_view,
					'tablet'  => $slides_per_view_tablet,
					'mobile'  => $slides_per_view_mobile,
				);
			}

			if ( 'wpb' === woodmart_get_current_page_builder() ) {
				$parsed_atts['wrapper_classes'] .= ' wd-wpb';
			}

			woodmart_enqueue_product_loop_styles( $product_hover );

			echo woodmart_generate_posts_slider( //phpcs:ignore
				wp_parse_args(
					array(
						'spacing'        => ! $highlighted_products ? $spacing : '',
						'spacing_tablet' => ! $highlighted_products ? $spacing_tablet : '',
						'spacing_mobile' => ! $highlighted_products ? $spacing_mobile : '',
						'inner_content'  => $content,
					),
					$parsed_atts
				),
				$products
			);

			$output = ob_get_clean();

			if ( $is_ajax ) {
				$output = array(
					'items'  => $output,
					'status' => ( $products->max_num_pages > $paged ) ? 'have-posts' : 'no-more-posts',
				);
			}

			return $output;
		}

		if ( empty( $items_per_page ) ) {
			$items_per_page = 12;
		}

		if ( 'arrows' !== $pagination ) {
			woodmart_set_loop_prop( 'woocommerce_loop', $items_per_page * ( $paged - 1 ) );
		}

		if ( 'list' === $layout ) {
			$product_hover = 'list';
			$class        .= ' elements-list';

			if ( '' === $list_spacing ) {
				$list_spacing = woodmart_get_opt( 'products_list_spacing', 30 );

				if ( '' === $list_spacing_tablet ) {
					$list_spacing_tablet = woodmart_get_opt( 'products_list_spacing_tablet' );
				}
				if ( '' === $list_spacing_mobile ) {
					$list_spacing_mobile = woodmart_get_opt( 'products_list_spacing_mobile' );
				}
			}

			$style_attrs = woodmart_get_grid_attrs(
				array(
					'columns'        => 1,
					'columns_tablet' => 1,
					'columns_mobile' => 1,
					'spacing'        => $list_spacing,
					'spacing_tablet' => $list_spacing_tablet,
					'spacing_mobile' => $list_spacing_mobile,
				)
			);
		} else {
			$style_attrs = woodmart_get_grid_attrs(
				array(
					'columns'        => woodmart_loop_prop( 'products_columns' ),
					'columns_tablet' => woodmart_loop_prop( 'products_columns_tablet' ),
					'columns_mobile' => woodmart_loop_prop( 'products_columns_mobile' ),
					'spacing'        => ! $highlighted_products ? $spacing : '',
					'spacing_tablet' => ! $highlighted_products ? $spacing_tablet : '',
					'spacing_mobile' => ! $highlighted_products ? $spacing_mobile : '',
					'post_type'      => 'product',
				)
			);

			$class .= ' grid-columns-' . $columns;
			$class .= ' elements-grid';
		}

		if ( $pagination ) {
			$class .= ' pagination-' . $pagination;
		}

		if ( 'yes' === $shop_tools ) {
			woodmart_enqueue_inline_style( 'woo-shop-el-order-by' );
			woodmart_enqueue_inline_style( 'woo-shop-el-products-per-page' );
			woodmart_enqueue_inline_style( 'woo-shop-el-products-view' );
			woodmart_enqueue_inline_style( 'woo-mod-shop-loop-head' );
		}

		if ( woodmart_loop_prop( 'products_masonry' ) ) {
			$class .= ' grid-masonry';
			wp_enqueue_script( 'imagesloaded' );
			woodmart_enqueue_js_library( 'isotope-bundle' );
			woodmart_enqueue_js_script( 'shop-masonry' );
		}

		if ( woodmart_loop_prop( 'products_masonry' ) || woodmart_loop_prop( 'products_different_sizes' ) ) {
			$class .= ' wd-grid-f-col';
		} else {
			$class .= ' wd-grid-g';
		}

		$products_element_classes .= $highlighted_products ? ' wd-highlighted-products' : '';
		if ( $highlighted_products ) {
			woodmart_enqueue_inline_style( 'highlighted-product' );
		}
		$products_element_classes .= ( $element_title ) ? ' with-title' : '';
		$products_element_classes .= $el_class ? ' ' . $el_class : '';
		$products_element_classes .= ! empty( $wrapper_classes ) ? ' ' . trim( $wrapper_classes ) : '';
		$products_element_classes .= ! $products->have_posts() ? ' wd-hide' : '';

		if ( 'yes' === $lazy_loading ) {
			woodmart_lazy_loading_init( true );
			woodmart_enqueue_inline_style( 'lazy-loading' );
		}

		if ( 'custom' === $product_hover && ! empty( $product_custom_hover ) && 'publish' === get_post_status( $product_custom_hover ) ) {
			if ( woodmart_is_gutenberg_blocks_enabled() ) {
				echo Blocks_Assets::get_instance()->get_inline_scripts( $product_custom_hover ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo Post_CSS::get_instance()->get_inline_blocks_css( $product_custom_hover ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}

			woodmart_enqueue_inline_style( 'woo-loop-prod-builder' );

			if ( ! $is_predefined_hover ) {
				woodmart_set_loop_prop( 'products_color_scheme', 'default' );
			}

			$class .= ' wd-loop-builder-on';
			$class .= ' wd-loop-item-wrap-' . $product_custom_hover;

			if ( get_post_meta( $product_custom_hover, 'wd_bordered_grid', true ) ) {
				woodmart_enqueue_inline_style( 'bordered-product' );

				$class .= ' products-bordered-grid';
			}

			if ( get_post_meta( $product_custom_hover, 'wd_stretch_product', true ) ) {
				woodmart_enqueue_inline_style( 'woo-opt-stretch-cont' );

				$class .= ' wd-stretch-cont-lg';
			}
			if ( get_post_meta( $product_custom_hover, 'wd_stretch_productTablet', true ) ) {
				woodmart_enqueue_inline_style( 'woo-opt-stretch-cont' );

				$class .= ' wd-stretch-cont-md';
			}
			if ( get_post_meta( $product_custom_hover, 'wd_stretch_productMobile', true ) ) {
				woodmart_enqueue_inline_style( 'woo-opt-stretch-cont' );

				$class .= ' wd-stretch-cont-sm';
			}
		} else {
			$class              .= ' wd-loop-builder-off';
			$is_predefined_hover = true;

			if ( ! empty( $products_divider ) && 'small' === $product_hover && 'list' !== $layout ) {
				woodmart_enqueue_inline_style( 'products-divider' );

				$class .= ' wd-with-divider';
			}

			if ( woodmart_loop_prop( 'product_quantity' ) ) {
				$class .= ' wd-quantity-enabled';
			}

			if ( 'none' !== woodmart_get_opt( 'product_title_lines_limit' ) && 'list' !== $layout ) {
				woodmart_enqueue_inline_style( 'woo-opt-title-limit-predefined' );
				$class .= ' title-line-' . woodmart_get_opt( 'product_title_lines_limit' );
			}

			if ( ( woodmart_loop_prop( 'stretch_product_desktop' ) || woodmart_loop_prop( 'stretch_product_tablet' ) || woodmart_loop_prop( 'stretch_product_mobile' ) ) && in_array( $product_hover, array( 'icons', 'alt', 'button', 'standard', 'tiled', 'quick', 'base', 'fw-button', 'buttons-on-hover' ), true ) ) {
				woodmart_enqueue_inline_style( 'woo-opt-stretch-cont' );
				woodmart_enqueue_inline_style( 'woo-opt-stretch-cont-predefined' );

				if ( woodmart_loop_prop( 'stretch_product_desktop' ) ) {
					$class .= ' wd-stretch-cont-lg';
				}
				if ( woodmart_loop_prop( 'stretch_product_tablet' ) ) {
					$class .= ' wd-stretch-cont-md';
				}
				if ( woodmart_loop_prop( 'stretch_product_mobile' ) ) {
					$class .= ' wd-stretch-cont-sm';
				}
			}
		}

		if ( $is_predefined_hover ) {
			if ( $products_bordered_grid && ! $highlighted_products ) {
				woodmart_enqueue_inline_style( 'bordered-product' );
				woodmart_enqueue_inline_style( 'bordered-product-predefined' );

				if ( 'outside' === $products_bordered_grid_style ) {
					$class .= ' products-bordered-grid';
				} elseif ( 'inside' === $products_bordered_grid_style ) {
					$class .= ' products-bordered-grid-ins';
				}
			}

			if ( 'default' !== $products_color_scheme && ( $products_bordered_grid || 'enable' === $products_bordered_grid ) && 'disable' !== $products_bordered_grid && 'outside' === $products_bordered_grid_style ) {
				$class .= ' wd-bordered-' . $products_color_scheme;
			}

			if ( $products_with_background ) {
				woodmart_enqueue_inline_style( 'woo-opt-products-bg' );

				$class .= ' wd-products-with-bg';
			}

			if ( $products_shadow ) {
				woodmart_enqueue_inline_style( 'woo-opt-products-shadow' );

				$class .= ' wd-products-with-shadow';
			}
		}

		if ( ! $is_ajax ) {
			?>
			<div <?php echo $el_id ? 'id="' . esc_attr( $el_id ) . '" ' : ''; ?>class="wd-products-element<?php echo esc_attr( $products_element_classes ); ?>">
			<?php

			echo do_shortcode( $content );
		}

		// Element title.
		if ( ! $is_ajax && $element_title ) {
			$element_title_tag = in_array( $element_title_tag, array_keys( woodmart_get_allowed_html() ), true ) ? $element_title_tag : 'h4';

			printf(
				'<%1$s class="wd-el-title title element-title">%2$s</%1$s>',
				esc_attr( apply_filters( 'woodmart_products_title_tag', $element_title_tag ) ),
				esc_html( $element_title )
			);
		}

		if ( ! $is_ajax && 'arrows' === $pagination ) {
			woodmart_enqueue_inline_style( 'product-arrows' );
			woodmart_sticky_loader();
		}

		?>

		<?php if ( ! $is_ajax && 'yes' === $shop_tools ) : ?>
			<div class="shop-loop-head">
				<div class="wd-shop-tools">
					<?php woodmart_products_per_page_select( true ); ?>
					<?php woodmart_products_view_select( true ); ?>
					<?php woocommerce_catalog_ordering(); ?>
				</div>
			</div>
		<?php endif; ?>

		<?php

		woodmart_enqueue_product_loop_styles( $product_hover );

		if ( ! $is_ajax && ( $products->have_posts() || 'yes' === $ajax_recently_viewed ) ) {
			?>
			<div class="products wd-products <?php echo esc_attr( $class ); ?>" data-paged="<?php echo esc_attr( $paged ); ?>" data-atts="<?php echo esc_attr( $encoded_atts ); ?>" data-source="shortcode" data-columns="<?php echo esc_attr( $columns ); ?>" style="<?php echo esc_attr( $style_attrs ); ?>">
			<?php
		}

		if ( $products->have_posts() ) :
			while ( $products->have_posts() ) :
				$products->the_post();
				wc_get_template_part( 'content', 'product' );
			endwhile;
		endif;

		woodmart_set_loop_prop( 'shop_pagination', $pagination );

		if ( ! $is_ajax ) {
			?>
			</div>
			<?php
		}

		if ( 'yes' === $lazy_loading ) {
			woodmart_lazy_loading_deinit();
		}

		if ( $products->max_num_pages > 1 && ! $is_ajax && $pagination ) {
			?>
			<?php wp_enqueue_script( 'imagesloaded' ); ?>
			<?php woodmart_enqueue_js_script( 'products-load-more' ); ?>
			<?php if ( 'infinit' === $pagination ) : ?>
				<?php woodmart_enqueue_js_library( 'waypoints' ); ?>
			<?php endif; ?>
			<?php if ( 'more-btn' === $pagination || 'infinit' === $pagination ) : ?>
				<div class="wd-loop-footer products-footer">
					<?php woodmart_enqueue_inline_style( 'load-more-button' ); ?>
					<a href="#" rel="nofollow noopener" class="btn wd-load-more wd-products-load-more load-on-<?php echo 'more-btn' === $pagination ? 'click' : 'scroll'; ?>"><span class="load-more-label"><?php esc_html_e( 'Load more products', 'woodmart' ); ?></span></a>
					<div class="btn wd-load-more wd-load-more-loader"><span class="load-more-loading"><?php esc_html_e( 'Loading...', 'woodmart' ); ?></span></div>
				</div>
			<?php endif; ?>
			<?php if ( 'arrows' === $pagination ) : ?>
				<?php
				$arrows_classes = ' wd-ajax-arrows';

				if ( $highlighted_products ) {
					$arrows_classes .= ' wd-custom-style';
				} else {
					$arrows_hover_style = woodmart_get_opt( 'carousel_arrows_hover_style', '1' );

					if ( 'disable' !== $arrows_hover_style ) {
						$arrows_classes .= ' wd-hover-' . $arrows_hover_style;
					}
				}

				if ( ! empty( $pagination_arrows_position ) ) {
					$arrows_classes .= ' wd-pos-' . $pagination_arrows_position;
				} elseif ( $highlighted_products ) {
					if ( $element_title ) {
						$arrows_classes .= ' wd-pos-together';
					} else {
						$arrows_classes .= ' wd-pos-sep';
					}
				} else {
					$arrows_classes .= ' wd-pos-' . woodmart_get_opt( 'carousel_arrows_position', 'sep' );
				}

				woodmart_enqueue_inline_style( 'product-arrows' );

				woodmart_get_carousel_nav_template(
					$arrows_classes,
					array(
						'tabindex'      => 0,
						'inner_classes' => ' wd-role-btn',
					)
				);
				?>
			<?php endif; ?>
			<?php if ( 'links' === $pagination ) : ?>
				<?php woocommerce_pagination(); ?>
			<?php endif ?>
			<?php
		}

		wc_reset_loop();
		wp_reset_postdata();
		woodmart_reset_loop();

		if ( ( ! $is_ajax && $products->have_posts() ) || 'yes' === $ajax_recently_viewed ) {
			echo '</div>';
		}

		$output = ob_get_clean();

		if ( $is_ajax ) {
			$output = array(
				'items'  => $output,
				'status' => ( $products->max_num_pages > $paged ) ? 'have-posts' : 'no-more-posts',
			);
		}

		return $output;
	}
}

if ( ! function_exists( 'woodmart_order_by_rating_post_clauses' ) ) {
	/**
	 * Order products by rating.
	 *
	 * @param array $args Query args.
	 *
	 * @return array
	 */
	function woodmart_order_by_rating_post_clauses( $args ) {
		global $wpdb;

		$args['where']  .= " AND $wpdb->commentmeta.meta_key = 'rating' ";
		$args['join']   .= "LEFT JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)";
		$args['orderby'] = "$wpdb->commentmeta.meta_value DESC";
		$args['groupby'] = "$wpdb->posts.ID";

		return $args;
	}
}

if ( ! function_exists( 'woodmart_get_default_product_shortcode_atts' ) ) {
	/**
	 * Get default product shortcode attributes.
	 *
	 * @return array
	 */
	function woodmart_get_default_product_shortcode_atts() {
		return array_merge(
			woodmart_get_carousel_atts(),
			array(
				'element_title'                => '',
				'element_title_tag'            => 'h4',
				'post_type'                    => 'product',
				'layout'                       => 'grid',
				'include'                      => '',
				'custom_query'                 => '',
				'taxonomies'                   => '',
				'pagination'                   => '',
				'items_per_page'               => 12,
				'product_hover'                => 'inherit',
				'product_custom_hover'         => '',
				'spacing'                      => '',
				'spacing_tablet'               => '',
				'spacing_mobile'               => '',
				'list_spacing'                 => '',
				'list_spacing_tablet'          => '',
				'list_spacing_mobile'          => '',
				'columns'                      => 4,
				'columns_tablet'               => 'auto',
				'columns_mobile'               => 'auto',
				'sale_countdown'               => 0,
				'stretch_product_desktop'      => 0,
				'stretch_product_tablet'       => 0,
				'stretch_product_mobile'       => 0,
				'stock_progress_bar'           => 0,
				'highlighted_products'         => 0,
				'products_divider'             => 0,
				'products_bordered_grid'       => 0,
				'products_bordered_grid_style' => 'outside',
				'products_with_background'     => 0,
				'products_shadow'              => 0,
				'products_color_scheme'        => 'default',
				'product_quantity'             => 0,
				'grid_gallery'                 => '',
				'grid_gallery_control'         => '',
				'grid_gallery_enable_arrows'   => '',
				'offset'                       => '',
				'orderby'                      => '',
				'query_type'                   => 'OR',
				'order'                        => '',
				'meta_key'                     => '', // phpcs:ignore
				'exclude'                      => '',
				'class'                        => '',
				'ajax_page'                    => '',
				'img_size'                     => 'woocommerce_thumbnail',
				'img_size_custom'              => array(),
				'force_not_ajax'               => 'no',
				'products_masonry'             => woodmart_get_opt( 'products_masonry' ),
				'products_different_sizes'     => woodmart_get_opt( 'products_different_sizes' ),
				'grid_items_different_sizes'   => '',
				'lazy_loading'                 => 'no',
				'el_class'                     => '',
				'shop_tools'                   => 'no',
				'query_post_type'              => 'product',
				'hide_out_of_stock'            => 'no',
				'css'                          => '',
				'woodmart_css_id'              => '',
				'ajax_recently_viewed'         => 'no',
				'pagination_arrows_position'   => '',
				'is_wishlist'                  => '',
				'wrapper_classes'              => '',
				'el_id'                        => '',
				'inner_content'                => '',
				'is_predefined_hover'          => false,
			)
		);
	}
}
