<?php 

/**
* WPBakery Page Builder Stockie Pricing table shortcode
*/

add_shortcode( 'stockie_pricing_table', 'stockie_pricing_table_func' );

function stockie_pricing_table_func( $atts ) {
	if ( isset( $atts ) && is_array( $atts ) ) extract( $atts );

	// Default values, parsing and filtering
	$title = isset( $title ) ? StockieExtraFilter::string( $title ) : false;
	$subtitle = isset( $subtitle ) ? StockieExtraFilter::string( $subtitle ) : false;
	$description = isset( $description ) ? StockieExtraFilter::string( $description, 'textarea', '' ) : '';
	$price = isset( $price ) ? StockieExtraFilter::string( $price, 'string', '' ) : '';
	$price_currency = isset( $price_currency ) ? StockieExtraFilter::string( $price_currency ) : false;
	$price_caption = isset( $price_caption ) ? StockieExtraFilter::string( $price_caption ) : false;
	$features_type = isset( $features_type ) ? StockieExtraFilter::string( $features_type, 'string', 'default' ) : 'default';
	$features_value = isset( $features_value ) ? json_decode( urldecode( StockieExtraFilter::string( $features_value ) ) ) : false;
	$add_button = isset( $add_button ) ? StockieExtraFilter::boolean( $add_button ) : false;

	$title_typo = isset( $title_typo ) ? StockieExtraFilter::string( $title_typo ) : false;
	$subtitle_typo = isset( $subtitle_typo ) ? StockieExtraFilter::string( $subtitle_typo ) : false;
	$description_typo = isset( $description_typo ) ? StockieExtraFilter::string( $description_typo ) : false;
	$price_typo = isset( $price_typo ) ? StockieExtraFilter::string( $price_typo ) : false;
	$features_title_typo = isset( $features_title_typo ) ? StockieExtraFilter::string( $features_title_typo ) : false;
	$features_subtitle_typo = isset( $features_subtitle_typo ) ? StockieExtraFilter::string( $features_subtitle_typo ) : false;

	$bg_color = isset( $bg_color ) ? StockieExtraFilter::string( $bg_color ) : false;
	$borders_color = isset( $borders_color ) ? StockieExtraFilter::string( $borders_color ) : false;
	$title_color = isset( $title_color ) ? StockieExtraFilter::string( $title_color ) : false;
	$subtitle_color = isset( $subtitle_color ) ? StockieExtraFilter::string( $subtitle_color ) : 'brand';
	$description_color = isset( $description_color ) ? StockieExtraFilter::string( $description_color ) : false;
	$price_color = isset( $price_color ) ? StockieExtraFilter::string( $price_color ) : false;
	$price_caption_color = isset( $price_caption_color ) ? StockieExtraFilter::string( $price_caption_color ) : false;
	$price_caption_bg_color = isset( $price_caption_bg_color ) ? StockieExtraFilter::string( $price_caption_bg_color ) : false;
	$features_titles_color = isset( $features_titles_color ) ? StockieExtraFilter::string( $features_titles_color ) : false;
	$features_icons_color = isset( $features_icons_color ) ? StockieExtraFilter::string( $features_icons_color ) : false;
	$features_disabled_titles_color = isset( $features_disabled_titles_color ) ? StockieExtraFilter::string( $features_disabled_titles_color ) : false;
	$features_disabled_icons_color = isset( $features_disabled_icons_color ) ? StockieExtraFilter::string( $features_disabled_icons_color ) : false;
	$readmore_button = isset( $readmore_button ) ? StockieExtraFilter::string( $readmore_button ) : 'type=outline';
	
	$appearance_effect = isset( $appearance_effect ) ? StockieExtraFilter::string( $appearance_effect, 'attr', 'none' )  : 'none';
	$appearance_duration = isset( $appearance_duration ) ? StockieExtraFilter::string( $appearance_duration, 'attr', false )  : false;
	if ( $appearance_duration ) $appearance_duration = intval( str_replace( 'ms', '', $appearance_duration ) );

	$css_class = isset( $css_class ) ? ' ' . StockieExtraFilter::string( $css_class, 'attr', '' )  : '';

	if ( isset( $button_link ) ) {
		$button_link = StockieExtraParser::VC_link_params( $button_link, array( 'caption' => __( 'Read More', 'stockie-extra' ) ) );
	} else {
		$button_link = StockieExtraParser::VC_link_params( '', array( 'caption' => __( 'Read More', 'stockie-extra' ) ) );
	}

	if ( $features_value ) {
		foreach ($features_value as $feature_key => $feature_value) {
			if ( isset( $feature_value->feature_title ) ) {
				$features_value[$feature_key]->feature_title = StockieExtraFilter::string( $feature_value->feature_title );
			} else {
				$features_value[$feature_key]->feature_title = false;
			}
			if ( isset( $feature_value->feature_icon ) ) {
				$features_value[$feature_key]->feature_icon = StockieExtraFilter::string( $feature_value->feature_icon, 'attr' );
			} else {
				$features_value[$feature_key]->feature_icon = false;
			}
		}
	}

	if ( isset( $features_value_type3 ) && $features_value_type3 ) {
		foreach ($features_value_type3 as $feature_key => $feature_value) {
			if ( isset( $feature_value->feature_icon ) ) {
				$features_value_type3[$feature_key]->feature_icon = StockieExtraFilter::string( $feature_value->feature_icon, 'string', 'yes' );
			} else {
				$features_value_type3[$feature_key]->feature_icon = 'yes';
			}
			$GLOBALS['stockie_icon_fonts'][] = 'my-icon-ui-cross';
		}
	}

	// Styling
	$pricing_table_uniqid = uniqid( 'stockie-custom-' );

	$pricing_table_settings = StockieExtraParser::VC_color_to_CSS( $bg_color, 'background-color:{{color}};' );
	$borders_settings = StockieExtraParser::VC_color_to_CSS( $borders_color, 'border-color:{{color}};' );
	$title_settings = StockieExtraParser::VC_color_to_CSS( $title_color, 'color:{{color}};' );
	$subtitle_settings = StockieExtraParser::VC_color_to_CSS( $subtitle_color, 'color:{{color}};' );
	$description_settings = StockieExtraParser::VC_color_to_CSS( $description_color, 'color:{{color}};' );
	$price_settings = StockieExtraParser::VC_color_to_CSS( $price_color, 'color:{{color}};' );
	$price_caption_settings = StockieExtraParser::VC_color_to_CSS( $price_caption_color, 'color:{{color}};' );
	$price_caption_bg_settings = StockieExtraParser::VC_color_to_CSS( $price_caption_bg_color, 'background-color:{{color}};' );
	$price_caption_settings .= $price_caption_bg_settings;
	$features_titles_settings = StockieExtraParser::VC_color_to_CSS( $features_titles_color, 'color:{{color}};' );
	$features_icons_settings = StockieExtraParser::VC_color_to_CSS( $features_icons_color, 'color:{{color}};' );
	$features_disabled_titles_settings = StockieExtraParser::VC_color_to_CSS( $features_disabled_titles_color, 'color:{{color}};' );
	$features_disabled_icons_settings = StockieExtraParser::VC_color_to_CSS( $features_disabled_icons_color, 'color:{{color}};' );

	// Read more button
	$readmore_button = preg_replace( '/\&amp\;/', '&', $readmore_button );
	parse_str( $readmore_button, $button_settings );
	$button_css = StockieExtraParser::VC_button_to_css( $button_settings, (bool)( $readmore_button ) );

	$title_settings .= StockieExtraParser::VC_typo_to_CSS( $title_typo );
	$subtitle_settings .= StockieExtraParser::VC_typo_to_CSS( $subtitle_typo );
	$description_settings .= StockieExtraParser::VC_typo_to_CSS( $description_typo );
	$price_settings .= StockieExtraParser::VC_typo_to_CSS( $price_typo );
	$features_titles_settings .= StockieExtraParser::VC_typo_to_CSS( $features_title_typo );

	StockieExtraParser::VC_typo_custom_font( $title_typo );
	StockieExtraParser::VC_typo_custom_font( $subtitle_typo );
	StockieExtraParser::VC_typo_custom_font( $description_typo );
	StockieExtraParser::VC_typo_custom_font( $price_typo );
	StockieExtraParser::VC_typo_custom_font( $features_title_typo );
	StockieExtraParser::VC_typo_custom_font( $features_subtitle_typo );

	$icons_collection = array();

	// Assembling
	include( plugin_dir_path( __FILE__ ) . 'pricing_table__style.php' );
	ob_start();
	include( plugin_dir_path( __FILE__ ) . 'pricing_table__view.php' );
	return ob_get_clean();
}