<?php

/**
 * Carousel Container shortcode template.
 *
 * @package Total WordPress Theme
 * @subpackage Total Theme Core
 * @version 2.6
 */

defined( 'ABSPATH' ) || exit;

if ( empty( $content ) ) {
	return '';
}

$wrap_class = [
	'vcex-carousel-container',
	'vcex-module',
];

$html         = '';
$is_vc_inline = vcex_vc_is_inline();
$items        = (string) ( ! empty( $atts['items'] ) ? absint( $atts['items'] ) : 4 );
$equal_height = vcex_validate_att_boolean( 'equal_height', $atts, true );

// Render Carousel
if ( $is_vc_inline ) {
	$carousel_data = '';
	$wrap_class[] = 'wpex-grid';
	if ( ! $equal_height ) {
		$wrap_class[] = 'wpex-items-start';
	}
	if ( empty( $atts['items_margin'] ) || 'none' !== $atts['items_margin'] ) {
		$wrap_class[] = 'wpex-gap-15';
	}
	$wrap_class[] = "wpex-grid-cols-{$items}";
} else {
	$wrap_class[] = 'wpex-carousel';

	if ( $equal_height ) {
		$wrap_class[] = 'wpex-carousel--flex';
	}

	if ( isset( $atts['arrows_position'] )
		&& 'abs' === $atts['arrows_position']
		&& vcex_validate_att_boolean( 'arrows_on_hover', $atts )
	) {
		$wrap_class[] = 'wpex-carousel--arrows-hover';
	}

	vcex_enqueue_carousel_scripts();

	$unique_classname  = vcex_element_unique_classname();
	$carousel_settings = vcex_get_carousel_settings( $atts, 'vcex_carousel_container', false );
	$carousel_css      = vcex_get_carousel_inline_css( $unique_classname, $carousel_settings, 'vcex_carousel_container' );
	$carousel_data     = vcex_carousel_settings_to_json( $carousel_settings );

	if ( ! vcex_validate_att_boolean( 'center', $atts, false )
		&& ( empty( $atts['out_animation'] ) || ( 'fadeOut' !== $atts['out_animation'] ) )
		&& ( ! empty( $carousel_settings['margin'] ) )
	) {
		$wrap_class[] = 'wpex-carousel--offset-fix';
	}

	if ( ! empty( $carousel_css ) ) {
		$wrap_class[] = 'wpex-carousel--render-onload';
		$wrap_class[] = $unique_classname;
		$html .= $carousel_css;
	}

	// Arrow classes
	if ( vcex_validate_att_boolean( 'arrows', $atts, true ) ) {
		$arrows_position = ! empty( $atts['arrows_position'] ) ? sanitize_text_field( $atts['arrows_position'] ) : 'default';
		$arrows_style    = ! empty( $atts['arrows_style'] ) ? sanitize_text_field( $atts['arrows_style'] ) : 'default';

		$wrap_class[] = sanitize_html_class( "arrwstyle-{$arrows_style}" );
		$wrap_class[] = sanitize_html_class( "arrwpos-{$arrows_position}" );
	}

	// Bleed Class
	if ( ! empty( $atts['bleed'] ) && in_array( $atts['bleed'], [ 'end', 'start-end' ], true ) ) {
		$wrap_class[] = "wpex-carousel--bleed-{$atts['bleed']}";
	}
}

// Add extra wrap classes
if ( $extra_classes = vcex_get_shortcode_extra_classes( $atts, 'vcex_carousel_container' ) ) {
	$wrap_class = array_merge( $wrap_class, $extra_classes );
}

// Parse wrap classes
$wrap_class = vcex_parse_shortcode_classes( $wrap_class, 'vcex_carousel_container', $atts );

// Get and combine inner styles so they don't get cloned
$inner_style = '';

// Begin output
$html .= '<div class="' . esc_attr( $wrap_class ) . '"' . vcex_get_unique_id( $atts ) . ' data-wpex-carousel="' . esc_attr( $carousel_data ) . '">';
	if ( $is_vc_inline ) {
		$shortcode_out_safe = do_shortcode( wp_kses_post( $content ) );
		if ( $shortcode_out_safe ) {
			$shortcode_out_safe = str_replace(
				'wpb_animate_when_almost_visible',
				'wpb_animate_when_almost_visible--inactive',
				$shortcode_out_safe
			);
			$html .= $shortcode_out_safe;
		}
	} else {
		// Shortcode regex - only certain shortcodes allowed for this element,
		// this allows us to extract the shortcodes and wrap each one inside it's own container
		$pattern = get_shortcode_regex( [
			'vcex_icon_box',
			'vcex_milestone',
			'vc_column_text',
			'vcex_image',
			'vcex_image_swap',
			'vcex_pricing',
			'vcex_image_banner',
			'vcex_feature_box',
		] );
		if ( preg_match_all( "/$pattern/s", $content, $matches ) ) {
			$inner_html = '';
			// @note we don't use wpex-carousel-slide class because that applies flex styles
			$item_class = 'vcex-carousel-container__item wpex-w-100 wpex-grid';
			if ( ! $equal_height ) {
				$item_class .= ' wpex-self-start';
			}
			// loop through shortcodes
			foreach ( $matches[0] as $shortcode ) {
				$shortcode_out_safe = do_shortcode( wp_kses_post( $shortcode ) );
				if ( $shortcode_out_safe ) {
					$shortcode_out_safe = str_replace(
						'wpb_animate_when_almost_visible',
						'wpb_animate_when_almost_visible--inactive',
						$shortcode_out_safe
					);
					// Get inline styles
					if ( str_contains( $shortcode_out_safe, '<style' ) ) {
						preg_match_all( '#<style\b[^>]*>(.*?)</style>#is', $shortcode_out_safe, $style_matches );
						if ( ! empty( $style_matches ) && ! empty( $style_matches[0] ) ) {
							$shortcode_out_safe = preg_replace( '#<style\b[^>]*>(.*?)</style>#is', '', $shortcode_out_safe );
							$inner_style .= implode( '', array_map( 'trim', $style_matches[1] ) );
						}
					}
					$html .= '<div class="' . esc_attr( $item_class ) . '">' . $shortcode_out_safe . '</div>';
				}
			}
		}
	}
$html .= '</div>';

if ( $inner_style ) {
	// $style is safe because it's extracted from the parsed shortcodes in our allowed list
	$html = "<style>{$inner_style}</style>" . $html;
}

echo $html; // @codingStandardsIgnoreLine
